File: nagios.c

package info (click to toggle)
uwsgi 2.0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,564 kB
  • sloc: ansic: 87,066; python: 7,004; cpp: 1,133; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (79 lines) | stat: -rw-r--r-- 1,622 bytes parent folder | download | duplicates (8)
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
#include "../../uwsgi.h"

extern struct uwsgi_server uwsgi;
int use_nagios = 0;

struct uwsgi_option nagios_options[] = {

	{"nagios", no_argument, 0, "nagios check", uwsgi_opt_true, &use_nagios, UWSGI_OPT_NO_INITIAL},
        {0, 0, 0, 0, 0, 0, 0},

};


int nagios() {

	struct uwsgi_header uh;
	char *buf = NULL;

	if (!use_nagios) {
		return 1;
	}

	if (!uwsgi.sockets) {
		fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n");
		exit(3);
	}


	int fd = uwsgi_connect(uwsgi.sockets->name, uwsgi.socket_timeout, 0);
	if (fd < 0) {
		fprintf(stdout, "UWSGI CRITICAL: could not connect() to workers %s\n", strerror(errno));
		if (errno == EPERM || errno == EACCES) {
			exit(3);
		}
		exit(2);
	}

	uh.modifier1 = UWSGI_MODIFIER_PING;
	uh.pktsize = 0;
	uh.modifier2 = 0;
	if (write(fd, &uh, 4) != 4) {
		uwsgi_error("write()");
		fprintf(stdout, "UWSGI CRITICAL: could not send ping packet to workers\n");
		exit(2);
	}


	int ret = uwsgi_read_response(fd, &uh, uwsgi.socket_timeout, &buf);

	if (ret == -2) {
		fprintf(stdout, "UWSGI CRITICAL: timed out waiting for response\n");
		exit(2);
	}
	else if (ret == -1) {
		fprintf(stdout, "UWSGI CRITICAL: error reading response\n");
		exit(2);
	}
	else {
		if (uh.pktsize > 0 && buf) {
			fprintf(stdout, "UWSGI WARNING: %.*s\n", uh.pktsize, buf);
			exit(1);
		}
		else {
			fprintf(stdout, "UWSGI OK: armed and ready\n");
			exit(0);
		}
	}

	// never here
	fprintf(stdout, "UWSGI UNKNOWN: probably you hit a bug of uWSGI !!!\n");
	exit(3);
}

struct uwsgi_plugin nagios_plugin = {
	
	.name = "nagios",
	.options = nagios_options,
	.init = nagios,
};