int main (int argc, char **argv) { /* Local Vars */ MYSQL *conn; MYSQL_RES *result; MYSQL_ROW row; int ret; int i; const char *server_version; struct timeval start_time; double time_delta; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); gettimeofday(&start_time, NULL); /* Connectiong to mysqld */ conn = mp_mysql_init(); /* Get server version */ server_version = mysql_get_server_info(conn); /* Get status info */ if (mp_showperfdata) { ret = mysql_query(conn, "SHOW /*!50002 GLOBAL */ STATUS;"); if (ret != 0) critical("Query 'SHOW GLOBAL STATUS' failed: %s", mysql_error(conn)); result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { for (i=0; i < variables; i++) { if (strcmp(variable[i], row[0]) == 0) { mp_perfdata_int(row[0], strtol(row[1], NULL, 10), unit[i], NULL); } } } mysql_free_result(result); } mp_mysql_deinit(conn); time_delta = mp_time_delta(start_time); mp_perfdata_float("time", (float)time_delta, "s", NULL); ok("MySQL v%s", server_version); critical("You should never reach this point."); }
int main (int argc, char **argv) { /* Local Vars */ int rv; pingobj_t *oping; pingobj_iter_t *iter; uint32_t dropped=0, num=0; int ttl=0; double rt, rta=0; char haddr[40]; size_t data_len, buf_len; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); /* Init liboping */ oping = ping_construct(); if (oping == NULL) unknown("Can't initialize liboping"); rv = ping_setopt(oping, PING_OPT_AF, &ipv); if (rv != 0) unknown("liboping setup Error: %s", ping_get_error(oping)); rv = ping_setopt(oping, PING_OPT_DATA, "monitoringplug/check_oping based on liboping. "); if (rv != 0) unknown("liboping setup Error: %s", ping_get_error(oping)); if (interface) { rv = ping_setopt(oping, PING_OPT_DEVICE, (char *)interface); if (rv != 0) unknown("liboping setup Error: %s", ping_get_error(oping)); } if (ttl) { rv = ping_setopt(oping, PING_OPT_TTL, &ttl); if (rv != 0) unknown("liboping setup Error: %s", ping_get_error(oping)); } rv = ping_host_add(oping, hostname); if (rv != 0) unknown("liboping setup Error: %s", ping_get_error(oping)); for (; packets > 0; packets--) { rv = ping_send(oping); if (rv < 0) critical("Send Error: %s", ping_get_error(oping)); for (iter = ping_iterator_get(oping); iter != NULL; iter = ping_iterator_next(iter)) { buf_len = sizeof(dropped); rv = ping_iterator_get_info(iter, PING_INFO_DROPPED, &dropped, &buf_len); if (rv != 0) unknown("liboping ping_iterator_get_info failed!"); buf_len = sizeof(rt); rv = ping_iterator_get_info(iter, PING_INFO_LATENCY, &rt, &buf_len); if (rv != 0) unknown("liboping ping_iterator_get_info failed!"); rta += rt; buf_len = sizeof(num); rv = ping_iterator_get_info(iter, PING_INFO_SEQUENCE, &num, &buf_len); if (rv != 0) unknown("liboping ping_iterator_get_info failed!"); data_len = 0; rv = ping_iterator_get_info(iter, PING_INFO_DATA, NULL, &data_len); if (rv != 0) unknown("liboping ping_iterator_get_info failed!"); buf_len = sizeof(haddr); rv = ping_iterator_get_info(iter, PING_INFO_ADDRESS, haddr, &buf_len); if (rv != 0) unknown("liboping ping_iterator_get_info failed!"); buf_len = sizeof(ttl); rv = ping_iterator_get_info(iter, PING_INFO_RECV_TTL, &ttl, &buf_len); if (rv != 0) unknown("liboping ping_iterator_get_info failed!"); if (mp_verbose > 0 && ttl > 0) printf("%zu bytes from %s (%s): icmp_seq=%u ttl=%i time=%.2f ms\n", data_len, hostname, haddr, num, ttl, rt); } if (quick && num > 0 && get_status((float)(rta/num), rta_thresholds) == STATE_OK && get_status((dropped*100)/num, lost_thresholds) == STATE_OK) { break; } else if (packets > 1) { if (interval.tv_sec || interval.tv_nsec) nanosleep(&interval, NULL); else sleep(1); } } if (num == 0) critical("PING_INFO_SEQUENCE is 0"); mp_perfdata_float("rta", (float)(rta/num), "s", rta_thresholds); mp_perfdata_int("pl", (int)(dropped*100)/num, "%", lost_thresholds); int result1, result2; result1 = get_status((float)(rta/num), rta_thresholds); result2 = get_status((dropped*100)/num, lost_thresholds); result1 = result1 > result2 ? result1 : result2; free_threshold(rta_thresholds); free_threshold(lost_thresholds); switch(result1) { case STATE_OK: ok("Packet loss = %d%, RTA = %.2f ms", (int)(dropped*100)/num, (float)(rta/num)); break; case STATE_WARNING: warning("Packet loss = %d%, RTA = %.2f ms", (int)(dropped*100)/num, (float)(rta/num)); break; case STATE_CRITICAL: critical("Packet loss = %d%, RTA = %.2f ms", (int)(dropped*100)/num, (float)(rta/num)); break; } critical("You should never reach this point."); }
int main (int argc, char **argv) { /* Local Vars */ CURL *curl; char *url; double size; double time; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); /* Magik */ size_t urllen = strlen(hostname)+strlen(filename) + 9; url = mp_malloc(urllen); snprintf(url, urllen, "tftp://%s/%s", hostname, filename); if (mp_verbose > 0) { printf("CURL Version: %s\n", curl_version()); printf("Try fetch %s\n", url); print_thresholds("fetch_thresholds", fetch_thresholds); } /* Init libcurl */ curl = mp_curl_init(); /* Setup request */ if (curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_TFTP) == CURLE_UNSUPPORTED_PROTOCOL) unknown("libcurl don't support tftp."); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, mp_curl_recv_blackhole); if (port != 0) curl_easy_setopt(curl, CURLOPT_LOCALPORT, port); /* Perform Request */ mp_curl_perform(curl); /* Get metric */ curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &time); curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD , &size); /* Clenup libcurl */ curl_easy_cleanup(curl); curl_global_cleanup(); mp_perfdata_float("time", (float)time, "s", fetch_thresholds); free(url); switch(get_status(time, fetch_thresholds)) { case STATE_OK: free_threshold(fetch_thresholds); ok("Received %'.0fbyte in %fs.", size, time); break; case STATE_WARNING: free_threshold(fetch_thresholds); warning("Received %'.0fbyte in %fs.", size, time); break; case STATE_CRITICAL: free_threshold(fetch_thresholds); critical("Received %'.0fbyte in %fs.", size, time); break; } free_threshold(fetch_thresholds); critical("You should never reach this point."); }
int main (int argc, char **argv) { /* Local Vars */ int state = STATE_OK; char *ups_ident = NULL; long ups_battery_status = LONG_MIN; long ups_seconds_on_battery = LONG_MIN; long ups_remaining_runtime = LONG_MIN; long ups_remaining_charge = LONG_MIN; long ups_battery_voltage = LONG_MIN; long ups_battery_current = LONG_MIN; long ups_battery_temperature = LONG_MIN; long ups_input_line_bads = LONG_MIN; long ups_input_lines = LONG_MIN; long ups_output_source = LONG_MIN; long ups_output_frequency = LONG_MIN; long ups_output_lines = LONG_MIN; long ups_alarms_present = LONG_MIN; char *output = NULL; char buf[64]; netsnmp_session *snmp_session; mp_snmp_query_cmd snmpcmd[] = { {{1,3,6,1,2,1,33,1,1,5,0}, 11, ASN_OCTET_STR, (void *)&ups_ident, 0}, {{1,3,6,1,2,1,33,1,2,1,0}, 11, ASN_INTEGER, (void *)&ups_battery_status, sizeof(long int)}, {{1,3,6,1,2,1,33,1,2,2,0}, 11, ASN_INTEGER, (void *)&ups_seconds_on_battery, sizeof(long int)}, {{1,3,6,1,2,1,33,1,2,3,0}, 11, ASN_INTEGER, (void *)&ups_remaining_runtime, sizeof(long int)}, {{1,3,6,1,2,1,33,1,2,4,0}, 11, ASN_INTEGER, (void *)&ups_remaining_charge, sizeof(long int)}, {{1,3,6,1,2,1,33,1,2,5,0}, 11, ASN_INTEGER, (void *)&ups_battery_voltage, sizeof(long int)}, /* 0.1 volts DC */ {{1,3,6,1,2,1,33,1,2,6,0}, 11, ASN_INTEGER, (void *)&ups_battery_current, sizeof(long int)}, /* 0.1 amps DC */ {{1,3,6,1,2,1,33,1,2,7,0}, 11, ASN_INTEGER, (void *)&ups_battery_temperature, sizeof(long int)}, /* deg C */ {{1,3,6,1,2,1,33,1,3,1,0}, 11, ASN_COUNTER, (void *)&ups_input_line_bads, sizeof(long int)}, {{1,3,6,1,2,1,33,1,3,2,0}, 11, ASN_INTEGER, (void *)&ups_input_lines, sizeof(long int)}, {{1,3,6,1,2,1,33,1,4,1,0}, 11, ASN_INTEGER, (void *)&ups_output_source, sizeof(long int)}, {{1,3,6,1,2,1,33,1,4,2,0}, 11, ASN_INTEGER, (void *)&ups_output_frequency, sizeof(long int)}, /* 0.1 RMS */ {{1,3,6,1,2,1,33,1,4,3,0}, 11, ASN_INTEGER, (void *)&ups_output_lines, sizeof(long int)}, {{1,3,6,1,2,1,33,1,6,1,0}, 11, ASN_GAUGE, (void *)&ups_alarms_present, sizeof(long int)}, {{0}, 0, 0, NULL}, }; /* set threshold defaults */ mp_threshold_set_warning(&threshold_charge, DEFAULT_CHARGE_WARNING, NOEXT); mp_threshold_set_critical(&threshold_charge, DEFAULT_CHARGE_CRITICAL, NOEXT); mp_threshold_set_warning(&threshold_runtime, DEFAULT_RUNTIME_WARNING, NOEXT); mp_threshold_set_critical(&threshold_runtime, DEFAULT_RUNTIME_CRITICAL, NOEXT); /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); snmp_session = mp_snmp_init(); mp_snmp_query(snmp_session, snmpcmd); mp_snmp_deinit(); if (mp_verbose > 1) { printf("battery status: %ld\n", ups_battery_status); printf("output source: %ld\n", ups_output_source); printf("alarms present: %ld\n", ups_alarms_present); printf("input lines: %ld\n", ups_input_lines); printf("output lines: %ld\n", ups_output_lines); printf("input line bads: %ld\n", ups_input_line_bads); } if (ups_ident) { if (*ups_ident != '\0') { mp_snprintf((char *)&buf, sizeof(buf), "[%s] ", ups_ident); mp_strcat(&output, buf); } free(ups_ident); } /* always warning, if on battery */ if ((ups_seconds_on_battery > 0) || (ups_output_source == OUTPUT_BATTERY)) { state = STATE_WARNING; if (ups_seconds_on_battery > 0) { mp_snprintf((char *) &buf, sizeof(buf), "ON BATTERY (since %d sec%s), ", ups_seconds_on_battery, ((ups_seconds_on_battery != 1) ? "s" : "")); mp_strcat(&output, buf); } else { mp_strcat(&output, "ON BATTERY, "); } } /* check alarms */ if ((ups_alarms_present != LONG_MIN) && (ups_alarms_present > 0)) { if (state != STATE_CRITICAL) state = STATE_WARNING; if (ups_alarms_present == 1) mp_strcat(&output, "UPS REPORTS AN ALARM, "); else mp_strcat(&output, "UPS REPORTS MULTIPLE ALARMS, "); } /* check remaining runtime threshold */ if (ups_remaining_runtime > LONG_MIN) { switch (get_status(ups_remaining_runtime, threshold_runtime)) { case STATE_CRITICAL: state = STATE_CRITICAL; mp_strcat(&output, "RUNTIME CRITICAL, "); break; case STATE_WARNING: if (state == STATE_OK) state = STATE_WARNING; mp_strcat(&output, "RUNTIME WARNING, "); break; } /* switch */ } /* check remaining charge threshold */ if (ups_remaining_charge > LONG_MIN) { switch (get_status(ups_remaining_charge, threshold_charge)) { case STATE_CRITICAL: state = STATE_CRITICAL; mp_strcat(&output, "CHARGE CRITICAL, "); break; case STATE_WARNING: if (state == STATE_OK) state = STATE_WARNING; mp_strcat(&output, "CHARGE WARNING, "); break; } /* switch */ } /* check battery status */ switch (ups_battery_status) { case BATTERY_NORMAL: break; case BATTERY_LOW: state = STATE_CRITICAL; break; case BATTERY_DEPLETED: state = STATE_CRITICAL; break; case BATTERY_UNKNOWN: /* FALL-THROUGH */ default: if (state == STATE_OK) state = STATE_UNKNOWN; break; } /* switch */ /* check output source */ switch (ups_output_source) { case OUTPUT_NORMAL: /* DO NOTHING */ break; case OUTPUT_OTHER: /* FALL-THROUGH */ case OUTPUT_BYPASS: /* FALL-THROUGH */ case OUTPUT_BOOSTER: /* FALL-THROUGH */ case OUTPUT_REDUCER: /* FALL-THROUGH */ case OUTPUT_BATTERY: /* FALL-THROUGH */ if (state != STATE_CRITICAL) state = STATE_WARNING; break; case OUTPUT_NONE: state = STATE_CRITICAL; break; default: if (state == STATE_OK) state = STATE_UNKNOWN; break; } /* switch */ /* * status line */ mp_snprintf((char *) &buf, sizeof(buf), "battery: %s", battery_status_to_string(ups_battery_status)); mp_strcat(&output, buf); if ((ups_remaining_runtime > LONG_MIN) && (ups_remaining_charge > LONG_MIN)) { mp_snprintf((char *) &buf, sizeof(buf), "remaining: %ld min%s [%d%%]", ups_remaining_runtime, ((ups_remaining_runtime != 1) ? "s" : ""), (int) ups_remaining_charge); mp_strcat_comma(&output, buf); } else if (ups_remaining_runtime > LONG_MIN) { mp_snprintf((char *) &buf, sizeof(buf), "remaining: %ld min%s", ups_remaining_runtime, ((ups_remaining_runtime != 1) ? "s" : "")); mp_strcat_comma(&output, buf); } else if (ups_remaining_charge > LONG_MIN) { mp_snprintf((char *) &buf, sizeof(buf), "remaining: %d%%", (int) ups_remaining_charge); mp_strcat_comma(&output, buf); } mp_snprintf((char *) &buf, sizeof(buf), "source: %s", output_source_to_string(ups_output_source)); mp_strcat_comma(&output, buf); if (extended_status) { if (ups_battery_voltage > LONG_MIN) { mp_snprintf((char *) &buf, sizeof(buf), "voltage: %1.0f V", (ups_battery_voltage * 0.1f)); mp_strcat_comma(&output, buf); } if (ups_battery_current > LONG_MIN) { mp_snprintf((char *) &buf, sizeof(buf), "current: %1.1f A", (ups_battery_current * 0.1f)); mp_strcat_comma(&output, buf); } if (ups_battery_temperature > LONG_MIN) { mp_snprintf((char *) &buf, sizeof(buf), "temperature: %ld C", ups_battery_temperature); mp_strcat_comma(&output, buf); } if (ups_output_frequency > LONG_MIN) { mp_snprintf((char *) &buf, sizeof(buf), "output frequency: %3.1f Hz", (ups_output_frequency * 0.1f)); mp_strcat_comma(&output, buf); } if (ups_input_line_bads > LONG_MIN) { mp_snprintf((char *) &buf, sizeof(buf), "input line bads: %ld", ups_input_line_bads); mp_strcat_comma(&output, buf); } } /* * performance data */ if (mp_showperfdata) { if (ups_remaining_charge > LONG_MIN) { mp_perfdata_int("remaining_charge", ups_remaining_charge, "%", threshold_charge); } if (ups_remaining_runtime > LONG_MIN) { mp_perfdata_int("remaining_runtime", ups_remaining_charge, "minutes", threshold_runtime); } if (ups_battery_voltage > LONG_MIN) { mp_perfdata_float("battery_voltage", ups_battery_voltage * 0.1f, "V", NULL); } if (ups_battery_current > LONG_MIN) { mp_perfdata_float("battery_current", ups_battery_current * 0.1f, "A", NULL); } if (ups_battery_temperature > LONG_MIN) { mp_perfdata_int("battery_temperature", ups_battery_temperature, "C", NULL); } if (ups_output_frequency > LONG_MIN) { mp_perfdata_float("output_frequency", ups_output_frequency * 0.1f, "Hz", NULL); } if (ups_input_line_bads > LONG_MIN) { mp_perfdata_int("input_line_bads", ups_input_line_bads, "", NULL); } } switch (state) { case STATE_OK: ok("UPS: %s", output); break; case STATE_WARNING: warning("UPS: %s", output); break; case STATE_CRITICAL: critical("UPS: %s", output); break; default: unknown("UPS: %s", output); break; } }
int main (int argc, char **argv) { /* Local Vars */ FILE *meminfo; char line[64]; char *line_ptr; char *key; float mem_total = 0, mem_free = 0; float slab = 0; float swap_cached = 0, swap_total = 0, swap_free = 0; float page_tables = 0; float buffers = 0; float cached = 0; float apps, swap, used; float usedp; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); // Read /proc/meminfo meminfo = fopen("/proc/meminfo", "r"); if (meminfo == NULL) unknown("Can't read /proc/meminfo"); while (fgets(line, 64, meminfo) != NULL) { line_ptr = line; key = strsep(&line_ptr, " "); if (strcmp("MemTotal:", key) == 0) { mem_total = readValue(line_ptr); } else if (strcmp("MemFree:", key) == 0) { mem_free = readValue(line_ptr); } else if (strcmp("Slab:", key) == 0) { slab = readValue(line_ptr); } else if (strcmp("SwapCached:", key) == 0) { swap_cached = readValue(line_ptr); } else if (strcmp("SwapTotal:", key) == 0) { swap_total = readValue(line_ptr); } else if (strcmp("SwapFree:", key) == 0) { swap_free= readValue(line_ptr); } else if (strcmp("PageTables:", key) == 0) { page_tables = readValue(line_ptr); } else if (strcmp("Buffers:", key) == 0) { buffers = readValue(line_ptr); } else if (strcmp("Cached:", key) == 0) { cached = readValue(line_ptr); } } // Calculations apps = mem_total - mem_free - buffers - cached - slab - page_tables - swap_cached; swap = swap_total - swap_free; used = mem_total - mem_free - buffers - cached; usedp = (float)(used*100)/(float)mem_total; mp_perfdata_float("memtotal", mem_total, "", NULL); mp_perfdata_float2("slab", slab, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("swapcached", swap_cached, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("pagetables", page_tables, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("apps", apps, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("memfree", mem_free, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("buffers", buffers, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("cached", cached, "", NULL, 1, 0, 1, mem_total); mp_perfdata_float2("swap", swap, "", NULL, 1, 0, 1, mem_total); switch (get_status(usedp, usage_thresholds)) { case STATE_OK: ok("Memory - %2.2f%% (%s of %s) used", usedp, mp_human_size(used), mp_human_size(mem_total)); break; case STATE_WARNING: warning("Memory - %2.2f%% (%s of %s) used", usedp, mp_human_size(used), mp_human_size(mem_total)); break; case STATE_CRITICAL: critical("Memory - %2.2f%% (%s of %s) used", usedp, mp_human_size(used), mp_human_size(mem_total)); break; } unknown("Memory - %2.2f%% (%s of %s) used", usedp, mp_human_size(used), mp_human_size(mem_total)); }
int main (int argc, char **argv) { /* Local Vars */ PGconn *conn; PGresult *res; char *val, *val2; float delay = 0; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); /* Connectiong to PostgreSQL server */ conn = mp_pgsql_init(); /* Check Recovery state */ res = mp_pgsql_exec(conn, "SELECT pg_is_in_recovery() AS recovery, NOW() - pg_last_xact_replay_timestamp() AS replication_delay;"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { critical("Query 'SELECT pg_is_in_recovery() AS recovery, NOW() - pg_last_xact_replay_timestamp() AS replication_delay;' failed: %s", PQerrorMessage(conn)); } val = PQgetvalue(res, 0, 0); if (val[0] == 'f') { mp_pgsql_deinit(conn); critical("PostgreSQL is not in recover mode."); } /* Calculate delay */ val = PQgetvalue(res, 0, 1); delay = strtol(val, &val2, 10) * 3600; delay += strtol(++val2, NULL,10) * 60; delay += strtof((val2+=3), NULL); val = strdup(val); mp_pgsql_deinit(conn); mp_perfdata_float("delay", (float)delay, "s", delay_thresholds); switch(get_status(delay, delay_thresholds)) { case STATE_OK: free_threshold(delay_thresholds); ok("PostgreSQL Slave Delay: %s", val); break; case STATE_WARNING: free_threshold(delay_thresholds); warning("PostgreSQL Slave Delay: %s", val); break; case STATE_CRITICAL: free_threshold(delay_thresholds); critical("PostgreSQL Slave Delay: %s", val); break; } free_threshold(delay_thresholds); critical("You should never reach this point."); }