aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2020-02-24 11:25:18 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2020-02-24 11:34:18 +0200
commit80049026737f46a71bebd4aeebbf7fef3ab312ec (patch)
tree28c725fcf202d2cf77e3634d300aaab51fbe4d05
parent4352cb046a38b969d9b90447d27fe90c07fa43d9 (diff)
downloadping903-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.
-rw-r--r--doc/ping903q.116
-rw-r--r--src/ping903q.c89
2 files changed, 76 insertions, 29 deletions
diff --git a/doc/ping903q.1 b/doc/ping903q.1
index ab57518..ffe1830 100644
--- a/doc/ping903q.1
+++ b/doc/ping903q.1
@@ -13,7 +13,7 @@
.\"
.\" You should have received a copy of the GNU General Public License
.\" along with Ping903. If not, see <http://www.gnu.org/licenses/>.
-.TH PING903Q 1 "February 13, 2020" "PING903Q" "User Commands"
+.TH PING903Q 1 "February 24, 2020" "PING903Q" "User Commands"
.SH NAME
ping903q \- ping903 query tool
.SH SYNOPSIS
@@ -24,6 +24,7 @@ ping903q \- ping903 query tool
.PP
\fBping903q\fR\
[\fB\-f \fIFILE\fR]\
+ [\fB\-p \fIPREFIX\fR]\
\fB\-H \fIHOST\fR\
\fB\-c \fIRTA\fB,\fIPCT\fB%\fR\
\fB\-w \fIRTA\fB,\fIPCT\fB%\fR
@@ -105,8 +106,9 @@ Print program version, copyright information, and exit.
.B \-v
Turn on verbose output.
.SS Options specific for Nagios check mode
-The presence of any of these options switches \fBping903q\fR to Nagios
-check mode. For this mode to succeed, all three options must be specified.
+The presence of any of the three options below switches \fBping903q\fR
+to Nagios check mode. For this mode to succeed, all three options
+must be specified.
.TP
\fB\-H \fIHOST\fR
Sets host name or IP address to query for.
@@ -123,6 +125,14 @@ also that the use of the percent sign is mandatory.
\fB\-w \fIRTA\fB,\fIPCT\fB%\fR
Sets the warning threshold value. See above for the discussion of the
arguments.
+.PP
+Other options valid in this mode:
+.TP
+\fB\-p \fIPREFIX\fR
+Supplies the prefix to be displayed before Nagios status string. The
+default is "PING". The \fIPREFIX\fR string can contain the
+\fB%h\fR escape sequence, which will be expanded to the name of the
+host being monitored. E.g. \fB\-p 'PING %h'\fR.
.SS Options specific for match mode
.TP
.B \-m
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;

Return to:

Send suggestions and report system problems to the System administrator.