aboutsummaryrefslogtreecommitdiff
path: root/src/ping903q.c
blob: 7bc35e1faca5771d846107bbb620b82dd3bec08f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <limits.h>
#include <math.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "json.h"
#include "defs.h"

static char *config_file = DEFAULT_CONFIG_FILE;
FILE *http;
int verbose;
int resolve_ip;
char const http_version[] = "HTTP/1.1";

enum {
	EX_NAGIOS_OK = 0,
	EX_NAGIOS_WARNING = 1,
	EX_NAGIOS_CRITICAL = 2,
	EX_NAGIOS_UNKNOWN = 3,
	EX_NAGIOS_DEPENDENT = 4
};

static char const *status_str[] = {
	"OK",
	"WARNING",
	"CRITICAL",
	"UNKNOWN",
	"DEPENDENT"
};

static void
abend(char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vlogger(LOG_CRIT, fmt, ap);
	va_end(ap);
	exit(EX_NAGIOS_UNKNOWN);
}

static char *
read_listen(char **ret_service)
{
	FILE *fp;
	int ln = 0;
	char buf[1024];
	int skip_to_eol = 0;
	char *retval = NULL;

	*ret_service = NULL;

	fp = fopen(config_file, "r");
	if (!fp) {
		if (errno == ENOENT)
			return NULL;
		else
			abend("can't open %s: %s",
			      config_file, strerror(errno));
	}

	while (fgets(buf, sizeof(buf), fp)) {
		int len;
		char *p;

		ln++;
		len = strlen(buf);
		if (len == 0)
			continue;

		if (skip_to_eol) {
			skip_to_eol = buf[len-1] != '\n';
			continue;
		}

		if (buf[len-1] == '\n')
			buf[--len] = 0;
		else if (!feof(fp)) {
			error("%s:%d: line too long", config_file, ln);
			skip_to_eol = 1;
			continue;
		}

		while (len > 0 && isspace(buf[len-1]))
			buf[--len] = 0;

		if (len == 0)
			continue;

		for (p = buf; (*p == ' ' || *p == '\t'); p++)
			;
		if (*p == 0 || *p == '#')
			continue;

		if (strncmp(p, "listen", 6) == 0) {
			char *q;

			p += 6;
			while (*p && (*p == ' '||*p == '\t'))
				p++;
			retval = estrdup(p);
			q = strchr(retval, ':');
			if (q) {
				*q++ = 0;
				if (*q) {
					*ret_service = estrdup(q);
				}
			}
			break;
		}
	}

	fclose(fp);
	return retval;
}

static void
http_connect(char *node, char *service)
{
	struct addrinfo hints;
	struct addrinfo *res;
	int fd;
	int rc;

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	rc = getaddrinfo(node, service, &hints, &res);
	if (rc)
		abend("%s: %s", node, gai_strerror(rc));

	fd = socket(res->ai_family, res->ai_socktype, 0);
	if (fd == -1)
		abend("socket: %s", strerror(errno));
	if (connect(fd, (struct sockaddr *)res->ai_addr, res->ai_addrlen))
		abend("failed to connect to %s:%s: %s",
		      node, service, strerror(errno));
	http = fdopen(fd, "w+");
	if (http == NULL)
		abend("failed to open socket: %s", strerror(errno));
	freeaddrinfo(res);
}

struct http_resp {
	int code;     /* HTTP response code */
	char *reason; /* Human-readable status */
	char **hv;    /* Headers */
	size_t hc;    /* Number of headers in hv */
	size_t hn;    /* Max capacity of hv */
	char *content;
	size_t content_length;
};

struct http_buf {
	char *base;
	size_t size;
	size_t len;
};

static char const *
http_resp_get_header(struct http_resp *resp, char const *name)
{
	int i;
	size_t len = strlen(name);

	for (i = 0; i < resp->hc; i++) {
		if (strlen(resp->hv[i]) > len
		    && resp->hv[i][len] == ':'
		    && strncasecmp(resp->hv[i], name, len) == 0) {
			char const *ret = resp->hv[i] + len + 1;
			while (*ret && (*ret == ' ' || *ret == '\t'))
				ret++;
			return ret;
		}
	}
	return NULL;
}

static void
http_readline(struct http_buf *hbuf)
{
	hbuf->len = 0;
	while (1) {
		if (hbuf->len == hbuf->size) {
			hbuf->base = e2nrealloc(hbuf->base, &hbuf->size, 1);
		}
		if (fgets(hbuf->base + hbuf->len, hbuf->size, http) == NULL) {
			if (feof(http))
				abend("connection closed prematurely");
			else
				abend("error reading from socket: %s",
				      strerror(errno));
		}
		hbuf->len += strlen(hbuf->base + hbuf->len);
		if (hbuf->base[hbuf->len-1] == '\n') {
			hbuf->base[--hbuf->len] = 0;
			if (hbuf->base[hbuf->len-1] == '\r')
				hbuf->base[--hbuf->len] = 0;
			break;
		}
	}
}

static int
split3(const char *input, char *res[3], int flag)
{
	size_t len;
	char *p;

	p = strchr(input, ' ');
	if (!p)
		return 1;

	len = p - input;
	res[0] = malloc(len + 1);
	if (!res[0])
		return -1;
	memcpy(res[0], input, len);
	res[0][len] = 0;

	input = p + 1;

	p = (flag ? strrchr : strchr)(input, ' ');

	if (!p)
		return 1;
	len = p - input;
	res[1] = malloc(len + 1);
	if (!res[1])
		return -1;
	memcpy(res[1], input, len);
	res[1][len] = 0;

	res[2] = strdup(p + 1);
	if (!res[2])
		return -1;

	return 0;
}

int
strsplit3(const char *input, char *result[3], int flag)
{
	char *tmp[3] = { NULL, NULL, NULL };
	int rc = split3(input, tmp, flag);
	if (rc)	{
		int ec = errno;
		free(tmp[0]);
		free(tmp[1]);
		free(tmp[2]);
		errno = ec;
	} else {
		result[0] = tmp[0];
		result[1] = tmp[1];
		result[2] = tmp[2];
	}
	return rc;
}

static void
http_resp_init(struct http_resp *resp)
{
	memset(resp, 0, sizeof(*resp));
}

static void
http_resp_reset(struct http_resp *resp)
{
	size_t i;

	resp->code = 0;
	free(resp->reason);
	resp->reason = NULL;
	for (i = 0; i < resp->hc; i++) {
		free(resp->hv[i]);
		resp->hv[i] = NULL;
	}
	resp->hc = 0;
	free(resp->content);
	resp->content = NULL;
	resp->content_length = 0;
}

static void
http_recv(struct http_resp *resp)
{
	enum input_state { is_initial, is_headers, is_content } state = is_initial;
	struct http_buf hbuf;
	char const *hval;

	memset(&hbuf, 0, sizeof(hbuf));
	http_resp_reset(resp);
	while (state != is_content) {
		http_readline(&hbuf);
		if (state == is_initial) {
			char *res[3];

			if (strsplit3(hbuf.base, res, 0)) {
				abend("malformed HTTP response");
			}
			if (strcmp(res[0], http_version)) {
				abend("unsupported HTTP version: %s", res[0]);
			}
			free(res[0]);

			resp->code = atoi(res[1]);
			if (resp->code <= 0 || resp->code > 559) {
				abend("bad response code: %s", res[1]);
			}
			free(res[1]);
			resp->reason = res[2];
			state = is_headers;
		} else if (state == is_headers) {
			if (resp->hc == resp->hn) {
				resp->hv = json_2nrealloc(resp->hv,
							  &resp->hn,
							  sizeof(resp->hv[0]));
			}
			if (hbuf.len > 0) {
				resp->hv[resp->hc++] = estrdup(hbuf.base);
			} else {
				resp->hv[resp->hc] = NULL;
				state = is_content;
			}
		}
	}

	hval = http_resp_get_header(resp, "content-length");
	if (hval) {
		char *p;
		unsigned long len = strtoul(hval, &p, 10);
		if (*p) {
			abend("malformed content-length");
		}
		resp->content = emalloc(len + 1);
		resp->content_length = len;

		p = resp->content;
		while (len) {
			ssize_t n = fread(p, 1, len, http);
			if (n == -1) {
				abend("socket read error: %s",
				      strerror(errno));
			}
			if (n == 0) {
				abend("short read from socket");
			}
			len -= n;
			p += n;
		}
		*p = 0;
	}
}

static void
http_query(char const *meth, char const *url, char **hdr)
{
	size_t i;
	int host = 0;

	fprintf(http, "%s %s HTTP/1.1\r\n", meth, url);
	if (hdr) {
		for (i = 0; hdr[i]; i++) {
			fprintf(http, "%s\r\n", hdr[i]);
			if (!host && strncasecmp(hdr[i], "host:", 5) == 0)
				host = 1;
		}
	}
	if (!host)
		fprintf(http, "Host: localhost\r\n");
	fprintf(http, "\r\n");
}

static char const *
json_strerror(int ec)
{
	static char *json_errstr[] = {
		[JSON_E_NOERR]     = "No error",
		[JSON_E_NOMEM]     = "Not enough memory",
		[JSON_E_BADTOK]    = "Unrecognized token",
		[JSON_E_BADDELIM]  = "Bad delimiter",
		[JSON_E_BADSTRING] = "Malformed string"
	};
	if (ec < 0 || ec >= sizeof(json_errstr) / sizeof(json_errstr[0]))
		return "Unknown error";
	return json_errstr[ec];
}

static struct json_value *
ejson_get(struct json_value *obj, char *name, int type)
{
	struct json_value *jv;
	if (json_object_get(obj, name, &jv)) {
		abend("no \"%s\" member in the response", name);
	}
	if (jv->type != type) {
		abend("\"%s\" member has wrong type", name);
	}
	return jv;
}

static int
print_host_status(struct json_value *obj, void *unused)
{
	struct json_value *jv;
	int alive;
	char const *name;

	jv = ejson_get(obj, "name", json_string);
	name = jv->v.s;

	jv = ejson_get(obj, "status", json_string);
	if (strcmp(jv->v.s, "init") == 0) {
		printf("%s: no data available yet; try later\n", name);
		return EX_NAGIOS_UNKNOWN;
	} else if (strcmp(jv->v.s, "invalid") == 0) {
		printf("no response from %s\n", name);
		return EX_NAGIOS_CRITICAL;
	}
	/* Status values "pending" and "valid" are OK */

	alive = ejson_get(obj, "alive", json_bool)->v.b;
	printf("%s is %s\n", name, alive ? "alive" : "not alive");
	if (verbose) {
		double xmit = ejson_get(obj, "xmit", json_number)->v.n;
		double recv = ejson_get(obj, "recv", json_number)->v.n;
		double start_ts = ejson_get(obj, "start-timestamp",
					   json_number)->v.n;
		double stop_ts = ejson_get(obj, "stop-timestamp",
					   json_number)->v.n;
		
		printf("--- %s ping statistics ---\n", name);
		printf("%lu packets transmitted, %lu received, %d%% packet loss, time %.0fms\n",
		       (unsigned long) xmit, (unsigned long) recv,
		       (int)(100 * (xmit - recv) / xmit),
		       (stop_ts - start_ts) * 1000);
		if (!json_object_get(obj, "stddev", &jv))
			printf("rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n",
			       ejson_get(obj, "tmin", json_number)->v.n,
			       ejson_get(obj, "avg", json_number)->v.n,
			       ejson_get(obj, "tmax", json_number)->v.n,
			       ejson_get(obj, "stddev", json_number)->v.n);
	}
	return alive ? EX_NAGIOS_OK : EX_NAGIOS_CRITICAL;
}

static char *
resolve_host(char const *name)
{
	struct addrinfo hints, *res;
	char hbuf[NI_MAXHOST];
	int rc;
	
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_protocol = IPPROTO_TCP;
	rc = getaddrinfo(name, NULL, &hints, &res);
	if (rc)
		abend("%s: %s", name, gai_strerror(rc));
	if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
			NULL, 0, NI_NUMERICHOST)) {
		/* shouldn't happen */
		abend("%s: can't resolve hostname", name);
	}
	freeaddrinfo(res);
	return estrdup(hbuf);
}

static char *std_headers[] = {
	"Accept: application/json",
	"User-Agent: ping903q (" PACKAGE_STRING ")",
	NULL
};

static void
query_host(char const *name, int (*report)(struct json_value *, void *),
	   void *report_data)
{
	int rc;
	struct http_resp resp;
	struct json_value *obj;
	char url[1024];
	char const *hval;
	char *p;
	char *ipstr = resolve_host(name);
	ssize_t n;

	if (resolve_ip) {
		n = snprintf(url, sizeof(url), "/ip/%s", ipstr);
	} else {
		n = snprintf(url, sizeof(url), "/host/%s", name);
	}
	
	if (n < 0 || n == sizeof(url)) {
		abend("bad host name or IP");
	}
	http_query("GET", url, std_headers);
	http_resp_init(&resp);
	http_recv(&resp);
	if (resp.code != 200) {
		abend("%s", resp.reason);
	}

	hval = http_resp_get_header(&resp, "content-type");
	if (!hval || strcmp(hval, "application/json")) {
		abend("missing or unsupported content type");
	}

	rc = json_parse_string(resp.content, &obj, &p);
	if (rc != JSON_E_NOERR)
		abend("%s near %s", json_strerror(rc), p);
	if (obj->type == json_null)
		abend("%s (%s): host is not being monitored", name, ipstr);
	if (obj->type != json_object)
		abend("returned entity has wrong type");
	exit(report(obj, report_data));
}

static void
match_host(char const *name)
{
	int rc;
	struct http_resp resp;
	struct json_value *obj;
	char url[1024];
	char const *hval;
	char *p;
	ssize_t n;
	size_t i, len;
	
	n = snprintf(url, sizeof(url), "/match/%s", name);
	if (n == -1 || n == sizeof(url)) {
		abend("url buffer overflow");
	}
	http_query("GET", url, std_headers);
	http_resp_init(&resp);
	http_recv(&resp);
	if (resp.code != 200) {
		abend("%s", resp.reason);
	}

	hval = http_resp_get_header(&resp, "content-type");
	if (!hval || strcmp(hval, "application/json")) {
		abend("missing or unsupported content type");
	}

	rc = json_parse_string(resp.content, &obj, &p);
	if (rc != JSON_E_NOERR)
		abend("%s near %s", json_strerror(rc), p);
	if (obj->type != json_array)
		abend("returned entity has wrong type");

	len = json_array_length(obj);
	if (json_array_length(obj) == 0) {
		if (verbose)
			error("no matching hosts found");
		exit(EX_NAGIOS_CRITICAL);
	}

	for (i = 0; i < len; i++) {
		struct json_value *jv;
		if (json_array_get(obj, i, &jv)) {
			abend("can't get element %lu", (unsigned long) i);
		}
		if (jv->type != json_string)
			abend("bad type of element %lu", (unsigned long) i);
		printf("%s\n", jv->v.s);
	}
	exit(EX_NAGIOS_OK);
}

static void
query_all(void)
{
	int rc;
	struct http_resp resp;
	struct json_value *obj;
	char const *hval;
	char *p;
	size_t count[2] = { 0, 0 };
	size_t len;
	size_t i;

	http_query("GET", "/host", NULL);
	http_resp_init(&resp);
	http_recv(&resp);
	if (resp.code != 200) {
		abend("%s", resp.reason);
	}

	hval = http_resp_get_header(&resp, "content-type");
	if (!hval || strcmp(hval, "application/json")) {
		abend("missing or unsupported content type");
	}

	rc = json_parse_string(resp.content, &obj, &p);
	if (rc != JSON_E_NOERR) {
		abend("%s near %s", json_strerror(rc), p);
	}
	if (obj->type != json_array) {
		abend("returned entity has wrong type");
	}
	len = json_array_length(obj);
	for (i = 0; i < len; i++) {
		struct json_value *jv;
		if (json_array_get(obj, i, &jv)) {
			abend("can't get element %lu", (unsigned long) i);
		}
		if (jv->type == json_object)
			count[print_host_status(jv, NULL) != EX_NAGIOS_OK]++;
	}
	if (count[1] == 0)
		exit(EX_NAGIOS_OK);
	if (count[0] == 0)
		exit(EX_NAGIOS_CRITICAL);
	exit(EX_NAGIOS_WARNING);
}

struct nagios_threshold {
	double round_trip;
	double loss_pct;
};

struct nagios_check_data {
	struct nagios_threshold wth;
	struct nagios_threshold cth;
};

static void
parse_nagios_threshold(char const *val, struct nagios_threshold *tp)
{
	char *p;
	double d;

	d = strtod(val, &p);
	if (d <= 0 || d == HUGE_VAL) {
		abend("invalid threshold: %s", val);
	}
	tp->round_trip = d;
	if (*p != ',') {
		abend("threshold missing percentage: %s", val);
	}
	d = strtod(p + 1, &p);
	if (d < 0 || (d == 0 && errno) || d > 100 || *p != '%') {
		abend("invalid threshold: %s", val);
	}
	tp->loss_pct = d;
}

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",
	       rta, cd->wth.round_trip, cd->cth.round_trip, 0.0,
	       pl, cd->wth.loss_pct, cd->cth.loss_pct, 0);
}

static inline int newstatus(int old, int new) {
	return old > new ? old : new;
}

static int
nagios_check(struct json_value *obj, void *data)
{
	struct nagios_check_data *cd = data;
	struct json_value *jv;
 	char const *name;
	int status;
	double rta, loss;
	
	jv = ejson_get(obj, "name", json_string);
	name = jv->v.s;

	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);
	
	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);
	exit(status);
}

void
usage(void)
{
	printf("Usage: %s [-hrVv] [-f FILE] [HOST]\n", progname);
	printf("   or: %s -H HOST -c RTA,PCT%% -w RTA,PCT%%\n", progname);
	printf("   or: %s -m HOST\n", progname);
	printf("Query ping903 daemon.\n");
	printf("\n");
	printf("Options:\n\n");
	printf("  -f FILE        read configuration from FILE\n");
	printf("  -h             print this help test\n");
	printf("  -r             resolve HOST to IP address\n");
	printf("  -V             print program version and exit\n");
	printf("  -v             additional verbosity\n");
	printf("\nNagios check mode:\n\n");
	printf("  -H HOST        query statistics for this host\n");
	printf("  -c RTA,PCT%%    set critical threshold\n");
	printf("  -w RTA,PCT%%    set warning threshold\n");
	printf("\n(all three must be given in this mode)\n");
	printf("\nMatch mode:\n\n");
	printf("  -m             check whether HOST is monitored\n");
	printf("\n");
	printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
	printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
}

void
version(void)
{
	printf("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
	printf("%s", COPYLEFT);
}

enum {
	MODE_DEFAULT,
	MODE_NAGIOS_CHECK,
	MODE_MATCH
};

static int mode = MODE_DEFAULT;
static int mode_opt = 0;
static inline void
setmode(int newmode, int opt)
{
	if (mode != MODE_DEFAULT && mode != newmode)
		abend("option -%d conflicts with -%d", opt, mode_opt);
	mode = newmode;
	mode_opt = opt;
}


int
main(int argc, char **argv)
{
	int c;
	char *p, *node, *service;
	char const *host = NULL;
	char *c_opt = NULL;
	char *w_opt = NULL;
	struct nagios_check_data chkdata;
	
	set_progname(argv[0]);
	
	if (argc == 2) {
		if (strcmp(argv[1], "--help") == 0) {
			usage();
			exit(0);
		}
		if (strcmp(argv[1], "--version") == 0) {
			version();
			exit(0);
		}
	}
	while ((c = getopt(argc, argv, "c:f:H:hmrVvw:")) != EOF) {
		switch (c) {
		case 'c':
			c_opt = optarg;
			setmode(MODE_NAGIOS_CHECK, c);
			break;
		case 'f':
			config_file = optarg;
			break;
		case 'H':
			host = optarg;
			setmode(MODE_NAGIOS_CHECK, c);
			break;
		case 'h':
			usage();
			exit(0);
		case 'm':
			setmode(MODE_MATCH, c);
			break;
		case 'r':
			resolve_ip = 1;
			break;
		case 'V':
			version();
			exit(0);
		case 'v':
			verbose++;
			break;
		case 'w':
			w_opt = optarg;
			setmode(MODE_NAGIOS_CHECK, c);
			break;
		default:
			exit(EX_NAGIOS_UNKNOWN);
		}
	}
	argc -= optind;
	argv += optind;

	switch (mode) {
	case MODE_DEFAULT:
		break;
	case MODE_NAGIOS_CHECK:
		if (argc != 0) {
			abend("bad number of arguments");
		}
		if (!host)
			abend("hostname missing; use -H option");
		if (!c_opt)
			abend("critical threshold missing; use -c option");
		parse_nagios_threshold(c_opt, &chkdata.cth);

		if (!w_opt)
			abend("warning threshold missing; use -w option");
		parse_nagios_threshold(w_opt, &chkdata.wth);
		break;
	case MODE_MATCH:
		if (argc != 1)
			abend("-m requires exactly one command line argument");
		break;
	}
	
	p = node = read_listen(&service);
	if (!node || !node[0])
		node = DEFAULT_ADDRESS;
	if (!service)
		service = DEFAULT_SERVICE;
	http_connect(node, service);
	free(p);

	switch (mode) {
	case MODE_DEFAULT:
		switch (argc) {
		case 0:
			query_all();
		case 1:
			query_host(argv[0], print_host_status, NULL);
		default:
			abend("bad number of arguments");
		}
		break;

	case MODE_NAGIOS_CHECK:
		query_host(host, nagios_check, &chkdata);
		break;

	case MODE_MATCH:
		match_host(argv[0]);
	}
	exit(EX_NAGIOS_UNKNOWN);
}

Return to:

Send suggestions and report system problems to the System administrator.