diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2020-02-24 11:25:18 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2020-02-24 11:34:18 +0200 |
commit | 80049026737f46a71bebd4aeebbf7fef3ab312ec (patch) | |
tree | 28c725fcf202d2cf77e3634d300aaab51fbe4d05 /src/ping903q.c | |
parent | 4352cb046a38b969d9b90447d27fe90c07fa43d9 (diff) | |
download | ping903-80049026737f46a71bebd4aeebbf7fef3ab312ec.tar.gz ping903-80049026737f46a71bebd4aeebbf7fef3ab312ec.tar.bz2 |
Improve ping903q output in Nagios check mode
* doc/ping903q.1: Document the -p option.
* src/ping903q.c: Use configurable prefix in Nagios mode. Round up
loss percentage to 2 decimal digits on output.
Diffstat (limited to 'src/ping903q.c')
-rw-r--r-- | src/ping903q.c | 89 |
1 files changed, 63 insertions, 26 deletions
diff --git a/src/ping903q.c b/src/ping903q.c index 7bc35e1..20c0ebc 100644 --- a/src/ping903q.c +++ b/src/ping903q.c @@ -18,6 +18,7 @@ static char *config_file = DEFAULT_CONFIG_FILE; FILE *http; int verbose; int resolve_ip; +char *nagios_prefix_format = "PING"; char const http_version[] = "HTTP/1.1"; enum { @@ -658,7 +659,7 @@ parse_nagios_threshold(char const *val, struct nagios_threshold *tp) static void print_perfdata(double rta, double pl, struct nagios_check_data *cd) { - printf("|rta=%.6fms;%.6f;%.6f;%.6f pl=%.6f%%;%.6f;%.6f;%d\n", + printf("|rta=%.3fms;%.3f;%.3f;%.3f pl=%.2f%%;%.2f;%.2f;%d\n", rta, cd->wth.round_trip, cd->cth.round_trip, 0.0, pl, cd->wth.loss_pct, cd->cth.loss_pct, 0); } @@ -667,6 +668,43 @@ static inline int newstatus(int old, int new) { return old > new ? old : new; } +static void +nagios_format_output(int status, double loss, double rta, + struct nagios_check_data *cd, char const *name) +{ + char *p; + size_t len; + /* Format initial prefix */ + p = nagios_prefix_format; + while (*p) { + len = strcspn(p, "%"); + fwrite(p, len, 1, stdout); + p += len; + if (p[0]) { + if (p[1] == 'h') { + fwrite(name, strlen(name), 1, stdout); + p += 2; + } else { + fputc(*p++, stdout); + if (*p) + fputc(*p++, stdout); + } + } + } + printf(" %s ", status_str[status]); + if (status == EX_NAGIOS_UNKNOWN) { + printf("%s\n", "- waiting for data to arrive"); + return; + } + if (loss >= 100) { + printf("%s", "- Packet loss = 100%"); + rta = cd->cth.round_trip; + } else { + printf("Packet loss = %.0f%%, RTA = %.2f ms", loss, rta); + } + print_perfdata(rta, loss, cd); +} + static int nagios_check(struct json_value *obj, void *data) { @@ -681,31 +719,27 @@ nagios_check(struct json_value *obj, void *data) jv = ejson_get(obj, "status", json_string); if (strcmp(jv->v.s, "init") == 0) { - printf("PING %s UNKNOWN - waiting for data to arrive\n", name); - exit(EX_NAGIOS_UNKNOWN); - } - if (strcmp(jv->v.s, "invalid") == 0 - || json_object_get(obj, "stddev", &jv)) { - printf("PING %s CRITICAL - Packet loss = 100%%", name); - print_perfdata(cd->cth.round_trip, 100.0, cd); - exit(EX_NAGIOS_CRITICAL); - } - - status = EX_NAGIOS_OK; - rta = ejson_get(obj, "avg", json_number)->v.n; - if (rta >= cd->cth.round_trip) - status = newstatus(status, EX_NAGIOS_CRITICAL); - if (rta >= cd->wth.round_trip) - status = newstatus(status, EX_NAGIOS_WARNING); + status = EX_NAGIOS_UNKNOWN; + } else if (strcmp(jv->v.s, "invalid") == 0 + || json_object_get(obj, "stddev", &jv)) { + loss = 100; + rta = cd->cth.round_trip; + status = EX_NAGIOS_CRITICAL; + } else { + status = EX_NAGIOS_OK; + rta = ejson_get(obj, "avg", json_number)->v.n; + if (rta >= cd->cth.round_trip) + status = newstatus(status, EX_NAGIOS_CRITICAL); + if (rta >= cd->wth.round_trip) + status = newstatus(status, EX_NAGIOS_WARNING); - loss = ejson_get(obj, "loss", json_number)->v.n; - if (loss >= cd->cth.loss_pct) - status = newstatus(status, EX_NAGIOS_CRITICAL); - if (loss >= cd->wth.loss_pct) - status = newstatus(status, EX_NAGIOS_WARNING); - printf("PING %s %s Packet loss = %d%%, RTA = %.2f ms", - name, status_str[status], (int)loss, rta); - print_perfdata(rta, loss, cd); + loss = ejson_get(obj, "loss", json_number)->v.n; + if (loss >= cd->cth.loss_pct) + status = newstatus(status, EX_NAGIOS_CRITICAL); + if (loss >= cd->wth.loss_pct) + status = newstatus(status, EX_NAGIOS_WARNING); + } + nagios_format_output(status, loss, rta, cd, name); exit(status); } @@ -782,7 +816,7 @@ main(int argc, char **argv) exit(0); } } - while ((c = getopt(argc, argv, "c:f:H:hmrVvw:")) != EOF) { + while ((c = getopt(argc, argv, "c:f:H:hmp:rVvw:")) != EOF) { switch (c) { case 'c': c_opt = optarg; @@ -801,6 +835,9 @@ main(int argc, char **argv) case 'm': setmode(MODE_MATCH, c); break; + case 'p': + nagios_prefix_format = optarg; + break; case 'r': resolve_ip = 1; break; |