diff options
Diffstat (limited to 'plugins')
84 files changed, 12724 insertions, 9797 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 30ca63d1..1a9399f0 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -13,8 +13,14 @@ AM_CFLAGS = -DNP_VERSION='"$(NP_VERSION)"' | |||
13 | 13 | ||
14 | VPATH = $(top_srcdir) $(top_srcdir)/lib $(top_srcdir)/plugins $(top_srcdir)/plugins/t | 14 | VPATH = $(top_srcdir) $(top_srcdir)/lib $(top_srcdir)/plugins $(top_srcdir)/plugins/t |
15 | 15 | ||
16 | AM_CPPFLAGS = -I.. -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl \ | 16 | AM_CPPFLAGS = -I.. \ |
17 | @LDAPINCLUDE@ @PGINCLUDE@ @SSLINCLUDE@ | 17 | -I$(top_srcdir)/lib \ |
18 | -I$(top_srcdir)/gl \ | ||
19 | -I$(top_srcdir)/intl \ | ||
20 | -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ | ||
21 | @LDAPINCLUDE@ \ | ||
22 | @PGINCLUDE@ \ | ||
23 | @SSLINCLUDE@ | ||
18 | 24 | ||
19 | localedir = $(datadir)/locale | 25 | localedir = $(datadir)/locale |
20 | # gettext docs say to use AM_CPPFLAGS, but per module_CPPFLAGS override this | 26 | # gettext docs say to use AM_CPPFLAGS, but per module_CPPFLAGS override this |
@@ -27,24 +33,29 @@ MATHLIBS = @MATHLIBS@ | |||
27 | #AM_CFLAGS = -Wall | 33 | #AM_CFLAGS = -Wall |
28 | 34 | ||
29 | libexec_PROGRAMS = check_apt check_cluster check_disk check_dummy check_http check_load \ | 35 | libexec_PROGRAMS = check_apt check_cluster check_disk check_dummy check_http check_load \ |
30 | check_mrtg check_mrtgtraf check_ntp check_ntp_peer check_nwstat check_ping \ | 36 | check_mrtg check_mrtgtraf check_ntp check_ntp_peer check_ping \ |
31 | check_real check_smtp check_ssh check_tcp check_time check_ntp_time \ | 37 | check_real check_smtp check_ssh check_tcp check_time check_ntp_time \ |
32 | check_ups check_users negate \ | 38 | check_ups check_users negate \ |
33 | urlize @EXTRAS@ | 39 | urlize @EXTRAS@ \ |
40 | check_snmp | ||
34 | 41 | ||
35 | check_tcp_programs = check_ftp check_imap check_nntp check_pop \ | 42 | check_tcp_programs = check_ftp check_imap check_nntp check_pop \ |
36 | check_udp check_clamd @check_tcp_ssl@ | 43 | check_udp check_clamd @check_tcp_ssl@ |
37 | 44 | ||
38 | EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \ | 45 | EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_hpjd \ |
39 | check_swap check_fping check_ldap check_game check_dig \ | 46 | check_swap check_fping check_ldap check_game check_dig \ |
40 | check_nagios check_by_ssh check_dns check_nt check_ide_smart \ | 47 | check_nagios check_by_ssh check_dns check_nt check_ide_smart \ |
41 | check_procs check_mysql_query check_apt check_dbi check_curl \ | 48 | check_procs check_mysql_query check_apt check_dbi check_curl \ |
42 | \ | 49 | \ |
43 | tests/test_check_swap | 50 | tests/test_check_swap \ |
51 | tests/test_check_snmp \ | ||
52 | tests/test_check_disk | ||
44 | 53 | ||
45 | SUBDIRS = picohttpparser | 54 | SUBDIRS = picohttpparser |
46 | 55 | ||
47 | np_test_scripts = tests/test_check_swap.t | 56 | np_test_scripts = tests/test_check_swap.t \ |
57 | tests/test_check_snmp.t \ | ||
58 | tests/test_check_disk.t | ||
48 | 59 | ||
49 | EXTRA_DIST = t \ | 60 | EXTRA_DIST = t \ |
50 | tests \ | 61 | tests \ |
@@ -55,7 +66,11 @@ EXTRA_DIST = t \ | |||
55 | check_hpjd.d \ | 66 | check_hpjd.d \ |
56 | check_game.d \ | 67 | check_game.d \ |
57 | check_radius.d \ | 68 | check_radius.d \ |
69 | check_curl.d \ | ||
70 | check_disk.d \ | ||
58 | check_time.d \ | 71 | check_time.d \ |
72 | check_users.d \ | ||
73 | check_load.d \ | ||
59 | check_nagios.d \ | 74 | check_nagios.d \ |
60 | check_dbi.d \ | 75 | check_dbi.d \ |
61 | check_tcp.d \ | 76 | check_tcp.d \ |
@@ -69,13 +84,17 @@ EXTRA_DIST = t \ | |||
69 | check_ntp_peer.d \ | 84 | check_ntp_peer.d \ |
70 | check_apt.d \ | 85 | check_apt.d \ |
71 | check_pgsql.d \ | 86 | check_pgsql.d \ |
87 | check_procs.d \ | ||
72 | check_ping.d \ | 88 | check_ping.d \ |
73 | check_by_ssh.d \ | 89 | check_by_ssh.d \ |
74 | check_smtp.d \ | 90 | check_smtp.d \ |
91 | check_snmp.d \ | ||
75 | check_mysql.d \ | 92 | check_mysql.d \ |
76 | check_ntp_time.d \ | 93 | check_ntp_time.d \ |
77 | check_dig.d \ | 94 | check_dig.d \ |
78 | check_cluster.d \ | 95 | check_cluster.d \ |
96 | check_curl.d \ | ||
97 | check_cluster.d \ | ||
79 | check_ups.d \ | 98 | check_ups.d \ |
80 | check_fping.d | 99 | check_fping.d |
81 | 100 | ||
@@ -116,9 +135,11 @@ check_cluster_LDADD = $(BASEOBJS) | |||
116 | check_curl_CFLAGS = $(AM_CFLAGS) $(LIBCURLCFLAGS) $(URIPARSERCFLAGS) $(LIBCURLINCLUDE) $(URIPARSERINCLUDE) -Ipicohttpparser | 135 | check_curl_CFLAGS = $(AM_CFLAGS) $(LIBCURLCFLAGS) $(URIPARSERCFLAGS) $(LIBCURLINCLUDE) $(URIPARSERINCLUDE) -Ipicohttpparser |
117 | check_curl_CPPFLAGS = $(AM_CPPFLAGS) $(LIBCURLCFLAGS) $(URIPARSERCFLAGS) $(LIBCURLINCLUDE) $(URIPARSERINCLUDE) -Ipicohttpparser | 136 | check_curl_CPPFLAGS = $(AM_CPPFLAGS) $(LIBCURLCFLAGS) $(URIPARSERCFLAGS) $(LIBCURLINCLUDE) $(URIPARSERINCLUDE) -Ipicohttpparser |
118 | check_curl_LDADD = $(NETLIBS) $(LIBCURLLIBS) $(SSLOBJS) $(URIPARSERLIBS) picohttpparser/libpicohttpparser.a | 137 | check_curl_LDADD = $(NETLIBS) $(LIBCURLLIBS) $(SSLOBJS) $(URIPARSERLIBS) picohttpparser/libpicohttpparser.a |
138 | check_curl_SOURCES = check_curl.c check_curl.d/check_curl_helpers.c | ||
119 | check_dbi_LDADD = $(NETLIBS) $(DBILIBS) | 139 | check_dbi_LDADD = $(NETLIBS) $(DBILIBS) |
120 | check_dig_LDADD = $(NETLIBS) | 140 | check_dig_LDADD = $(NETLIBS) |
121 | check_disk_LDADD = $(BASEOBJS) | 141 | check_disk_LDADD = $(BASEOBJS) |
142 | check_disk_SOURCES = check_disk.c check_disk.d/utils_disk.c | ||
122 | check_dns_LDADD = $(NETLIBS) | 143 | check_dns_LDADD = $(NETLIBS) |
123 | check_dummy_LDADD = $(BASEOBJS) | 144 | check_dummy_LDADD = $(BASEOBJS) |
124 | check_fping_LDADD = $(NETLIBS) | 145 | check_fping_LDADD = $(NETLIBS) |
@@ -139,13 +160,15 @@ check_nagios_LDADD = $(BASEOBJS) | |||
139 | check_nt_LDADD = $(NETLIBS) | 160 | check_nt_LDADD = $(NETLIBS) |
140 | check_ntp_LDADD = $(NETLIBS) $(MATHLIBS) | 161 | check_ntp_LDADD = $(NETLIBS) $(MATHLIBS) |
141 | check_ntp_peer_LDADD = $(NETLIBS) $(MATHLIBS) | 162 | check_ntp_peer_LDADD = $(NETLIBS) $(MATHLIBS) |
142 | check_nwstat_LDADD = $(NETLIBS) | ||
143 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) | 163 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) |
144 | check_ping_LDADD = $(NETLIBS) | 164 | check_ping_LDADD = $(NETLIBS) |
145 | check_procs_LDADD = $(BASEOBJS) | 165 | check_procs_LDADD = $(BASEOBJS) |
146 | check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) | 166 | check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) |
147 | check_real_LDADD = $(NETLIBS) | 167 | check_real_LDADD = $(NETLIBS) |
168 | check_snmp_SOURCES = check_snmp.c check_snmp.d/check_snmp_helpers.c | ||
148 | check_snmp_LDADD = $(BASEOBJS) | 169 | check_snmp_LDADD = $(BASEOBJS) |
170 | check_snmp_LDFLAGS = $(AM_LDFLAGS) `net-snmp-config --libs` | ||
171 | check_snmp_CFLAGS = $(AM_CFLAGS) `net-snmp-config --cflags` | ||
149 | check_smtp_LDADD = $(SSLOBJS) | 172 | check_smtp_LDADD = $(SSLOBJS) |
150 | check_ssh_LDADD = $(NETLIBS) | 173 | check_ssh_LDADD = $(NETLIBS) |
151 | check_swap_SOURCES = check_swap.c check_swap.d/swap.c | 174 | check_swap_SOURCES = check_swap.c check_swap.d/swap.c |
@@ -154,6 +177,7 @@ check_tcp_LDADD = $(SSLOBJS) | |||
154 | check_time_LDADD = $(NETLIBS) | 177 | check_time_LDADD = $(NETLIBS) |
155 | check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS) | 178 | check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS) |
156 | check_ups_LDADD = $(NETLIBS) | 179 | check_ups_LDADD = $(NETLIBS) |
180 | check_users_SOURCES = check_users.c check_users.d/users.c | ||
157 | check_users_LDADD = $(BASEOBJS) $(WTSAPI32LIBS) $(SYSTEMDLIBS) | 181 | check_users_LDADD = $(BASEOBJS) $(WTSAPI32LIBS) $(SYSTEMDLIBS) |
158 | check_by_ssh_LDADD = $(NETLIBS) | 182 | check_by_ssh_LDADD = $(NETLIBS) |
159 | check_ide_smart_LDADD = $(BASEOBJS) | 183 | check_ide_smart_LDADD = $(BASEOBJS) |
@@ -166,6 +190,10 @@ endif | |||
166 | 190 | ||
167 | tests_test_check_swap_LDADD = $(BASEOBJS) $(tap_ldflags) -ltap | 191 | tests_test_check_swap_LDADD = $(BASEOBJS) $(tap_ldflags) -ltap |
168 | tests_test_check_swap_SOURCES = tests/test_check_swap.c check_swap.d/swap.c | 192 | tests_test_check_swap_SOURCES = tests/test_check_swap.c check_swap.d/swap.c |
193 | tests_test_check_snmp_LDADD = $(BASEOBJS) $(tap_ldflags) -ltap | ||
194 | tests_test_check_snmp_SOURCES = tests/test_check_snmp.c check_snmp.d/check_snmp_helpers.c | ||
195 | tests_test_check_disk_LDADD = $(BASEOBJS) $(tap_ldflags) check_disk.d/utils_disk.c -ltap | ||
196 | tests_test_check_disk_SOURCES = tests/test_check_disk.c | ||
169 | 197 | ||
170 | ############################################################################## | 198 | ############################################################################## |
171 | # secondary dependencies | 199 | # secondary dependencies |
diff --git a/plugins/check_apt.c b/plugins/check_apt.c index e840184b..ab66a8d2 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c | |||
@@ -310,8 +310,8 @@ run_upgrade_result run_upgrade(const upgrade_type upgrade, const char *do_includ | |||
310 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); | 310 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); |
311 | } | 311 | } |
312 | 312 | ||
313 | struct output chld_out; | 313 | output chld_out; |
314 | struct output chld_err; | 314 | output chld_err; |
315 | char *cmdline = NULL; | 315 | char *cmdline = NULL; |
316 | cmdline = construct_cmdline(upgrade, upgrade_opts); | 316 | cmdline = construct_cmdline(upgrade, upgrade_opts); |
317 | if (input_filename != NULL) { | 317 | if (input_filename != NULL) { |
@@ -410,8 +410,8 @@ int run_update(char *update_opts) { | |||
410 | /* run the update */ | 410 | /* run the update */ |
411 | cmdline = construct_cmdline(NO_UPGRADE, update_opts); | 411 | cmdline = construct_cmdline(NO_UPGRADE, update_opts); |
412 | 412 | ||
413 | struct output chld_out; | 413 | output chld_out; |
414 | struct output chld_err; | 414 | output chld_err; |
415 | result = np_runcmd(cmdline, &chld_out, &chld_err, 0); | 415 | result = np_runcmd(cmdline, &chld_out, &chld_err, 0); |
416 | /* apt-get update changes exit status if it can't fetch packages. | 416 | /* apt-get update changes exit status if it can't fetch packages. |
417 | * since we were explicitly asked to do so, this is treated as | 417 | * since we were explicitly asked to do so, this is treated as |
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 2bc38d49..74b7a46f 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c | |||
@@ -45,7 +45,8 @@ typedef struct { | |||
45 | check_by_ssh_config config; | 45 | check_by_ssh_config config; |
46 | } check_by_ssh_config_wrapper; | 46 | } check_by_ssh_config_wrapper; |
47 | static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | 47 | static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
48 | static check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/); | 48 | static check_by_ssh_config_wrapper |
49 | validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/); | ||
49 | 50 | ||
50 | static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/); | 51 | static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/); |
51 | static void print_help(void); | 52 | static void print_help(void); |
@@ -90,7 +91,8 @@ int main(int argc, char **argv) { | |||
90 | 91 | ||
91 | /* SSH returns 255 if connection attempt fails; include the first line of error output */ | 92 | /* SSH returns 255 if connection attempt fails; include the first line of error output */ |
92 | if (result == 255 && config.unknown_timeout) { | 93 | if (result == 255 && config.unknown_timeout) { |
93 | printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); | 94 | printf(_("SSH connection failed: %s\n"), |
95 | chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); | ||
94 | return STATE_UNKNOWN; | 96 | return STATE_UNKNOWN; |
95 | } | 97 | } |
96 | 98 | ||
@@ -134,7 +136,8 @@ int main(int argc, char **argv) { | |||
134 | puts(chld_out.line[i]); | 136 | puts(chld_out.line[i]); |
135 | } | 137 | } |
136 | } else { | 138 | } else { |
137 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), config.remotecmd, result); | 139 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), |
140 | state_text(result), config.remotecmd, result); | ||
138 | } | 141 | } |
139 | return result; /* return error status from remote command */ | 142 | return result; /* return error status from remote command */ |
140 | } | 143 | } |
@@ -160,9 +163,11 @@ int main(int argc, char **argv) { | |||
160 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); | 163 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); |
161 | } | 164 | } |
162 | 165 | ||
163 | if (config.service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { | 166 | if (config.service[commands] && status_text && |
164 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, config.host_shortname, | 167 | sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { |
165 | config.service[commands++], cresult, status_text); | 168 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", |
169 | (int)local_time, config.host_shortname, config.service[commands++], cresult, | ||
170 | status_text); | ||
166 | } | 171 | } |
167 | } | 172 | } |
168 | 173 | ||
@@ -172,34 +177,35 @@ int main(int argc, char **argv) { | |||
172 | 177 | ||
173 | /* process command-line arguments */ | 178 | /* process command-line arguments */ |
174 | check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { | 179 | check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { |
175 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 180 | static struct option longopts[] = { |
176 | {"help", no_argument, 0, 'h'}, | 181 | {"version", no_argument, 0, 'V'}, |
177 | {"verbose", no_argument, 0, 'v'}, | 182 | {"help", no_argument, 0, 'h'}, |
178 | {"fork", no_argument, 0, 'f'}, | 183 | {"verbose", no_argument, 0, 'v'}, |
179 | {"timeout", required_argument, 0, 't'}, | 184 | {"fork", no_argument, 0, 'f'}, |
180 | {"unknown-timeout", no_argument, 0, 'U'}, | 185 | {"timeout", required_argument, 0, 't'}, |
181 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ | 186 | {"unknown-timeout", no_argument, 0, 'U'}, |
182 | {"hostname", required_argument, 0, 'H'}, | 187 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ |
183 | {"port", required_argument, 0, 'p'}, | 188 | {"hostname", required_argument, 0, 'H'}, |
184 | {"output", required_argument, 0, 'O'}, | 189 | {"port", required_argument, 0, 'p'}, |
185 | {"name", required_argument, 0, 'n'}, | 190 | {"output", required_argument, 0, 'O'}, |
186 | {"services", required_argument, 0, 's'}, | 191 | {"name", required_argument, 0, 'n'}, |
187 | {"identity", required_argument, 0, 'i'}, | 192 | {"services", required_argument, 0, 's'}, |
188 | {"user", required_argument, 0, 'u'}, | 193 | {"identity", required_argument, 0, 'i'}, |
189 | {"logname", required_argument, 0, 'l'}, | 194 | {"user", required_argument, 0, 'u'}, |
190 | {"command", required_argument, 0, 'C'}, | 195 | {"logname", required_argument, 0, 'l'}, |
191 | {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */ | 196 | {"command", required_argument, 0, 'C'}, |
192 | {"skip-stdout", optional_argument, 0, 'S'}, | 197 | {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */ |
193 | {"skip-stderr", optional_argument, 0, 'E'}, | 198 | {"skip-stdout", optional_argument, 0, 'S'}, |
194 | {"warn-on-stderr", no_argument, 0, 'W'}, | 199 | {"skip-stderr", optional_argument, 0, 'E'}, |
195 | {"proto1", no_argument, 0, '1'}, | 200 | {"warn-on-stderr", no_argument, 0, 'W'}, |
196 | {"proto2", no_argument, 0, '2'}, | 201 | {"proto1", no_argument, 0, '1'}, |
197 | {"use-ipv4", no_argument, 0, '4'}, | 202 | {"proto2", no_argument, 0, '2'}, |
198 | {"use-ipv6", no_argument, 0, '6'}, | 203 | {"use-ipv4", no_argument, 0, '4'}, |
199 | {"ssh-option", required_argument, 0, 'o'}, | 204 | {"use-ipv6", no_argument, 0, '6'}, |
200 | {"quiet", no_argument, 0, 'q'}, | 205 | {"ssh-option", required_argument, 0, 'o'}, |
201 | {"configfile", optional_argument, 0, 'F'}, | 206 | {"quiet", no_argument, 0, 'q'}, |
202 | {0, 0, 0, 0}}; | 207 | {"configfile", optional_argument, 0, 'F'}, |
208 | {0, 0, 0, 0}}; | ||
203 | 209 | ||
204 | check_by_ssh_config_wrapper result = { | 210 | check_by_ssh_config_wrapper result = { |
205 | .errorcode = OK, | 211 | .errorcode = OK, |
@@ -221,7 +227,8 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { | |||
221 | 227 | ||
222 | int option = 0; | 228 | int option = 0; |
223 | while (true) { | 229 | while (true) { |
224 | int opt_index = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | 230 | int opt_index = |
231 | getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | ||
225 | 232 | ||
226 | if (opt_index == -1 || opt_index == EOF) { | 233 | if (opt_index == -1 || opt_index == EOF) { |
227 | break; | 234 | break; |
@@ -266,11 +273,13 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { | |||
266 | char *p2; | 273 | char *p2; |
267 | 274 | ||
268 | p1 = optarg; | 275 | p1 = optarg; |
269 | result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); | 276 | result.config.service = realloc(result.config.service, |
277 | (++result.config.number_of_services) * sizeof(char *)); | ||
270 | while ((p2 = index(p1, ':'))) { | 278 | while ((p2 = index(p1, ':'))) { |
271 | *p2 = '\0'; | 279 | *p2 = '\0'; |
272 | result.config.service[result.config.number_of_services - 1] = p1; | 280 | result.config.service[result.config.number_of_services - 1] = p1; |
273 | result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); | 281 | result.config.service = realloc( |
282 | result.config.service, (++result.config.number_of_services) * sizeof(char *)); | ||
274 | p1 = p2 + 1; | 283 | p1 = p2 + 1; |
275 | } | 284 | } |
276 | result.config.service[result.config.number_of_services - 1] = p1; | 285 | result.config.service[result.config.number_of_services - 1] = p1; |
@@ -309,7 +318,8 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { | |||
309 | case 'C': /* Command for remote machine */ | 318 | case 'C': /* Command for remote machine */ |
310 | result.config.commands++; | 319 | result.config.commands++; |
311 | if (result.config.commands > 1) { | 320 | if (result.config.commands > 1) { |
312 | xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd); | 321 | xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", |
322 | result.config.remotecmd); | ||
313 | } | 323 | } |
314 | xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg); | 324 | xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg); |
315 | break; | 325 | break; |
@@ -396,7 +406,8 @@ command_construct comm_append(command_construct cmd, const char *str) { | |||
396 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); | 406 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); |
397 | } | 407 | } |
398 | 408 | ||
399 | if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) == NULL) { | 409 | if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) == |
410 | NULL) { | ||
400 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); | 411 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); |
401 | } | 412 | } |
402 | 413 | ||
@@ -412,12 +423,18 @@ check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper confi | |||
412 | return config_wrapper; | 423 | return config_wrapper; |
413 | } | 424 | } |
414 | 425 | ||
415 | if (config_wrapper.config.passive && config_wrapper.config.commands != config_wrapper.config.number_of_services) { | 426 | if (config_wrapper.config.passive && |
416 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); | 427 | config_wrapper.config.commands != config_wrapper.config.number_of_services) { |
428 | die(STATE_UNKNOWN, | ||
429 | _("%s: In passive mode, you must provide a service name for each command.\n"), | ||
430 | progname); | ||
417 | } | 431 | } |
418 | 432 | ||
419 | if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) { | 433 | if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) { |
420 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); | 434 | die(STATE_UNKNOWN, |
435 | _("%s: In passive mode, you must provide the host short name from the monitoring " | ||
436 | "configs.\n"), | ||
437 | progname); | ||
421 | } | 438 | } |
422 | 439 | ||
423 | return config_wrapper; | 440 | return config_wrapper; |
@@ -454,7 +471,8 @@ void print_help(void) { | |||
454 | printf(" %s\n", "-W, --warn-on-stderr]"); | 471 | printf(" %s\n", "-W, --warn-on-stderr]"); |
455 | printf(" %s\n", _("Exit with an warning, if there is an output on STDERR")); | 472 | printf(" %s\n", _("Exit with an warning, if there is an output on STDERR")); |
456 | printf(" %s\n", "-f"); | 473 | printf(" %s\n", "-f"); |
457 | printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed")); | 474 | printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always " |
475 | "return OK if ssh is executed")); | ||
458 | printf(" %s\n", "-C, --command='COMMAND STRING'"); | 476 | printf(" %s\n", "-C, --command='COMMAND STRING'"); |
459 | printf(" %s\n", _("command to execute on the remote machine")); | 477 | printf(" %s\n", _("command to execute on the remote machine")); |
460 | printf(" %s\n", "-l, --logname=USERNAME"); | 478 | printf(" %s\n", "-l, --logname=USERNAME"); |
@@ -490,7 +508,9 @@ void print_help(void) { | |||
490 | printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)")); | 508 | printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)")); |
491 | printf("\n"); | 509 | printf("\n"); |
492 | printf("%s\n", _("Examples:")); | 510 | printf("%s\n", _("Examples:")); |
493 | printf(" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo"); | 511 | printf( |
512 | " %s\n", | ||
513 | "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo"); | ||
494 | printf(" %s\n", "$ cat /tmp/foo"); | 514 | printf(" %s\n", "$ cat /tmp/foo"); |
495 | printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days"); | 515 | printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days"); |
496 | printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days"); | 516 | printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days"); |
diff --git a/plugins/check_cluster.c b/plugins/check_cluster.c index 9b695499..373520ee 100644 --- a/plugins/check_cluster.c +++ b/plugins/check_cluster.c | |||
@@ -112,25 +112,30 @@ int main(int argc, char **argv) { | |||
112 | int return_code = STATE_OK; | 112 | int return_code = STATE_OK; |
113 | /* return the status of the cluster */ | 113 | /* return the status of the cluster */ |
114 | if (config.check_type == CHECK_SERVICES) { | 114 | if (config.check_type == CHECK_SERVICES) { |
115 | return_code = get_status(total_services_warning + total_services_unknown + total_services_critical, config.thresholds); | 115 | return_code = |
116 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", state_text(return_code), | 116 | get_status(total_services_warning + total_services_unknown + total_services_critical, |
117 | (config.label == NULL) ? "Service cluster" : config.label, total_services_ok, total_services_warning, total_services_unknown, | 117 | config.thresholds); |
118 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", | ||
119 | state_text(return_code), (config.label == NULL) ? "Service cluster" : config.label, | ||
120 | total_services_ok, total_services_warning, total_services_unknown, | ||
118 | total_services_critical); | 121 | total_services_critical); |
119 | } else { | 122 | } else { |
120 | return_code = get_status(total_hosts_down + total_hosts_unreachable, config.thresholds); | 123 | return_code = get_status(total_hosts_down + total_hosts_unreachable, config.thresholds); |
121 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), | 124 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), |
122 | (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up, total_hosts_down, total_hosts_unreachable); | 125 | (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up, |
126 | total_hosts_down, total_hosts_unreachable); | ||
123 | } | 127 | } |
124 | 128 | ||
125 | exit(return_code); | 129 | exit(return_code); |
126 | } | 130 | } |
127 | 131 | ||
128 | check_cluster_config_wrapper process_arguments(int argc, char **argv) { | 132 | check_cluster_config_wrapper process_arguments(int argc, char **argv) { |
129 | static struct option longopts[] = {{"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, | 133 | static struct option longopts[] = { |
130 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, | 134 | {"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, |
131 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, | 135 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, |
132 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 136 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, |
133 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; | 137 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
138 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; | ||
134 | 139 | ||
135 | check_cluster_config_wrapper result = { | 140 | check_cluster_config_wrapper result = { |
136 | .errorcode = OK, | 141 | .errorcode = OK, |
@@ -251,7 +256,8 @@ void print_help(void) { | |||
251 | printf("\n"); | 256 | printf("\n"); |
252 | printf("%s\n", _("Examples:")); | 257 | printf("%s\n", _("Examples:")); |
253 | printf(" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:"); | 258 | printf(" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:"); |
254 | printf(" %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK")); | 259 | printf(" %s\n", |
260 | _("Will alert critical if there are 3 or more service data points in a non-OK")); | ||
255 | printf(" %s\n", _("state.")); | 261 | printf(" %s\n", _("state.")); |
256 | 262 | ||
257 | printf(UT_SUPPORT); | 263 | printf(UT_SUPPORT); |
diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 748201e8..fc704171 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c | |||
@@ -32,16 +32,23 @@ | |||
32 | * | 32 | * |
33 | * | 33 | * |
34 | *****************************************************************************/ | 34 | *****************************************************************************/ |
35 | const char *progname = "check_curl"; | ||
36 | 35 | ||
36 | const char *progname = "check_curl"; | ||
37 | const char *copyright = "2006-2024"; | 37 | const char *copyright = "2006-2024"; |
38 | const char *email = "devel@monitoring-plugins.org"; | 38 | const char *email = "devel@monitoring-plugins.org"; |
39 | 39 | ||
40 | #include "check_curl.d/config.h" | ||
41 | #include "states.h" | ||
42 | #include "thresholds.h" | ||
40 | #include <stdbool.h> | 43 | #include <stdbool.h> |
41 | #include <ctype.h> | 44 | #include <ctype.h> |
45 | #include "output.h" | ||
46 | #include "perfdata.h" | ||
42 | 47 | ||
48 | #include <assert.h> | ||
43 | #include "common.h" | 49 | #include "common.h" |
44 | #include "utils.h" | 50 | #include "utils.h" |
51 | #include "./check_curl.d/check_curl_helpers.h" | ||
45 | 52 | ||
46 | #ifndef LIBCURL_PROTOCOL_HTTP | 53 | #ifndef LIBCURL_PROTOCOL_HTTP |
47 | # error libcurl compiled without HTTP support, compiling check_curl plugin does not makes a lot of sense | 54 | # error libcurl compiled without HTTP support, compiling check_curl plugin does not makes a lot of sense |
@@ -50,8 +57,6 @@ const char *email = "devel@monitoring-plugins.org"; | |||
50 | #include "curl/curl.h" | 57 | #include "curl/curl.h" |
51 | #include "curl/easy.h" | 58 | #include "curl/easy.h" |
52 | 59 | ||
53 | #include "picohttpparser.h" | ||
54 | |||
55 | #include "uriparser/Uri.h" | 60 | #include "uriparser/Uri.h" |
56 | 61 | ||
57 | #include <arpa/inet.h> | 62 | #include <arpa/inet.h> |
@@ -63,207 +68,62 @@ const char *email = "devel@monitoring-plugins.org"; | |||
63 | 68 | ||
64 | #include <netdb.h> | 69 | #include <netdb.h> |
65 | 70 | ||
66 | #define MAKE_LIBCURL_VERSION(major, minor, patch) ((major)*0x10000 + (minor)*0x100 + (patch)) | ||
67 | |||
68 | #define DEFAULT_BUFFER_SIZE 2048 | ||
69 | #define DEFAULT_SERVER_URL "/" | ||
70 | #define HTTP_EXPECT "HTTP/" | ||
71 | #define INET_ADDR_MAX_SIZE INET6_ADDRSTRLEN | ||
72 | enum { | 71 | enum { |
73 | MAX_IPV4_HOSTLENGTH = 255, | 72 | MAX_IPV4_HOSTLENGTH = 255, |
74 | HTTP_PORT = 80, | ||
75 | HTTPS_PORT = 443, | ||
76 | MAX_PORT = 65535, | ||
77 | DEFAULT_MAX_REDIRS = 15 | ||
78 | }; | 73 | }; |
79 | 74 | ||
80 | enum { | 75 | enum { |
81 | STICKY_NONE = 0, | 76 | REGS = 2, |
82 | STICKY_HOST = 1, | ||
83 | STICKY_PORT = 2 | ||
84 | }; | ||
85 | |||
86 | enum { | ||
87 | FOLLOW_HTTP_CURL = 0, | ||
88 | FOLLOW_LIBCURL = 1 | ||
89 | }; | 77 | }; |
90 | 78 | ||
91 | /* for buffers for header and body */ | 79 | #include "regex.h" |
92 | typedef struct { | ||
93 | char *buf; | ||
94 | size_t buflen; | ||
95 | size_t bufsize; | ||
96 | } curlhelp_write_curlbuf; | ||
97 | 80 | ||
98 | /* for buffering the data sent in PUT */ | 81 | // Globals |
99 | typedef struct { | 82 | int verbose = 0; |
100 | char *buf; | ||
101 | size_t buflen; | ||
102 | off_t pos; | ||
103 | } curlhelp_read_curlbuf; | ||
104 | 83 | ||
105 | /* for parsing the HTTP status line */ | 84 | extern char errbuf[MAX_INPUT_BUFFER]; |
106 | typedef struct { | 85 | extern bool is_openssl_callback; |
107 | int http_major; /* major version of the protocol, always 1 (HTTP/0.9 | 86 | extern bool add_sslctx_verify_fun; |
108 | * never reached the big internet most likely) */ | ||
109 | int http_minor; /* minor version of the protocol, usually 0 or 1 */ | ||
110 | int http_code; /* HTTP return code as in RFC 2145 */ | ||
111 | int http_subcode; /* Microsoft IIS extension, HTTP subcodes, see | ||
112 | * http://support.microsoft.com/kb/318380/en-us */ | ||
113 | const char *msg; /* the human readable message */ | ||
114 | char *first_line; /* a copy of the first line */ | ||
115 | } curlhelp_statusline; | ||
116 | |||
117 | /* to know the underlying SSL library used by libcurl */ | ||
118 | typedef enum curlhelp_ssl_library { | ||
119 | CURLHELP_SSL_LIBRARY_UNKNOWN, | ||
120 | CURLHELP_SSL_LIBRARY_OPENSSL, | ||
121 | CURLHELP_SSL_LIBRARY_LIBRESSL, | ||
122 | CURLHELP_SSL_LIBRARY_GNUTLS, | ||
123 | CURLHELP_SSL_LIBRARY_NSS | ||
124 | } curlhelp_ssl_library; | ||
125 | 87 | ||
126 | enum { | ||
127 | REGS = 2, | ||
128 | MAX_RE_SIZE = 1024 | ||
129 | }; | ||
130 | #include "regex.h" | ||
131 | static regex_t preg; | ||
132 | static regmatch_t pmatch[REGS]; | ||
133 | static char regexp[MAX_RE_SIZE]; | ||
134 | static int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; | ||
135 | static int errcode; | ||
136 | static bool invert_regex = false; | ||
137 | static int state_regex = STATE_CRITICAL; | ||
138 | |||
139 | static char *server_address = NULL; | ||
140 | static char *host_name = NULL; | ||
141 | static char *server_url = 0; | ||
142 | static struct curl_slist *server_ips = NULL; | ||
143 | static bool specify_port = false; | ||
144 | static unsigned short server_port = HTTP_PORT; | ||
145 | static unsigned short virtual_port = 0; | ||
146 | static int host_name_length; | ||
147 | static char output_header_search[30] = ""; | ||
148 | static char output_string_search[30] = ""; | ||
149 | static char *warning_thresholds = NULL; | ||
150 | static char *critical_thresholds = NULL; | ||
151 | static int days_till_exp_warn, days_till_exp_crit; | ||
152 | static thresholds *thlds; | ||
153 | static char user_agent[DEFAULT_BUFFER_SIZE]; | ||
154 | static int verbose = 0; | ||
155 | static bool show_extended_perfdata = false; | ||
156 | static bool show_body = false; | ||
157 | static int min_page_len = 0; | ||
158 | static int max_page_len = 0; | ||
159 | static int redir_depth = 0; | ||
160 | static int max_depth = DEFAULT_MAX_REDIRS; | ||
161 | static char *http_method = NULL; | ||
162 | static char *http_post_data = NULL; | ||
163 | static char *http_content_type = NULL; | ||
164 | static CURL *curl; | ||
165 | static bool curl_global_initialized = false; | ||
166 | static bool curl_easy_initialized = false; | ||
167 | static struct curl_slist *header_list = NULL; | ||
168 | static bool body_buf_initialized = false; | ||
169 | static curlhelp_write_curlbuf body_buf; | ||
170 | static bool header_buf_initialized = false; | ||
171 | static curlhelp_write_curlbuf header_buf; | ||
172 | static bool status_line_initialized = false; | ||
173 | static curlhelp_statusline status_line; | ||
174 | static bool put_buf_initialized = false; | ||
175 | static curlhelp_read_curlbuf put_buf; | ||
176 | static char http_header[DEFAULT_BUFFER_SIZE]; | ||
177 | static long code; | ||
178 | static long socket_timeout = DEFAULT_SOCKET_TIMEOUT; | ||
179 | static double total_time; | ||
180 | static double time_connect; | ||
181 | static double time_appconnect; | ||
182 | static double time_headers; | ||
183 | static double time_firstbyte; | ||
184 | static char errbuf[MAX_INPUT_BUFFER]; | ||
185 | static CURLcode res; | ||
186 | static char url[DEFAULT_BUFFER_SIZE]; | ||
187 | static char msg[DEFAULT_BUFFER_SIZE]; | ||
188 | static char perfstring[DEFAULT_BUFFER_SIZE]; | ||
189 | static char header_expect[MAX_INPUT_BUFFER] = ""; | ||
190 | static char string_expect[MAX_INPUT_BUFFER] = ""; | ||
191 | static char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; | ||
192 | static int server_expect_yn = 0; | ||
193 | static char user_auth[MAX_INPUT_BUFFER] = ""; | ||
194 | static char proxy_auth[MAX_INPUT_BUFFER] = ""; | ||
195 | static char **http_opt_headers; | ||
196 | static int http_opt_headers_count = 0; | ||
197 | static bool display_html = false; | ||
198 | static int onredirect = STATE_OK; | ||
199 | static int followmethod = FOLLOW_HTTP_CURL; | ||
200 | static int followsticky = STICKY_NONE; | ||
201 | static bool use_ssl = false; | ||
202 | static bool check_cert = false; | ||
203 | static bool continue_after_check_cert = false; | ||
204 | typedef union { | ||
205 | struct curl_slist *to_info; | ||
206 | struct curl_certinfo *to_certinfo; | ||
207 | } cert_ptr_union; | ||
208 | static cert_ptr_union cert_ptr; | ||
209 | static int ssl_version = CURL_SSLVERSION_DEFAULT; | ||
210 | static char *client_cert = NULL; | ||
211 | static char *client_privkey = NULL; | ||
212 | static char *ca_cert = NULL; | ||
213 | static bool verify_peer_and_host = false; | ||
214 | static bool is_openssl_callback = false; | ||
215 | static bool add_sslctx_verify_fun = false; | ||
216 | #if defined(HAVE_SSL) && defined(USE_OPENSSL) | 88 | #if defined(HAVE_SSL) && defined(USE_OPENSSL) |
217 | static X509 *cert = NULL; | 89 | static X509 *cert = NULL; |
218 | #endif /* defined(HAVE_SSL) && defined(USE_OPENSSL) */ | 90 | #endif /* defined(HAVE_SSL) && defined(USE_OPENSSL) */ |
219 | static bool no_body = false; | 91 | |
220 | static int maximum_age = -1; | 92 | typedef struct { |
221 | static int address_family = AF_UNSPEC; | 93 | int errorcode; |
222 | static curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; | 94 | check_curl_config config; |
223 | static int curl_http_version = CURL_HTTP_VERSION_NONE; | 95 | } check_curl_config_wrapper; |
224 | static bool automatic_decompression = false; | 96 | static check_curl_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
225 | static char *cookie_jar_file = NULL; | 97 | |
226 | static bool haproxy_protocol = false; | 98 | static mp_subcheck check_http(check_curl_config /*config*/, check_curl_working_state workingState, |
227 | 99 | int redir_depth); | |
228 | static bool process_arguments(int /*argc*/, char ** /*argv*/); | 100 | |
229 | static void handle_curl_option_return_code(CURLcode res, const char *option); | 101 | typedef struct { |
230 | static int check_http(void); | 102 | int redir_depth; |
231 | static void redir(curlhelp_write_curlbuf * /*header_buf*/); | 103 | check_curl_working_state working_state; |
232 | static char *perfd_time(double elapsed_time); | 104 | int error_code; |
233 | static char *perfd_time_connect(double elapsed_time_connect); | 105 | check_curl_global_state curl_state; |
234 | static char *perfd_time_ssl(double elapsed_time_ssl); | 106 | } redir_wrapper; |
235 | static char *perfd_time_firstbyte(double elapsed_time_firstbyte); | 107 | static redir_wrapper redir(curlhelp_write_curlbuf * /*header_buf*/, check_curl_config /*config*/, |
236 | static char *perfd_time_headers(double elapsed_time_headers); | 108 | int redir_depth, check_curl_working_state working_state); |
237 | static char *perfd_time_transfer(double elapsed_time_transfer); | 109 | |
238 | static char *perfd_size(int page_len); | ||
239 | static void print_help(void); | 110 | static void print_help(void); |
240 | void print_usage(void); | 111 | void print_usage(void); |
112 | |||
241 | static void print_curl_version(void); | 113 | static void print_curl_version(void); |
242 | static int curlhelp_initwritebuffer(curlhelp_write_curlbuf * /*buf*/); | 114 | |
243 | static size_t curlhelp_buffer_write_callback(void * /*buffer*/, size_t /*size*/, size_t /*nmemb*/, void * /*stream*/); | 115 | // typedef struct { |
244 | static void curlhelp_freewritebuffer(curlhelp_write_curlbuf * /*buf*/); | 116 | // int errorcode; |
245 | static int curlhelp_initreadbuffer(curlhelp_read_curlbuf * /*buf*/, const char * /*data*/, size_t /*datalen*/); | 117 | // } check_curl_evaluation_wrapper; |
246 | static size_t curlhelp_buffer_read_callback(void * /*buffer*/, size_t /*size*/, size_t /*nmemb*/, void * /*stream*/); | 118 | // check_curl_evaluation_wrapper check_curl_evaluate(check_curl_config config, |
247 | static void curlhelp_freereadbuffer(curlhelp_read_curlbuf * /*buf*/); | 119 | // mp_check overall[static 1]) {} |
248 | static curlhelp_ssl_library curlhelp_get_ssl_library(void); | ||
249 | static const char *curlhelp_get_ssl_library_string(curlhelp_ssl_library /*ssl_library*/); | ||
250 | int net_noopenssl_check_certificate(cert_ptr_union *, int, int); | ||
251 | |||
252 | static int curlhelp_parse_statusline(const char * /*buf*/, curlhelp_statusline * /*status_line*/); | ||
253 | static void curlhelp_free_statusline(curlhelp_statusline * /*status_line*/); | ||
254 | static char *get_header_value(const struct phr_header *headers, size_t nof_headers, const char *header); | ||
255 | static int check_document_dates(const curlhelp_write_curlbuf * /*header_buf*/, char (*msg)[DEFAULT_BUFFER_SIZE]); | ||
256 | static int get_content_length(const curlhelp_write_curlbuf *header_buf, const curlhelp_write_curlbuf *body_buf); | ||
257 | 120 | ||
258 | #if defined(HAVE_SSL) && defined(USE_OPENSSL) | 121 | #if defined(HAVE_SSL) && defined(USE_OPENSSL) |
259 | int np_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, int days_till_exp_crit); | 122 | mp_state_enum np_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, |
123 | int days_till_exp_crit); | ||
260 | #endif /* defined(HAVE_SSL) && defined(USE_OPENSSL) */ | 124 | #endif /* defined(HAVE_SSL) && defined(USE_OPENSSL) */ |
261 | 125 | ||
262 | static void test_file(char * /*path*/); | ||
263 | |||
264 | int main(int argc, char **argv) { | 126 | int main(int argc, char **argv) { |
265 | int result = STATE_UNKNOWN; | ||
266 | |||
267 | setlocale(LC_ALL, ""); | 127 | setlocale(LC_ALL, ""); |
268 | bindtextdomain(PACKAGE, LOCALEDIR); | 128 | bindtextdomain(PACKAGE, LOCALEDIR); |
269 | textdomain(PACKAGE); | 129 | textdomain(PACKAGE); |
@@ -271,24 +131,30 @@ int main(int argc, char **argv) { | |||
271 | /* Parse extra opts if any */ | 131 | /* Parse extra opts if any */ |
272 | argv = np_extra_opts(&argc, argv, progname); | 132 | argv = np_extra_opts(&argc, argv, progname); |
273 | 133 | ||
274 | /* set defaults */ | ||
275 | snprintf(user_agent, DEFAULT_BUFFER_SIZE, "%s/v%s (monitoring-plugins %s, %s)", progname, NP_VERSION, VERSION, curl_version()); | ||
276 | |||
277 | /* parse arguments */ | 134 | /* parse arguments */ |
278 | if (process_arguments(argc, argv) == false) | 135 | check_curl_config_wrapper tmp_config = process_arguments(argc, argv); |
136 | if (tmp_config.errorcode == ERROR) { | ||
279 | usage4(_("Could not parse arguments")); | 137 | usage4(_("Could not parse arguments")); |
138 | } | ||
280 | 139 | ||
281 | if (display_html) | 140 | const check_curl_config config = tmp_config.config; |
282 | printf("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">", use_ssl ? "https" : "http", host_name ? host_name : server_address, | ||
283 | virtual_port ? virtual_port : server_port, server_url); | ||
284 | 141 | ||
285 | result = check_http(); | 142 | if (config.output_format_is_set) { |
286 | return result; | 143 | mp_set_format(config.output_format); |
144 | } | ||
145 | |||
146 | check_curl_working_state working_state = config.initial_config; | ||
147 | |||
148 | mp_check overall = mp_check_init(); | ||
149 | mp_subcheck sc_test = check_http(config, working_state, 0); | ||
150 | |||
151 | mp_add_subcheck_to_check(&overall, sc_test); | ||
152 | |||
153 | mp_exit(overall); | ||
287 | } | 154 | } |
288 | 155 | ||
289 | #ifdef HAVE_SSL | 156 | #ifdef HAVE_SSL |
290 | # ifdef USE_OPENSSL | 157 | # ifdef USE_OPENSSL |
291 | |||
292 | int verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) { | 158 | int verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) { |
293 | (void)preverify_ok; | 159 | (void)preverify_ok; |
294 | /* TODO: we get all certificates of the chain, so which ones | 160 | /* TODO: we get all certificates of the chain, so which ones |
@@ -301,19 +167,21 @@ int verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) { | |||
301 | # endif | 167 | # endif |
302 | if (verbose >= 2) { | 168 | if (verbose >= 2) { |
303 | puts("* SSL verify callback with certificate:"); | 169 | puts("* SSL verify callback with certificate:"); |
304 | X509_NAME *subject; | ||
305 | X509_NAME *issuer; | ||
306 | printf("* issuer:\n"); | 170 | printf("* issuer:\n"); |
307 | issuer = X509_get_issuer_name(cert); | 171 | X509_NAME *issuer = X509_get_issuer_name(cert); |
308 | X509_NAME_print_ex_fp(stdout, issuer, 5, XN_FLAG_MULTILINE); | 172 | X509_NAME_print_ex_fp(stdout, issuer, 5, XN_FLAG_MULTILINE); |
309 | printf("* curl verify_callback:\n* subject:\n"); | 173 | printf("* curl verify_callback:\n* subject:\n"); |
310 | subject = X509_get_subject_name(cert); | 174 | X509_NAME *subject = X509_get_subject_name(cert); |
311 | X509_NAME_print_ex_fp(stdout, subject, 5, XN_FLAG_MULTILINE); | 175 | X509_NAME_print_ex_fp(stdout, subject, 5, XN_FLAG_MULTILINE); |
312 | puts(""); | 176 | puts(""); |
313 | } | 177 | } |
314 | return 1; | 178 | return 1; |
315 | } | 179 | } |
180 | # endif /* USE_OPENSSL */ | ||
181 | #endif /* HAVE_SSL */ | ||
316 | 182 | ||
183 | #ifdef HAVE_SSL | ||
184 | # ifdef USE_OPENSSL | ||
317 | CURLcode sslctxfun(CURL *curl, SSL_CTX *sslctx, void *parm) { | 185 | CURLcode sslctxfun(CURL *curl, SSL_CTX *sslctx, void *parm) { |
318 | (void)curl; // ignore unused parameter | 186 | (void)curl; // ignore unused parameter |
319 | (void)parm; // ignore unused parameter | 187 | (void)parm; // ignore unused parameter |
@@ -330,877 +198,503 @@ CURLcode sslctxfun(CURL *curl, SSL_CTX *sslctx, void *parm) { | |||
330 | 198 | ||
331 | return CURLE_OK; | 199 | return CURLE_OK; |
332 | } | 200 | } |
333 | |||
334 | # endif /* USE_OPENSSL */ | 201 | # endif /* USE_OPENSSL */ |
335 | #endif /* HAVE_SSL */ | 202 | #endif /* HAVE_SSL */ |
336 | 203 | ||
337 | /* returns a string "HTTP/1.x" or "HTTP/2" */ | 204 | mp_subcheck check_http(const check_curl_config config, check_curl_working_state workingState, |
338 | static char *string_statuscode(int major, int minor) { | 205 | int redir_depth) { |
339 | static char buf[10]; | ||
340 | |||
341 | switch (major) { | ||
342 | case 1: | ||
343 | snprintf(buf, sizeof(buf), "HTTP/%d.%d", major, minor); | ||
344 | break; | ||
345 | case 2: | ||
346 | case 3: | ||
347 | snprintf(buf, sizeof(buf), "HTTP/%d", major); | ||
348 | break; | ||
349 | default: | ||
350 | /* assuming here HTTP/N with N>=4 */ | ||
351 | snprintf(buf, sizeof(buf), "HTTP/%d", major); | ||
352 | break; | ||
353 | } | ||
354 | 206 | ||
355 | return buf; | 207 | // ======================= |
356 | } | 208 | // Initialisation for curl |
209 | // ======================= | ||
210 | check_curl_configure_curl_wrapper conf_curl_struct = check_curl_configure_curl( | ||
211 | config.curl_config, workingState, config.check_cert, config.on_redirect_dependent, | ||
212 | config.followmethod, config.max_depth); | ||
357 | 213 | ||
358 | /* Checks if the server 'reply' is one of the expected 'statuscodes' */ | 214 | check_curl_global_state curl_state = conf_curl_struct.curl_state; |
359 | static int expected_statuscode(const char *reply, const char *statuscodes) { | 215 | workingState = conf_curl_struct.working_state; |
360 | char *expected; | ||
361 | char *code; | ||
362 | int result = 0; | ||
363 | 216 | ||
364 | if ((expected = strdup(statuscodes)) == NULL) | 217 | mp_subcheck sc_result = mp_subcheck_init(); |
365 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); | ||
366 | 218 | ||
367 | for (code = strtok(expected, ","); code != NULL; code = strtok(NULL, ",")) | 219 | char *url = fmt_url(workingState); |
368 | if (strstr(reply, code) != NULL) { | 220 | xasprintf(&sc_result.output, "Testing %s", url); |
369 | result = 1; | 221 | // TODO add some output here URL or something |
370 | break; | 222 | free(url); |
371 | } | ||
372 | |||
373 | free(expected); | ||
374 | return result; | ||
375 | } | ||
376 | 223 | ||
377 | void handle_curl_option_return_code(CURLcode res, const char *option) { | 224 | // ============== |
378 | if (res != CURLE_OK) { | 225 | // do the request |
379 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Error while setting cURL option '%s': cURL returned %d - %s"), option, res, | 226 | // ============== |
380 | curl_easy_strerror(res)); | 227 | CURLcode res = curl_easy_perform(curl_state.curl); |
381 | die(STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
382 | } | ||
383 | } | ||
384 | |||
385 | int lookup_host(const char *host, char *buf, size_t buflen) { | ||
386 | struct addrinfo hints, *res, *result; | ||
387 | char addrstr[100]; | ||
388 | size_t addrstr_len; | ||
389 | int errcode; | ||
390 | void *ptr = {0}; | ||
391 | size_t buflen_remaining = buflen - 1; | ||
392 | |||
393 | memset(&hints, 0, sizeof(hints)); | ||
394 | hints.ai_family = address_family; | ||
395 | hints.ai_socktype = SOCK_STREAM; | ||
396 | hints.ai_flags |= AI_CANONNAME; | ||
397 | |||
398 | errcode = getaddrinfo(host, NULL, &hints, &result); | ||
399 | if (errcode != 0) | ||
400 | return errcode; | ||
401 | |||
402 | strcpy(buf, ""); | ||
403 | res = result; | ||
404 | |||
405 | while (res) { | ||
406 | switch (res->ai_family) { | ||
407 | case AF_INET: | ||
408 | ptr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; | ||
409 | break; | ||
410 | case AF_INET6: | ||
411 | ptr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; | ||
412 | break; | ||
413 | } | ||
414 | |||
415 | inet_ntop(res->ai_family, ptr, addrstr, 100); | ||
416 | if (verbose >= 1) { | ||
417 | printf("* getaddrinfo IPv%d address: %s\n", res->ai_family == PF_INET6 ? 6 : 4, addrstr); | ||
418 | } | ||
419 | |||
420 | // Append all IPs to buf as a comma-separated string | ||
421 | addrstr_len = strlen(addrstr); | ||
422 | if (buflen_remaining > addrstr_len + 1) { | ||
423 | if (buf[0] != '\0') { | ||
424 | strncat(buf, ",", buflen_remaining); | ||
425 | buflen_remaining -= 1; | ||
426 | } | ||
427 | strncat(buf, addrstr, buflen_remaining); | ||
428 | buflen_remaining -= addrstr_len; | ||
429 | } | ||
430 | |||
431 | res = res->ai_next; | ||
432 | } | ||
433 | |||
434 | freeaddrinfo(result); | ||
435 | |||
436 | return 0; | ||
437 | } | ||
438 | |||
439 | static void cleanup(void) { | ||
440 | if (status_line_initialized) | ||
441 | curlhelp_free_statusline(&status_line); | ||
442 | status_line_initialized = false; | ||
443 | if (curl_easy_initialized) | ||
444 | curl_easy_cleanup(curl); | ||
445 | curl_easy_initialized = false; | ||
446 | if (curl_global_initialized) | ||
447 | curl_global_cleanup(); | ||
448 | curl_global_initialized = false; | ||
449 | if (body_buf_initialized) | ||
450 | curlhelp_freewritebuffer(&body_buf); | ||
451 | body_buf_initialized = false; | ||
452 | if (header_buf_initialized) | ||
453 | curlhelp_freewritebuffer(&header_buf); | ||
454 | header_buf_initialized = false; | ||
455 | if (put_buf_initialized) | ||
456 | curlhelp_freereadbuffer(&put_buf); | ||
457 | put_buf_initialized = false; | ||
458 | } | ||
459 | 228 | ||
460 | int check_http(void) { | 229 | if (verbose >= 2 && workingState.http_post_data) { |
461 | int result = STATE_OK; | 230 | printf("**** REQUEST CONTENT ****\n%s\n", workingState.http_post_data); |
462 | int result_ssl = STATE_OK; | ||
463 | int page_len = 0; | ||
464 | int i; | ||
465 | char *force_host_header = NULL; | ||
466 | struct curl_slist *host = NULL; | ||
467 | char addrstr[DEFAULT_BUFFER_SIZE / 2]; | ||
468 | char dnscache[DEFAULT_BUFFER_SIZE]; | ||
469 | |||
470 | /* initialize curl */ | ||
471 | if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) | ||
472 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); | ||
473 | curl_global_initialized = true; | ||
474 | |||
475 | if ((curl = curl_easy_init()) == NULL) { | ||
476 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_easy_init failed\n"); | ||
477 | } | 231 | } |
478 | curl_easy_initialized = true; | ||
479 | 232 | ||
480 | /* register cleanup function to shut down libcurl properly */ | 233 | mp_subcheck sc_curl = mp_subcheck_init(); |
481 | atexit(cleanup); | ||
482 | 234 | ||
483 | if (verbose >= 1) | 235 | /* Curl errors, result in critical Nagios state */ |
484 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_VERBOSE, 1), "CURLOPT_VERBOSE"); | 236 | if (res != CURLE_OK) { |
485 | 237 | xasprintf(&sc_curl.output, _("Error while performing connection: cURL returned %d - %s"), | |
486 | /* print everything on stdout like check_http would do */ | 238 | res, errbuf[0] ? errbuf : curl_easy_strerror(res)); |
487 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_STDERR, stdout), "CURLOPT_STDERR"); | 239 | sc_curl = mp_set_subcheck_state(sc_curl, STATE_CRITICAL); |
488 | 240 | mp_add_subcheck_to_subcheck(&sc_result, sc_curl); | |
489 | if (automatic_decompression) | 241 | return sc_result; |
490 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 6) | ||
491 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""), "CURLOPT_ACCEPT_ENCODING"); | ||
492 | #else | ||
493 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_ENCODING, ""), "CURLOPT_ENCODING"); | ||
494 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 6) */ | ||
495 | |||
496 | /* initialize buffer for body of the answer */ | ||
497 | if (curlhelp_initwritebuffer(&body_buf) < 0) | ||
498 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for body\n"); | ||
499 | body_buf_initialized = true; | ||
500 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), | ||
501 | "CURLOPT_WRITEFUNCTION"); | ||
502 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&body_buf), "CURLOPT_WRITEDATA"); | ||
503 | |||
504 | /* initialize buffer for header of the answer */ | ||
505 | if (curlhelp_initwritebuffer(&header_buf) < 0) | ||
506 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for header\n"); | ||
507 | header_buf_initialized = true; | ||
508 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), | ||
509 | "CURLOPT_HEADERFUNCTION"); | ||
510 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)&header_buf), "CURLOPT_WRITEHEADER"); | ||
511 | |||
512 | /* set the error buffer */ | ||
513 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf), "CURLOPT_ERRORBUFFER"); | ||
514 | |||
515 | /* set timeouts */ | ||
516 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, socket_timeout), "CURLOPT_CONNECTTIMEOUT"); | ||
517 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_TIMEOUT, socket_timeout), "CURLOPT_TIMEOUT"); | ||
518 | |||
519 | /* enable haproxy protocol */ | ||
520 | if (haproxy_protocol) { | ||
521 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_HAPROXYPROTOCOL, 1L), "CURLOPT_HAPROXYPROTOCOL"); | ||
522 | } | 242 | } |
523 | 243 | ||
524 | // fill dns resolve cache to make curl connect to the given server_address instead of the host_name, only required for ssl, because we | 244 | /* get status line of answer, check sanity of HTTP code */ |
525 | // use the host_name later on to make SNI happy | 245 | if (curlhelp_parse_statusline(curl_state.header_buf->buf, curl_state.status_line) < 0) { |
526 | if (use_ssl && host_name != NULL) { | 246 | sc_result = mp_set_subcheck_state(sc_result, STATE_CRITICAL); |
527 | if ((res = lookup_host(server_address, addrstr, DEFAULT_BUFFER_SIZE / 2)) != 0) { | 247 | /* we cannot know the major/minor version here for sure as we cannot parse the first |
528 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Unable to lookup IP address for '%s': getaddrinfo returned %d - %s"), server_address, res, | 248 | * line */ |
529 | gai_strerror(res)); | 249 | xasprintf(&sc_result.output, "HTTP/x.x unknown - Unparsable status line"); |
530 | die(STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | 250 | return sc_result; |
531 | } | ||
532 | snprintf(dnscache, DEFAULT_BUFFER_SIZE, "%s:%d:%s", host_name, server_port, addrstr); | ||
533 | host = curl_slist_append(NULL, dnscache); | ||
534 | curl_easy_setopt(curl, CURLOPT_RESOLVE, host); | ||
535 | if (verbose >= 1) | ||
536 | printf("* curl CURLOPT_RESOLVE: %s\n", dnscache); | ||
537 | } | 251 | } |
538 | 252 | ||
539 | // If server_address is an IPv6 address it must be surround by square brackets | 253 | curl_state.status_line_initialized = true; |
540 | struct in6_addr tmp_in_addr; | ||
541 | if (inet_pton(AF_INET6, server_address, &tmp_in_addr) == 1) { | ||
542 | char *new_server_address = malloc(strlen(server_address) + 3); | ||
543 | if (new_server_address == NULL) { | ||
544 | die(STATE_UNKNOWN, "HTTP UNKNOWN - Unable to allocate memory\n"); | ||
545 | } | ||
546 | snprintf(new_server_address, strlen(server_address) + 3, "[%s]", server_address); | ||
547 | free(server_address); | ||
548 | server_address = new_server_address; | ||
549 | } | ||
550 | 254 | ||
551 | /* compose URL: use the address we want to connect to, set Host: header later */ | 255 | size_t page_len = get_content_length(curl_state.header_buf, curl_state.body_buf); |
552 | snprintf(url, DEFAULT_BUFFER_SIZE, "%s://%s:%d%s", use_ssl ? "https" : "http", | ||
553 | (use_ssl & (host_name != NULL)) ? host_name : server_address, server_port, server_url); | ||
554 | |||
555 | if (verbose >= 1) | ||
556 | printf("* curl CURLOPT_URL: %s\n", url); | ||
557 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_URL, url), "CURLOPT_URL"); | ||
558 | |||
559 | /* extract proxy information for legacy proxy https requests */ | ||
560 | if (!strcmp(http_method, "CONNECT") || strstr(server_url, "http") == server_url) { | ||
561 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXY, server_address), "CURLOPT_PROXY"); | ||
562 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXYPORT, (long)server_port), "CURLOPT_PROXYPORT"); | ||
563 | if (verbose >= 2) | ||
564 | printf("* curl CURLOPT_PROXY: %s:%d\n", server_address, server_port); | ||
565 | http_method = "GET"; | ||
566 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_URL, server_url), "CURLOPT_URL"); | ||
567 | } | ||
568 | 256 | ||
569 | /* disable body for HEAD request */ | 257 | double total_time; |
570 | if (http_method && !strcmp(http_method, "HEAD")) { | 258 | handle_curl_option_return_code( |
571 | no_body = true; | 259 | curl_easy_getinfo(curl_state.curl, CURLINFO_TOTAL_TIME, &total_time), |
572 | } | 260 | "CURLINFO_TOTAL_TIME"); |
573 | 261 | ||
574 | /* set HTTP protocol version */ | 262 | xasprintf( |
575 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, curl_http_version), "CURLOPT_HTTP_VERSION"); | 263 | &sc_curl.output, "%s %d %s - %ld bytes in %.3f second response time", |
576 | 264 | string_statuscode(curl_state.status_line->http_major, curl_state.status_line->http_minor), | |
577 | /* set HTTP method */ | 265 | curl_state.status_line->http_code, curl_state.status_line->msg, page_len, total_time); |
578 | if (http_method) { | 266 | sc_curl = mp_set_subcheck_state(sc_curl, STATE_OK); |
579 | if (!strcmp(http_method, "POST")) | 267 | mp_add_subcheck_to_subcheck(&sc_result, sc_curl); |
580 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_POST, 1), "CURLOPT_POST"); | ||
581 | else if (!strcmp(http_method, "PUT")) | ||
582 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_UPLOAD, 1), "CURLOPT_UPLOAD"); | ||
583 | else | ||
584 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, http_method), "CURLOPT_CUSTOMREQUEST"); | ||
585 | } | ||
586 | 268 | ||
587 | /* check if Host header is explicitly set in options */ | 269 | // ========== |
588 | if (http_opt_headers_count) { | 270 | // Evaluation |
589 | for (i = 0; i < http_opt_headers_count; i++) { | 271 | // ========== |
590 | if (strncmp(http_opt_headers[i], "Host:", 5) == 0) { | ||
591 | force_host_header = http_opt_headers[i]; | ||
592 | } | ||
593 | } | ||
594 | } | ||
595 | |||
596 | /* set hostname (virtual hosts), not needed if CURLOPT_CONNECT_TO is used, but left in anyway */ | ||
597 | if (host_name != NULL && force_host_header == NULL) { | ||
598 | if ((virtual_port != HTTP_PORT && !use_ssl) || (virtual_port != HTTPS_PORT && use_ssl)) { | ||
599 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s:%d", host_name, virtual_port); | ||
600 | } else { | ||
601 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s", host_name); | ||
602 | } | ||
603 | header_list = curl_slist_append(header_list, http_header); | ||
604 | } | ||
605 | |||
606 | /* always close connection, be nice to servers */ | ||
607 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Connection: close"); | ||
608 | header_list = curl_slist_append(header_list, http_header); | ||
609 | |||
610 | /* attach additional headers supplied by the user */ | ||
611 | /* optionally send any other header tag */ | ||
612 | if (http_opt_headers_count) { | ||
613 | for (i = 0; i < http_opt_headers_count; i++) { | ||
614 | header_list = curl_slist_append(header_list, http_opt_headers[i]); | ||
615 | } | ||
616 | /* This cannot be free'd here because a redirection will then try to access this and segfault */ | ||
617 | /* Covered in a testcase in tests/check_http.t */ | ||
618 | /* free(http_opt_headers); */ | ||
619 | } | ||
620 | |||
621 | /* set HTTP headers */ | ||
622 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list), "CURLOPT_HTTPHEADER"); | ||
623 | 272 | ||
624 | #ifdef LIBCURL_FEATURE_SSL | 273 | #ifdef LIBCURL_FEATURE_SSL |
274 | if (workingState.use_ssl && config.check_cert) { | ||
275 | mp_subcheck sc_certificate = check_curl_certificate_checks( | ||
276 | curl_state.curl, cert, config.days_till_exp_warn, config.days_till_exp_crit); | ||
625 | 277 | ||
626 | /* set SSL version, warn about insecure or unsupported versions */ | 278 | mp_add_subcheck_to_subcheck(&sc_result, sc_certificate); |
627 | if (use_ssl) { | 279 | if (!config.continue_after_check_cert) { |
628 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSLVERSION, ssl_version), "CURLOPT_SSLVERSION"); | 280 | return sc_result; |
629 | } | ||
630 | |||
631 | /* client certificate and key to present to server (SSL) */ | ||
632 | if (client_cert) | ||
633 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSLCERT, client_cert), "CURLOPT_SSLCERT"); | ||
634 | if (client_privkey) | ||
635 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSLKEY, client_privkey), "CURLOPT_SSLKEY"); | ||
636 | if (ca_cert) { | ||
637 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CAINFO, ca_cert), "CURLOPT_CAINFO"); | ||
638 | } | ||
639 | if (ca_cert || verify_peer_and_host) { | ||
640 | /* per default if we have a CA verify both the peer and the | ||
641 | * hostname in the certificate, can be switched off later */ | ||
642 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1), "CURLOPT_SSL_VERIFYPEER"); | ||
643 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2), "CURLOPT_SSL_VERIFYHOST"); | ||
644 | } else { | ||
645 | /* backward-compatible behaviour, be tolerant in checks | ||
646 | * TODO: depending on more options have aspects we want | ||
647 | * to be less tolerant about ssl verfications | ||
648 | */ | ||
649 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0), "CURLOPT_SSL_VERIFYPEER"); | ||
650 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0), "CURLOPT_SSL_VERIFYHOST"); | ||
651 | } | ||
652 | |||
653 | /* detect SSL library used by libcurl */ | ||
654 | ssl_library = curlhelp_get_ssl_library(); | ||
655 | |||
656 | /* try hard to get a stack of certificates to verify against */ | ||
657 | if (check_cert) { | ||
658 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) | ||
659 | /* inform curl to report back certificates */ | ||
660 | switch (ssl_library) { | ||
661 | case CURLHELP_SSL_LIBRARY_OPENSSL: | ||
662 | case CURLHELP_SSL_LIBRARY_LIBRESSL: | ||
663 | /* set callback to extract certificate with OpenSSL context function (works with | ||
664 | * OpenSSL-style libraries only!) */ | ||
665 | # ifdef USE_OPENSSL | ||
666 | /* libcurl and monitoring plugins built with OpenSSL, good */ | ||
667 | add_sslctx_verify_fun = true; | ||
668 | is_openssl_callback = true; | ||
669 | # endif /* USE_OPENSSL */ | ||
670 | /* libcurl is built with OpenSSL, monitoring plugins, so falling | ||
671 | * back to manually extracting certificate information */ | ||
672 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
673 | break; | ||
674 | |||
675 | case CURLHELP_SSL_LIBRARY_NSS: | ||
676 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
677 | /* NSS: support for CERTINFO is implemented since 7.34.0 */ | ||
678 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
679 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
680 | die(STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (libcurl linked with SSL library '%s' is too old)\n", | ||
681 | curlhelp_get_ssl_library_string(ssl_library)); | ||
682 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
683 | break; | ||
684 | |||
685 | case CURLHELP_SSL_LIBRARY_GNUTLS: | ||
686 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) | ||
687 | /* GnuTLS: support for CERTINFO is implemented since 7.42.0 */ | ||
688 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
689 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) */ | ||
690 | die(STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (libcurl linked with SSL library '%s' is too old)\n", | ||
691 | curlhelp_get_ssl_library_string(ssl_library)); | ||
692 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) */ | ||
693 | break; | ||
694 | |||
695 | case CURLHELP_SSL_LIBRARY_UNKNOWN: | ||
696 | default: | ||
697 | die(STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (unknown SSL library '%s', must implement first)\n", | ||
698 | curlhelp_get_ssl_library_string(ssl_library)); | ||
699 | break; | ||
700 | } | 281 | } |
701 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | ||
702 | /* old libcurl, our only hope is OpenSSL, otherwise we are out of luck */ | ||
703 | if (ssl_library == CURLHELP_SSL_LIBRARY_OPENSSL || ssl_library == CURLHELP_SSL_LIBRARY_LIBRESSL) | ||
704 | add_sslctx_verify_fun = true; | ||
705 | else | ||
706 | die(STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (no CURLOPT_SSL_CTX_FUNCTION, no OpenSSL library or libcurl " | ||
707 | "too old and has no CURLOPT_CERTINFO)\n"); | ||
708 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | ||
709 | } | 282 | } |
710 | |||
711 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 10, 6) /* required for CURLOPT_SSL_CTX_FUNCTION */ | ||
712 | // ssl ctx function is not available with all ssl backends | ||
713 | if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, NULL) != CURLE_UNKNOWN_OPTION) | ||
714 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun), "CURLOPT_SSL_CTX_FUNCTION"); | ||
715 | # endif | ||
716 | |||
717 | #endif /* LIBCURL_FEATURE_SSL */ | ||
718 | |||
719 | /* set default or user-given user agent identification */ | ||
720 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent), "CURLOPT_USERAGENT"); | ||
721 | |||
722 | /* proxy-authentication */ | ||
723 | if (strcmp(proxy_auth, "")) | ||
724 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_auth), "CURLOPT_PROXYUSERPWD"); | ||
725 | |||
726 | /* authentication */ | ||
727 | if (strcmp(user_auth, "")) | ||
728 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_USERPWD, user_auth), "CURLOPT_USERPWD"); | ||
729 | |||
730 | /* TODO: parameter auth method, bitfield of following methods: | ||
731 | * CURLAUTH_BASIC (default) | ||
732 | * CURLAUTH_DIGEST | ||
733 | * CURLAUTH_DIGEST_IE | ||
734 | * CURLAUTH_NEGOTIATE | ||
735 | * CURLAUTH_NTLM | ||
736 | * CURLAUTH_NTLM_WB | ||
737 | * | ||
738 | * convenience tokens for typical sets of methods: | ||
739 | * CURLAUTH_ANYSAFE: most secure, without BASIC | ||
740 | * or CURLAUTH_ANY: most secure, even BASIC if necessary | ||
741 | * | ||
742 | * handle_curl_option_return_code (curl_easy_setopt( curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST ), "CURLOPT_HTTPAUTH"); | ||
743 | */ | ||
744 | |||
745 | /* handle redirections */ | ||
746 | if (onredirect == STATE_DEPENDENT) { | ||
747 | if (followmethod == FOLLOW_LIBCURL) { | ||
748 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1), "CURLOPT_FOLLOWLOCATION"); | ||
749 | |||
750 | /* default -1 is infinite, not good, could lead to zombie plugins! | ||
751 | Setting it to one bigger than maximal limit to handle errors nicely below | ||
752 | */ | ||
753 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_MAXREDIRS, max_depth + 1), "CURLOPT_MAXREDIRS"); | ||
754 | |||
755 | /* for now allow only http and https (we are a http(s) check plugin in the end) */ | ||
756 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 85, 0) | ||
757 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS_STR, "http,https"), | ||
758 | "CURLOPT_REDIR_PROTOCOLS_STR"); | ||
759 | #elif LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 4) | ||
760 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS), | ||
761 | "CURLOPT_REDIRECT_PROTOCOLS"); | ||
762 | #endif | 283 | #endif |
763 | 284 | ||
764 | /* TODO: handle the following aspects of redirection, make them | 285 | /* we got the data and we executed the request in a given time, so we can append |
765 | * command line options too later: | 286 | * performance data to the answer always |
766 | CURLOPT_POSTREDIR: method switch | 287 | */ |
767 | CURLINFO_REDIRECT_URL: custom redirect option | ||
768 | CURLOPT_REDIRECT_PROTOCOLS: allow people to step outside safe protocols | ||
769 | CURLINFO_REDIRECT_COUNT: get the number of redirects, print it, maybe a range option here is nice like for expected page size? | ||
770 | */ | ||
771 | } else { | ||
772 | /* old style redirection is handled below */ | ||
773 | } | ||
774 | } | ||
775 | |||
776 | /* no-body */ | ||
777 | if (no_body) | ||
778 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_NOBODY, 1), "CURLOPT_NOBODY"); | ||
779 | |||
780 | /* IPv4 or IPv6 forced DNS resolution */ | ||
781 | if (address_family == AF_UNSPEC) | ||
782 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER), | ||
783 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_WHATEVER)"); | ||
784 | else if (address_family == AF_INET) | ||
785 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4), | ||
786 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V4)"); | ||
787 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) | ||
788 | else if (address_family == AF_INET6) | ||
789 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6), | ||
790 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V6)"); | ||
791 | #endif | ||
792 | 288 | ||
793 | /* either send http POST data (any data, not only POST)*/ | 289 | // total time the query took |
794 | if (!strcmp(http_method, "POST") || !strcmp(http_method, "PUT")) { | 290 | mp_perfdata pd_total_time = perfdata_init(); |
795 | /* set content of payload for POST and PUT */ | 291 | mp_perfdata_value pd_val_total_time = mp_create_pd_value(total_time); |
796 | if (http_content_type) { | 292 | pd_total_time.value = pd_val_total_time; |
797 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Content-Type: %s", http_content_type); | 293 | pd_total_time = mp_pd_set_thresholds(pd_total_time, config.thlds); |
798 | header_list = curl_slist_append(header_list, http_header); | 294 | pd_total_time.label = "time"; |
799 | } | 295 | pd_total_time.uom = "s"; |
800 | /* NULL indicates "HTTP Continue" in libcurl, provide an empty string | 296 | |
801 | * in case of no POST/PUT data */ | 297 | mp_subcheck sc_total_time = mp_subcheck_init(); |
802 | if (!http_post_data) | 298 | sc_total_time = mp_set_subcheck_state(sc_total_time, mp_get_pd_status(pd_total_time)); |
803 | http_post_data = ""; | 299 | xasprintf(&sc_total_time.output, "Total connection time: %fs", total_time); |
804 | if (!strcmp(http_method, "POST")) { | 300 | mp_add_perfdata_to_subcheck(&sc_total_time, pd_total_time); |
805 | /* POST method, set payload with CURLOPT_POSTFIELDS */ | 301 | |
806 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_POSTFIELDS, http_post_data), "CURLOPT_POSTFIELDS"); | 302 | mp_add_subcheck_to_subcheck(&sc_result, sc_total_time); |
807 | } else if (!strcmp(http_method, "PUT")) { | 303 | |
808 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback)curlhelp_buffer_read_callback), | 304 | if (config.show_extended_perfdata) { |
809 | "CURLOPT_READFUNCTION"); | 305 | // overall connection time |
810 | if (curlhelp_initreadbuffer(&put_buf, http_post_data, strlen(http_post_data)) < 0) | 306 | mp_perfdata pd_time_connect = perfdata_init(); |
811 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating read buffer for PUT\n"); | 307 | double time_connect; |
812 | put_buf_initialized = true; | 308 | handle_curl_option_return_code( |
813 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&put_buf), "CURLOPT_READDATA"); | 309 | curl_easy_getinfo(curl_state.curl, CURLINFO_CONNECT_TIME, &time_connect), |
814 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_INFILESIZE, (curl_off_t)strlen(http_post_data)), | 310 | "CURLINFO_CONNECT_TIME"); |
815 | "CURLOPT_INFILESIZE"); | 311 | |
312 | mp_perfdata_value pd_val_time_connect = mp_create_pd_value(time_connect); | ||
313 | pd_time_connect.value = pd_val_time_connect; | ||
314 | pd_time_connect.label = "time_connect"; | ||
315 | pd_time_connect.uom = "s"; | ||
316 | pd_time_connect = mp_set_pd_max_value( | ||
317 | pd_time_connect, mp_create_pd_value(config.curl_config.socket_timeout)); | ||
318 | |||
319 | pd_time_connect = mp_pd_set_thresholds(pd_time_connect, config.thlds); | ||
320 | mp_add_perfdata_to_subcheck(&sc_result, pd_time_connect); | ||
321 | |||
322 | // application connection time, used to compute other timings | ||
323 | double time_appconnect; | ||
324 | handle_curl_option_return_code( | ||
325 | curl_easy_getinfo(curl_state.curl, CURLINFO_APPCONNECT_TIME, &time_appconnect), | ||
326 | "CURLINFO_APPCONNECT_TIME"); | ||
327 | |||
328 | if (workingState.use_ssl) { | ||
329 | mp_perfdata pd_time_tls = perfdata_init(); | ||
330 | { | ||
331 | mp_perfdata_value pd_val_time_tls = | ||
332 | mp_create_pd_value(time_appconnect - time_connect); | ||
333 | |||
334 | pd_time_tls.value = pd_val_time_tls; | ||
335 | } | ||
336 | pd_time_tls.label = "time_tls"; | ||
337 | pd_time_tls.uom = "s"; | ||
338 | mp_add_perfdata_to_subcheck(&sc_result, pd_time_tls); | ||
816 | } | 339 | } |
817 | } | ||
818 | |||
819 | /* cookie handling */ | ||
820 | if (cookie_jar_file != NULL) { | ||
821 | /* enable reading cookies from a file, and if the filename is an empty string, only enable the curl cookie engine */ | ||
822 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_jar_file), "CURLOPT_COOKIEFILE"); | ||
823 | /* now enable saving cookies to a file, but only if the filename is not an empty string, since writing it would fail */ | ||
824 | if (*cookie_jar_file) | ||
825 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_jar_file), "CURLOPT_COOKIEJAR"); | ||
826 | } | ||
827 | |||
828 | /* do the request */ | ||
829 | res = curl_easy_perform(curl); | ||
830 | 340 | ||
831 | if (verbose >= 2 && http_post_data) | 341 | mp_perfdata pd_time_headers = perfdata_init(); |
832 | printf("**** REQUEST CONTENT ****\n%s\n", http_post_data); | 342 | { |
343 | double time_headers; | ||
344 | handle_curl_option_return_code( | ||
345 | curl_easy_getinfo(curl_state.curl, CURLINFO_PRETRANSFER_TIME, &time_headers), | ||
346 | "CURLINFO_PRETRANSFER_TIME"); | ||
833 | 347 | ||
834 | /* free header and server IP resolve lists, we don't need it anymore */ | 348 | mp_perfdata_value pd_val_time_headers = |
835 | curl_slist_free_all(header_list); | 349 | mp_create_pd_value(time_headers - time_appconnect); |
836 | header_list = NULL; | ||
837 | curl_slist_free_all(server_ips); | ||
838 | server_ips = NULL; | ||
839 | if (host) { | ||
840 | curl_slist_free_all(host); | ||
841 | host = NULL; | ||
842 | } | ||
843 | 350 | ||
844 | /* Curl errors, result in critical Nagios state */ | 351 | pd_time_headers.value = pd_val_time_headers; |
845 | if (res != CURLE_OK) { | 352 | } |
846 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host on port %d: cURL returned %d - %s"), server_port, | 353 | pd_time_headers.label = "time_headers"; |
847 | res, errbuf[0] ? errbuf : curl_easy_strerror(res)); | 354 | pd_time_headers.uom = "s"; |
848 | die(STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | 355 | mp_add_perfdata_to_subcheck(&sc_result, pd_time_headers); |
849 | } | ||
850 | 356 | ||
851 | /* certificate checks */ | 357 | mp_perfdata pd_time_firstbyte = perfdata_init(); |
852 | #ifdef LIBCURL_FEATURE_SSL | 358 | double time_firstbyte; |
853 | if (use_ssl) { | 359 | handle_curl_option_return_code( |
854 | if (check_cert) { | 360 | curl_easy_getinfo(curl_state.curl, CURLINFO_STARTTRANSFER_TIME, &time_firstbyte), |
855 | if (is_openssl_callback) { | 361 | "CURLINFO_STARTTRANSFER_TIME"); |
856 | # ifdef USE_OPENSSL | ||
857 | /* check certificate with OpenSSL functions, curl has been built against OpenSSL | ||
858 | * and we actually have OpenSSL in the monitoring tools | ||
859 | */ | ||
860 | result_ssl = np_net_ssl_check_certificate(cert, days_till_exp_warn, days_till_exp_crit); | ||
861 | if (!continue_after_check_cert) { | ||
862 | return result_ssl; | ||
863 | } | ||
864 | # else /* USE_OPENSSL */ | ||
865 | die(STATE_CRITICAL, | ||
866 | "HTTP CRITICAL - Cannot retrieve certificates - OpenSSL callback used and not linked against OpenSSL\n"); | ||
867 | # endif /* USE_OPENSSL */ | ||
868 | } else { | ||
869 | int i; | ||
870 | struct curl_slist *slist; | ||
871 | 362 | ||
872 | cert_ptr.to_info = NULL; | 363 | mp_perfdata_value pd_val_time_firstbyte = mp_create_pd_value(time_firstbyte); |
873 | res = curl_easy_getinfo(curl, CURLINFO_CERTINFO, &cert_ptr.to_info); | 364 | pd_time_firstbyte.value = pd_val_time_firstbyte; |
874 | if (!res && cert_ptr.to_info) { | 365 | pd_time_firstbyte.label = "time_firstbyte"; |
875 | # ifdef USE_OPENSSL | 366 | pd_time_firstbyte.uom = "s"; |
876 | /* We have no OpenSSL in libcurl, but we can use OpenSSL for X509 cert parsing | 367 | mp_add_perfdata_to_subcheck(&sc_result, pd_time_firstbyte); |
877 | * We only check the first certificate and assume it's the one of the server | ||
878 | */ | ||
879 | const char *raw_cert = NULL; | ||
880 | for (i = 0; i < cert_ptr.to_certinfo->num_of_certs; i++) { | ||
881 | for (slist = cert_ptr.to_certinfo->certinfo[i]; slist; slist = slist->next) { | ||
882 | if (verbose >= 2) | ||
883 | printf("%d ** %s\n", i, slist->data); | ||
884 | if (strncmp(slist->data, "Cert:", 5) == 0) { | ||
885 | raw_cert = &slist->data[5]; | ||
886 | goto GOT_FIRST_CERT; | ||
887 | } | ||
888 | } | ||
889 | } | ||
890 | GOT_FIRST_CERT: | ||
891 | if (!raw_cert) { | ||
892 | snprintf(msg, DEFAULT_BUFFER_SIZE, | ||
893 | _("Cannot retrieve certificates from CERTINFO information - certificate data was empty")); | ||
894 | die(STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
895 | } | ||
896 | BIO *cert_BIO = BIO_new(BIO_s_mem()); | ||
897 | BIO_write(cert_BIO, raw_cert, strlen(raw_cert)); | ||
898 | cert = PEM_read_bio_X509(cert_BIO, NULL, NULL, NULL); | ||
899 | if (!cert) { | ||
900 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Cannot read certificate from CERTINFO information - BIO error")); | ||
901 | die(STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
902 | } | ||
903 | BIO_free(cert_BIO); | ||
904 | result_ssl = np_net_ssl_check_certificate(cert, days_till_exp_warn, days_till_exp_crit); | ||
905 | if (!continue_after_check_cert) { | ||
906 | return result_ssl; | ||
907 | } | ||
908 | # else /* USE_OPENSSL */ | ||
909 | /* We assume we don't have OpenSSL and np_net_ssl_check_certificate at our disposal, | ||
910 | * so we use the libcurl CURLINFO data | ||
911 | */ | ||
912 | result_ssl = net_noopenssl_check_certificate(&cert_ptr, days_till_exp_warn, days_till_exp_crit); | ||
913 | if (!continue_after_check_cert) { | ||
914 | return result_ssl; | ||
915 | } | ||
916 | # endif /* USE_OPENSSL */ | ||
917 | } else { | ||
918 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Cannot retrieve certificates - cURL returned %d - %s"), res, | ||
919 | curl_easy_strerror(res)); | ||
920 | die(STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
921 | } | ||
922 | } | ||
923 | } | ||
924 | } | ||
925 | #endif /* LIBCURL_FEATURE_SSL */ | ||
926 | 368 | ||
927 | /* we got the data and we executed the request in a given time, so we can append | 369 | mp_perfdata pd_time_transfer = perfdata_init(); |
928 | * performance data to the answer always | 370 | pd_time_transfer.value = mp_create_pd_value(total_time - time_firstbyte); |
929 | */ | 371 | pd_time_transfer.label = "time_transfer"; |
930 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time), "CURLINFO_TOTAL_TIME"); | 372 | pd_time_transfer.uom = "s"; |
931 | page_len = get_content_length(&header_buf, &body_buf); | 373 | mp_add_perfdata_to_subcheck(&sc_result, pd_time_transfer); |
932 | if (show_extended_perfdata) { | ||
933 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &time_connect), "CURLINFO_CONNECT_TIME"); | ||
934 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_APPCONNECT_TIME, &time_appconnect), "CURLINFO_APPCONNECT_TIME"); | ||
935 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &time_headers), "CURLINFO_PRETRANSFER_TIME"); | ||
936 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &time_firstbyte), | ||
937 | "CURLINFO_STARTTRANSFER_TIME"); | ||
938 | snprintf(perfstring, DEFAULT_BUFFER_SIZE, "%s %s %s %s %s %s %s", perfd_time(total_time), perfd_size(page_len), | ||
939 | perfd_time_connect(time_connect), use_ssl ? perfd_time_ssl(time_appconnect - time_connect) : "", | ||
940 | perfd_time_headers(time_headers - time_appconnect), perfd_time_firstbyte(time_firstbyte - time_headers), | ||
941 | perfd_time_transfer(total_time - time_firstbyte)); | ||
942 | } else { | ||
943 | snprintf(perfstring, DEFAULT_BUFFER_SIZE, "%s %s", perfd_time(total_time), perfd_size(page_len)); | ||
944 | } | 374 | } |
945 | 375 | ||
946 | /* return a CRITICAL status if we couldn't read any data */ | 376 | /* return a CRITICAL status if we couldn't read any data */ |
947 | if (strlen(header_buf.buf) == 0 && strlen(body_buf.buf) == 0) | 377 | if (strlen(curl_state.header_buf->buf) == 0 && strlen(curl_state.body_buf->buf) == 0) { |
948 | die(STATE_CRITICAL, _("HTTP CRITICAL - No header received from host\n")); | 378 | sc_result = mp_set_subcheck_state(sc_result, STATE_CRITICAL); |
949 | 379 | xasprintf(&sc_result.output, "No header received from host"); | |
950 | /* get status line of answer, check sanity of HTTP code */ | 380 | return sc_result; |
951 | if (curlhelp_parse_statusline(header_buf.buf, &status_line) < 0) { | ||
952 | snprintf(msg, DEFAULT_BUFFER_SIZE, "Unparsable status line in %.3g seconds response time|%s\n", total_time, perfstring); | ||
953 | /* we cannot know the major/minor version here for sure as we cannot parse the first line */ | ||
954 | die(STATE_CRITICAL, "HTTP CRITICAL HTTP/x.x %ld unknown - %s", code, msg); | ||
955 | } | 381 | } |
956 | status_line_initialized = true; | ||
957 | 382 | ||
958 | /* get result code from cURL */ | 383 | /* get result code from cURL */ |
959 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code), "CURLINFO_RESPONSE_CODE"); | 384 | long httpReturnCode; |
960 | if (verbose >= 2) | 385 | handle_curl_option_return_code( |
961 | printf("* curl CURLINFO_RESPONSE_CODE is %ld\n", code); | 386 | curl_easy_getinfo(curl_state.curl, CURLINFO_RESPONSE_CODE, &httpReturnCode), |
387 | "CURLINFO_RESPONSE_CODE"); | ||
388 | if (verbose >= 2) { | ||
389 | printf("* curl CURLINFO_RESPONSE_CODE is %ld\n", httpReturnCode); | ||
390 | } | ||
962 | 391 | ||
963 | /* print status line, header, body if verbose */ | 392 | /* print status line, header, body if verbose */ |
964 | if (verbose >= 2) { | 393 | if (verbose >= 2) { |
965 | printf("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header_buf.buf, (no_body ? " [[ skipped ]]" : body_buf.buf)); | 394 | printf("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", curl_state.header_buf->buf, |
395 | (workingState.no_body ? " [[ skipped ]]" : curl_state.body_buf->buf)); | ||
966 | } | 396 | } |
967 | 397 | ||
968 | /* make sure the status line matches the response we are looking for */ | 398 | /* make sure the status line matches the response we are looking for */ |
969 | if (!expected_statuscode(status_line.first_line, server_expect)) { | 399 | mp_subcheck sc_expect = mp_subcheck_init(); |
970 | if (server_port == HTTP_PORT) | 400 | sc_expect = mp_set_subcheck_default_state(sc_expect, STATE_OK); |
971 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host: %s\n"), status_line.first_line); | 401 | if (!expected_statuscode(curl_state.status_line->first_line, config.server_expect.string)) { |
972 | else | 402 | if (workingState.serverPort == HTTP_PORT) { |
973 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host on port %d: %s\n"), server_port, | 403 | xasprintf(&sc_expect.output, _("Invalid HTTP response received from host: %s\n"), |
974 | status_line.first_line); | 404 | curl_state.status_line->first_line); |
975 | die(STATE_CRITICAL, "HTTP CRITICAL - %s%s%s", msg, show_body ? "\n" : "", show_body ? body_buf.buf : ""); | 405 | } else { |
406 | xasprintf(&sc_expect.output, | ||
407 | _("Invalid HTTP response received from host on port %d: %s\n"), | ||
408 | workingState.serverPort, curl_state.status_line->first_line); | ||
409 | } | ||
410 | sc_expect = mp_set_subcheck_default_state(sc_expect, STATE_CRITICAL); | ||
411 | } else { | ||
412 | xasprintf(&sc_expect.output, _("Status line output matched \"%s\""), | ||
413 | config.server_expect.string); | ||
976 | } | 414 | } |
415 | mp_add_subcheck_to_subcheck(&sc_result, sc_expect); | ||
977 | 416 | ||
978 | if (server_expect_yn) { | 417 | if (!config.server_expect.is_present) { |
979 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Status line output matched \"%s\" - "), server_expect); | ||
980 | if (verbose) | ||
981 | printf("%s\n", msg); | ||
982 | result = STATE_OK; | ||
983 | } else { | ||
984 | /* illegal return codes result in a critical state */ | 418 | /* illegal return codes result in a critical state */ |
985 | if (code >= 600 || code < 100) { | 419 | mp_subcheck sc_return_code = mp_subcheck_init(); |
986 | die(STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%d, %.40s)\n"), status_line.http_code, status_line.msg); | 420 | sc_return_code = mp_set_subcheck_default_state(sc_return_code, STATE_OK); |
987 | /* server errors result in a critical state */ | 421 | xasprintf(&sc_return_code.output, "HTTP return code: %d", |
988 | } else if (code >= 500) { | 422 | curl_state.status_line->http_code); |
989 | result = STATE_CRITICAL; | 423 | |
424 | if (httpReturnCode >= 600 || httpReturnCode < 100) { | ||
425 | sc_return_code = mp_set_subcheck_state(sc_return_code, STATE_CRITICAL); | ||
426 | xasprintf(&sc_return_code.output, _("Invalid Status (%d, %.40s)"), | ||
427 | curl_state.status_line->http_code, curl_state.status_line->msg); | ||
428 | mp_add_subcheck_to_subcheck(&sc_result, sc_return_code); | ||
429 | return sc_result; | ||
430 | } | ||
431 | |||
432 | // server errors result in a critical state | ||
433 | if (httpReturnCode >= 500) { | ||
434 | sc_return_code = mp_set_subcheck_state(sc_return_code, STATE_CRITICAL); | ||
990 | /* client errors result in a warning state */ | 435 | /* client errors result in a warning state */ |
991 | } else if (code >= 400) { | 436 | } else if (httpReturnCode >= 400) { |
992 | result = STATE_WARNING; | 437 | sc_return_code = mp_set_subcheck_state(sc_return_code, STATE_WARNING); |
993 | /* check redirected page if specified */ | 438 | /* check redirected page if specified */ |
994 | } else if (code >= 300) { | 439 | } else if (httpReturnCode >= 300) { |
995 | if (onredirect == STATE_DEPENDENT) { | 440 | if (config.on_redirect_dependent) { |
996 | if (followmethod == FOLLOW_LIBCURL) { | 441 | if (config.followmethod == FOLLOW_LIBCURL) { |
997 | code = status_line.http_code; | 442 | httpReturnCode = curl_state.status_line->http_code; |
443 | handle_curl_option_return_code( | ||
444 | curl_easy_getinfo(curl_state.curl, CURLINFO_REDIRECT_COUNT, &redir_depth), | ||
445 | "CURLINFO_REDIRECT_COUNT"); | ||
446 | |||
447 | if (verbose >= 2) { | ||
448 | printf(_("* curl LIBINFO_REDIRECT_COUNT is %d\n"), redir_depth); | ||
449 | } | ||
450 | |||
451 | mp_subcheck sc_redir_depth = mp_subcheck_init(); | ||
452 | if (redir_depth > config.max_depth) { | ||
453 | xasprintf(&sc_redir_depth.output, | ||
454 | "maximum redirection depth %d exceeded in libcurl", | ||
455 | config.max_depth); | ||
456 | sc_redir_depth = mp_set_subcheck_state(sc_redir_depth, STATE_CRITICAL); | ||
457 | mp_add_subcheck_to_subcheck(&sc_result, sc_redir_depth); | ||
458 | return sc_result; | ||
459 | } | ||
460 | xasprintf(&sc_redir_depth.output, "redirection depth %d (of a maximum %d)", | ||
461 | redir_depth, config.max_depth); | ||
462 | mp_add_subcheck_to_subcheck(&sc_result, sc_redir_depth); | ||
463 | |||
998 | } else { | 464 | } else { |
999 | /* old check_http style redirection, if we come | 465 | /* old check_http style redirection, if we come |
1000 | * back here, we are in the same status as with | 466 | * back here, we are in the same status as with |
1001 | * the libcurl method | 467 | * the libcurl method |
1002 | */ | 468 | */ |
1003 | redir(&header_buf); | 469 | redir_wrapper redir_result = |
470 | redir(curl_state.header_buf, config, redir_depth, workingState); | ||
471 | cleanup(curl_state); | ||
472 | mp_subcheck sc_redir = | ||
473 | check_http(config, redir_result.working_state, redir_result.redir_depth); | ||
474 | mp_add_subcheck_to_subcheck(&sc_result, sc_redir); | ||
475 | |||
476 | return sc_result; | ||
1004 | } | 477 | } |
1005 | } else { | 478 | } else { |
1006 | /* this is a specific code in the command line to | 479 | /* this is a specific code in the command line to |
1007 | * be returned when a redirection is encountered | 480 | * be returned when a redirection is encountered |
1008 | */ | 481 | */ |
482 | sc_return_code = | ||
483 | mp_set_subcheck_state(sc_return_code, config.on_redirect_result_state); | ||
1009 | } | 484 | } |
1010 | result = max_state_alt(onredirect, result); | ||
1011 | /* all other codes are considered ok */ | ||
1012 | } else { | 485 | } else { |
1013 | result = STATE_OK; | 486 | sc_return_code = mp_set_subcheck_state(sc_return_code, STATE_OK); |
1014 | } | 487 | } |
1015 | } | ||
1016 | 488 | ||
1017 | /* libcurl redirection internally, handle error states here */ | 489 | mp_add_subcheck_to_subcheck(&sc_result, sc_return_code); |
1018 | if (followmethod == FOLLOW_LIBCURL) { | ||
1019 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &redir_depth), "CURLINFO_REDIRECT_COUNT"); | ||
1020 | if (verbose >= 2) | ||
1021 | printf(_("* curl LIBINFO_REDIRECT_COUNT is %d\n"), redir_depth); | ||
1022 | if (redir_depth > max_depth) { | ||
1023 | snprintf(msg, DEFAULT_BUFFER_SIZE, "maximum redirection depth %d exceeded in libcurl", max_depth); | ||
1024 | die(STATE_WARNING, "HTTP WARNING - %s", msg); | ||
1025 | } | ||
1026 | } | 490 | } |
1027 | 491 | ||
1028 | /* check status codes, set exit status accordingly */ | 492 | /* check status codes, set exit status accordingly */ |
1029 | if (status_line.http_code != code) { | 493 | if (curl_state.status_line->http_code != httpReturnCode) { |
1030 | die(STATE_CRITICAL, _("HTTP CRITICAL %s %d %s - different HTTP codes (cUrl has %ld)\n"), | 494 | mp_subcheck sc_http_return_code_sanity = mp_subcheck_init(); |
1031 | string_statuscode(status_line.http_major, status_line.http_minor), status_line.http_code, status_line.msg, code); | 495 | sc_http_return_code_sanity = |
496 | mp_set_subcheck_state(sc_http_return_code_sanity, STATE_CRITICAL); | ||
497 | xasprintf(&sc_http_return_code_sanity.output, | ||
498 | _("HTTP CRITICAL %s %d %s - different HTTP codes (cUrl has %ld)\n"), | ||
499 | string_statuscode(curl_state.status_line->http_major, | ||
500 | curl_state.status_line->http_minor), | ||
501 | curl_state.status_line->http_code, curl_state.status_line->msg, httpReturnCode); | ||
502 | |||
503 | mp_add_subcheck_to_subcheck(&sc_result, sc_http_return_code_sanity); | ||
504 | return sc_result; | ||
1032 | } | 505 | } |
1033 | 506 | ||
1034 | if (maximum_age >= 0) { | 507 | if (config.maximum_age >= 0) { |
1035 | result = max_state_alt(check_document_dates(&header_buf, &msg), result); | 508 | mp_subcheck sc_max_age = check_document_dates(curl_state.header_buf, config.maximum_age); |
509 | mp_add_subcheck_to_subcheck(&sc_result, sc_max_age); | ||
1036 | } | 510 | } |
1037 | 511 | ||
1038 | /* Page and Header content checks go here */ | 512 | /* Page and Header content checks go here */ |
513 | if (strlen(config.header_expect)) { | ||
514 | mp_subcheck sc_header_expect = mp_subcheck_init(); | ||
515 | sc_header_expect = mp_set_subcheck_default_state(sc_header_expect, STATE_OK); | ||
516 | xasprintf(&sc_header_expect.output, "Expect %s in header", config.header_expect); | ||
1039 | 517 | ||
1040 | if (strlen(header_expect)) { | 518 | if (!strstr(curl_state.header_buf->buf, config.header_expect)) { |
1041 | if (!strstr(header_buf.buf, header_expect)) { | 519 | char output_header_search[30] = ""; |
1042 | 520 | strncpy(&output_header_search[0], config.header_expect, sizeof(output_header_search)); | |
1043 | strncpy(&output_header_search[0], header_expect, sizeof(output_header_search)); | ||
1044 | 521 | ||
1045 | if (output_header_search[sizeof(output_header_search) - 1] != '\0') { | 522 | if (output_header_search[sizeof(output_header_search) - 1] != '\0') { |
1046 | bcopy("...", &output_header_search[sizeof(output_header_search) - 4], 4); | 523 | bcopy("...", &output_header_search[sizeof(output_header_search) - 4], 4); |
1047 | } | 524 | } |
1048 | 525 | ||
1049 | char tmp[DEFAULT_BUFFER_SIZE]; | 526 | xasprintf(&sc_header_expect.output, _("header '%s' not found on '%s://%s:%d%s', "), |
1050 | 527 | output_header_search, workingState.use_ssl ? "https" : "http", | |
1051 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sheader '%s' not found on '%s://%s:%d%s', "), msg, output_header_search, | 528 | workingState.host_name ? workingState.host_name : workingState.server_address, |
1052 | use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, server_url); | 529 | workingState.serverPort, workingState.server_url); |
1053 | 530 | ||
1054 | strcpy(msg, tmp); | 531 | sc_header_expect = mp_set_subcheck_state(sc_header_expect, STATE_CRITICAL); |
1055 | |||
1056 | result = STATE_CRITICAL; | ||
1057 | } | 532 | } |
533 | |||
534 | mp_add_subcheck_to_subcheck(&sc_result, sc_header_expect); | ||
1058 | } | 535 | } |
1059 | 536 | ||
1060 | if (strlen(string_expect)) { | 537 | if (strlen(config.string_expect)) { |
1061 | if (!strstr(body_buf.buf, string_expect)) { | 538 | mp_subcheck sc_string_expect = mp_subcheck_init(); |
539 | sc_string_expect = mp_set_subcheck_default_state(sc_string_expect, STATE_OK); | ||
540 | xasprintf(&sc_string_expect.output, "Expect string \"%s\" in body", config.string_expect); | ||
1062 | 541 | ||
1063 | strncpy(&output_string_search[0], string_expect, sizeof(output_string_search)); | 542 | if (!strstr(curl_state.body_buf->buf, config.string_expect)) { |
543 | char output_string_search[30] = ""; | ||
544 | strncpy(&output_string_search[0], config.string_expect, sizeof(output_string_search)); | ||
1064 | 545 | ||
1065 | if (output_string_search[sizeof(output_string_search) - 1] != '\0') { | 546 | if (output_string_search[sizeof(output_string_search) - 1] != '\0') { |
1066 | bcopy("...", &output_string_search[sizeof(output_string_search) - 4], 4); | 547 | bcopy("...", &output_string_search[sizeof(output_string_search) - 4], 4); |
1067 | } | 548 | } |
1068 | 549 | ||
1069 | char tmp[DEFAULT_BUFFER_SIZE]; | 550 | xasprintf(&sc_string_expect.output, _("string '%s' not found on '%s://%s:%d%s', "), |
1070 | 551 | output_string_search, workingState.use_ssl ? "https" : "http", | |
1071 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sstring '%s' not found on '%s://%s:%d%s', "), msg, output_string_search, | 552 | workingState.host_name ? workingState.host_name : workingState.server_address, |
1072 | use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, server_url); | 553 | workingState.serverPort, workingState.server_url); |
1073 | |||
1074 | strcpy(msg, tmp); | ||
1075 | 554 | ||
1076 | result = STATE_CRITICAL; | 555 | sc_string_expect = mp_set_subcheck_state(sc_string_expect, STATE_CRITICAL); |
1077 | } | 556 | } |
557 | |||
558 | mp_add_subcheck_to_subcheck(&sc_result, sc_string_expect); | ||
1078 | } | 559 | } |
1079 | 560 | ||
1080 | if (strlen(regexp)) { | 561 | if (strlen(config.regexp)) { |
1081 | errcode = regexec(&preg, body_buf.buf, REGS, pmatch, 0); | 562 | mp_subcheck sc_body_regex = mp_subcheck_init(); |
1082 | if ((errcode == 0 && !invert_regex) || (errcode == REG_NOMATCH && invert_regex)) { | 563 | xasprintf(&sc_body_regex.output, "Regex \"%s\" in body matched", config.regexp); |
1083 | /* OK - No-op to avoid changing the logic around it */ | 564 | regmatch_t pmatch[REGS]; |
1084 | result = max_state_alt(STATE_OK, result); | ||
1085 | } else if ((errcode == REG_NOMATCH && !invert_regex) || (errcode == 0 && invert_regex)) { | ||
1086 | if (!invert_regex) { | ||
1087 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
1088 | 565 | ||
1089 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%spattern not found, "), msg); | 566 | int errcode = regexec(&config.compiled_regex, curl_state.body_buf->buf, REGS, pmatch, 0); |
1090 | strcpy(msg, tmp); | ||
1091 | 567 | ||
568 | if (errcode == 0) { | ||
569 | // got a match | ||
570 | if (config.invert_regex) { | ||
571 | sc_body_regex = mp_set_subcheck_state(sc_body_regex, config.state_regex); | ||
1092 | } else { | 572 | } else { |
1093 | char tmp[DEFAULT_BUFFER_SIZE]; | 573 | sc_body_regex = mp_set_subcheck_state(sc_body_regex, STATE_OK); |
574 | } | ||
575 | } else if (errcode == REG_NOMATCH) { | ||
576 | // got no match | ||
577 | xasprintf(&sc_body_regex.output, "%s not", sc_body_regex.output); | ||
1094 | 578 | ||
1095 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%spattern found, "), msg); | 579 | if (config.invert_regex) { |
1096 | strcpy(msg, tmp); | 580 | sc_body_regex = mp_set_subcheck_state(sc_body_regex, STATE_OK); |
581 | } else { | ||
582 | sc_body_regex = mp_set_subcheck_state(sc_body_regex, config.state_regex); | ||
1097 | } | 583 | } |
1098 | result = state_regex; | ||
1099 | } else { | 584 | } else { |
1100 | regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); | 585 | // error in regexec |
1101 | 586 | char error_buffer[DEFAULT_BUFFER_SIZE]; | |
1102 | char tmp[DEFAULT_BUFFER_SIZE]; | 587 | regerror(errcode, &config.compiled_regex, &error_buffer[0], DEFAULT_BUFFER_SIZE); |
1103 | 588 | xasprintf(&sc_body_regex.output, "regexec error: %s", error_buffer); | |
1104 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sExecute Error: %s, "), msg, errbuf); | 589 | sc_body_regex = mp_set_subcheck_state(sc_body_regex, STATE_UNKNOWN); |
1105 | strcpy(msg, tmp); | ||
1106 | result = STATE_UNKNOWN; | ||
1107 | } | 590 | } |
591 | |||
592 | mp_add_subcheck_to_subcheck(&sc_result, sc_body_regex); | ||
1108 | } | 593 | } |
1109 | 594 | ||
1110 | /* make sure the page is of an appropriate size */ | 595 | // size a.k.a. page length |
1111 | if ((max_page_len > 0) && (page_len > max_page_len)) { | 596 | mp_perfdata pd_page_length = perfdata_init(); |
1112 | char tmp[DEFAULT_BUFFER_SIZE]; | 597 | mp_perfdata_value pd_val_page_length = mp_create_pd_value(page_len); |
598 | pd_page_length.value = pd_val_page_length; | ||
599 | pd_page_length.label = "size"; | ||
600 | pd_page_length.uom = "B"; | ||
601 | pd_page_length.min = mp_create_pd_value(0); | ||
602 | pd_page_length.warn = config.page_length_limits; | ||
603 | pd_page_length.warn_present = true; | ||
1113 | 604 | ||
1114 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%spage size %d too large, "), msg, page_len); | 605 | /* make sure the page is of an appropriate size */ |
606 | if (config.page_length_limits_is_set) { | ||
607 | mp_thresholds page_length_threshold = mp_thresholds_init(); | ||
608 | page_length_threshold.warning = config.page_length_limits; | ||
609 | page_length_threshold.warning_is_set = true; | ||
1115 | 610 | ||
1116 | strcpy(msg, tmp); | 611 | pd_page_length = mp_pd_set_thresholds(pd_page_length, page_length_threshold); |
1117 | 612 | ||
1118 | result = max_state_alt(STATE_WARNING, result); | 613 | mp_subcheck sc_page_length = mp_subcheck_init(); |
1119 | 614 | ||
1120 | } else if ((min_page_len > 0) && (page_len < min_page_len)) { | 615 | mp_add_perfdata_to_subcheck(&sc_page_length, pd_page_length); |
1121 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
1122 | 616 | ||
1123 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%spage size %d too small, "), msg, page_len); | 617 | mp_state_enum tmp_state = mp_get_pd_status(pd_page_length); |
1124 | strcpy(msg, tmp); | 618 | sc_page_length = mp_set_subcheck_state(sc_page_length, tmp_state); |
1125 | result = max_state_alt(STATE_WARNING, result); | ||
1126 | } | ||
1127 | 619 | ||
1128 | /* -w, -c: check warning and critical level */ | 620 | switch (tmp_state) { |
1129 | result = max_state_alt(get_status(total_time, thlds), result); | 621 | case STATE_CRITICAL: |
622 | case STATE_WARNING: | ||
623 | xasprintf(&sc_page_length.output, _("page size %zu violates threshold"), page_len); | ||
624 | break; | ||
625 | case STATE_OK: | ||
626 | xasprintf(&sc_page_length.output, _("page size %zu is OK"), page_len); | ||
627 | break; | ||
628 | default: | ||
629 | assert(false); | ||
630 | } | ||
1130 | 631 | ||
1131 | /* Cut-off trailing characters */ | 632 | mp_add_subcheck_to_subcheck(&sc_result, sc_page_length); |
1132 | if (strlen(msg) >= 2) { | ||
1133 | if (msg[strlen(msg) - 2] == ',') | ||
1134 | msg[strlen(msg) - 2] = '\0'; | ||
1135 | else | ||
1136 | msg[strlen(msg) - 3] = '\0'; | ||
1137 | } | 633 | } |
1138 | 634 | ||
1139 | /* TODO: separate _() msg and status code: die (result, "HTTP %s: %s\n", state_text(result), msg); */ | 635 | return sc_result; |
1140 | die(max_state_alt(result, result_ssl), "HTTP %s: %s %d %s%s%s - %d bytes in %.3f second response time %s|%s\n%s%s", state_text(result), | ||
1141 | string_statuscode(status_line.http_major, status_line.http_minor), status_line.http_code, status_line.msg, | ||
1142 | strlen(msg) > 0 ? " - " : "", msg, page_len, total_time, (display_html ? "</A>" : ""), perfstring, (show_body ? body_buf.buf : ""), | ||
1143 | (show_body ? "\n" : "")); | ||
1144 | |||
1145 | return max_state_alt(result, result_ssl); | ||
1146 | } | 636 | } |
1147 | 637 | ||
1148 | int uri_strcmp(const UriTextRangeA range, const char *s) { | 638 | int uri_strcmp(const UriTextRangeA range, const char *stringToCompare) { |
1149 | if (!range.first) | 639 | if (!range.first) { |
1150 | return -1; | 640 | return -1; |
1151 | if ((size_t)(range.afterLast - range.first) < strlen(s)) | 641 | } |
642 | if ((size_t)(range.afterLast - range.first) < strlen(stringToCompare)) { | ||
1152 | return -1; | 643 | return -1; |
1153 | return strncmp(s, range.first, min((size_t)(range.afterLast - range.first), strlen(s))); | 644 | } |
645 | return strncmp(stringToCompare, range.first, | ||
646 | min((size_t)(range.afterLast - range.first), strlen(stringToCompare))); | ||
1154 | } | 647 | } |
1155 | 648 | ||
1156 | char *uri_string(const UriTextRangeA range, char *buf, size_t buflen) { | 649 | char *uri_string(const UriTextRangeA range, char *buf, size_t buflen) { |
1157 | if (!range.first) | 650 | if (!range.first) { |
1158 | return "(null)"; | 651 | return "(null)"; |
652 | } | ||
1159 | strncpy(buf, range.first, max(buflen - 1, (size_t)(range.afterLast - range.first))); | 653 | strncpy(buf, range.first, max(buflen - 1, (size_t)(range.afterLast - range.first))); |
1160 | buf[max(buflen - 1, (size_t)(range.afterLast - range.first))] = '\0'; | 654 | buf[max(buflen - 1, (size_t)(range.afterLast - range.first))] = '\0'; |
1161 | buf[range.afterLast - range.first] = '\0'; | 655 | buf[range.afterLast - range.first] = '\0'; |
1162 | return buf; | 656 | return buf; |
1163 | } | 657 | } |
1164 | 658 | ||
1165 | void redir(curlhelp_write_curlbuf *header_buf) { | 659 | redir_wrapper redir(curlhelp_write_curlbuf *header_buf, const check_curl_config config, |
1166 | char *location = NULL; | 660 | int redir_depth, check_curl_working_state working_state) { |
1167 | curlhelp_statusline status_line; | 661 | curlhelp_statusline status_line; |
1168 | struct phr_header headers[255]; | 662 | struct phr_header headers[255]; |
1169 | size_t nof_headers = 255; | ||
1170 | size_t msglen; | 663 | size_t msglen; |
1171 | char buf[DEFAULT_BUFFER_SIZE]; | 664 | size_t nof_headers = 255; |
1172 | char ipstr[INET_ADDR_MAX_SIZE]; | 665 | int res = phr_parse_response(header_buf->buf, header_buf->buflen, &status_line.http_major, |
1173 | int new_port; | 666 | &status_line.http_minor, &status_line.http_code, &status_line.msg, |
1174 | char *new_host; | 667 | &msglen, headers, &nof_headers, 0); |
1175 | char *new_url; | ||
1176 | |||
1177 | int res = phr_parse_response(header_buf->buf, header_buf->buflen, &status_line.http_major, &status_line.http_minor, | ||
1178 | &status_line.http_code, &status_line.msg, &msglen, headers, &nof_headers, 0); | ||
1179 | 668 | ||
1180 | if (res == -1) { | 669 | if (res == -1) { |
1181 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse Response\n")); | 670 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse Response\n")); |
1182 | } | 671 | } |
1183 | 672 | ||
1184 | location = get_header_value(headers, nof_headers, "location"); | 673 | char *location = get_header_value(headers, nof_headers, "location"); |
1185 | 674 | ||
1186 | if (verbose >= 2) | 675 | if (verbose >= 2) { |
1187 | printf(_("* Seen redirect location %s\n"), location); | 676 | printf(_("* Seen redirect location %s\n"), location); |
677 | } | ||
1188 | 678 | ||
1189 | if (++redir_depth > max_depth) | 679 | if (++redir_depth > config.max_depth) { |
1190 | die(STATE_WARNING, _("HTTP WARNING - maximum redirection depth %d exceeded - %s%s\n"), max_depth, location, | 680 | die(STATE_WARNING, _("HTTP WARNING - maximum redirection depth %d exceeded - %s\n"), |
1191 | (display_html ? "</A>" : "")); | 681 | config.max_depth, location); |
682 | } | ||
1192 | 683 | ||
1193 | UriParserStateA state; | 684 | UriParserStateA state; |
1194 | UriUriA uri; | 685 | UriUriA uri; |
1195 | state.uri = &uri; | 686 | state.uri = &uri; |
1196 | if (uriParseUriA(&state, location) != URI_SUCCESS) { | 687 | if (uriParseUriA(&state, location) != URI_SUCCESS) { |
1197 | if (state.errorCode == URI_ERROR_SYNTAX) { | 688 | if (state.errorCode == URI_ERROR_SYNTAX) { |
1198 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not parse redirect location '%s'%s\n"), location, (display_html ? "</A>" : "")); | 689 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not parse redirect location '%s'\n"), |
690 | location); | ||
1199 | } else if (state.errorCode == URI_ERROR_MALLOC) { | 691 | } else if (state.errorCode == URI_ERROR_MALLOC) { |
1200 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate URL\n")); | 692 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate URL\n")); |
1201 | } | 693 | } |
1202 | } | 694 | } |
1203 | 695 | ||
696 | char ipstr[INET_ADDR_MAX_SIZE]; | ||
697 | char buf[DEFAULT_BUFFER_SIZE]; | ||
1204 | if (verbose >= 2) { | 698 | if (verbose >= 2) { |
1205 | printf(_("** scheme: %s\n"), uri_string(uri.scheme, buf, DEFAULT_BUFFER_SIZE)); | 699 | printf(_("** scheme: %s\n"), uri_string(uri.scheme, buf, DEFAULT_BUFFER_SIZE)); |
1206 | printf(_("** host: %s\n"), uri_string(uri.hostText, buf, DEFAULT_BUFFER_SIZE)); | 700 | printf(_("** host: %s\n"), uri_string(uri.hostText, buf, DEFAULT_BUFFER_SIZE)); |
@@ -1215,9 +709,9 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1215 | } | 709 | } |
1216 | if (uri.pathHead) { | 710 | if (uri.pathHead) { |
1217 | printf(_("** path: ")); | 711 | printf(_("** path: ")); |
1218 | const UriPathSegmentA *p = uri.pathHead; | 712 | for (UriPathSegmentA *path_segment = uri.pathHead; path_segment; |
1219 | for (; p; p = p->next) { | 713 | path_segment = path_segment->next) { |
1220 | printf("/%s", uri_string(p->text, buf, DEFAULT_BUFFER_SIZE)); | 714 | printf("/%s", uri_string(path_segment->text, buf, DEFAULT_BUFFER_SIZE)); |
1221 | } | 715 | } |
1222 | puts(""); | 716 | puts(""); |
1223 | } | 717 | } |
@@ -1230,99 +724,104 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1230 | } | 724 | } |
1231 | 725 | ||
1232 | if (uri.scheme.first) { | 726 | if (uri.scheme.first) { |
1233 | if (!uri_strcmp(uri.scheme, "https")) | 727 | working_state.use_ssl = (bool)(!uri_strcmp(uri.scheme, "https")); |
1234 | use_ssl = true; | ||
1235 | else | ||
1236 | use_ssl = false; | ||
1237 | } | 728 | } |
1238 | 729 | ||
1239 | /* we do a sloppy test here only, because uriparser would have failed | 730 | /* we do a sloppy test here only, because uriparser would have failed |
1240 | * above, if the port would be invalid, we just check for MAX_PORT | 731 | * above, if the port would be invalid, we just check for MAX_PORT |
1241 | */ | 732 | */ |
733 | int new_port; | ||
1242 | if (uri.portText.first) { | 734 | if (uri.portText.first) { |
1243 | new_port = atoi(uri_string(uri.portText, buf, DEFAULT_BUFFER_SIZE)); | 735 | new_port = atoi(uri_string(uri.portText, buf, DEFAULT_BUFFER_SIZE)); |
1244 | } else { | 736 | } else { |
1245 | new_port = HTTP_PORT; | 737 | new_port = HTTP_PORT; |
1246 | if (use_ssl) | 738 | if (working_state.use_ssl) { |
1247 | new_port = HTTPS_PORT; | 739 | new_port = HTTPS_PORT; |
740 | } | ||
741 | } | ||
742 | if (new_port > MAX_PORT) { | ||
743 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Redirection to port above %d - %s\n"), MAX_PORT, | ||
744 | location); | ||
1248 | } | 745 | } |
1249 | if (new_port > MAX_PORT) | ||
1250 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Redirection to port above %d - %s%s\n"), MAX_PORT, location, display_html ? "</A>" : ""); | ||
1251 | 746 | ||
1252 | /* by RFC 7231 relative URLs in Location should be taken relative to | 747 | /* by RFC 7231 relative URLs in Location should be taken relative to |
1253 | * the original URL, so we try to form a new absolute URL here | 748 | * the original URL, so we try to form a new absolute URL here |
1254 | */ | 749 | */ |
750 | char *new_host; | ||
1255 | if (!uri.scheme.first && !uri.hostText.first) { | 751 | if (!uri.scheme.first && !uri.hostText.first) { |
1256 | new_host = strdup(host_name ? host_name : server_address); | 752 | new_host = strdup(working_state.host_name ? working_state.host_name |
1257 | new_port = server_port; | 753 | : working_state.server_address); |
1258 | if (use_ssl) | 754 | new_port = working_state.serverPort; |
755 | if (working_state.use_ssl) { | ||
1259 | uri_string(uri.scheme, "https", DEFAULT_BUFFER_SIZE); | 756 | uri_string(uri.scheme, "https", DEFAULT_BUFFER_SIZE); |
757 | } | ||
1260 | } else { | 758 | } else { |
1261 | new_host = strdup(uri_string(uri.hostText, buf, DEFAULT_BUFFER_SIZE)); | 759 | new_host = strdup(uri_string(uri.hostText, buf, DEFAULT_BUFFER_SIZE)); |
1262 | } | 760 | } |
1263 | 761 | ||
1264 | /* compose new path */ | 762 | /* compose new path */ |
1265 | /* TODO: handle fragments and query part of URL */ | 763 | /* TODO: handle fragments and query part of URL */ |
1266 | new_url = (char *)calloc(1, DEFAULT_BUFFER_SIZE); | 764 | char *new_url = (char *)calloc(1, DEFAULT_BUFFER_SIZE); |
1267 | if (uri.pathHead) { | 765 | if (uri.pathHead) { |
1268 | const UriPathSegmentA *p = uri.pathHead; | 766 | for (UriPathSegmentA *pathSegment = uri.pathHead; pathSegment; |
1269 | for (; p; p = p->next) { | 767 | pathSegment = pathSegment->next) { |
1270 | strncat(new_url, "/", DEFAULT_BUFFER_SIZE); | 768 | strncat(new_url, "/", DEFAULT_BUFFER_SIZE); |
1271 | strncat(new_url, uri_string(p->text, buf, DEFAULT_BUFFER_SIZE), DEFAULT_BUFFER_SIZE - 1); | 769 | strncat(new_url, uri_string(pathSegment->text, buf, DEFAULT_BUFFER_SIZE), |
770 | DEFAULT_BUFFER_SIZE - 1); | ||
1272 | } | 771 | } |
1273 | } | 772 | } |
1274 | 773 | ||
1275 | if (server_port == new_port && !strncmp(server_address, new_host, MAX_IPV4_HOSTLENGTH) && | 774 | if (working_state.serverPort == new_port && |
1276 | (host_name && !strncmp(host_name, new_host, MAX_IPV4_HOSTLENGTH)) && !strcmp(server_url, new_url)) | 775 | !strncmp(working_state.server_address, new_host, MAX_IPV4_HOSTLENGTH) && |
1277 | die(STATE_CRITICAL, _("HTTP CRITICAL - redirection creates an infinite loop - %s://%s:%d%s%s\n"), use_ssl ? "https" : "http", | 776 | (working_state.host_name && |
1278 | new_host, new_port, new_url, (display_html ? "</A>" : "")); | 777 | !strncmp(working_state.host_name, new_host, MAX_IPV4_HOSTLENGTH)) && |
778 | !strcmp(working_state.server_url, new_url)) { | ||
779 | die(STATE_CRITICAL, | ||
780 | _("HTTP CRITICAL - redirection creates an infinite loop - %s://%s:%d%s\n"), | ||
781 | working_state.use_ssl ? "https" : "http", new_host, new_port, new_url); | ||
782 | } | ||
1279 | 783 | ||
1280 | /* set new values for redirected request */ | 784 | /* set new values for redirected request */ |
1281 | 785 | ||
1282 | if (!(followsticky & STICKY_HOST)) { | 786 | if (!(config.followsticky & STICKY_HOST)) { |
1283 | free(server_address); | 787 | free(working_state.server_address); |
1284 | server_address = strndup(new_host, MAX_IPV4_HOSTLENGTH); | 788 | working_state.server_address = strndup(new_host, MAX_IPV4_HOSTLENGTH); |
1285 | } | 789 | } |
1286 | if (!(followsticky & STICKY_PORT)) { | 790 | if (!(config.followsticky & STICKY_PORT)) { |
1287 | server_port = (unsigned short)new_port; | 791 | working_state.serverPort = (unsigned short)new_port; |
1288 | } | 792 | } |
1289 | 793 | ||
1290 | free(host_name); | 794 | free(working_state.host_name); |
1291 | host_name = strndup(new_host, MAX_IPV4_HOSTLENGTH); | 795 | working_state.host_name = strndup(new_host, MAX_IPV4_HOSTLENGTH); |
1292 | 796 | ||
1293 | /* reset virtual port */ | 797 | /* reset virtual port */ |
1294 | virtual_port = server_port; | 798 | working_state.virtualPort = working_state.serverPort; |
1295 | 799 | ||
1296 | free(new_host); | 800 | free(new_host); |
1297 | free(server_url); | 801 | free(working_state.server_url); |
1298 | server_url = new_url; | 802 | working_state.server_url = new_url; |
1299 | 803 | ||
1300 | uriFreeUriMembersA(&uri); | 804 | uriFreeUriMembersA(&uri); |
1301 | 805 | ||
1302 | if (verbose) | 806 | if (verbose) { |
1303 | printf(_("Redirection to %s://%s:%d%s\n"), use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, | 807 | printf(_("Redirection to %s://%s:%d%s\n"), working_state.use_ssl ? "https" : "http", |
1304 | server_url); | 808 | working_state.host_name ? working_state.host_name : working_state.server_address, |
809 | working_state.serverPort, working_state.server_url); | ||
810 | } | ||
1305 | 811 | ||
1306 | /* TODO: the hash component MUST be taken from the original URL and | 812 | /* TODO: the hash component MUST be taken from the original URL and |
1307 | * attached to the URL in Location | 813 | * attached to the URL in Location |
1308 | */ | 814 | */ |
1309 | 815 | ||
1310 | cleanup(); | 816 | redir_wrapper result = { |
1311 | check_http(); | 817 | .redir_depth = redir_depth, |
1312 | } | 818 | .working_state = working_state, |
1313 | 819 | .error_code = OK, | |
1314 | /* check whether a file exists */ | 820 | }; |
1315 | void test_file(char *path) { | 821 | return result; |
1316 | if (access(path, R_OK) == 0) | ||
1317 | return; | ||
1318 | usage2(_("file does not exist or is not readable"), path); | ||
1319 | } | 822 | } |
1320 | 823 | ||
1321 | bool process_arguments(int argc, char **argv) { | 824 | check_curl_config_wrapper process_arguments(int argc, char **argv) { |
1322 | char *p; | ||
1323 | int c = 1; | ||
1324 | char *temp; | ||
1325 | |||
1326 | enum { | 825 | enum { |
1327 | INVERT_REGEX = CHAR_MAX + 1, | 826 | INVERT_REGEX = CHAR_MAX + 1, |
1328 | SNI_OPTION, | 827 | SNI_OPTION, |
@@ -1333,81 +832,101 @@ bool process_arguments(int argc, char **argv) { | |||
1333 | AUTOMATIC_DECOMPRESSION, | 832 | AUTOMATIC_DECOMPRESSION, |
1334 | COOKIE_JAR, | 833 | COOKIE_JAR, |
1335 | HAPROXY_PROTOCOL, | 834 | HAPROXY_PROTOCOL, |
1336 | STATE_REGEX | 835 | STATE_REGEX, |
836 | OUTPUT_FORMAT | ||
1337 | }; | 837 | }; |
1338 | 838 | ||
1339 | int option = 0; | 839 | static struct option longopts[] = { |
1340 | int got_plus = 0; | 840 | STD_LONG_OPTS, |
1341 | static struct option longopts[] = {STD_LONG_OPTS, | 841 | {"link", no_argument, 0, 'L'}, |
1342 | {"link", no_argument, 0, 'L'}, | 842 | {"nohtml", no_argument, 0, 'n'}, |
1343 | {"nohtml", no_argument, 0, 'n'}, | 843 | {"ssl", optional_argument, 0, 'S'}, |
1344 | {"ssl", optional_argument, 0, 'S'}, | 844 | {"sni", no_argument, 0, SNI_OPTION}, |
1345 | {"sni", no_argument, 0, SNI_OPTION}, | 845 | {"post", required_argument, 0, 'P'}, |
1346 | {"post", required_argument, 0, 'P'}, | 846 | {"method", required_argument, 0, 'j'}, |
1347 | {"method", required_argument, 0, 'j'}, | 847 | {"IP-address", required_argument, 0, 'I'}, |
1348 | {"IP-address", required_argument, 0, 'I'}, | 848 | {"url", required_argument, 0, 'u'}, |
1349 | {"url", required_argument, 0, 'u'}, | 849 | {"port", required_argument, 0, 'p'}, |
1350 | {"port", required_argument, 0, 'p'}, | 850 | {"authorization", required_argument, 0, 'a'}, |
1351 | {"authorization", required_argument, 0, 'a'}, | 851 | {"proxy-authorization", required_argument, 0, 'b'}, |
1352 | {"proxy-authorization", required_argument, 0, 'b'}, | 852 | {"header-string", required_argument, 0, 'd'}, |
1353 | {"header-string", required_argument, 0, 'd'}, | 853 | {"string", required_argument, 0, 's'}, |
1354 | {"string", required_argument, 0, 's'}, | 854 | {"expect", required_argument, 0, 'e'}, |
1355 | {"expect", required_argument, 0, 'e'}, | 855 | {"regex", required_argument, 0, 'r'}, |
1356 | {"regex", required_argument, 0, 'r'}, | 856 | {"ereg", required_argument, 0, 'r'}, |
1357 | {"ereg", required_argument, 0, 'r'}, | 857 | {"eregi", required_argument, 0, 'R'}, |
1358 | {"eregi", required_argument, 0, 'R'}, | 858 | {"linespan", no_argument, 0, 'l'}, |
1359 | {"linespan", no_argument, 0, 'l'}, | 859 | {"onredirect", required_argument, 0, 'f'}, |
1360 | {"onredirect", required_argument, 0, 'f'}, | 860 | {"certificate", required_argument, 0, 'C'}, |
1361 | {"certificate", required_argument, 0, 'C'}, | 861 | {"client-cert", required_argument, 0, 'J'}, |
1362 | {"client-cert", required_argument, 0, 'J'}, | 862 | {"private-key", required_argument, 0, 'K'}, |
1363 | {"private-key", required_argument, 0, 'K'}, | 863 | {"ca-cert", required_argument, 0, CA_CERT_OPTION}, |
1364 | {"ca-cert", required_argument, 0, CA_CERT_OPTION}, | 864 | {"verify-cert", no_argument, 0, 'D'}, |
1365 | {"verify-cert", no_argument, 0, 'D'}, | 865 | {"continue-after-certificate", no_argument, 0, CONTINUE_AFTER_CHECK_CERT}, |
1366 | {"continue-after-certificate", no_argument, 0, CONTINUE_AFTER_CHECK_CERT}, | 866 | {"useragent", required_argument, 0, 'A'}, |
1367 | {"useragent", required_argument, 0, 'A'}, | 867 | {"header", required_argument, 0, 'k'}, |
1368 | {"header", required_argument, 0, 'k'}, | 868 | {"no-body", no_argument, 0, 'N'}, |
1369 | {"no-body", no_argument, 0, 'N'}, | 869 | {"max-age", required_argument, 0, 'M'}, |
1370 | {"max-age", required_argument, 0, 'M'}, | 870 | {"content-type", required_argument, 0, 'T'}, |
1371 | {"content-type", required_argument, 0, 'T'}, | 871 | {"pagesize", required_argument, 0, 'm'}, |
1372 | {"pagesize", required_argument, 0, 'm'}, | 872 | {"invert-regex", no_argument, NULL, INVERT_REGEX}, |
1373 | {"invert-regex", no_argument, NULL, INVERT_REGEX}, | 873 | {"state-regex", required_argument, 0, STATE_REGEX}, |
1374 | {"state-regex", required_argument, 0, STATE_REGEX}, | 874 | {"use-ipv4", no_argument, 0, '4'}, |
1375 | {"use-ipv4", no_argument, 0, '4'}, | 875 | {"use-ipv6", no_argument, 0, '6'}, |
1376 | {"use-ipv6", no_argument, 0, '6'}, | 876 | {"extended-perfdata", no_argument, 0, 'E'}, |
1377 | {"extended-perfdata", no_argument, 0, 'E'}, | 877 | {"show-body", no_argument, 0, 'B'}, |
1378 | {"show-body", no_argument, 0, 'B'}, | 878 | {"max-redirs", required_argument, 0, MAX_REDIRS_OPTION}, |
1379 | {"max-redirs", required_argument, 0, MAX_REDIRS_OPTION}, | 879 | {"http-version", required_argument, 0, HTTP_VERSION_OPTION}, |
1380 | {"http-version", required_argument, 0, HTTP_VERSION_OPTION}, | 880 | {"enable-automatic-decompression", no_argument, 0, AUTOMATIC_DECOMPRESSION}, |
1381 | {"enable-automatic-decompression", no_argument, 0, AUTOMATIC_DECOMPRESSION}, | 881 | {"cookie-jar", required_argument, 0, COOKIE_JAR}, |
1382 | {"cookie-jar", required_argument, 0, COOKIE_JAR}, | 882 | {"haproxy-protocol", no_argument, 0, HAPROXY_PROTOCOL}, |
1383 | {"haproxy-protocol", no_argument, 0, HAPROXY_PROTOCOL}, | 883 | {"output-format", required_argument, 0, OUTPUT_FORMAT}, |
1384 | {0, 0, 0, 0}}; | 884 | {0, 0, 0, 0}}; |
1385 | 885 | ||
1386 | if (argc < 2) | 886 | check_curl_config_wrapper result = { |
1387 | return false; | 887 | .errorcode = OK, |
888 | .config = check_curl_config_init(), | ||
889 | }; | ||
1388 | 890 | ||
1389 | /* support check_http compatible arguments */ | 891 | if (argc < 2) { |
1390 | for (c = 1; c < argc; c++) { | 892 | result.errorcode = ERROR; |
1391 | if (strcmp("-to", argv[c]) == 0) | 893 | return result; |
1392 | strcpy(argv[c], "-t"); | ||
1393 | if (strcmp("-hn", argv[c]) == 0) | ||
1394 | strcpy(argv[c], "-H"); | ||
1395 | if (strcmp("-wt", argv[c]) == 0) | ||
1396 | strcpy(argv[c], "-w"); | ||
1397 | if (strcmp("-ct", argv[c]) == 0) | ||
1398 | strcpy(argv[c], "-c"); | ||
1399 | if (strcmp("-nohtml", argv[c]) == 0) | ||
1400 | strcpy(argv[c], "-n"); | ||
1401 | } | 894 | } |
1402 | 895 | ||
1403 | server_url = strdup(DEFAULT_SERVER_URL); | 896 | /* support check_http compatible arguments */ |
897 | for (int index = 1; index < argc; index++) { | ||
898 | if (strcmp("-to", argv[index]) == 0) { | ||
899 | strcpy(argv[index], "-t"); | ||
900 | } | ||
901 | if (strcmp("-hn", argv[index]) == 0) { | ||
902 | strcpy(argv[index], "-H"); | ||
903 | } | ||
904 | if (strcmp("-wt", argv[index]) == 0) { | ||
905 | strcpy(argv[index], "-w"); | ||
906 | } | ||
907 | if (strcmp("-ct", argv[index]) == 0) { | ||
908 | strcpy(argv[index], "-c"); | ||
909 | } | ||
910 | if (strcmp("-nohtml", argv[index]) == 0) { | ||
911 | strcpy(argv[index], "-n"); | ||
912 | } | ||
913 | } | ||
1404 | 914 | ||
1405 | while (1) { | 915 | int option = 0; |
1406 | c = getopt_long(argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:d:e:p:s:R:r:u:f:C:J:K:DnlLS::m:M:NEB", longopts, &option); | 916 | int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; |
1407 | if (c == -1 || c == EOF || c == 1) | 917 | bool specify_port = false; |
918 | bool enable_tls = false; | ||
919 | char *tls_option_optarg = NULL; | ||
920 | |||
921 | while (true) { | ||
922 | int option_index = getopt_long( | ||
923 | argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:d:e:p:s:R:r:u:f:C:J:K:DnlLS::m:M:NEB", | ||
924 | longopts, &option); | ||
925 | if (option_index == -1 || option_index == EOF || option_index == 1) { | ||
1408 | break; | 926 | break; |
927 | } | ||
1409 | 928 | ||
1410 | switch (c) { | 929 | switch (option_index) { |
1411 | case 'h': | 930 | case 'h': |
1412 | print_help(); | 931 | print_help(); |
1413 | exit(STATE_UNKNOWN); | 932 | exit(STATE_UNKNOWN); |
@@ -1421,270 +940,253 @@ bool process_arguments(int argc, char **argv) { | |||
1421 | verbose++; | 940 | verbose++; |
1422 | break; | 941 | break; |
1423 | case 't': /* timeout period */ | 942 | case 't': /* timeout period */ |
1424 | if (!is_intnonneg(optarg)) | 943 | if (!is_intnonneg(optarg)) { |
1425 | usage2(_("Timeout interval must be a positive integer"), optarg); | 944 | usage2(_("Timeout interval must be a positive integer"), optarg); |
1426 | else | 945 | } else { |
1427 | socket_timeout = (int)strtol(optarg, NULL, 10); | 946 | result.config.curl_config.socket_timeout = (int)strtol(optarg, NULL, 10); |
947 | } | ||
1428 | break; | 948 | break; |
1429 | case 'c': /* critical time threshold */ | 949 | case 'c': /* critical time threshold */ |
1430 | critical_thresholds = optarg; | 950 | { |
1431 | break; | 951 | mp_range_parsed critical_range = mp_parse_range_string(optarg); |
952 | if (critical_range.error != MP_PARSING_SUCCES) { | ||
953 | die(STATE_UNKNOWN, "failed to parse critical threshold: %s", optarg); | ||
954 | } | ||
955 | result.config.thlds = mp_thresholds_set_crit(result.config.thlds, critical_range.range); | ||
956 | } break; | ||
1432 | case 'w': /* warning time threshold */ | 957 | case 'w': /* warning time threshold */ |
1433 | warning_thresholds = optarg; | 958 | { |
1434 | break; | 959 | mp_range_parsed warning_range = mp_parse_range_string(optarg); |
960 | |||
961 | if (warning_range.error != MP_PARSING_SUCCES) { | ||
962 | die(STATE_UNKNOWN, "failed to parse warning threshold: %s", optarg); | ||
963 | } | ||
964 | result.config.thlds = mp_thresholds_set_warn(result.config.thlds, warning_range.range); | ||
965 | } break; | ||
1435 | case 'H': /* virtual host */ | 966 | case 'H': /* virtual host */ |
1436 | host_name = strdup(optarg); | 967 | result.config.initial_config.host_name = strdup(optarg); |
1437 | if (host_name[0] == '[') { | 968 | char *tmp_string; |
1438 | if ((p = strstr(host_name, "]:")) != NULL) { /* [IPv6]:port */ | 969 | size_t host_name_length; |
1439 | virtual_port = atoi(p + 2); | 970 | if (result.config.initial_config.host_name[0] == '[') { |
971 | if ((tmp_string = strstr(result.config.initial_config.host_name, "]:")) != | ||
972 | NULL) { /* [IPv6]:port */ | ||
973 | result.config.initial_config.virtualPort = atoi(tmp_string + 2); | ||
1440 | /* cut off the port */ | 974 | /* cut off the port */ |
1441 | host_name_length = strlen(host_name) - strlen(p) - 1; | 975 | host_name_length = |
1442 | free(host_name); | 976 | strlen(result.config.initial_config.host_name) - strlen(tmp_string) - 1; |
1443 | host_name = strndup(optarg, host_name_length); | 977 | free(result.config.initial_config.host_name); |
978 | result.config.initial_config.host_name = strndup(optarg, host_name_length); | ||
1444 | } | 979 | } |
1445 | } else if ((p = strchr(host_name, ':')) != NULL && strchr(++p, ':') == NULL) { /* IPv4:port or host:port */ | 980 | } else if ((tmp_string = strchr(result.config.initial_config.host_name, ':')) != NULL && |
1446 | virtual_port = atoi(p); | 981 | strchr(++tmp_string, ':') == NULL) { /* IPv4:port or host:port */ |
982 | result.config.initial_config.virtualPort = atoi(tmp_string); | ||
1447 | /* cut off the port */ | 983 | /* cut off the port */ |
1448 | host_name_length = strlen(host_name) - strlen(p) - 1; | 984 | host_name_length = |
1449 | free(host_name); | 985 | strlen(result.config.initial_config.host_name) - strlen(tmp_string) - 1; |
1450 | host_name = strndup(optarg, host_name_length); | 986 | free(result.config.initial_config.host_name); |
987 | result.config.initial_config.host_name = strndup(optarg, host_name_length); | ||
1451 | } | 988 | } |
1452 | break; | 989 | break; |
1453 | case 'I': /* internet address */ | 990 | case 'I': /* internet address */ |
1454 | server_address = strdup(optarg); | 991 | result.config.initial_config.server_address = strdup(optarg); |
1455 | break; | 992 | break; |
1456 | case 'u': /* URL path */ | 993 | case 'u': /* URL path */ |
1457 | server_url = strdup(optarg); | 994 | result.config.initial_config.server_url = strdup(optarg); |
1458 | break; | 995 | break; |
1459 | case 'p': /* Server port */ | 996 | case 'p': /* Server port */ |
1460 | if (!is_intnonneg(optarg)) | 997 | if (!is_intnonneg(optarg)) { |
1461 | usage2(_("Invalid port number, expecting a non-negative number"), optarg); | 998 | usage2(_("Invalid port number, expecting a non-negative number"), optarg); |
1462 | else { | 999 | } else { |
1463 | if (strtol(optarg, NULL, 10) > MAX_PORT) | 1000 | if (strtol(optarg, NULL, 10) > MAX_PORT) { |
1464 | usage2(_("Invalid port number, supplied port number is too big"), optarg); | 1001 | usage2(_("Invalid port number, supplied port number is too big"), optarg); |
1465 | server_port = (unsigned short)strtol(optarg, NULL, 10); | 1002 | } |
1003 | result.config.initial_config.serverPort = (unsigned short)strtol(optarg, NULL, 10); | ||
1466 | specify_port = true; | 1004 | specify_port = true; |
1467 | } | 1005 | } |
1468 | break; | 1006 | break; |
1469 | case 'a': /* authorization info */ | 1007 | case 'a': /* authorization info */ |
1470 | strncpy(user_auth, optarg, MAX_INPUT_BUFFER - 1); | 1008 | strncpy(result.config.curl_config.user_auth, optarg, MAX_INPUT_BUFFER - 1); |
1471 | user_auth[MAX_INPUT_BUFFER - 1] = 0; | 1009 | result.config.curl_config.user_auth[MAX_INPUT_BUFFER - 1] = 0; |
1472 | break; | 1010 | break; |
1473 | case 'b': /* proxy-authorization info */ | 1011 | case 'b': /* proxy-authorization info */ |
1474 | strncpy(proxy_auth, optarg, MAX_INPUT_BUFFER - 1); | 1012 | strncpy(result.config.curl_config.proxy_auth, optarg, MAX_INPUT_BUFFER - 1); |
1475 | proxy_auth[MAX_INPUT_BUFFER - 1] = 0; | 1013 | result.config.curl_config.proxy_auth[MAX_INPUT_BUFFER - 1] = 0; |
1476 | break; | 1014 | break; |
1477 | case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */ | 1015 | case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */ |
1478 | if (!http_post_data) | 1016 | if (!result.config.initial_config.http_post_data) { |
1479 | http_post_data = strdup(optarg); | 1017 | result.config.initial_config.http_post_data = strdup(optarg); |
1480 | if (!http_method) | 1018 | } |
1481 | http_method = strdup("POST"); | 1019 | if (!result.config.initial_config.http_method) { |
1020 | result.config.initial_config.http_method = strdup("POST"); | ||
1021 | } | ||
1482 | break; | 1022 | break; |
1483 | case 'j': /* Set HTTP method */ | 1023 | case 'j': /* Set HTTP method */ |
1484 | if (http_method) | 1024 | if (result.config.initial_config.http_method) { |
1485 | free(http_method); | 1025 | free(result.config.initial_config.http_method); |
1486 | http_method = strdup(optarg); | 1026 | } |
1027 | result.config.initial_config.http_method = strdup(optarg); | ||
1487 | break; | 1028 | break; |
1488 | case 'A': /* useragent */ | 1029 | case 'A': /* useragent */ |
1489 | strncpy(user_agent, optarg, DEFAULT_BUFFER_SIZE); | 1030 | strncpy(result.config.curl_config.user_agent, optarg, DEFAULT_BUFFER_SIZE); |
1490 | user_agent[DEFAULT_BUFFER_SIZE - 1] = '\0'; | 1031 | result.config.curl_config.user_agent[DEFAULT_BUFFER_SIZE - 1] = '\0'; |
1491 | break; | 1032 | break; |
1492 | case 'k': /* Additional headers */ | 1033 | case 'k': /* Additional headers */ |
1493 | if (http_opt_headers_count == 0) | 1034 | if (result.config.curl_config.http_opt_headers_count == 0) { |
1494 | http_opt_headers = malloc(sizeof(char *) * (++http_opt_headers_count)); | 1035 | result.config.curl_config.http_opt_headers = |
1495 | else | 1036 | malloc(sizeof(char *) * (++result.config.curl_config.http_opt_headers_count)); |
1496 | http_opt_headers = realloc(http_opt_headers, sizeof(char *) * (++http_opt_headers_count)); | 1037 | } else { |
1497 | http_opt_headers[http_opt_headers_count - 1] = optarg; | 1038 | result.config.curl_config.http_opt_headers = |
1039 | realloc(result.config.curl_config.http_opt_headers, | ||
1040 | sizeof(char *) * (++result.config.curl_config.http_opt_headers_count)); | ||
1041 | } | ||
1042 | result.config.curl_config | ||
1043 | .http_opt_headers[result.config.curl_config.http_opt_headers_count - 1] = optarg; | ||
1498 | break; | 1044 | break; |
1499 | case 'L': /* show html link */ | 1045 | case 'L': /* show html link */ |
1500 | display_html = true; | ||
1501 | break; | ||
1502 | case 'n': /* do not show html link */ | 1046 | case 'n': /* do not show html link */ |
1503 | display_html = false; | 1047 | // HTML link related options are deprecated |
1504 | break; | 1048 | break; |
1505 | case 'C': /* Check SSL cert validity */ | 1049 | case 'C': /* Check SSL cert validity */ |
1506 | #ifdef LIBCURL_FEATURE_SSL | 1050 | #ifndef LIBCURL_FEATURE_SSL |
1507 | if ((temp = strchr(optarg, ',')) != NULL) { | 1051 | usage4(_("Invalid option - SSL is not available")); |
1508 | *temp = '\0'; | ||
1509 | if (!is_intnonneg(optarg)) | ||
1510 | usage2(_("Invalid certificate expiration period"), optarg); | ||
1511 | days_till_exp_warn = atoi(optarg); | ||
1512 | *temp = ','; | ||
1513 | temp++; | ||
1514 | if (!is_intnonneg(temp)) | ||
1515 | usage2(_("Invalid certificate expiration period"), temp); | ||
1516 | days_till_exp_crit = atoi(temp); | ||
1517 | } else { | ||
1518 | days_till_exp_crit = 0; | ||
1519 | if (!is_intnonneg(optarg)) | ||
1520 | usage2(_("Invalid certificate expiration period"), optarg); | ||
1521 | days_till_exp_warn = atoi(optarg); | ||
1522 | } | ||
1523 | check_cert = true; | ||
1524 | goto enable_ssl; | ||
1525 | #endif | 1052 | #endif |
1053 | { | ||
1054 | char *temp; | ||
1055 | if ((temp = strchr(optarg, ',')) != NULL) { | ||
1056 | *temp = '\0'; | ||
1057 | if (!is_intnonneg(optarg)) { | ||
1058 | usage2(_("Invalid certificate expiration period"), optarg); | ||
1059 | } | ||
1060 | result.config.days_till_exp_warn = atoi(optarg); | ||
1061 | *temp = ','; | ||
1062 | temp++; | ||
1063 | if (!is_intnonneg(temp)) { | ||
1064 | usage2(_("Invalid certificate expiration period"), temp); | ||
1065 | } | ||
1066 | result.config.days_till_exp_crit = atoi(temp); | ||
1067 | } else { | ||
1068 | result.config.days_till_exp_crit = 0; | ||
1069 | if (!is_intnonneg(optarg)) { | ||
1070 | usage2(_("Invalid certificate expiration period"), optarg); | ||
1071 | } | ||
1072 | result.config.days_till_exp_warn = atoi(optarg); | ||
1073 | } | ||
1074 | result.config.check_cert = true; | ||
1075 | enable_tls = true; | ||
1076 | } | ||
1077 | break; | ||
1526 | case CONTINUE_AFTER_CHECK_CERT: /* don't stop after the certificate is checked */ | 1078 | case CONTINUE_AFTER_CHECK_CERT: /* don't stop after the certificate is checked */ |
1527 | #ifdef HAVE_SSL | 1079 | #ifdef HAVE_SSL |
1528 | continue_after_check_cert = true; | 1080 | result.config.continue_after_check_cert = true; |
1529 | break; | 1081 | break; |
1530 | #endif | 1082 | #endif |
1531 | case 'J': /* use client certificate */ | 1083 | case 'J': /* use client certificate */ |
1532 | #ifdef LIBCURL_FEATURE_SSL | 1084 | #ifndef LIBCURL_FEATURE_SSL |
1533 | test_file(optarg); | 1085 | usage4(_("Invalid option - SSL is not available")); |
1534 | client_cert = optarg; | ||
1535 | goto enable_ssl; | ||
1536 | #endif | 1086 | #endif |
1537 | case 'K': /* use client private key */ | ||
1538 | #ifdef LIBCURL_FEATURE_SSL | ||
1539 | test_file(optarg); | 1087 | test_file(optarg); |
1540 | client_privkey = optarg; | 1088 | result.config.curl_config.client_cert = optarg; |
1541 | goto enable_ssl; | 1089 | enable_tls = true; |
1090 | break; | ||
1091 | case 'K': /* use client private key */ | ||
1092 | #ifndef LIBCURL_FEATURE_SSL | ||
1093 | usage4(_("Invalid option - SSL is not available")); | ||
1542 | #endif | 1094 | #endif |
1543 | #ifdef LIBCURL_FEATURE_SSL | ||
1544 | case CA_CERT_OPTION: /* use CA chain file */ | ||
1545 | test_file(optarg); | 1095 | test_file(optarg); |
1546 | ca_cert = optarg; | 1096 | result.config.curl_config.client_privkey = optarg; |
1547 | goto enable_ssl; | 1097 | enable_tls = true; |
1098 | break; | ||
1099 | case CA_CERT_OPTION: /* use CA chain file */ | ||
1100 | #ifndef LIBCURL_FEATURE_SSL | ||
1101 | usage4(_("Invalid option - SSL is not available")); | ||
1548 | #endif | 1102 | #endif |
1549 | #ifdef LIBCURL_FEATURE_SSL | 1103 | test_file(optarg); |
1550 | case 'D': /* verify peer certificate & host */ | 1104 | result.config.curl_config.ca_cert = optarg; |
1551 | verify_peer_and_host = true; | 1105 | enable_tls = true; |
1552 | break; | 1106 | break; |
1107 | case 'D': /* verify peer certificate & host */ | ||
1108 | #ifndef LIBCURL_FEATURE_SSL | ||
1109 | usage4(_("Invalid option - SSL is not available")); | ||
1553 | #endif | 1110 | #endif |
1554 | case 'S': /* use SSL */ | 1111 | result.config.curl_config.verify_peer_and_host = true; |
1555 | #ifdef LIBCURL_FEATURE_SSL | 1112 | enable_tls = true; |
1556 | enable_ssl: | ||
1557 | use_ssl = true; | ||
1558 | /* ssl_version initialized to CURL_SSLVERSION_DEFAULT as a default. | ||
1559 | * Only set if it's non-zero. This helps when we include multiple | ||
1560 | * parameters, like -S and -C combinations */ | ||
1561 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1562 | if (c == 'S' && optarg != NULL) { | ||
1563 | char *plus_ptr = strchr(optarg, '+'); | ||
1564 | if (plus_ptr) { | ||
1565 | got_plus = 1; | ||
1566 | *plus_ptr = '\0'; | ||
1567 | } | ||
1568 | |||
1569 | if (optarg[0] == '2') | ||
1570 | ssl_version = CURL_SSLVERSION_SSLv2; | ||
1571 | else if (optarg[0] == '3') | ||
1572 | ssl_version = CURL_SSLVERSION_SSLv3; | ||
1573 | else if (!strcmp(optarg, "1") || !strcmp(optarg, "1.0")) | ||
1574 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
1575 | ssl_version = CURL_SSLVERSION_TLSv1_0; | ||
1576 | # else | ||
1577 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1578 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
1579 | else if (!strcmp(optarg, "1.1")) | ||
1580 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
1581 | ssl_version = CURL_SSLVERSION_TLSv1_1; | ||
1582 | # else | ||
1583 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1584 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
1585 | else if (!strcmp(optarg, "1.2")) | ||
1586 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
1587 | ssl_version = CURL_SSLVERSION_TLSv1_2; | ||
1588 | # else | ||
1589 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1590 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
1591 | else if (!strcmp(optarg, "1.3")) | ||
1592 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) | ||
1593 | ssl_version = CURL_SSLVERSION_TLSv1_3; | ||
1594 | # else | ||
1595 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1596 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) */ | ||
1597 | else | ||
1598 | usage4(_("Invalid option - Valid SSL/TLS versions: 2, 3, 1, 1.1, 1.2, 1.3 (with optional '+' suffix)")); | ||
1599 | } | ||
1600 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) | ||
1601 | if (got_plus) { | ||
1602 | switch (ssl_version) { | ||
1603 | case CURL_SSLVERSION_TLSv1_3: | ||
1604 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_3; | ||
1605 | break; | ||
1606 | case CURL_SSLVERSION_TLSv1_2: | ||
1607 | case CURL_SSLVERSION_TLSv1_1: | ||
1608 | case CURL_SSLVERSION_TLSv1_0: | ||
1609 | ssl_version |= CURL_SSLVERSION_MAX_DEFAULT; | ||
1610 | break; | ||
1611 | } | ||
1612 | } else { | ||
1613 | switch (ssl_version) { | ||
1614 | case CURL_SSLVERSION_TLSv1_3: | ||
1615 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_3; | ||
1616 | break; | ||
1617 | case CURL_SSLVERSION_TLSv1_2: | ||
1618 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_2; | ||
1619 | break; | ||
1620 | case CURL_SSLVERSION_TLSv1_1: | ||
1621 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_1; | ||
1622 | break; | ||
1623 | case CURL_SSLVERSION_TLSv1_0: | ||
1624 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_0; | ||
1625 | break; | ||
1626 | } | ||
1627 | } | ||
1628 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) */ | ||
1629 | if (verbose >= 2) | ||
1630 | printf(_("* Set SSL/TLS version to %d\n"), ssl_version); | ||
1631 | if (!specify_port) | ||
1632 | server_port = HTTPS_PORT; | ||
1633 | break; | 1113 | break; |
1634 | #else /* LIBCURL_FEATURE_SSL */ | 1114 | case 'S': /* use SSL */ |
1635 | /* -C -J and -K fall through to here without SSL */ | 1115 | tls_option_optarg = optarg; |
1116 | enable_tls = true; | ||
1117 | #ifndef LIBCURL_FEATURE_SSL | ||
1636 | usage4(_("Invalid option - SSL is not available")); | 1118 | usage4(_("Invalid option - SSL is not available")); |
1119 | #endif | ||
1637 | break; | 1120 | break; |
1638 | case SNI_OPTION: /* --sni is parsed, but ignored, the default is true with libcurl */ | 1121 | case SNI_OPTION: /* --sni is parsed, but ignored, the default is true with libcurl */ |
1639 | use_sni = true; | 1122 | #ifndef LIBCURL_FEATURE_SSL |
1640 | break; | 1123 | usage4(_("Invalid option - SSL is not available")); |
1641 | #endif /* LIBCURL_FEATURE_SSL */ | 1124 | #endif /* LIBCURL_FEATURE_SSL */ |
1125 | break; | ||
1642 | case MAX_REDIRS_OPTION: | 1126 | case MAX_REDIRS_OPTION: |
1643 | if (!is_intnonneg(optarg)) | 1127 | if (!is_intnonneg(optarg)) { |
1644 | usage2(_("Invalid max_redirs count"), optarg); | 1128 | usage2(_("Invalid max_redirs count"), optarg); |
1645 | else { | 1129 | } else { |
1646 | max_depth = atoi(optarg); | 1130 | result.config.max_depth = atoi(optarg); |
1647 | } | 1131 | } |
1648 | break; | 1132 | break; |
1649 | case 'f': /* onredirect */ | 1133 | case 'f': /* onredirect */ |
1650 | if (!strcmp(optarg, "ok")) | 1134 | if (!strcmp(optarg, "ok")) { |
1651 | onredirect = STATE_OK; | 1135 | result.config.on_redirect_result_state = STATE_OK; |
1652 | else if (!strcmp(optarg, "warning")) | 1136 | result.config.on_redirect_dependent = false; |
1653 | onredirect = STATE_WARNING; | 1137 | } else if (!strcmp(optarg, "warning")) { |
1654 | else if (!strcmp(optarg, "critical")) | 1138 | result.config.on_redirect_result_state = STATE_WARNING; |
1655 | onredirect = STATE_CRITICAL; | 1139 | result.config.on_redirect_dependent = false; |
1656 | else if (!strcmp(optarg, "unknown")) | 1140 | } else if (!strcmp(optarg, "critical")) { |
1657 | onredirect = STATE_UNKNOWN; | 1141 | result.config.on_redirect_result_state = STATE_CRITICAL; |
1658 | else if (!strcmp(optarg, "follow")) | 1142 | result.config.on_redirect_dependent = false; |
1659 | onredirect = STATE_DEPENDENT; | 1143 | } else if (!strcmp(optarg, "unknown")) { |
1660 | else if (!strcmp(optarg, "stickyport")) | 1144 | result.config.on_redirect_result_state = STATE_UNKNOWN; |
1661 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_HOST | STICKY_PORT; | 1145 | result.config.on_redirect_dependent = false; |
1662 | else if (!strcmp(optarg, "sticky")) | 1146 | } else if (!strcmp(optarg, "follow")) { |
1663 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_HOST; | 1147 | result.config.on_redirect_dependent = true; |
1664 | else if (!strcmp(optarg, "follow")) | 1148 | } else if (!strcmp(optarg, "stickyport")) { |
1665 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_NONE; | 1149 | result.config.on_redirect_dependent = true; |
1666 | else if (!strcmp(optarg, "curl")) | 1150 | result.config.followmethod = FOLLOW_HTTP_CURL, |
1667 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_LIBCURL; | 1151 | result.config.followsticky = STICKY_HOST | STICKY_PORT; |
1668 | else | 1152 | } else if (!strcmp(optarg, "sticky")) { |
1153 | result.config.on_redirect_dependent = true; | ||
1154 | result.config.followmethod = FOLLOW_HTTP_CURL, | ||
1155 | result.config.followsticky = STICKY_HOST; | ||
1156 | } else if (!strcmp(optarg, "follow")) { | ||
1157 | result.config.on_redirect_dependent = true; | ||
1158 | result.config.followmethod = FOLLOW_HTTP_CURL, | ||
1159 | result.config.followsticky = STICKY_NONE; | ||
1160 | } else if (!strcmp(optarg, "curl")) { | ||
1161 | result.config.on_redirect_dependent = true; | ||
1162 | result.config.followmethod = FOLLOW_LIBCURL; | ||
1163 | } else { | ||
1669 | usage2(_("Invalid onredirect option"), optarg); | 1164 | usage2(_("Invalid onredirect option"), optarg); |
1670 | if (verbose >= 2) | 1165 | } |
1671 | printf(_("* Following redirects set to %s\n"), state_text(onredirect)); | 1166 | if (verbose >= 2) { |
1167 | if (result.config.on_redirect_dependent) { | ||
1168 | printf(_("* Following redirects\n")); | ||
1169 | } else { | ||
1170 | printf(_("* Following redirects set to state %s\n"), | ||
1171 | state_text(result.config.on_redirect_result_state)); | ||
1172 | } | ||
1173 | } | ||
1672 | break; | 1174 | break; |
1673 | case 'd': /* string or substring */ | 1175 | case 'd': /* string or substring */ |
1674 | strncpy(header_expect, optarg, MAX_INPUT_BUFFER - 1); | 1176 | strncpy(result.config.header_expect, optarg, MAX_INPUT_BUFFER - 1); |
1675 | header_expect[MAX_INPUT_BUFFER - 1] = 0; | 1177 | result.config.header_expect[MAX_INPUT_BUFFER - 1] = 0; |
1676 | break; | 1178 | break; |
1677 | case 's': /* string or substring */ | 1179 | case 's': /* string or substring */ |
1678 | strncpy(string_expect, optarg, MAX_INPUT_BUFFER - 1); | 1180 | strncpy(result.config.string_expect, optarg, MAX_INPUT_BUFFER - 1); |
1679 | string_expect[MAX_INPUT_BUFFER - 1] = 0; | 1181 | result.config.string_expect[MAX_INPUT_BUFFER - 1] = 0; |
1680 | break; | 1182 | break; |
1681 | case 'e': /* string or substring */ | 1183 | case 'e': /* string or substring */ |
1682 | strncpy(server_expect, optarg, MAX_INPUT_BUFFER - 1); | 1184 | strncpy(result.config.server_expect.string, optarg, MAX_INPUT_BUFFER - 1); |
1683 | server_expect[MAX_INPUT_BUFFER - 1] = 0; | 1185 | result.config.server_expect.string[MAX_INPUT_BUFFER - 1] = 0; |
1684 | server_expect_yn = 1; | 1186 | result.config.server_expect.is_present = true; |
1685 | break; | 1187 | break; |
1686 | case 'T': /* Content-type */ | 1188 | case 'T': /* Content-type */ |
1687 | http_content_type = strdup(optarg); | 1189 | result.config.curl_config.http_content_type = strdup(optarg); |
1688 | break; | 1190 | break; |
1689 | case 'l': /* linespan */ | 1191 | case 'l': /* linespan */ |
1690 | cflags &= ~REG_NEWLINE; | 1192 | cflags &= ~REG_NEWLINE; |
@@ -1693,185 +1195,258 @@ bool process_arguments(int argc, char **argv) { | |||
1693 | cflags |= REG_ICASE; | 1195 | cflags |= REG_ICASE; |
1694 | // fall through | 1196 | // fall through |
1695 | case 'r': /* regex */ | 1197 | case 'r': /* regex */ |
1696 | strncpy(regexp, optarg, MAX_RE_SIZE - 1); | 1198 | strncpy(result.config.regexp, optarg, MAX_RE_SIZE - 1); |
1697 | regexp[MAX_RE_SIZE - 1] = 0; | 1199 | result.config.regexp[MAX_RE_SIZE - 1] = 0; |
1698 | errcode = regcomp(&preg, regexp, cflags); | 1200 | regex_t preg; |
1201 | int errcode = regcomp(&preg, result.config.regexp, cflags); | ||
1699 | if (errcode != 0) { | 1202 | if (errcode != 0) { |
1700 | (void)regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); | 1203 | (void)regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); |
1701 | printf(_("Could Not Compile Regular Expression: %s"), errbuf); | 1204 | printf(_("Could Not Compile Regular Expression: %s"), errbuf); |
1702 | return false; | 1205 | result.errorcode = ERROR; |
1206 | return result; | ||
1703 | } | 1207 | } |
1208 | |||
1209 | result.config.compiled_regex = preg; | ||
1704 | break; | 1210 | break; |
1705 | case INVERT_REGEX: | 1211 | case INVERT_REGEX: |
1706 | invert_regex = true; | 1212 | result.config.invert_regex = true; |
1707 | break; | 1213 | break; |
1708 | case STATE_REGEX: | 1214 | case STATE_REGEX: |
1709 | if (!strcasecmp(optarg, "critical")) | 1215 | if (!strcasecmp(optarg, "critical")) { |
1710 | state_regex = STATE_CRITICAL; | 1216 | result.config.state_regex = STATE_CRITICAL; |
1711 | else if (!strcasecmp(optarg, "warning")) | 1217 | } else if (!strcasecmp(optarg, "warning")) { |
1712 | state_regex = STATE_WARNING; | 1218 | result.config.state_regex = STATE_WARNING; |
1713 | else | 1219 | } else { |
1714 | usage2(_("Invalid state-regex option"), optarg); | 1220 | usage2(_("Invalid state-regex option"), optarg); |
1221 | } | ||
1715 | break; | 1222 | break; |
1716 | case '4': | 1223 | case '4': |
1717 | address_family = AF_INET; | 1224 | result.config.curl_config.sin_family = AF_INET; |
1718 | break; | 1225 | break; |
1719 | case '6': | 1226 | case '6': |
1720 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) | 1227 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) |
1721 | address_family = AF_INET6; | 1228 | result.config.curl_config.sin_family = AF_INET6; |
1722 | #else | 1229 | #else |
1723 | usage4(_("IPv6 support not available")); | 1230 | usage4(_("IPv6 support not available")); |
1724 | #endif | 1231 | #endif |
1725 | break; | 1232 | break; |
1726 | case 'm': /* min_page_length */ | 1233 | case 'm': /* min_page_length */ |
1727 | { | 1234 | { |
1728 | char *tmp; | 1235 | mp_range_parsed foo = mp_parse_range_string(optarg); |
1729 | if (strchr(optarg, ':') != (char *)NULL) { | 1236 | |
1730 | /* range, so get two values, min:max */ | 1237 | if (foo.error != MP_PARSING_SUCCES) { |
1731 | tmp = strtok(optarg, ":"); | 1238 | die(STATE_CRITICAL, "failed to parse page size limits: %s", optarg); |
1732 | if (tmp == NULL) { | 1239 | } |
1733 | printf("Bad format: try \"-m min:max\"\n"); | 1240 | |
1734 | exit(STATE_WARNING); | 1241 | result.config.page_length_limits = foo.range; |
1735 | } else | 1242 | result.config.page_length_limits_is_set = true; |
1736 | min_page_len = atoi(tmp); | ||
1737 | |||
1738 | tmp = strtok(NULL, ":"); | ||
1739 | if (tmp == NULL) { | ||
1740 | printf("Bad format: try \"-m min:max\"\n"); | ||
1741 | exit(STATE_WARNING); | ||
1742 | } else | ||
1743 | max_page_len = atoi(tmp); | ||
1744 | } else | ||
1745 | min_page_len = atoi(optarg); | ||
1746 | break; | 1243 | break; |
1747 | } | 1244 | } |
1748 | case 'N': /* no-body */ | 1245 | case 'N': /* no-body */ |
1749 | no_body = true; | 1246 | result.config.initial_config.no_body = true; |
1750 | break; | 1247 | break; |
1751 | case 'M': /* max-age */ | 1248 | case 'M': /* max-age */ |
1752 | { | 1249 | { |
1753 | int L = strlen(optarg); | 1250 | size_t option_length = strlen(optarg); |
1754 | if (L && optarg[L - 1] == 'm') | 1251 | if (option_length && optarg[option_length - 1] == 'm') { |
1755 | maximum_age = atoi(optarg) * 60; | 1252 | result.config.maximum_age = atoi(optarg) * 60; |
1756 | else if (L && optarg[L - 1] == 'h') | 1253 | } else if (option_length && optarg[option_length - 1] == 'h') { |
1757 | maximum_age = atoi(optarg) * 60 * 60; | 1254 | result.config.maximum_age = atoi(optarg) * 60 * 60; |
1758 | else if (L && optarg[L - 1] == 'd') | 1255 | } else if (option_length && optarg[option_length - 1] == 'd') { |
1759 | maximum_age = atoi(optarg) * 60 * 60 * 24; | 1256 | result.config.maximum_age = atoi(optarg) * 60 * 60 * 24; |
1760 | else if (L && (optarg[L - 1] == 's' || isdigit(optarg[L - 1]))) | 1257 | } else if (option_length && |
1761 | maximum_age = atoi(optarg); | 1258 | (optarg[option_length - 1] == 's' || isdigit(optarg[option_length - 1]))) { |
1762 | else { | 1259 | result.config.maximum_age = atoi(optarg); |
1260 | } else { | ||
1763 | fprintf(stderr, "unparsable max-age: %s\n", optarg); | 1261 | fprintf(stderr, "unparsable max-age: %s\n", optarg); |
1764 | exit(STATE_WARNING); | 1262 | exit(STATE_WARNING); |
1765 | } | 1263 | } |
1766 | if (verbose >= 2) | 1264 | if (verbose >= 2) { |
1767 | printf("* Maximal age of document set to %d seconds\n", maximum_age); | 1265 | printf("* Maximal age of document set to %d seconds\n", result.config.maximum_age); |
1266 | } | ||
1768 | } break; | 1267 | } break; |
1769 | case 'E': /* show extended perfdata */ | 1268 | case 'E': /* show extended perfdata */ |
1770 | show_extended_perfdata = true; | 1269 | result.config.show_extended_perfdata = true; |
1771 | break; | 1270 | break; |
1772 | case 'B': /* print body content after status line */ | 1271 | case 'B': /* print body content after status line */ |
1773 | show_body = true; | 1272 | result.config.show_body = true; |
1774 | break; | 1273 | break; |
1775 | case HTTP_VERSION_OPTION: | 1274 | case HTTP_VERSION_OPTION: |
1776 | curl_http_version = CURL_HTTP_VERSION_NONE; | 1275 | result.config.curl_config.curl_http_version = CURL_HTTP_VERSION_NONE; |
1777 | if (strcmp(optarg, "1.0") == 0) { | 1276 | if (strcmp(optarg, "1.0") == 0) { |
1778 | curl_http_version = CURL_HTTP_VERSION_1_0; | 1277 | result.config.curl_config.curl_http_version = CURL_HTTP_VERSION_1_0; |
1779 | } else if (strcmp(optarg, "1.1") == 0) { | 1278 | } else if (strcmp(optarg, "1.1") == 0) { |
1780 | curl_http_version = CURL_HTTP_VERSION_1_1; | 1279 | result.config.curl_config.curl_http_version = CURL_HTTP_VERSION_1_1; |
1781 | } else if ((strcmp(optarg, "2.0") == 0) || (strcmp(optarg, "2") == 0)) { | 1280 | } else if ((strcmp(optarg, "2.0") == 0) || (strcmp(optarg, "2") == 0)) { |
1782 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 33, 0) | 1281 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 33, 0) |
1783 | curl_http_version = CURL_HTTP_VERSION_2_0; | 1282 | result.config.curl_config.curl_http_version = CURL_HTTP_VERSION_2_0; |
1784 | #else | 1283 | #else |
1785 | curl_http_version = CURL_HTTP_VERSION_NONE; | 1284 | result.config.curl_http_version = CURL_HTTP_VERSION_NONE; |
1786 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 33, 0) */ | 1285 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 33, 0) */ |
1286 | } else if ((strcmp(optarg, "3") == 0)) { | ||
1287 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 66, 0) | ||
1288 | result.config.curl_config.curl_http_version = CURL_HTTP_VERSION_3; | ||
1289 | #else | ||
1290 | result.config.curl_config.curl_http_version = CURL_HTTP_VERSION_NONE; | ||
1291 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 66, 0) */ | ||
1787 | } else { | 1292 | } else { |
1788 | fprintf(stderr, "unknown http-version parameter: %s\n", optarg); | 1293 | fprintf(stderr, "unknown http-version parameter: %s\n", optarg); |
1789 | exit(STATE_WARNING); | 1294 | exit(STATE_WARNING); |
1790 | } | 1295 | } |
1791 | break; | 1296 | break; |
1792 | case AUTOMATIC_DECOMPRESSION: | 1297 | case AUTOMATIC_DECOMPRESSION: |
1793 | automatic_decompression = true; | 1298 | result.config.curl_config.automatic_decompression = true; |
1794 | break; | 1299 | break; |
1795 | case COOKIE_JAR: | 1300 | case COOKIE_JAR: |
1796 | cookie_jar_file = optarg; | 1301 | result.config.curl_config.cookie_jar_file = optarg; |
1797 | break; | 1302 | break; |
1798 | case HAPROXY_PROTOCOL: | 1303 | case HAPROXY_PROTOCOL: |
1799 | haproxy_protocol = true; | 1304 | result.config.curl_config.haproxy_protocol = true; |
1800 | break; | 1305 | break; |
1801 | case '?': | 1306 | case '?': |
1802 | /* print short usage statement if args not parsable */ | 1307 | /* print short usage statement if args not parsable */ |
1803 | usage5(); | 1308 | usage5(); |
1804 | break; | 1309 | break; |
1310 | case OUTPUT_FORMAT: { | ||
1311 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
1312 | if (!parser.parsing_success) { | ||
1313 | // TODO List all available formats here, maybe add anothoer usage function | ||
1314 | printf("Invalid output format: %s\n", optarg); | ||
1315 | exit(STATE_UNKNOWN); | ||
1316 | } | ||
1317 | |||
1318 | result.config.output_format_is_set = true; | ||
1319 | result.config.output_format = parser.output_format; | ||
1320 | break; | ||
1321 | } | ||
1805 | } | 1322 | } |
1806 | } | 1323 | } |
1807 | 1324 | ||
1808 | c = optind; | 1325 | if (enable_tls) { |
1326 | bool got_plus = false; | ||
1327 | result.config.initial_config.use_ssl = true; | ||
1328 | /* ssl_version initialized to CURL_SSLVERSION_DEFAULT as a default. | ||
1329 | * Only set if it's non-zero. This helps when we include multiple | ||
1330 | * parameters, like -S and -C combinations */ | ||
1331 | result.config.curl_config.ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1332 | if (tls_option_optarg != NULL) { | ||
1333 | char *plus_ptr = strchr(optarg, '+'); | ||
1334 | if (plus_ptr) { | ||
1335 | got_plus = true; | ||
1336 | *plus_ptr = '\0'; | ||
1337 | } | ||
1809 | 1338 | ||
1810 | if (server_address == NULL && c < argc) | 1339 | if (optarg[0] == '2') { |
1811 | server_address = strdup(argv[c++]); | 1340 | result.config.curl_config.ssl_version = CURL_SSLVERSION_SSLv2; |
1341 | } else if (optarg[0] == '3') { | ||
1342 | result.config.curl_config.ssl_version = CURL_SSLVERSION_SSLv3; | ||
1343 | } else if (!strcmp(optarg, "1") || !strcmp(optarg, "1.0")) { | ||
1344 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
1345 | result.config.curl_config.ssl_version = CURL_SSLVERSION_TLSv1_0; | ||
1346 | #else | ||
1347 | result.config.ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1348 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
1349 | } else if (!strcmp(optarg, "1.1")) { | ||
1350 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
1351 | result.config.curl_config.ssl_version = CURL_SSLVERSION_TLSv1_1; | ||
1352 | #else | ||
1353 | result.config.ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1354 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
1355 | } else if (!strcmp(optarg, "1.2")) { | ||
1356 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
1357 | result.config.curl_config.ssl_version = CURL_SSLVERSION_TLSv1_2; | ||
1358 | #else | ||
1359 | result.config.ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1360 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
1361 | } else if (!strcmp(optarg, "1.3")) { | ||
1362 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) | ||
1363 | result.config.curl_config.ssl_version = CURL_SSLVERSION_TLSv1_3; | ||
1364 | #else | ||
1365 | result.config.ssl_version = CURL_SSLVERSION_DEFAULT; | ||
1366 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) */ | ||
1367 | } else { | ||
1368 | usage4(_("Invalid option - Valid SSL/TLS versions: 2, 3, 1, 1.1, 1.2, 1.3 " | ||
1369 | "(with optional '+' suffix)")); | ||
1370 | } | ||
1371 | } | ||
1372 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) | ||
1373 | if (got_plus) { | ||
1374 | switch (result.config.curl_config.ssl_version) { | ||
1375 | case CURL_SSLVERSION_TLSv1_3: | ||
1376 | result.config.curl_config.ssl_version |= CURL_SSLVERSION_MAX_TLSv1_3; | ||
1377 | break; | ||
1378 | case CURL_SSLVERSION_TLSv1_2: | ||
1379 | case CURL_SSLVERSION_TLSv1_1: | ||
1380 | case CURL_SSLVERSION_TLSv1_0: | ||
1381 | result.config.curl_config.ssl_version |= CURL_SSLVERSION_MAX_DEFAULT; | ||
1382 | break; | ||
1383 | } | ||
1384 | } else { | ||
1385 | switch (result.config.curl_config.ssl_version) { | ||
1386 | case CURL_SSLVERSION_TLSv1_3: | ||
1387 | result.config.curl_config.ssl_version |= CURL_SSLVERSION_MAX_TLSv1_3; | ||
1388 | break; | ||
1389 | case CURL_SSLVERSION_TLSv1_2: | ||
1390 | result.config.curl_config.ssl_version |= CURL_SSLVERSION_MAX_TLSv1_2; | ||
1391 | break; | ||
1392 | case CURL_SSLVERSION_TLSv1_1: | ||
1393 | result.config.curl_config.ssl_version |= CURL_SSLVERSION_MAX_TLSv1_1; | ||
1394 | break; | ||
1395 | case CURL_SSLVERSION_TLSv1_0: | ||
1396 | result.config.curl_config.ssl_version |= CURL_SSLVERSION_MAX_TLSv1_0; | ||
1397 | break; | ||
1398 | } | ||
1399 | } | ||
1400 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) */ | ||
1401 | if (verbose >= 2) { | ||
1402 | printf(_("* Set SSL/TLS version to %d\n"), result.config.curl_config.ssl_version); | ||
1403 | } | ||
1404 | if (!specify_port) { | ||
1405 | result.config.initial_config.serverPort = HTTPS_PORT; | ||
1406 | } | ||
1407 | } | ||
1812 | 1408 | ||
1813 | if (host_name == NULL && c < argc) | 1409 | int option_counter = optind; |
1814 | host_name = strdup(argv[c++]); | ||
1815 | 1410 | ||
1816 | if (server_address == NULL) { | 1411 | if (result.config.initial_config.server_address == NULL && option_counter < argc) { |
1817 | if (host_name == NULL) | 1412 | result.config.initial_config.server_address = strdup(argv[option_counter++]); |
1818 | usage4(_("You must specify a server address or host name")); | ||
1819 | else | ||
1820 | server_address = strdup(host_name); | ||
1821 | } | 1413 | } |
1822 | 1414 | ||
1823 | set_thresholds(&thlds, warning_thresholds, critical_thresholds); | 1415 | if (result.config.initial_config.host_name == NULL && option_counter < argc) { |
1416 | result.config.initial_config.host_name = strdup(argv[option_counter++]); | ||
1417 | } | ||
1824 | 1418 | ||
1825 | if (critical_thresholds && thlds->critical->end > (double)socket_timeout) | 1419 | if (result.config.initial_config.server_address == NULL) { |
1826 | socket_timeout = (int)thlds->critical->end + 1; | 1420 | if (result.config.initial_config.host_name == NULL) { |
1827 | if (verbose >= 2) | 1421 | usage4(_("You must specify a server address or host name")); |
1828 | printf("* Socket timeout set to %ld seconds\n", socket_timeout); | 1422 | } else { |
1423 | result.config.initial_config.server_address = | ||
1424 | strdup(result.config.initial_config.host_name); | ||
1425 | } | ||
1426 | } | ||
1829 | 1427 | ||
1830 | if (http_method == NULL) | 1428 | if (result.config.initial_config.http_method == NULL) { |
1831 | http_method = strdup("GET"); | 1429 | result.config.initial_config.http_method = strdup("GET"); |
1430 | } | ||
1832 | 1431 | ||
1833 | if (client_cert && !client_privkey) | 1432 | if (result.config.curl_config.client_cert && !result.config.curl_config.client_privkey) { |
1834 | usage4(_("If you use a client certificate you must also specify a private key file")); | 1433 | usage4(_("If you use a client certificate you must also specify a private key file")); |
1835 | |||
1836 | if (virtual_port == 0) | ||
1837 | virtual_port = server_port; | ||
1838 | else { | ||
1839 | if ((use_ssl && server_port == HTTPS_PORT) || (!use_ssl && server_port == HTTP_PORT)) | ||
1840 | if (!specify_port) | ||
1841 | server_port = virtual_port; | ||
1842 | } | 1434 | } |
1843 | 1435 | ||
1844 | return true; | 1436 | if (result.config.initial_config.virtualPort == 0) { |
1845 | } | 1437 | result.config.initial_config.virtualPort = result.config.initial_config.serverPort; |
1846 | 1438 | } else { | |
1847 | char *perfd_time(double elapsed_time) { | 1439 | if ((result.config.initial_config.use_ssl && |
1848 | return fperfdata("time", elapsed_time, "s", thlds->warning ? true : false, thlds->warning ? thlds->warning->end : 0, | 1440 | result.config.initial_config.serverPort == HTTPS_PORT) || |
1849 | thlds->critical ? true : false, thlds->critical ? thlds->critical->end : 0, true, 0, true, socket_timeout); | 1441 | (!result.config.initial_config.use_ssl && |
1850 | } | 1442 | result.config.initial_config.serverPort == HTTP_PORT)) { |
1851 | 1443 | if (!specify_port) { | |
1852 | char *perfd_time_connect(double elapsed_time_connect) { | 1444 | result.config.initial_config.serverPort = result.config.initial_config.virtualPort; |
1853 | return fperfdata("time_connect", elapsed_time_connect, "s", false, 0, false, 0, false, 0, true, socket_timeout); | 1445 | } |
1854 | } | 1446 | } |
1855 | 1447 | } | |
1856 | char *perfd_time_ssl(double elapsed_time_ssl) { | ||
1857 | return fperfdata("time_ssl", elapsed_time_ssl, "s", false, 0, false, 0, false, 0, true, socket_timeout); | ||
1858 | } | ||
1859 | |||
1860 | char *perfd_time_headers(double elapsed_time_headers) { | ||
1861 | return fperfdata("time_headers", elapsed_time_headers, "s", false, 0, false, 0, false, 0, true, socket_timeout); | ||
1862 | } | ||
1863 | |||
1864 | char *perfd_time_firstbyte(double elapsed_time_firstbyte) { | ||
1865 | return fperfdata("time_firstbyte", elapsed_time_firstbyte, "s", false, 0, false, 0, false, 0, true, socket_timeout); | ||
1866 | } | ||
1867 | |||
1868 | char *perfd_time_transfer(double elapsed_time_transfer) { | ||
1869 | return fperfdata("time_transfer", elapsed_time_transfer, "s", false, 0, false, 0, false, 0, true, socket_timeout); | ||
1870 | } | ||
1871 | 1448 | ||
1872 | char *perfd_size(int page_len) { | 1449 | return result; |
1873 | return perfdata("size", page_len, "B", (min_page_len > 0 ? true : false), min_page_len, (min_page_len > 0 ? true : false), 0, true, 0, | ||
1874 | false, 0); | ||
1875 | } | 1450 | } |
1876 | 1451 | ||
1877 | void print_help(void) { | 1452 | void print_help(void) { |
@@ -1885,7 +1460,8 @@ void print_help(void) { | |||
1885 | printf("%s\n", _("strings and regular expressions, check connection times, and report on")); | 1460 | printf("%s\n", _("strings and regular expressions, check connection times, and report on")); |
1886 | printf("%s\n", _("certificate expiration times.")); | 1461 | printf("%s\n", _("certificate expiration times.")); |
1887 | printf("\n"); | 1462 | printf("\n"); |
1888 | printf("%s\n", _("It makes use of libcurl to do so. It tries to be as compatible to check_http")); | 1463 | printf("%s\n", |
1464 | _("It makes use of libcurl to do so. It tries to be as compatible to check_http")); | ||
1889 | printf("%s\n", _("as possible.")); | 1465 | printf("%s\n", _("as possible.")); |
1890 | 1466 | ||
1891 | printf("\n\n"); | 1467 | printf("\n\n"); |
@@ -1903,7 +1479,8 @@ void print_help(void) { | |||
1903 | printf(" %s\n", _("Host name argument for servers using host headers (virtual host)")); | 1479 | printf(" %s\n", _("Host name argument for servers using host headers (virtual host)")); |
1904 | printf(" %s\n", _("Append a port to include it in the header (eg: example.com:5000)")); | 1480 | printf(" %s\n", _("Append a port to include it in the header (eg: example.com:5000)")); |
1905 | printf(" %s\n", "-I, --IP-address=ADDRESS"); | 1481 | printf(" %s\n", "-I, --IP-address=ADDRESS"); |
1906 | printf(" %s\n", _("IP address or name (use numeric address if possible to bypass DNS lookup).")); | 1482 | printf(" %s\n", |
1483 | _("IP address or name (use numeric address if possible to bypass DNS lookup).")); | ||
1907 | printf(" %s\n", "-p, --port=INTEGER"); | 1484 | printf(" %s\n", "-p, --port=INTEGER"); |
1908 | printf(" %s", _("Port number (default: ")); | 1485 | printf(" %s", _("Port number (default: ")); |
1909 | printf("%d)\n", HTTP_PORT); | 1486 | printf("%d)\n", HTTP_PORT); |
@@ -1912,27 +1489,36 @@ void print_help(void) { | |||
1912 | 1489 | ||
1913 | #ifdef LIBCURL_FEATURE_SSL | 1490 | #ifdef LIBCURL_FEATURE_SSL |
1914 | printf(" %s\n", "-S, --ssl=VERSION[+]"); | 1491 | printf(" %s\n", "-S, --ssl=VERSION[+]"); |
1915 | printf(" %s\n", _("Connect via SSL. Port defaults to 443. VERSION is optional, and prevents")); | 1492 | printf(" %s\n", |
1493 | _("Connect via SSL. Port defaults to 443. VERSION is optional, and prevents")); | ||
1916 | printf(" %s\n", _("auto-negotiation (2 = SSLv2, 3 = SSLv3, 1 = TLSv1, 1.1 = TLSv1.1,")); | 1494 | printf(" %s\n", _("auto-negotiation (2 = SSLv2, 3 = SSLv3, 1 = TLSv1, 1.1 = TLSv1.1,")); |
1917 | printf(" %s\n", _("1.2 = TLSv1.2, 1.3 = TLSv1.3). With a '+' suffix, newer versions are also accepted.")); | 1495 | printf(" %s\n", _("1.2 = TLSv1.2, 1.3 = TLSv1.3). With a '+' suffix, newer versions are " |
1918 | printf(" %s\n", _("Note: SSLv2, SSLv3, TLSv1.0 and TLSv1.1 are deprecated and are usually disabled in libcurl")); | 1496 | "also accepted.")); |
1497 | printf(" %s\n", _("Note: SSLv2, SSLv3, TLSv1.0 and TLSv1.1 are deprecated and are usually " | ||
1498 | "disabled in libcurl")); | ||
1919 | printf(" %s\n", "--sni"); | 1499 | printf(" %s\n", "--sni"); |
1920 | printf(" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); | 1500 | printf(" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); |
1921 | # if LIBCURL_VERSION_NUM >= 0x071801 | 1501 | # if LIBCURL_VERSION_NUM >= 0x071801 |
1922 | printf(" %s\n", _("Note: --sni is the default in libcurl as SSLv2 and SSLV3 are deprecated and")); | 1502 | printf(" %s\n", |
1503 | _("Note: --sni is the default in libcurl as SSLv2 and SSLV3 are deprecated and")); | ||
1923 | printf(" %s\n", _(" SNI only really works since TLSv1.0")); | 1504 | printf(" %s\n", _(" SNI only really works since TLSv1.0")); |
1924 | # else | 1505 | # else |
1925 | printf(" %s\n", _("Note: SNI is not supported in libcurl before 7.18.1")); | 1506 | printf(" %s\n", _("Note: SNI is not supported in libcurl before 7.18.1")); |
1926 | # endif | 1507 | # endif |
1927 | printf(" %s\n", "-C, --certificate=INTEGER[,INTEGER]"); | 1508 | printf(" %s\n", "-C, --certificate=INTEGER[,INTEGER]"); |
1928 | printf(" %s\n", _("Minimum number of days a certificate has to be valid. Port defaults to 443.")); | 1509 | printf(" %s\n", |
1929 | printf(" %s\n", _("A STATE_WARNING is returned if the certificate has a validity less than the")); | 1510 | _("Minimum number of days a certificate has to be valid. Port defaults to 443.")); |
1930 | printf(" %s\n", _("first agument's value. If there is a second argument and the certificate's")); | 1511 | printf(" %s\n", |
1512 | _("A STATE_WARNING is returned if the certificate has a validity less than the")); | ||
1513 | printf(" %s\n", | ||
1514 | _("first agument's value. If there is a second argument and the certificate's")); | ||
1931 | printf(" %s\n", _("validity is less than its value, a STATE_CRITICAL is returned.")); | 1515 | printf(" %s\n", _("validity is less than its value, a STATE_CRITICAL is returned.")); |
1932 | printf(" %s\n", _("(When this option is used the URL is not checked by default. You can use")); | 1516 | printf(" %s\n", |
1517 | _("(When this option is used the URL is not checked by default. You can use")); | ||
1933 | printf(" %s\n", _(" --continue-after-certificate to override this behavior)")); | 1518 | printf(" %s\n", _(" --continue-after-certificate to override this behavior)")); |
1934 | printf(" %s\n", "--continue-after-certificate"); | 1519 | printf(" %s\n", "--continue-after-certificate"); |
1935 | printf(" %s\n", _("Allows the HTTP check to continue after performing the certificate check.")); | 1520 | printf(" %s\n", |
1521 | _("Allows the HTTP check to continue after performing the certificate check.")); | ||
1936 | printf(" %s\n", _("Does nothing unless -C is used.")); | 1522 | printf(" %s\n", _("Does nothing unless -C is used.")); |
1937 | printf(" %s\n", "-J, --client-cert=FILE"); | 1523 | printf(" %s\n", "-J, --client-cert=FILE"); |
1938 | printf(" %s\n", _("Name of file that contains the client certificate (PEM format)")); | 1524 | printf(" %s\n", _("Name of file that contains the client certificate (PEM format)")); |
@@ -1950,7 +1536,8 @@ void print_help(void) { | |||
1950 | printf(" %s\n", _("Comma-delimited list of strings, at least one of them is expected in")); | 1536 | printf(" %s\n", _("Comma-delimited list of strings, at least one of them is expected in")); |
1951 | printf(" %s", _("the first (status) line of the server response (default: ")); | 1537 | printf(" %s", _("the first (status) line of the server response (default: ")); |
1952 | printf("%s)\n", HTTP_EXPECT); | 1538 | printf("%s)\n", HTTP_EXPECT); |
1953 | printf(" %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)")); | 1539 | printf(" %s\n", |
1540 | _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)")); | ||
1954 | printf(" %s\n", "-d, --header-string=STRING"); | 1541 | printf(" %s\n", "-d, --header-string=STRING"); |
1955 | printf(" %s\n", _("String to expect in the response headers")); | 1542 | printf(" %s\n", _("String to expect in the response headers")); |
1956 | printf(" %s\n", "-s, --string=STRING"); | 1543 | printf(" %s\n", "-s, --string=STRING"); |
@@ -1959,7 +1546,8 @@ void print_help(void) { | |||
1959 | printf(" %s\n", _("URL to GET or POST (default: /)")); | 1546 | printf(" %s\n", _("URL to GET or POST (default: /)")); |
1960 | printf(" %s\n", "-P, --post=STRING"); | 1547 | printf(" %s\n", "-P, --post=STRING"); |
1961 | printf(" %s\n", _("URL decoded http POST data")); | 1548 | printf(" %s\n", _("URL decoded http POST data")); |
1962 | printf(" %s\n", "-j, --method=STRING (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, CONNECT)"); | 1549 | printf(" %s\n", |
1550 | "-j, --method=STRING (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, CONNECT)"); | ||
1963 | printf(" %s\n", _("Set HTTP method.")); | 1551 | printf(" %s\n", _("Set HTTP method.")); |
1964 | printf(" %s\n", "-N, --no-body"); | 1552 | printf(" %s\n", "-N, --no-body"); |
1965 | printf(" %s\n", _("Don't wait for document body: stop reading after headers.")); | 1553 | printf(" %s\n", _("Don't wait for document body: stop reading after headers.")); |
@@ -1979,7 +1567,8 @@ void print_help(void) { | |||
1979 | printf(" %s\n", _("Return STATE if found, OK if not (STATE is CRITICAL, per default)")); | 1567 | printf(" %s\n", _("Return STATE if found, OK if not (STATE is CRITICAL, per default)")); |
1980 | printf(" %s\n", _("can be changed with --state--regex)")); | 1568 | printf(" %s\n", _("can be changed with --state--regex)")); |
1981 | printf(" %s\n", "--state-regex=STATE"); | 1569 | printf(" %s\n", "--state-regex=STATE"); |
1982 | printf(" %s\n", _("Return STATE if regex is found, OK if not. STATE can be one of \"critical\",\"warning\"")); | 1570 | printf(" %s\n", _("Return STATE if regex is found, OK if not. STATE can be one of " |
1571 | "\"critical\",\"warning\"")); | ||
1983 | printf(" %s\n", "-a, --authorization=AUTH_PAIR"); | 1572 | printf(" %s\n", "-a, --authorization=AUTH_PAIR"); |
1984 | printf(" %s\n", _("Username:password on sites with basic authentication")); | 1573 | printf(" %s\n", _("Username:password on sites with basic authentication")); |
1985 | printf(" %s\n", "-b, --proxy-authorization=AUTH_PAIR"); | 1574 | printf(" %s\n", "-b, --proxy-authorization=AUTH_PAIR"); |
@@ -1987,13 +1576,14 @@ void print_help(void) { | |||
1987 | printf(" %s\n", "-A, --useragent=STRING"); | 1576 | printf(" %s\n", "-A, --useragent=STRING"); |
1988 | printf(" %s\n", _("String to be sent in http header as \"User Agent\"")); | 1577 | printf(" %s\n", _("String to be sent in http header as \"User Agent\"")); |
1989 | printf(" %s\n", "-k, --header=STRING"); | 1578 | printf(" %s\n", "-k, --header=STRING"); |
1990 | printf(" %s\n", _("Any other tags to be sent in http header. Use multiple times for additional headers")); | 1579 | printf(" %s\n", _("Any other tags to be sent in http header. Use multiple times for " |
1580 | "additional headers")); | ||
1991 | printf(" %s\n", "-E, --extended-perfdata"); | 1581 | printf(" %s\n", "-E, --extended-perfdata"); |
1992 | printf(" %s\n", _("Print additional performance data")); | 1582 | printf(" %s\n", _("Print additional performance data")); |
1993 | printf(" %s\n", "-B, --show-body"); | 1583 | printf(" %s\n", "-B, --show-body"); |
1994 | printf(" %s\n", _("Print body content below status line")); | 1584 | printf(" %s\n", _("Print body content below status line")); |
1995 | printf(" %s\n", "-L, --link"); | 1585 | // printf(" %s\n", "-L, --link"); |
1996 | printf(" %s\n", _("Wrap output in HTML link (obsoleted by urlize)")); | 1586 | // printf(" %s\n", _("Wrap output in HTML link (obsoleted by urlize)")); |
1997 | printf(" %s\n", "-f, --onredirect=<ok|warning|critical|follow|sticky|stickyport|curl>"); | 1587 | printf(" %s\n", "-f, --onredirect=<ok|warning|critical|follow|sticky|stickyport|curl>"); |
1998 | printf(" %s\n", _("How to handle redirected pages. sticky is like follow but stick to the")); | 1588 | printf(" %s\n", _("How to handle redirected pages. sticky is like follow but stick to the")); |
1999 | printf(" %s\n", _("specified IP address. stickyport also ensures port stays the same.")); | 1589 | printf(" %s\n", _("specified IP address. stickyport also ensures port stays the same.")); |
@@ -2003,20 +1593,25 @@ void print_help(void) { | |||
2003 | printf(" %s", _("Maximal number of redirects (default: ")); | 1593 | printf(" %s", _("Maximal number of redirects (default: ")); |
2004 | printf("%d)\n", DEFAULT_MAX_REDIRS); | 1594 | printf("%d)\n", DEFAULT_MAX_REDIRS); |
2005 | printf(" %s\n", "-m, --pagesize=INTEGER<:INTEGER>"); | 1595 | printf(" %s\n", "-m, --pagesize=INTEGER<:INTEGER>"); |
2006 | printf(" %s\n", _("Minimum page size required (bytes) : Maximum page size required (bytes)")); | 1596 | printf(" %s\n", |
1597 | _("Minimum page size required (bytes) : Maximum page size required (bytes)")); | ||
2007 | printf("\n"); | 1598 | printf("\n"); |
2008 | printf(" %s\n", "--http-version=VERSION"); | 1599 | printf(" %s\n", "--http-version=VERSION"); |
2009 | printf(" %s\n", _("Connect via specific HTTP protocol.")); | 1600 | printf(" %s\n", _("Connect via specific HTTP protocol.")); |
2010 | printf(" %s\n", _("1.0 = HTTP/1.0, 1.1 = HTTP/1.1, 2.0 = HTTP/2 (HTTP/2 will fail without -S)")); | 1601 | printf(" %s\n", |
1602 | _("1.0 = HTTP/1.0, 1.1 = HTTP/1.1, 2.0 = HTTP/2 (HTTP/2 will fail without -S)")); | ||
2011 | printf(" %s\n", "--enable-automatic-decompression"); | 1603 | printf(" %s\n", "--enable-automatic-decompression"); |
2012 | printf(" %s\n", _("Enable automatic decompression of body (CURLOPT_ACCEPT_ENCODING).")); | 1604 | printf(" %s\n", _("Enable automatic decompression of body (CURLOPT_ACCEPT_ENCODING).")); |
2013 | printf(" %s\n", "--haproxy-protocol"); | 1605 | printf(" %s\n", "--haproxy-protocol"); |
2014 | printf(" %s\n", _("Send HAProxy proxy protocol v1 header (CURLOPT_HAPROXYPROTOCOL).")); | 1606 | printf(" %s\n", _("Send HAProxy proxy protocol v1 header (CURLOPT_HAPROXYPROTOCOL).")); |
2015 | printf(" %s\n", "--cookie-jar=FILE"); | 1607 | printf(" %s\n", "--cookie-jar=FILE"); |
2016 | printf(" %s\n", _("Store cookies in the cookie jar and send them out when requested.")); | 1608 | printf(" %s\n", _("Store cookies in the cookie jar and send them out when requested.")); |
2017 | printf(" %s\n", _("Specify an empty string as FILE to enable curl's cookie engine without saving")); | 1609 | printf(" %s\n", |
2018 | printf(" %s\n", _("the cookies to disk. Only enabling the engine without saving to disk requires")); | 1610 | _("Specify an empty string as FILE to enable curl's cookie engine without saving")); |
2019 | printf(" %s\n", _("handling multiple requests internally to curl, so use it with --onredirect=curl")); | 1611 | printf(" %s\n", |
1612 | _("the cookies to disk. Only enabling the engine without saving to disk requires")); | ||
1613 | printf(" %s\n", | ||
1614 | _("handling multiple requests internally to curl, so use it with --onredirect=curl")); | ||
2020 | printf("\n"); | 1615 | printf("\n"); |
2021 | 1616 | ||
2022 | printf(UT_WARN_CRIT); | 1617 | printf(UT_WARN_CRIT); |
@@ -2025,13 +1620,18 @@ void print_help(void) { | |||
2025 | 1620 | ||
2026 | printf(UT_VERBOSE); | 1621 | printf(UT_VERBOSE); |
2027 | 1622 | ||
1623 | printf(UT_OUTPUT_FORMAT); | ||
1624 | |||
2028 | printf("\n"); | 1625 | printf("\n"); |
2029 | printf("%s\n", _("Notes:")); | 1626 | printf("%s\n", _("Notes:")); |
2030 | printf(" %s\n", _("This plugin will attempt to open an HTTP connection with the host.")); | 1627 | printf(" %s\n", _("This plugin will attempt to open an HTTP connection with the host.")); |
2031 | printf(" %s\n", _("Successful connects return STATE_OK, refusals and timeouts return STATE_CRITICAL")); | 1628 | printf(" %s\n", |
2032 | printf(" %s\n", _("other errors return STATE_UNKNOWN. Successful connects, but incorrect response")); | 1629 | _("Successful connects return STATE_OK, refusals and timeouts return STATE_CRITICAL")); |
1630 | printf(" %s\n", | ||
1631 | _("other errors return STATE_UNKNOWN. Successful connects, but incorrect response")); | ||
2033 | printf(" %s\n", _("messages from the host result in STATE_WARNING return values. If you are")); | 1632 | printf(" %s\n", _("messages from the host result in STATE_WARNING return values. If you are")); |
2034 | printf(" %s\n", _("checking a virtual server that uses 'host headers' you must supply the FQDN")); | 1633 | printf(" %s\n", |
1634 | _("checking a virtual server that uses 'host headers' you must supply the FQDN")); | ||
2035 | printf(" %s\n", _("(fully qualified domain name) as the [host_name] argument.")); | 1635 | printf(" %s\n", _("(fully qualified domain name) as the [host_name] argument.")); |
2036 | 1636 | ||
2037 | #ifdef LIBCURL_FEATURE_SSL | 1637 | #ifdef LIBCURL_FEATURE_SSL |
@@ -2047,38 +1647,53 @@ void print_help(void) { | |||
2047 | printf("%s\n", _("Examples:")); | 1647 | printf("%s\n", _("Examples:")); |
2048 | printf(" %s\n\n", "CHECK CONTENT: check_curl -w 5 -c 10 --ssl -H www.verisign.com"); | 1648 | printf(" %s\n\n", "CHECK CONTENT: check_curl -w 5 -c 10 --ssl -H www.verisign.com"); |
2049 | printf(" %s\n", _("When the 'www.verisign.com' server returns its content within 5 seconds,")); | 1649 | printf(" %s\n", _("When the 'www.verisign.com' server returns its content within 5 seconds,")); |
2050 | printf(" %s\n", _("a STATE_OK will be returned. When the server returns its content but exceeds")); | 1650 | printf(" %s\n", |
2051 | printf(" %s\n", _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | 1651 | _("a STATE_OK will be returned. When the server returns its content but exceeds")); |
1652 | printf(" %s\n", | ||
1653 | _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | ||
2052 | printf(" %s\n", _("a STATE_CRITICAL will be returned.")); | 1654 | printf(" %s\n", _("a STATE_CRITICAL will be returned.")); |
2053 | printf("\n"); | 1655 | printf("\n"); |
2054 | printf(" %s\n\n", "CHECK CERTIFICATE: check_curl -H www.verisign.com -C 14"); | 1656 | printf(" %s\n\n", "CHECK CERTIFICATE: check_curl -H www.verisign.com -C 14"); |
2055 | printf(" %s\n", _("When the certificate of 'www.verisign.com' is valid for more than 14 days,")); | 1657 | printf(" %s\n", |
2056 | printf(" %s\n", _("a STATE_OK is returned. When the certificate is still valid, but for less than")); | 1658 | _("When the certificate of 'www.verisign.com' is valid for more than 14 days,")); |
2057 | printf(" %s\n", _("14 days, a STATE_WARNING is returned. A STATE_CRITICAL will be returned when")); | 1659 | printf(" %s\n", |
1660 | _("a STATE_OK is returned. When the certificate is still valid, but for less than")); | ||
1661 | printf(" %s\n", | ||
1662 | _("14 days, a STATE_WARNING is returned. A STATE_CRITICAL will be returned when")); | ||
2058 | printf(" %s\n\n", _("the certificate is expired.")); | 1663 | printf(" %s\n\n", _("the certificate is expired.")); |
2059 | printf("\n"); | 1664 | printf("\n"); |
2060 | printf(" %s\n\n", "CHECK CERTIFICATE: check_curl -H www.verisign.com -C 30,14"); | 1665 | printf(" %s\n\n", "CHECK CERTIFICATE: check_curl -H www.verisign.com -C 30,14"); |
2061 | printf(" %s\n", _("When the certificate of 'www.verisign.com' is valid for more than 30 days,")); | 1666 | printf(" %s\n", |
2062 | printf(" %s\n", _("a STATE_OK is returned. When the certificate is still valid, but for less than")); | 1667 | _("When the certificate of 'www.verisign.com' is valid for more than 30 days,")); |
1668 | printf(" %s\n", | ||
1669 | _("a STATE_OK is returned. When the certificate is still valid, but for less than")); | ||
2063 | printf(" %s\n", _("30 days, but more than 14 days, a STATE_WARNING is returned.")); | 1670 | printf(" %s\n", _("30 days, but more than 14 days, a STATE_WARNING is returned.")); |
2064 | printf(" %s\n", _("A STATE_CRITICAL will be returned when certificate expires in less than 14 days")); | 1671 | printf(" %s\n", |
1672 | _("A STATE_CRITICAL will be returned when certificate expires in less than 14 days")); | ||
2065 | #endif | 1673 | #endif |
2066 | 1674 | ||
2067 | printf("\n %s\n", "CHECK WEBSERVER CONTENT VIA PROXY:"); | 1675 | printf("\n %s\n", "CHECK WEBSERVER CONTENT VIA PROXY:"); |
2068 | printf(" %s\n", _("It is recommended to use an environment proxy like:")); | 1676 | printf(" %s\n", _("It is recommended to use an environment proxy like:")); |
2069 | printf(" %s\n", _("http_proxy=http://192.168.100.35:3128 ./check_curl -H www.monitoring-plugins.org")); | 1677 | printf(" %s\n", |
1678 | _("http_proxy=http://192.168.100.35:3128 ./check_curl -H www.monitoring-plugins.org")); | ||
2070 | printf(" %s\n", _("legacy proxy requests in check_http style still work:")); | 1679 | printf(" %s\n", _("legacy proxy requests in check_http style still work:")); |
2071 | printf(" %s\n", _("check_curl -I 192.168.100.35 -p 3128 -u http://www.monitoring-plugins.org/ -H www.monitoring-plugins.org")); | 1680 | printf(" %s\n", _("check_curl -I 192.168.100.35 -p 3128 -u http://www.monitoring-plugins.org/ " |
1681 | "-H www.monitoring-plugins.org")); | ||
2072 | 1682 | ||
2073 | #ifdef LIBCURL_FEATURE_SSL | 1683 | #ifdef LIBCURL_FEATURE_SSL |
2074 | printf("\n %s\n", "CHECK SSL WEBSERVER CONTENT VIA PROXY USING HTTP 1.1 CONNECT: "); | 1684 | printf("\n %s\n", "CHECK SSL WEBSERVER CONTENT VIA PROXY USING HTTP 1.1 CONNECT: "); |
2075 | printf(" %s\n", _("It is recommended to use an environment proxy like:")); | 1685 | printf(" %s\n", _("It is recommended to use an environment proxy like:")); |
2076 | printf(" %s\n", _("https_proxy=http://192.168.100.35:3128 ./check_curl -H www.verisign.com -S")); | 1686 | printf(" %s\n", |
1687 | _("https_proxy=http://192.168.100.35:3128 ./check_curl -H www.verisign.com -S")); | ||
2077 | printf(" %s\n", _("legacy proxy requests in check_http style still work:")); | 1688 | printf(" %s\n", _("legacy proxy requests in check_http style still work:")); |
2078 | printf(" %s\n", _("check_curl -I 192.168.100.35 -p 3128 -u https://www.verisign.com/ -S -j CONNECT -H www.verisign.com ")); | 1689 | printf(" %s\n", _("check_curl -I 192.168.100.35 -p 3128 -u https://www.verisign.com/ -S -j " |
2079 | printf(" %s\n", _("all these options are needed: -I <proxy> -p <proxy-port> -u <check-url> -S(sl) -j CONNECT -H <webserver>")); | 1690 | "CONNECT -H www.verisign.com ")); |
2080 | printf(" %s\n", _("a STATE_OK will be returned. When the server returns its content but exceeds")); | 1691 | printf(" %s\n", _("all these options are needed: -I <proxy> -p <proxy-port> -u <check-url> " |
2081 | printf(" %s\n", _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | 1692 | "-S(sl) -j CONNECT -H <webserver>")); |
1693 | printf(" %s\n", | ||
1694 | _("a STATE_OK will be returned. When the server returns its content but exceeds")); | ||
1695 | printf(" %s\n", | ||
1696 | _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | ||
2082 | printf(" %s\n", _("a STATE_CRITICAL will be returned.")); | 1697 | printf(" %s\n", _("a STATE_CRITICAL will be returned.")); |
2083 | 1698 | ||
2084 | #endif | 1699 | #endif |
@@ -2089,10 +1704,12 @@ void print_help(void) { | |||
2089 | void print_usage(void) { | 1704 | void print_usage(void) { |
2090 | printf("%s\n", _("Usage:")); | 1705 | printf("%s\n", _("Usage:")); |
2091 | printf(" %s -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]\n", progname); | 1706 | printf(" %s -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]\n", progname); |
2092 | printf(" [-J <client certificate file>] [-K <private key>] [--ca-cert <CA certificate file>] [-D]\n"); | 1707 | printf(" [-J <client certificate file>] [-K <private key>] [--ca-cert <CA certificate " |
1708 | "file>] [-D]\n"); | ||
2093 | printf(" [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L] [-E] [-a auth]\n"); | 1709 | printf(" [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L] [-E] [-a auth]\n"); |
2094 | printf(" [-b proxy_auth] [-f <ok|warning|critical|follow|sticky|stickyport|curl>]\n"); | 1710 | printf(" [-b proxy_auth] [-f <ok|warning|critical|follow|sticky|stickyport|curl>]\n"); |
2095 | printf(" [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n"); | 1711 | printf(" [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive " |
1712 | "regex>]\n"); | ||
2096 | printf(" [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]\n"); | 1713 | printf(" [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]\n"); |
2097 | printf(" [-A string] [-k string] [-S <version>] [--sni] [--haproxy-protocol]\n"); | 1714 | printf(" [-A string] [-k string] [-S <version>] [--sni] [--haproxy-protocol]\n"); |
2098 | printf(" [-T <content-type>] [-j method]\n"); | 1715 | printf(" [-T <content-type>] [-j method]\n"); |
@@ -2109,435 +1726,49 @@ void print_usage(void) { | |||
2109 | 1726 | ||
2110 | void print_curl_version(void) { printf("%s\n", curl_version()); } | 1727 | void print_curl_version(void) { printf("%s\n", curl_version()); } |
2111 | 1728 | ||
2112 | int curlhelp_initwritebuffer(curlhelp_write_curlbuf *buf) { | ||
2113 | buf->bufsize = DEFAULT_BUFFER_SIZE; | ||
2114 | buf->buflen = 0; | ||
2115 | buf->buf = (char *)malloc((size_t)buf->bufsize); | ||
2116 | if (buf->buf == NULL) | ||
2117 | return -1; | ||
2118 | return 0; | ||
2119 | } | ||
2120 | |||
2121 | size_t curlhelp_buffer_write_callback(void *buffer, size_t size, size_t nmemb, void *stream) { | ||
2122 | curlhelp_write_curlbuf *buf = (curlhelp_write_curlbuf *)stream; | ||
2123 | |||
2124 | while (buf->bufsize < buf->buflen + size * nmemb + 1) { | ||
2125 | buf->bufsize = buf->bufsize * 2; | ||
2126 | buf->buf = (char *)realloc(buf->buf, buf->bufsize); | ||
2127 | if (buf->buf == NULL) { | ||
2128 | fprintf(stderr, "malloc failed (%d) %s\n", errno, strerror(errno)); | ||
2129 | return -1; | ||
2130 | } | ||
2131 | } | ||
2132 | |||
2133 | memcpy(buf->buf + buf->buflen, buffer, size * nmemb); | ||
2134 | buf->buflen += size * nmemb; | ||
2135 | buf->buf[buf->buflen] = '\0'; | ||
2136 | |||
2137 | return (int)(size * nmemb); | ||
2138 | } | ||
2139 | |||
2140 | size_t curlhelp_buffer_read_callback(void *buffer, size_t size, size_t nmemb, void *stream) { | ||
2141 | curlhelp_read_curlbuf *buf = (curlhelp_read_curlbuf *)stream; | ||
2142 | |||
2143 | size_t n = min(nmemb * size, buf->buflen - buf->pos); | ||
2144 | |||
2145 | memcpy(buffer, buf->buf + buf->pos, n); | ||
2146 | buf->pos += n; | ||
2147 | |||
2148 | return (int)n; | ||
2149 | } | ||
2150 | |||
2151 | void curlhelp_freewritebuffer(curlhelp_write_curlbuf *buf) { | ||
2152 | free(buf->buf); | ||
2153 | buf->buf = NULL; | ||
2154 | } | ||
2155 | |||
2156 | int curlhelp_initreadbuffer(curlhelp_read_curlbuf *buf, const char *data, size_t datalen) { | ||
2157 | buf->buflen = datalen; | ||
2158 | buf->buf = (char *)malloc((size_t)buf->buflen); | ||
2159 | if (buf->buf == NULL) | ||
2160 | return -1; | ||
2161 | memcpy(buf->buf, data, datalen); | ||
2162 | buf->pos = 0; | ||
2163 | return 0; | ||
2164 | } | ||
2165 | |||
2166 | void curlhelp_freereadbuffer(curlhelp_read_curlbuf *buf) { | ||
2167 | free(buf->buf); | ||
2168 | buf->buf = NULL; | ||
2169 | } | ||
2170 | |||
2171 | /* TODO: where to put this, it's actually part of sstrings2 (logically)? | ||
2172 | */ | ||
2173 | const char *strrstr2(const char *haystack, const char *needle) { | ||
2174 | int counter; | ||
2175 | size_t len; | ||
2176 | const char *prev_pos; | ||
2177 | const char *pos; | ||
2178 | |||
2179 | if (haystack == NULL || needle == NULL) | ||
2180 | return NULL; | ||
2181 | |||
2182 | if (haystack[0] == '\0' || needle[0] == '\0') | ||
2183 | return NULL; | ||
2184 | |||
2185 | counter = 0; | ||
2186 | prev_pos = NULL; | ||
2187 | pos = haystack; | ||
2188 | len = strlen(needle); | ||
2189 | for (;;) { | ||
2190 | pos = strstr(pos, needle); | ||
2191 | if (pos == NULL) { | ||
2192 | if (counter == 0) | ||
2193 | return NULL; | ||
2194 | return prev_pos; | ||
2195 | } | ||
2196 | counter++; | ||
2197 | prev_pos = pos; | ||
2198 | pos += len; | ||
2199 | if (*pos == '\0') | ||
2200 | return prev_pos; | ||
2201 | } | ||
2202 | } | ||
2203 | |||
2204 | int curlhelp_parse_statusline(const char *buf, curlhelp_statusline *status_line) { | ||
2205 | char *first_line_end; | ||
2206 | char *p; | ||
2207 | size_t first_line_len; | ||
2208 | char *pp; | ||
2209 | const char *start; | ||
2210 | char *first_line_buf; | ||
2211 | |||
2212 | /* find last start of a new header */ | ||
2213 | start = strrstr2(buf, "\r\nHTTP/"); | ||
2214 | if (start != NULL) { | ||
2215 | start += 2; | ||
2216 | buf = start; | ||
2217 | } | ||
2218 | |||
2219 | first_line_end = strstr(buf, "\r\n"); | ||
2220 | if (first_line_end == NULL) | ||
2221 | return -1; | ||
2222 | |||
2223 | first_line_len = (size_t)(first_line_end - buf); | ||
2224 | status_line->first_line = (char *)malloc(first_line_len + 1); | ||
2225 | if (status_line->first_line == NULL) | ||
2226 | return -1; | ||
2227 | memcpy(status_line->first_line, buf, first_line_len); | ||
2228 | status_line->first_line[first_line_len] = '\0'; | ||
2229 | first_line_buf = strdup(status_line->first_line); | ||
2230 | |||
2231 | /* protocol and version: "HTTP/x.x" SP or "HTTP/2" SP */ | ||
2232 | |||
2233 | p = strtok(first_line_buf, "/"); | ||
2234 | if (p == NULL) { | ||
2235 | free(first_line_buf); | ||
2236 | return -1; | ||
2237 | } | ||
2238 | if (strcmp(p, "HTTP") != 0) { | ||
2239 | free(first_line_buf); | ||
2240 | return -1; | ||
2241 | } | ||
2242 | |||
2243 | p = strtok(NULL, " "); | ||
2244 | if (p == NULL) { | ||
2245 | free(first_line_buf); | ||
2246 | return -1; | ||
2247 | } | ||
2248 | if (strchr(p, '.') != NULL) { | ||
2249 | |||
2250 | /* HTTP 1.x case */ | ||
2251 | strtok(p, "."); | ||
2252 | status_line->http_major = (int)strtol(p, &pp, 10); | ||
2253 | if (*pp != '\0') { | ||
2254 | free(first_line_buf); | ||
2255 | return -1; | ||
2256 | } | ||
2257 | strtok(NULL, " "); | ||
2258 | status_line->http_minor = (int)strtol(p, &pp, 10); | ||
2259 | if (*pp != '\0') { | ||
2260 | free(first_line_buf); | ||
2261 | return -1; | ||
2262 | } | ||
2263 | p += 4; /* 1.x SP */ | ||
2264 | } else { | ||
2265 | /* HTTP 2 case */ | ||
2266 | status_line->http_major = (int)strtol(p, &pp, 10); | ||
2267 | status_line->http_minor = 0; | ||
2268 | p += 2; /* 2 SP */ | ||
2269 | } | ||
2270 | |||
2271 | /* status code: "404" or "404.1", then SP */ | ||
2272 | |||
2273 | p = strtok(p, " "); | ||
2274 | if (p == NULL) { | ||
2275 | free(first_line_buf); | ||
2276 | return -1; | ||
2277 | } | ||
2278 | if (strchr(p, '.') != NULL) { | ||
2279 | char *ppp; | ||
2280 | ppp = strtok(p, "."); | ||
2281 | status_line->http_code = (int)strtol(ppp, &pp, 10); | ||
2282 | if (*pp != '\0') { | ||
2283 | free(first_line_buf); | ||
2284 | return -1; | ||
2285 | } | ||
2286 | ppp = strtok(NULL, ""); | ||
2287 | status_line->http_subcode = (int)strtol(ppp, &pp, 10); | ||
2288 | if (*pp != '\0') { | ||
2289 | free(first_line_buf); | ||
2290 | return -1; | ||
2291 | } | ||
2292 | p += 6; /* 400.1 SP */ | ||
2293 | } else { | ||
2294 | status_line->http_code = (int)strtol(p, &pp, 10); | ||
2295 | status_line->http_subcode = -1; | ||
2296 | if (*pp != '\0') { | ||
2297 | free(first_line_buf); | ||
2298 | return -1; | ||
2299 | } | ||
2300 | p += 4; /* 400 SP */ | ||
2301 | } | ||
2302 | |||
2303 | /* Human readable message: "Not Found" CRLF */ | ||
2304 | |||
2305 | p = strtok(p, ""); | ||
2306 | if (p == NULL) { | ||
2307 | status_line->msg = ""; | ||
2308 | return 0; | ||
2309 | } | ||
2310 | status_line->msg = status_line->first_line + (p - first_line_buf); | ||
2311 | free(first_line_buf); | ||
2312 | |||
2313 | return 0; | ||
2314 | } | ||
2315 | |||
2316 | void curlhelp_free_statusline(curlhelp_statusline *status_line) { free(status_line->first_line); } | ||
2317 | |||
2318 | char *get_header_value(const struct phr_header *headers, const size_t nof_headers, const char *header) { | ||
2319 | for (size_t i = 0; i < nof_headers; i++) { | ||
2320 | if (headers[i].name != NULL && strncasecmp(header, headers[i].name, max(headers[i].name_len, 4)) == 0) { | ||
2321 | return strndup(headers[i].value, headers[i].value_len); | ||
2322 | } | ||
2323 | } | ||
2324 | return NULL; | ||
2325 | } | ||
2326 | |||
2327 | int check_document_dates(const curlhelp_write_curlbuf *header_buf, char (*msg)[DEFAULT_BUFFER_SIZE]) { | ||
2328 | char *server_date = NULL; | ||
2329 | char *document_date = NULL; | ||
2330 | int date_result = STATE_OK; | ||
2331 | curlhelp_statusline status_line; | ||
2332 | struct phr_header headers[255]; | ||
2333 | size_t nof_headers = 255; | ||
2334 | size_t msglen; | ||
2335 | |||
2336 | int res = phr_parse_response(header_buf->buf, header_buf->buflen, &status_line.http_major, &status_line.http_minor, | ||
2337 | &status_line.http_code, &status_line.msg, &msglen, headers, &nof_headers, 0); | ||
2338 | |||
2339 | if (res == -1) { | ||
2340 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse Response\n")); | ||
2341 | } | ||
2342 | |||
2343 | server_date = get_header_value(headers, nof_headers, "date"); | ||
2344 | document_date = get_header_value(headers, nof_headers, "last-modified"); | ||
2345 | |||
2346 | if (!server_date || !*server_date) { | ||
2347 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
2348 | |||
2349 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sServer date unknown, "), *msg); | ||
2350 | strcpy(*msg, tmp); | ||
2351 | |||
2352 | date_result = max_state_alt(STATE_UNKNOWN, date_result); | ||
2353 | |||
2354 | } else if (!document_date || !*document_date) { | ||
2355 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
2356 | |||
2357 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sDocument modification date unknown, "), *msg); | ||
2358 | strcpy(*msg, tmp); | ||
2359 | |||
2360 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
2361 | |||
2362 | } else { | ||
2363 | time_t srv_data = curl_getdate(server_date, NULL); | ||
2364 | time_t doc_data = curl_getdate(document_date, NULL); | ||
2365 | if (verbose >= 2) | ||
2366 | printf("* server date: '%s' (%d), doc_date: '%s' (%d)\n", server_date, (int)srv_data, document_date, (int)doc_data); | ||
2367 | if (srv_data <= 0) { | ||
2368 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
2369 | |||
2370 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sServer date \"%100s\" unparsable, "), *msg, server_date); | ||
2371 | strcpy(*msg, tmp); | ||
2372 | |||
2373 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
2374 | } else if (doc_data <= 0) { | ||
2375 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
2376 | |||
2377 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sDocument date \"%100s\" unparsable, "), *msg, document_date); | ||
2378 | strcpy(*msg, tmp); | ||
2379 | |||
2380 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
2381 | } else if (doc_data > srv_data + 30) { | ||
2382 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
2383 | |||
2384 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sDocument is %d seconds in the future, "), *msg, (int)doc_data - (int)srv_data); | ||
2385 | strcpy(*msg, tmp); | ||
2386 | |||
2387 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
2388 | } else if (doc_data < srv_data - maximum_age) { | ||
2389 | int n = (srv_data - doc_data); | ||
2390 | if (n > (60 * 60 * 24 * 2)) { | ||
2391 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
2392 | |||
2393 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sLast modified %.1f days ago, "), *msg, ((float)n) / (60 * 60 * 24)); | ||
2394 | strcpy(*msg, tmp); | ||
2395 | |||
2396 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
2397 | } else { | ||
2398 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
2399 | |||
2400 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sLast modified %d:%02d:%02d ago, "), *msg, n / (60 * 60), (n / 60) % 60, n % 60); | ||
2401 | strcpy(*msg, tmp); | ||
2402 | |||
2403 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
2404 | } | ||
2405 | } | ||
2406 | } | ||
2407 | |||
2408 | if (server_date) | ||
2409 | free(server_date); | ||
2410 | if (document_date) | ||
2411 | free(document_date); | ||
2412 | |||
2413 | return date_result; | ||
2414 | } | ||
2415 | |||
2416 | int get_content_length(const curlhelp_write_curlbuf *header_buf, const curlhelp_write_curlbuf *body_buf) { | ||
2417 | size_t content_length = 0; | ||
2418 | struct phr_header headers[255]; | ||
2419 | size_t nof_headers = 255; | ||
2420 | size_t msglen; | ||
2421 | char *content_length_s = NULL; | ||
2422 | curlhelp_statusline status_line; | ||
2423 | |||
2424 | int res = phr_parse_response(header_buf->buf, header_buf->buflen, &status_line.http_major, &status_line.http_minor, | ||
2425 | &status_line.http_code, &status_line.msg, &msglen, headers, &nof_headers, 0); | ||
2426 | |||
2427 | if (res == -1) { | ||
2428 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse Response\n")); | ||
2429 | } | ||
2430 | |||
2431 | content_length_s = get_header_value(headers, nof_headers, "content-length"); | ||
2432 | if (!content_length_s) { | ||
2433 | return header_buf->buflen + body_buf->buflen; | ||
2434 | } | ||
2435 | content_length_s += strspn(content_length_s, " \t"); | ||
2436 | content_length = atoi(content_length_s); | ||
2437 | if (content_length != body_buf->buflen) { | ||
2438 | /* TODO: should we warn if the actual and the reported body length don't match? */ | ||
2439 | } | ||
2440 | |||
2441 | if (content_length_s) | ||
2442 | free(content_length_s); | ||
2443 | |||
2444 | return header_buf->buflen + body_buf->buflen; | ||
2445 | } | ||
2446 | |||
2447 | /* TODO: is there a better way in libcurl to check for the SSL library? */ | ||
2448 | curlhelp_ssl_library curlhelp_get_ssl_library(void) { | ||
2449 | curl_version_info_data *version_data; | ||
2450 | char *ssl_version; | ||
2451 | char *library; | ||
2452 | curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
2453 | |||
2454 | version_data = curl_version_info(CURLVERSION_NOW); | ||
2455 | if (version_data == NULL) | ||
2456 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
2457 | |||
2458 | ssl_version = strdup(version_data->ssl_version); | ||
2459 | if (ssl_version == NULL) | ||
2460 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
2461 | |||
2462 | library = strtok(ssl_version, "/"); | ||
2463 | if (library == NULL) | ||
2464 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
2465 | |||
2466 | if (strcmp(library, "OpenSSL") == 0) | ||
2467 | ssl_library = CURLHELP_SSL_LIBRARY_OPENSSL; | ||
2468 | else if (strcmp(library, "LibreSSL") == 0) | ||
2469 | ssl_library = CURLHELP_SSL_LIBRARY_LIBRESSL; | ||
2470 | else if (strcmp(library, "GnuTLS") == 0) | ||
2471 | ssl_library = CURLHELP_SSL_LIBRARY_GNUTLS; | ||
2472 | else if (strcmp(library, "NSS") == 0) | ||
2473 | ssl_library = CURLHELP_SSL_LIBRARY_NSS; | ||
2474 | |||
2475 | if (verbose >= 2) | ||
2476 | printf("* SSL library string is : %s %s (%d)\n", version_data->ssl_version, library, ssl_library); | ||
2477 | |||
2478 | free(ssl_version); | ||
2479 | |||
2480 | return ssl_library; | ||
2481 | } | ||
2482 | |||
2483 | const char *curlhelp_get_ssl_library_string(curlhelp_ssl_library ssl_library) { | ||
2484 | switch (ssl_library) { | ||
2485 | case CURLHELP_SSL_LIBRARY_OPENSSL: | ||
2486 | return "OpenSSL"; | ||
2487 | case CURLHELP_SSL_LIBRARY_LIBRESSL: | ||
2488 | return "LibreSSL"; | ||
2489 | case CURLHELP_SSL_LIBRARY_GNUTLS: | ||
2490 | return "GnuTLS"; | ||
2491 | case CURLHELP_SSL_LIBRARY_NSS: | ||
2492 | return "NSS"; | ||
2493 | case CURLHELP_SSL_LIBRARY_UNKNOWN: | ||
2494 | default: | ||
2495 | return "unknown"; | ||
2496 | } | ||
2497 | } | ||
2498 | |||
2499 | #ifdef LIBCURL_FEATURE_SSL | 1729 | #ifdef LIBCURL_FEATURE_SSL |
2500 | # ifndef USE_OPENSSL | 1730 | # ifndef USE_OPENSSL |
2501 | time_t parse_cert_date(const char *s) { | 1731 | time_t parse_cert_date(const char *s) { |
2502 | struct tm tm; | 1732 | if (!s) { |
2503 | time_t date; | ||
2504 | char *res; | ||
2505 | |||
2506 | if (!s) | ||
2507 | return -1; | 1733 | return -1; |
1734 | } | ||
2508 | 1735 | ||
2509 | /* Jan 17 14:25:12 2020 GMT */ | 1736 | /* Jan 17 14:25:12 2020 GMT */ |
2510 | res = strptime(s, "%Y-%m-%d %H:%M:%S GMT", &tm); | 1737 | struct tm tm; |
1738 | char *res = strptime(s, "%Y-%m-%d %H:%M:%S GMT", &tm); | ||
2511 | /* Sep 11 12:00:00 2020 GMT */ | 1739 | /* Sep 11 12:00:00 2020 GMT */ |
2512 | if (res == NULL) | 1740 | if (res == NULL) { |
2513 | strptime(s, "%Y %m %d %H:%M:%S GMT", &tm); | 1741 | strptime(s, "%Y %m %d %H:%M:%S GMT", &tm); |
2514 | date = mktime(&tm); | 1742 | } |
1743 | time_t date = mktime(&tm); | ||
2515 | 1744 | ||
2516 | return date; | 1745 | return date; |
2517 | } | 1746 | } |
1747 | # endif /* USE_OPENSSL */ | ||
1748 | #endif /* LIBCURL_FEATURE_SSL */ | ||
2518 | 1749 | ||
1750 | #ifdef LIBCURL_FEATURE_SSL | ||
1751 | # ifndef USE_OPENSSL | ||
2519 | /* TODO: this needs cleanup in the sslutils.c, maybe we the #else case to | 1752 | /* TODO: this needs cleanup in the sslutils.c, maybe we the #else case to |
2520 | * OpenSSL could be this function | 1753 | * OpenSSL could be this function |
2521 | */ | 1754 | */ |
2522 | int net_noopenssl_check_certificate(cert_ptr_union *cert_ptr, int days_till_exp_warn, int days_till_exp_crit) { | 1755 | int net_noopenssl_check_certificate(cert_ptr_union *cert_ptr, int days_till_exp_warn, |
2523 | int i; | 1756 | int days_till_exp_crit) { |
2524 | struct curl_slist *slist; | ||
2525 | int cname_found = 0; | ||
2526 | char *start_date_str = NULL; | ||
2527 | char *end_date_str = NULL; | ||
2528 | time_t start_date; | ||
2529 | time_t end_date; | ||
2530 | char *tz; | ||
2531 | float time_left; | ||
2532 | int days_left; | ||
2533 | int time_remaining; | ||
2534 | char timestamp[50] = ""; | ||
2535 | int status = STATE_UNKNOWN; | ||
2536 | 1757 | ||
2537 | if (verbose >= 2) | 1758 | if (verbose >= 2) { |
2538 | printf("**** REQUEST CERTIFICATES ****\n"); | 1759 | printf("**** REQUEST CERTIFICATES ****\n"); |
1760 | } | ||
1761 | |||
1762 | char *start_date_str = NULL; | ||
1763 | char *end_date_str = NULL; | ||
1764 | bool have_first_cert = false; | ||
1765 | bool cname_found = false; | ||
1766 | for (int i = 0; i < cert_ptr->to_certinfo->num_of_certs; i++) { | ||
1767 | if (have_first_cert) { | ||
1768 | break; | ||
1769 | } | ||
2539 | 1770 | ||
2540 | for (i = 0; i < cert_ptr->to_certinfo->num_of_certs; i++) { | 1771 | struct curl_slist *slist; |
2541 | for (slist = cert_ptr->to_certinfo->certinfo[i]; slist; slist = slist->next) { | 1772 | for (slist = cert_ptr->to_certinfo->certinfo[i]; slist; slist = slist->next) { |
2542 | /* find first common name in subject, | 1773 | /* find first common name in subject, |
2543 | * TODO: check alternative subjects for | 1774 | * TODO: check alternative subjects for |
@@ -2553,7 +1784,7 @@ int net_noopenssl_check_certificate(cert_ptr_union *cert_ptr, int days_till_exp_ | |||
2553 | } | 1784 | } |
2554 | if (p != NULL) { | 1785 | if (p != NULL) { |
2555 | if (strncmp(host_name, p + d, strlen(host_name)) == 0) { | 1786 | if (strncmp(host_name, p + d, strlen(host_name)) == 0) { |
2556 | cname_found = 1; | 1787 | cname_found = true; |
2557 | } | 1788 | } |
2558 | } | 1789 | } |
2559 | } else if (strncasecmp(slist->data, "Start Date:", 11) == 0) { | 1790 | } else if (strncasecmp(slist->data, "Start Date:", 11) == 0) { |
@@ -2561,78 +1792,93 @@ int net_noopenssl_check_certificate(cert_ptr_union *cert_ptr, int days_till_exp_ | |||
2561 | } else if (strncasecmp(slist->data, "Expire Date:", 12) == 0) { | 1792 | } else if (strncasecmp(slist->data, "Expire Date:", 12) == 0) { |
2562 | end_date_str = &slist->data[12]; | 1793 | end_date_str = &slist->data[12]; |
2563 | } else if (strncasecmp(slist->data, "Cert:", 5) == 0) { | 1794 | } else if (strncasecmp(slist->data, "Cert:", 5) == 0) { |
2564 | goto HAVE_FIRST_CERT; | 1795 | have_first_cert = true; |
1796 | break; | ||
2565 | } | 1797 | } |
2566 | if (verbose >= 2) | 1798 | if (verbose >= 2) { |
2567 | printf("%d ** %s\n", i, slist->data); | 1799 | printf("%d ** %s\n", i, slist->data); |
1800 | } | ||
2568 | } | 1801 | } |
2569 | } | 1802 | } |
2570 | HAVE_FIRST_CERT: | ||
2571 | 1803 | ||
2572 | if (verbose >= 2) | 1804 | if (verbose >= 2) { |
2573 | printf("**** REQUEST CERTIFICATES ****\n"); | 1805 | printf("**** REQUEST CERTIFICATES ****\n"); |
1806 | } | ||
2574 | 1807 | ||
2575 | if (!cname_found) { | 1808 | if (!cname_found) { |
2576 | printf("%s\n", _("CRITICAL - Cannot retrieve certificate subject.")); | 1809 | printf("%s\n", _("CRITICAL - Cannot retrieve certificate subject.")); |
2577 | return STATE_CRITICAL; | 1810 | return STATE_CRITICAL; |
2578 | } | 1811 | } |
2579 | 1812 | ||
2580 | start_date = parse_cert_date(start_date_str); | 1813 | time_t start_date = parse_cert_date(start_date_str); |
2581 | if (start_date <= 0) { | 1814 | if (start_date <= 0) { |
2582 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("WARNING - Unparsable 'Start Date' in certificate: '%s'"), start_date_str); | 1815 | snprintf(msg, DEFAULT_BUFFER_SIZE, |
1816 | _("WARNING - Unparsable 'Start Date' in certificate: '%s'"), start_date_str); | ||
2583 | puts(msg); | 1817 | puts(msg); |
2584 | return STATE_WARNING; | 1818 | return STATE_WARNING; |
2585 | } | 1819 | } |
2586 | 1820 | ||
2587 | end_date = parse_cert_date(end_date_str); | 1821 | time_t end_date = parse_cert_date(end_date_str); |
2588 | if (end_date <= 0) { | 1822 | if (end_date <= 0) { |
2589 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("WARNING - Unparsable 'Expire Date' in certificate: '%s'"), start_date_str); | 1823 | snprintf(msg, DEFAULT_BUFFER_SIZE, |
1824 | _("WARNING - Unparsable 'Expire Date' in certificate: '%s'"), start_date_str); | ||
2590 | puts(msg); | 1825 | puts(msg); |
2591 | return STATE_WARNING; | 1826 | return STATE_WARNING; |
2592 | } | 1827 | } |
2593 | 1828 | ||
2594 | time_left = difftime(end_date, time(NULL)); | 1829 | float time_left = difftime(end_date, time(NULL)); |
2595 | days_left = time_left / 86400; | 1830 | int days_left = time_left / 86400; |
2596 | tz = getenv("TZ"); | 1831 | char *tz = getenv("TZ"); |
2597 | setenv("TZ", "GMT", 1); | 1832 | setenv("TZ", "GMT", 1); |
2598 | tzset(); | 1833 | tzset(); |
1834 | |||
1835 | char timestamp[50] = ""; | ||
2599 | strftime(timestamp, 50, "%c %z", localtime(&end_date)); | 1836 | strftime(timestamp, 50, "%c %z", localtime(&end_date)); |
2600 | if (tz) | 1837 | if (tz) { |
2601 | setenv("TZ", tz, 1); | 1838 | setenv("TZ", tz, 1); |
2602 | else | 1839 | } else { |
2603 | unsetenv("TZ"); | 1840 | unsetenv("TZ"); |
1841 | } | ||
2604 | tzset(); | 1842 | tzset(); |
2605 | 1843 | ||
1844 | mp_state_enum status = STATE_UNKNOWN; | ||
1845 | int time_remaining; | ||
2606 | if (days_left > 0 && days_left <= days_till_exp_warn) { | 1846 | if (days_left > 0 && days_left <= days_till_exp_warn) { |
2607 | printf(_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", | 1847 | printf(_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), |
2608 | host_name, days_left, timestamp); | 1848 | (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, days_left, |
2609 | if (days_left > days_till_exp_crit) | 1849 | timestamp); |
1850 | if (days_left > days_till_exp_crit) { | ||
2610 | status = STATE_WARNING; | 1851 | status = STATE_WARNING; |
2611 | else | 1852 | } else { |
2612 | status = STATE_CRITICAL; | 1853 | status = STATE_CRITICAL; |
1854 | } | ||
2613 | } else if (days_left == 0 && time_left > 0) { | 1855 | } else if (days_left == 0 && time_left > 0) { |
2614 | if (time_left >= 3600) | 1856 | if (time_left >= 3600) { |
2615 | time_remaining = (int)time_left / 3600; | 1857 | time_remaining = (int)time_left / 3600; |
2616 | else | 1858 | } else { |
2617 | time_remaining = (int)time_left / 60; | 1859 | time_remaining = (int)time_left / 60; |
1860 | } | ||
2618 | 1861 | ||
2619 | printf(_("%s - Certificate '%s' expires in %u %s (%s)\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, | 1862 | printf(_("%s - Certificate '%s' expires in %u %s (%s)\n"), |
2620 | time_remaining, time_left >= 3600 ? "hours" : "minutes", timestamp); | 1863 | (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, time_remaining, |
1864 | time_left >= 3600 ? "hours" : "minutes", timestamp); | ||
2621 | 1865 | ||
2622 | if (days_left > days_till_exp_crit) | 1866 | if (days_left > days_till_exp_crit) { |
2623 | status = STATE_WARNING; | 1867 | status = STATE_WARNING; |
2624 | else | 1868 | } else { |
2625 | status = STATE_CRITICAL; | 1869 | status = STATE_CRITICAL; |
1870 | } | ||
2626 | } else if (time_left < 0) { | 1871 | } else if (time_left < 0) { |
2627 | printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), host_name, timestamp); | 1872 | printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), host_name, timestamp); |
2628 | status = STATE_CRITICAL; | 1873 | status = STATE_CRITICAL; |
2629 | } else if (days_left == 0) { | 1874 | } else if (days_left == 0) { |
2630 | printf(_("%s - Certificate '%s' just expired (%s).\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, | 1875 | printf(_("%s - Certificate '%s' just expired (%s).\n"), |
2631 | timestamp); | 1876 | (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, timestamp); |
2632 | if (days_left > days_till_exp_crit) | 1877 | if (days_left > days_till_exp_crit) { |
2633 | status = STATE_WARNING; | 1878 | status = STATE_WARNING; |
2634 | else | 1879 | } else { |
2635 | status = STATE_CRITICAL; | 1880 | status = STATE_CRITICAL; |
1881 | } | ||
2636 | } else { | 1882 | } else { |
2637 | printf(_("OK - Certificate '%s' will expire on %s.\n"), host_name, timestamp); | 1883 | printf(_("OK - Certificate '%s' will expire on %s.\n"), host_name, timestamp); |
2638 | status = STATE_OK; | 1884 | status = STATE_OK; |
diff --git a/plugins/check_curl.d/check_curl_helpers.c b/plugins/check_curl.d/check_curl_helpers.c new file mode 100644 index 00000000..c3c2ba55 --- /dev/null +++ b/plugins/check_curl.d/check_curl_helpers.c | |||
@@ -0,0 +1,1267 @@ | |||
1 | #include "./check_curl_helpers.h" | ||
2 | #include <stdbool.h> | ||
3 | #include <arpa/inet.h> | ||
4 | #include <netinet/in.h> | ||
5 | #include <netdb.h> | ||
6 | #include <stdlib.h> | ||
7 | #include "../utils.h" | ||
8 | #include "check_curl.d/config.h" | ||
9 | #include "output.h" | ||
10 | #include "perfdata.h" | ||
11 | #include "states.h" | ||
12 | |||
13 | extern int verbose; | ||
14 | char errbuf[MAX_INPUT_BUFFER]; | ||
15 | bool is_openssl_callback = false; | ||
16 | bool add_sslctx_verify_fun = false; | ||
17 | |||
18 | check_curl_configure_curl_wrapper | ||
19 | check_curl_configure_curl(const check_curl_static_curl_config config, | ||
20 | check_curl_working_state working_state, bool check_cert, | ||
21 | bool on_redirect_dependent, int follow_method, int max_depth) { | ||
22 | check_curl_configure_curl_wrapper result = { | ||
23 | .errorcode = OK, | ||
24 | .curl_state = | ||
25 | { | ||
26 | .curl_global_initialized = false, | ||
27 | .curl_easy_initialized = false, | ||
28 | .curl = NULL, | ||
29 | |||
30 | .body_buf_initialized = false, | ||
31 | .body_buf = NULL, | ||
32 | .header_buf_initialized = false, | ||
33 | .header_buf = NULL, | ||
34 | .status_line_initialized = false, | ||
35 | .status_line = NULL, | ||
36 | .put_buf_initialized = false, | ||
37 | .put_buf = NULL, | ||
38 | |||
39 | .header_list = NULL, | ||
40 | .host = NULL, | ||
41 | }, | ||
42 | }; | ||
43 | |||
44 | if ((result.curl_state.status_line = calloc(1, sizeof(curlhelp_statusline))) == NULL) { | ||
45 | die(STATE_UNKNOWN, "HTTP UNKNOWN - allocation of statusline failed\n"); | ||
46 | } | ||
47 | |||
48 | if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) { | ||
49 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); | ||
50 | } | ||
51 | result.curl_state.curl_global_initialized = true; | ||
52 | |||
53 | if ((result.curl_state.curl = curl_easy_init()) == NULL) { | ||
54 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_easy_init failed\n"); | ||
55 | } | ||
56 | result.curl_state.curl_easy_initialized = true; | ||
57 | |||
58 | if (verbose >= 1) { | ||
59 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_VERBOSE, 1), | ||
60 | "CURLOPT_VERBOSE"); | ||
61 | } | ||
62 | |||
63 | /* print everything on stdout like check_http would do */ | ||
64 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_STDERR, stdout), | ||
65 | "CURLOPT_STDERR"); | ||
66 | |||
67 | if (config.automatic_decompression) { | ||
68 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 6) | ||
69 | handle_curl_option_return_code( | ||
70 | curl_easy_setopt(result.curl_state.curl, CURLOPT_ACCEPT_ENCODING, ""), | ||
71 | "CURLOPT_ACCEPT_ENCODING"); | ||
72 | #else | ||
73 | handle_curl_option_return_code( | ||
74 | curl_easy_setopt(result.curl_state.curl, CURLOPT_ENCODING, ""), "CURLOPT_ENCODING"); | ||
75 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 6) */ | ||
76 | } | ||
77 | |||
78 | /* initialize buffer for body of the answer */ | ||
79 | if (curlhelp_initwritebuffer(&result.curl_state.body_buf) < 0) { | ||
80 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for body\n"); | ||
81 | } | ||
82 | result.curl_state.body_buf_initialized = true; | ||
83 | |||
84 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_WRITEFUNCTION, | ||
85 | curlhelp_buffer_write_callback), | ||
86 | "CURLOPT_WRITEFUNCTION"); | ||
87 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_WRITEDATA, | ||
88 | (void *)result.curl_state.body_buf), | ||
89 | "CURLOPT_WRITEDATA"); | ||
90 | |||
91 | /* initialize buffer for header of the answer */ | ||
92 | if (curlhelp_initwritebuffer(&result.curl_state.header_buf) < 0) { | ||
93 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for header\n"); | ||
94 | } | ||
95 | result.curl_state.header_buf_initialized = true; | ||
96 | |||
97 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_HEADERFUNCTION, | ||
98 | curlhelp_buffer_write_callback), | ||
99 | "CURLOPT_HEADERFUNCTION"); | ||
100 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_WRITEHEADER, | ||
101 | (void *)result.curl_state.header_buf), | ||
102 | "CURLOPT_WRITEHEADER"); | ||
103 | |||
104 | /* set the error buffer */ | ||
105 | handle_curl_option_return_code( | ||
106 | curl_easy_setopt(result.curl_state.curl, CURLOPT_ERRORBUFFER, errbuf), | ||
107 | "CURLOPT_ERRORBUFFER"); | ||
108 | |||
109 | /* set timeouts */ | ||
110 | handle_curl_option_return_code( | ||
111 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CONNECTTIMEOUT, config.socket_timeout), | ||
112 | "CURLOPT_CONNECTTIMEOUT"); | ||
113 | |||
114 | handle_curl_option_return_code( | ||
115 | curl_easy_setopt(result.curl_state.curl, CURLOPT_TIMEOUT, config.socket_timeout), | ||
116 | "CURLOPT_TIMEOUT"); | ||
117 | |||
118 | /* enable haproxy protocol */ | ||
119 | if (config.haproxy_protocol) { | ||
120 | handle_curl_option_return_code( | ||
121 | curl_easy_setopt(result.curl_state.curl, CURLOPT_HAPROXYPROTOCOL, 1L), | ||
122 | "CURLOPT_HAPROXYPROTOCOL"); | ||
123 | } | ||
124 | |||
125 | // fill dns resolve cache to make curl connect to the given server_address instead of the | ||
126 | // host_name, only required for ssl, because we use the host_name later on to make SNI happy | ||
127 | char dnscache[DEFAULT_BUFFER_SIZE]; | ||
128 | char addrstr[DEFAULT_BUFFER_SIZE / 2]; | ||
129 | if (working_state.use_ssl && working_state.host_name != NULL) { | ||
130 | int res; | ||
131 | if ((res = lookup_host(working_state.server_address, addrstr, DEFAULT_BUFFER_SIZE / 2, | ||
132 | config.sin_family)) != 0) { | ||
133 | die(STATE_CRITICAL, | ||
134 | _("Unable to lookup IP address for '%s': getaddrinfo returned %d - %s"), | ||
135 | working_state.server_address, res, gai_strerror(res)); | ||
136 | } | ||
137 | |||
138 | snprintf(dnscache, DEFAULT_BUFFER_SIZE, "%s:%d:%s", working_state.host_name, | ||
139 | working_state.serverPort, addrstr); | ||
140 | result.curl_state.host = curl_slist_append(NULL, dnscache); | ||
141 | curl_easy_setopt(result.curl_state.curl, CURLOPT_RESOLVE, result.curl_state.host); | ||
142 | |||
143 | if (verbose >= 1) { | ||
144 | printf("* curl CURLOPT_RESOLVE: %s\n", dnscache); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | // If server_address is an IPv6 address it must be surround by square brackets | ||
149 | struct in6_addr tmp_in_addr; | ||
150 | if (inet_pton(AF_INET6, working_state.server_address, &tmp_in_addr) == 1) { | ||
151 | char *new_server_address = calloc(strlen(working_state.server_address) + 3, sizeof(char)); | ||
152 | if (new_server_address == NULL) { | ||
153 | die(STATE_UNKNOWN, "HTTP UNKNOWN - Unable to allocate memory\n"); | ||
154 | } | ||
155 | snprintf(new_server_address, strlen(working_state.server_address) + 3, "[%s]", | ||
156 | working_state.server_address); | ||
157 | working_state.server_address = new_server_address; | ||
158 | } | ||
159 | |||
160 | /* compose URL: use the address we want to connect to, set Host: header later */ | ||
161 | char *url = fmt_url(working_state); | ||
162 | |||
163 | if (verbose >= 1) { | ||
164 | printf("* curl CURLOPT_URL: %s\n", url); | ||
165 | } | ||
166 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_URL, url), | ||
167 | "CURLOPT_URL"); | ||
168 | |||
169 | free(url); | ||
170 | |||
171 | /* extract proxy information for legacy proxy https requests */ | ||
172 | if (!strcmp(working_state.http_method, "CONNECT") || | ||
173 | strstr(working_state.server_url, "http") == working_state.server_url) { | ||
174 | handle_curl_option_return_code( | ||
175 | curl_easy_setopt(result.curl_state.curl, CURLOPT_PROXY, working_state.server_address), | ||
176 | "CURLOPT_PROXY"); | ||
177 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_PROXYPORT, | ||
178 | (long)working_state.serverPort), | ||
179 | "CURLOPT_PROXYPORT"); | ||
180 | if (verbose >= 2) { | ||
181 | printf("* curl CURLOPT_PROXY: %s:%d\n", working_state.server_address, | ||
182 | working_state.serverPort); | ||
183 | } | ||
184 | working_state.http_method = "GET"; | ||
185 | handle_curl_option_return_code( | ||
186 | curl_easy_setopt(result.curl_state.curl, CURLOPT_URL, working_state.server_url), | ||
187 | "CURLOPT_URL"); | ||
188 | } | ||
189 | |||
190 | /* disable body for HEAD request */ | ||
191 | if (working_state.http_method && !strcmp(working_state.http_method, "HEAD")) { | ||
192 | working_state.no_body = true; | ||
193 | } | ||
194 | |||
195 | /* set HTTP protocol version */ | ||
196 | handle_curl_option_return_code( | ||
197 | curl_easy_setopt(result.curl_state.curl, CURLOPT_HTTP_VERSION, config.curl_http_version), | ||
198 | "CURLOPT_HTTP_VERSION"); | ||
199 | |||
200 | /* set HTTP method */ | ||
201 | if (working_state.http_method) { | ||
202 | if (!strcmp(working_state.http_method, "POST")) { | ||
203 | handle_curl_option_return_code( | ||
204 | curl_easy_setopt(result.curl_state.curl, CURLOPT_POST, 1), "CURLOPT_POST"); | ||
205 | } else if (!strcmp(working_state.http_method, "PUT")) { | ||
206 | handle_curl_option_return_code( | ||
207 | curl_easy_setopt(result.curl_state.curl, CURLOPT_UPLOAD, 1), "CURLOPT_UPLOAD"); | ||
208 | } else { | ||
209 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, | ||
210 | CURLOPT_CUSTOMREQUEST, | ||
211 | working_state.http_method), | ||
212 | "CURLOPT_CUSTOMREQUEST"); | ||
213 | } | ||
214 | } | ||
215 | |||
216 | char *force_host_header = NULL; | ||
217 | /* check if Host header is explicitly set in options */ | ||
218 | if (config.http_opt_headers_count) { | ||
219 | for (size_t i = 0; i < config.http_opt_headers_count; i++) { | ||
220 | if (strncmp(config.http_opt_headers[i], "Host:", 5) == 0) { | ||
221 | force_host_header = config.http_opt_headers[i]; | ||
222 | } | ||
223 | } | ||
224 | } | ||
225 | |||
226 | /* set hostname (virtual hosts), not needed if CURLOPT_CONNECT_TO is used, but left in | ||
227 | * anyway */ | ||
228 | char http_header[DEFAULT_BUFFER_SIZE]; | ||
229 | if (working_state.host_name != NULL && force_host_header == NULL) { | ||
230 | if ((working_state.virtualPort != HTTP_PORT && !working_state.use_ssl) || | ||
231 | (working_state.virtualPort != HTTPS_PORT && working_state.use_ssl)) { | ||
232 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s:%d", working_state.host_name, | ||
233 | working_state.virtualPort); | ||
234 | } else { | ||
235 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s", working_state.host_name); | ||
236 | } | ||
237 | result.curl_state.header_list = | ||
238 | curl_slist_append(result.curl_state.header_list, http_header); | ||
239 | } | ||
240 | |||
241 | /* always close connection, be nice to servers */ | ||
242 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Connection: close"); | ||
243 | result.curl_state.header_list = curl_slist_append(result.curl_state.header_list, http_header); | ||
244 | |||
245 | /* attach additional headers supplied by the user */ | ||
246 | /* optionally send any other header tag */ | ||
247 | if (config.http_opt_headers_count) { | ||
248 | for (size_t i = 0; i < config.http_opt_headers_count; i++) { | ||
249 | result.curl_state.header_list = | ||
250 | curl_slist_append(result.curl_state.header_list, config.http_opt_headers[i]); | ||
251 | } | ||
252 | } | ||
253 | |||
254 | /* set HTTP headers */ | ||
255 | handle_curl_option_return_code( | ||
256 | curl_easy_setopt(result.curl_state.curl, CURLOPT_HTTPHEADER, result.curl_state.header_list), | ||
257 | "CURLOPT_HTTPHEADER"); | ||
258 | |||
259 | #ifdef LIBCURL_FEATURE_SSL | ||
260 | /* set SSL version, warn about insecure or unsupported versions */ | ||
261 | if (working_state.use_ssl) { | ||
262 | handle_curl_option_return_code( | ||
263 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSLVERSION, config.ssl_version), | ||
264 | "CURLOPT_SSLVERSION"); | ||
265 | } | ||
266 | |||
267 | /* client certificate and key to present to server (SSL) */ | ||
268 | if (config.client_cert) { | ||
269 | handle_curl_option_return_code( | ||
270 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSLCERT, config.client_cert), | ||
271 | "CURLOPT_SSLCERT"); | ||
272 | } | ||
273 | |||
274 | if (config.client_privkey) { | ||
275 | handle_curl_option_return_code( | ||
276 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSLKEY, config.client_privkey), | ||
277 | "CURLOPT_SSLKEY"); | ||
278 | } | ||
279 | |||
280 | if (config.ca_cert) { | ||
281 | handle_curl_option_return_code( | ||
282 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CAINFO, config.ca_cert), | ||
283 | "CURLOPT_CAINFO"); | ||
284 | } | ||
285 | |||
286 | if (config.ca_cert || config.verify_peer_and_host) { | ||
287 | /* per default if we have a CA verify both the peer and the | ||
288 | * hostname in the certificate, can be switched off later */ | ||
289 | handle_curl_option_return_code( | ||
290 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_VERIFYPEER, 1), | ||
291 | "CURLOPT_SSL_VERIFYPEER"); | ||
292 | handle_curl_option_return_code( | ||
293 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_VERIFYHOST, 2), | ||
294 | "CURLOPT_SSL_VERIFYHOST"); | ||
295 | } else { | ||
296 | /* backward-compatible behaviour, be tolerant in checks | ||
297 | * TODO: depending on more options have aspects we want | ||
298 | * to be less tolerant about ssl verfications | ||
299 | */ | ||
300 | handle_curl_option_return_code( | ||
301 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_VERIFYPEER, 0), | ||
302 | "CURLOPT_SSL_VERIFYPEER"); | ||
303 | handle_curl_option_return_code( | ||
304 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_VERIFYHOST, 0), | ||
305 | "CURLOPT_SSL_VERIFYHOST"); | ||
306 | } | ||
307 | |||
308 | /* detect SSL library used by libcurl */ | ||
309 | curlhelp_ssl_library ssl_library = curlhelp_get_ssl_library(); | ||
310 | |||
311 | /* try hard to get a stack of certificates to verify against */ | ||
312 | if (check_cert) { | ||
313 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) | ||
314 | /* inform curl to report back certificates */ | ||
315 | switch (ssl_library) { | ||
316 | case CURLHELP_SSL_LIBRARY_OPENSSL: | ||
317 | case CURLHELP_SSL_LIBRARY_LIBRESSL: | ||
318 | /* set callback to extract certificate with OpenSSL context function (works with | ||
319 | * OpenSSL-style libraries only!) */ | ||
320 | # ifdef USE_OPENSSL | ||
321 | /* libcurl and monitoring plugins built with OpenSSL, good */ | ||
322 | add_sslctx_verify_fun = true; | ||
323 | is_openssl_callback = true; | ||
324 | # endif /* USE_OPENSSL */ | ||
325 | /* libcurl is built with OpenSSL, monitoring plugins, so falling | ||
326 | * back to manually extracting certificate information */ | ||
327 | handle_curl_option_return_code( | ||
328 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
329 | break; | ||
330 | |||
331 | case CURLHELP_SSL_LIBRARY_NSS: | ||
332 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
333 | /* NSS: support for CERTINFO is implemented since 7.34.0 */ | ||
334 | handle_curl_option_return_code( | ||
335 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
336 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
337 | die(STATE_CRITICAL, | ||
338 | "HTTP CRITICAL - Cannot retrieve certificates (libcurl linked with SSL library " | ||
339 | "'%s' is too old)\n", | ||
340 | curlhelp_get_ssl_library_string(ssl_library)); | ||
341 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
342 | break; | ||
343 | |||
344 | case CURLHELP_SSL_LIBRARY_GNUTLS: | ||
345 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) | ||
346 | /* GnuTLS: support for CERTINFO is implemented since 7.42.0 */ | ||
347 | handle_curl_option_return_code( | ||
348 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
349 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) */ | ||
350 | die(STATE_CRITICAL, | ||
351 | "HTTP CRITICAL - Cannot retrieve certificates (libcurl linked with SSL library " | ||
352 | "'%s' is too old)\n", | ||
353 | curlhelp_get_ssl_library_string(ssl_library)); | ||
354 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) */ | ||
355 | break; | ||
356 | |||
357 | case CURLHELP_SSL_LIBRARY_UNKNOWN: | ||
358 | default: | ||
359 | die(STATE_CRITICAL, | ||
360 | "HTTP CRITICAL - Cannot retrieve certificates (unknown SSL library '%s', must " | ||
361 | "implement first)\n", | ||
362 | curlhelp_get_ssl_library_string(ssl_library)); | ||
363 | break; | ||
364 | } | ||
365 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | ||
366 | /* old libcurl, our only hope is OpenSSL, otherwise we are out of luck */ | ||
367 | if (ssl_library == CURLHELP_SSL_LIBRARY_OPENSSL || | ||
368 | ssl_library == CURLHELP_SSL_LIBRARY_LIBRESSL) { | ||
369 | add_sslctx_verify_fun = true; | ||
370 | } else { | ||
371 | die(STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (no " | ||
372 | "CURLOPT_SSL_CTX_FUNCTION, no OpenSSL library or libcurl " | ||
373 | "too old and has no CURLOPT_CERTINFO)\n"); | ||
374 | } | ||
375 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | ||
376 | } | ||
377 | |||
378 | # if LIBCURL_VERSION_NUM >= \ | ||
379 | MAKE_LIBCURL_VERSION(7, 10, 6) /* required for CURLOPT_SSL_CTX_FUNCTION */ | ||
380 | // ssl ctx function is not available with all ssl backends | ||
381 | if (curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_CTX_FUNCTION, NULL) != | ||
382 | CURLE_UNKNOWN_OPTION) { | ||
383 | handle_curl_option_return_code( | ||
384 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun), | ||
385 | "CURLOPT_SSL_CTX_FUNCTION"); | ||
386 | } | ||
387 | # endif | ||
388 | #endif /* LIBCURL_FEATURE_SSL */ | ||
389 | |||
390 | /* set default or user-given user agent identification */ | ||
391 | handle_curl_option_return_code( | ||
392 | curl_easy_setopt(result.curl_state.curl, CURLOPT_USERAGENT, config.user_agent), | ||
393 | "CURLOPT_USERAGENT"); | ||
394 | |||
395 | /* proxy-authentication */ | ||
396 | if (strcmp(config.proxy_auth, "")) { | ||
397 | handle_curl_option_return_code( | ||
398 | curl_easy_setopt(result.curl_state.curl, CURLOPT_PROXYUSERPWD, config.proxy_auth), | ||
399 | "CURLOPT_PROXYUSERPWD"); | ||
400 | } | ||
401 | |||
402 | /* authentication */ | ||
403 | if (strcmp(config.user_auth, "")) { | ||
404 | handle_curl_option_return_code( | ||
405 | curl_easy_setopt(result.curl_state.curl, CURLOPT_USERPWD, config.user_auth), | ||
406 | "CURLOPT_USERPWD"); | ||
407 | } | ||
408 | /* TODO: parameter auth method, bitfield of following methods: | ||
409 | * CURLAUTH_BASIC (default) | ||
410 | * CURLAUTH_DIGEST | ||
411 | * CURLAUTH_DIGEST_IE | ||
412 | * CURLAUTH_NEGOTIATE | ||
413 | * CURLAUTH_NTLM | ||
414 | * CURLAUTH_NTLM_WB | ||
415 | * | ||
416 | * convenience tokens for typical sets of methods: | ||
417 | * CURLAUTH_ANYSAFE: most secure, without BASIC | ||
418 | * or CURLAUTH_ANY: most secure, even BASIC if necessary | ||
419 | * | ||
420 | * handle_curl_option_return_code (curl_easy_setopt( curl, CURLOPT_HTTPAUTH, | ||
421 | * (long)CURLAUTH_DIGEST ), "CURLOPT_HTTPAUTH"); | ||
422 | */ | ||
423 | |||
424 | /* handle redirections */ | ||
425 | if (on_redirect_dependent) { | ||
426 | if (follow_method == FOLLOW_LIBCURL) { | ||
427 | handle_curl_option_return_code( | ||
428 | curl_easy_setopt(result.curl_state.curl, CURLOPT_FOLLOWLOCATION, 1), | ||
429 | "CURLOPT_FOLLOWLOCATION"); | ||
430 | |||
431 | /* default -1 is infinite, not good, could lead to zombie plugins! | ||
432 | Setting it to one bigger than maximal limit to handle errors nicely below | ||
433 | */ | ||
434 | handle_curl_option_return_code( | ||
435 | curl_easy_setopt(result.curl_state.curl, CURLOPT_MAXREDIRS, max_depth + 1), | ||
436 | "CURLOPT_MAXREDIRS"); | ||
437 | |||
438 | /* for now allow only http and https (we are a http(s) check plugin in the end) */ | ||
439 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 85, 0) | ||
440 | handle_curl_option_return_code( | ||
441 | curl_easy_setopt(result.curl_state.curl, CURLOPT_REDIR_PROTOCOLS_STR, "http,https"), | ||
442 | "CURLOPT_REDIR_PROTOCOLS_STR"); | ||
443 | #elif LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 4) | ||
444 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, | ||
445 | CURLOPT_REDIR_PROTOCOLS, | ||
446 | CURLPROTO_HTTP | CURLPROTO_HTTPS), | ||
447 | "CURLOPT_REDIRECT_PROTOCOLS"); | ||
448 | #endif | ||
449 | |||
450 | /* TODO: handle the following aspects of redirection, make them | ||
451 | * command line options too later: | ||
452 | CURLOPT_POSTREDIR: method switch | ||
453 | CURLINFO_REDIRECT_URL: custom redirect option | ||
454 | CURLOPT_REDIRECT_PROTOCOLS: allow people to step outside safe protocols | ||
455 | CURLINFO_REDIRECT_COUNT: get the number of redirects, print it, maybe a range | ||
456 | option here is nice like for expected page size? | ||
457 | */ | ||
458 | } else { | ||
459 | /* old style redirection*/ | ||
460 | } | ||
461 | } | ||
462 | /* no-body */ | ||
463 | if (working_state.no_body) { | ||
464 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_NOBODY, 1), | ||
465 | "CURLOPT_NOBODY"); | ||
466 | } | ||
467 | |||
468 | /* IPv4 or IPv6 forced DNS resolution */ | ||
469 | if (config.sin_family == AF_UNSPEC) { | ||
470 | handle_curl_option_return_code( | ||
471 | curl_easy_setopt(result.curl_state.curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER), | ||
472 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_WHATEVER)"); | ||
473 | } else if (config.sin_family == AF_INET) { | ||
474 | handle_curl_option_return_code( | ||
475 | curl_easy_setopt(result.curl_state.curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4), | ||
476 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V4)"); | ||
477 | } | ||
478 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) | ||
479 | else if (config.sin_family == AF_INET6) { | ||
480 | handle_curl_option_return_code( | ||
481 | curl_easy_setopt(result.curl_state.curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6), | ||
482 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V6)"); | ||
483 | } | ||
484 | #endif | ||
485 | |||
486 | /* either send http POST data (any data, not only POST)*/ | ||
487 | if (!strcmp(working_state.http_method, "POST") || !strcmp(working_state.http_method, "PUT")) { | ||
488 | /* set content of payload for POST and PUT */ | ||
489 | if (config.http_content_type) { | ||
490 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Content-Type: %s", | ||
491 | config.http_content_type); | ||
492 | result.curl_state.header_list = | ||
493 | curl_slist_append(result.curl_state.header_list, http_header); | ||
494 | } | ||
495 | /* NULL indicates "HTTP Continue" in libcurl, provide an empty string | ||
496 | * in case of no POST/PUT data */ | ||
497 | if (!working_state.http_post_data) { | ||
498 | working_state.http_post_data = ""; | ||
499 | } | ||
500 | |||
501 | if (!strcmp(working_state.http_method, "POST")) { | ||
502 | /* POST method, set payload with CURLOPT_POSTFIELDS */ | ||
503 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, | ||
504 | CURLOPT_POSTFIELDS, | ||
505 | working_state.http_post_data), | ||
506 | "CURLOPT_POSTFIELDS"); | ||
507 | } else if (!strcmp(working_state.http_method, "PUT")) { | ||
508 | handle_curl_option_return_code( | ||
509 | curl_easy_setopt(result.curl_state.curl, CURLOPT_READFUNCTION, | ||
510 | (curl_read_callback)curlhelp_buffer_read_callback), | ||
511 | "CURLOPT_READFUNCTION"); | ||
512 | if (curlhelp_initreadbuffer(&result.curl_state.put_buf, working_state.http_post_data, | ||
513 | strlen(working_state.http_post_data)) < 0) { | ||
514 | die(STATE_UNKNOWN, | ||
515 | "HTTP CRITICAL - out of memory allocating read buffer for PUT\n"); | ||
516 | } | ||
517 | result.curl_state.put_buf_initialized = true; | ||
518 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, | ||
519 | CURLOPT_READDATA, | ||
520 | (void *)result.curl_state.put_buf), | ||
521 | "CURLOPT_READDATA"); | ||
522 | handle_curl_option_return_code( | ||
523 | curl_easy_setopt(result.curl_state.curl, CURLOPT_INFILESIZE, | ||
524 | (curl_off_t)strlen(working_state.http_post_data)), | ||
525 | "CURLOPT_INFILESIZE"); | ||
526 | } | ||
527 | } | ||
528 | |||
529 | /* cookie handling */ | ||
530 | if (config.cookie_jar_file != NULL) { | ||
531 | /* enable reading cookies from a file, and if the filename is an empty string, only | ||
532 | * enable the curl cookie engine */ | ||
533 | handle_curl_option_return_code( | ||
534 | curl_easy_setopt(result.curl_state.curl, CURLOPT_COOKIEFILE, config.cookie_jar_file), | ||
535 | "CURLOPT_COOKIEFILE"); | ||
536 | /* now enable saving cookies to a file, but only if the filename is not an empty string, | ||
537 | * since writing it would fail */ | ||
538 | if (*config.cookie_jar_file) { | ||
539 | handle_curl_option_return_code( | ||
540 | curl_easy_setopt(result.curl_state.curl, CURLOPT_COOKIEJAR, config.cookie_jar_file), | ||
541 | "CURLOPT_COOKIEJAR"); | ||
542 | } | ||
543 | } | ||
544 | |||
545 | result.working_state = working_state; | ||
546 | |||
547 | return result; | ||
548 | } | ||
549 | |||
550 | void handle_curl_option_return_code(CURLcode res, const char *option) { | ||
551 | if (res != CURLE_OK) { | ||
552 | die(STATE_CRITICAL, _("Error while setting cURL option '%s': cURL returned %d - %s"), | ||
553 | option, res, curl_easy_strerror(res)); | ||
554 | } | ||
555 | } | ||
556 | |||
557 | char *get_header_value(const struct phr_header *headers, const size_t nof_headers, | ||
558 | const char *header) { | ||
559 | for (size_t i = 0; i < nof_headers; i++) { | ||
560 | if (headers[i].name != NULL && | ||
561 | strncasecmp(header, headers[i].name, max(headers[i].name_len, 4)) == 0) { | ||
562 | return strndup(headers[i].value, headers[i].value_len); | ||
563 | } | ||
564 | } | ||
565 | return NULL; | ||
566 | } | ||
567 | |||
568 | check_curl_working_state check_curl_working_state_init() { | ||
569 | check_curl_working_state result = { | ||
570 | .server_address = NULL, | ||
571 | .server_url = DEFAULT_SERVER_URL, | ||
572 | .host_name = NULL, | ||
573 | .http_method = NULL, | ||
574 | .http_post_data = NULL, | ||
575 | .virtualPort = 0, | ||
576 | .serverPort = HTTP_PORT, | ||
577 | .use_ssl = false, | ||
578 | .no_body = false, | ||
579 | }; | ||
580 | return result; | ||
581 | } | ||
582 | |||
583 | check_curl_config check_curl_config_init() { | ||
584 | check_curl_config tmp = { | ||
585 | .initial_config = check_curl_working_state_init(), | ||
586 | |||
587 | .curl_config = | ||
588 | { | ||
589 | .automatic_decompression = false, | ||
590 | .socket_timeout = DEFAULT_SOCKET_TIMEOUT, | ||
591 | .haproxy_protocol = false, | ||
592 | .sin_family = AF_UNSPEC, | ||
593 | .curl_http_version = CURL_HTTP_VERSION_NONE, | ||
594 | .http_opt_headers = NULL, | ||
595 | .http_opt_headers_count = 0, | ||
596 | .ssl_version = CURL_SSLVERSION_DEFAULT, | ||
597 | .client_cert = NULL, | ||
598 | .client_privkey = NULL, | ||
599 | .ca_cert = NULL, | ||
600 | .verify_peer_and_host = false, | ||
601 | .user_agent = {'\0'}, | ||
602 | .proxy_auth = "", | ||
603 | .user_auth = "", | ||
604 | .http_content_type = NULL, | ||
605 | .cookie_jar_file = NULL, | ||
606 | }, | ||
607 | .max_depth = DEFAULT_MAX_REDIRS, | ||
608 | .followmethod = FOLLOW_HTTP_CURL, | ||
609 | .followsticky = STICKY_NONE, | ||
610 | |||
611 | .maximum_age = -1, | ||
612 | .regexp = {}, | ||
613 | .compiled_regex = {}, | ||
614 | .state_regex = STATE_CRITICAL, | ||
615 | .invert_regex = false, | ||
616 | .check_cert = false, | ||
617 | .continue_after_check_cert = false, | ||
618 | .days_till_exp_warn = 0, | ||
619 | .days_till_exp_crit = 0, | ||
620 | .thlds = mp_thresholds_init(), | ||
621 | .page_length_limits = mp_range_init(), | ||
622 | .page_length_limits_is_set = false, | ||
623 | .server_expect = | ||
624 | { | ||
625 | .string = HTTP_EXPECT, | ||
626 | .is_present = false, | ||
627 | }, | ||
628 | .string_expect = "", | ||
629 | .header_expect = "", | ||
630 | .on_redirect_result_state = STATE_OK, | ||
631 | .on_redirect_dependent = false, | ||
632 | |||
633 | .show_extended_perfdata = false, | ||
634 | .show_body = false, | ||
635 | |||
636 | .output_format_is_set = false, | ||
637 | }; | ||
638 | |||
639 | snprintf(tmp.curl_config.user_agent, DEFAULT_BUFFER_SIZE, "%s/v%s (monitoring-plugins %s, %s)", | ||
640 | "check_curl", NP_VERSION, VERSION, curl_version()); | ||
641 | |||
642 | return tmp; | ||
643 | } | ||
644 | |||
645 | /* TODO: is there a better way in libcurl to check for the SSL library? */ | ||
646 | curlhelp_ssl_library curlhelp_get_ssl_library(void) { | ||
647 | curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
648 | |||
649 | curl_version_info_data *version_data = curl_version_info(CURLVERSION_NOW); | ||
650 | if (version_data == NULL) { | ||
651 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
652 | } | ||
653 | |||
654 | char *ssl_version = strdup(version_data->ssl_version); | ||
655 | if (ssl_version == NULL) { | ||
656 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
657 | } | ||
658 | |||
659 | char *library = strtok(ssl_version, "/"); | ||
660 | if (library == NULL) { | ||
661 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
662 | } | ||
663 | |||
664 | if (strcmp(library, "OpenSSL") == 0) { | ||
665 | ssl_library = CURLHELP_SSL_LIBRARY_OPENSSL; | ||
666 | } else if (strcmp(library, "LibreSSL") == 0) { | ||
667 | ssl_library = CURLHELP_SSL_LIBRARY_LIBRESSL; | ||
668 | } else if (strcmp(library, "GnuTLS") == 0) { | ||
669 | ssl_library = CURLHELP_SSL_LIBRARY_GNUTLS; | ||
670 | } else if (strcmp(library, "NSS") == 0) { | ||
671 | ssl_library = CURLHELP_SSL_LIBRARY_NSS; | ||
672 | } | ||
673 | |||
674 | if (verbose >= 2) { | ||
675 | printf("* SSL library string is : %s %s (%d)\n", version_data->ssl_version, library, | ||
676 | ssl_library); | ||
677 | } | ||
678 | |||
679 | free(ssl_version); | ||
680 | |||
681 | return ssl_library; | ||
682 | } | ||
683 | |||
684 | const char *curlhelp_get_ssl_library_string(const curlhelp_ssl_library ssl_library) { | ||
685 | switch (ssl_library) { | ||
686 | case CURLHELP_SSL_LIBRARY_OPENSSL: | ||
687 | return "OpenSSL"; | ||
688 | case CURLHELP_SSL_LIBRARY_LIBRESSL: | ||
689 | return "LibreSSL"; | ||
690 | case CURLHELP_SSL_LIBRARY_GNUTLS: | ||
691 | return "GnuTLS"; | ||
692 | case CURLHELP_SSL_LIBRARY_NSS: | ||
693 | return "NSS"; | ||
694 | case CURLHELP_SSL_LIBRARY_UNKNOWN: | ||
695 | default: | ||
696 | return "unknown"; | ||
697 | } | ||
698 | } | ||
699 | |||
700 | size_t get_content_length(const curlhelp_write_curlbuf *header_buf, | ||
701 | const curlhelp_write_curlbuf *body_buf) { | ||
702 | struct phr_header headers[255]; | ||
703 | size_t nof_headers = 255; | ||
704 | size_t msglen; | ||
705 | curlhelp_statusline status_line; | ||
706 | int res = phr_parse_response(header_buf->buf, header_buf->buflen, &status_line.http_major, | ||
707 | &status_line.http_minor, &status_line.http_code, &status_line.msg, | ||
708 | &msglen, headers, &nof_headers, 0); | ||
709 | |||
710 | if (res == -1) { | ||
711 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse Response\n")); | ||
712 | } | ||
713 | |||
714 | char *content_length_s = get_header_value(headers, nof_headers, "content-length"); | ||
715 | if (!content_length_s) { | ||
716 | return header_buf->buflen + body_buf->buflen; | ||
717 | } | ||
718 | |||
719 | content_length_s += strspn(content_length_s, " \t"); | ||
720 | size_t content_length = atoi(content_length_s); | ||
721 | if (content_length != body_buf->buflen) { | ||
722 | /* TODO: should we warn if the actual and the reported body length don't match? */ | ||
723 | } | ||
724 | |||
725 | if (content_length_s) { | ||
726 | free(content_length_s); | ||
727 | } | ||
728 | |||
729 | return header_buf->buflen + body_buf->buflen; | ||
730 | } | ||
731 | |||
732 | mp_subcheck check_document_dates(const curlhelp_write_curlbuf *header_buf, const int maximum_age) { | ||
733 | struct phr_header headers[255]; | ||
734 | size_t nof_headers = 255; | ||
735 | curlhelp_statusline status_line; | ||
736 | size_t msglen; | ||
737 | int res = phr_parse_response(header_buf->buf, header_buf->buflen, &status_line.http_major, | ||
738 | &status_line.http_minor, &status_line.http_code, &status_line.msg, | ||
739 | &msglen, headers, &nof_headers, 0); | ||
740 | |||
741 | if (res == -1) { | ||
742 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse Response\n")); | ||
743 | } | ||
744 | |||
745 | char *server_date = get_header_value(headers, nof_headers, "date"); | ||
746 | char *document_date = get_header_value(headers, nof_headers, "last-modified"); | ||
747 | |||
748 | mp_subcheck sc_document_dates = mp_subcheck_init(); | ||
749 | if (!server_date || !*server_date) { | ||
750 | xasprintf(&sc_document_dates.output, _("Server date unknown")); | ||
751 | sc_document_dates = mp_set_subcheck_state(sc_document_dates, STATE_UNKNOWN); | ||
752 | } else if (!document_date || !*document_date) { | ||
753 | xasprintf(&sc_document_dates.output, _("Document modification date unknown, ")); | ||
754 | sc_document_dates = mp_set_subcheck_state(sc_document_dates, STATE_CRITICAL); | ||
755 | } else { | ||
756 | time_t srv_data = curl_getdate(server_date, NULL); | ||
757 | time_t doc_data = curl_getdate(document_date, NULL); | ||
758 | |||
759 | if (verbose >= 2) { | ||
760 | printf("* server date: '%s' (%d), doc_date: '%s' (%d)\n", server_date, (int)srv_data, | ||
761 | document_date, (int)doc_data); | ||
762 | } | ||
763 | |||
764 | if (srv_data <= 0) { | ||
765 | xasprintf(&sc_document_dates.output, _("Server date \"%100s\" unparsable"), | ||
766 | server_date); | ||
767 | sc_document_dates = mp_set_subcheck_state(sc_document_dates, STATE_CRITICAL); | ||
768 | } else if (doc_data <= 0) { | ||
769 | |||
770 | xasprintf(&sc_document_dates.output, _("Document date \"%100s\" unparsable"), | ||
771 | document_date); | ||
772 | sc_document_dates = mp_set_subcheck_state(sc_document_dates, STATE_CRITICAL); | ||
773 | } else if (doc_data > srv_data + 30) { | ||
774 | |||
775 | xasprintf(&sc_document_dates.output, _("Document is %d seconds in the future"), | ||
776 | (int)doc_data - (int)srv_data); | ||
777 | |||
778 | sc_document_dates = mp_set_subcheck_state(sc_document_dates, STATE_CRITICAL); | ||
779 | } else if (doc_data < srv_data - maximum_age) { | ||
780 | time_t last_modified = (srv_data - doc_data); | ||
781 | if (last_modified > (60 * 60 * 24 * 2)) { // two days hardcoded? | ||
782 | xasprintf(&sc_document_dates.output, _("Last modified %.1f days ago"), | ||
783 | ((float)last_modified) / (60 * 60 * 24)); | ||
784 | sc_document_dates = mp_set_subcheck_state(sc_document_dates, STATE_CRITICAL); | ||
785 | } else { | ||
786 | xasprintf(&sc_document_dates.output, _("Last modified %ld:%02ld:%02ld ago"), | ||
787 | last_modified / (60 * 60), (last_modified / 60) % 60, last_modified % 60); | ||
788 | sc_document_dates = mp_set_subcheck_state(sc_document_dates, STATE_CRITICAL); | ||
789 | } | ||
790 | } else { | ||
791 | // TODO is this the OK case? | ||
792 | time_t last_modified = (srv_data - doc_data); | ||
793 | xasprintf(&sc_document_dates.output, _("Last modified %ld:%02ld:%02ld ago"), | ||
794 | last_modified / (60 * 60), (last_modified / 60) % 60, last_modified % 60); | ||
795 | sc_document_dates = mp_set_subcheck_state(sc_document_dates, STATE_OK); | ||
796 | } | ||
797 | } | ||
798 | |||
799 | if (server_date) { | ||
800 | free(server_date); | ||
801 | } | ||
802 | if (document_date) { | ||
803 | free(document_date); | ||
804 | } | ||
805 | |||
806 | return sc_document_dates; | ||
807 | } | ||
808 | |||
809 | void curlhelp_free_statusline(curlhelp_statusline *status_line) { free(status_line->first_line); } | ||
810 | |||
811 | int curlhelp_parse_statusline(const char *buf, curlhelp_statusline *status_line) { | ||
812 | /* find last start of a new header */ | ||
813 | const char *start = strrstr2(buf, "\r\nHTTP/"); | ||
814 | if (start != NULL) { | ||
815 | start += 2; | ||
816 | buf = start; | ||
817 | } | ||
818 | |||
819 | char *first_line_end = strstr(buf, "\r\n"); | ||
820 | if (first_line_end == NULL) { | ||
821 | return -1; | ||
822 | } | ||
823 | |||
824 | size_t first_line_len = (size_t)(first_line_end - buf); | ||
825 | status_line->first_line = (char *)calloc(first_line_len + 1, sizeof(char)); | ||
826 | if (status_line->first_line == NULL) { | ||
827 | return -1; | ||
828 | } | ||
829 | memcpy(status_line->first_line, buf, first_line_len); | ||
830 | status_line->first_line[first_line_len] = '\0'; | ||
831 | char *first_line_buf = strdup(status_line->first_line); | ||
832 | |||
833 | /* protocol and version: "HTTP/x.x" SP or "HTTP/2" SP */ | ||
834 | char *temp_string = strtok(first_line_buf, "/"); | ||
835 | if (temp_string == NULL) { | ||
836 | free(first_line_buf); | ||
837 | return -1; | ||
838 | } | ||
839 | if (strcmp(temp_string, "HTTP") != 0) { | ||
840 | free(first_line_buf); | ||
841 | return -1; | ||
842 | } | ||
843 | |||
844 | temp_string = strtok(NULL, " "); | ||
845 | if (temp_string == NULL) { | ||
846 | free(first_line_buf); | ||
847 | return -1; | ||
848 | } | ||
849 | |||
850 | char *temp_string_2; | ||
851 | if (strchr(temp_string, '.') != NULL) { | ||
852 | |||
853 | /* HTTP 1.x case */ | ||
854 | strtok(temp_string, "."); | ||
855 | status_line->http_major = (int)strtol(temp_string, &temp_string_2, 10); | ||
856 | if (*temp_string_2 != '\0') { | ||
857 | free(first_line_buf); | ||
858 | return -1; | ||
859 | } | ||
860 | strtok(NULL, " "); | ||
861 | status_line->http_minor = (int)strtol(temp_string, &temp_string_2, 10); | ||
862 | if (*temp_string_2 != '\0') { | ||
863 | free(first_line_buf); | ||
864 | return -1; | ||
865 | } | ||
866 | temp_string += 4; /* 1.x SP */ | ||
867 | } else { | ||
868 | /* HTTP 2 case */ | ||
869 | status_line->http_major = (int)strtol(temp_string, &temp_string_2, 10); | ||
870 | status_line->http_minor = 0; | ||
871 | temp_string += 2; /* 2 SP */ | ||
872 | } | ||
873 | |||
874 | /* status code: "404" or "404.1", then SP */ | ||
875 | temp_string = strtok(temp_string, " "); | ||
876 | if (temp_string == NULL) { | ||
877 | free(first_line_buf); | ||
878 | return -1; | ||
879 | } | ||
880 | if (strchr(temp_string, '.') != NULL) { | ||
881 | char *ppp; | ||
882 | ppp = strtok(temp_string, "."); | ||
883 | status_line->http_code = (int)strtol(ppp, &temp_string_2, 10); | ||
884 | if (*temp_string_2 != '\0') { | ||
885 | free(first_line_buf); | ||
886 | return -1; | ||
887 | } | ||
888 | ppp = strtok(NULL, ""); | ||
889 | status_line->http_subcode = (int)strtol(ppp, &temp_string_2, 10); | ||
890 | if (*temp_string_2 != '\0') { | ||
891 | free(first_line_buf); | ||
892 | return -1; | ||
893 | } | ||
894 | temp_string += 6; /* 400.1 SP */ | ||
895 | } else { | ||
896 | status_line->http_code = (int)strtol(temp_string, &temp_string_2, 10); | ||
897 | status_line->http_subcode = -1; | ||
898 | if (*temp_string_2 != '\0') { | ||
899 | free(first_line_buf); | ||
900 | return -1; | ||
901 | } | ||
902 | temp_string += 4; /* 400 SP */ | ||
903 | } | ||
904 | |||
905 | /* Human readable message: "Not Found" CRLF */ | ||
906 | |||
907 | temp_string = strtok(temp_string, ""); | ||
908 | if (temp_string == NULL) { | ||
909 | status_line->msg = ""; | ||
910 | return 0; | ||
911 | } | ||
912 | status_line->msg = status_line->first_line + (temp_string - first_line_buf); | ||
913 | free(first_line_buf); | ||
914 | |||
915 | return 0; | ||
916 | } | ||
917 | |||
918 | /* TODO: where to put this, it's actually part of sstrings2 (logically)? | ||
919 | */ | ||
920 | const char *strrstr2(const char *haystack, const char *needle) { | ||
921 | if (haystack == NULL || needle == NULL) { | ||
922 | return NULL; | ||
923 | } | ||
924 | |||
925 | if (haystack[0] == '\0' || needle[0] == '\0') { | ||
926 | return NULL; | ||
927 | } | ||
928 | |||
929 | int counter = 0; | ||
930 | const char *prev_pos = NULL; | ||
931 | const char *pos = haystack; | ||
932 | size_t len = strlen(needle); | ||
933 | for (;;) { | ||
934 | pos = strstr(pos, needle); | ||
935 | if (pos == NULL) { | ||
936 | if (counter == 0) { | ||
937 | return NULL; | ||
938 | } | ||
939 | return prev_pos; | ||
940 | } | ||
941 | counter++; | ||
942 | prev_pos = pos; | ||
943 | pos += len; | ||
944 | if (*pos == '\0') { | ||
945 | return prev_pos; | ||
946 | } | ||
947 | } | ||
948 | } | ||
949 | |||
950 | void curlhelp_freereadbuffer(curlhelp_read_curlbuf *buf) { | ||
951 | free(buf->buf); | ||
952 | buf->buf = NULL; | ||
953 | } | ||
954 | |||
955 | void curlhelp_freewritebuffer(curlhelp_write_curlbuf *buf) { | ||
956 | free(buf->buf); | ||
957 | buf->buf = NULL; | ||
958 | } | ||
959 | |||
960 | int curlhelp_initreadbuffer(curlhelp_read_curlbuf **buf, const char *data, size_t datalen) { | ||
961 | if ((*buf = calloc(1, sizeof(curlhelp_read_curlbuf))) == NULL) { | ||
962 | return 1; | ||
963 | } | ||
964 | |||
965 | (*buf)->buflen = datalen; | ||
966 | (*buf)->buf = (char *)calloc((*buf)->buflen, sizeof(char)); | ||
967 | if ((*buf)->buf == NULL) { | ||
968 | return -1; | ||
969 | } | ||
970 | memcpy((*buf)->buf, data, datalen); | ||
971 | (*buf)->pos = 0; | ||
972 | return 0; | ||
973 | } | ||
974 | |||
975 | size_t curlhelp_buffer_read_callback(void *buffer, size_t size, size_t nmemb, void *stream) { | ||
976 | curlhelp_read_curlbuf *buf = (curlhelp_read_curlbuf *)stream; | ||
977 | |||
978 | size_t minimalSize = min(nmemb * size, buf->buflen - buf->pos); | ||
979 | |||
980 | memcpy(buffer, buf->buf + buf->pos, minimalSize); | ||
981 | buf->pos += minimalSize; | ||
982 | |||
983 | return minimalSize; | ||
984 | } | ||
985 | |||
986 | int curlhelp_initwritebuffer(curlhelp_write_curlbuf **buf) { | ||
987 | if ((*buf = calloc(1, sizeof(curlhelp_write_curlbuf))) == NULL) { | ||
988 | return 1; | ||
989 | } | ||
990 | (*buf)->bufsize = DEFAULT_BUFFER_SIZE * sizeof(char); | ||
991 | (*buf)->buflen = 0; | ||
992 | (*buf)->buf = (char *)calloc((*buf)->bufsize, sizeof(char)); | ||
993 | if ((*buf)->buf == NULL) { | ||
994 | return -1; | ||
995 | } | ||
996 | return 0; | ||
997 | } | ||
998 | |||
999 | size_t curlhelp_buffer_write_callback(void *buffer, size_t size, size_t nmemb, void *stream) { | ||
1000 | curlhelp_write_curlbuf *buf = (curlhelp_write_curlbuf *)stream; | ||
1001 | |||
1002 | while (buf->bufsize < buf->buflen + size * nmemb + 1) { | ||
1003 | buf->bufsize = buf->bufsize * 2; | ||
1004 | buf->buf = (char *)realloc(buf->buf, buf->bufsize); | ||
1005 | if (buf->buf == NULL) { | ||
1006 | fprintf(stderr, "malloc failed (%d) %s\n", errno, strerror(errno)); | ||
1007 | return 0; | ||
1008 | } | ||
1009 | } | ||
1010 | |||
1011 | memcpy(buf->buf + buf->buflen, buffer, size * nmemb); | ||
1012 | buf->buflen += size * nmemb; | ||
1013 | buf->buf[buf->buflen] = '\0'; | ||
1014 | |||
1015 | return size * nmemb; | ||
1016 | } | ||
1017 | |||
1018 | void cleanup(check_curl_global_state global_state) { | ||
1019 | if (global_state.status_line_initialized) { | ||
1020 | curlhelp_free_statusline(global_state.status_line); | ||
1021 | } | ||
1022 | global_state.status_line_initialized = false; | ||
1023 | |||
1024 | if (global_state.curl_easy_initialized) { | ||
1025 | curl_easy_cleanup(global_state.curl); | ||
1026 | } | ||
1027 | global_state.curl_easy_initialized = false; | ||
1028 | |||
1029 | if (global_state.curl_global_initialized) { | ||
1030 | curl_global_cleanup(); | ||
1031 | } | ||
1032 | global_state.curl_global_initialized = false; | ||
1033 | |||
1034 | if (global_state.body_buf_initialized) { | ||
1035 | curlhelp_freewritebuffer(global_state.body_buf); | ||
1036 | } | ||
1037 | global_state.body_buf_initialized = false; | ||
1038 | |||
1039 | if (global_state.header_buf_initialized) { | ||
1040 | curlhelp_freewritebuffer(global_state.header_buf); | ||
1041 | } | ||
1042 | global_state.header_buf_initialized = false; | ||
1043 | |||
1044 | if (global_state.put_buf_initialized) { | ||
1045 | curlhelp_freereadbuffer(global_state.put_buf); | ||
1046 | } | ||
1047 | global_state.put_buf_initialized = false; | ||
1048 | |||
1049 | if (global_state.header_list) { | ||
1050 | curl_slist_free_all(global_state.header_list); | ||
1051 | } | ||
1052 | |||
1053 | if (global_state.host) { | ||
1054 | curl_slist_free_all(global_state.host); | ||
1055 | } | ||
1056 | } | ||
1057 | |||
1058 | int lookup_host(const char *host, char *buf, size_t buflen, sa_family_t addr_family) { | ||
1059 | struct addrinfo hints = { | ||
1060 | .ai_family = addr_family, | ||
1061 | .ai_socktype = SOCK_STREAM, | ||
1062 | .ai_flags = AI_CANONNAME, | ||
1063 | }; | ||
1064 | |||
1065 | struct addrinfo *result; | ||
1066 | int errcode = getaddrinfo(host, NULL, &hints, &result); | ||
1067 | if (errcode != 0) { | ||
1068 | return errcode; | ||
1069 | } | ||
1070 | |||
1071 | strcpy(buf, ""); | ||
1072 | struct addrinfo *res = result; | ||
1073 | |||
1074 | size_t buflen_remaining = buflen - 1; | ||
1075 | size_t addrstr_len; | ||
1076 | char addrstr[100]; | ||
1077 | void *ptr = {0}; | ||
1078 | while (res) { | ||
1079 | switch (res->ai_family) { | ||
1080 | case AF_INET: | ||
1081 | ptr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; | ||
1082 | break; | ||
1083 | case AF_INET6: | ||
1084 | ptr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; | ||
1085 | break; | ||
1086 | } | ||
1087 | |||
1088 | inet_ntop(res->ai_family, ptr, addrstr, 100); | ||
1089 | if (verbose >= 1) { | ||
1090 | printf("* getaddrinfo IPv%d address: %s\n", res->ai_family == PF_INET6 ? 6 : 4, | ||
1091 | addrstr); | ||
1092 | } | ||
1093 | |||
1094 | // Append all IPs to buf as a comma-separated string | ||
1095 | addrstr_len = strlen(addrstr); | ||
1096 | if (buflen_remaining > addrstr_len + 1) { | ||
1097 | if (buf[0] != '\0') { | ||
1098 | strncat(buf, ",", buflen_remaining); | ||
1099 | buflen_remaining -= 1; | ||
1100 | } | ||
1101 | strncat(buf, addrstr, buflen_remaining); | ||
1102 | buflen_remaining -= addrstr_len; | ||
1103 | } | ||
1104 | |||
1105 | res = res->ai_next; | ||
1106 | } | ||
1107 | |||
1108 | freeaddrinfo(result); | ||
1109 | |||
1110 | return 0; | ||
1111 | } | ||
1112 | |||
1113 | /* Checks if the server 'reply' is one of the expected 'statuscodes' */ | ||
1114 | bool expected_statuscode(const char *reply, const char *statuscodes) { | ||
1115 | char *expected; | ||
1116 | |||
1117 | if ((expected = strdup(statuscodes)) == NULL) { | ||
1118 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); | ||
1119 | } | ||
1120 | |||
1121 | bool result = false; | ||
1122 | for (char *code = strtok(expected, ","); code != NULL; code = strtok(NULL, ",")) { | ||
1123 | if (strstr(reply, code) != NULL) { | ||
1124 | result = true; | ||
1125 | break; | ||
1126 | } | ||
1127 | } | ||
1128 | |||
1129 | free(expected); | ||
1130 | return result; | ||
1131 | } | ||
1132 | |||
1133 | /* returns a string "HTTP/1.x" or "HTTP/2" */ | ||
1134 | char *string_statuscode(int major, int minor) { | ||
1135 | static char buf[10]; | ||
1136 | |||
1137 | switch (major) { | ||
1138 | case 1: | ||
1139 | snprintf(buf, sizeof(buf), "HTTP/%d.%d", major, minor); | ||
1140 | break; | ||
1141 | case 2: | ||
1142 | case 3: | ||
1143 | snprintf(buf, sizeof(buf), "HTTP/%d", major); | ||
1144 | break; | ||
1145 | default: | ||
1146 | /* assuming here HTTP/N with N>=4 */ | ||
1147 | snprintf(buf, sizeof(buf), "HTTP/%d", major); | ||
1148 | break; | ||
1149 | } | ||
1150 | |||
1151 | return buf; | ||
1152 | } | ||
1153 | |||
1154 | /* check whether a file exists */ | ||
1155 | void test_file(char *path) { | ||
1156 | if (access(path, R_OK) == 0) { | ||
1157 | return; | ||
1158 | } | ||
1159 | usage2(_("file does not exist or is not readable"), path); | ||
1160 | } | ||
1161 | |||
1162 | mp_subcheck mp_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, | ||
1163 | int days_till_exp_crit); | ||
1164 | |||
1165 | mp_subcheck check_curl_certificate_checks(CURL *curl, X509 *cert, int warn_days_till_exp, | ||
1166 | int crit_days_till_exp) { | ||
1167 | mp_subcheck sc_cert_result = mp_subcheck_init(); | ||
1168 | sc_cert_result = mp_set_subcheck_default_state(sc_cert_result, STATE_OK); | ||
1169 | |||
1170 | #ifdef LIBCURL_FEATURE_SSL | ||
1171 | if (is_openssl_callback) { | ||
1172 | # ifdef USE_OPENSSL | ||
1173 | /* check certificate with OpenSSL functions, curl has been built against OpenSSL | ||
1174 | * and we actually have OpenSSL in the monitoring tools | ||
1175 | */ | ||
1176 | return mp_net_ssl_check_certificate(cert, warn_days_till_exp, crit_days_till_exp); | ||
1177 | # else /* USE_OPENSSL */ | ||
1178 | xasprintf(&result.output, "HTTP CRITICAL - Cannot retrieve certificates - OpenSSL " | ||
1179 | "callback used and not linked against OpenSSL\n"); | ||
1180 | mp_set_subcheck_state(result, STATE_CRITICAL); | ||
1181 | # endif /* USE_OPENSSL */ | ||
1182 | } else { | ||
1183 | struct curl_slist *slist; | ||
1184 | |||
1185 | cert_ptr_union cert_ptr = {0}; | ||
1186 | cert_ptr.to_info = NULL; | ||
1187 | CURLcode res = curl_easy_getinfo(curl, CURLINFO_CERTINFO, &cert_ptr.to_info); | ||
1188 | if (!res && cert_ptr.to_info) { | ||
1189 | # ifdef USE_OPENSSL | ||
1190 | /* We have no OpenSSL in libcurl, but we can use OpenSSL for X509 cert | ||
1191 | * parsing We only check the first certificate and assume it's the one of | ||
1192 | * the server | ||
1193 | */ | ||
1194 | char *raw_cert = NULL; | ||
1195 | bool got_first_cert = false; | ||
1196 | for (int i = 0; i < cert_ptr.to_certinfo->num_of_certs; i++) { | ||
1197 | if (got_first_cert) { | ||
1198 | break; | ||
1199 | } | ||
1200 | |||
1201 | for (slist = cert_ptr.to_certinfo->certinfo[i]; slist; slist = slist->next) { | ||
1202 | if (verbose >= 2) { | ||
1203 | printf("%d ** %s\n", i, slist->data); | ||
1204 | } | ||
1205 | if (strncmp(slist->data, "Cert:", 5) == 0) { | ||
1206 | raw_cert = &slist->data[5]; | ||
1207 | got_first_cert = true; | ||
1208 | break; | ||
1209 | } | ||
1210 | } | ||
1211 | } | ||
1212 | |||
1213 | if (!raw_cert) { | ||
1214 | |||
1215 | xasprintf(&sc_cert_result.output, | ||
1216 | _("Cannot retrieve certificates from CERTINFO information - " | ||
1217 | "certificate data was empty")); | ||
1218 | sc_cert_result = mp_set_subcheck_state(sc_cert_result, STATE_CRITICAL); | ||
1219 | return sc_cert_result; | ||
1220 | } | ||
1221 | |||
1222 | BIO *cert_BIO = BIO_new(BIO_s_mem()); | ||
1223 | BIO_write(cert_BIO, raw_cert, (int)strlen(raw_cert)); | ||
1224 | |||
1225 | cert = PEM_read_bio_X509(cert_BIO, NULL, NULL, NULL); | ||
1226 | if (!cert) { | ||
1227 | xasprintf(&sc_cert_result.output, | ||
1228 | _("Cannot read certificate from CERTINFO information - BIO error")); | ||
1229 | sc_cert_result = mp_set_subcheck_state(sc_cert_result, STATE_CRITICAL); | ||
1230 | return sc_cert_result; | ||
1231 | } | ||
1232 | |||
1233 | BIO_free(cert_BIO); | ||
1234 | return mp_net_ssl_check_certificate(cert, warn_days_till_exp, crit_days_till_exp); | ||
1235 | # else /* USE_OPENSSL */ | ||
1236 | /* We assume we don't have OpenSSL and np_net_ssl_check_certificate at our | ||
1237 | * disposal, so we use the libcurl CURLINFO data | ||
1238 | */ | ||
1239 | return net_noopenssl_check_certificate(&cert_ptr, days_till_exp_warn, | ||
1240 | days_till_exp_crit); | ||
1241 | # endif /* USE_OPENSSL */ | ||
1242 | } else { | ||
1243 | xasprintf(&sc_cert_result.output, | ||
1244 | _("Cannot retrieve certificates - cURL returned %d - %s"), res, | ||
1245 | curl_easy_strerror(res)); | ||
1246 | mp_set_subcheck_state(sc_cert_result, STATE_CRITICAL); | ||
1247 | } | ||
1248 | } | ||
1249 | #endif /* LIBCURL_FEATURE_SSL */ | ||
1250 | |||
1251 | return sc_cert_result; | ||
1252 | } | ||
1253 | |||
1254 | char *fmt_url(check_curl_working_state workingState) { | ||
1255 | char *url = calloc(DEFAULT_BUFFER_SIZE, sizeof(char)); | ||
1256 | if (url == NULL) { | ||
1257 | die(STATE_UNKNOWN, "memory allocation failed"); | ||
1258 | } | ||
1259 | |||
1260 | snprintf(url, DEFAULT_BUFFER_SIZE, "%s://%s:%d%s", workingState.use_ssl ? "https" : "http", | ||
1261 | (workingState.use_ssl & (workingState.host_name != NULL)) | ||
1262 | ? workingState.host_name | ||
1263 | : workingState.server_address, | ||
1264 | workingState.serverPort, workingState.server_url); | ||
1265 | |||
1266 | return url; | ||
1267 | } | ||
diff --git a/plugins/check_curl.d/check_curl_helpers.h b/plugins/check_curl.d/check_curl_helpers.h new file mode 100644 index 00000000..87e45a9d --- /dev/null +++ b/plugins/check_curl.d/check_curl_helpers.h | |||
@@ -0,0 +1,124 @@ | |||
1 | #include "./config.h" | ||
2 | #include <curl/curl.h> | ||
3 | #include "../picohttpparser/picohttpparser.h" | ||
4 | #include "output.h" | ||
5 | |||
6 | #if defined(HAVE_SSL) && defined(USE_OPENSSL) | ||
7 | # include <openssl/opensslv.h> | ||
8 | #endif | ||
9 | |||
10 | /* for buffers for header and body */ | ||
11 | typedef struct { | ||
12 | size_t buflen; | ||
13 | size_t bufsize; | ||
14 | char *buf; | ||
15 | } curlhelp_write_curlbuf; | ||
16 | |||
17 | /* for buffering the data sent in PUT */ | ||
18 | typedef struct { | ||
19 | size_t buflen; | ||
20 | off_t pos; | ||
21 | char *buf; | ||
22 | } curlhelp_read_curlbuf; | ||
23 | |||
24 | /* for parsing the HTTP status line */ | ||
25 | typedef struct { | ||
26 | int http_major; /* major version of the protocol, always 1 (HTTP/0.9 | ||
27 | * never reached the big internet most likely) */ | ||
28 | int http_minor; /* minor version of the protocol, usually 0 or 1 */ | ||
29 | int http_code; /* HTTP return code as in RFC 2145 */ | ||
30 | int http_subcode; /* Microsoft IIS extension, HTTP subcodes, see | ||
31 | * http://support.microsoft.com/kb/318380/en-us */ | ||
32 | const char *msg; /* the human readable message */ | ||
33 | char *first_line; /* a copy of the first line */ | ||
34 | } curlhelp_statusline; | ||
35 | |||
36 | typedef struct { | ||
37 | bool curl_global_initialized; | ||
38 | bool curl_easy_initialized; | ||
39 | |||
40 | bool body_buf_initialized; | ||
41 | curlhelp_write_curlbuf *body_buf; | ||
42 | |||
43 | bool header_buf_initialized; | ||
44 | curlhelp_write_curlbuf *header_buf; | ||
45 | |||
46 | bool status_line_initialized; | ||
47 | curlhelp_statusline *status_line; | ||
48 | |||
49 | bool put_buf_initialized; | ||
50 | curlhelp_read_curlbuf *put_buf; | ||
51 | |||
52 | CURL *curl; | ||
53 | |||
54 | struct curl_slist *header_list; | ||
55 | struct curl_slist *host; | ||
56 | } check_curl_global_state; | ||
57 | |||
58 | /* to know the underlying SSL library used by libcurl */ | ||
59 | typedef enum curlhelp_ssl_library { | ||
60 | CURLHELP_SSL_LIBRARY_UNKNOWN, | ||
61 | CURLHELP_SSL_LIBRARY_OPENSSL, | ||
62 | CURLHELP_SSL_LIBRARY_LIBRESSL, | ||
63 | CURLHELP_SSL_LIBRARY_GNUTLS, | ||
64 | CURLHELP_SSL_LIBRARY_NSS | ||
65 | } curlhelp_ssl_library; | ||
66 | |||
67 | #define MAKE_LIBCURL_VERSION(major, minor, patch) ((major) * 0x10000 + (minor) * 0x100 + (patch)) | ||
68 | |||
69 | typedef struct { | ||
70 | int errorcode; | ||
71 | check_curl_global_state curl_state; | ||
72 | check_curl_working_state working_state; | ||
73 | } check_curl_configure_curl_wrapper; | ||
74 | |||
75 | check_curl_configure_curl_wrapper check_curl_configure_curl(check_curl_static_curl_config config, | ||
76 | check_curl_working_state working_state, | ||
77 | bool check_cert, | ||
78 | bool on_redirect_dependent, | ||
79 | int follow_method, int max_depth); | ||
80 | |||
81 | void handle_curl_option_return_code(CURLcode res, const char *option); | ||
82 | |||
83 | int curlhelp_initwritebuffer(curlhelp_write_curlbuf **buf); | ||
84 | size_t curlhelp_buffer_write_callback(void * /*buffer*/, size_t /*size*/, size_t /*nmemb*/, | ||
85 | void * /*stream*/); | ||
86 | void curlhelp_freewritebuffer(curlhelp_write_curlbuf * /*buf*/); | ||
87 | |||
88 | int curlhelp_initreadbuffer(curlhelp_read_curlbuf **buf, const char * /*data*/, size_t /*datalen*/); | ||
89 | size_t curlhelp_buffer_read_callback(void * /*buffer*/, size_t /*size*/, size_t /*nmemb*/, | ||
90 | void * /*stream*/); | ||
91 | void curlhelp_freereadbuffer(curlhelp_read_curlbuf * /*buf*/); | ||
92 | |||
93 | curlhelp_ssl_library curlhelp_get_ssl_library(void); | ||
94 | const char *curlhelp_get_ssl_library_string(curlhelp_ssl_library /*ssl_library*/); | ||
95 | |||
96 | typedef union { | ||
97 | struct curl_slist *to_info; | ||
98 | struct curl_certinfo *to_certinfo; | ||
99 | } cert_ptr_union; | ||
100 | int net_noopenssl_check_certificate(cert_ptr_union *, int, int); | ||
101 | |||
102 | int curlhelp_parse_statusline(const char * /*buf*/, curlhelp_statusline * /*status_line*/); | ||
103 | void curlhelp_free_statusline(curlhelp_statusline * /*status_line*/); | ||
104 | |||
105 | char *get_header_value(const struct phr_header *headers, size_t nof_headers, const char *header); | ||
106 | mp_subcheck check_document_dates(const curlhelp_write_curlbuf * /*header_buf*/, | ||
107 | int /*maximum_age*/); | ||
108 | size_t get_content_length(const curlhelp_write_curlbuf *header_buf, | ||
109 | const curlhelp_write_curlbuf *body_buf); | ||
110 | int lookup_host(const char *host, char *buf, size_t buflen, sa_family_t addr_family); | ||
111 | CURLcode sslctxfun(CURL *curl, SSL_CTX *sslctx, void *parm); | ||
112 | |||
113 | #define INET_ADDR_MAX_SIZE INET6_ADDRSTRLEN | ||
114 | const char *strrstr2(const char *haystack, const char *needle); | ||
115 | |||
116 | void cleanup(check_curl_global_state global_state); | ||
117 | |||
118 | bool expected_statuscode(const char *reply, const char *statuscodes); | ||
119 | char *string_statuscode(int major, int minor); | ||
120 | |||
121 | void test_file(char *path); | ||
122 | mp_subcheck check_curl_certificate_checks(CURL *curl, X509 *cert, int warn_days_till_exp, | ||
123 | int crit_days_till_exp); | ||
124 | char *fmt_url(check_curl_working_state workingState); | ||
diff --git a/plugins/check_curl.d/config.h b/plugins/check_curl.d/config.h new file mode 100644 index 00000000..f51b2ee9 --- /dev/null +++ b/plugins/check_curl.d/config.h | |||
@@ -0,0 +1,116 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include "../common.h" | ||
5 | #include "../../lib/states.h" | ||
6 | #include "../../lib/thresholds.h" | ||
7 | #include <stddef.h> | ||
8 | #include <string.h> | ||
9 | #include <sys/socket.h> | ||
10 | #include "curl/curl.h" | ||
11 | #include "perfdata.h" | ||
12 | #include "regex.h" | ||
13 | |||
14 | enum { | ||
15 | MAX_RE_SIZE = 1024, | ||
16 | HTTP_PORT = 80, | ||
17 | HTTPS_PORT = 443, | ||
18 | MAX_PORT = 65535, | ||
19 | DEFAULT_MAX_REDIRS = 15 | ||
20 | }; | ||
21 | |||
22 | enum { | ||
23 | FOLLOW_HTTP_CURL = 0, | ||
24 | FOLLOW_LIBCURL = 1 | ||
25 | }; | ||
26 | |||
27 | enum { | ||
28 | STICKY_NONE = 0, | ||
29 | STICKY_HOST = 1, | ||
30 | STICKY_PORT = 2 | ||
31 | }; | ||
32 | |||
33 | #define HTTP_EXPECT "HTTP/" | ||
34 | #define DEFAULT_BUFFER_SIZE 2048 | ||
35 | #define DEFAULT_SERVER_URL "/" | ||
36 | |||
37 | typedef struct { | ||
38 | char *server_address; | ||
39 | char *server_url; | ||
40 | char *host_name; | ||
41 | |||
42 | char *http_method; | ||
43 | |||
44 | char *http_post_data; | ||
45 | |||
46 | unsigned short virtualPort; | ||
47 | unsigned short serverPort; | ||
48 | |||
49 | bool use_ssl; | ||
50 | bool no_body; | ||
51 | } check_curl_working_state; | ||
52 | |||
53 | check_curl_working_state check_curl_working_state_init(); | ||
54 | |||
55 | typedef struct { | ||
56 | bool automatic_decompression; | ||
57 | bool haproxy_protocol; | ||
58 | long socket_timeout; | ||
59 | sa_family_t sin_family; | ||
60 | int curl_http_version; | ||
61 | char **http_opt_headers; | ||
62 | size_t http_opt_headers_count; | ||
63 | int ssl_version; | ||
64 | char *client_cert; | ||
65 | char *client_privkey; | ||
66 | char *ca_cert; | ||
67 | bool verify_peer_and_host; | ||
68 | char user_agent[DEFAULT_BUFFER_SIZE]; | ||
69 | char proxy_auth[MAX_INPUT_BUFFER]; | ||
70 | char user_auth[MAX_INPUT_BUFFER]; | ||
71 | char *http_content_type; | ||
72 | char *cookie_jar_file; | ||
73 | } check_curl_static_curl_config; | ||
74 | |||
75 | typedef struct { | ||
76 | check_curl_working_state initial_config; | ||
77 | |||
78 | check_curl_static_curl_config curl_config; | ||
79 | int max_depth; | ||
80 | int followmethod; | ||
81 | int followsticky; | ||
82 | |||
83 | int maximum_age; | ||
84 | |||
85 | // the original regex string from the command line | ||
86 | char regexp[MAX_RE_SIZE]; | ||
87 | |||
88 | // the compiled regex for usage later | ||
89 | regex_t compiled_regex; | ||
90 | |||
91 | mp_state_enum state_regex; | ||
92 | bool invert_regex; | ||
93 | bool check_cert; | ||
94 | bool continue_after_check_cert; | ||
95 | int days_till_exp_warn; | ||
96 | int days_till_exp_crit; | ||
97 | mp_thresholds thlds; | ||
98 | mp_range page_length_limits; | ||
99 | bool page_length_limits_is_set; | ||
100 | struct { | ||
101 | char string[MAX_INPUT_BUFFER]; | ||
102 | bool is_present; | ||
103 | } server_expect; | ||
104 | char string_expect[MAX_INPUT_BUFFER]; | ||
105 | char header_expect[MAX_INPUT_BUFFER]; | ||
106 | mp_state_enum on_redirect_result_state; | ||
107 | bool on_redirect_dependent; | ||
108 | |||
109 | bool show_extended_perfdata; | ||
110 | bool show_body; | ||
111 | |||
112 | bool output_format_is_set; | ||
113 | mp_output_format output_format; | ||
114 | } check_curl_config; | ||
115 | |||
116 | check_curl_config check_curl_config_init(); | ||
diff --git a/plugins/check_dbi.c b/plugins/check_dbi.c index 9efcd1cb..468ded31 100644 --- a/plugins/check_dbi.c +++ b/plugins/check_dbi.c | |||
@@ -71,8 +71,9 @@ static double timediff(struct timeval /*start*/, struct timeval /*end*/); | |||
71 | 71 | ||
72 | static void np_dbi_print_error(dbi_conn /*conn*/, char * /*fmt*/, ...); | 72 | static void np_dbi_print_error(dbi_conn /*conn*/, char * /*fmt*/, ...); |
73 | 73 | ||
74 | static mp_state_enum do_query(dbi_conn /*conn*/, const char ** /*res_val_str*/, double * /*res_val*/, double * /*res_time*/, mp_dbi_metric /*metric*/, | 74 | static mp_state_enum do_query(dbi_conn /*conn*/, const char ** /*res_val_str*/, |
75 | mp_dbi_type /*type*/, char * /*np_dbi_query*/); | 75 | double * /*res_val*/, double * /*res_time*/, mp_dbi_metric /*metric*/, |
76 | mp_dbi_type /*type*/, char * /*np_dbi_query*/); | ||
76 | 77 | ||
77 | int main(int argc, char **argv) { | 78 | int main(int argc, char **argv) { |
78 | int status = STATE_UNKNOWN; | 79 | int status = STATE_UNKNOWN; |
@@ -118,7 +119,8 @@ int main(int argc, char **argv) { | |||
118 | dbi_inst *instance_p = {0}; | 119 | dbi_inst *instance_p = {0}; |
119 | 120 | ||
120 | if (dbi_initialize_r(NULL, instance_p) < 0) { | 121 | if (dbi_initialize_r(NULL, instance_p) < 0) { |
121 | printf("UNKNOWN - failed to initialize DBI; possibly you don't have any drivers installed.\n"); | 122 | printf( |
123 | "UNKNOWN - failed to initialize DBI; possibly you don't have any drivers installed.\n"); | ||
122 | return STATE_UNKNOWN; | 124 | return STATE_UNKNOWN; |
123 | } | 125 | } |
124 | 126 | ||
@@ -133,10 +135,12 @@ int main(int argc, char **argv) { | |||
133 | 135 | ||
134 | driver = dbi_driver_open_r(config.dbi_driver, instance_p); | 136 | driver = dbi_driver_open_r(config.dbi_driver, instance_p); |
135 | if (!driver) { | 137 | if (!driver) { |
136 | printf("UNKNOWN - failed to open DBI driver '%s'; possibly it's not installed.\n", config.dbi_driver); | 138 | printf("UNKNOWN - failed to open DBI driver '%s'; possibly it's not installed.\n", |
139 | config.dbi_driver); | ||
137 | 140 | ||
138 | printf("Known drivers:\n"); | 141 | printf("Known drivers:\n"); |
139 | for (driver = dbi_driver_list_r(NULL, instance_p); driver; driver = dbi_driver_list_r(driver, instance_p)) { | 142 | for (driver = dbi_driver_list_r(NULL, instance_p); driver; |
143 | driver = dbi_driver_list_r(driver, instance_p)) { | ||
140 | printf(" - %s\n", dbi_driver_get_name(driver)); | 144 | printf(" - %s\n", dbi_driver_get_name(driver)); |
141 | } | 145 | } |
142 | return STATE_UNKNOWN; | 146 | return STATE_UNKNOWN; |
@@ -156,7 +160,8 @@ int main(int argc, char **argv) { | |||
156 | const char *opt; | 160 | const char *opt; |
157 | 161 | ||
158 | if (verbose > 1) { | 162 | if (verbose > 1) { |
159 | printf("Setting DBI driver option '%s' to '%s'\n", config.dbi_options[i].key, config.dbi_options[i].value); | 163 | printf("Setting DBI driver option '%s' to '%s'\n", config.dbi_options[i].key, |
164 | config.dbi_options[i].value); | ||
160 | } | 165 | } |
161 | 166 | ||
162 | if (!dbi_conn_set_option(conn, config.dbi_options[i].key, config.dbi_options[i].value)) { | 167 | if (!dbi_conn_set_option(conn, config.dbi_options[i].key, config.dbi_options[i].value)) { |
@@ -164,10 +169,12 @@ int main(int argc, char **argv) { | |||
164 | } | 169 | } |
165 | /* else: status != 0 */ | 170 | /* else: status != 0 */ |
166 | 171 | ||
167 | np_dbi_print_error(conn, "UNKNOWN - failed to set option '%s' to '%s'", config.dbi_options[i].key, config.dbi_options[i].value); | 172 | np_dbi_print_error(conn, "UNKNOWN - failed to set option '%s' to '%s'", |
173 | config.dbi_options[i].key, config.dbi_options[i].value); | ||
168 | printf("Known driver options:\n"); | 174 | printf("Known driver options:\n"); |
169 | 175 | ||
170 | for (opt = dbi_conn_get_option_list(conn, NULL); opt; opt = dbi_conn_get_option_list(conn, opt)) { | 176 | for (opt = dbi_conn_get_option_list(conn, NULL); opt; |
177 | opt = dbi_conn_get_option_list(conn, opt)) { | ||
171 | printf(" - %s\n", opt); | 178 | printf(" - %s\n", opt); |
172 | } | 179 | } |
173 | dbi_conn_close(conn); | 180 | dbi_conn_close(conn); |
@@ -230,14 +237,16 @@ int main(int argc, char **argv) { | |||
230 | } | 237 | } |
231 | 238 | ||
232 | if (dbi_conn_select_db(conn, config.dbi_database)) { | 239 | if (dbi_conn_select_db(conn, config.dbi_database)) { |
233 | np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", config.dbi_database); | 240 | np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", |
241 | config.dbi_database); | ||
234 | return STATE_UNKNOWN; | 242 | return STATE_UNKNOWN; |
235 | } | 243 | } |
236 | } | 244 | } |
237 | 245 | ||
238 | if (config.dbi_query) { | 246 | if (config.dbi_query) { |
239 | /* execute query */ | 247 | /* execute query */ |
240 | status = do_query(conn, &query_val_str, &query_val, &query_time, config.metric, config.type, config.dbi_query); | 248 | status = do_query(conn, &query_val_str, &query_val, &query_time, config.metric, config.type, |
249 | config.dbi_query); | ||
241 | if (status != STATE_OK) { | 250 | if (status != STATE_OK) { |
242 | /* do_query prints an error message in this case */ | 251 | /* do_query prints an error message in this case */ |
243 | return status; | 252 | return status; |
@@ -281,7 +290,8 @@ int main(int argc, char **argv) { | |||
281 | /* In case of METRIC_QUERY_RESULT, isnan(query_val) indicates an error | 290 | /* In case of METRIC_QUERY_RESULT, isnan(query_val) indicates an error |
282 | * which should have been reported and handled (abort) before | 291 | * which should have been reported and handled (abort) before |
283 | * ... unless we expected a string to be returned */ | 292 | * ... unless we expected a string to be returned */ |
284 | assert((config.metric != METRIC_QUERY_RESULT) || (!isnan(query_val)) || (config.type == TYPE_STRING)); | 293 | assert((config.metric != METRIC_QUERY_RESULT) || (!isnan(query_val)) || |
294 | (config.type == TYPE_STRING)); | ||
285 | 295 | ||
286 | assert((config.type != TYPE_STRING) || (config.expect || config.expect_re_str)); | 296 | assert((config.type != TYPE_STRING) || (config.expect || config.expect_re_str)); |
287 | 297 | ||
@@ -289,12 +299,14 @@ int main(int argc, char **argv) { | |||
289 | if (config.dbi_query) { | 299 | if (config.dbi_query) { |
290 | if (config.type == TYPE_STRING) { | 300 | if (config.type == TYPE_STRING) { |
291 | assert(config.expect || config.expect_re_str); | 301 | assert(config.expect || config.expect_re_str); |
292 | printf(", '%s' returned '%s' in %fs", config.dbi_query, query_val_str ? query_val_str : "<nothing>", query_time); | 302 | printf(", '%s' returned '%s' in %fs", config.dbi_query, |
303 | query_val_str ? query_val_str : "<nothing>", query_time); | ||
293 | if (status != STATE_OK) { | 304 | if (status != STATE_OK) { |
294 | if (config.expect) { | 305 | if (config.expect) { |
295 | printf(" (expected '%s')", config.expect); | 306 | printf(" (expected '%s')", config.expect); |
296 | } else if (config.expect_re_str) { | 307 | } else if (config.expect_re_str) { |
297 | printf(" (expected regex /%s/%s)", config.expect_re_str, ((config.expect_re_cflags & REG_ICASE) ? "i" : "")); | 308 | printf(" (expected regex /%s/%s)", config.expect_re_str, |
309 | ((config.expect_re_cflags & REG_ICASE) ? "i" : "")); | ||
298 | } | 310 | } |
299 | } | 311 | } |
300 | } else if (isnan(query_val)) { | 312 | } else if (isnan(query_val)) { |
@@ -304,18 +316,31 @@ int main(int argc, char **argv) { | |||
304 | } | 316 | } |
305 | } | 317 | } |
306 | 318 | ||
307 | printf(" | conntime=%fs;%s;%s;0; server_version=%u;%s;%s;0;", conn_time, | 319 | printf( |
308 | ((config.metric == METRIC_CONN_TIME) && config.warning_range) ? config.warning_range : "", | 320 | " | conntime=%fs;%s;%s;0; server_version=%u;%s;%s;0;", conn_time, |
309 | ((config.metric == METRIC_CONN_TIME) && config.critical_range) ? config.critical_range : "", server_version, | 321 | ((config.metric == METRIC_CONN_TIME) && config.warning_range) ? config.warning_range : "", |
310 | ((config.metric == METRIC_SERVER_VERSION) && config.warning_range) ? config.warning_range : "", | 322 | ((config.metric == METRIC_CONN_TIME) && config.critical_range) ? config.critical_range : "", |
311 | ((config.metric == METRIC_SERVER_VERSION) && config.critical_range) ? config.critical_range : ""); | 323 | server_version, |
324 | ((config.metric == METRIC_SERVER_VERSION) && config.warning_range) ? config.warning_range | ||
325 | : "", | ||
326 | ((config.metric == METRIC_SERVER_VERSION) && config.critical_range) ? config.critical_range | ||
327 | : ""); | ||
312 | if (config.dbi_query) { | 328 | if (config.dbi_query) { |
313 | if (!isnan(query_val)) { /* this is also true when -e is used */ | 329 | if (!isnan(query_val)) { /* this is also true when -e is used */ |
314 | printf(" query=%f;%s;%s;;", query_val, ((config.metric == METRIC_QUERY_RESULT) && config.warning_range) ? config.warning_range : "", | 330 | printf(" query=%f;%s;%s;;", query_val, |
315 | ((config.metric == METRIC_QUERY_RESULT) && config.critical_range) ? config.critical_range : ""); | 331 | ((config.metric == METRIC_QUERY_RESULT) && config.warning_range) |
332 | ? config.warning_range | ||
333 | : "", | ||
334 | ((config.metric == METRIC_QUERY_RESULT) && config.critical_range) | ||
335 | ? config.critical_range | ||
336 | : ""); | ||
316 | } | 337 | } |
317 | printf(" querytime=%fs;%s;%s;0;", query_time, ((config.metric == METRIC_QUERY_TIME) && config.warning_range) ? config.warning_range : "", | 338 | printf(" querytime=%fs;%s;%s;0;", query_time, |
318 | ((config.metric == METRIC_QUERY_TIME) && config.critical_range) ? config.critical_range : ""); | 339 | ((config.metric == METRIC_QUERY_TIME) && config.warning_range) ? config.warning_range |
340 | : "", | ||
341 | ((config.metric == METRIC_QUERY_TIME) && config.critical_range) | ||
342 | ? config.critical_range | ||
343 | : ""); | ||
319 | } | 344 | } |
320 | printf("\n"); | 345 | printf("\n"); |
321 | return status; | 346 | return status; |
@@ -442,7 +467,8 @@ check_dbi_config_wrapper process_arguments(int argc, char **argv) { | |||
442 | *value = '\0'; | 467 | *value = '\0'; |
443 | ++value; | 468 | ++value; |
444 | 469 | ||
445 | new = realloc(result.config.dbi_options, (result.config.dbi_options_num + 1) * sizeof(*new)); | 470 | new = realloc(result.config.dbi_options, |
471 | (result.config.dbi_options_num + 1) * sizeof(*new)); | ||
446 | if (!new) { | 472 | if (!new) { |
447 | printf("UNKNOWN - failed to reallocate memory\n"); | 473 | printf("UNKNOWN - failed to reallocate memory\n"); |
448 | exit(STATE_UNKNOWN); | 474 | exit(STATE_UNKNOWN); |
@@ -464,7 +490,8 @@ check_dbi_config_wrapper process_arguments(int argc, char **argv) { | |||
464 | } | 490 | } |
465 | } | 491 | } |
466 | 492 | ||
467 | set_thresholds(&result.config.dbi_thresholds, result.config.warning_range, result.config.critical_range); | 493 | set_thresholds(&result.config.dbi_thresholds, result.config.warning_range, |
494 | result.config.critical_range); | ||
468 | 495 | ||
469 | return validate_arguments(result); | 496 | return validate_arguments(result); |
470 | } | 497 | } |
@@ -474,21 +501,28 @@ check_dbi_config_wrapper validate_arguments(check_dbi_config_wrapper config_wrap | |||
474 | usage("Must specify a DBI driver"); | 501 | usage("Must specify a DBI driver"); |
475 | } | 502 | } |
476 | 503 | ||
477 | if (((config_wrapper.config.metric == METRIC_QUERY_RESULT) || (config_wrapper.config.metric == METRIC_QUERY_TIME)) && | 504 | if (((config_wrapper.config.metric == METRIC_QUERY_RESULT) || |
505 | (config_wrapper.config.metric == METRIC_QUERY_TIME)) && | ||
478 | (!config_wrapper.config.dbi_query)) { | 506 | (!config_wrapper.config.dbi_query)) { |
479 | usage("Must specify a query to execute (metric == QUERY_RESULT)"); | 507 | usage("Must specify a query to execute (metric == QUERY_RESULT)"); |
480 | } | 508 | } |
481 | 509 | ||
482 | if ((config_wrapper.config.metric != METRIC_CONN_TIME) && (config_wrapper.config.metric != METRIC_SERVER_VERSION) && | 510 | if ((config_wrapper.config.metric != METRIC_CONN_TIME) && |
483 | (config_wrapper.config.metric != METRIC_QUERY_RESULT) && (config_wrapper.config.metric != METRIC_QUERY_TIME)) { | 511 | (config_wrapper.config.metric != METRIC_SERVER_VERSION) && |
512 | (config_wrapper.config.metric != METRIC_QUERY_RESULT) && | ||
513 | (config_wrapper.config.metric != METRIC_QUERY_TIME)) { | ||
484 | usage("Invalid metric specified"); | 514 | usage("Invalid metric specified"); |
485 | } | 515 | } |
486 | 516 | ||
487 | if (config_wrapper.config.expect && (config_wrapper.config.warning_range || config_wrapper.config.critical_range || config_wrapper.config.expect_re_str)) { | 517 | if (config_wrapper.config.expect && |
518 | (config_wrapper.config.warning_range || config_wrapper.config.critical_range || | ||
519 | config_wrapper.config.expect_re_str)) { | ||
488 | usage("Do not mix -e and -w/-c/-r/-R"); | 520 | usage("Do not mix -e and -w/-c/-r/-R"); |
489 | } | 521 | } |
490 | 522 | ||
491 | if (config_wrapper.config.expect_re_str && (config_wrapper.config.warning_range || config_wrapper.config.critical_range || config_wrapper.config.expect)) { | 523 | if (config_wrapper.config.expect_re_str && |
524 | (config_wrapper.config.warning_range || config_wrapper.config.critical_range || | ||
525 | config_wrapper.config.expect)) { | ||
492 | usage("Do not mix -r/-R and -w/-c/-e"); | 526 | usage("Do not mix -r/-R and -w/-c/-e"); |
493 | } | 527 | } |
494 | 528 | ||
@@ -496,7 +530,8 @@ check_dbi_config_wrapper validate_arguments(check_dbi_config_wrapper config_wrap | |||
496 | usage("Option -e requires metric QUERY_RESULT"); | 530 | usage("Option -e requires metric QUERY_RESULT"); |
497 | } | 531 | } |
498 | 532 | ||
499 | if (config_wrapper.config.expect_re_str && (config_wrapper.config.metric != METRIC_QUERY_RESULT)) { | 533 | if (config_wrapper.config.expect_re_str && |
534 | (config_wrapper.config.metric != METRIC_QUERY_RESULT)) { | ||
500 | usage("Options -r/-R require metric QUERY_RESULT"); | 535 | usage("Options -r/-R require metric QUERY_RESULT"); |
501 | } | 536 | } |
502 | 537 | ||
@@ -607,7 +642,8 @@ void print_usage(void) { | |||
607 | printf(" [-e <string>] [-r|-R <regex>]\n"); | 642 | printf(" [-e <string>] [-r|-R <regex>]\n"); |
608 | } | 643 | } |
609 | 644 | ||
610 | const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_type, mp_dbi_metric metric, mp_dbi_type type) { | 645 | const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_type, |
646 | mp_dbi_metric metric, mp_dbi_type type) { | ||
611 | const char *str; | 647 | const char *str; |
612 | 648 | ||
613 | if (field_type != DBI_TYPE_STRING) { | 649 | if (field_type != DBI_TYPE_STRING) { |
@@ -630,7 +666,8 @@ const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_ty | |||
630 | return str; | 666 | return str; |
631 | } | 667 | } |
632 | 668 | ||
633 | double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type, mp_dbi_metric metric, mp_dbi_type type) { | 669 | double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type, mp_dbi_metric metric, |
670 | mp_dbi_type type) { | ||
634 | double val = NAN; | 671 | double val = NAN; |
635 | 672 | ||
636 | if (*field_type == DBI_TYPE_INTEGER) { | 673 | if (*field_type == DBI_TYPE_INTEGER) { |
@@ -679,7 +716,8 @@ double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type, mp_d | |||
679 | return val; | 716 | return val; |
680 | } | 717 | } |
681 | 718 | ||
682 | mp_state_enum get_query_result(dbi_conn conn, dbi_result res, const char **res_val_str, double *res_val, mp_dbi_metric metric, mp_dbi_type type) { | 719 | mp_state_enum get_query_result(dbi_conn conn, dbi_result res, const char **res_val_str, |
720 | double *res_val, mp_dbi_metric metric, mp_dbi_type type) { | ||
683 | unsigned short field_type; | 721 | unsigned short field_type; |
684 | double val = NAN; | 722 | double val = NAN; |
685 | 723 | ||
@@ -747,8 +785,8 @@ mp_state_enum get_query_result(dbi_conn conn, dbi_result res, const char **res_v | |||
747 | return STATE_OK; | 785 | return STATE_OK; |
748 | } | 786 | } |
749 | 787 | ||
750 | mp_state_enum do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *res_time, mp_dbi_metric metric, mp_dbi_type type, | 788 | mp_state_enum do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *res_time, |
751 | char *np_dbi_query) { | 789 | mp_dbi_metric metric, mp_dbi_type type, char *np_dbi_query) { |
752 | dbi_result res; | 790 | dbi_result res; |
753 | 791 | ||
754 | struct timeval timeval_start; | 792 | struct timeval timeval_start; |
diff --git a/plugins/check_dig.c b/plugins/check_dig.c index d0903be2..c27e5f13 100644 --- a/plugins/check_dig.c +++ b/plugins/check_dig.c | |||
@@ -81,8 +81,9 @@ int main(int argc, char **argv) { | |||
81 | 81 | ||
82 | char *command_line; | 82 | char *command_line; |
83 | /* get the command to run */ | 83 | /* get the command to run */ |
84 | xasprintf(&command_line, "%s %s %s -p %d @%s %s %s +retry=%d +time=%d", PATH_TO_DIG, config.dig_args, config.query_transport, | 84 | xasprintf(&command_line, "%s %s %s -p %d @%s %s %s +retry=%d +time=%d", PATH_TO_DIG, |
85 | config.server_port, config.dns_server, config.query_address, config.record_type, config.number_tries, timeout_interval_dig); | 85 | config.dig_args, config.query_transport, config.server_port, config.dns_server, |
86 | config.query_address, config.record_type, config.number_tries, timeout_interval_dig); | ||
86 | 87 | ||
87 | alarm(timeout_interval); | 88 | alarm(timeout_interval); |
88 | struct timeval start_time; | 89 | struct timeval start_time; |
@@ -118,8 +119,9 @@ int main(int argc, char **argv) { | |||
118 | printf("%s\n", chld_out.line[i]); | 119 | printf("%s\n", chld_out.line[i]); |
119 | } | 120 | } |
120 | 121 | ||
121 | if (strcasestr(chld_out.line[i], (config.expected_address == NULL ? config.query_address : config.expected_address)) != | 122 | if (strcasestr(chld_out.line[i], (config.expected_address == NULL |
122 | NULL) { | 123 | ? config.query_address |
124 | : config.expected_address)) != NULL) { | ||
123 | msg = chld_out.line[i]; | 125 | msg = chld_out.line[i]; |
124 | result = STATE_OK; | 126 | result = STATE_OK; |
125 | 127 | ||
@@ -174,8 +176,9 @@ int main(int argc, char **argv) { | |||
174 | 176 | ||
175 | printf("DNS %s - %.3f seconds response time (%s)|%s\n", state_text(result), elapsed_time, | 177 | printf("DNS %s - %.3f seconds response time (%s)|%s\n", state_text(result), elapsed_time, |
176 | msg ? msg : _("Probably a non-existent host/domain"), | 178 | msg ? msg : _("Probably a non-existent host/domain"), |
177 | fperfdata("time", elapsed_time, "s", (config.warning_interval > UNDEFINED), config.warning_interval, | 179 | fperfdata("time", elapsed_time, "s", (config.warning_interval > UNDEFINED), |
178 | (config.critical_interval > UNDEFINED), config.critical_interval, true, 0, false, 0)); | 180 | config.warning_interval, (config.critical_interval > UNDEFINED), |
181 | config.critical_interval, true, 0, false, 0)); | ||
179 | exit(result); | 182 | exit(result); |
180 | } | 183 | } |
181 | 184 | ||
@@ -335,7 +338,8 @@ void print_help(void) { | |||
335 | printf(" %s\n", "-T, --record_type=STRING"); | 338 | printf(" %s\n", "-T, --record_type=STRING"); |
336 | printf(" %s\n", _("Record type to lookup (default: A)")); | 339 | printf(" %s\n", _("Record type to lookup (default: A)")); |
337 | printf(" %s\n", "-a, --expected_address=STRING"); | 340 | printf(" %s\n", "-a, --expected_address=STRING"); |
338 | printf(" %s\n", _("An address expected to be in the answer section. If not set, uses whatever")); | 341 | printf(" %s\n", |
342 | _("An address expected to be in the answer section. If not set, uses whatever")); | ||
339 | printf(" %s\n", _("was in -l")); | 343 | printf(" %s\n", _("was in -l")); |
340 | printf(" %s\n", "-A, --dig-arguments=STRING"); | 344 | printf(" %s\n", "-A, --dig-arguments=STRING"); |
341 | printf(" %s\n", _("Pass STRING as argument(s) to dig")); | 345 | printf(" %s\n", _("Pass STRING as argument(s) to dig")); |
diff --git a/plugins/check_disk.c b/plugins/check_disk.c index 037a6f7a..d42b5486 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c | |||
@@ -31,24 +31,35 @@ const char *program_name = "check_disk"; /* Required for coreutils libs */ | |||
31 | const char *copyright = "1999-2024"; | 31 | const char *copyright = "1999-2024"; |
32 | const char *email = "devel@monitoring-plugins.org"; | 32 | const char *email = "devel@monitoring-plugins.org"; |
33 | 33 | ||
34 | #include "states.h" | ||
34 | #include "common.h" | 35 | #include "common.h" |
36 | #include "output.h" | ||
37 | #include "perfdata.h" | ||
38 | #include "utils_base.h" | ||
39 | #include "lib/thresholds.h" | ||
40 | |||
35 | #ifdef HAVE_SYS_STAT_H | 41 | #ifdef HAVE_SYS_STAT_H |
36 | # include <sys/stat.h> | 42 | # include <sys/stat.h> |
37 | #endif | 43 | #endif |
44 | |||
38 | #if HAVE_INTTYPES_H | 45 | #if HAVE_INTTYPES_H |
39 | # include <inttypes.h> | 46 | # include <inttypes.h> |
40 | #endif | 47 | #endif |
48 | |||
41 | #include <assert.h> | 49 | #include <assert.h> |
42 | #include "popen.h" | ||
43 | #include "utils.h" | ||
44 | #include "utils_disk.h" | ||
45 | #include <stdarg.h> | 50 | #include <stdarg.h> |
46 | #include "fsusage.h" | 51 | #include <stdint.h> |
47 | #include "mountlist.h" | ||
48 | #include <float.h> | 52 | #include <float.h> |
53 | #include "./popen.h" | ||
54 | #include "./utils.h" | ||
55 | #include "../gl/fsusage.h" | ||
56 | #include "../gl/mountlist.h" | ||
57 | #include "./check_disk.d/utils_disk.h" | ||
58 | |||
49 | #if HAVE_LIMITS_H | 59 | #if HAVE_LIMITS_H |
50 | # include <limits.h> | 60 | # include <limits.h> |
51 | #endif | 61 | #endif |
62 | |||
52 | #include "regex.h" | 63 | #include "regex.h" |
53 | 64 | ||
54 | #ifdef __CYGWIN__ | 65 | #ifdef __CYGWIN__ |
@@ -57,424 +68,322 @@ const char *email = "devel@monitoring-plugins.org"; | |||
57 | # define ERROR -1 | 68 | # define ERROR -1 |
58 | #endif | 69 | #endif |
59 | 70 | ||
60 | /* If nonzero, show even filesystems with zero size or | ||
61 | uninteresting types. */ | ||
62 | static int show_all_fs = 1; | ||
63 | |||
64 | /* If nonzero, show only local filesystems. */ | ||
65 | static int show_local_fs = 0; | ||
66 | |||
67 | /* If nonzero, show only local filesystems but call stat() on remote ones. */ | ||
68 | static int stat_remote_fs = 0; | ||
69 | |||
70 | /* If positive, the units to use when printing sizes; | ||
71 | if negative, the human-readable base. */ | ||
72 | /* static int output_block_size; */ | ||
73 | |||
74 | /* If nonzero, invoke the `sync' system call before getting any usage data. | ||
75 | Using this option can make df very slow, especially with many or very | ||
76 | busy disks. Note that this may make a difference on some systems -- | ||
77 | SunOs4.1.3, for one. It is *not* necessary on Linux. */ | ||
78 | /* static int require_sync = 0; */ | ||
79 | |||
80 | /* Linked list of filesystem types to display. | ||
81 | If `fs_select_list' is NULL, list all types. | ||
82 | This table is generated dynamically from command-line options, | ||
83 | rather than hardcoding into the program what it thinks are the | ||
84 | valid filesystem types; let the user specify any filesystem type | ||
85 | they want to, and if there are any filesystems of that type, they | ||
86 | will be shown. | ||
87 | |||
88 | Some filesystem types: | ||
89 | 4.2 4.3 ufs nfs swap ignore io vm efs dbg */ | ||
90 | |||
91 | /* static struct parameter_list *fs_select_list; */ | ||
92 | |||
93 | /* Linked list of filesystem types to omit. | ||
94 | If the list is empty, don't exclude any types. */ | ||
95 | static struct regex_list *fs_exclude_list = NULL; | ||
96 | |||
97 | /* Linked list of filesystem types to check. | ||
98 | If the list is empty, include all types. */ | ||
99 | static struct regex_list *fs_include_list; | ||
100 | |||
101 | static struct name_list *dp_exclude_list; | ||
102 | |||
103 | static struct parameter_list *path_select_list = NULL; | ||
104 | |||
105 | /* Linked list of mounted filesystems. */ | ||
106 | static struct mount_entry *mount_list; | ||
107 | |||
108 | /* For long options that have no equivalent short option, use a | ||
109 | non-character as a pseudo short option, starting with CHAR_MAX + 1. */ | ||
110 | enum { | ||
111 | SYNC_OPTION = CHAR_MAX + 1, | ||
112 | NO_SYNC_OPTION, | ||
113 | BLOCK_SIZE_OPTION | ||
114 | }; | ||
115 | |||
116 | #ifdef _AIX | 71 | #ifdef _AIX |
117 | # pragma alloca | 72 | # pragma alloca |
118 | #endif | 73 | #endif |
119 | 74 | ||
120 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 75 | typedef struct { |
121 | static void set_all_thresholds(struct parameter_list *path); | 76 | int errorcode; |
122 | static void print_help(void); | 77 | check_disk_config config; |
78 | } check_disk_config_wrapper; | ||
79 | static check_disk_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
80 | |||
81 | static void set_all_thresholds(parameter_list_elem *path, char *warn_freespace_units, | ||
82 | char *crit_freespace_units, char *warn_freespace_percent, | ||
83 | char *crit_freespace_percent, char *warn_freeinodes_percent, | ||
84 | char *crit_freeinodes_percent); | ||
85 | static double calculate_percent(uintmax_t /*value*/, uintmax_t /*total*/); | ||
86 | static bool stat_path(parameter_list_elem * /*parameters*/, bool /*ignore_missing*/); | ||
87 | |||
88 | /* | ||
89 | * Puts the values from a struct fs_usage into a parameter_list with an additional flag to control | ||
90 | * how reserved and inodes should be judged (ignored or not) | ||
91 | */ | ||
92 | static parameter_list_elem get_path_stats(parameter_list_elem parameters, struct fs_usage fsp, | ||
93 | bool freespace_ignore_reserved); | ||
94 | static mp_subcheck evaluate_filesystem(measurement_unit measurement_unit, | ||
95 | bool display_inodes_perfdata, byte_unit unit); | ||
96 | |||
123 | void print_usage(void); | 97 | void print_usage(void); |
124 | static double calculate_percent(uintmax_t, uintmax_t); | 98 | static void print_help(void); |
125 | static bool stat_path(struct parameter_list *p); | ||
126 | static void get_stats(struct parameter_list *p, struct fs_usage *fsp); | ||
127 | static void get_path_stats(struct parameter_list *p, struct fs_usage *fsp); | ||
128 | 99 | ||
129 | static char *units; | ||
130 | static uintmax_t mult = 1024 * 1024; | ||
131 | static int verbose = 0; | 100 | static int verbose = 0; |
132 | static bool erronly = false; | ||
133 | static bool display_mntp = false; | ||
134 | static bool exact_match = false; | ||
135 | static bool ignore_missing = false; | ||
136 | static bool freespace_ignore_reserved = false; | ||
137 | static bool display_inodes_perfdata = false; | ||
138 | static char *warn_freespace_units = NULL; | ||
139 | static char *crit_freespace_units = NULL; | ||
140 | static char *warn_freespace_percent = NULL; | ||
141 | static char *crit_freespace_percent = NULL; | ||
142 | static char *warn_usedspace_units = NULL; | ||
143 | static char *crit_usedspace_units = NULL; | ||
144 | static char *warn_usedspace_percent = NULL; | ||
145 | static char *crit_usedspace_percent = NULL; | ||
146 | static char *warn_usedinodes_percent = NULL; | ||
147 | static char *crit_usedinodes_percent = NULL; | ||
148 | static char *warn_freeinodes_percent = NULL; | ||
149 | static char *crit_freeinodes_percent = NULL; | ||
150 | static bool path_selected = false; | ||
151 | static bool path_ignored = false; | ||
152 | static char *group = NULL; | ||
153 | static struct stat *stat_buf; | ||
154 | static struct name_list *seen = NULL; | ||
155 | 101 | ||
156 | int main(int argc, char **argv) { | 102 | // This would not be necessary in C23!! |
157 | int result = STATE_UNKNOWN; | 103 | const byte_unit Bytes_Factor = 1; |
158 | int disk_result = STATE_UNKNOWN; | 104 | const byte_unit KibiBytes_factor = 1024; |
159 | char *output = NULL; | 105 | const byte_unit MebiBytes_factor = 1048576; |
160 | char *ignored = NULL; | 106 | const byte_unit GibiBytes_factor = 1073741824; |
161 | char *details = NULL; | 107 | const byte_unit TebiBytes_factor = 1099511627776; |
162 | char *perf = NULL; | 108 | const byte_unit PebiBytes_factor = 1125899906842624; |
163 | char *perf_ilabel = NULL; | 109 | const byte_unit ExbiBytes_factor = 1152921504606846976; |
164 | char *preamble = " - free space:"; | 110 | const byte_unit KiloBytes_factor = 1000; |
165 | char *ignored_preamble = " - ignored paths:"; | 111 | const byte_unit MegaBytes_factor = 1000000; |
166 | char *flag_header = NULL; | 112 | const byte_unit GigaBytes_factor = 1000000000; |
167 | int temp_result = STATE_UNKNOWN; | 113 | const byte_unit TeraBytes_factor = 1000000000000; |
168 | 114 | const byte_unit PetaBytes_factor = 1000000000000000; | |
169 | struct mount_entry *me = NULL; | 115 | const byte_unit ExaBytes_factor = 1000000000000000000; |
170 | struct fs_usage fsp = {0}; | ||
171 | struct parameter_list *temp_list = NULL; | ||
172 | struct parameter_list *path = NULL; | ||
173 | |||
174 | #ifdef __CYGWIN__ | ||
175 | char mountdir[32]; | ||
176 | #endif | ||
177 | |||
178 | output = strdup(""); | ||
179 | ignored = strdup(""); | ||
180 | details = strdup(""); | ||
181 | perf = strdup(""); | ||
182 | perf_ilabel = strdup(""); | ||
183 | stat_buf = malloc(sizeof *stat_buf); | ||
184 | 116 | ||
117 | int main(int argc, char **argv) { | ||
185 | setlocale(LC_ALL, ""); | 118 | setlocale(LC_ALL, ""); |
186 | bindtextdomain(PACKAGE, LOCALEDIR); | 119 | bindtextdomain(PACKAGE, LOCALEDIR); |
187 | textdomain(PACKAGE); | 120 | textdomain(PACKAGE); |
188 | 121 | ||
189 | mount_list = read_file_system_list(0); | 122 | #ifdef __CYGWIN__ |
123 | char mountdir[32]; | ||
124 | #endif | ||
190 | 125 | ||
191 | /* Parse extra opts if any */ | 126 | // Parse extra opts if any |
192 | argv = np_extra_opts(&argc, argv, progname); | 127 | argv = np_extra_opts(&argc, argv, progname); |
193 | 128 | ||
194 | if (process_arguments(argc, argv) == ERROR) | 129 | check_disk_config_wrapper tmp_config = process_arguments(argc, argv); |
130 | if (tmp_config.errorcode == ERROR) { | ||
195 | usage4(_("Could not parse arguments")); | 131 | usage4(_("Could not parse arguments")); |
132 | } | ||
196 | 133 | ||
197 | /* If a list of paths has not been selected, find entire | 134 | check_disk_config config = tmp_config.config; |
198 | mount list and create list of paths | 135 | |
199 | */ | 136 | if (config.output_format_is_set) { |
200 | if (path_selected == false && path_ignored == false) { | 137 | mp_set_format(config.output_format); |
201 | for (me = mount_list; me; me = me->me_next) { | ||
202 | if (!(path = np_find_parameter(path_select_list, me->me_mountdir))) { | ||
203 | path = np_add_parameter(&path_select_list, me->me_mountdir); | ||
204 | } | ||
205 | path->best_match = me; | ||
206 | path->group = group; | ||
207 | set_all_thresholds(path); | ||
208 | } | ||
209 | } | 138 | } |
210 | 139 | ||
211 | if (path_ignored == false) { | 140 | if (config.erronly) { |
212 | np_set_best_match(path_select_list, mount_list, exact_match); | 141 | mp_set_level_of_detail(MP_DETAIL_NON_OK_ONLY); |
213 | } | 142 | } |
214 | 143 | ||
215 | /* Error if no match found for specified paths */ | 144 | if (!config.path_ignored) { |
216 | temp_list = path_select_list; | 145 | mp_int_fs_list_set_best_match(config.path_select_list, config.mount_list, |
146 | config.exact_match); | ||
147 | } | ||
217 | 148 | ||
218 | while (path_select_list) { | 149 | // Error if no match found for specified paths |
219 | if (!path_select_list->best_match && ignore_missing == true) { | 150 | for (parameter_list_elem *elem = config.path_select_list.first; elem;) { |
220 | /* If the first element will be deleted, the temp_list must be updated with the new start address as well */ | 151 | if (!elem->best_match && config.ignore_missing) { |
221 | if (path_select_list == temp_list) { | ||
222 | temp_list = path_select_list->name_next; | ||
223 | } | ||
224 | /* Add path argument to list of ignored paths to inform about missing paths being ignored and not alerted */ | ||
225 | xasprintf(&ignored, "%s %s;", ignored, path_select_list->name); | ||
226 | /* Delete the path from the list so that it is not stat-checked later in the code. */ | 152 | /* Delete the path from the list so that it is not stat-checked later in the code. */ |
227 | path_select_list = np_del_parameter(path_select_list, path_select_list->name_prev); | 153 | elem = mp_int_fs_list_del(&config.path_select_list, elem); |
228 | } else if (!path_select_list->best_match) { | 154 | continue; |
155 | } | ||
156 | if (!elem->best_match) { | ||
229 | /* Without --ignore-missing option, exit with Critical state. */ | 157 | /* Without --ignore-missing option, exit with Critical state. */ |
230 | die(STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), path_select_list->name); | 158 | die(STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), elem->name); |
231 | } else { | ||
232 | /* Continue jumping through the list */ | ||
233 | path_select_list = path_select_list->name_next; | ||
234 | } | 159 | } |
235 | } | ||
236 | 160 | ||
237 | path_select_list = temp_list; | 161 | elem = mp_int_fs_list_get_next(elem); |
162 | } | ||
238 | 163 | ||
239 | if (!path_select_list && ignore_missing == true) { | 164 | mp_check overall = mp_check_init(); |
240 | result = STATE_OK; | 165 | if (config.path_select_list.length == 0) { |
241 | if (verbose >= 2) { | 166 | mp_subcheck none_sc = mp_subcheck_init(); |
242 | printf("None of the provided paths were found\n"); | 167 | xasprintf(&none_sc.output, "No filesystems were found for the provided parameters"); |
168 | if (config.ignore_missing) { | ||
169 | none_sc = mp_set_subcheck_state(none_sc, STATE_OK); | ||
170 | } else { | ||
171 | none_sc = mp_set_subcheck_state(none_sc, STATE_UNKNOWN); | ||
172 | if (verbose >= 2) { | ||
173 | printf("None of the provided paths were found\n"); | ||
174 | } | ||
243 | } | 175 | } |
176 | mp_add_subcheck_to_check(&overall, none_sc); | ||
177 | mp_exit(overall); | ||
244 | } | 178 | } |
245 | 179 | ||
246 | /* Process for every path in list */ | 180 | // Filter list first |
247 | for (path = path_select_list; path; path = path->name_next) { | 181 | for (parameter_list_elem *path = config.path_select_list.first; path;) { |
248 | if (verbose >= 3 && path->freespace_percent->warning != NULL && path->freespace_percent->critical != NULL) | 182 | if (!path->best_match) { |
249 | printf("Thresholds(pct) for %s warn: %f crit %f\n", path->name, path->freespace_percent->warning->end, | 183 | path = mp_int_fs_list_del(&config.path_select_list, path); |
250 | path->freespace_percent->critical->end); | ||
251 | |||
252 | if (verbose >= 3 && path->group != NULL) | ||
253 | printf("Group of %s: %s\n", path->name, path->group); | ||
254 | |||
255 | /* reset disk result */ | ||
256 | disk_result = STATE_UNKNOWN; | ||
257 | |||
258 | me = path->best_match; | ||
259 | |||
260 | if (!me) { | ||
261 | continue; | 184 | continue; |
262 | } | 185 | } |
263 | 186 | ||
187 | struct mount_entry *mount_entry = path->best_match; | ||
188 | |||
264 | #ifdef __CYGWIN__ | 189 | #ifdef __CYGWIN__ |
265 | if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) | 190 | if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) { |
191 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
266 | continue; | 192 | continue; |
193 | } | ||
194 | |||
195 | char *mountdir = NULL; | ||
267 | snprintf(mountdir, sizeof(mountdir), "%s:\\", me->me_mountdir + 10); | 196 | snprintf(mountdir, sizeof(mountdir), "%s:\\", me->me_mountdir + 10); |
268 | if (GetDriveType(mountdir) != DRIVE_FIXED) | 197 | if (GetDriveType(mountdir) != DRIVE_FIXED) { |
269 | me->me_remote = 1; | 198 | mount_entry->me_remote = 1; |
199 | } | ||
270 | #endif | 200 | #endif |
271 | /* Filters */ | ||
272 | 201 | ||
273 | /* Remove filesystems already seen */ | 202 | /* Remove filesystems already seen */ |
274 | if (np_seen_name(seen, me->me_mountdir)) { | 203 | if (np_seen_name(config.seen, mount_entry->me_mountdir)) { |
204 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
275 | continue; | 205 | continue; |
276 | } | 206 | } |
277 | np_add_name(&seen, me->me_mountdir); | ||
278 | 207 | ||
279 | if (path->group == NULL) { | 208 | if (path->group == NULL) { |
280 | /* Skip remote filesystems if we're not interested in them */ | 209 | if (config.fs_exclude_list && |
281 | if (me->me_remote && show_local_fs) { | 210 | np_find_regmatch(config.fs_exclude_list, mount_entry->me_type)) { |
282 | if (stat_remote_fs) { | 211 | // Skip excluded fs's |
283 | if (!stat_path(path) && ignore_missing == true) { | 212 | path = mp_int_fs_list_del(&config.path_select_list, path); |
284 | result = STATE_OK; | ||
285 | xasprintf(&ignored, "%s %s;", ignored, path->name); | ||
286 | } | ||
287 | } | ||
288 | continue; | 213 | continue; |
289 | /* Skip pseudo fs's if we haven't asked for all fs's */ | ||
290 | } | 214 | } |
291 | if (me->me_dummy && !show_all_fs) { | 215 | |
292 | continue; | 216 | if (config.device_path_exclude_list && |
293 | /* Skip excluded fstypes */ | 217 | (np_find_name(config.device_path_exclude_list, mount_entry->me_devname) || |
294 | } | 218 | np_find_name(config.device_path_exclude_list, mount_entry->me_mountdir))) { |
295 | if (fs_exclude_list && np_find_regmatch(fs_exclude_list, me->me_type)) { | 219 | // Skip excluded device or mount paths |
220 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
296 | continue; | 221 | continue; |
297 | /* Skip excluded fs's */ | ||
298 | } | 222 | } |
299 | if (dp_exclude_list && (np_find_name(dp_exclude_list, me->me_devname) || np_find_name(dp_exclude_list, me->me_mountdir))) { | 223 | |
224 | if (config.fs_include_list && | ||
225 | !np_find_regmatch(config.fs_include_list, mount_entry->me_type)) { | ||
226 | // Skip not included fstypes | ||
227 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
300 | continue; | 228 | continue; |
301 | /* Skip not included fstypes */ | ||
302 | } | 229 | } |
303 | if (fs_include_list && !np_find_regmatch(fs_include_list, me->me_type)) { | 230 | |
231 | /* Skip remote filesystems if we're not interested in them */ | ||
232 | if (mount_entry->me_remote && config.show_local_fs) { | ||
233 | if (config.stat_remote_fs) { | ||
234 | // TODO Stat here | ||
235 | if (!stat_path(path, config.ignore_missing) && config.ignore_missing) { | ||
236 | } | ||
237 | } | ||
304 | continue; | 238 | continue; |
305 | } | 239 | } |
306 | } | ||
307 | 240 | ||
308 | if (!stat_path(path)) { | 241 | // TODO why stat here? remove unstatable fs? |
309 | if (ignore_missing == true) { | 242 | if (!stat_path(path, config.ignore_missing)) { |
310 | result = STATE_OK; | 243 | // if (config.ignore_missing) { |
311 | xasprintf(&ignored, "%s %s;", ignored, path->name); | 244 | // xasprintf(&ignored, "%s %s;", ignored, path->name); |
245 | // } | ||
246 | // not accessible, remove from list | ||
247 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
248 | continue; | ||
312 | } | 249 | } |
313 | continue; | ||
314 | } | 250 | } |
315 | get_fs_usage(me->me_mountdir, me->me_devname, &fsp); | ||
316 | |||
317 | if (fsp.fsu_blocks && strcmp("none", me->me_mountdir)) { | ||
318 | get_stats(path, &fsp); | ||
319 | |||
320 | if (verbose >= 3) { | ||
321 | printf("For %s, used_pct=%f free_pct=%f used_units=%lu free_units=%lu total_units=%lu used_inodes_pct=%f " | ||
322 | "free_inodes_pct=%f fsp.fsu_blocksize=%lu mult=%lu\n", | ||
323 | me->me_mountdir, path->dused_pct, path->dfree_pct, path->dused_units, path->dfree_units, path->dtotal_units, | ||
324 | path->dused_inodes_percent, path->dfree_inodes_percent, fsp.fsu_blocksize, mult); | ||
325 | } | ||
326 | |||
327 | /* Threshold comparisons */ | ||
328 | |||
329 | temp_result = get_status(path->dfree_units, path->freespace_units); | ||
330 | if (verbose >= 3) | ||
331 | printf("Freespace_units result=%d\n", temp_result); | ||
332 | disk_result = max_state(disk_result, temp_result); | ||
333 | |||
334 | temp_result = get_status(path->dfree_pct, path->freespace_percent); | ||
335 | if (verbose >= 3) | ||
336 | printf("Freespace%% result=%d\n", temp_result); | ||
337 | disk_result = max_state(disk_result, temp_result); | ||
338 | 251 | ||
339 | temp_result = get_status(path->dused_units, path->usedspace_units); | 252 | path = mp_int_fs_list_get_next(path); |
340 | if (verbose >= 3) | 253 | } |
341 | printf("Usedspace_units result=%d\n", temp_result); | ||
342 | disk_result = max_state(disk_result, temp_result); | ||
343 | |||
344 | temp_result = get_status(path->dused_pct, path->usedspace_percent); | ||
345 | if (verbose >= 3) | ||
346 | printf("Usedspace_percent result=%d\n", temp_result); | ||
347 | disk_result = max_state(disk_result, temp_result); | ||
348 | |||
349 | temp_result = get_status(path->dused_inodes_percent, path->usedinodes_percent); | ||
350 | if (verbose >= 3) | ||
351 | printf("Usedinodes_percent result=%d\n", temp_result); | ||
352 | disk_result = max_state(disk_result, temp_result); | ||
353 | |||
354 | temp_result = get_status(path->dfree_inodes_percent, path->freeinodes_percent); | ||
355 | if (verbose >= 3) | ||
356 | printf("Freeinodes_percent result=%d\n", temp_result); | ||
357 | disk_result = max_state(disk_result, temp_result); | ||
358 | |||
359 | result = max_state(result, disk_result); | ||
360 | |||
361 | /* What a mess of units. The output shows free space, the perf data shows used space. Yikes! | ||
362 | Hack here. Trying to get warn/crit levels from freespace_(units|percent) for perf | ||
363 | data. Assumption that start=0. Roll on new syntax... | ||
364 | */ | ||
365 | |||
366 | /* *_high_tide must be reinitialized at each run */ | ||
367 | uint64_t warning_high_tide = UINT64_MAX; | ||
368 | 254 | ||
369 | if (path->freespace_units->warning != NULL) { | 255 | // now get the actual measurements |
370 | warning_high_tide = (path->dtotal_units - path->freespace_units->warning->end) * mult; | 256 | for (parameter_list_elem *filesystem = config.path_select_list.first; filesystem;) { |
371 | } | 257 | // Get actual metrics here |
372 | if (path->freespace_percent->warning != NULL) { | 258 | struct mount_entry *mount_entry = filesystem->best_match; |
373 | warning_high_tide = | 259 | struct fs_usage fsp = {0}; |
374 | min(warning_high_tide, (uint64_t)((1.0 - path->freespace_percent->warning->end / 100) * (path->dtotal_units * mult))); | 260 | get_fs_usage(mount_entry->me_mountdir, mount_entry->me_devname, &fsp); |
375 | } | ||
376 | 261 | ||
377 | uint64_t critical_high_tide = UINT64_MAX; | 262 | if (fsp.fsu_blocks != 0 && strcmp("none", mount_entry->me_mountdir) != 0) { |
263 | *filesystem = get_path_stats(*filesystem, fsp, config.freespace_ignore_reserved); | ||
378 | 264 | ||
379 | if (path->freespace_units->critical != NULL) { | 265 | if (verbose >= 3) { |
380 | critical_high_tide = (path->dtotal_units - path->freespace_units->critical->end) * mult; | 266 | printf("For %s, used_units=%lu free_units=%lu total_units=%lu " |
381 | } | 267 | "fsp.fsu_blocksize=%lu\n", |
382 | if (path->freespace_percent->critical != NULL) { | 268 | mount_entry->me_mountdir, filesystem->used_bytes, filesystem->free_bytes, |
383 | critical_high_tide = | 269 | filesystem->total_bytes, fsp.fsu_blocksize); |
384 | min(critical_high_tide, (uint64_t)((1.0 - path->freespace_percent->critical->end / 100) * (path->dtotal_units * mult))); | ||
385 | } | 270 | } |
271 | } else { | ||
272 | // failed to retrieve file system data or not mounted? | ||
273 | filesystem = mp_int_fs_list_del(&config.path_select_list, filesystem); | ||
274 | continue; | ||
275 | } | ||
276 | filesystem = mp_int_fs_list_get_next(filesystem); | ||
277 | } | ||
386 | 278 | ||
387 | /* Nb: *_high_tide are unset when == UINT64_MAX */ | 279 | if (verbose > 2) { |
388 | xasprintf(&perf, "%s %s", perf, | 280 | for (parameter_list_elem *filesystem = config.path_select_list.first; filesystem; |
389 | perfdata_uint64((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | 281 | filesystem = mp_int_fs_list_get_next(filesystem)) { |
390 | path->dused_units * mult, "B", (warning_high_tide == UINT64_MAX ? false : true), warning_high_tide, | 282 | assert(filesystem->best_match != NULL); |
391 | (critical_high_tide == UINT64_MAX ? false : true), critical_high_tide, true, 0, true, | 283 | if (filesystem->best_match == NULL) { |
392 | path->dtotal_units * mult)); | 284 | printf("Filesystem path %s has no mount_entry!\n", filesystem->name); |
393 | 285 | } else { | |
394 | if (display_inodes_perfdata) { | 286 | // printf("Filesystem path %s has a mount_entry!\n", filesystem->name); |
395 | /* *_high_tide must be reinitialized at each run */ | ||
396 | warning_high_tide = UINT64_MAX; | ||
397 | critical_high_tide = UINT64_MAX; | ||
398 | |||
399 | if (path->freeinodes_percent->warning != NULL) { | ||
400 | warning_high_tide = (uint64_t)fabs( | ||
401 | min((double)warning_high_tide, (double)(1.0 - path->freeinodes_percent->warning->end / 100) * path->inodes_total)); | ||
402 | } | ||
403 | if (path->freeinodes_percent->critical != NULL) { | ||
404 | critical_high_tide = (uint64_t)fabs(min( | ||
405 | (double)critical_high_tide, (double)(1.0 - path->freeinodes_percent->critical->end / 100) * path->inodes_total)); | ||
406 | } | ||
407 | |||
408 | xasprintf(&perf_ilabel, "%s (inodes)", | ||
409 | (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir); | ||
410 | /* Nb: *_high_tide are unset when == UINT64_MAX */ | ||
411 | xasprintf(&perf, "%s %s", perf, | ||
412 | perfdata_uint64(perf_ilabel, path->inodes_used, "", (warning_high_tide != UINT64_MAX ? true : false), | ||
413 | warning_high_tide, (critical_high_tide != UINT64_MAX ? true : false), critical_high_tide, true, 0, | ||
414 | true, path->inodes_total)); | ||
415 | } | 287 | } |
288 | } | ||
289 | } | ||
416 | 290 | ||
417 | if (disk_result == STATE_OK && erronly && !verbose) | 291 | measurement_unit_list *measurements = NULL; |
418 | continue; | 292 | measurement_unit_list *current = NULL; |
419 | 293 | // create measuring units, because of groups | |
420 | if (disk_result && verbose >= 1) { | 294 | for (parameter_list_elem *filesystem = config.path_select_list.first; filesystem; |
421 | xasprintf(&flag_header, " %s [", state_text(disk_result)); | 295 | filesystem = mp_int_fs_list_get_next(filesystem)) { |
296 | assert(filesystem->best_match != NULL); | ||
297 | |||
298 | if (filesystem->group == NULL) { | ||
299 | // create a measurement unit for the fs | ||
300 | measurement_unit unit = | ||
301 | create_measurement_unit_from_filesystem(*filesystem, config.display_mntp); | ||
302 | if (measurements == NULL) { | ||
303 | measurements = current = add_measurement_list(NULL, unit); | ||
422 | } else { | 304 | } else { |
423 | xasprintf(&flag_header, ""); | 305 | current = add_measurement_list(measurements, unit); |
424 | } | 306 | } |
425 | xasprintf(&output, "%s%s %s %llu%s (%.1f%%", output, flag_header, | 307 | } else { |
426 | (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, path->dfree_units, units, | 308 | // Grouped elements are consecutive |
427 | path->dfree_pct); | 309 | if (measurements == NULL) { |
428 | if (path->dused_inodes_percent < 0) { | 310 | // first entry |
429 | xasprintf(&output, "%s inode=-)%s;", output, (disk_result ? "]" : "")); | 311 | measurement_unit unit = |
312 | create_measurement_unit_from_filesystem(*filesystem, config.display_mntp); | ||
313 | unit.name = strdup(filesystem->group); | ||
314 | measurements = current = add_measurement_list(NULL, unit); | ||
430 | } else { | 315 | } else { |
431 | xasprintf(&output, "%s inode=%.0f%%)%s;", output, path->dfree_inodes_percent, ((disk_result && verbose >= 1) ? "]" : "")); | 316 | // if this is the first element of a group, the name of the previous entry is |
317 | // different | ||
318 | if (strcmp(filesystem->group, current->unit.name) != 0) { | ||
319 | // so, this must be the first element of a group | ||
320 | measurement_unit unit = | ||
321 | create_measurement_unit_from_filesystem(*filesystem, config.display_mntp); | ||
322 | unit.name = filesystem->group; | ||
323 | current = add_measurement_list(measurements, unit); | ||
324 | |||
325 | } else { | ||
326 | // NOT the first entry of a group, add info to the other one | ||
327 | current->unit = add_filesystem_to_measurement_unit(current->unit, *filesystem); | ||
328 | } | ||
432 | } | 329 | } |
433 | free(flag_header); | ||
434 | } | 330 | } |
435 | } | 331 | } |
436 | 332 | ||
437 | if (verbose >= 2) | 333 | /* Process for every path in list */ |
438 | xasprintf(&output, "%s%s", output, details); | 334 | if (measurements != NULL) { |
335 | for (measurement_unit_list *unit = measurements; unit; unit = unit->next) { | ||
336 | mp_subcheck unit_sc = evaluate_filesystem(unit->unit, config.display_inodes_perfdata, | ||
337 | config.display_unit); | ||
338 | mp_add_subcheck_to_check(&overall, unit_sc); | ||
339 | } | ||
340 | } else { | ||
341 | // Apparently no machting fs found | ||
342 | mp_subcheck none_sc = mp_subcheck_init(); | ||
343 | xasprintf(&none_sc.output, "No filesystems were found for the provided parameters"); | ||
439 | 344 | ||
440 | if (strcmp(output, "") == 0 && !erronly) { | 345 | if (config.ignore_missing) { |
441 | preamble = ""; | 346 | none_sc = mp_set_subcheck_state(none_sc, STATE_OK); |
442 | xasprintf(&output, " - No disks were found for provided parameters"); | 347 | } else { |
348 | none_sc = mp_set_subcheck_state(none_sc, STATE_UNKNOWN); | ||
349 | } | ||
350 | mp_add_subcheck_to_check(&overall, none_sc); | ||
443 | } | 351 | } |
444 | 352 | ||
445 | printf("DISK %s%s%s%s%s|%s\n", state_text(result), ((erronly && result == STATE_OK)) ? "" : preamble, output, | 353 | mp_exit(overall); |
446 | (strcmp(ignored, "") == 0) ? "" : ignored_preamble, ignored, perf); | ||
447 | return result; | ||
448 | } | 354 | } |
449 | 355 | ||
450 | double calculate_percent(uintmax_t value, uintmax_t total) { | 356 | double calculate_percent(uintmax_t value, uintmax_t total) { |
451 | double pct = -1; | 357 | double pct = -1; |
452 | if (value <= DBL_MAX && total != 0) { | 358 | if (value <= DBL_MAX && total != 0) { |
453 | pct = (double)value / total * 100.0; | 359 | pct = (double)value / (double)total * 100.0; |
454 | } | 360 | } |
361 | |||
455 | return pct; | 362 | return pct; |
456 | } | 363 | } |
457 | 364 | ||
458 | /* process command-line arguments */ | 365 | /* process command-line arguments */ |
459 | int process_arguments(int argc, char **argv) { | 366 | check_disk_config_wrapper process_arguments(int argc, char **argv) { |
460 | int c; | 367 | |
461 | int err; | 368 | check_disk_config_wrapper result = { |
462 | struct parameter_list *se; | 369 | .errorcode = OK, |
463 | struct parameter_list *temp_list = NULL; | 370 | .config = check_disk_config_init(), |
464 | struct parameter_list *previous = NULL; | 371 | }; |
465 | struct mount_entry *me; | 372 | |
466 | regex_t re; | 373 | if (argc < 2) { |
467 | int cflags = REG_NOSUB | REG_EXTENDED; | 374 | result.errorcode = ERROR; |
468 | int default_cflags = cflags; | 375 | return result; |
469 | char errbuf[MAX_INPUT_BUFFER]; | 376 | } |
470 | int fnd = 0; | 377 | |
378 | enum { | ||
379 | output_format_index = CHAR_MAX + 1, | ||
380 | display_unit_index, | ||
381 | }; | ||
471 | 382 | ||
472 | int option = 0; | ||
473 | static struct option longopts[] = {{"timeout", required_argument, 0, 't'}, | 383 | static struct option longopts[] = {{"timeout", required_argument, 0, 't'}, |
474 | {"warning", required_argument, 0, 'w'}, | 384 | {"warning", required_argument, 0, 'w'}, |
475 | {"critical", required_argument, 0, 'c'}, | 385 | {"critical", required_argument, 0, 'c'}, |
476 | {"iwarning", required_argument, 0, 'W'}, | 386 | {"iwarning", required_argument, 0, 'W'}, |
477 | /* Dang, -C is taken. We might want to reshuffle this. */ | ||
478 | {"icritical", required_argument, 0, 'K'}, | 387 | {"icritical", required_argument, 0, 'K'}, |
479 | {"kilobytes", no_argument, 0, 'k'}, | 388 | {"kilobytes", no_argument, 0, 'k'}, |
480 | {"megabytes", no_argument, 0, 'm'}, | 389 | {"megabytes", no_argument, 0, 'm'}, |
@@ -507,24 +416,43 @@ int process_arguments(int argc, char **argv) { | |||
507 | {"clear", no_argument, 0, 'C'}, | 416 | {"clear", no_argument, 0, 'C'}, |
508 | {"version", no_argument, 0, 'V'}, | 417 | {"version", no_argument, 0, 'V'}, |
509 | {"help", no_argument, 0, 'h'}, | 418 | {"help", no_argument, 0, 'h'}, |
419 | {"output-format", required_argument, 0, output_format_index}, | ||
420 | {"display-unit", required_argument, 0, display_unit_index}, | ||
510 | {0, 0, 0, 0}}; | 421 | {0, 0, 0, 0}}; |
511 | 422 | ||
512 | if (argc < 2) | 423 | for (int index = 1; index < argc; index++) { |
513 | return ERROR; | 424 | if (strcmp("-to", argv[index]) == 0) { |
425 | strcpy(argv[index], "-t"); | ||
426 | } | ||
427 | } | ||
428 | |||
429 | int cflags = REG_NOSUB | REG_EXTENDED; | ||
430 | int default_cflags = cflags; | ||
431 | char *warn_freespace_units = NULL; | ||
432 | char *crit_freespace_units = NULL; | ||
433 | char *warn_freespace_percent = NULL; | ||
434 | char *crit_freespace_percent = NULL; | ||
435 | char *warn_freeinodes_percent = NULL; | ||
436 | char *crit_freeinodes_percent = NULL; | ||
514 | 437 | ||
515 | np_add_regex(&fs_exclude_list, "iso9660", REG_EXTENDED); | 438 | bool path_selected = false; |
439 | char *group = NULL; | ||
440 | byte_unit unit = MebiBytes_factor; | ||
516 | 441 | ||
517 | for (c = 1; c < argc; c++) | 442 | result.config.mount_list = read_file_system_list(false); |
518 | if (strcmp("-to", argv[c]) == 0) | ||
519 | strcpy(argv[c], "-t"); | ||
520 | 443 | ||
521 | while (1) { | 444 | np_add_regex(&result.config.fs_exclude_list, "iso9660", REG_EXTENDED); |
522 | c = getopt_long(argc, argv, "+?VqhvefCt:c:w:K:W:u:p:x:X:N:mklLPg:R:r:i:I:MEAn", longopts, &option); | ||
523 | 445 | ||
524 | if (c == -1 || c == EOF) | 446 | while (true) { |
447 | int option = 0; | ||
448 | int option_index = getopt_long( | ||
449 | argc, argv, "+?VqhvefCt:c:w:K:W:u:p:x:X:N:mklLPg:R:r:i:I:MEAn", longopts, &option); | ||
450 | |||
451 | if (option_index == -1 || option_index == EOF) { | ||
525 | break; | 452 | break; |
453 | } | ||
526 | 454 | ||
527 | switch (c) { | 455 | switch (option_index) { |
528 | case 't': /* timeout period */ | 456 | case 't': /* timeout period */ |
529 | if (is_integer(optarg)) { | 457 | if (is_integer(optarg)) { |
530 | timeout_interval = atoi(optarg); | 458 | timeout_interval = atoi(optarg); |
@@ -555,10 +483,10 @@ int process_arguments(int argc, char **argv) { | |||
555 | break; | 483 | break; |
556 | 484 | ||
557 | /* Awful mistake where the range values do not make sense. Normally, | 485 | /* Awful mistake where the range values do not make sense. Normally, |
558 | you alert if the value is within the range, but since we are using | 486 | * you alert if the value is within the range, but since we are using |
559 | freespace, we have to alert if outside the range. Thus we artificially | 487 | * freespace, we have to alert if outside the range. Thus we artificially |
560 | force @ at the beginning of the range, so that it is backwards compatible | 488 | * force @ at the beginning of the range, so that it is backwards compatible |
561 | */ | 489 | */ |
562 | case 'c': /* critical threshold */ | 490 | case 'c': /* critical threshold */ |
563 | if (!is_percentage_expression(optarg) && !is_numeric(optarg)) { | 491 | if (!is_percentage_expression(optarg) && !is_numeric(optarg)) { |
564 | die(STATE_UNKNOWN, "Argument for --critical invalid or missing: %s\n", optarg); | 492 | die(STATE_UNKNOWN, "Argument for --critical invalid or missing: %s\n", optarg); |
@@ -594,181 +522,193 @@ int process_arguments(int argc, char **argv) { | |||
594 | } | 522 | } |
595 | break; | 523 | break; |
596 | case 'u': | 524 | case 'u': |
597 | if (units) | ||
598 | free(units); | ||
599 | if (!strcasecmp(optarg, "bytes")) { | 525 | if (!strcasecmp(optarg, "bytes")) { |
600 | mult = (uintmax_t)1; | 526 | unit = Bytes_Factor; |
601 | units = strdup("B"); | ||
602 | } else if (!strcmp(optarg, "KiB")) { | 527 | } else if (!strcmp(optarg, "KiB")) { |
603 | mult = (uintmax_t)1024; | 528 | unit = KibiBytes_factor; |
604 | units = strdup("KiB"); | ||
605 | } else if (!strcmp(optarg, "kB")) { | 529 | } else if (!strcmp(optarg, "kB")) { |
606 | mult = (uintmax_t)1000; | 530 | unit = KiloBytes_factor; |
607 | units = strdup("kB"); | ||
608 | } else if (!strcmp(optarg, "MiB")) { | 531 | } else if (!strcmp(optarg, "MiB")) { |
609 | mult = (uintmax_t)1024 * 1024; | 532 | unit = MebiBytes_factor; |
610 | units = strdup("MiB"); | ||
611 | } else if (!strcmp(optarg, "MB")) { | 533 | } else if (!strcmp(optarg, "MB")) { |
612 | mult = (uintmax_t)1000 * 1000; | 534 | unit = MegaBytes_factor; |
613 | units = strdup("MB"); | ||
614 | } else if (!strcmp(optarg, "GiB")) { | 535 | } else if (!strcmp(optarg, "GiB")) { |
615 | mult = (uintmax_t)1024 * 1024 * 1024; | 536 | unit = GibiBytes_factor; |
616 | units = strdup("GiB"); | ||
617 | } else if (!strcmp(optarg, "GB")) { | 537 | } else if (!strcmp(optarg, "GB")) { |
618 | mult = (uintmax_t)1000 * 1000 * 1000; | 538 | unit = GigaBytes_factor; |
619 | units = strdup("GB"); | ||
620 | } else if (!strcmp(optarg, "TiB")) { | 539 | } else if (!strcmp(optarg, "TiB")) { |
621 | mult = (uintmax_t)1024 * 1024 * 1024 * 1024; | 540 | unit = TebiBytes_factor; |
622 | units = strdup("TiB"); | ||
623 | } else if (!strcmp(optarg, "TB")) { | 541 | } else if (!strcmp(optarg, "TB")) { |
624 | mult = (uintmax_t)1000 * 1000 * 1000 * 1000; | 542 | unit = TeraBytes_factor; |
625 | units = strdup("TB"); | ||
626 | } else if (!strcmp(optarg, "PiB")) { | 543 | } else if (!strcmp(optarg, "PiB")) { |
627 | mult = (uintmax_t)1024 * 1024 * 1024 * 1024 * 1024; | 544 | unit = PebiBytes_factor; |
628 | units = strdup("PiB"); | ||
629 | } else if (!strcmp(optarg, "PB")) { | 545 | } else if (!strcmp(optarg, "PB")) { |
630 | mult = (uintmax_t)1000 * 1000 * 1000 * 1000 * 1000; | 546 | unit = PetaBytes_factor; |
631 | units = strdup("PB"); | ||
632 | } else { | 547 | } else { |
633 | die(STATE_UNKNOWN, _("unit type %s not known\n"), optarg); | 548 | die(STATE_UNKNOWN, _("unit type %s not known\n"), optarg); |
634 | } | 549 | } |
635 | if (units == NULL) | ||
636 | die(STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units"); | ||
637 | break; | 550 | break; |
638 | case 'k': /* display mountpoint */ | 551 | case 'k': |
639 | mult = 1024; | 552 | unit = KibiBytes_factor; |
640 | if (units) | ||
641 | free(units); | ||
642 | units = strdup("kiB"); | ||
643 | break; | 553 | break; |
644 | case 'm': /* display mountpoint */ | 554 | case 'm': |
645 | mult = 1024 * 1024; | 555 | unit = MebiBytes_factor; |
646 | if (units) | 556 | break; |
647 | free(units); | 557 | case display_unit_index: |
648 | units = strdup("MiB"); | 558 | if (!strcasecmp(optarg, "bytes")) { |
559 | result.config.display_unit = Bytes; | ||
560 | } else if (!strcmp(optarg, "KiB")) { | ||
561 | result.config.display_unit = KibiBytes; | ||
562 | } else if (!strcmp(optarg, "kB")) { | ||
563 | result.config.display_unit = KiloBytes; | ||
564 | } else if (!strcmp(optarg, "MiB")) { | ||
565 | result.config.display_unit = MebiBytes; | ||
566 | } else if (!strcmp(optarg, "MB")) { | ||
567 | result.config.display_unit = MegaBytes; | ||
568 | } else if (!strcmp(optarg, "GiB")) { | ||
569 | result.config.display_unit = GibiBytes; | ||
570 | } else if (!strcmp(optarg, "GB")) { | ||
571 | result.config.display_unit = GigaBytes; | ||
572 | } else if (!strcmp(optarg, "TiB")) { | ||
573 | result.config.display_unit = TebiBytes; | ||
574 | } else if (!strcmp(optarg, "TB")) { | ||
575 | result.config.display_unit = TeraBytes; | ||
576 | } else if (!strcmp(optarg, "PiB")) { | ||
577 | result.config.display_unit = PebiBytes; | ||
578 | } else if (!strcmp(optarg, "PB")) { | ||
579 | result.config.display_unit = PetaBytes; | ||
580 | } else { | ||
581 | die(STATE_UNKNOWN, _("unit type %s not known\n"), optarg); | ||
582 | } | ||
649 | break; | 583 | break; |
650 | case 'L': | 584 | case 'L': |
651 | stat_remote_fs = 1; | 585 | result.config.stat_remote_fs = true; |
652 | /* fallthrough */ | 586 | /* fallthrough */ |
653 | case 'l': | 587 | case 'l': |
654 | show_local_fs = 1; | 588 | result.config.show_local_fs = true; |
655 | break; | 589 | break; |
656 | case 'P': | 590 | case 'P': |
657 | display_inodes_perfdata = 1; | 591 | result.config.display_inodes_perfdata = true; |
658 | break; | 592 | break; |
659 | case 'p': /* select path */ | 593 | case 'p': /* select path */ { |
660 | if (!(warn_freespace_units || crit_freespace_units || warn_freespace_percent || crit_freespace_percent || | 594 | if (!(warn_freespace_units || crit_freespace_units || warn_freespace_percent || |
661 | warn_usedspace_units || crit_usedspace_units || warn_usedspace_percent || crit_usedspace_percent || | 595 | crit_freespace_percent || warn_freeinodes_percent || crit_freeinodes_percent)) { |
662 | warn_usedinodes_percent || crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent)) { | 596 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), |
663 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n")); | 597 | _("Must set a threshold value before using -p\n")); |
664 | } | 598 | } |
665 | 599 | ||
666 | /* add parameter if not found. overwrite thresholds if path has already been added */ | 600 | /* add parameter if not found. overwrite thresholds if path has already been added */ |
667 | if (!(se = np_find_parameter(path_select_list, optarg))) { | 601 | parameter_list_elem *search_entry; |
668 | se = np_add_parameter(&path_select_list, optarg); | 602 | if (!(search_entry = mp_int_fs_list_find(result.config.path_select_list, optarg))) { |
669 | 603 | search_entry = mp_int_fs_list_append(&result.config.path_select_list, optarg); | |
670 | if (stat(optarg, &stat_buf[0]) && ignore_missing == true) { | 604 | |
671 | path_ignored = true; | 605 | // struct stat stat_buf = {}; |
672 | break; | 606 | // if (stat(optarg, &stat_buf) && result.config.ignore_missing) { |
673 | } | 607 | // result.config.path_ignored = true; |
608 | // break; | ||
609 | // } | ||
674 | } | 610 | } |
675 | se->group = group; | 611 | search_entry->group = group; |
676 | set_all_thresholds(se); | 612 | set_all_thresholds(search_entry, warn_freespace_units, crit_freespace_units, |
613 | warn_freespace_percent, crit_freespace_percent, | ||
614 | |||
615 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
677 | 616 | ||
678 | /* With autofs, it is required to stat() the path before re-populating the mount_list */ | 617 | /* With autofs, it is required to stat() the path before re-populating the mount_list */ |
679 | if (!stat_path(se)) { | 618 | // if (!stat_path(se, result.config.ignore_missing)) { |
680 | break; | 619 | // break; |
681 | } | 620 | // } |
682 | /* NB: We can't free the old mount_list "just like that": both list pointers and struct | 621 | mp_int_fs_list_set_best_match(result.config.path_select_list, result.config.mount_list, |
683 | * pointers are copied around. One of the reason it wasn't done yet is that other parts | 622 | result.config.exact_match); |
684 | * of check_disk need the same kind of cleanup so it'd better be done as a whole */ | ||
685 | mount_list = read_file_system_list(0); | ||
686 | np_set_best_match(se, mount_list, exact_match); | ||
687 | 623 | ||
688 | path_selected = true; | 624 | path_selected = true; |
689 | break; | 625 | } break; |
690 | case 'x': /* exclude path or partition */ | 626 | case 'x': /* exclude path or partition */ |
691 | np_add_name(&dp_exclude_list, optarg); | 627 | np_add_name(&result.config.device_path_exclude_list, optarg); |
692 | break; | 628 | break; |
693 | case 'X': /* exclude file system type */ | 629 | case 'X': /* exclude file system type */ { |
694 | err = np_add_regex(&fs_exclude_list, optarg, REG_EXTENDED); | 630 | int err = np_add_regex(&result.config.fs_exclude_list, optarg, REG_EXTENDED); |
695 | if (err != 0) { | 631 | if (err != 0) { |
696 | regerror(err, &fs_exclude_list->regex, errbuf, MAX_INPUT_BUFFER); | 632 | char errbuf[MAX_INPUT_BUFFER]; |
697 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 633 | regerror(err, &result.config.fs_exclude_list->regex, errbuf, MAX_INPUT_BUFFER); |
634 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), | ||
635 | _("Could not compile regular expression"), errbuf); | ||
698 | } | 636 | } |
699 | break; | 637 | break; |
700 | case 'N': /* include file system type */ | 638 | case 'N': /* include file system type */ |
701 | err = np_add_regex(&fs_include_list, optarg, REG_EXTENDED); | 639 | err = np_add_regex(&result.config.fs_include_list, optarg, REG_EXTENDED); |
702 | if (err != 0) { | 640 | if (err != 0) { |
703 | regerror(err, &fs_exclude_list->regex, errbuf, MAX_INPUT_BUFFER); | 641 | char errbuf[MAX_INPUT_BUFFER]; |
704 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 642 | regerror(err, &result.config.fs_exclude_list->regex, errbuf, MAX_INPUT_BUFFER); |
643 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), | ||
644 | _("Could not compile regular expression"), errbuf); | ||
705 | } | 645 | } |
706 | break; | 646 | } break; |
707 | case 'v': /* verbose */ | 647 | case 'v': /* verbose */ |
708 | verbose++; | 648 | verbose++; |
709 | break; | 649 | break; |
710 | case 'q': /* TODO: this function should eventually go away (removed 2007-09-20) */ | 650 | case 'q': /* TODO: this function should eventually go away (removed 2007-09-20) */ |
711 | /* verbose--; **replaced by line below**. -q was only a broken way of implementing -e */ | 651 | /* verbose--; **replaced by line below**. -q was only a broken way of implementing -e */ |
712 | erronly = true; | 652 | result.config.erronly = true; |
713 | break; | 653 | break; |
714 | case 'e': | 654 | case 'e': |
715 | erronly = true; | 655 | result.config.erronly = true; |
716 | break; | 656 | break; |
717 | case 'E': | 657 | case 'E': |
718 | if (path_selected) | 658 | if (path_selected) { |
719 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set -E before selecting paths\n")); | 659 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), |
720 | exact_match = true; | 660 | _("Must set -E before selecting paths\n")); |
661 | } | ||
662 | result.config.exact_match = true; | ||
721 | break; | 663 | break; |
722 | case 'f': | 664 | case 'f': |
723 | freespace_ignore_reserved = true; | 665 | result.config.freespace_ignore_reserved = true; |
724 | break; | 666 | break; |
725 | case 'g': | 667 | case 'g': |
726 | if (path_selected) | 668 | if (path_selected) { |
727 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before selecting paths\n")); | 669 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), |
670 | _("Must set group value before selecting paths\n")); | ||
671 | } | ||
728 | group = optarg; | 672 | group = optarg; |
729 | break; | 673 | break; |
730 | case 'I': | 674 | case 'I': |
731 | cflags |= REG_ICASE; | 675 | cflags |= REG_ICASE; |
732 | // Intentional fallthrough | 676 | // Intentional fallthrough |
733 | case 'i': | 677 | case 'i': { |
734 | if (!path_selected) | 678 | if (!path_selected) { |
735 | die(STATE_UNKNOWN, "DISK %s: %s\n", _("UNKNOWN"), | 679 | die(STATE_UNKNOWN, "DISK %s: %s\n", _("UNKNOWN"), |
736 | _("Paths need to be selected before using -i/-I. Use -A to select all paths explicitly")); | 680 | _("Paths need to be selected before using -i/-I. Use -A to select all paths " |
737 | err = regcomp(&re, optarg, cflags); | 681 | "explicitly")); |
682 | } | ||
683 | regex_t regex; | ||
684 | int err = regcomp(®ex, optarg, cflags); | ||
738 | if (err != 0) { | 685 | if (err != 0) { |
739 | regerror(err, &re, errbuf, MAX_INPUT_BUFFER); | 686 | char errbuf[MAX_INPUT_BUFFER]; |
740 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 687 | regerror(err, ®ex, errbuf, MAX_INPUT_BUFFER); |
688 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), | ||
689 | _("Could not compile regular expression"), errbuf); | ||
741 | } | 690 | } |
742 | 691 | ||
743 | temp_list = path_select_list; | 692 | for (parameter_list_elem *elem = result.config.path_select_list.first; elem;) { |
693 | if (elem->best_match) { | ||
694 | if (np_regex_match_mount_entry(elem->best_match, ®ex)) { | ||
744 | 695 | ||
745 | previous = NULL; | 696 | if (verbose >= 3) { |
746 | while (temp_list) { | 697 | printf("ignoring %s matching regex\n", elem->name); |
747 | if (temp_list->best_match) { | 698 | } |
748 | if (np_regex_match_mount_entry(temp_list->best_match, &re)) { | ||
749 | 699 | ||
750 | if (verbose >= 3) | 700 | elem = mp_int_fs_list_del(&result.config.path_select_list, elem); |
751 | printf("ignoring %s matching regex\n", temp_list->name); | 701 | continue; |
752 | |||
753 | temp_list = np_del_parameter(temp_list, previous); | ||
754 | /* pointer to first element needs to be updated if first item gets deleted */ | ||
755 | if (previous == NULL) | ||
756 | path_select_list = temp_list; | ||
757 | } else { | ||
758 | previous = temp_list; | ||
759 | temp_list = temp_list->name_next; | ||
760 | } | 702 | } |
761 | } else { | ||
762 | previous = temp_list; | ||
763 | temp_list = temp_list->name_next; | ||
764 | } | 703 | } |
704 | |||
705 | elem = mp_int_fs_list_get_next(elem); | ||
765 | } | 706 | } |
766 | 707 | ||
767 | cflags = default_cflags; | 708 | cflags = default_cflags; |
768 | break; | 709 | } break; |
769 | |||
770 | case 'n': | 710 | case 'n': |
771 | ignore_missing = true; | 711 | result.config.ignore_missing = true; |
772 | break; | 712 | break; |
773 | case 'A': | 713 | case 'A': |
774 | optarg = strdup(".*"); | 714 | optarg = strdup(".*"); |
@@ -776,80 +716,96 @@ int process_arguments(int argc, char **argv) { | |||
776 | case 'R': | 716 | case 'R': |
777 | cflags |= REG_ICASE; | 717 | cflags |= REG_ICASE; |
778 | // Intentional fallthrough | 718 | // Intentional fallthrough |
779 | case 'r': | 719 | case 'r': { |
780 | if (!(warn_freespace_units || crit_freespace_units || warn_freespace_percent || crit_freespace_percent || | 720 | if (!(warn_freespace_units || crit_freespace_units || warn_freespace_percent || |
781 | warn_usedspace_units || crit_usedspace_units || warn_usedspace_percent || crit_usedspace_percent || | 721 | crit_freespace_percent || warn_freeinodes_percent || crit_freeinodes_percent)) { |
782 | warn_usedinodes_percent || crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent)) { | ||
783 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), | 722 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), |
784 | _("Must set a threshold value before using -r/-R/-A (--ereg-path/--eregi-path/--all)\n")); | 723 | _("Must set a threshold value before using -r/-R/-A " |
724 | "(--ereg-path/--eregi-path/--all)\n")); | ||
785 | } | 725 | } |
786 | 726 | ||
787 | err = regcomp(&re, optarg, cflags); | 727 | regex_t regex; |
728 | int err = regcomp(®ex, optarg, cflags); | ||
788 | if (err != 0) { | 729 | if (err != 0) { |
789 | regerror(err, &re, errbuf, MAX_INPUT_BUFFER); | 730 | char errbuf[MAX_INPUT_BUFFER]; |
790 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 731 | regerror(err, ®ex, errbuf, MAX_INPUT_BUFFER); |
732 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), | ||
733 | _("Could not compile regular expression"), errbuf); | ||
791 | } | 734 | } |
792 | 735 | ||
793 | for (me = mount_list; me; me = me->me_next) { | 736 | bool found = false; |
794 | if (np_regex_match_mount_entry(me, &re)) { | 737 | for (struct mount_entry *me = result.config.mount_list; me; me = me->me_next) { |
795 | fnd = true; | 738 | if (np_regex_match_mount_entry(me, ®ex)) { |
796 | if (verbose >= 3) | 739 | found = true; |
797 | printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg); | 740 | if (verbose >= 3) { |
741 | printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, | ||
742 | optarg); | ||
743 | } | ||
798 | 744 | ||
799 | /* add parameter if not found. overwrite thresholds if path has already been added */ | 745 | /* add parameter if not found. overwrite thresholds if path has already been |
800 | if (!(se = np_find_parameter(path_select_list, me->me_mountdir))) { | 746 | * added */ |
801 | se = np_add_parameter(&path_select_list, me->me_mountdir); | 747 | parameter_list_elem *se = NULL; |
748 | if (!(se = mp_int_fs_list_find(result.config.path_select_list, | ||
749 | me->me_mountdir))) { | ||
750 | se = | ||
751 | mp_int_fs_list_append(&result.config.path_select_list, me->me_mountdir); | ||
802 | } | 752 | } |
803 | se->group = group; | 753 | se->group = group; |
804 | set_all_thresholds(se); | 754 | set_all_thresholds(se, warn_freespace_units, crit_freespace_units, |
755 | warn_freespace_percent, crit_freespace_percent, | ||
756 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
805 | } | 757 | } |
806 | } | 758 | } |
807 | 759 | ||
808 | if (!fnd && ignore_missing == true) { | 760 | if (!found) { |
809 | path_ignored = true; | 761 | if (result.config.ignore_missing) { |
810 | path_selected = true; | 762 | result.config.path_ignored = true; |
811 | break; | 763 | path_selected = true; |
764 | break; | ||
765 | } | ||
766 | |||
767 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), | ||
768 | _("Regular expression did not match any path or disk"), optarg); | ||
812 | } | 769 | } |
813 | if (!fnd) | ||
814 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Regular expression did not match any path or disk"), optarg); | ||
815 | 770 | ||
816 | fnd = false; | ||
817 | path_selected = true; | 771 | path_selected = true; |
818 | np_set_best_match(path_select_list, mount_list, exact_match); | 772 | mp_int_fs_list_set_best_match(result.config.path_select_list, result.config.mount_list, |
773 | result.config.exact_match); | ||
819 | cflags = default_cflags; | 774 | cflags = default_cflags; |
820 | 775 | ||
821 | break; | 776 | } break; |
822 | case 'M': /* display mountpoint */ | 777 | case 'M': /* display mountpoint */ |
823 | display_mntp = true; | 778 | result.config.display_mntp = true; |
824 | break; | 779 | break; |
825 | case 'C': | 780 | case 'C': { |
826 | /* add all mount entries to path_select list if no partitions have been explicitly defined using -p */ | 781 | /* add all mount entries to path_select list if no partitions have been explicitly |
827 | if (path_selected == false) { | 782 | * defined using -p */ |
828 | struct parameter_list *path; | 783 | if (!path_selected) { |
829 | for (me = mount_list; me; me = me->me_next) { | 784 | parameter_list_elem *path; |
830 | if (!(path = np_find_parameter(path_select_list, me->me_mountdir))) | 785 | for (struct mount_entry *me = result.config.mount_list; me; me = me->me_next) { |
831 | path = np_add_parameter(&path_select_list, me->me_mountdir); | 786 | if (!(path = mp_int_fs_list_find(result.config.path_select_list, |
787 | me->me_mountdir))) { | ||
788 | path = | ||
789 | mp_int_fs_list_append(&result.config.path_select_list, me->me_mountdir); | ||
790 | } | ||
832 | path->best_match = me; | 791 | path->best_match = me; |
833 | path->group = group; | 792 | path->group = group; |
834 | set_all_thresholds(path); | 793 | set_all_thresholds(path, warn_freespace_units, crit_freespace_units, |
794 | warn_freespace_percent, crit_freespace_percent, | ||
795 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
835 | } | 796 | } |
836 | } | 797 | } |
798 | |||
837 | warn_freespace_units = NULL; | 799 | warn_freespace_units = NULL; |
838 | crit_freespace_units = NULL; | 800 | crit_freespace_units = NULL; |
839 | warn_usedspace_units = NULL; | ||
840 | crit_usedspace_units = NULL; | ||
841 | warn_freespace_percent = NULL; | 801 | warn_freespace_percent = NULL; |
842 | crit_freespace_percent = NULL; | 802 | crit_freespace_percent = NULL; |
843 | warn_usedspace_percent = NULL; | ||
844 | crit_usedspace_percent = NULL; | ||
845 | warn_usedinodes_percent = NULL; | ||
846 | crit_usedinodes_percent = NULL; | ||
847 | warn_freeinodes_percent = NULL; | 803 | warn_freeinodes_percent = NULL; |
848 | crit_freeinodes_percent = NULL; | 804 | crit_freeinodes_percent = NULL; |
849 | 805 | ||
850 | path_selected = false; | 806 | path_selected = false; |
851 | group = NULL; | 807 | group = NULL; |
852 | break; | 808 | } break; |
853 | case 'V': /* version */ | 809 | case 'V': /* version */ |
854 | print_revision(progname, NP_VERSION); | 810 | print_revision(progname, NP_VERSION); |
855 | exit(STATE_UNKNOWN); | 811 | exit(STATE_UNKNOWN); |
@@ -858,50 +814,152 @@ int process_arguments(int argc, char **argv) { | |||
858 | exit(STATE_UNKNOWN); | 814 | exit(STATE_UNKNOWN); |
859 | case '?': /* help */ | 815 | case '?': /* help */ |
860 | usage(_("Unknown argument")); | 816 | usage(_("Unknown argument")); |
817 | case output_format_index: { | ||
818 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
819 | if (!parser.parsing_success) { | ||
820 | // TODO List all available formats here, maybe add anothoer usage function | ||
821 | printf("Invalid output format: %s\n", optarg); | ||
822 | exit(STATE_UNKNOWN); | ||
823 | } | ||
824 | |||
825 | result.config.output_format_is_set = true; | ||
826 | result.config.output_format = parser.output_format; | ||
827 | break; | ||
828 | } | ||
861 | } | 829 | } |
862 | } | 830 | } |
863 | 831 | ||
864 | /* Support for "check_disk warn crit [fs]" with thresholds at used% level */ | 832 | /* Support for "check_disk warn crit [fs]" with thresholds at used% level */ |
865 | c = optind; | 833 | int index = optind; |
866 | if (warn_usedspace_percent == NULL && argc > c && is_intnonneg(argv[c])) | 834 | |
867 | warn_usedspace_percent = argv[c++]; | 835 | if (argc > index && is_intnonneg(argv[index])) { |
836 | if (verbose > 0) { | ||
837 | printf("Got an positional warn threshold: %s\n", argv[index]); | ||
838 | } | ||
839 | char *range = argv[index++]; | ||
840 | mp_range_parsed tmp = mp_parse_range_string(range); | ||
841 | if (tmp.error != MP_PARSING_SUCCES) { | ||
842 | die(STATE_UNKNOWN, "failed to parse warning threshold"); | ||
843 | } | ||
844 | |||
845 | mp_range tmp_range = tmp.range; | ||
846 | // Invert range to use it for free instead of used | ||
847 | // tmp_range.alert_on_inside_range = !tmp_range.alert_on_inside_range; | ||
868 | 848 | ||
869 | if (crit_usedspace_percent == NULL && argc > c && is_intnonneg(argv[c])) | 849 | warn_freespace_percent = mp_range_to_string(tmp_range); |
870 | crit_usedspace_percent = argv[c++]; | ||
871 | 850 | ||
872 | if (argc > c) { | 851 | if (verbose > 0) { |
873 | se = np_add_parameter(&path_select_list, strdup(argv[c++])); | 852 | printf("Positional warning threshold transformed to: %s\n", warn_freespace_percent); |
853 | } | ||
854 | } | ||
855 | |||
856 | if (argc > index && is_intnonneg(argv[index])) { | ||
857 | if (verbose > 0) { | ||
858 | printf("Got an positional crit threshold: %s\n", argv[index]); | ||
859 | } | ||
860 | char *range = argv[index++]; | ||
861 | mp_range_parsed tmp = mp_parse_range_string(range); | ||
862 | if (tmp.error != MP_PARSING_SUCCES) { | ||
863 | die(STATE_UNKNOWN, "failed to parse warning threshold"); | ||
864 | } | ||
865 | |||
866 | mp_range tmp_range = tmp.range; | ||
867 | // Invert range to use it for free instead of used | ||
868 | // tmp_range.alert_on_inside_range = !tmp_range.alert_on_inside_range; | ||
869 | |||
870 | crit_freespace_percent = mp_range_to_string(tmp_range); | ||
871 | |||
872 | if (verbose > 0) { | ||
873 | printf("Positional critical threshold transformed to: %s\n", crit_freespace_percent); | ||
874 | } | ||
875 | } | ||
876 | |||
877 | if (argc > index) { | ||
878 | if (verbose > 0) { | ||
879 | printf("Got an positional filesystem: %s\n", argv[index]); | ||
880 | } | ||
881 | struct parameter_list *se = | ||
882 | mp_int_fs_list_append(&result.config.path_select_list, strdup(argv[index++])); | ||
874 | path_selected = true; | 883 | path_selected = true; |
875 | set_all_thresholds(se); | 884 | set_all_thresholds(se, warn_freespace_units, crit_freespace_units, warn_freespace_percent, |
885 | crit_freespace_percent, warn_freeinodes_percent, | ||
886 | crit_freeinodes_percent); | ||
876 | } | 887 | } |
877 | 888 | ||
878 | if (units == NULL) { | 889 | // If a list of paths has not been explicitly selected, find entire |
879 | units = strdup("MiB"); | 890 | // mount list and create list of paths |
880 | mult = (uintmax_t)1024 * 1024; | 891 | if (!path_selected && !result.config.path_ignored) { |
892 | for (struct mount_entry *me = result.config.mount_list; me; me = me->me_next) { | ||
893 | if (me->me_dummy != 0) { | ||
894 | // just do not add dummy filesystems | ||
895 | continue; | ||
896 | } | ||
897 | |||
898 | parameter_list_elem *path = NULL; | ||
899 | if (!(path = mp_int_fs_list_find(result.config.path_select_list, me->me_mountdir))) { | ||
900 | path = mp_int_fs_list_append(&result.config.path_select_list, me->me_mountdir); | ||
901 | } | ||
902 | path->best_match = me; | ||
903 | path->group = group; | ||
904 | set_all_thresholds(path, warn_freespace_units, crit_freespace_units, | ||
905 | warn_freespace_percent, crit_freespace_percent, | ||
906 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
907 | } | ||
881 | } | 908 | } |
882 | 909 | ||
883 | return true; | 910 | // Set thresholds to the appropriate unit |
911 | for (parameter_list_elem *tmp = result.config.path_select_list.first; tmp; | ||
912 | tmp = mp_int_fs_list_get_next(tmp)) { | ||
913 | |||
914 | mp_perfdata_value factor = mp_create_pd_value(unit); | ||
915 | |||
916 | if (tmp->freespace_units.critical_is_set) { | ||
917 | tmp->freespace_units.critical = | ||
918 | mp_range_multiply(tmp->freespace_units.critical, factor); | ||
919 | } | ||
920 | if (tmp->freespace_units.warning_is_set) { | ||
921 | tmp->freespace_units.warning = mp_range_multiply(tmp->freespace_units.warning, factor); | ||
922 | } | ||
923 | } | ||
924 | |||
925 | return result; | ||
884 | } | 926 | } |
885 | 927 | ||
886 | void set_all_thresholds(struct parameter_list *path) { | 928 | void set_all_thresholds(parameter_list_elem *path, char *warn_freespace_units, |
887 | if (path->freespace_units != NULL) | 929 | char *crit_freespace_units, char *warn_freespace_percent, |
888 | free(path->freespace_units); | 930 | char *crit_freespace_percent, char *warn_freeinodes_percent, |
889 | set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units); | 931 | char *crit_freeinodes_percent) { |
890 | if (path->freespace_percent != NULL) | 932 | mp_range_parsed tmp; |
891 | free(path->freespace_percent); | 933 | |
892 | set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent); | 934 | if (warn_freespace_units) { |
893 | if (path->usedspace_units != NULL) | 935 | tmp = mp_parse_range_string(warn_freespace_units); |
894 | free(path->usedspace_units); | 936 | path->freespace_units = mp_thresholds_set_warn(path->freespace_units, tmp.range); |
895 | set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units); | 937 | } |
896 | if (path->usedspace_percent != NULL) | 938 | |
897 | free(path->usedspace_percent); | 939 | if (crit_freespace_units) { |
898 | set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent); | 940 | tmp = mp_parse_range_string(crit_freespace_units); |
899 | if (path->usedinodes_percent != NULL) | 941 | path->freespace_units = mp_thresholds_set_crit(path->freespace_units, tmp.range); |
900 | free(path->usedinodes_percent); | 942 | } |
901 | set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent); | 943 | |
902 | if (path->freeinodes_percent != NULL) | 944 | if (warn_freespace_percent) { |
903 | free(path->freeinodes_percent); | 945 | tmp = mp_parse_range_string(warn_freespace_percent); |
904 | set_thresholds(&path->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent); | 946 | path->freespace_percent = mp_thresholds_set_warn(path->freespace_percent, tmp.range); |
947 | } | ||
948 | |||
949 | if (crit_freespace_percent) { | ||
950 | tmp = mp_parse_range_string(crit_freespace_percent); | ||
951 | path->freespace_percent = mp_thresholds_set_crit(path->freespace_percent, tmp.range); | ||
952 | } | ||
953 | |||
954 | if (warn_freeinodes_percent) { | ||
955 | tmp = mp_parse_range_string(warn_freeinodes_percent); | ||
956 | path->freeinodes_percent = mp_thresholds_set_warn(path->freeinodes_percent, tmp.range); | ||
957 | } | ||
958 | |||
959 | if (crit_freeinodes_percent) { | ||
960 | tmp = mp_parse_range_string(crit_freeinodes_percent); | ||
961 | path->freeinodes_percent = mp_thresholds_set_crit(path->freeinodes_percent, tmp.range); | ||
962 | } | ||
905 | } | 963 | } |
906 | 964 | ||
907 | void print_help(void) { | 965 | void print_help(void) { |
@@ -911,7 +969,8 @@ void print_help(void) { | |||
911 | printf(COPYRIGHT, copyright, email); | 969 | printf(COPYRIGHT, copyright, email); |
912 | 970 | ||
913 | printf("%s\n", _("This plugin checks the amount of used disk space on a mounted file system")); | 971 | printf("%s\n", _("This plugin checks the amount of used disk space on a mounted file system")); |
914 | printf("%s\n", _("and generates an alert if free space is less than one of the threshold values")); | 972 | printf("%s\n", |
973 | _("and generates an alert if free space is less than one of the threshold values")); | ||
915 | 974 | ||
916 | printf("\n\n"); | 975 | printf("\n\n"); |
917 | 976 | ||
@@ -933,7 +992,8 @@ void print_help(void) { | |||
933 | printf(" %s\n", "-K, --icritical=PERCENT%"); | 992 | printf(" %s\n", "-K, --icritical=PERCENT%"); |
934 | printf(" %s\n", _("Exit with CRITICAL status if less than PERCENT of inode space is free")); | 993 | printf(" %s\n", _("Exit with CRITICAL status if less than PERCENT of inode space is free")); |
935 | printf(" %s\n", "-p, --path=PATH, --partition=PARTITION"); | 994 | printf(" %s\n", "-p, --path=PATH, --partition=PARTITION"); |
936 | printf(" %s\n", _("Mount point or block device as emitted by the mount(8) command (may be repeated)")); | 995 | printf(" %s\n", |
996 | _("Mount point or block device as emitted by the mount(8) command (may be repeated)")); | ||
937 | printf(" %s\n", "-x, --exclude_device=PATH <STRING>"); | 997 | printf(" %s\n", "-x, --exclude_device=PATH <STRING>"); |
938 | printf(" %s\n", _("Ignore device (only works if -p unspecified)")); | 998 | printf(" %s\n", _("Ignore device (only works if -p unspecified)")); |
939 | printf(" %s\n", "-C, --clear"); | 999 | printf(" %s\n", "-C, --clear"); |
@@ -947,167 +1007,298 @@ void print_help(void) { | |||
947 | printf(" %s\n", "-P, --iperfdata"); | 1007 | printf(" %s\n", "-P, --iperfdata"); |
948 | printf(" %s\n", _("Display inode usage in perfdata")); | 1008 | printf(" %s\n", _("Display inode usage in perfdata")); |
949 | printf(" %s\n", "-g, --group=NAME"); | 1009 | printf(" %s\n", "-g, --group=NAME"); |
950 | printf(" %s\n", _("Group paths. Thresholds apply to (free-)space of all partitions together")); | 1010 | printf(" %s\n", |
951 | printf(" %s\n", "-k, --kilobytes"); | 1011 | _("Group paths. Thresholds apply to (free-)space of all partitions together")); |
952 | printf(" %s\n", _("Same as '--units kB'")); | ||
953 | printf(" %s\n", "-l, --local"); | 1012 | printf(" %s\n", "-l, --local"); |
954 | printf(" %s\n", _("Only check local filesystems")); | 1013 | printf(" %s\n", _("Only check local filesystems")); |
955 | printf(" %s\n", "-L, --stat-remote-fs"); | 1014 | printf(" %s\n", "-L, --stat-remote-fs"); |
956 | printf(" %s\n", _("Only check local filesystems against thresholds. Yet call stat on remote filesystems")); | 1015 | printf( |
1016 | " %s\n", | ||
1017 | _("Only check local filesystems against thresholds. Yet call stat on remote filesystems")); | ||
957 | printf(" %s\n", _("to test if they are accessible (e.g. to detect Stale NFS Handles)")); | 1018 | printf(" %s\n", _("to test if they are accessible (e.g. to detect Stale NFS Handles)")); |
958 | printf(" %s\n", "-M, --mountpoint"); | 1019 | printf(" %s\n", "-M, --mountpoint"); |
959 | printf(" %s\n", _("Display the (block) device instead of the mount point")); | 1020 | printf(" %s\n", _("Display the (block) device instead of the mount point")); |
960 | printf(" %s\n", "-m, --megabytes"); | ||
961 | printf(" %s\n", _("Same as '--units MB'")); | ||
962 | printf(" %s\n", "-A, --all"); | 1021 | printf(" %s\n", "-A, --all"); |
963 | printf(" %s\n", _("Explicitly select all paths. This is equivalent to -R '.*'")); | 1022 | printf(" %s\n", _("Explicitly select all paths. This is equivalent to -R '.*'")); |
964 | printf(" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION"); | 1023 | printf(" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION"); |
965 | printf(" %s\n", _("Case insensitive regular expression for path/partition (may be repeated)")); | 1024 | printf(" %s\n", |
1025 | _("Case insensitive regular expression for path/partition (may be repeated)")); | ||
966 | printf(" %s\n", "-r, --ereg-path=PATH, --ereg-partition=PARTITION"); | 1026 | printf(" %s\n", "-r, --ereg-path=PATH, --ereg-partition=PARTITION"); |
967 | printf(" %s\n", _("Regular expression for path or partition (may be repeated)")); | 1027 | printf(" %s\n", _("Regular expression for path or partition (may be repeated)")); |
968 | printf(" %s\n", "-I, --ignore-eregi-path=PATH, --ignore-eregi-partition=PARTITION"); | 1028 | printf(" %s\n", "-I, --ignore-eregi-path=PATH, --ignore-eregi-partition=PARTITION"); |
969 | printf(" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) (may be repeated)")); | 1029 | printf(" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) " |
1030 | "(may be repeated)")); | ||
970 | printf(" %s\n", "-i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION"); | 1031 | printf(" %s\n", "-i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION"); |
971 | printf(" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)")); | 1032 | printf(" %s\n", |
1033 | _("Regular expression to ignore selected path or partition (may be repeated)")); | ||
972 | printf(" %s\n", "-n, --ignore-missing"); | 1034 | printf(" %s\n", "-n, --ignore-missing"); |
973 | printf(" %s\n", _("Return OK if no filesystem matches, filesystem does not exist or is inaccessible.")); | 1035 | printf(" %s\n", |
1036 | _("Return OK if no filesystem matches, filesystem does not exist or is inaccessible.")); | ||
974 | printf(" %s\n", _("(Provide this option before -p / -r / --ereg-path if used)")); | 1037 | printf(" %s\n", _("(Provide this option before -p / -r / --ereg-path if used)")); |
975 | printf(UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 1038 | printf(UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
976 | printf(" %s\n", "-u, --units=STRING"); | 1039 | printf(" %s\n", "-u, --units=STRING"); |
977 | printf(" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); | 1040 | printf(" %s\n", _("Select the unit used for the absolute value thresholds")); |
1041 | printf(" %s\n", _("Choose one of \"bytes\", \"KiB\", \"kB\", \"MiB\", \"MB\", \"GiB\", " | ||
1042 | "\"GB\", \"TiB\", \"TB\", \"PiB\", \"PB\" (default: MiB)")); | ||
1043 | printf(" %s\n", "-k, --kilobytes"); | ||
1044 | printf(" %s\n", _("Same as '--units kB'")); | ||
1045 | printf(" %s\n", "--display-unit"); | ||
1046 | printf(" %s\n", _("Select the unit used for in the output")); | ||
1047 | printf(" %s\n", _("Choose one of \"bytes\", \"KiB\", \"kB\", \"MiB\", \"MB\", \"GiB\", " | ||
1048 | "\"GB\", \"TiB\", \"TB\", \"PiB\", \"PB\" (default: MiB)")); | ||
1049 | printf(" %s\n", "-m, --megabytes"); | ||
1050 | printf(" %s\n", _("Same as '--units MB'")); | ||
978 | printf(UT_VERBOSE); | 1051 | printf(UT_VERBOSE); |
979 | printf(" %s\n", "-X, --exclude-type=TYPE_REGEX"); | 1052 | printf(" %s\n", "-X, --exclude-type=TYPE_REGEX"); |
980 | printf(" %s\n", _("Ignore all filesystems of types matching given regex(7) (may be repeated)")); | 1053 | printf(" %s\n", |
1054 | _("Ignore all filesystems of types matching given regex(7) (may be repeated)")); | ||
981 | printf(" %s\n", "-N, --include-type=TYPE_REGEX"); | 1055 | printf(" %s\n", "-N, --include-type=TYPE_REGEX"); |
982 | printf(" %s\n", _("Check only filesystems where the type matches this given regex(7) (may be repeated)")); | 1056 | printf( |
1057 | " %s\n", | ||
1058 | _("Check only filesystems where the type matches this given regex(7) (may be repeated)")); | ||
1059 | printf(UT_OUTPUT_FORMAT); | ||
983 | 1060 | ||
984 | printf("\n"); | 1061 | printf("\n"); |
985 | printf("%s\n", _("General usage hints:")); | 1062 | printf("%s\n", _("General usage hints:")); |
986 | printf(" %s\n", _("- Arguments are positional! \"-w 5 -c 1 -p /foo -w6 -c2 -p /bar\" is not the same as")); | 1063 | printf( |
1064 | " %s\n", | ||
1065 | _("- Arguments are positional! \"-w 5 -c 1 -p /foo -w6 -c2 -p /bar\" is not the same as")); | ||
987 | printf(" %s\n", _("\"-w 5 -c 1 -p /bar w6 -c2 -p /foo\".")); | 1066 | printf(" %s\n", _("\"-w 5 -c 1 -p /bar w6 -c2 -p /foo\".")); |
988 | printf(" %s\n", _("- The syntax is broadly: \"{thresholds a} {paths a} -C {thresholds b} {thresholds b} ...\"")); | 1067 | printf(" %s\n", _("- The syntax is broadly: \"{thresholds a} {paths a} -C {thresholds b} " |
1068 | "{thresholds b} ...\"")); | ||
989 | 1069 | ||
990 | printf("\n"); | 1070 | printf("\n"); |
991 | printf("%s\n", _("Examples:")); | 1071 | printf("%s\n", _("Examples:")); |
992 | printf(" %s\n", "check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /"); | 1072 | printf(" %s\n", "check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /"); |
993 | printf(" %s\n\n", _("Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB")); | 1073 | printf(" %s\n\n", _("Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB")); |
994 | printf(" %s\n", "check_disk -w 100 -c 50 -C -w 1000 -c 500 -g sidDATA -r '^/oracle/SID/data.*$'"); | 1074 | printf(" %s\n", |
995 | printf(" %s\n", _("Checks all filesystems not matching -r at 100M and 50M. The fs matching the -r regex")); | 1075 | "check_disk -w 100 -c 50 -C -w 1000 -c 500 -g sidDATA -r '^/oracle/SID/data.*$'"); |
996 | printf(" %s\n\n", _("are grouped which means the freespace thresholds are applied to all disks together")); | 1076 | printf( |
1077 | " %s\n", | ||
1078 | _("Checks all filesystems not matching -r at 100M and 50M. The fs matching the -r regex")); | ||
1079 | printf(" %s\n\n", | ||
1080 | _("are grouped which means the freespace thresholds are applied to all disks together")); | ||
997 | printf(" %s\n", "check_disk -w 100 -c 50 -C -w 1000 -c 500 -p /foo -C -w 5% -c 3% -p /bar"); | 1081 | printf(" %s\n", "check_disk -w 100 -c 50 -C -w 1000 -c 500 -p /foo -C -w 5% -c 3% -p /bar"); |
998 | printf(" %s\n", _("Checks /foo for 1000M/500M and /bar for 5/3%. All remaining volumes use 100M/50M")); | 1082 | printf(" %s\n", |
1083 | _("Checks /foo for 1000M/500M and /bar for 5/3%. All remaining volumes use 100M/50M")); | ||
999 | 1084 | ||
1000 | printf(UT_SUPPORT); | 1085 | printf(UT_SUPPORT); |
1001 | } | 1086 | } |
1002 | 1087 | ||
1003 | void print_usage(void) { | 1088 | void print_usage(void) { |
1004 | printf("%s\n", _("Usage:")); | 1089 | printf("%s\n", _("Usage:")); |
1005 | printf(" %s {-w absolute_limit |-w percentage_limit%% | -W inode_percentage_limit } {-c absolute_limit|-c percentage_limit%% | -K " | 1090 | printf(" %s {-w absolute_limit |-w percentage_limit%% | -W inode_percentage_limit } {-c " |
1091 | "absolute_limit|-c percentage_limit%% | -K " | ||
1006 | "inode_percentage_limit } {-p path | -x device}\n", | 1092 | "inode_percentage_limit } {-p path | -x device}\n", |
1007 | progname); | 1093 | progname); |
1008 | printf("[-C] [-E] [-e] [-f] [-g group ] [-k] [-l] [-M] [-m] [-R path ] [-r path ]\n"); | 1094 | printf("[-C] [-E] [-e] [-f] [-g group ] [-k] [-l] [-M] [-m] [-R path ] [-r path ]\n"); |
1009 | printf("[-t timeout] [-u unit] [-v] [-X type_regex] [-N type]\n"); | 1095 | printf("[-t timeout] [-u unit] [-v] [-X type_regex] [-N type]\n"); |
1010 | } | 1096 | } |
1011 | 1097 | ||
1012 | bool stat_path(struct parameter_list *p) { | 1098 | bool stat_path(parameter_list_elem *parameters, bool ignore_missing) { |
1013 | /* Stat entry to check that dir exists and is accessible */ | 1099 | /* Stat entry to check that dir exists and is accessible */ |
1014 | if (verbose >= 3) | 1100 | if (verbose >= 3) { |
1015 | printf("calling stat on %s\n", p->name); | 1101 | printf("calling stat on %s\n", parameters->name); |
1016 | if (stat(p->name, &stat_buf[0])) { | 1102 | } |
1017 | if (verbose >= 3) | 1103 | |
1018 | printf("stat failed on %s\n", p->name); | 1104 | struct stat stat_buf = {0}; |
1019 | if (ignore_missing == true) { | 1105 | if (stat(parameters->name, &stat_buf)) { |
1106 | if (verbose >= 3) { | ||
1107 | printf("stat failed on %s\n", parameters->name); | ||
1108 | } | ||
1109 | if (ignore_missing) { | ||
1020 | return false; | 1110 | return false; |
1021 | } | 1111 | } |
1022 | printf("DISK %s - ", _("CRITICAL")); | 1112 | printf("DISK %s - ", _("CRITICAL")); |
1023 | die(STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); | 1113 | die(STATE_CRITICAL, _("%s %s: %s\n"), parameters->name, _("is not accessible"), |
1114 | strerror(errno)); | ||
1024 | } | 1115 | } |
1116 | |||
1025 | return true; | 1117 | return true; |
1026 | } | 1118 | } |
1027 | 1119 | ||
1028 | void get_stats(struct parameter_list *p, struct fs_usage *fsp) { | 1120 | static parameter_list_elem get_path_stats(parameter_list_elem parameters, const struct fs_usage fsp, |
1029 | struct parameter_list *p_list; | 1121 | bool freespace_ignore_reserved) { |
1030 | struct fs_usage tmpfsp; | 1122 | uintmax_t available = fsp.fsu_bavail; |
1031 | int first = 1; | 1123 | uintmax_t available_to_root = fsp.fsu_bfree; |
1124 | uintmax_t used = fsp.fsu_blocks - fsp.fsu_bfree; | ||
1125 | uintmax_t total; | ||
1032 | 1126 | ||
1033 | if (p->group == NULL) { | ||
1034 | get_path_stats(p, fsp); | ||
1035 | } else { | ||
1036 | /* find all group members */ | ||
1037 | for (p_list = path_select_list; p_list; p_list = p_list->name_next) { | ||
1038 | #ifdef __CYGWIN__ | ||
1039 | if (strncmp(p_list->name, "/cygdrive/", 10) != 0) | ||
1040 | continue; | ||
1041 | #endif | ||
1042 | if (p_list->group && !(strcmp(p_list->group, p->group))) { | ||
1043 | if (!stat_path(p_list)) | ||
1044 | continue; | ||
1045 | get_fs_usage(p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp); | ||
1046 | get_path_stats(p_list, &tmpfsp); | ||
1047 | if (verbose >= 3) | ||
1048 | printf("Group %s: adding %lu blocks sized %lu, (%s) used_units=%lu free_units=%lu total_units=%lu mult=%lu\n", | ||
1049 | p_list->group, tmpfsp.fsu_blocks, tmpfsp.fsu_blocksize, p_list->best_match->me_mountdir, p_list->dused_units, | ||
1050 | p_list->dfree_units, p_list->dtotal_units, mult); | ||
1051 | |||
1052 | /* prevent counting the first FS of a group twice since its parameter_list entry | ||
1053 | * is used to carry the information of all file systems of the entire group */ | ||
1054 | if (!first) { | ||
1055 | p->total += p_list->total; | ||
1056 | p->available += p_list->available; | ||
1057 | p->available_to_root += p_list->available_to_root; | ||
1058 | p->used += p_list->used; | ||
1059 | |||
1060 | p->dused_units += p_list->dused_units; | ||
1061 | p->dfree_units += p_list->dfree_units; | ||
1062 | p->dtotal_units += p_list->dtotal_units; | ||
1063 | p->inodes_total += p_list->inodes_total; | ||
1064 | p->inodes_free += p_list->inodes_free; | ||
1065 | p->inodes_free_to_root += p_list->inodes_free_to_root; | ||
1066 | p->inodes_used += p_list->inodes_used; | ||
1067 | } | ||
1068 | first = 0; | ||
1069 | } | ||
1070 | if (verbose >= 3) | ||
1071 | printf("Group %s now has: used_units=%lu free_units=%lu total_units=%lu fsu_blocksize=%lu mult=%lu\n", p->group, | ||
1072 | p->dused_units, p->dfree_units, p->dtotal_units, tmpfsp.fsu_blocksize, mult); | ||
1073 | } | ||
1074 | /* modify devname and mountdir for output */ | ||
1075 | p->best_match->me_mountdir = p->best_match->me_devname = p->group; | ||
1076 | } | ||
1077 | /* finally calculate percentages for either plain FS or summed up group */ | ||
1078 | p->dused_pct = calculate_percent(p->used, p->used + p->available); /* used + available can never be > uintmax */ | ||
1079 | p->dfree_pct = 100.0 - p->dused_pct; | ||
1080 | p->dused_inodes_percent = calculate_percent(p->inodes_total - p->inodes_free, p->inodes_total); | ||
1081 | p->dfree_inodes_percent = 100 - p->dused_inodes_percent; | ||
1082 | } | ||
1083 | |||
1084 | void get_path_stats(struct parameter_list *p, struct fs_usage *fsp) { | ||
1085 | p->available = fsp->fsu_bavail; | ||
1086 | p->available_to_root = fsp->fsu_bfree; | ||
1087 | p->used = fsp->fsu_blocks - fsp->fsu_bfree; | ||
1088 | if (freespace_ignore_reserved) { | 1127 | if (freespace_ignore_reserved) { |
1089 | /* option activated : we subtract the root-reserved space from the total */ | 1128 | /* option activated : we subtract the root-reserved space from the total */ |
1090 | p->total = fsp->fsu_blocks - p->available_to_root + p->available; | 1129 | total = fsp.fsu_blocks - available_to_root + available; |
1091 | } else { | 1130 | } else { |
1092 | /* default behaviour : take all the blocks into account */ | 1131 | /* default behaviour : take all the blocks into account */ |
1093 | p->total = fsp->fsu_blocks; | 1132 | total = fsp.fsu_blocks; |
1094 | } | 1133 | } |
1095 | 1134 | ||
1096 | p->dused_units = p->used * fsp->fsu_blocksize / mult; | 1135 | parameters.used_bytes = used * fsp.fsu_blocksize; |
1097 | p->dfree_units = p->available * fsp->fsu_blocksize / mult; | 1136 | parameters.free_bytes = available * fsp.fsu_blocksize; |
1098 | p->dtotal_units = p->total * fsp->fsu_blocksize / mult; | 1137 | parameters.total_bytes = total * fsp.fsu_blocksize; |
1138 | |||
1099 | /* Free file nodes. Not sure the workaround is required, but in case...*/ | 1139 | /* Free file nodes. Not sure the workaround is required, but in case...*/ |
1100 | p->inodes_free = fsp->fsu_ffree; | 1140 | parameters.inodes_free = fsp.fsu_ffree; |
1101 | p->inodes_free_to_root = fsp->fsu_ffree; /* Free file nodes for root. */ | 1141 | parameters.inodes_free_to_root = fsp.fsu_ffree; /* Free file nodes for root. */ |
1102 | p->inodes_used = fsp->fsu_files - fsp->fsu_ffree; | 1142 | parameters.inodes_used = fsp.fsu_files - fsp.fsu_ffree; |
1143 | |||
1103 | if (freespace_ignore_reserved) { | 1144 | if (freespace_ignore_reserved) { |
1104 | /* option activated : we subtract the root-reserved inodes from the total */ | 1145 | /* option activated : we subtract the root-reserved inodes from the total */ |
1105 | /* not all OS report fsp->fsu_favail, only the ones with statvfs syscall */ | 1146 | /* not all OS report fsp->fsu_favail, only the ones with statvfs syscall */ |
1106 | /* for others, fsp->fsu_ffree == fsp->fsu_favail */ | 1147 | /* for others, fsp->fsu_ffree == fsp->fsu_favail */ |
1107 | p->inodes_total = fsp->fsu_files - p->inodes_free_to_root + p->inodes_free; | 1148 | parameters.inodes_total = |
1149 | fsp.fsu_files - parameters.inodes_free_to_root + parameters.inodes_free; | ||
1108 | } else { | 1150 | } else { |
1109 | /* default behaviour : take all the inodes into account */ | 1151 | /* default behaviour : take all the inodes into account */ |
1110 | p->inodes_total = fsp->fsu_files; | 1152 | parameters.inodes_total = fsp.fsu_files; |
1111 | } | 1153 | } |
1112 | np_add_name(&seen, p->best_match->me_mountdir); | 1154 | |
1155 | return parameters; | ||
1156 | } | ||
1157 | |||
1158 | mp_subcheck evaluate_filesystem(measurement_unit measurement_unit, bool display_inodes_perfdata, | ||
1159 | byte_unit unit) { | ||
1160 | mp_subcheck result = mp_subcheck_init(); | ||
1161 | result = mp_set_subcheck_default_state(result, STATE_UNKNOWN); | ||
1162 | xasprintf(&result.output, "%s", measurement_unit.name); | ||
1163 | |||
1164 | if (!measurement_unit.is_group && measurement_unit.filesystem_type) { | ||
1165 | xasprintf(&result.output, "%s (%s)", result.output, measurement_unit.filesystem_type); | ||
1166 | } | ||
1167 | |||
1168 | /* Threshold comparisons */ | ||
1169 | |||
1170 | // =============================== | ||
1171 | // Free space absolute values test | ||
1172 | mp_subcheck freespace_bytes_sc = mp_subcheck_init(); | ||
1173 | freespace_bytes_sc = mp_set_subcheck_default_state(freespace_bytes_sc, STATE_OK); | ||
1174 | |||
1175 | if (unit != Humanized) { | ||
1176 | xasprintf(&freespace_bytes_sc.output, "Free space absolute: %ju%s (of %ju%s)", | ||
1177 | (uintmax_t)(measurement_unit.free_bytes / unit), get_unit_string(unit), | ||
1178 | (uintmax_t)(measurement_unit.total_bytes / unit), get_unit_string(unit)); | ||
1179 | } else { | ||
1180 | xasprintf(&freespace_bytes_sc.output, "Free space absolute: %s (of %s)", | ||
1181 | humanize_byte_value(measurement_unit.free_bytes, false), | ||
1182 | humanize_byte_value((unsigned long long)measurement_unit.total_bytes, false)); | ||
1183 | } | ||
1184 | |||
1185 | mp_perfdata used_space = perfdata_init(); | ||
1186 | used_space.label = measurement_unit.name; | ||
1187 | used_space.value = mp_create_pd_value(measurement_unit.free_bytes); | ||
1188 | used_space = mp_set_pd_max_value(used_space, mp_create_pd_value(measurement_unit.total_bytes)); | ||
1189 | used_space = mp_set_pd_min_value(used_space, mp_create_pd_value(0)); | ||
1190 | used_space.uom = "B"; | ||
1191 | used_space = mp_pd_set_thresholds(used_space, measurement_unit.freespace_bytes_thresholds); | ||
1192 | freespace_bytes_sc = mp_set_subcheck_state(freespace_bytes_sc, mp_get_pd_status(used_space)); | ||
1193 | |||
1194 | // special case for absolute space thresholds here: | ||
1195 | // if absolute values are not set, compute the thresholds from percentage thresholds | ||
1196 | mp_thresholds temp_thlds = measurement_unit.freespace_bytes_thresholds; | ||
1197 | if (!temp_thlds.critical_is_set && | ||
1198 | measurement_unit.freespace_percent_thresholds.critical_is_set) { | ||
1199 | mp_range tmp_range = measurement_unit.freespace_percent_thresholds.critical; | ||
1200 | |||
1201 | if (!tmp_range.end_infinity) { | ||
1202 | tmp_range.end = mp_create_pd_value(mp_get_pd_value(tmp_range.end) / 100 * | ||
1203 | measurement_unit.total_bytes); | ||
1204 | } | ||
1205 | |||
1206 | if (!tmp_range.start_infinity) { | ||
1207 | tmp_range.start = mp_create_pd_value(mp_get_pd_value(tmp_range.start) / 100 * | ||
1208 | measurement_unit.total_bytes); | ||
1209 | } | ||
1210 | measurement_unit.freespace_bytes_thresholds = | ||
1211 | mp_thresholds_set_crit(measurement_unit.freespace_bytes_thresholds, tmp_range); | ||
1212 | used_space = mp_pd_set_thresholds(used_space, measurement_unit.freespace_bytes_thresholds); | ||
1213 | } | ||
1214 | |||
1215 | if (!temp_thlds.warning_is_set && | ||
1216 | measurement_unit.freespace_percent_thresholds.warning_is_set) { | ||
1217 | mp_range tmp_range = measurement_unit.freespace_percent_thresholds.warning; | ||
1218 | if (!tmp_range.end_infinity) { | ||
1219 | tmp_range.end = mp_create_pd_value(mp_get_pd_value(tmp_range.end) / 100 * | ||
1220 | measurement_unit.total_bytes); | ||
1221 | } | ||
1222 | if (!tmp_range.start_infinity) { | ||
1223 | tmp_range.start = mp_create_pd_value(mp_get_pd_value(tmp_range.start) / 100 * | ||
1224 | measurement_unit.total_bytes); | ||
1225 | } | ||
1226 | measurement_unit.freespace_bytes_thresholds = | ||
1227 | mp_thresholds_set_warn(measurement_unit.freespace_bytes_thresholds, tmp_range); | ||
1228 | used_space = mp_pd_set_thresholds(used_space, measurement_unit.freespace_bytes_thresholds); | ||
1229 | } | ||
1230 | |||
1231 | mp_add_perfdata_to_subcheck(&freespace_bytes_sc, used_space); | ||
1232 | mp_add_subcheck_to_subcheck(&result, freespace_bytes_sc); | ||
1233 | |||
1234 | // ========================== | ||
1235 | // Free space percentage test | ||
1236 | mp_subcheck freespace_percent_sc = mp_subcheck_init(); | ||
1237 | freespace_percent_sc = mp_set_subcheck_default_state(freespace_percent_sc, STATE_OK); | ||
1238 | |||
1239 | double free_percentage = | ||
1240 | calculate_percent(measurement_unit.free_bytes, measurement_unit.total_bytes); | ||
1241 | xasprintf(&freespace_percent_sc.output, "Free space percentage: %g%%", free_percentage); | ||
1242 | |||
1243 | // Using perfdata here just to get to the test result | ||
1244 | mp_perfdata free_space_percent_pd = perfdata_init(); | ||
1245 | free_space_percent_pd.value = mp_create_pd_value(free_percentage); | ||
1246 | free_space_percent_pd = | ||
1247 | mp_pd_set_thresholds(free_space_percent_pd, measurement_unit.freespace_percent_thresholds); | ||
1248 | |||
1249 | freespace_percent_sc = | ||
1250 | mp_set_subcheck_state(freespace_percent_sc, mp_get_pd_status(free_space_percent_pd)); | ||
1251 | mp_add_subcheck_to_subcheck(&result, freespace_percent_sc); | ||
1252 | |||
1253 | // ================ | ||
1254 | // Free inodes test | ||
1255 | // Only ever useful if the number of inodes is static (e.g. ext4), | ||
1256 | // not when it is dynamic (e.g btrfs) | ||
1257 | // Assumption: if the total number of inodes == 0, we have such a case and just skip the test | ||
1258 | if (measurement_unit.inodes_total > 0) { | ||
1259 | mp_subcheck freeindodes_percent_sc = mp_subcheck_init(); | ||
1260 | freeindodes_percent_sc = mp_set_subcheck_default_state(freeindodes_percent_sc, STATE_OK); | ||
1261 | |||
1262 | double free_inode_percentage = | ||
1263 | calculate_percent(measurement_unit.inodes_free, measurement_unit.inodes_total); | ||
1264 | |||
1265 | if (verbose > 0) { | ||
1266 | printf("free inode percentage computed: %g\n", free_inode_percentage); | ||
1267 | } | ||
1268 | |||
1269 | xasprintf(&freeindodes_percent_sc.output, "Inodes free: %g%% (%ju of %ju)", | ||
1270 | free_inode_percentage, measurement_unit.inodes_free, | ||
1271 | measurement_unit.inodes_total); | ||
1272 | |||
1273 | mp_perfdata inodes_pd = perfdata_init(); | ||
1274 | xasprintf(&inodes_pd.label, "%s (inodes)", measurement_unit.name); | ||
1275 | inodes_pd = mp_set_pd_value(inodes_pd, measurement_unit.inodes_used); | ||
1276 | inodes_pd = | ||
1277 | mp_set_pd_max_value(inodes_pd, mp_create_pd_value(measurement_unit.inodes_total)); | ||
1278 | inodes_pd = mp_set_pd_min_value(inodes_pd, mp_create_pd_value(0)); | ||
1279 | |||
1280 | mp_thresholds absolut_inode_thresholds = measurement_unit.freeinodes_percent_thresholds; | ||
1281 | |||
1282 | if (absolut_inode_thresholds.critical_is_set) { | ||
1283 | absolut_inode_thresholds.critical = | ||
1284 | mp_range_multiply(absolut_inode_thresholds.critical, | ||
1285 | mp_create_pd_value(measurement_unit.inodes_total / 100)); | ||
1286 | } | ||
1287 | if (absolut_inode_thresholds.warning_is_set) { | ||
1288 | absolut_inode_thresholds.warning = | ||
1289 | mp_range_multiply(absolut_inode_thresholds.warning, | ||
1290 | mp_create_pd_value(measurement_unit.inodes_total / 100)); | ||
1291 | } | ||
1292 | |||
1293 | inodes_pd = mp_pd_set_thresholds(inodes_pd, absolut_inode_thresholds); | ||
1294 | |||
1295 | freeindodes_percent_sc = | ||
1296 | mp_set_subcheck_state(freeindodes_percent_sc, mp_get_pd_status(inodes_pd)); | ||
1297 | if (display_inodes_perfdata) { | ||
1298 | mp_add_perfdata_to_subcheck(&freeindodes_percent_sc, inodes_pd); | ||
1299 | } | ||
1300 | mp_add_subcheck_to_subcheck(&result, freeindodes_percent_sc); | ||
1301 | } | ||
1302 | |||
1303 | return result; | ||
1113 | } | 1304 | } |
diff --git a/plugins/check_disk.d/utils_disk.c b/plugins/check_disk.d/utils_disk.c new file mode 100644 index 00000000..0b89018d --- /dev/null +++ b/plugins/check_disk.d/utils_disk.c | |||
@@ -0,0 +1,528 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Library for check_disk | ||
4 | * | ||
5 | * License: GPL | ||
6 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team | ||
7 | * | ||
8 | * Description: | ||
9 | * | ||
10 | * This file contains utilities for check_disk. These are tested by libtap | ||
11 | * | ||
12 | * | ||
13 | * This program is free software: you can redistribute it and/or modify | ||
14 | * it under the terms of the GNU General Public License as published by | ||
15 | * the Free Software Foundation, either version 3 of the License, or | ||
16 | * (at your option) any later version. | ||
17 | * | ||
18 | * This program is distributed in the hope that it will be useful, | ||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
21 | * GNU General Public License for more details. | ||
22 | * | ||
23 | * You should have received a copy of the GNU General Public License | ||
24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
25 | * | ||
26 | * | ||
27 | *****************************************************************************/ | ||
28 | |||
29 | #include "../common.h" | ||
30 | #include "utils_disk.h" | ||
31 | #include "../../gl/fsusage.h" | ||
32 | #include "../../lib/thresholds.h" | ||
33 | #include "../../lib/states.h" | ||
34 | #include <stdint.h> | ||
35 | #include <stdio.h> | ||
36 | #include <string.h> | ||
37 | #include <assert.h> | ||
38 | |||
39 | void np_add_name(struct name_list **list, const char *name) { | ||
40 | struct name_list *new_entry; | ||
41 | new_entry = (struct name_list *)malloc(sizeof *new_entry); | ||
42 | new_entry->name = (char *)name; | ||
43 | new_entry->next = *list; | ||
44 | *list = new_entry; | ||
45 | } | ||
46 | |||
47 | /* @brief Initialises a new regex at the begin of list via regcomp(3) | ||
48 | * | ||
49 | * @details if the regex fails to compile the error code of regcomp(3) is returned | ||
50 | * and list is not modified, otherwise list is modified to point to the new | ||
51 | * element | ||
52 | * @param list Pointer to a linked list of regex_list elements | ||
53 | * @param regex the string containing the regex which should be inserted into the list | ||
54 | * @param clags the cflags parameter for regcomp(3) | ||
55 | */ | ||
56 | int np_add_regex(struct regex_list **list, const char *regex, int cflags) { | ||
57 | struct regex_list *new_entry = (struct regex_list *)malloc(sizeof *new_entry); | ||
58 | |||
59 | if (new_entry == NULL) { | ||
60 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
61 | } | ||
62 | |||
63 | int regcomp_result = regcomp(&new_entry->regex, regex, cflags); | ||
64 | |||
65 | if (!regcomp_result) { | ||
66 | // regcomp succeeded | ||
67 | new_entry->next = *list; | ||
68 | *list = new_entry; | ||
69 | |||
70 | return 0; | ||
71 | } | ||
72 | // regcomp failed | ||
73 | free(new_entry); | ||
74 | |||
75 | return regcomp_result; | ||
76 | } | ||
77 | |||
78 | parameter_list_elem parameter_list_init(const char *name) { | ||
79 | parameter_list_elem result = { | ||
80 | .name = strdup(name), | ||
81 | .best_match = NULL, | ||
82 | |||
83 | .freespace_units = mp_thresholds_init(), | ||
84 | .freespace_percent = mp_thresholds_init(), | ||
85 | .freeinodes_percent = mp_thresholds_init(), | ||
86 | |||
87 | .group = NULL, | ||
88 | |||
89 | .inodes_total = 0, | ||
90 | .inodes_free = 0, | ||
91 | .inodes_free_to_root = 0, | ||
92 | .inodes_used = 0, | ||
93 | |||
94 | .used_bytes = 0, | ||
95 | .free_bytes = 0, | ||
96 | .total_bytes = 0, | ||
97 | |||
98 | .next = NULL, | ||
99 | .prev = NULL, | ||
100 | }; | ||
101 | return result; | ||
102 | } | ||
103 | |||
104 | /* Returns true if name is in list */ | ||
105 | bool np_find_name(struct name_list *list, const char *name) { | ||
106 | if (list == NULL || name == NULL) { | ||
107 | return false; | ||
108 | } | ||
109 | for (struct name_list *iterator = list; iterator; iterator = iterator->next) { | ||
110 | if (!strcmp(name, iterator->name)) { | ||
111 | return true; | ||
112 | } | ||
113 | } | ||
114 | return false; | ||
115 | } | ||
116 | |||
117 | /* Returns true if name is in list */ | ||
118 | bool np_find_regmatch(struct regex_list *list, const char *name) { | ||
119 | if (name == NULL) { | ||
120 | return false; | ||
121 | } | ||
122 | |||
123 | size_t len = strlen(name); | ||
124 | |||
125 | for (; list; list = list->next) { | ||
126 | /* Emulate a full match as if surrounded with ^( )$ | ||
127 | by checking whether the match spans the whole name */ | ||
128 | regmatch_t dummy_match; | ||
129 | if (!regexec(&list->regex, name, 1, &dummy_match, 0) && dummy_match.rm_so == 0 && | ||
130 | dummy_match.rm_eo == len) { | ||
131 | return true; | ||
132 | } | ||
133 | } | ||
134 | |||
135 | return false; | ||
136 | } | ||
137 | |||
138 | bool np_seen_name(struct name_list *list, const char *name) { | ||
139 | for (struct name_list *iterator = list; iterator; iterator = iterator->next) { | ||
140 | if (!strcmp(iterator->name, name)) { | ||
141 | return true; | ||
142 | } | ||
143 | } | ||
144 | return false; | ||
145 | } | ||
146 | |||
147 | bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re) { | ||
148 | return ((regexec(re, me->me_devname, (size_t)0, NULL, 0) == 0) || | ||
149 | (regexec(re, me->me_mountdir, (size_t)0, NULL, 0) == 0)); | ||
150 | } | ||
151 | |||
152 | check_disk_config check_disk_config_init() { | ||
153 | check_disk_config tmp = { | ||
154 | .erronly = false, | ||
155 | .display_mntp = false, | ||
156 | .show_local_fs = false, | ||
157 | .stat_remote_fs = false, | ||
158 | .display_inodes_perfdata = false, | ||
159 | |||
160 | .exact_match = false, | ||
161 | .freespace_ignore_reserved = false, | ||
162 | |||
163 | .ignore_missing = false, | ||
164 | .path_ignored = false, | ||
165 | |||
166 | // FS Filters | ||
167 | .fs_exclude_list = NULL, | ||
168 | .fs_include_list = NULL, | ||
169 | .device_path_exclude_list = NULL, | ||
170 | |||
171 | // Actual filesystems paths to investigate | ||
172 | .path_select_list = filesystem_list_init(), | ||
173 | |||
174 | .mount_list = NULL, | ||
175 | .seen = NULL, | ||
176 | |||
177 | .display_unit = Humanized, | ||
178 | // .unit = MebiBytes, | ||
179 | |||
180 | .output_format_is_set = false, | ||
181 | }; | ||
182 | return tmp; | ||
183 | } | ||
184 | |||
185 | char *get_unit_string(byte_unit_enum unit) { | ||
186 | switch (unit) { | ||
187 | case Bytes: | ||
188 | return "Bytes"; | ||
189 | case KibiBytes: | ||
190 | return "KiB"; | ||
191 | case MebiBytes: | ||
192 | return "MiB"; | ||
193 | case GibiBytes: | ||
194 | return "GiB"; | ||
195 | case TebiBytes: | ||
196 | return "TiB"; | ||
197 | case PebiBytes: | ||
198 | return "PiB"; | ||
199 | case ExbiBytes: | ||
200 | return "EiB"; | ||
201 | case KiloBytes: | ||
202 | return "KB"; | ||
203 | case MegaBytes: | ||
204 | return "MB"; | ||
205 | case GigaBytes: | ||
206 | return "GB"; | ||
207 | case TeraBytes: | ||
208 | return "TB"; | ||
209 | case PetaBytes: | ||
210 | return "PB"; | ||
211 | case ExaBytes: | ||
212 | return "EB"; | ||
213 | default: | ||
214 | assert(false); | ||
215 | } | ||
216 | } | ||
217 | |||
218 | measurement_unit measurement_unit_init() { | ||
219 | measurement_unit tmp = { | ||
220 | .name = NULL, | ||
221 | .filesystem_type = NULL, | ||
222 | .is_group = false, | ||
223 | |||
224 | .freeinodes_percent_thresholds = mp_thresholds_init(), | ||
225 | .freespace_percent_thresholds = mp_thresholds_init(), | ||
226 | .freespace_bytes_thresholds = mp_thresholds_init(), | ||
227 | |||
228 | .free_bytes = 0, | ||
229 | .used_bytes = 0, | ||
230 | .total_bytes = 0, | ||
231 | |||
232 | .inodes_total = 0, | ||
233 | .inodes_free = 0, | ||
234 | .inodes_free_to_root = 0, | ||
235 | .inodes_used = 0, | ||
236 | }; | ||
237 | return tmp; | ||
238 | } | ||
239 | |||
240 | // Add a given element to the list, memory for the new element is freshly allocated | ||
241 | // Returns a pointer to new element | ||
242 | measurement_unit_list *add_measurement_list(measurement_unit_list *list, measurement_unit elem) { | ||
243 | // find last element | ||
244 | measurement_unit_list *new = NULL; | ||
245 | if (list == NULL) { | ||
246 | new = calloc(1, sizeof(measurement_unit_list)); | ||
247 | if (new == NULL) { | ||
248 | die(STATE_UNKNOWN, _("allocation failed")); | ||
249 | } | ||
250 | } else { | ||
251 | measurement_unit_list *list_elem = list; | ||
252 | while (list_elem->next != NULL) { | ||
253 | list_elem = list_elem->next; | ||
254 | } | ||
255 | |||
256 | new = calloc(1, sizeof(measurement_unit_list)); | ||
257 | if (new == NULL) { | ||
258 | die(STATE_UNKNOWN, _("allocation failed")); | ||
259 | } | ||
260 | |||
261 | list_elem->next = new; | ||
262 | } | ||
263 | |||
264 | new->unit = elem; | ||
265 | new->next = NULL; | ||
266 | return new; | ||
267 | } | ||
268 | |||
269 | measurement_unit add_filesystem_to_measurement_unit(measurement_unit unit, | ||
270 | parameter_list_elem filesystem) { | ||
271 | |||
272 | unit.free_bytes += filesystem.free_bytes; | ||
273 | unit.used_bytes += filesystem.used_bytes; | ||
274 | unit.total_bytes += filesystem.total_bytes; | ||
275 | |||
276 | unit.inodes_total += filesystem.inodes_total; | ||
277 | unit.inodes_free += filesystem.inodes_free; | ||
278 | unit.inodes_free_to_root += filesystem.inodes_free_to_root; | ||
279 | unit.inodes_used += filesystem.inodes_used; | ||
280 | return unit; | ||
281 | } | ||
282 | |||
283 | measurement_unit create_measurement_unit_from_filesystem(parameter_list_elem filesystem, | ||
284 | bool display_mntp) { | ||
285 | measurement_unit result = measurement_unit_init(); | ||
286 | if (!display_mntp) { | ||
287 | result.name = strdup(filesystem.best_match->me_mountdir); | ||
288 | } else { | ||
289 | result.name = strdup(filesystem.best_match->me_devname); | ||
290 | } | ||
291 | |||
292 | if (filesystem.group) { | ||
293 | result.is_group = true; | ||
294 | } else { | ||
295 | result.is_group = false; | ||
296 | if (filesystem.best_match) { | ||
297 | result.filesystem_type = filesystem.best_match->me_type; | ||
298 | } | ||
299 | } | ||
300 | |||
301 | result.freeinodes_percent_thresholds = filesystem.freeinodes_percent; | ||
302 | result.freespace_percent_thresholds = filesystem.freespace_percent; | ||
303 | result.freespace_bytes_thresholds = filesystem.freespace_units; | ||
304 | result.free_bytes = filesystem.free_bytes; | ||
305 | result.total_bytes = filesystem.total_bytes; | ||
306 | result.used_bytes = filesystem.used_bytes; | ||
307 | result.inodes_total = filesystem.inodes_total; | ||
308 | result.inodes_used = filesystem.inodes_used; | ||
309 | result.inodes_free = filesystem.inodes_free; | ||
310 | result.inodes_free_to_root = filesystem.inodes_free_to_root; | ||
311 | return result; | ||
312 | } | ||
313 | |||
314 | #define RANDOM_STRING_LENGTH 64 | ||
315 | |||
316 | char *humanize_byte_value(unsigned long long value, bool use_si_units) { | ||
317 | // Idea: A reasonable output should have at most 3 orders of magnitude | ||
318 | // before the decimal separator | ||
319 | // 353GiB is ok, 2444 GiB should be 2.386 TiB | ||
320 | char *result = calloc(RANDOM_STRING_LENGTH, sizeof(char)); | ||
321 | if (result == NULL) { | ||
322 | die(STATE_UNKNOWN, _("allocation failed")); | ||
323 | } | ||
324 | const byte_unit KibiBytes_factor = 1024; | ||
325 | const byte_unit MebiBytes_factor = 1048576; | ||
326 | const byte_unit GibiBytes_factor = 1073741824; | ||
327 | const byte_unit TebiBytes_factor = 1099511627776; | ||
328 | const byte_unit PebiBytes_factor = 1125899906842624; | ||
329 | const byte_unit ExbiBytes_factor = 1152921504606846976; | ||
330 | const byte_unit KiloBytes_factor = 1000; | ||
331 | const byte_unit MegaBytes_factor = 1000000; | ||
332 | const byte_unit GigaBytes_factor = 1000000000; | ||
333 | const byte_unit TeraBytes_factor = 1000000000000; | ||
334 | const byte_unit PetaBytes_factor = 1000000000000000; | ||
335 | const byte_unit ExaBytes_factor = 1000000000000000000; | ||
336 | |||
337 | if (use_si_units) { | ||
338 | // SI units, powers of 10 | ||
339 | if (value < KiloBytes_factor) { | ||
340 | snprintf(result, RANDOM_STRING_LENGTH, "%llu B", value); | ||
341 | } else if (value < MegaBytes_factor) { | ||
342 | snprintf(result, RANDOM_STRING_LENGTH, "%llu KB", value / KiloBytes_factor); | ||
343 | } else if (value < GigaBytes_factor) { | ||
344 | snprintf(result, RANDOM_STRING_LENGTH, "%llu MB", value / MegaBytes_factor); | ||
345 | } else if (value < TeraBytes_factor) { | ||
346 | snprintf(result, RANDOM_STRING_LENGTH, "%llu GB", value / GigaBytes_factor); | ||
347 | } else if (value < PetaBytes_factor) { | ||
348 | snprintf(result, RANDOM_STRING_LENGTH, "%llu TB", value / TeraBytes_factor); | ||
349 | } else if (value < ExaBytes_factor) { | ||
350 | snprintf(result, RANDOM_STRING_LENGTH, "%llu PB", value / PetaBytes_factor); | ||
351 | } else { | ||
352 | snprintf(result, RANDOM_STRING_LENGTH, "%llu EB", value / ExaBytes_factor); | ||
353 | } | ||
354 | } else { | ||
355 | // IEC units, powers of 2 ^ 10 | ||
356 | if (value < KibiBytes_factor) { | ||
357 | snprintf(result, RANDOM_STRING_LENGTH, "%llu B", value); | ||
358 | } else if (value < MebiBytes_factor) { | ||
359 | snprintf(result, RANDOM_STRING_LENGTH, "%llu KiB", value / KibiBytes_factor); | ||
360 | } else if (value < GibiBytes_factor) { | ||
361 | snprintf(result, RANDOM_STRING_LENGTH, "%llu MiB", value / MebiBytes_factor); | ||
362 | } else if (value < TebiBytes_factor) { | ||
363 | snprintf(result, RANDOM_STRING_LENGTH, "%llu GiB", value / GibiBytes_factor); | ||
364 | } else if (value < PebiBytes_factor) { | ||
365 | snprintf(result, RANDOM_STRING_LENGTH, "%llu TiB", value / TebiBytes_factor); | ||
366 | } else if (value < ExbiBytes_factor) { | ||
367 | snprintf(result, RANDOM_STRING_LENGTH, "%llu PiB", value / PebiBytes_factor); | ||
368 | } else { | ||
369 | snprintf(result, RANDOM_STRING_LENGTH, "%llu EiB", value / ExbiBytes_factor); | ||
370 | } | ||
371 | } | ||
372 | |||
373 | return result; | ||
374 | } | ||
375 | |||
376 | filesystem_list filesystem_list_init() { | ||
377 | filesystem_list tmp = { | ||
378 | .length = 0, | ||
379 | .first = NULL, | ||
380 | }; | ||
381 | return tmp; | ||
382 | } | ||
383 | |||
384 | parameter_list_elem *mp_int_fs_list_append(filesystem_list *list, const char *name) { | ||
385 | parameter_list_elem *current = list->first; | ||
386 | parameter_list_elem *new_path = (struct parameter_list *)malloc(sizeof *new_path); | ||
387 | *new_path = parameter_list_init(name); | ||
388 | |||
389 | if (current == NULL) { | ||
390 | list->first = new_path; | ||
391 | new_path->prev = NULL; | ||
392 | list->length = 1; | ||
393 | } else { | ||
394 | while (current->next) { | ||
395 | current = current->next; | ||
396 | } | ||
397 | current->next = new_path; | ||
398 | new_path->prev = current; | ||
399 | list->length++; | ||
400 | } | ||
401 | return new_path; | ||
402 | } | ||
403 | |||
404 | parameter_list_elem *mp_int_fs_list_find(filesystem_list list, const char *name) { | ||
405 | if (list.length == 0) { | ||
406 | return NULL; | ||
407 | } | ||
408 | |||
409 | for (parameter_list_elem *temp_list = list.first; temp_list; temp_list = temp_list->next) { | ||
410 | if (!strcmp(temp_list->name, name)) { | ||
411 | return temp_list; | ||
412 | } | ||
413 | } | ||
414 | |||
415 | return NULL; | ||
416 | } | ||
417 | |||
418 | parameter_list_elem *mp_int_fs_list_del(filesystem_list *list, parameter_list_elem *item) { | ||
419 | if (list->length == 0) { | ||
420 | return NULL; | ||
421 | } | ||
422 | |||
423 | if (item == NULL) { | ||
424 | // Got NULL for item, interpret this as "delete first element" | ||
425 | // as a kind of compatibility to the old function | ||
426 | item = list->first; | ||
427 | } | ||
428 | |||
429 | if (list->first == item) { | ||
430 | list->length--; | ||
431 | |||
432 | list->first = item->next; | ||
433 | if (list->first) { | ||
434 | list->first->prev = NULL; | ||
435 | } | ||
436 | return list->first; | ||
437 | } | ||
438 | |||
439 | // Was not the first element, continue | ||
440 | parameter_list_elem *prev = list->first; | ||
441 | parameter_list_elem *current = list->first->next; | ||
442 | |||
443 | while (current != item && current != NULL) { | ||
444 | prev = current; | ||
445 | current = current->next; | ||
446 | } | ||
447 | |||
448 | if (current == NULL) { | ||
449 | // didn't find that element .... | ||
450 | return NULL; | ||
451 | } | ||
452 | |||
453 | // remove the element | ||
454 | parameter_list_elem *next = current->next; | ||
455 | prev->next = next; | ||
456 | list->length--; | ||
457 | if (next) { | ||
458 | next->prev = prev; | ||
459 | } | ||
460 | |||
461 | if (item->name) { | ||
462 | free(item->name); | ||
463 | } | ||
464 | free(item); | ||
465 | |||
466 | return next; | ||
467 | } | ||
468 | |||
469 | parameter_list_elem *mp_int_fs_list_get_next(parameter_list_elem *current) { | ||
470 | if (!current) { | ||
471 | return NULL; | ||
472 | } | ||
473 | return current->next; | ||
474 | } | ||
475 | |||
476 | void mp_int_fs_list_set_best_match(filesystem_list list, struct mount_entry *mount_list, | ||
477 | bool exact) { | ||
478 | for (parameter_list_elem *elem = list.first; elem; elem = mp_int_fs_list_get_next(elem)) { | ||
479 | if (!elem->best_match) { | ||
480 | size_t name_len = strlen(elem->name); | ||
481 | struct mount_entry *best_match = NULL; | ||
482 | |||
483 | /* set best match if path name exactly matches a mounted device name */ | ||
484 | for (struct mount_entry *mount_entry = mount_list; mount_entry; | ||
485 | mount_entry = mount_entry->me_next) { | ||
486 | if (strcmp(mount_entry->me_devname, elem->name) == 0) { | ||
487 | struct fs_usage fsp; | ||
488 | if (get_fs_usage(mount_entry->me_mountdir, mount_entry->me_devname, &fsp) >= | ||
489 | 0) { | ||
490 | best_match = mount_entry; | ||
491 | } | ||
492 | } | ||
493 | } | ||
494 | |||
495 | /* set best match by directory name if no match was found by devname */ | ||
496 | if (!best_match) { | ||
497 | size_t best_match_len = 0; | ||
498 | for (struct mount_entry *mount_entry = mount_list; mount_entry; | ||
499 | mount_entry = mount_entry->me_next) { | ||
500 | size_t len = strlen(mount_entry->me_mountdir); | ||
501 | |||
502 | if ((!exact && | ||
503 | (best_match_len <= len && len <= name_len && | ||
504 | (len == 1 || strncmp(mount_entry->me_mountdir, elem->name, len) == 0))) || | ||
505 | (exact && strcmp(mount_entry->me_mountdir, elem->name) == 0)) { | ||
506 | struct fs_usage fsp; | ||
507 | |||
508 | if (get_fs_usage(mount_entry->me_mountdir, mount_entry->me_devname, &fsp) >= | ||
509 | 0) { | ||
510 | best_match = mount_entry; | ||
511 | best_match_len = len; | ||
512 | } | ||
513 | } | ||
514 | } | ||
515 | } | ||
516 | |||
517 | if (best_match) { | ||
518 | elem->best_match = best_match; | ||
519 | } else { | ||
520 | elem->best_match = | ||
521 | NULL; /* Not sure why this is needed as it should be null on initialisation */ | ||
522 | } | ||
523 | |||
524 | // No filesystem without a mount_entry! | ||
525 | // assert(elem->best_match != NULL); | ||
526 | } | ||
527 | } | ||
528 | } | ||
diff --git a/plugins/check_disk.d/utils_disk.h b/plugins/check_disk.d/utils_disk.h new file mode 100644 index 00000000..c96d4296 --- /dev/null +++ b/plugins/check_disk.d/utils_disk.h | |||
@@ -0,0 +1,160 @@ | |||
1 | #pragma once | ||
2 | /* Header file for utils_disk */ | ||
3 | |||
4 | #include "../../config.h" | ||
5 | #include "../../gl/mountlist.h" | ||
6 | #include "../../lib/utils_base.h" | ||
7 | #include "../../lib/output.h" | ||
8 | #include "regex.h" | ||
9 | #include <stdint.h> | ||
10 | |||
11 | typedef unsigned long long byte_unit; | ||
12 | |||
13 | typedef enum { | ||
14 | Humanized, | ||
15 | Bytes, | ||
16 | KibiBytes, | ||
17 | MebiBytes, | ||
18 | GibiBytes, | ||
19 | TebiBytes, | ||
20 | PebiBytes, | ||
21 | ExbiBytes, | ||
22 | KiloBytes, | ||
23 | MegaBytes, | ||
24 | GigaBytes, | ||
25 | TeraBytes, | ||
26 | PetaBytes, | ||
27 | ExaBytes, | ||
28 | } byte_unit_enum; | ||
29 | |||
30 | typedef struct name_list string_list; | ||
31 | struct name_list { | ||
32 | char *name; | ||
33 | string_list *next; | ||
34 | }; | ||
35 | |||
36 | struct regex_list { | ||
37 | regex_t regex; | ||
38 | struct regex_list *next; | ||
39 | }; | ||
40 | |||
41 | typedef struct parameter_list parameter_list_elem; | ||
42 | struct parameter_list { | ||
43 | char *name; | ||
44 | char *group; | ||
45 | |||
46 | mp_thresholds freespace_units; | ||
47 | mp_thresholds freespace_percent; | ||
48 | mp_thresholds freeinodes_percent; | ||
49 | |||
50 | struct mount_entry *best_match; | ||
51 | |||
52 | uintmax_t inodes_free_to_root; | ||
53 | uintmax_t inodes_free; | ||
54 | uintmax_t inodes_used; | ||
55 | uintmax_t inodes_total; | ||
56 | |||
57 | uint64_t used_bytes; | ||
58 | uint64_t free_bytes; | ||
59 | uint64_t total_bytes; | ||
60 | |||
61 | parameter_list_elem *next; | ||
62 | parameter_list_elem *prev; | ||
63 | }; | ||
64 | |||
65 | typedef struct { | ||
66 | size_t length; | ||
67 | parameter_list_elem *first; | ||
68 | } filesystem_list; | ||
69 | |||
70 | filesystem_list filesystem_list_init(); | ||
71 | |||
72 | typedef struct { | ||
73 | char *name; | ||
74 | char *filesystem_type; | ||
75 | bool is_group; | ||
76 | |||
77 | mp_thresholds freespace_bytes_thresholds; | ||
78 | mp_thresholds freespace_percent_thresholds; | ||
79 | mp_thresholds freeinodes_percent_thresholds; | ||
80 | |||
81 | uintmax_t inodes_free_to_root; | ||
82 | uintmax_t inodes_free; | ||
83 | uintmax_t inodes_used; | ||
84 | uintmax_t inodes_total; | ||
85 | |||
86 | uintmax_t used_bytes; | ||
87 | uintmax_t free_bytes; | ||
88 | uintmax_t total_bytes; | ||
89 | } measurement_unit; | ||
90 | |||
91 | typedef struct measurement_unit_list measurement_unit_list; | ||
92 | struct measurement_unit_list { | ||
93 | measurement_unit unit; | ||
94 | measurement_unit_list *next; | ||
95 | }; | ||
96 | |||
97 | typedef struct { | ||
98 | // Output options | ||
99 | bool erronly; | ||
100 | bool display_mntp; | ||
101 | /* show only local filesystems. */ | ||
102 | bool show_local_fs; | ||
103 | /* show only local filesystems but call stat() on remote ones. */ | ||
104 | bool stat_remote_fs; | ||
105 | bool display_inodes_perfdata; | ||
106 | |||
107 | bool exact_match; | ||
108 | bool freespace_ignore_reserved; | ||
109 | |||
110 | bool ignore_missing; | ||
111 | bool path_ignored; | ||
112 | |||
113 | /* Linked list of filesystem types to omit. | ||
114 | If the list is empty, don't exclude any types. */ | ||
115 | struct regex_list *fs_exclude_list; | ||
116 | /* Linked list of filesystem types to check. | ||
117 | If the list is empty, include all types. */ | ||
118 | struct regex_list *fs_include_list; | ||
119 | struct name_list *device_path_exclude_list; | ||
120 | filesystem_list path_select_list; | ||
121 | /* Linked list of mounted filesystems. */ | ||
122 | struct mount_entry *mount_list; | ||
123 | struct name_list *seen; | ||
124 | |||
125 | byte_unit_enum display_unit; | ||
126 | // byte_unit unit; | ||
127 | |||
128 | bool output_format_is_set; | ||
129 | mp_output_format output_format; | ||
130 | } check_disk_config; | ||
131 | |||
132 | void np_add_name(struct name_list **list, const char *name); | ||
133 | bool np_find_name(struct name_list *list, const char *name); | ||
134 | bool np_seen_name(struct name_list *list, const char *name); | ||
135 | int np_add_regex(struct regex_list **list, const char *regex, int cflags); | ||
136 | bool np_find_regmatch(struct regex_list *list, const char *name); | ||
137 | |||
138 | parameter_list_elem parameter_list_init(const char *); | ||
139 | |||
140 | parameter_list_elem *mp_int_fs_list_append(filesystem_list *list, const char *name); | ||
141 | parameter_list_elem *mp_int_fs_list_find(filesystem_list list, const char *name); | ||
142 | parameter_list_elem *mp_int_fs_list_del(filesystem_list *list, parameter_list_elem *item); | ||
143 | parameter_list_elem *mp_int_fs_list_get_next(parameter_list_elem *current); | ||
144 | void mp_int_fs_list_set_best_match(filesystem_list list, struct mount_entry *mount_list, | ||
145 | bool exact); | ||
146 | |||
147 | measurement_unit measurement_unit_init(); | ||
148 | measurement_unit_list *add_measurement_list(measurement_unit_list *list, measurement_unit elem); | ||
149 | measurement_unit add_filesystem_to_measurement_unit(measurement_unit unit, | ||
150 | parameter_list_elem filesystem); | ||
151 | measurement_unit create_measurement_unit_from_filesystem(parameter_list_elem filesystem, | ||
152 | bool display_mntp); | ||
153 | |||
154 | int search_parameter_list(parameter_list_elem *list, const char *name); | ||
155 | bool np_regex_match_mount_entry(struct mount_entry *, regex_t *); | ||
156 | |||
157 | char *get_unit_string(byte_unit_enum); | ||
158 | check_disk_config check_disk_config_init(); | ||
159 | |||
160 | char *humanize_byte_value(unsigned long long value, bool use_si_units); | ||
diff --git a/plugins/check_dns.c b/plugins/check_dns.c index 95f33083..56f91dae 100644 --- a/plugins/check_dns.c +++ b/plugins/check_dns.c | |||
@@ -48,7 +48,8 @@ typedef struct { | |||
48 | } check_dns_config_wrapper; | 48 | } check_dns_config_wrapper; |
49 | static check_dns_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | 49 | static check_dns_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
50 | static check_dns_config_wrapper validate_arguments(check_dns_config_wrapper /*config_wrapper*/); | 50 | static check_dns_config_wrapper validate_arguments(check_dns_config_wrapper /*config_wrapper*/); |
51 | static mp_state_enum error_scan(char * /*input_buffer*/, bool * /*is_nxdomain*/, const char /*dns_server*/[ADDRESS_LENGTH]); | 51 | static mp_state_enum error_scan(char * /*input_buffer*/, bool * /*is_nxdomain*/, |
52 | const char /*dns_server*/[ADDRESS_LENGTH]); | ||
52 | static bool ip_match_cidr(const char * /*addr*/, const char * /*cidr_ro*/); | 53 | static bool ip_match_cidr(const char * /*addr*/, const char * /*cidr_ro*/); |
53 | static unsigned long ip2long(const char * /*src*/); | 54 | static unsigned long ip2long(const char * /*src*/); |
54 | static void print_help(void); | 55 | static void print_help(void); |
@@ -127,7 +128,8 @@ int main(int argc, char **argv) { | |||
127 | puts(chld_out.line[i]); | 128 | puts(chld_out.line[i]); |
128 | } | 129 | } |
129 | 130 | ||
130 | if (strcasestr(chld_out.line[i], ".in-addr.arpa") || strcasestr(chld_out.line[i], ".ip6.arpa")) { | 131 | if (strcasestr(chld_out.line[i], ".in-addr.arpa") || |
132 | strcasestr(chld_out.line[i], ".ip6.arpa")) { | ||
131 | if ((strstr(chld_out.line[i], "canonical name = ") != NULL)) { | 133 | if ((strstr(chld_out.line[i], "canonical name = ") != NULL)) { |
132 | continue; | 134 | continue; |
133 | } | 135 | } |
@@ -145,7 +147,8 @@ int main(int argc, char **argv) { | |||
145 | if (strstr(chld_out.line[i], "Server:") && strlen(config.dns_server) > 0) { | 147 | if (strstr(chld_out.line[i], "Server:") && strlen(config.dns_server) > 0) { |
146 | char *temp_buffer = strchr(chld_out.line[i], ':'); | 148 | char *temp_buffer = strchr(chld_out.line[i], ':'); |
147 | if (temp_buffer == NULL) { | 149 | if (temp_buffer == NULL) { |
148 | die(STATE_UNKNOWN, _("'%s' returned a weirdly formatted Server line\n"), NSLOOKUP_COMMAND); | 150 | die(STATE_UNKNOWN, _("'%s' returned a weirdly formatted Server line\n"), |
151 | NSLOOKUP_COMMAND); | ||
149 | } | 152 | } |
150 | 153 | ||
151 | temp_buffer++; | 154 | temp_buffer++; |
@@ -157,21 +160,25 @@ int main(int argc, char **argv) { | |||
157 | 160 | ||
158 | strip(temp_buffer); | 161 | strip(temp_buffer); |
159 | if (strlen(temp_buffer) == 0) { | 162 | if (strlen(temp_buffer) == 0) { |
160 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' returned empty server string\n"), NSLOOKUP_COMMAND); | 163 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' returned empty server string\n"), |
164 | NSLOOKUP_COMMAND); | ||
161 | } | 165 | } |
162 | 166 | ||
163 | if (strcmp(temp_buffer, config.dns_server) != 0) { | 167 | if (strcmp(temp_buffer, config.dns_server) != 0) { |
164 | die(STATE_CRITICAL, _("DNS CRITICAL - No response from DNS %s\n"), config.dns_server); | 168 | die(STATE_CRITICAL, _("DNS CRITICAL - No response from DNS %s\n"), |
169 | config.dns_server); | ||
165 | } | 170 | } |
166 | } | 171 | } |
167 | 172 | ||
168 | /* the server is responding, we just got the host name... */ | 173 | /* the server is responding, we just got the host name... */ |
169 | if (strstr(chld_out.line[i], "Name:")) { | 174 | if (strstr(chld_out.line[i], "Name:")) { |
170 | parse_address = true; | 175 | parse_address = true; |
171 | } else if (parse_address && (strstr(chld_out.line[i], "Address:") || strstr(chld_out.line[i], "Addresses:"))) { | 176 | } else if (parse_address && (strstr(chld_out.line[i], "Address:") || |
177 | strstr(chld_out.line[i], "Addresses:"))) { | ||
172 | char *temp_buffer = strchr(chld_out.line[i], ':'); | 178 | char *temp_buffer = strchr(chld_out.line[i], ':'); |
173 | if (temp_buffer == NULL) { | 179 | if (temp_buffer == NULL) { |
174 | die(STATE_UNKNOWN, _("'%s' returned a weirdly formatted Address line\n"), NSLOOKUP_COMMAND); | 180 | die(STATE_UNKNOWN, _("'%s' returned a weirdly formatted Address line\n"), |
181 | NSLOOKUP_COMMAND); | ||
175 | } | 182 | } |
176 | 183 | ||
177 | temp_buffer++; | 184 | temp_buffer++; |
@@ -183,7 +190,8 @@ int main(int argc, char **argv) { | |||
183 | 190 | ||
184 | strip(temp_buffer); | 191 | strip(temp_buffer); |
185 | if (strlen(temp_buffer) == 0) { | 192 | if (strlen(temp_buffer) == 0) { |
186 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' returned empty host name string\n"), NSLOOKUP_COMMAND); | 193 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' returned empty host name string\n"), |
194 | NSLOOKUP_COMMAND); | ||
187 | } | 195 | } |
188 | 196 | ||
189 | addresses[n_addresses++] = strdup(temp_buffer); | 197 | addresses[n_addresses++] = strdup(temp_buffer); |
@@ -209,7 +217,8 @@ int main(int argc, char **argv) { | |||
209 | } | 217 | } |
210 | 218 | ||
211 | if (error_scan(chld_err.line[i], &is_nxdomain, config.dns_server) != STATE_OK) { | 219 | if (error_scan(chld_err.line[i], &is_nxdomain, config.dns_server) != STATE_OK) { |
212 | result = max_state(result, error_scan(chld_err.line[i], &is_nxdomain, config.dns_server)); | 220 | result = |
221 | max_state(result, error_scan(chld_err.line[i], &is_nxdomain, config.dns_server)); | ||
213 | msg = strchr(input_buffer, ':'); | 222 | msg = strchr(input_buffer, ':'); |
214 | if (msg) { | 223 | if (msg) { |
215 | msg++; | 224 | msg++; |
@@ -242,7 +251,8 @@ int main(int argc, char **argv) { | |||
242 | } | 251 | } |
243 | *adrp = 0; | 252 | *adrp = 0; |
244 | } else { | 253 | } else { |
245 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' msg parsing exited with no address\n"), NSLOOKUP_COMMAND); | 254 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' msg parsing exited with no address\n"), |
255 | NSLOOKUP_COMMAND); | ||
246 | } | 256 | } |
247 | 257 | ||
248 | /* compare to expected address */ | 258 | /* compare to expected address */ |
@@ -255,7 +265,8 @@ int main(int argc, char **argv) { | |||
255 | for (size_t i = 0; i < config.expected_address_cnt; i++) { | 265 | for (size_t i = 0; i < config.expected_address_cnt; i++) { |
256 | /* check if we get a match on 'raw' ip or cidr */ | 266 | /* check if we get a match on 'raw' ip or cidr */ |
257 | for (size_t j = 0; j < n_addresses; j++) { | 267 | for (size_t j = 0; j < n_addresses; j++) { |
258 | if (strcmp(addresses[j], config.expected_address[i]) == 0 || ip_match_cidr(addresses[j], config.expected_address[i])) { | 268 | if (strcmp(addresses[j], config.expected_address[i]) == 0 || |
269 | ip_match_cidr(addresses[j], config.expected_address[i])) { | ||
259 | result = STATE_OK; | 270 | result = STATE_OK; |
260 | addr_match &= ~(1 << j); | 271 | addr_match &= ~(1 << j); |
261 | expect_match &= ~(1 << i); | 272 | expect_match &= ~(1 << i); |
@@ -279,7 +290,8 @@ int main(int argc, char **argv) { | |||
279 | if (config.expect_nxdomain) { | 290 | if (config.expect_nxdomain) { |
280 | if (!is_nxdomain) { | 291 | if (!is_nxdomain) { |
281 | result = STATE_CRITICAL; | 292 | result = STATE_CRITICAL; |
282 | xasprintf(&msg, _("Domain '%s' was found by the server: '%s'\n"), config.query_address, address); | 293 | xasprintf(&msg, _("Domain '%s' was found by the server: '%s'\n"), config.query_address, |
294 | address); | ||
283 | } else { | 295 | } else { |
284 | if (address != NULL) { | 296 | if (address != NULL) { |
285 | free(address); | 297 | free(address); |
@@ -291,7 +303,8 @@ int main(int argc, char **argv) { | |||
291 | /* check if authoritative */ | 303 | /* check if authoritative */ |
292 | if (result == STATE_OK && config.expect_authority && non_authoritative) { | 304 | if (result == STATE_OK && config.expect_authority && non_authoritative) { |
293 | result = STATE_CRITICAL; | 305 | result = STATE_CRITICAL; |
294 | xasprintf(&msg, _("server %s is not authoritative for %s"), config.dns_server, config.query_address); | 306 | xasprintf(&msg, _("server %s is not authoritative for %s"), config.dns_server, |
307 | config.query_address); | ||
295 | } | 308 | } |
296 | 309 | ||
297 | long microsec = deltime(tv); | 310 | long microsec = deltime(tv); |
@@ -306,24 +319,36 @@ int main(int argc, char **argv) { | |||
306 | } else if (result == STATE_CRITICAL) { | 319 | } else if (result == STATE_CRITICAL) { |
307 | printf("DNS %s: ", _("CRITICAL")); | 320 | printf("DNS %s: ", _("CRITICAL")); |
308 | } | 321 | } |
309 | printf(ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time); | 322 | printf(ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), |
323 | elapsed_time); | ||
310 | printf(_(". %s returns %s"), config.query_address, address); | 324 | printf(_(". %s returns %s"), config.query_address, address); |
311 | if ((config.time_thresholds->warning != NULL) && (config.time_thresholds->critical != NULL)) { | 325 | if ((config.time_thresholds->warning != NULL) && |
312 | printf("|%s\n", fperfdata("time", elapsed_time, "s", true, config.time_thresholds->warning->end, true, | 326 | (config.time_thresholds->critical != NULL)) { |
327 | printf("|%s\n", | ||
328 | fperfdata("time", elapsed_time, "s", true, config.time_thresholds->warning->end, | ||
329 | true, config.time_thresholds->critical->end, true, 0, false, 0)); | ||
330 | } else if ((config.time_thresholds->warning == NULL) && | ||
331 | (config.time_thresholds->critical != NULL)) { | ||
332 | printf("|%s\n", fperfdata("time", elapsed_time, "s", false, 0, true, | ||
313 | config.time_thresholds->critical->end, true, 0, false, 0)); | 333 | config.time_thresholds->critical->end, true, 0, false, 0)); |
314 | } else if ((config.time_thresholds->warning == NULL) && (config.time_thresholds->critical != NULL)) { | 334 | } else if ((config.time_thresholds->warning != NULL) && |
315 | printf("|%s\n", fperfdata("time", elapsed_time, "s", false, 0, true, config.time_thresholds->critical->end, true, 0, false, 0)); | 335 | (config.time_thresholds->critical == NULL)) { |
316 | } else if ((config.time_thresholds->warning != NULL) && (config.time_thresholds->critical == NULL)) { | 336 | printf("|%s\n", |
317 | printf("|%s\n", fperfdata("time", elapsed_time, "s", true, config.time_thresholds->warning->end, false, 0, true, 0, false, 0)); | 337 | fperfdata("time", elapsed_time, "s", true, config.time_thresholds->warning->end, |
338 | false, 0, true, 0, false, 0)); | ||
318 | } else { | 339 | } else { |
319 | printf("|%s\n", fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, false, 0)); | 340 | printf("|%s\n", |
341 | fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, false, 0)); | ||
320 | } | 342 | } |
321 | } else if (result == STATE_WARNING) { | 343 | } else if (result == STATE_WARNING) { |
322 | printf(_("DNS WARNING - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | 344 | printf(_("DNS WARNING - %s\n"), |
345 | !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | ||
323 | } else if (result == STATE_CRITICAL) { | 346 | } else if (result == STATE_CRITICAL) { |
324 | printf(_("DNS CRITICAL - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | 347 | printf(_("DNS CRITICAL - %s\n"), |
348 | !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | ||
325 | } else { | 349 | } else { |
326 | printf(_("DNS UNKNOWN - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | 350 | printf(_("DNS UNKNOWN - %s\n"), |
351 | !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | ||
327 | } | 352 | } |
328 | 353 | ||
329 | exit(result); | 354 | exit(result); |
@@ -342,29 +367,34 @@ bool ip_match_cidr(const char *addr, const char *cidr_ro) { | |||
342 | mask = atoi(mask_c); | 367 | mask = atoi(mask_c); |
343 | 368 | ||
344 | /* https://www.cryptobells.com/verifying-ips-in-a-subnet-in-php/ */ | 369 | /* https://www.cryptobells.com/verifying-ips-in-a-subnet-in-php/ */ |
345 | return (ip2long(addr) & ~((1 << (32 - mask)) - 1)) == (ip2long(subnet) >> (32 - mask)) << (32 - mask); | 370 | return (ip2long(addr) & ~((1 << (32 - mask)) - 1)) == (ip2long(subnet) >> (32 - mask)) |
371 | << (32 - mask); | ||
346 | } | 372 | } |
347 | 373 | ||
348 | unsigned long ip2long(const char *src) { | 374 | unsigned long ip2long(const char *src) { |
349 | unsigned long ip[4]; | 375 | unsigned long ip[4]; |
350 | /* http://computer-programming-forum.com/47-c-language/1376ffb92a12c471.htm */ | 376 | /* http://computer-programming-forum.com/47-c-language/1376ffb92a12c471.htm */ |
351 | return (sscanf(src, "%3lu.%3lu.%3lu.%3lu", &ip[0], &ip[1], &ip[2], &ip[3]) == 4 && ip[0] < 256 && ip[1] < 256 && ip[2] < 256 && | 377 | return (sscanf(src, "%3lu.%3lu.%3lu.%3lu", &ip[0], &ip[1], &ip[2], &ip[3]) == 4 && |
352 | ip[3] < 256) | 378 | ip[0] < 256 && ip[1] < 256 && ip[2] < 256 && ip[3] < 256) |
353 | ? ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3] | 379 | ? ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3] |
354 | : 0; | 380 | : 0; |
355 | } | 381 | } |
356 | 382 | ||
357 | mp_state_enum error_scan(char *input_buffer, bool *is_nxdomain, const char dns_server[ADDRESS_LENGTH]) { | 383 | mp_state_enum error_scan(char *input_buffer, bool *is_nxdomain, |
384 | const char dns_server[ADDRESS_LENGTH]) { | ||
358 | 385 | ||
359 | const int nxdomain = strstr(input_buffer, "Non-existent") || strstr(input_buffer, "** server can't find") || | 386 | const int nxdomain = strstr(input_buffer, "Non-existent") || |
387 | strstr(input_buffer, "** server can't find") || | ||
360 | strstr(input_buffer, "** Can't find") || strstr(input_buffer, "NXDOMAIN"); | 388 | strstr(input_buffer, "** Can't find") || strstr(input_buffer, "NXDOMAIN"); |
361 | if (nxdomain) { | 389 | if (nxdomain) { |
362 | *is_nxdomain = true; | 390 | *is_nxdomain = true; |
363 | } | 391 | } |
364 | 392 | ||
365 | /* the DNS lookup timed out */ | 393 | /* the DNS lookup timed out */ |
366 | if (strstr(input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) || | 394 | if (strstr(input_buffer, |
367 | strstr(input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) || | 395 | _("Note: nslookup is deprecated and may be removed from future releases.")) || |
396 | strstr(input_buffer, | ||
397 | _("Consider using the `dig' or `host' programs instead. Run nslookup with")) || | ||
368 | strstr(input_buffer, _("the `-sil[ent]' option to prevent this message from appearing."))) { | 398 | strstr(input_buffer, _("the `-sil[ent]' option to prevent this message from appearing."))) { |
369 | return STATE_OK; | 399 | return STATE_OK; |
370 | } | 400 | } |
@@ -382,8 +412,9 @@ mp_state_enum error_scan(char *input_buffer, bool *is_nxdomain, const char dns_s | |||
382 | } | 412 | } |
383 | 413 | ||
384 | /* Connection was refused */ | 414 | /* Connection was refused */ |
385 | else if (strstr(input_buffer, "Connection refused") || strstr(input_buffer, "Couldn't find server") || | 415 | else if (strstr(input_buffer, "Connection refused") || |
386 | strstr(input_buffer, "Refused") || (strstr(input_buffer, "** server can't find") && strstr(input_buffer, ": REFUSED"))) { | 416 | strstr(input_buffer, "Couldn't find server") || strstr(input_buffer, "Refused") || |
417 | (strstr(input_buffer, "** server can't find") && strstr(input_buffer, ": REFUSED"))) { | ||
387 | die(STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server); | 418 | die(STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server); |
388 | } | 419 | } |
389 | 420 | ||
@@ -504,20 +535,24 @@ check_dns_config_wrapper process_arguments(int argc, char **argv) { | |||
504 | if (strchr(optarg, ',') != NULL) { | 535 | if (strchr(optarg, ',') != NULL) { |
505 | char *comma = strchr(optarg, ','); | 536 | char *comma = strchr(optarg, ','); |
506 | while (comma != NULL) { | 537 | while (comma != NULL) { |
507 | result.config.expected_address = | 538 | result.config.expected_address = (char **)realloc( |
508 | (char **)realloc(result.config.expected_address, (result.config.expected_address_cnt + 1) * sizeof(char **)); | 539 | result.config.expected_address, |
509 | result.config.expected_address[result.config.expected_address_cnt] = strndup(optarg, comma - optarg); | 540 | (result.config.expected_address_cnt + 1) * sizeof(char **)); |
541 | result.config.expected_address[result.config.expected_address_cnt] = | ||
542 | strndup(optarg, comma - optarg); | ||
510 | result.config.expected_address_cnt++; | 543 | result.config.expected_address_cnt++; |
511 | optarg = comma + 1; | 544 | optarg = comma + 1; |
512 | comma = strchr(optarg, ','); | 545 | comma = strchr(optarg, ','); |
513 | } | 546 | } |
514 | result.config.expected_address = | 547 | result.config.expected_address = |
515 | (char **)realloc(result.config.expected_address, (result.config.expected_address_cnt + 1) * sizeof(char **)); | 548 | (char **)realloc(result.config.expected_address, |
549 | (result.config.expected_address_cnt + 1) * sizeof(char **)); | ||
516 | result.config.expected_address[result.config.expected_address_cnt] = strdup(optarg); | 550 | result.config.expected_address[result.config.expected_address_cnt] = strdup(optarg); |
517 | result.config.expected_address_cnt++; | 551 | result.config.expected_address_cnt++; |
518 | } else { | 552 | } else { |
519 | result.config.expected_address = | 553 | result.config.expected_address = |
520 | (char **)realloc(result.config.expected_address, (result.config.expected_address_cnt + 1) * sizeof(char **)); | 554 | (char **)realloc(result.config.expected_address, |
555 | (result.config.expected_address_cnt + 1) * sizeof(char **)); | ||
521 | result.config.expected_address[result.config.expected_address_cnt] = strdup(optarg); | 556 | result.config.expected_address[result.config.expected_address_cnt] = strdup(optarg); |
522 | result.config.expected_address_cnt++; | 557 | result.config.expected_address_cnt++; |
523 | } | 558 | } |
@@ -586,9 +621,11 @@ void print_help(void) { | |||
586 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | 621 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
587 | printf(COPYRIGHT, copyright, email); | 622 | printf(COPYRIGHT, copyright, email); |
588 | 623 | ||
589 | printf("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query.")); | 624 | printf("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given " |
625 | "host/domain query.")); | ||
590 | printf("%s\n", _("An optional DNS server to use may be specified.")); | 626 | printf("%s\n", _("An optional DNS server to use may be specified.")); |
591 | printf("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used.")); | 627 | printf("%s\n", _("If no DNS server is specified, the default server(s) specified in " |
628 | "/etc/resolv.conf will be used.")); | ||
592 | 629 | ||
593 | printf("\n\n"); | 630 | printf("\n\n"); |
594 | 631 | ||
@@ -602,11 +639,14 @@ void print_help(void) { | |||
602 | printf(" -s, --server=HOST\n"); | 639 | printf(" -s, --server=HOST\n"); |
603 | printf(" %s\n", _("Optional DNS server you want to use for the lookup")); | 640 | printf(" %s\n", _("Optional DNS server you want to use for the lookup")); |
604 | printf(" -a, --expected-address=IP-ADDRESS|CIDR|HOST\n"); | 641 | printf(" -a, --expected-address=IP-ADDRESS|CIDR|HOST\n"); |
605 | printf(" %s\n", _("Optional IP-ADDRESS/CIDR you expect the DNS server to return. HOST must end")); | 642 | printf(" %s\n", |
606 | printf(" %s\n", _("with a dot (.). This option can be repeated multiple times (Returns OK if any")); | 643 | _("Optional IP-ADDRESS/CIDR you expect the DNS server to return. HOST must end")); |
644 | printf(" %s\n", | ||
645 | _("with a dot (.). This option can be repeated multiple times (Returns OK if any")); | ||
607 | printf(" %s\n", _("value matches).")); | 646 | printf(" %s\n", _("value matches).")); |
608 | printf(" -n, --expect-nxdomain\n"); | 647 | printf(" -n, --expect-nxdomain\n"); |
609 | printf(" %s\n", _("Expect the DNS server to return NXDOMAIN (i.e. the domain was not found)")); | 648 | printf(" %s\n", |
649 | _("Expect the DNS server to return NXDOMAIN (i.e. the domain was not found)")); | ||
610 | printf(" %s\n", _("Cannot be used together with -a")); | 650 | printf(" %s\n", _("Cannot be used together with -a")); |
611 | printf(" -A, --expect-authority\n"); | 651 | printf(" -A, --expect-authority\n"); |
612 | printf(" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup")); | 652 | printf(" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup")); |
@@ -615,7 +655,8 @@ void print_help(void) { | |||
615 | printf(" -c, --critical=seconds\n"); | 655 | printf(" -c, --critical=seconds\n"); |
616 | printf(" %s\n", _("Return critical if elapsed time exceeds value. Default off")); | 656 | printf(" %s\n", _("Return critical if elapsed time exceeds value. Default off")); |
617 | printf(" -L, --all\n"); | 657 | printf(" -L, --all\n"); |
618 | printf(" %s\n", _("Return critical if the list of expected addresses does not match all addresses")); | 658 | printf(" %s\n", |
659 | _("Return critical if the list of expected addresses does not match all addresses")); | ||
619 | printf(" %s\n", _("returned. Default off")); | 660 | printf(" %s\n", _("returned. Default off")); |
620 | 661 | ||
621 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 662 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
@@ -625,5 +666,7 @@ void print_help(void) { | |||
625 | 666 | ||
626 | void print_usage(void) { | 667 | void print_usage(void) { |
627 | printf("%s\n", _("Usage:")); | 668 | printf("%s\n", _("Usage:")); |
628 | printf("%s -H host [-s server] [-a expected-address] [-n] [-A] [-t timeout] [-w warn] [-c crit] [-L]\n", progname); | 669 | printf("%s -H host [-s server] [-a expected-address] [-n] [-A] [-t timeout] [-w warn] [-c " |
670 | "crit] [-L]\n", | ||
671 | progname); | ||
629 | } | 672 | } |
diff --git a/plugins/check_dummy.c b/plugins/check_dummy.c index 19f6c046..602d5868 100644 --- a/plugins/check_dummy.c +++ b/plugins/check_dummy.c | |||
@@ -45,18 +45,19 @@ int main(int argc, char **argv) { | |||
45 | bindtextdomain(PACKAGE, LOCALEDIR); | 45 | bindtextdomain(PACKAGE, LOCALEDIR); |
46 | textdomain(PACKAGE); | 46 | textdomain(PACKAGE); |
47 | 47 | ||
48 | if (argc < 2) | 48 | if (argc < 2) { |
49 | usage4(_("Could not parse arguments")); | 49 | usage4(_("Could not parse arguments")); |
50 | else if (strcmp(argv[1], "-V") == 0 || strcmp(argv[1], "--version") == 0) { | 50 | } else if (strcmp(argv[1], "-V") == 0 || strcmp(argv[1], "--version") == 0) { |
51 | print_revision(progname, NP_VERSION); | 51 | print_revision(progname, NP_VERSION); |
52 | exit(STATE_UNKNOWN); | 52 | exit(STATE_UNKNOWN); |
53 | } else if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { | 53 | } else if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { |
54 | print_help(); | 54 | print_help(); |
55 | exit(STATE_UNKNOWN); | 55 | exit(STATE_UNKNOWN); |
56 | } else if (!is_integer(argv[1])) | 56 | } else if (!is_integer(argv[1])) { |
57 | usage4(_("Arguments to check_dummy must be an integer")); | 57 | usage4(_("Arguments to check_dummy must be an integer")); |
58 | else | 58 | } else { |
59 | result = atoi(argv[1]); | 59 | result = atoi(argv[1]); |
60 | } | ||
60 | 61 | ||
61 | switch (result) { | 62 | switch (result) { |
62 | case STATE_OK: | 63 | case STATE_OK: |
@@ -78,8 +79,9 @@ int main(int argc, char **argv) { | |||
78 | return STATE_UNKNOWN; | 79 | return STATE_UNKNOWN; |
79 | } | 80 | } |
80 | 81 | ||
81 | if (argc >= 3) | 82 | if (argc >= 3) { |
82 | printf(": %s", argv[2]); | 83 | printf(": %s", argv[2]); |
84 | } | ||
83 | 85 | ||
84 | printf("\n"); | 86 | printf("\n"); |
85 | 87 | ||
@@ -92,7 +94,8 @@ void print_help(void) { | |||
92 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | 94 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
93 | printf(COPYRIGHT, copyright, email); | 95 | printf(COPYRIGHT, copyright, email); |
94 | 96 | ||
95 | printf("%s\n", _("This plugin will simply return the state corresponding to the numeric value")); | 97 | printf("%s\n", |
98 | _("This plugin will simply return the state corresponding to the numeric value")); | ||
96 | 99 | ||
97 | printf("%s\n", _("of the <state> argument with optional text")); | 100 | printf("%s\n", _("of the <state> argument with optional text")); |
98 | 101 | ||
diff --git a/plugins/check_fping.c b/plugins/check_fping.c index ec7abb67..6160c2cb 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c | |||
@@ -46,8 +46,9 @@ enum { | |||
46 | RTA = 1 | 46 | RTA = 1 |
47 | }; | 47 | }; |
48 | 48 | ||
49 | static mp_state_enum textscan(char *buf, const char * /*server_name*/, bool /*crta_p*/, double /*crta*/, bool /*wrta_p*/, double /*wrta*/, | 49 | static mp_state_enum textscan(char *buf, const char * /*server_name*/, bool /*crta_p*/, |
50 | bool /*cpl_p*/, int /*cpl*/, bool /*wpl_p*/, int /*wpl*/, bool /*alive_p*/); | 50 | double /*crta*/, bool /*wrta_p*/, double /*wrta*/, bool /*cpl_p*/, |
51 | int /*cpl*/, bool /*wpl_p*/, int /*wpl*/, bool /*alive_p*/); | ||
51 | 52 | ||
52 | typedef struct { | 53 | typedef struct { |
53 | int errorcode; | 54 | int errorcode; |
@@ -79,6 +80,24 @@ int main(int argc, char **argv) { | |||
79 | server = strscpy(server, config.server_name); | 80 | server = strscpy(server, config.server_name); |
80 | 81 | ||
81 | char *option_string = ""; | 82 | char *option_string = ""; |
83 | char *fping_prog = NULL; | ||
84 | |||
85 | /* First determine if the target is dualstack or ipv6 only. */ | ||
86 | bool server_is_inet6_addr = is_inet6_addr(server); | ||
87 | |||
88 | /* | ||
89 | * If the user requested -6 OR the user made no assertion and the address is v6 or dualstack | ||
90 | * -> we use ipv6 | ||
91 | * If the user requested -4 OR the user made no assertion and the address is v4 ONLY | ||
92 | * -> we use ipv4 | ||
93 | */ | ||
94 | if (address_family == AF_INET6 || (address_family == AF_UNSPEC && server_is_inet6_addr)) { | ||
95 | xasprintf(&option_string, "%s-6 ", option_string); | ||
96 | } else { | ||
97 | xasprintf(&option_string, "%s-4 ", option_string); | ||
98 | } | ||
99 | fping_prog = strdup(PATH_TO_FPING); | ||
100 | |||
82 | /* compose the command */ | 101 | /* compose the command */ |
83 | if (config.target_timeout) { | 102 | if (config.target_timeout) { |
84 | xasprintf(&option_string, "%s-t %d ", option_string, config.target_timeout); | 103 | xasprintf(&option_string, "%s-t %d ", option_string, config.target_timeout); |
@@ -99,19 +118,28 @@ int main(int argc, char **argv) { | |||
99 | xasprintf(&option_string, "%s-R ", option_string); | 118 | xasprintf(&option_string, "%s-R ", option_string); |
100 | } | 119 | } |
101 | 120 | ||
102 | char *fping_prog = NULL; | 121 | if (config.fwmark_set) { |
103 | #ifdef PATH_TO_FPING6 | 122 | xasprintf(&option_string, "%s--fwmark %u ", option_string, config.fwmark); |
104 | if (address_family != AF_INET && is_inet6_addr(server)) { | 123 | } |
105 | fping_prog = strdup(PATH_TO_FPING6); | 124 | |
106 | } else { | 125 | if (config.icmp_timestamp) { |
107 | fping_prog = strdup(PATH_TO_FPING); | 126 | xasprintf(&option_string, "%s--icmp-timestamp ", option_string); |
127 | } | ||
128 | |||
129 | if (config.check_source) { | ||
130 | xasprintf(&option_string, "%s--check-source ", option_string); | ||
108 | } | 131 | } |
109 | #else | ||
110 | fping_prog = strdup(PATH_TO_FPING); | ||
111 | #endif | ||
112 | 132 | ||
113 | char *command_line = NULL; | 133 | char *command_line = NULL; |
114 | xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server); | 134 | |
135 | if (config.icmp_timestamp) { | ||
136 | // no packet size settable for ICMP timestamp | ||
137 | xasprintf(&command_line, "%s %s -c %d %s", fping_prog, option_string, config.packet_count, | ||
138 | server); | ||
139 | } else { | ||
140 | xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, | ||
141 | config.packet_size, config.packet_count, server); | ||
142 | } | ||
115 | 143 | ||
116 | if (verbose) { | 144 | if (verbose) { |
117 | printf("%s\n", command_line); | 145 | printf("%s\n", command_line); |
@@ -135,8 +163,9 @@ int main(int argc, char **argv) { | |||
135 | if (verbose) { | 163 | if (verbose) { |
136 | printf("%s", input_buffer); | 164 | printf("%s", input_buffer); |
137 | } | 165 | } |
138 | status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta, | 166 | status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, |
139 | config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p)); | 167 | config.crta, config.wrta_p, config.wrta, config.cpl_p, |
168 | config.cpl, config.wpl_p, config.wpl, config.alive_p)); | ||
140 | } | 169 | } |
141 | 170 | ||
142 | /* If we get anything on STDERR, at least set warning */ | 171 | /* If we get anything on STDERR, at least set warning */ |
@@ -145,8 +174,9 @@ int main(int argc, char **argv) { | |||
145 | if (verbose) { | 174 | if (verbose) { |
146 | printf("%s", input_buffer); | 175 | printf("%s", input_buffer); |
147 | } | 176 | } |
148 | status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta, | 177 | status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, |
149 | config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p)); | 178 | config.crta, config.wrta_p, config.wrta, config.cpl_p, |
179 | config.cpl, config.wpl_p, config.wpl, config.alive_p)); | ||
150 | } | 180 | } |
151 | (void)fclose(child_stderr); | 181 | (void)fclose(child_stderr); |
152 | 182 | ||
@@ -175,8 +205,8 @@ int main(int argc, char **argv) { | |||
175 | return status; | 205 | return status; |
176 | } | 206 | } |
177 | 207 | ||
178 | mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double crta, bool wrta_p, double wrta, bool cpl_p, int cpl, | 208 | mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double crta, bool wrta_p, |
179 | bool wpl_p, int wpl, bool alive_p) { | 209 | double wrta, bool cpl_p, int cpl, bool wpl_p, int wpl, bool alive_p) { |
180 | /* stops testing after the first successful reply. */ | 210 | /* stops testing after the first successful reply. */ |
181 | double rta; | 211 | double rta; |
182 | double loss; | 212 | double loss; |
@@ -189,7 +219,8 @@ mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double c | |||
189 | die(STATE_OK, _("FPING %s - %s (rta=%f ms)|%s\n"), state_text(STATE_OK), server_name, rta, | 219 | die(STATE_OK, _("FPING %s - %s (rta=%f ms)|%s\n"), state_text(STATE_OK), server_name, rta, |
190 | /* No loss since we only waited for the first reply | 220 | /* No loss since we only waited for the first reply |
191 | perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), */ | 221 | perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), */ |
192 | fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); | 222 | fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, |
223 | false, 0)); | ||
193 | } | 224 | } |
194 | 225 | ||
195 | mp_state_enum status = STATE_UNKNOWN; | 226 | mp_state_enum status = STATE_UNKNOWN; |
@@ -230,9 +261,11 @@ mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double c | |||
230 | } else { | 261 | } else { |
231 | status = STATE_OK; | 262 | status = STATE_OK; |
232 | } | 263 | } |
233 | die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta, | 264 | die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), |
265 | server_name, loss, rta, | ||
234 | perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0), | 266 | perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0), |
235 | fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); | 267 | fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, |
268 | false, 0)); | ||
236 | 269 | ||
237 | } else if (strstr(buf, "xmt/rcv/%loss")) { | 270 | } else if (strstr(buf, "xmt/rcv/%loss")) { |
238 | /* no min/max/avg if host was unreachable in fping v2.2.b1 */ | 271 | /* no min/max/avg if host was unreachable in fping v2.2.b1 */ |
@@ -268,13 +301,38 @@ mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double c | |||
268 | 301 | ||
269 | /* process command-line arguments */ | 302 | /* process command-line arguments */ |
270 | check_fping_config_wrapper process_arguments(int argc, char **argv) { | 303 | check_fping_config_wrapper process_arguments(int argc, char **argv) { |
271 | static struct option longopts[] = { | 304 | enum { |
272 | {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'}, | 305 | FWMARK_OPT = CHAR_MAX + 1, |
273 | {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'}, | 306 | ICMP_TIMESTAMP_OPT, |
274 | {"bytes", required_argument, 0, 'b'}, {"number", required_argument, 0, 'n'}, {"target-timeout", required_argument, 0, 'T'}, | 307 | CHECK_SOURCE_OPT, |
275 | {"interval", required_argument, 0, 'i'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 308 | }; |
276 | {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, | 309 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
277 | {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}}; | 310 | {"sourceip", required_argument, 0, 'S'}, |
311 | {"sourceif", required_argument, 0, 'I'}, | ||
312 | {"critical", required_argument, 0, 'c'}, | ||
313 | {"warning", required_argument, 0, 'w'}, | ||
314 | {"alive", no_argument, 0, 'a'}, | ||
315 | {"bytes", required_argument, 0, 'b'}, | ||
316 | {"number", required_argument, 0, 'n'}, | ||
317 | {"target-timeout", required_argument, 0, 'T'}, | ||
318 | {"interval", required_argument, 0, 'i'}, | ||
319 | {"verbose", no_argument, 0, 'v'}, | ||
320 | {"version", no_argument, 0, 'V'}, | ||
321 | {"help", no_argument, 0, 'h'}, | ||
322 | {"use-ipv4", no_argument, 0, '4'}, | ||
323 | {"use-ipv6", no_argument, 0, '6'}, | ||
324 | {"dontfrag", no_argument, 0, 'M'}, | ||
325 | {"random", no_argument, 0, 'R'}, | ||
326 | #ifdef FPING_VERSION_5_2_OR_HIGHER | ||
327 | // only available with fping version >= 5.2 | ||
328 | {"fwmark", required_argument, NULL, FWMARK_OPT}, | ||
329 | # ifdef FPING_VERSION_5_3_OR_HIGHER | ||
330 | // only available with fping version >= 5.3 | ||
331 | {"icmp-timestamp", no_argument, NULL, ICMP_TIMESTAMP_OPT}, | ||
332 | {"check-source", no_argument, NULL, CHECK_SOURCE_OPT}, | ||
333 | # endif | ||
334 | #endif | ||
335 | {0, 0, 0, 0}}; | ||
278 | 336 | ||
279 | char *rv[2]; | 337 | char *rv[2]; |
280 | rv[PL] = NULL; | 338 | rv[PL] = NULL; |
@@ -299,8 +357,9 @@ check_fping_config_wrapper process_arguments(int argc, char **argv) { | |||
299 | argc--; | 357 | argc--; |
300 | } | 358 | } |
301 | 359 | ||
302 | while (1) { | 360 | while (true) { |
303 | int option_index = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); | 361 | int option_index = |
362 | getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); | ||
304 | 363 | ||
305 | if (option_index == -1 || option_index == EOF || option_index == 1) { | 364 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
306 | break; | 365 | break; |
@@ -340,11 +399,7 @@ check_fping_config_wrapper process_arguments(int argc, char **argv) { | |||
340 | address_family = AF_INET; | 399 | address_family = AF_INET; |
341 | break; | 400 | break; |
342 | case '6': /* IPv6 only */ | 401 | case '6': /* IPv6 only */ |
343 | #ifdef USE_IPV6 | ||
344 | address_family = AF_INET6; | 402 | address_family = AF_INET6; |
345 | #else | ||
346 | usage(_("IPv6 support not available\n")); | ||
347 | #endif | ||
348 | break; | 403 | break; |
349 | case 'c': | 404 | case 'c': |
350 | get_threshold(optarg, rv); | 405 | get_threshold(optarg, rv); |
@@ -406,6 +461,20 @@ check_fping_config_wrapper process_arguments(int argc, char **argv) { | |||
406 | case 'M': | 461 | case 'M': |
407 | result.config.dontfrag = true; | 462 | result.config.dontfrag = true; |
408 | break; | 463 | break; |
464 | case FWMARK_OPT: | ||
465 | if (is_intpos(optarg)) { | ||
466 | result.config.fwmark = (unsigned int)atol(optarg); | ||
467 | result.config.fwmark_set = true; | ||
468 | } else { | ||
469 | usage(_("fwmark must be a positive integer")); | ||
470 | } | ||
471 | break; | ||
472 | case ICMP_TIMESTAMP_OPT: | ||
473 | result.config.icmp_timestamp = true; | ||
474 | break; | ||
475 | case CHECK_SOURCE_OPT: | ||
476 | result.config.check_source = true; | ||
477 | break; | ||
409 | } | 478 | } |
410 | } | 479 | } |
411 | 480 | ||
@@ -427,10 +496,12 @@ int get_threshold(char *arg, char *rv[2]) { | |||
427 | if (arg2) { | 496 | if (arg2) { |
428 | arg1[strcspn(arg1, ",:")] = 0; | 497 | arg1[strcspn(arg1, ",:")] = 0; |
429 | if (strstr(arg1, "%") && strstr(arg2, "%")) { | 498 | if (strstr(arg1, "%") && strstr(arg2, "%")) { |
430 | die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, arg); | 499 | die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, |
500 | arg); | ||
431 | } | 501 | } |
432 | if (!strstr(arg1, "%") && !strstr(arg2, "%")) { | 502 | if (!strstr(arg1, "%") && !strstr(arg2, "%")) { |
433 | die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, arg); | 503 | die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, |
504 | arg); | ||
434 | } | 505 | } |
435 | } | 506 | } |
436 | 507 | ||
@@ -456,7 +527,8 @@ void print_help(void) { | |||
456 | printf("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n"); | 527 | printf("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n"); |
457 | printf(COPYRIGHT, copyright, email); | 528 | printf(COPYRIGHT, copyright, email); |
458 | 529 | ||
459 | printf("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check")); | 530 | printf("%s\n", |
531 | _("This plugin will use the fping command to ping the specified host for a fast check")); | ||
460 | 532 | ||
461 | printf("%s\n", _("Note that it is necessary to set the suid flag on fping.")); | 533 | printf("%s\n", _("Note that it is necessary to set the suid flag on fping.")); |
462 | 534 | ||
@@ -470,7 +542,8 @@ void print_help(void) { | |||
470 | printf(UT_IPv46); | 542 | printf(UT_IPv46); |
471 | 543 | ||
472 | printf(" %s\n", "-H, --hostname=HOST"); | 544 | printf(" %s\n", "-H, --hostname=HOST"); |
473 | printf(" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)")); | 545 | printf(" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, " |
546 | "reducing system load)")); | ||
474 | printf(" %s\n", "-w, --warning=THRESHOLD"); | 547 | printf(" %s\n", "-w, --warning=THRESHOLD"); |
475 | printf(" %s\n", _("warning threshold pair")); | 548 | printf(" %s\n", _("warning threshold pair")); |
476 | printf(" %s\n", "-c, --critical=THRESHOLD"); | 549 | printf(" %s\n", "-c, --critical=THRESHOLD"); |
@@ -484,7 +557,8 @@ void print_help(void) { | |||
484 | printf(" %s\n", "-T, --target-timeout=INTEGER"); | 557 | printf(" %s\n", "-T, --target-timeout=INTEGER"); |
485 | printf(" %s (default: fping's default for -t)\n", _("Target timeout (ms)")); | 558 | printf(" %s (default: fping's default for -t)\n", _("Target timeout (ms)")); |
486 | printf(" %s\n", "-i, --interval=INTEGER"); | 559 | printf(" %s\n", "-i, --interval=INTEGER"); |
487 | printf(" %s (default: fping's default for -p)\n", _("Interval (ms) between sending packets")); | 560 | printf(" %s (default: fping's default for -p)\n", |
561 | _("Interval (ms) between sending packets")); | ||
488 | printf(" %s\n", "-S, --sourceip=HOST"); | 562 | printf(" %s\n", "-S, --sourceip=HOST"); |
489 | printf(" %s\n", _("name or IP Address of sourceip")); | 563 | printf(" %s\n", _("name or IP Address of sourceip")); |
490 | printf(" %s\n", "-I, --sourceif=IF"); | 564 | printf(" %s\n", "-I, --sourceif=IF"); |
@@ -493,9 +567,20 @@ void print_help(void) { | |||
493 | printf(" %s\n", _("set the Don't Fragment flag")); | 567 | printf(" %s\n", _("set the Don't Fragment flag")); |
494 | printf(" %s\n", "-R, --random"); | 568 | printf(" %s\n", "-R, --random"); |
495 | printf(" %s\n", _("random packet data (to foil link data compression)")); | 569 | printf(" %s\n", _("random packet data (to foil link data compression)")); |
570 | #ifdef FPING_VERSION_5_2_OR_HIGHER | ||
571 | printf(" %s\n", "--fwmark=INTEGER"); | ||
572 | printf(" %s\n", _("set the routing mark to INTEGER (fping option)")); | ||
573 | # ifdef FPING_VERSION_5_3_OR_HIGHER | ||
574 | printf(" %s\n", "--icmp-timestamp"); | ||
575 | printf(" %s\n", _("use ICMP Timestamp instead of ICMP Echo (fping option)")); | ||
576 | printf(" %s\n", "--check-source"); | ||
577 | printf(" %s\n", _("discard replies not from target address (fping option)")); | ||
578 | # endif | ||
579 | #endif | ||
496 | printf(UT_VERBOSE); | 580 | printf(UT_VERBOSE); |
497 | printf("\n"); | 581 | printf("\n"); |
498 | printf(" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)")); | 582 | printf(" %s\n", |
583 | _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)")); | ||
499 | printf(" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of")); | 584 | printf(" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of")); |
500 | printf(" %s\n", _("packet loss to trigger an alarm state.")); | 585 | printf(" %s\n", _("packet loss to trigger an alarm state.")); |
501 | 586 | ||
@@ -507,5 +592,6 @@ void print_help(void) { | |||
507 | 592 | ||
508 | void print_usage(void) { | 593 | void print_usage(void) { |
509 | printf("%s\n", _("Usage:")); | 594 | printf("%s\n", _("Usage:")); |
510 | printf(" %s <host_address> -w limit -c limit [-b size] [-n number] [-T number] [-i number]\n", progname); | 595 | printf(" %s <host_address> -w limit -c limit [-b size] [-n number] [-T number] [-i number]\n", |
596 | progname); | ||
511 | } | 597 | } |
diff --git a/plugins/check_fping.d/config.h b/plugins/check_fping.d/config.h index a0697bf3..d3e50565 100644 --- a/plugins/check_fping.d/config.h +++ b/plugins/check_fping.d/config.h | |||
@@ -29,6 +29,20 @@ typedef struct { | |||
29 | bool cpl_p; | 29 | bool cpl_p; |
30 | int wpl; | 30 | int wpl; |
31 | bool wpl_p; | 31 | bool wpl_p; |
32 | |||
33 | // only available with fping version >= 5.2 | ||
34 | // for a given uint _fwmark_ fping sets _fwmark_ as a firewall mark | ||
35 | // in the packets | ||
36 | unsigned int fwmark; | ||
37 | bool fwmark_set; | ||
38 | |||
39 | // only available with fping version >= 5.3 | ||
40 | // Setting icmp_timestamp tells fping to use ICMP Timestamp (ICMP type 13) instead | ||
41 | // of ICMP Echo | ||
42 | bool icmp_timestamp; | ||
43 | |||
44 | // Setting check_source lets fping discard replies which are not from the target address | ||
45 | bool check_source; | ||
32 | } check_fping_config; | 46 | } check_fping_config; |
33 | 47 | ||
34 | check_fping_config check_fping_config_init() { | 48 | check_fping_config check_fping_config_init() { |
@@ -53,6 +67,15 @@ check_fping_config check_fping_config_init() { | |||
53 | .cpl_p = false, | 67 | .cpl_p = false, |
54 | .wpl = 0, | 68 | .wpl = 0, |
55 | .wpl_p = false, | 69 | .wpl_p = false, |
70 | |||
71 | // only available with fping version >= 5.2 | ||
72 | .fwmark = 0, | ||
73 | .fwmark_set = false, // just to be deterministic | ||
74 | |||
75 | // only available with fping version >= 5.3 | ||
76 | .icmp_timestamp = false, | ||
77 | .check_source = false, | ||
78 | |||
56 | }; | 79 | }; |
57 | return tmp; | 80 | return tmp; |
58 | } | 81 | } |
diff --git a/plugins/check_game.c b/plugins/check_game.c index c0193b03..974a7253 100644 --- a/plugins/check_game.c +++ b/plugins/check_game.c | |||
@@ -77,7 +77,8 @@ int main(int argc, char **argv) { | |||
77 | 77 | ||
78 | /* create the command line to execute */ | 78 | /* create the command line to execute */ |
79 | char *command_line = NULL; | 79 | char *command_line = NULL; |
80 | xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, config.game_type, config.server_ip); | 80 | xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, |
81 | config.game_type, config.server_ip); | ||
81 | 82 | ||
82 | if (config.port) { | 83 | if (config.port) { |
83 | xasprintf(&command_line, "%s:%-d", command_line, config.port); | 84 | xasprintf(&command_line, "%s:%-d", command_line, config.port); |
@@ -130,11 +131,13 @@ int main(int argc, char **argv) { | |||
130 | printf(_("CRITICAL - Game server timeout\n")); | 131 | printf(_("CRITICAL - Game server timeout\n")); |
131 | result = STATE_CRITICAL; | 132 | result = STATE_CRITICAL; |
132 | } else { | 133 | } else { |
133 | printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[config.qstat_game_players], ret[config.qstat_game_players_max], | 134 | printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[config.qstat_game_players], |
134 | ret[config.qstat_game_field], ret[config.qstat_map_field], ret[config.qstat_ping_field], | 135 | ret[config.qstat_game_players_max], ret[config.qstat_game_field], |
135 | perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, true, 0, true, | 136 | ret[config.qstat_map_field], ret[config.qstat_ping_field], |
136 | atol(ret[config.qstat_game_players_max])), | 137 | perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, |
137 | fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); | 138 | true, 0, true, atol(ret[config.qstat_game_players_max])), |
139 | fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, | ||
140 | true, 0, false, 0)); | ||
138 | } | 141 | } |
139 | 142 | ||
140 | exit(result); | 143 | exit(result); |
@@ -144,19 +147,20 @@ int main(int argc, char **argv) { | |||
144 | #define max_players_field_index 130 | 147 | #define max_players_field_index 130 |
145 | 148 | ||
146 | check_game_config_wrapper process_arguments(int argc, char **argv) { | 149 | check_game_config_wrapper process_arguments(int argc, char **argv) { |
147 | static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, | 150 | static struct option long_opts[] = { |
148 | {"version", no_argument, 0, 'V'}, | 151 | {"help", no_argument, 0, 'h'}, |
149 | {"verbose", no_argument, 0, 'v'}, | 152 | {"version", no_argument, 0, 'V'}, |
150 | {"timeout", required_argument, 0, 't'}, | 153 | {"verbose", no_argument, 0, 'v'}, |
151 | {"hostname", required_argument, 0, 'H'}, | 154 | {"timeout", required_argument, 0, 't'}, |
152 | {"port", required_argument, 0, 'P'}, | 155 | {"hostname", required_argument, 0, 'H'}, |
153 | {"game-type", required_argument, 0, 'G'}, | 156 | {"port", required_argument, 0, 'P'}, |
154 | {"map-field", required_argument, 0, 'm'}, | 157 | {"game-type", required_argument, 0, 'G'}, |
155 | {"ping-field", required_argument, 0, 'p'}, | 158 | {"map-field", required_argument, 0, 'm'}, |
156 | {"game-field", required_argument, 0, 'g'}, | 159 | {"ping-field", required_argument, 0, 'p'}, |
157 | {"players-field", required_argument, 0, players_field_index}, | 160 | {"game-field", required_argument, 0, 'g'}, |
158 | {"max-players-field", required_argument, 0, max_players_field_index}, | 161 | {"players-field", required_argument, 0, players_field_index}, |
159 | {0, 0, 0, 0}}; | 162 | {"max-players-field", required_argument, 0, max_players_field_index}, |
163 | {0, 0, 0, 0}}; | ||
160 | 164 | ||
161 | check_game_config_wrapper result = { | 165 | check_game_config_wrapper result = { |
162 | .config = check_game_config_init(), | 166 | .config = check_game_config_init(), |
@@ -216,21 +220,24 @@ check_game_config_wrapper process_arguments(int argc, char **argv) { | |||
216 | break; | 220 | break; |
217 | case 'p': /* index of ping field */ | 221 | case 'p': /* index of ping field */ |
218 | result.config.qstat_ping_field = atoi(optarg); | 222 | result.config.qstat_ping_field = atoi(optarg); |
219 | if (result.config.qstat_ping_field < 0 || result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) { | 223 | if (result.config.qstat_ping_field < 0 || |
224 | result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) { | ||
220 | result.errorcode = ERROR; | 225 | result.errorcode = ERROR; |
221 | return result; | 226 | return result; |
222 | } | 227 | } |
223 | break; | 228 | break; |
224 | case 'm': /* index on map field */ | 229 | case 'm': /* index on map field */ |
225 | result.config.qstat_map_field = atoi(optarg); | 230 | result.config.qstat_map_field = atoi(optarg); |
226 | if (result.config.qstat_map_field < 0 || result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) { | 231 | if (result.config.qstat_map_field < 0 || |
232 | result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) { | ||
227 | result.errorcode = ERROR; | 233 | result.errorcode = ERROR; |
228 | return result; | 234 | return result; |
229 | } | 235 | } |
230 | break; | 236 | break; |
231 | case 'g': /* index of game field */ | 237 | case 'g': /* index of game field */ |
232 | result.config.qstat_game_field = atoi(optarg); | 238 | result.config.qstat_game_field = atoi(optarg); |
233 | if (result.config.qstat_game_field < 0 || result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) { | 239 | if (result.config.qstat_game_field < 0 || |
240 | result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) { | ||
234 | result.errorcode = ERROR; | 241 | result.errorcode = ERROR; |
235 | return result; | 242 | return result; |
236 | } | 243 | } |
@@ -240,14 +247,16 @@ check_game_config_wrapper process_arguments(int argc, char **argv) { | |||
240 | if (result.config.qstat_game_players_max == 0) { | 247 | if (result.config.qstat_game_players_max == 0) { |
241 | result.config.qstat_game_players_max = result.config.qstat_game_players - 1; | 248 | result.config.qstat_game_players_max = result.config.qstat_game_players - 1; |
242 | } | 249 | } |
243 | if (result.config.qstat_game_players < 0 || result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) { | 250 | if (result.config.qstat_game_players < 0 || |
251 | result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) { | ||
244 | result.errorcode = ERROR; | 252 | result.errorcode = ERROR; |
245 | return result; | 253 | return result; |
246 | } | 254 | } |
247 | break; | 255 | break; |
248 | case max_players_field_index: /* index of max players field */ | 256 | case max_players_field_index: /* index of max players field */ |
249 | result.config.qstat_game_players_max = atoi(optarg); | 257 | result.config.qstat_game_players_max = atoi(optarg); |
250 | if (result.config.qstat_game_players_max < 0 || result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) { | 258 | if (result.config.qstat_game_players_max < 0 || |
259 | result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) { | ||
251 | result.errorcode = ERROR; | 260 | result.errorcode = ERROR; |
252 | return result; | 261 | return result; |
253 | } | 262 | } |
@@ -286,7 +295,7 @@ void print_help(void) { | |||
286 | printf(UT_HELP_VRSN); | 295 | printf(UT_HELP_VRSN); |
287 | printf(UT_EXTRA_OPTS); | 296 | printf(UT_EXTRA_OPTS); |
288 | printf(" -H, --hostname=ADDRESS\n" | 297 | printf(" -H, --hostname=ADDRESS\n" |
289 | " Host name, IP Address, or unix socket (must be an absolute path)\n"); | 298 | " Host name, IP Address, or unix socket (must be an absolute path)\n"); |
290 | printf(" %s\n", "-P"); | 299 | printf(" %s\n", "-P"); |
291 | printf(" %s\n", _("Optional port to connect to")); | 300 | printf(" %s\n", _("Optional port to connect to")); |
292 | printf(" %s\n", "-g"); | 301 | printf(" %s\n", "-g"); |
@@ -300,8 +309,10 @@ void print_help(void) { | |||
300 | 309 | ||
301 | printf("\n"); | 310 | printf("\n"); |
302 | printf("%s\n", _("Notes:")); | 311 | printf("%s\n", _("Notes:")); |
303 | printf(" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool.")); | 312 | printf(" %s\n", |
304 | printf(" %s\n", _("If you don't have the package installed, you will need to download it from")); | 313 | _("This plugin uses the 'qstat' command, the popular game server status query tool.")); |
314 | printf(" %s\n", | ||
315 | _("If you don't have the package installed, you will need to download it from")); | ||
305 | printf(" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin.")); | 316 | printf(" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin.")); |
306 | 317 | ||
307 | printf(UT_SUPPORT); | 318 | printf(UT_SUPPORT); |
@@ -309,7 +320,8 @@ void print_help(void) { | |||
309 | 320 | ||
310 | void print_usage(void) { | 321 | void print_usage(void) { |
311 | printf("%s\n", _("Usage:")); | 322 | printf("%s\n", _("Usage:")); |
312 | printf(" %s [-hvV] [-P port] [-t timeout] [-g game_field] [-m map_field] [-p ping_field] [-G game-time] [-H hostname] <game> " | 323 | printf(" %s [-hvV] [-P port] [-t timeout] [-g game_field] [-m map_field] [-p ping_field] [-G " |
324 | "game-time] [-H hostname] <game> " | ||
313 | "<ip_address>\n", | 325 | "<ip_address>\n", |
314 | progname); | 326 | progname); |
315 | } | 327 | } |
diff --git a/plugins/check_hpjd.c b/plugins/check_hpjd.c index 62417fd6..9907abc5 100644 --- a/plugins/check_hpjd.c +++ b/plugins/check_hpjd.c | |||
@@ -85,13 +85,16 @@ int main(int argc, char **argv) { | |||
85 | char query_string[512]; | 85 | char query_string[512]; |
86 | /* removed ' 2>1' at end of command 10/27/1999 - EG */ | 86 | /* removed ' 2>1' at end of command 10/27/1999 - EG */ |
87 | /* create the query string */ | 87 | /* create the query string */ |
88 | sprintf(query_string, "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0", HPJD_LINE_STATUS, HPJD_PAPER_STATUS, | 88 | sprintf(query_string, "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0", |
89 | HPJD_INTERVENTION_REQUIRED, HPJD_GD_PERIPHERAL_ERROR, HPJD_GD_PAPER_JAM, HPJD_GD_PAPER_OUT, HPJD_GD_TONER_LOW, | 89 | HPJD_LINE_STATUS, HPJD_PAPER_STATUS, HPJD_INTERVENTION_REQUIRED, |
90 | HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); | 90 | HPJD_GD_PERIPHERAL_ERROR, HPJD_GD_PAPER_JAM, HPJD_GD_PAPER_OUT, HPJD_GD_TONER_LOW, |
91 | HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, | ||
92 | HPJD_GD_STATUS_DISPLAY); | ||
91 | 93 | ||
92 | /* get the command to run */ | 94 | /* get the command to run */ |
93 | char command_line[1024]; | 95 | char command_line[1024]; |
94 | sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, config.community, config.address, config.port, query_string); | 96 | sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, config.community, |
97 | config.address, config.port, query_string); | ||
95 | 98 | ||
96 | /* run the command */ | 99 | /* run the command */ |
97 | child_process = spopen(command_line); | 100 | child_process = spopen(command_line); |
@@ -177,7 +180,8 @@ int main(int argc, char **argv) { | |||
177 | strcpy(display_message, temp_buffer + 1); | 180 | strcpy(display_message, temp_buffer + 1); |
178 | break; | 181 | break; |
179 | default: /* fold multiline message */ | 182 | default: /* fold multiline message */ |
180 | strncat(display_message, input_buffer, sizeof(display_message) - strlen(display_message) - 1); | 183 | strncat(display_message, input_buffer, |
184 | sizeof(display_message) - strlen(display_message) - 1); | ||
181 | } | 185 | } |
182 | } | 186 | } |
183 | 187 | ||
diff --git a/plugins/check_http.c b/plugins/check_http.c index baff682a..d264b95d 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c | |||
@@ -1,35 +1,35 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_http plugin | 3 | * Monitoring check_http plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team | 6 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains the check_http plugin | 10 | * This file contains the check_http plugin |
11 | * | 11 | * |
12 | * This plugin tests the HTTP service on the specified host. It can test | 12 | * This plugin tests the HTTP service on the specified host. It can test |
13 | * normal (http) and secure (https) servers, follow redirects, search for | 13 | * normal (http) and secure (https) servers, follow redirects, search for |
14 | * strings and regular expressions, check connection times, and report on | 14 | * strings and regular expressions, check connection times, and report on |
15 | * certificate expiration times. | 15 | * certificate expiration times. |
16 | * | 16 | * |
17 | * | 17 | * |
18 | * This program is free software: you can redistribute it and/or modify | 18 | * This program is free software: you can redistribute it and/or modify |
19 | * it under the terms of the GNU General Public License as published by | 19 | * it under the terms of the GNU General Public License as published by |
20 | * the Free Software Foundation, either version 3 of the License, or | 20 | * the Free Software Foundation, either version 3 of the License, or |
21 | * (at your option) any later version. | 21 | * (at your option) any later version. |
22 | * | 22 | * |
23 | * This program is distributed in the hope that it will be useful, | 23 | * This program is distributed in the hope that it will be useful, |
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 | * GNU General Public License for more details. | 26 | * GNU General Public License for more details. |
27 | * | 27 | * |
28 | * You should have received a copy of the GNU General Public License | 28 | * You should have received a copy of the GNU General Public License |
29 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 29 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
30 | * | 30 | * |
31 | * | 31 | * |
32 | *****************************************************************************/ | 32 | *****************************************************************************/ |
33 | 33 | ||
34 | const char *progname = "check_http"; | 34 | const char *progname = "check_http"; |
35 | const char *copyright = "1999-2024"; | 35 | const char *copyright = "1999-2024"; |
@@ -41,7 +41,6 @@ const char *email = "devel@monitoring-plugins.org"; | |||
41 | #include "base64.h" | 41 | #include "base64.h" |
42 | #include "netutils.h" | 42 | #include "netutils.h" |
43 | #include "utils.h" | 43 | #include "utils.h" |
44 | #include "base64.h" | ||
45 | #include <ctype.h> | 44 | #include <ctype.h> |
46 | 45 | ||
47 | #define STICKY_NONE 0 | 46 | #define STICKY_NONE 0 |
@@ -50,1346 +49,1394 @@ const char *email = "devel@monitoring-plugins.org"; | |||
50 | 49 | ||
51 | #define HTTP_EXPECT "HTTP/1." | 50 | #define HTTP_EXPECT "HTTP/1." |
52 | enum { | 51 | enum { |
53 | MAX_IPV4_HOSTLENGTH = 255, | 52 | MAX_IPV4_HOSTLENGTH = 255, |
54 | HTTP_PORT = 80, | 53 | HTTP_PORT = 80, |
55 | HTTPS_PORT = 443, | 54 | HTTPS_PORT = 443, |
56 | MAX_PORT = 65535, | 55 | MAX_PORT = 65535, |
57 | DEFAULT_MAX_REDIRS = 15 | 56 | DEFAULT_MAX_REDIRS = 15 |
58 | }; | 57 | }; |
59 | 58 | ||
60 | #ifdef HAVE_SSL | 59 | #ifdef HAVE_SSL |
61 | bool check_cert = false; | 60 | static bool check_cert = false; |
62 | bool continue_after_check_cert = false; | 61 | static bool continue_after_check_cert = false; |
63 | int ssl_version = 0; | 62 | static int ssl_version = 0; |
64 | int days_till_exp_warn, days_till_exp_crit; | 63 | static int days_till_exp_warn, days_till_exp_crit; |
65 | char *randbuff; | 64 | # define my_recv(buf, len) ((use_ssl) ? np_net_ssl_read(buf, len) : read(sd, buf, len)) |
66 | X509 *server_cert; | 65 | # define my_send(buf, len) ((use_ssl) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0)) |
67 | # define my_recv(buf, len) ((use_ssl) ? np_net_ssl_read(buf, len) : read(sd, buf, len)) | ||
68 | # define my_send(buf, len) ((use_ssl) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0)) | ||
69 | #else /* ifndef HAVE_SSL */ | 66 | #else /* ifndef HAVE_SSL */ |
70 | # define my_recv(buf, len) read(sd, buf, len) | 67 | # define my_recv(buf, len) read(sd, buf, len) |
71 | # define my_send(buf, len) send(sd, buf, len, 0) | 68 | # define my_send(buf, len) send(sd, buf, len, 0) |
72 | #endif /* HAVE_SSL */ | 69 | #endif /* HAVE_SSL */ |
73 | bool no_body = false; | 70 | static bool no_body = false; |
74 | int maximum_age = -1; | 71 | static int maximum_age = -1; |
75 | 72 | ||
76 | enum { | 73 | enum { |
77 | REGS = 2, | 74 | REGS = 2, |
78 | MAX_RE_SIZE = 1024 | 75 | MAX_RE_SIZE = 1024 |
79 | }; | 76 | }; |
80 | #include "regex.h" | 77 | #include "regex.h" |
81 | regex_t preg; | 78 | static regex_t preg; |
82 | regmatch_t pmatch[REGS]; | 79 | static regmatch_t pmatch[REGS]; |
83 | char regexp[MAX_RE_SIZE]; | 80 | static char regexp[MAX_RE_SIZE]; |
84 | char errbuf[MAX_INPUT_BUFFER]; | 81 | static char errbuf[MAX_INPUT_BUFFER]; |
85 | int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; | 82 | static int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; |
86 | int errcode; | 83 | static int errcode; |
87 | int invert_regex = 0; | 84 | static int invert_regex = 0; |
88 | int state_regex = STATE_CRITICAL; | 85 | static int state_regex = STATE_CRITICAL; |
89 | 86 | ||
90 | struct timeval tv; | 87 | static struct timeval tv; |
91 | struct timeval tv_temp; | 88 | static struct timeval tv_temp; |
92 | 89 | ||
93 | #define HTTP_URL "/" | 90 | #define HTTP_URL "/" |
94 | #define CRLF "\r\n" | 91 | #define CRLF "\r\n" |
95 | 92 | ||
96 | bool specify_port = false; | 93 | static bool specify_port = false; |
97 | int server_port = HTTP_PORT; | 94 | static int server_port = HTTP_PORT; |
98 | int virtual_port = 0; | 95 | static int virtual_port = 0; |
99 | char server_port_text[6] = ""; | 96 | static char server_type[6] = "http"; |
100 | char server_type[6] = "http"; | 97 | static char *server_address; |
101 | char *server_address; | 98 | static char *host_name; |
102 | char *host_name; | 99 | static int host_name_length; |
103 | int host_name_length; | 100 | static char *server_url; |
104 | char *server_url; | 101 | static char *user_agent; |
105 | char *user_agent; | 102 | static int server_url_length; |
106 | int server_url_length; | 103 | static int server_expect_yn = 0; |
107 | int server_expect_yn = 0; | 104 | static char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; |
108 | char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; | 105 | static char header_expect[MAX_INPUT_BUFFER] = ""; |
109 | char header_expect[MAX_INPUT_BUFFER] = ""; | 106 | static char string_expect[MAX_INPUT_BUFFER] = ""; |
110 | char string_expect[MAX_INPUT_BUFFER] = ""; | 107 | static char *warning_thresholds = NULL; |
111 | char *warning_thresholds = NULL; | 108 | static char *critical_thresholds = NULL; |
112 | char *critical_thresholds = NULL; | 109 | static thresholds *thlds; |
113 | thresholds *thlds; | 110 | static char user_auth[MAX_INPUT_BUFFER] = ""; |
114 | char user_auth[MAX_INPUT_BUFFER] = ""; | 111 | static char proxy_auth[MAX_INPUT_BUFFER] = ""; |
115 | char proxy_auth[MAX_INPUT_BUFFER] = ""; | 112 | static bool display_html = false; |
116 | bool display_html = false; | 113 | static char **http_opt_headers; |
117 | char **http_opt_headers; | 114 | static int http_opt_headers_count = 0; |
118 | int http_opt_headers_count = 0; | 115 | static int onredirect = STATE_OK; |
119 | int onredirect = STATE_OK; | 116 | static int followsticky = STICKY_NONE; |
120 | int followsticky = STICKY_NONE; | 117 | static bool use_ssl = false; |
121 | bool use_ssl = false; | 118 | static bool use_sni = false; |
122 | bool use_sni = false; | 119 | static bool verbose = false; |
123 | bool verbose = false; | 120 | static bool show_extended_perfdata = false; |
124 | bool show_extended_perfdata = false; | 121 | static bool show_body = false; |
125 | bool show_body = false; | 122 | static int sd; |
126 | int sd; | 123 | static int min_page_len = 0; |
127 | int min_page_len = 0; | 124 | static int max_page_len = 0; |
128 | int max_page_len = 0; | 125 | static int redir_depth = 0; |
129 | int redir_depth = 0; | 126 | static int max_depth = DEFAULT_MAX_REDIRS; |
130 | int max_depth = DEFAULT_MAX_REDIRS; | 127 | static char *http_method; |
131 | char *http_method; | 128 | static char *http_method_proxy; |
132 | char *http_method_proxy; | 129 | static char *http_post_data; |
133 | char *http_post_data; | 130 | static char *http_content_type; |
134 | char *http_content_type; | 131 | static char buffer[MAX_INPUT_BUFFER]; |
135 | char buffer[MAX_INPUT_BUFFER]; | 132 | static char *client_cert = NULL; |
136 | char *client_cert = NULL; | 133 | static char *client_privkey = NULL; |
137 | char *client_privkey = NULL; | ||
138 | 134 | ||
139 | // Forward function declarations | 135 | // Forward function declarations |
140 | bool process_arguments (int, char **); | 136 | static bool process_arguments(int /*argc*/, char ** /*argv*/); |
141 | int check_http (void); | 137 | static int check_http(void); |
142 | void redir (char *pos, char *status_line); | 138 | static void redir(char *pos, char *status_line); |
143 | bool server_type_check(const char *type); | 139 | static bool server_type_check(const char *type); |
144 | int server_port_check(int ssl_flag); | 140 | static int server_port_check(int ssl_flag); |
145 | char *perfd_time (double microsec); | 141 | static char *perfd_time(double elapsed_time); |
146 | char *perfd_time_connect (double microsec); | 142 | static char *perfd_time_connect(double elapsed_time_connect); |
147 | char *perfd_time_ssl (double microsec); | 143 | static char *perfd_time_ssl(double elapsed_time_ssl); |
148 | char *perfd_time_firstbyte (double microsec); | 144 | static char *perfd_time_firstbyte(double elapsed_time_firstbyte); |
149 | char *perfd_time_headers (double microsec); | 145 | static char *perfd_time_headers(double elapsed_time_headers); |
150 | char *perfd_time_transfer (double microsec); | 146 | static char *perfd_time_transfer(double elapsed_time_transfer); |
151 | char *perfd_size (int page_len); | 147 | static char *perfd_size(int page_len); |
152 | void print_help (void); | 148 | void print_help(void); |
153 | void print_usage (void); | 149 | void print_usage(void); |
154 | char *unchunk_content(const char *content); | 150 | static char *unchunk_content(const char *content); |
155 | 151 | ||
156 | int | 152 | int main(int argc, char **argv) { |
157 | main (int argc, char **argv) | 153 | int result = STATE_UNKNOWN; |
158 | { | 154 | |
159 | int result = STATE_UNKNOWN; | 155 | setlocale(LC_ALL, ""); |
160 | 156 | bindtextdomain(PACKAGE, LOCALEDIR); | |
161 | setlocale (LC_ALL, ""); | 157 | textdomain(PACKAGE); |
162 | bindtextdomain (PACKAGE, LOCALEDIR); | 158 | |
163 | textdomain (PACKAGE); | 159 | /* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */ |
164 | 160 | server_url = strdup(HTTP_URL); | |
165 | /* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */ | 161 | server_url_length = strlen(server_url); |
166 | server_url = strdup(HTTP_URL); | 162 | xasprintf(&user_agent, "User-Agent: check_http/v%s (monitoring-plugins %s)", NP_VERSION, |
167 | server_url_length = strlen(server_url); | 163 | VERSION); |
168 | xasprintf (&user_agent, "User-Agent: check_http/v%s (monitoring-plugins %s)", | 164 | |
169 | NP_VERSION, VERSION); | 165 | /* Parse extra opts if any */ |
170 | 166 | argv = np_extra_opts(&argc, argv, progname); | |
171 | /* Parse extra opts if any */ | 167 | |
172 | argv=np_extra_opts (&argc, argv, progname); | 168 | if (!process_arguments(argc, argv)) { |
173 | 169 | usage4(_("Could not parse arguments")); | |
174 | if (process_arguments (argc, argv) == false) | 170 | } |
175 | usage4 (_("Could not parse arguments")); | 171 | |
176 | 172 | if (display_html) { | |
177 | if (display_html == true) | 173 | printf("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">", use_ssl ? "https" : "http", |
178 | printf ("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">", | 174 | host_name ? host_name : server_address, server_port, server_url); |
179 | use_ssl ? "https" : "http", host_name ? host_name : server_address, | 175 | } |
180 | server_port, server_url); | 176 | |
181 | 177 | /* initialize alarm signal handling, set socket timeout, start timer */ | |
182 | /* initialize alarm signal handling, set socket timeout, start timer */ | 178 | (void)signal(SIGALRM, socket_timeout_alarm_handler); |
183 | (void) signal (SIGALRM, socket_timeout_alarm_handler); | 179 | (void)alarm(socket_timeout); |
184 | (void) alarm (socket_timeout); | 180 | gettimeofday(&tv, NULL); |
185 | gettimeofday (&tv, NULL); | 181 | |
186 | 182 | result = check_http(); | |
187 | result = check_http (); | 183 | return result; |
188 | return result; | ||
189 | } | 184 | } |
190 | 185 | ||
191 | /* check whether a file exists */ | 186 | /* check whether a file exists */ |
192 | void | 187 | void test_file(char *path) { |
193 | test_file (char *path) | 188 | if (access(path, R_OK) == 0) { |
194 | { | 189 | return; |
195 | if (access(path, R_OK) == 0) | 190 | } |
196 | return; | 191 | usage2(_("file does not exist or is not readable"), path); |
197 | usage2 (_("file does not exist or is not readable"), path); | ||
198 | } | 192 | } |
199 | 193 | ||
200 | /* | 194 | /* |
201 | * process command-line arguments | 195 | * process command-line arguments |
202 | * returns true on success, false otherwise | 196 | * returns true on success, false otherwise |
203 | */ | 197 | */ |
204 | bool process_arguments (int argc, char **argv) | 198 | bool process_arguments(int argc, char **argv) { |
205 | { | 199 | int c = 1; |
206 | int c = 1; | 200 | char *p; |
207 | char *p; | 201 | char *temp; |
208 | char *temp; | 202 | |
209 | 203 | enum { | |
210 | enum { | 204 | INVERT_REGEX = CHAR_MAX + 1, |
211 | INVERT_REGEX = CHAR_MAX + 1, | 205 | SNI_OPTION, |
212 | SNI_OPTION, | 206 | MAX_REDIRS_OPTION, |
213 | MAX_REDIRS_OPTION, | 207 | CONTINUE_AFTER_CHECK_CERT, |
214 | CONTINUE_AFTER_CHECK_CERT, | 208 | STATE_REGEX |
215 | STATE_REGEX | 209 | }; |
216 | }; | 210 | |
217 | 211 | int option = 0; | |
218 | int option = 0; | 212 | static struct option longopts[] = { |
219 | static struct option longopts[] = { | 213 | STD_LONG_OPTS, |
220 | STD_LONG_OPTS, | 214 | {"link", no_argument, 0, 'L'}, |
221 | {"link", no_argument, 0, 'L'}, | 215 | {"nohtml", no_argument, 0, 'n'}, |
222 | {"nohtml", no_argument, 0, 'n'}, | 216 | {"ssl", optional_argument, 0, 'S'}, |
223 | {"ssl", optional_argument, 0, 'S'}, | 217 | {"sni", no_argument, 0, SNI_OPTION}, |
224 | {"sni", no_argument, 0, SNI_OPTION}, | 218 | {"post", required_argument, 0, 'P'}, |
225 | {"post", required_argument, 0, 'P'}, | 219 | {"method", required_argument, 0, 'j'}, |
226 | {"method", required_argument, 0, 'j'}, | 220 | {"IP-address", required_argument, 0, 'I'}, |
227 | {"IP-address", required_argument, 0, 'I'}, | 221 | {"url", required_argument, 0, 'u'}, |
228 | {"url", required_argument, 0, 'u'}, | 222 | {"port", required_argument, 0, 'p'}, |
229 | {"port", required_argument, 0, 'p'}, | 223 | {"authorization", required_argument, 0, 'a'}, |
230 | {"authorization", required_argument, 0, 'a'}, | 224 | {"proxy-authorization", required_argument, 0, 'b'}, |
231 | {"proxy-authorization", required_argument, 0, 'b'}, | 225 | {"header-string", required_argument, 0, 'd'}, |
232 | {"header-string", required_argument, 0, 'd'}, | 226 | {"string", required_argument, 0, 's'}, |
233 | {"string", required_argument, 0, 's'}, | 227 | {"expect", required_argument, 0, 'e'}, |
234 | {"expect", required_argument, 0, 'e'}, | 228 | {"regex", required_argument, 0, 'r'}, |
235 | {"regex", required_argument, 0, 'r'}, | 229 | {"ereg", required_argument, 0, 'r'}, |
236 | {"ereg", required_argument, 0, 'r'}, | 230 | {"eregi", required_argument, 0, 'R'}, |
237 | {"eregi", required_argument, 0, 'R'}, | 231 | {"linespan", no_argument, 0, 'l'}, |
238 | {"linespan", no_argument, 0, 'l'}, | 232 | {"onredirect", required_argument, 0, 'f'}, |
239 | {"onredirect", required_argument, 0, 'f'}, | 233 | {"certificate", required_argument, 0, 'C'}, |
240 | {"certificate", required_argument, 0, 'C'}, | 234 | {"client-cert", required_argument, 0, 'J'}, |
241 | {"client-cert", required_argument, 0, 'J'}, | 235 | {"private-key", required_argument, 0, 'K'}, |
242 | {"private-key", required_argument, 0, 'K'}, | 236 | {"continue-after-certificate", no_argument, 0, CONTINUE_AFTER_CHECK_CERT}, |
243 | {"continue-after-certificate", no_argument, 0, CONTINUE_AFTER_CHECK_CERT}, | 237 | {"useragent", required_argument, 0, 'A'}, |
244 | {"useragent", required_argument, 0, 'A'}, | 238 | {"header", required_argument, 0, 'k'}, |
245 | {"header", required_argument, 0, 'k'}, | 239 | {"no-body", no_argument, 0, 'N'}, |
246 | {"no-body", no_argument, 0, 'N'}, | 240 | {"max-age", required_argument, 0, 'M'}, |
247 | {"max-age", required_argument, 0, 'M'}, | 241 | {"content-type", required_argument, 0, 'T'}, |
248 | {"content-type", required_argument, 0, 'T'}, | 242 | {"pagesize", required_argument, 0, 'm'}, |
249 | {"pagesize", required_argument, 0, 'm'}, | 243 | {"invert-regex", no_argument, NULL, INVERT_REGEX}, |
250 | {"invert-regex", no_argument, NULL, INVERT_REGEX}, | 244 | {"state-regex", required_argument, 0, STATE_REGEX}, |
251 | {"state-regex", required_argument, 0, STATE_REGEX}, | 245 | {"use-ipv4", no_argument, 0, '4'}, |
252 | {"use-ipv4", no_argument, 0, '4'}, | 246 | {"use-ipv6", no_argument, 0, '6'}, |
253 | {"use-ipv6", no_argument, 0, '6'}, | 247 | {"extended-perfdata", no_argument, 0, 'E'}, |
254 | {"extended-perfdata", no_argument, 0, 'E'}, | 248 | {"show-body", no_argument, 0, 'B'}, |
255 | {"show-body", no_argument, 0, 'B'}, | 249 | {"max-redirs", required_argument, 0, MAX_REDIRS_OPTION}, |
256 | {"max-redirs", required_argument, 0, MAX_REDIRS_OPTION}, | 250 | {0, 0, 0, 0}}; |
257 | {0, 0, 0, 0} | 251 | |
258 | }; | 252 | if (argc < 2) { |
259 | 253 | return false; | |
260 | if (argc < 2) | 254 | } |
261 | return false; | 255 | |
262 | 256 | for (c = 1; c < argc; c++) { | |
263 | for (c = 1; c < argc; c++) { | 257 | if (strcmp("-to", argv[c]) == 0) { |
264 | if (strcmp ("-to", argv[c]) == 0) | 258 | strcpy(argv[c], "-t"); |
265 | strcpy (argv[c], "-t"); | 259 | } |
266 | if (strcmp ("-hn", argv[c]) == 0) | 260 | if (strcmp("-hn", argv[c]) == 0) { |
267 | strcpy (argv[c], "-H"); | 261 | strcpy(argv[c], "-H"); |
268 | if (strcmp ("-wt", argv[c]) == 0) | 262 | } |
269 | strcpy (argv[c], "-w"); | 263 | if (strcmp("-wt", argv[c]) == 0) { |
270 | if (strcmp ("-ct", argv[c]) == 0) | 264 | strcpy(argv[c], "-w"); |
271 | strcpy (argv[c], "-c"); | 265 | } |
272 | if (strcmp ("-nohtml", argv[c]) == 0) | 266 | if (strcmp("-ct", argv[c]) == 0) { |
273 | strcpy (argv[c], "-n"); | 267 | strcpy(argv[c], "-c"); |
274 | } | 268 | } |
275 | 269 | if (strcmp("-nohtml", argv[c]) == 0) { | |
276 | while (1) { | 270 | strcpy(argv[c], "-n"); |
277 | c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:d:e:p:s:R:r:u:f:C:J:K:nlLS::m:M:NEB", longopts, &option); | 271 | } |
278 | if (c == -1 || c == EOF) | 272 | } |
279 | break; | 273 | |
280 | 274 | while (1) { | |
281 | switch (c) { | 275 | c = getopt_long(argc, argv, |
282 | case '?': /* usage */ | 276 | "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:d:e:p:s:R:r:u:f:C:J:K:nlLS::m:M:NEB", |
283 | usage5 (); | 277 | longopts, &option); |
284 | break; | 278 | if (c == -1 || c == EOF) { |
285 | case 'h': /* help */ | 279 | break; |
286 | print_help (); | 280 | } |
287 | exit (STATE_UNKNOWN); | 281 | |
288 | break; | 282 | switch (c) { |
289 | case 'V': /* version */ | 283 | case '?': /* usage */ |
290 | print_revision (progname, NP_VERSION); | 284 | usage5(); |
291 | exit (STATE_UNKNOWN); | 285 | break; |
292 | break; | 286 | case 'h': /* help */ |
293 | case 't': /* timeout period */ | 287 | print_help(); |
294 | if (!is_intnonneg (optarg)) | 288 | exit(STATE_UNKNOWN); |
295 | usage2 (_("Timeout interval must be a positive integer"), optarg); | 289 | break; |
296 | else | 290 | case 'V': /* version */ |
297 | socket_timeout = atoi (optarg); | 291 | print_revision(progname, NP_VERSION); |
298 | break; | 292 | exit(STATE_UNKNOWN); |
299 | case 'c': /* critical time threshold */ | 293 | break; |
300 | critical_thresholds = optarg; | 294 | case 't': /* timeout period */ |
301 | break; | 295 | if (!is_intnonneg(optarg)) { |
302 | case 'w': /* warning time threshold */ | 296 | usage2(_("Timeout interval must be a positive integer"), optarg); |
303 | warning_thresholds = optarg; | 297 | } else { |
304 | break; | 298 | socket_timeout = atoi(optarg); |
305 | case 'A': /* User Agent String */ | 299 | } |
306 | xasprintf (&user_agent, "User-Agent: %s", optarg); | 300 | break; |
307 | break; | 301 | case 'c': /* critical time threshold */ |
308 | case 'k': /* Additional headers */ | 302 | critical_thresholds = optarg; |
309 | if (http_opt_headers_count == 0) | 303 | break; |
310 | http_opt_headers = malloc (sizeof (char *) * (++http_opt_headers_count)); | 304 | case 'w': /* warning time threshold */ |
311 | else | 305 | warning_thresholds = optarg; |
312 | http_opt_headers = realloc (http_opt_headers, sizeof (char *) * (++http_opt_headers_count)); | 306 | break; |
313 | http_opt_headers[http_opt_headers_count - 1] = optarg; | 307 | case 'A': /* User Agent String */ |
314 | /* xasprintf (&http_opt_headers, "%s", optarg); */ | 308 | xasprintf(&user_agent, "User-Agent: %s", optarg); |
315 | break; | 309 | break; |
316 | case 'L': /* show html link */ | 310 | case 'k': /* Additional headers */ |
317 | display_html = true; | 311 | if (http_opt_headers_count == 0) { |
318 | break; | 312 | http_opt_headers = malloc(sizeof(char *) * (++http_opt_headers_count)); |
319 | case 'n': /* do not show html link */ | 313 | } else { |
320 | display_html = false; | 314 | http_opt_headers = |
321 | break; | 315 | realloc(http_opt_headers, sizeof(char *) * (++http_opt_headers_count)); |
322 | case 'C': /* Check SSL cert validity */ | 316 | } |
317 | http_opt_headers[http_opt_headers_count - 1] = optarg; | ||
318 | /* xasprintf (&http_opt_headers, "%s", optarg); */ | ||
319 | break; | ||
320 | case 'L': /* show html link */ | ||
321 | display_html = true; | ||
322 | break; | ||
323 | case 'n': /* do not show html link */ | ||
324 | display_html = false; | ||
325 | break; | ||
326 | case 'C': /* Check SSL cert validity */ | ||
323 | #ifdef HAVE_SSL | 327 | #ifdef HAVE_SSL |
324 | if ((temp=strchr(optarg,','))!=NULL) { | 328 | if ((temp = strchr(optarg, ',')) != NULL) { |
325 | *temp='\0'; | 329 | *temp = '\0'; |
326 | if (!is_intnonneg (optarg)) | 330 | if (!is_intnonneg(optarg)) { |
327 | usage2 (_("Invalid certificate expiration period"), optarg); | 331 | usage2(_("Invalid certificate expiration period"), optarg); |
328 | days_till_exp_warn = atoi(optarg); | 332 | } |
329 | *temp=','; | 333 | days_till_exp_warn = atoi(optarg); |
330 | temp++; | 334 | *temp = ','; |
331 | if (!is_intnonneg (temp)) | 335 | temp++; |
332 | usage2 (_("Invalid certificate expiration period"), temp); | 336 | if (!is_intnonneg(temp)) { |
333 | days_till_exp_crit = atoi (temp); | 337 | usage2(_("Invalid certificate expiration period"), temp); |
334 | } | 338 | } |
335 | else { | 339 | days_till_exp_crit = atoi(temp); |
336 | days_till_exp_crit=0; | 340 | } else { |
337 | if (!is_intnonneg (optarg)) | 341 | days_till_exp_crit = 0; |
338 | usage2 (_("Invalid certificate expiration period"), optarg); | 342 | if (!is_intnonneg(optarg)) { |
339 | days_till_exp_warn = atoi (optarg); | 343 | usage2(_("Invalid certificate expiration period"), optarg); |
340 | } | 344 | } |
341 | check_cert = true; | 345 | days_till_exp_warn = atoi(optarg); |
342 | goto enable_ssl; | 346 | } |
347 | check_cert = true; | ||
348 | goto enable_ssl; | ||
343 | #endif | 349 | #endif |
344 | case CONTINUE_AFTER_CHECK_CERT: /* don't stop after the certificate is checked */ | 350 | case CONTINUE_AFTER_CHECK_CERT: /* don't stop after the certificate is checked */ |
345 | #ifdef HAVE_SSL | 351 | #ifdef HAVE_SSL |
346 | continue_after_check_cert = true; | 352 | continue_after_check_cert = true; |
347 | break; | 353 | break; |
348 | #endif | 354 | #endif |
349 | case 'J': /* use client certificate */ | 355 | case 'J': /* use client certificate */ |
350 | #ifdef HAVE_SSL | 356 | #ifdef HAVE_SSL |
351 | test_file(optarg); | 357 | test_file(optarg); |
352 | client_cert = optarg; | 358 | client_cert = optarg; |
353 | goto enable_ssl; | 359 | goto enable_ssl; |
354 | #endif | 360 | #endif |
355 | case 'K': /* use client private key */ | 361 | case 'K': /* use client private key */ |
356 | #ifdef HAVE_SSL | 362 | #ifdef HAVE_SSL |
357 | test_file(optarg); | 363 | test_file(optarg); |
358 | client_privkey = optarg; | 364 | client_privkey = optarg; |
359 | goto enable_ssl; | 365 | goto enable_ssl; |
360 | #endif | 366 | #endif |
361 | case 'S': /* use SSL */ | 367 | case 'S': /* use SSL */ |
362 | #ifdef HAVE_SSL | 368 | #ifdef HAVE_SSL |
363 | enable_ssl: | 369 | enable_ssl: |
364 | /* ssl_version initialized to 0 as a default. Only set if it's non-zero. This helps when we include multiple | 370 | /* ssl_version initialized to 0 as a default. Only set if it's non-zero. This helps |
365 | parameters, like -S and -C combinations */ | 371 | when we include multiple parameters, like -S and -C combinations */ |
366 | use_ssl = true; | 372 | use_ssl = true; |
367 | if (c=='S' && optarg != NULL) { | 373 | if (c == 'S' && optarg != NULL) { |
368 | int got_plus = strchr(optarg, '+') != NULL; | 374 | int got_plus = strchr(optarg, '+') != NULL; |
369 | 375 | ||
370 | if (!strncmp (optarg, "1.2", 3)) | 376 | if (!strncmp(optarg, "1.2", 3)) { |
371 | ssl_version = got_plus ? MP_TLSv1_2_OR_NEWER : MP_TLSv1_2; | 377 | ssl_version = got_plus ? MP_TLSv1_2_OR_NEWER : MP_TLSv1_2; |
372 | else if (!strncmp (optarg, "1.1", 3)) | 378 | } else if (!strncmp(optarg, "1.1", 3)) { |
373 | ssl_version = got_plus ? MP_TLSv1_1_OR_NEWER : MP_TLSv1_1; | 379 | ssl_version = got_plus ? MP_TLSv1_1_OR_NEWER : MP_TLSv1_1; |
374 | else if (optarg[0] == '1') | 380 | } else if (optarg[0] == '1') { |
375 | ssl_version = got_plus ? MP_TLSv1_OR_NEWER : MP_TLSv1; | 381 | ssl_version = got_plus ? MP_TLSv1_OR_NEWER : MP_TLSv1; |
376 | else if (optarg[0] == '3') | 382 | } else if (optarg[0] == '3') { |
377 | ssl_version = got_plus ? MP_SSLv3_OR_NEWER : MP_SSLv3; | 383 | ssl_version = got_plus ? MP_SSLv3_OR_NEWER : MP_SSLv3; |
378 | else if (optarg[0] == '2') | 384 | } else if (optarg[0] == '2') { |
379 | ssl_version = got_plus ? MP_SSLv2_OR_NEWER : MP_SSLv2; | 385 | ssl_version = got_plus ? MP_SSLv2_OR_NEWER : MP_SSLv2; |
380 | else | 386 | } else { |
381 | usage4 (_("Invalid option - Valid SSL/TLS versions: 2, 3, 1, 1.1, 1.2 (with optional '+' suffix)")); | 387 | usage4(_("Invalid option - Valid SSL/TLS versions: 2, 3, 1, 1.1, 1.2 (with " |
382 | } | 388 | "optional '+' suffix)")); |
383 | if (specify_port == false) | 389 | } |
384 | server_port = HTTPS_PORT; | 390 | } |
391 | if (!specify_port) { | ||
392 | server_port = HTTPS_PORT; | ||
393 | } | ||
385 | #else | 394 | #else |
386 | /* -C -J and -K fall through to here without SSL */ | 395 | /* -C -J and -K fall through to here without SSL */ |
387 | usage4 (_("Invalid option - SSL is not available")); | 396 | usage4(_("Invalid option - SSL is not available")); |
388 | #endif | 397 | #endif |
389 | break; | 398 | break; |
390 | case SNI_OPTION: | 399 | case SNI_OPTION: |
391 | use_sni = true; | 400 | use_sni = true; |
392 | break; | 401 | break; |
393 | case MAX_REDIRS_OPTION: | 402 | case MAX_REDIRS_OPTION: |
394 | if (!is_intnonneg (optarg)) | 403 | if (!is_intnonneg(optarg)) { |
395 | usage2 (_("Invalid max_redirs count"), optarg); | 404 | usage2(_("Invalid max_redirs count"), optarg); |
396 | else { | 405 | } else { |
397 | max_depth = atoi (optarg); | 406 | max_depth = atoi(optarg); |
398 | } | 407 | } |
399 | break; | 408 | break; |
400 | case 'f': /* onredirect */ | 409 | case 'f': /* onredirect */ |
401 | if (!strcmp (optarg, "stickyport")) | 410 | if (!strcmp(optarg, "stickyport")) { |
402 | onredirect = STATE_DEPENDENT, followsticky = STICKY_HOST|STICKY_PORT; | 411 | onredirect = STATE_DEPENDENT, followsticky = STICKY_HOST | STICKY_PORT; |
403 | else if (!strcmp (optarg, "sticky")) | 412 | } else if (!strcmp(optarg, "sticky")) { |
404 | onredirect = STATE_DEPENDENT, followsticky = STICKY_HOST; | 413 | onredirect = STATE_DEPENDENT, followsticky = STICKY_HOST; |
405 | else if (!strcmp (optarg, "follow")) | 414 | } else if (!strcmp(optarg, "follow")) { |
406 | onredirect = STATE_DEPENDENT, followsticky = STICKY_NONE; | 415 | onredirect = STATE_DEPENDENT, followsticky = STICKY_NONE; |
407 | else if (!strcmp (optarg, "unknown")) | 416 | } else if (!strcmp(optarg, "unknown")) { |
408 | onredirect = STATE_UNKNOWN; | 417 | onredirect = STATE_UNKNOWN; |
409 | else if (!strcmp (optarg, "ok")) | 418 | } else if (!strcmp(optarg, "ok")) { |
410 | onredirect = STATE_OK; | 419 | onredirect = STATE_OK; |
411 | else if (!strcmp (optarg, "warning")) | 420 | } else if (!strcmp(optarg, "warning")) { |
412 | onredirect = STATE_WARNING; | 421 | onredirect = STATE_WARNING; |
413 | else if (!strcmp (optarg, "critical")) | 422 | } else if (!strcmp(optarg, "critical")) { |
414 | onredirect = STATE_CRITICAL; | 423 | onredirect = STATE_CRITICAL; |
415 | else usage2 (_("Invalid onredirect option"), optarg); | 424 | } else { |
416 | if (verbose) | 425 | usage2(_("Invalid onredirect option"), optarg); |
417 | printf(_("option f:%d \n"), onredirect); | 426 | } |
418 | break; | 427 | if (verbose) { |
419 | /* Note: H, I, and u must be malloc'd or will fail on redirects */ | 428 | printf(_("option f:%d \n"), onredirect); |
420 | case 'H': /* Host Name (virtual host) */ | 429 | } |
421 | host_name = strdup (optarg); | 430 | break; |
422 | if (host_name[0] == '[') { | 431 | /* Note: H, I, and u must be malloc'd or will fail on redirects */ |
423 | if ((p = strstr (host_name, "]:")) != NULL) { /* [IPv6]:port */ | 432 | case 'H': /* Host Name (virtual host) */ |
424 | virtual_port = atoi (p + 2); | 433 | host_name = strdup(optarg); |
425 | /* cut off the port */ | 434 | if (host_name[0] == '[') { |
426 | host_name_length = strlen (host_name) - strlen (p) - 1; | 435 | if ((p = strstr(host_name, "]:")) != NULL) { /* [IPv6]:port */ |
427 | free (host_name); | 436 | virtual_port = atoi(p + 2); |
428 | host_name = strndup (optarg, host_name_length); | 437 | /* cut off the port */ |
429 | if (specify_port == false) | 438 | host_name_length = strlen(host_name) - strlen(p) - 1; |
430 | server_port = virtual_port; | 439 | free(host_name); |
431 | } | 440 | host_name = strndup(optarg, host_name_length); |
432 | } else if ((p = strchr (host_name, ':')) != NULL | 441 | if (!specify_port) { |
433 | && strchr (++p, ':') == NULL) { /* IPv4:port or host:port */ | 442 | server_port = virtual_port; |
434 | virtual_port = atoi (p); | 443 | } |
435 | /* cut off the port */ | 444 | } |
436 | host_name_length = strlen (host_name) - strlen (p) - 1; | 445 | } else if ((p = strchr(host_name, ':')) != NULL && |
437 | free (host_name); | 446 | strchr(++p, ':') == NULL) { /* IPv4:port or host:port */ |
438 | host_name = strndup (optarg, host_name_length); | 447 | virtual_port = atoi(p); |
439 | if (specify_port == false) | 448 | /* cut off the port */ |
440 | server_port = virtual_port; | 449 | host_name_length = strlen(host_name) - strlen(p) - 1; |
441 | } | 450 | free(host_name); |
442 | break; | 451 | host_name = strndup(optarg, host_name_length); |
443 | case 'I': /* Server IP-address */ | 452 | if (!specify_port) { |
444 | server_address = strdup (optarg); | 453 | server_port = virtual_port; |
445 | break; | 454 | } |
446 | case 'u': /* URL path */ | 455 | } |
447 | server_url = strdup (optarg); | 456 | break; |
448 | server_url_length = strlen (server_url); | 457 | case 'I': /* Server IP-address */ |
449 | break; | 458 | server_address = strdup(optarg); |
450 | case 'p': /* Server port */ | 459 | break; |
451 | if (!is_intnonneg (optarg)) | 460 | case 'u': /* URL path */ |
452 | usage2 (_("Invalid port number"), optarg); | 461 | server_url = strdup(optarg); |
453 | else { | 462 | server_url_length = strlen(server_url); |
454 | server_port = atoi (optarg); | 463 | break; |
455 | specify_port = true; | 464 | case 'p': /* Server port */ |
456 | } | 465 | if (!is_intnonneg(optarg)) { |
457 | break; | 466 | usage2(_("Invalid port number"), optarg); |
458 | case 'a': /* authorization info */ | 467 | } else { |
459 | strncpy (user_auth, optarg, MAX_INPUT_BUFFER - 1); | 468 | server_port = atoi(optarg); |
460 | user_auth[MAX_INPUT_BUFFER - 1] = 0; | 469 | specify_port = true; |
461 | break; | 470 | } |
462 | case 'b': /* proxy-authorization info */ | 471 | break; |
463 | strncpy (proxy_auth, optarg, MAX_INPUT_BUFFER - 1); | 472 | case 'a': /* authorization info */ |
464 | proxy_auth[MAX_INPUT_BUFFER - 1] = 0; | 473 | strncpy(user_auth, optarg, MAX_INPUT_BUFFER - 1); |
465 | break; | 474 | user_auth[MAX_INPUT_BUFFER - 1] = 0; |
466 | case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */ | 475 | break; |
467 | if (! http_post_data) | 476 | case 'b': /* proxy-authorization info */ |
468 | http_post_data = strdup (optarg); | 477 | strncpy(proxy_auth, optarg, MAX_INPUT_BUFFER - 1); |
469 | if (! http_method) | 478 | proxy_auth[MAX_INPUT_BUFFER - 1] = 0; |
470 | http_method = strdup("POST"); | 479 | break; |
471 | break; | 480 | case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */ |
472 | case 'j': /* Set HTTP method */ | 481 | if (!http_post_data) { |
473 | if (http_method) | 482 | http_post_data = strdup(optarg); |
474 | free(http_method); | 483 | } |
475 | http_method = strdup (optarg); | 484 | if (!http_method) { |
476 | char *tmp; | 485 | http_method = strdup("POST"); |
477 | if ((tmp = strstr(http_method, ":")) != NULL) { | 486 | } |
478 | tmp[0] = '\0'; // set the ":" in the middle to 0 | 487 | break; |
479 | http_method_proxy = ++tmp; // this points to the second part | 488 | case 'j': /* Set HTTP method */ |
480 | } | 489 | if (http_method) { |
481 | break; | 490 | free(http_method); |
482 | case 'd': /* string or substring */ | 491 | } |
483 | strncpy (header_expect, optarg, MAX_INPUT_BUFFER - 1); | 492 | http_method = strdup(optarg); |
484 | header_expect[MAX_INPUT_BUFFER - 1] = 0; | 493 | char *tmp; |
485 | break; | 494 | if ((tmp = strstr(http_method, ":")) != NULL) { |
486 | case 's': /* string or substring */ | 495 | tmp[0] = '\0'; // set the ":" in the middle to 0 |
487 | strncpy (string_expect, optarg, MAX_INPUT_BUFFER - 1); | 496 | http_method_proxy = ++tmp; // this points to the second part |
488 | string_expect[MAX_INPUT_BUFFER - 1] = 0; | 497 | } |
489 | break; | 498 | break; |
490 | case 'e': /* string or substring */ | 499 | case 'd': /* string or substring */ |
491 | strncpy (server_expect, optarg, MAX_INPUT_BUFFER - 1); | 500 | strncpy(header_expect, optarg, MAX_INPUT_BUFFER - 1); |
492 | server_expect[MAX_INPUT_BUFFER - 1] = 0; | 501 | header_expect[MAX_INPUT_BUFFER - 1] = 0; |
493 | server_expect_yn = 1; | 502 | break; |
494 | break; | 503 | case 's': /* string or substring */ |
495 | case 'T': /* Content-type */ | 504 | strncpy(string_expect, optarg, MAX_INPUT_BUFFER - 1); |
496 | xasprintf (&http_content_type, "%s", optarg); | 505 | string_expect[MAX_INPUT_BUFFER - 1] = 0; |
497 | break; | 506 | break; |
498 | case 'l': /* linespan */ | 507 | case 'e': /* string or substring */ |
499 | cflags &= ~REG_NEWLINE; | 508 | strncpy(server_expect, optarg, MAX_INPUT_BUFFER - 1); |
500 | break; | 509 | server_expect[MAX_INPUT_BUFFER - 1] = 0; |
501 | case 'R': /* regex */ | 510 | server_expect_yn = 1; |
502 | cflags |= REG_ICASE; | 511 | break; |
512 | case 'T': /* Content-type */ | ||
513 | xasprintf(&http_content_type, "%s", optarg); | ||
514 | break; | ||
515 | case 'l': /* linespan */ | ||
516 | cflags &= ~REG_NEWLINE; | ||
517 | break; | ||
518 | case 'R': /* regex */ | ||
519 | cflags |= REG_ICASE; | ||
503 | // fall through | 520 | // fall through |
504 | case 'r': /* regex */ | 521 | case 'r': /* regex */ |
505 | strncpy (regexp, optarg, MAX_RE_SIZE - 1); | 522 | strncpy(regexp, optarg, MAX_RE_SIZE - 1); |
506 | regexp[MAX_RE_SIZE - 1] = 0; | 523 | regexp[MAX_RE_SIZE - 1] = 0; |
507 | errcode = regcomp (&preg, regexp, cflags); | 524 | errcode = regcomp(&preg, regexp, cflags); |
508 | if (errcode != 0) { | 525 | if (errcode != 0) { |
509 | (void) regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); | 526 | (void)regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); |
510 | printf (_("Could Not Compile Regular Expression: %s"), errbuf); | 527 | printf(_("Could Not Compile Regular Expression: %s"), errbuf); |
511 | return false; | 528 | return false; |
512 | } | 529 | } |
513 | break; | 530 | break; |
514 | case INVERT_REGEX: | 531 | case INVERT_REGEX: |
515 | invert_regex = 1; | 532 | invert_regex = 1; |
516 | break; | 533 | break; |
517 | case STATE_REGEX: | 534 | case STATE_REGEX: |
518 | if (!strcmp (optarg, "critical")) | 535 | if (!strcmp(optarg, "critical")) { |
519 | state_regex = STATE_CRITICAL; | 536 | state_regex = STATE_CRITICAL; |
520 | else if (!strcmp (optarg, "warning")) | 537 | } else if (!strcmp(optarg, "warning")) { |
521 | state_regex = STATE_WARNING; | 538 | state_regex = STATE_WARNING; |
522 | else usage2 (_("Invalid state-regex option"), optarg); | 539 | } else { |
523 | break; | 540 | usage2(_("Invalid state-regex option"), optarg); |
524 | case '4': | 541 | } |
525 | address_family = AF_INET; | 542 | break; |
526 | break; | 543 | case '4': |
527 | case '6': | 544 | address_family = AF_INET; |
545 | break; | ||
546 | case '6': | ||
528 | #ifdef USE_IPV6 | 547 | #ifdef USE_IPV6 |
529 | address_family = AF_INET6; | 548 | address_family = AF_INET6; |
530 | #else | 549 | #else |
531 | usage4 (_("IPv6 support not available")); | 550 | usage4(_("IPv6 support not available")); |
532 | #endif | 551 | #endif |
533 | break; | 552 | break; |
534 | case 'v': /* verbose */ | 553 | case 'v': /* verbose */ |
535 | verbose = true; | 554 | verbose = true; |
536 | break; | 555 | break; |
537 | case 'm': /* min_page_length */ | 556 | case 'm': /* min_page_length */ |
538 | { | 557 | { |
539 | char *tmp; | 558 | char *tmp; |
540 | if (strchr(optarg, ':') != (char *)NULL) { | 559 | if (strchr(optarg, ':') != (char *)NULL) { |
541 | /* range, so get two values, min:max */ | 560 | /* range, so get two values, min:max */ |
542 | tmp = strtok(optarg, ":"); | 561 | tmp = strtok(optarg, ":"); |
543 | if (tmp == NULL) { | 562 | if (tmp == NULL) { |
544 | printf("Bad format: try \"-m min:max\"\n"); | 563 | printf("Bad format: try \"-m min:max\"\n"); |
545 | exit (STATE_WARNING); | 564 | exit(STATE_WARNING); |
546 | } else | 565 | } else { |
547 | min_page_len = atoi(tmp); | 566 | min_page_len = atoi(tmp); |
548 | 567 | } | |
549 | tmp = strtok(NULL, ":"); | 568 | |
550 | if (tmp == NULL) { | 569 | tmp = strtok(NULL, ":"); |
551 | printf("Bad format: try \"-m min:max\"\n"); | 570 | if (tmp == NULL) { |
552 | exit (STATE_WARNING); | 571 | printf("Bad format: try \"-m min:max\"\n"); |
553 | } else | 572 | exit(STATE_WARNING); |
554 | max_page_len = atoi(tmp); | 573 | } else { |
555 | } else | 574 | max_page_len = atoi(tmp); |
556 | min_page_len = atoi (optarg); | 575 | } |
557 | break; | 576 | } else { |
558 | } | 577 | min_page_len = atoi(optarg); |
559 | case 'N': /* no-body */ | 578 | } |
560 | no_body = true; | 579 | break; |
561 | break; | 580 | } |
562 | case 'M': /* max-age */ | 581 | case 'N': /* no-body */ |
563 | { | 582 | no_body = true; |
564 | int L = strlen(optarg); | 583 | break; |
565 | if (L && optarg[L-1] == 'm') | 584 | case 'M': /* max-age */ |
566 | maximum_age = atoi (optarg) * 60; | 585 | { |
567 | else if (L && optarg[L-1] == 'h') | 586 | int L = strlen(optarg); |
568 | maximum_age = atoi (optarg) * 60 * 60; | 587 | if (L && optarg[L - 1] == 'm') { |
569 | else if (L && optarg[L-1] == 'd') | 588 | maximum_age = atoi(optarg) * 60; |
570 | maximum_age = atoi (optarg) * 60 * 60 * 24; | 589 | } else if (L && optarg[L - 1] == 'h') { |
571 | else if (L && (optarg[L-1] == 's' || | 590 | maximum_age = atoi(optarg) * 60 * 60; |
572 | isdigit (optarg[L-1]))) | 591 | } else if (L && optarg[L - 1] == 'd') { |
573 | maximum_age = atoi (optarg); | 592 | maximum_age = atoi(optarg) * 60 * 60 * 24; |
574 | else { | 593 | } else if (L && (optarg[L - 1] == 's' || isdigit(optarg[L - 1]))) { |
575 | fprintf (stderr, "unparsable max-age: %s\n", optarg); | 594 | maximum_age = atoi(optarg); |
576 | exit (STATE_WARNING); | 595 | } else { |
577 | } | 596 | fprintf(stderr, "unparsable max-age: %s\n", optarg); |
578 | } | 597 | exit(STATE_WARNING); |
579 | break; | 598 | } |
580 | case 'E': /* show extended perfdata */ | 599 | } break; |
581 | show_extended_perfdata = true; | 600 | case 'E': /* show extended perfdata */ |
582 | break; | 601 | show_extended_perfdata = true; |
583 | case 'B': /* print body content after status line */ | 602 | break; |
584 | show_body = true; | 603 | case 'B': /* print body content after status line */ |
585 | break; | 604 | show_body = true; |
586 | } | 605 | break; |
587 | } | 606 | } |
588 | 607 | } | |
589 | c = optind; | 608 | |
590 | 609 | c = optind; | |
591 | if (server_address == NULL && c < argc) | 610 | |
592 | server_address = strdup (argv[c++]); | 611 | if (server_address == NULL && c < argc) { |
593 | 612 | server_address = strdup(argv[c++]); | |
594 | if (host_name == NULL && c < argc) | 613 | } |
595 | host_name = strdup (argv[c++]); | ||
596 | |||
597 | if (server_address == NULL) { | ||
598 | if (host_name == NULL) | ||
599 | usage4 (_("You must specify a server address or host name")); | ||
600 | else | ||
601 | server_address = strdup (host_name); | ||
602 | } | ||
603 | |||
604 | set_thresholds(&thlds, warning_thresholds, critical_thresholds); | ||
605 | |||
606 | if (critical_thresholds && thlds->critical->end>(double)socket_timeout) | ||
607 | socket_timeout = (int)thlds->critical->end + 1; | ||
608 | |||
609 | if (http_method == NULL) | ||
610 | http_method = strdup ("GET"); | ||
611 | |||
612 | if (http_method_proxy == NULL) | ||
613 | http_method_proxy = strdup ("GET"); | ||
614 | |||
615 | if (client_cert && !client_privkey) | ||
616 | usage4 (_("If you use a client certificate you must also specify a private key file")); | ||
617 | |||
618 | if (virtual_port == 0) | ||
619 | virtual_port = server_port; | ||
620 | |||
621 | return true; | ||
622 | } | ||
623 | 614 | ||
615 | if (host_name == NULL && c < argc) { | ||
616 | host_name = strdup(argv[c++]); | ||
617 | } | ||
618 | |||
619 | if (server_address == NULL) { | ||
620 | if (host_name == NULL) { | ||
621 | usage4(_("You must specify a server address or host name")); | ||
622 | } else { | ||
623 | server_address = strdup(host_name); | ||
624 | } | ||
625 | } | ||
624 | 626 | ||
627 | set_thresholds(&thlds, warning_thresholds, critical_thresholds); | ||
628 | |||
629 | if (critical_thresholds && thlds->critical->end > (double)socket_timeout) { | ||
630 | socket_timeout = (int)thlds->critical->end + 1; | ||
631 | } | ||
632 | |||
633 | if (http_method == NULL) { | ||
634 | http_method = strdup("GET"); | ||
635 | } | ||
636 | |||
637 | if (http_method_proxy == NULL) { | ||
638 | http_method_proxy = strdup("GET"); | ||
639 | } | ||
640 | |||
641 | if (client_cert && !client_privkey) { | ||
642 | usage4(_("If you use a client certificate you must also specify a private key file")); | ||
643 | } | ||
644 | |||
645 | if (virtual_port == 0) { | ||
646 | virtual_port = server_port; | ||
647 | } | ||
648 | |||
649 | return true; | ||
650 | } | ||
625 | 651 | ||
626 | /* Returns 1 if we're done processing the document body; 0 to keep going */ | 652 | /* Returns 1 if we're done processing the document body; 0 to keep going */ |
627 | static int | 653 | static int document_headers_done(char *full_page) { |
628 | document_headers_done (char *full_page) | 654 | const char *body; |
629 | { | ||
630 | const char *body; | ||
631 | 655 | ||
632 | for (body = full_page; *body; body++) { | 656 | for (body = full_page; *body; body++) { |
633 | if (!strncmp (body, "\n\n", 2) || !strncmp (body, "\n\r\n", 3)) | 657 | if (!strncmp(body, "\n\n", 2) || !strncmp(body, "\n\r\n", 3)) { |
634 | break; | 658 | break; |
635 | } | 659 | } |
660 | } | ||
636 | 661 | ||
637 | if (!*body) | 662 | if (!*body) { |
638 | return 0; /* haven't read end of headers yet */ | 663 | return 0; /* haven't read end of headers yet */ |
664 | } | ||
639 | 665 | ||
640 | full_page[body - full_page] = 0; | 666 | full_page[body - full_page] = 0; |
641 | return 1; | 667 | return 1; |
642 | } | 668 | } |
643 | 669 | ||
644 | static time_t | 670 | static time_t parse_time_string(const char *string) { |
645 | parse_time_string (const char *string) | 671 | struct tm tm; |
646 | { | 672 | time_t t; |
647 | struct tm tm; | 673 | memset(&tm, 0, sizeof(tm)); |
648 | time_t t; | 674 | |
649 | memset (&tm, 0, sizeof(tm)); | 675 | /* Like this: Tue, 25 Dec 2001 02:59:03 GMT */ |
650 | 676 | ||
651 | /* Like this: Tue, 25 Dec 2001 02:59:03 GMT */ | 677 | if (isupper(string[0]) && /* Tue */ |
652 | 678 | islower(string[1]) && islower(string[2]) && ',' == string[3] && ' ' == string[4] && | |
653 | if (isupper (string[0]) && /* Tue */ | 679 | (isdigit(string[5]) || string[5] == ' ') && /* 25 */ |
654 | islower (string[1]) && | 680 | isdigit(string[6]) && ' ' == string[7] && isupper(string[8]) && /* Dec */ |
655 | islower (string[2]) && | 681 | islower(string[9]) && islower(string[10]) && ' ' == string[11] && |
656 | ',' == string[3] && | 682 | isdigit(string[12]) && /* 2001 */ |
657 | ' ' == string[4] && | 683 | isdigit(string[13]) && isdigit(string[14]) && isdigit(string[15]) && ' ' == string[16] && |
658 | (isdigit(string[5]) || string[5] == ' ') && /* 25 */ | 684 | isdigit(string[17]) && /* 02: */ |
659 | isdigit (string[6]) && | 685 | isdigit(string[18]) && ':' == string[19] && isdigit(string[20]) && /* 59: */ |
660 | ' ' == string[7] && | 686 | isdigit(string[21]) && ':' == string[22] && isdigit(string[23]) && /* 03 */ |
661 | isupper (string[8]) && /* Dec */ | 687 | isdigit(string[24]) && ' ' == string[25] && 'G' == string[26] && /* GMT */ |
662 | islower (string[9]) && | 688 | 'M' == string[27] && /* GMT */ |
663 | islower (string[10]) && | 689 | 'T' == string[28]) { |
664 | ' ' == string[11] && | 690 | |
665 | isdigit (string[12]) && /* 2001 */ | 691 | tm.tm_sec = 10 * (string[23] - '0') + (string[24] - '0'); |
666 | isdigit (string[13]) && | 692 | tm.tm_min = 10 * (string[20] - '0') + (string[21] - '0'); |
667 | isdigit (string[14]) && | 693 | tm.tm_hour = 10 * (string[17] - '0') + (string[18] - '0'); |
668 | isdigit (string[15]) && | 694 | tm.tm_mday = 10 * (string[5] == ' ' ? 0 : string[5] - '0') + (string[6] - '0'); |
669 | ' ' == string[16] && | 695 | tm.tm_mon = (!strncmp(string + 8, "Jan", 3) ? 0 |
670 | isdigit (string[17]) && /* 02: */ | 696 | : !strncmp(string + 8, "Feb", 3) ? 1 |
671 | isdigit (string[18]) && | 697 | : !strncmp(string + 8, "Mar", 3) ? 2 |
672 | ':' == string[19] && | 698 | : !strncmp(string + 8, "Apr", 3) ? 3 |
673 | isdigit (string[20]) && /* 59: */ | 699 | : !strncmp(string + 8, "May", 3) ? 4 |
674 | isdigit (string[21]) && | 700 | : !strncmp(string + 8, "Jun", 3) ? 5 |
675 | ':' == string[22] && | 701 | : !strncmp(string + 8, "Jul", 3) ? 6 |
676 | isdigit (string[23]) && /* 03 */ | 702 | : !strncmp(string + 8, "Aug", 3) ? 7 |
677 | isdigit (string[24]) && | 703 | : !strncmp(string + 8, "Sep", 3) ? 8 |
678 | ' ' == string[25] && | 704 | : !strncmp(string + 8, "Oct", 3) ? 9 |
679 | 'G' == string[26] && /* GMT */ | 705 | : !strncmp(string + 8, "Nov", 3) ? 10 |
680 | 'M' == string[27] && /* GMT */ | 706 | : !strncmp(string + 8, "Dec", 3) ? 11 |
681 | 'T' == string[28]) { | 707 | : -1); |
682 | 708 | tm.tm_year = ((1000 * (string[12] - '0') + 100 * (string[13] - '0') + | |
683 | tm.tm_sec = 10 * (string[23]-'0') + (string[24]-'0'); | 709 | 10 * (string[14] - '0') + (string[15] - '0')) - |
684 | tm.tm_min = 10 * (string[20]-'0') + (string[21]-'0'); | 710 | 1900); |
685 | tm.tm_hour = 10 * (string[17]-'0') + (string[18]-'0'); | 711 | |
686 | tm.tm_mday = 10 * (string[5] == ' ' ? 0 : string[5]-'0') + (string[6]-'0'); | 712 | tm.tm_isdst = 0; /* GMT is never in DST, right? */ |
687 | tm.tm_mon = (!strncmp (string+8, "Jan", 3) ? 0 : | 713 | |
688 | !strncmp (string+8, "Feb", 3) ? 1 : | 714 | if (tm.tm_mon < 0 || tm.tm_mday < 1 || tm.tm_mday > 31) { |
689 | !strncmp (string+8, "Mar", 3) ? 2 : | 715 | return 0; |
690 | !strncmp (string+8, "Apr", 3) ? 3 : | 716 | } |
691 | !strncmp (string+8, "May", 3) ? 4 : | 717 | |
692 | !strncmp (string+8, "Jun", 3) ? 5 : | 718 | /* |
693 | !strncmp (string+8, "Jul", 3) ? 6 : | 719 | This is actually wrong: we need to subtract the local timezone |
694 | !strncmp (string+8, "Aug", 3) ? 7 : | 720 | offset from GMT from this value. But, that's ok in this usage, |
695 | !strncmp (string+8, "Sep", 3) ? 8 : | 721 | because we only comparing these two GMT dates against each other, |
696 | !strncmp (string+8, "Oct", 3) ? 9 : | 722 | so it doesn't matter what time zone we parse them in. |
697 | !strncmp (string+8, "Nov", 3) ? 10 : | 723 | */ |
698 | !strncmp (string+8, "Dec", 3) ? 11 : | 724 | |
699 | -1); | 725 | t = mktime(&tm); |
700 | tm.tm_year = ((1000 * (string[12]-'0') + | 726 | if (t == (time_t)-1) { |
701 | 100 * (string[13]-'0') + | 727 | t = 0; |
702 | 10 * (string[14]-'0') + | 728 | } |
703 | (string[15]-'0')) | 729 | |
704 | - 1900); | 730 | if (verbose) { |
705 | 731 | const char *s = string; | |
706 | tm.tm_isdst = 0; /* GMT is never in DST, right? */ | 732 | while (*s && *s != '\r' && *s != '\n') { |
707 | 733 | fputc(*s++, stdout); | |
708 | if (tm.tm_mon < 0 || tm.tm_mday < 1 || tm.tm_mday > 31) | 734 | } |
709 | return 0; | 735 | printf(" ==> %lu\n", (unsigned long)t); |
710 | 736 | } | |
711 | /* | 737 | |
712 | This is actually wrong: we need to subtract the local timezone | 738 | return t; |
713 | offset from GMT from this value. But, that's ok in this usage, | 739 | } |
714 | because we only comparing these two GMT dates against each other, | 740 | return 0; |
715 | so it doesn't matter what time zone we parse them in. | ||
716 | */ | ||
717 | |||
718 | t = mktime (&tm); | ||
719 | if (t == (time_t) -1) t = 0; | ||
720 | |||
721 | if (verbose) { | ||
722 | const char *s = string; | ||
723 | while (*s && *s != '\r' && *s != '\n') | ||
724 | fputc (*s++, stdout); | ||
725 | printf (" ==> %lu\n", (unsigned long) t); | ||
726 | } | ||
727 | |||
728 | return t; | ||
729 | |||
730 | } else { | ||
731 | return 0; | ||
732 | } | ||
733 | } | 741 | } |
734 | 742 | ||
735 | /* Checks if the server 'reply' is one of the expected 'statuscodes' */ | 743 | /* Checks if the server 'reply' is one of the expected 'statuscodes' */ |
736 | static int | 744 | static int expected_statuscode(const char *reply, const char *statuscodes) { |
737 | expected_statuscode (const char *reply, const char *statuscodes) | 745 | char *expected; |
738 | { | 746 | char *code; |
739 | char *expected, *code; | 747 | int result = 0; |
740 | int result = 0; | 748 | |
741 | 749 | if ((expected = strdup(statuscodes)) == NULL) { | |
742 | if ((expected = strdup (statuscodes)) == NULL) | 750 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); |
743 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); | 751 | } |
744 | 752 | ||
745 | for (code = strtok (expected, ","); code != NULL; code = strtok (NULL, ",")) | 753 | for (code = strtok(expected, ","); code != NULL; code = strtok(NULL, ",")) { |
746 | if (strstr (reply, code) != NULL) { | 754 | if (strstr(reply, code) != NULL) { |
747 | result = 1; | 755 | result = 1; |
748 | break; | 756 | break; |
749 | } | 757 | } |
750 | 758 | } | |
751 | free (expected); | 759 | |
752 | return result; | 760 | free(expected); |
761 | return result; | ||
753 | } | 762 | } |
754 | 763 | ||
755 | static int | 764 | static int check_document_dates(const char *headers, char **msg) { |
756 | check_document_dates (const char *headers, char **msg) | 765 | const char *s; |
757 | { | 766 | char *server_date = 0; |
758 | const char *s; | 767 | char *document_date = 0; |
759 | char *server_date = 0; | 768 | int date_result = STATE_OK; |
760 | char *document_date = 0; | 769 | |
761 | int date_result = STATE_OK; | 770 | s = headers; |
762 | 771 | while (*s) { | |
763 | s = headers; | 772 | const char *field = s; |
764 | while (*s) { | 773 | const char *value = 0; |
765 | const char *field = s; | 774 | |
766 | const char *value = 0; | 775 | /* Find the end of the header field */ |
767 | 776 | while (*s && !isspace(*s) && *s != ':') { | |
768 | /* Find the end of the header field */ | 777 | s++; |
769 | while (*s && !isspace(*s) && *s != ':') | 778 | } |
770 | s++; | 779 | |
771 | 780 | /* Remember the header value, if any. */ | |
772 | /* Remember the header value, if any. */ | 781 | if (*s == ':') { |
773 | if (*s == ':') | 782 | value = ++s; |
774 | value = ++s; | 783 | } |
775 | 784 | ||
776 | /* Skip to the end of the header, including continuation lines. */ | 785 | /* Skip to the end of the header, including continuation lines. */ |
777 | while (*s && !(*s == '\n' && (s[1] != ' ' && s[1] != '\t'))) | 786 | while (*s && !(*s == '\n' && (s[1] != ' ' && s[1] != '\t'))) { |
778 | s++; | 787 | s++; |
779 | 788 | } | |
780 | /* Avoid stepping over end-of-string marker */ | 789 | |
781 | if (*s) | 790 | /* Avoid stepping over end-of-string marker */ |
782 | s++; | 791 | if (*s) { |
783 | 792 | s++; | |
784 | /* Process this header. */ | 793 | } |
785 | if (value && value > field+2) { | 794 | |
786 | char *ff = (char *) malloc (value-field); | 795 | /* Process this header. */ |
787 | char *ss = ff; | 796 | if (value && value > field + 2) { |
788 | while (field < value-1) | 797 | char *ff = (char *)malloc(value - field); |
789 | *ss++ = tolower(*field++); | 798 | char *ss = ff; |
790 | *ss++ = 0; | 799 | while (field < value - 1) { |
791 | 800 | *ss++ = tolower(*field++); | |
792 | if (!strcmp (ff, "date") || !strcmp (ff, "last-modified")) { | 801 | } |
793 | const char *e; | 802 | *ss++ = 0; |
794 | while (*value && isspace (*value)) | 803 | |
795 | value++; | 804 | if (!strcmp(ff, "date") || !strcmp(ff, "last-modified")) { |
796 | for (e = value; *e && *e != '\r' && *e != '\n'; e++) | 805 | const char *e; |
797 | ; | 806 | while (*value && isspace(*value)) { |
798 | ss = (char *) malloc (e - value + 1); | 807 | value++; |
799 | strncpy (ss, value, e - value); | 808 | } |
800 | ss[e - value] = 0; | 809 | for (e = value; *e && *e != '\r' && *e != '\n'; e++) { |
801 | if (!strcmp (ff, "date")) { | 810 | ; |
802 | if (server_date) free (server_date); | 811 | } |
803 | server_date = ss; | 812 | ss = (char *)malloc(e - value + 1); |
804 | } else { | 813 | strncpy(ss, value, e - value); |
805 | if (document_date) free (document_date); | 814 | ss[e - value] = 0; |
806 | document_date = ss; | 815 | if (!strcmp(ff, "date")) { |
807 | } | 816 | if (server_date) { |
808 | } | 817 | free(server_date); |
809 | free (ff); | 818 | } |
810 | } | 819 | server_date = ss; |
811 | } | 820 | } else { |
812 | 821 | if (document_date) { | |
813 | /* Done parsing the body. Now check the dates we (hopefully) parsed. */ | 822 | free(document_date); |
814 | if (!server_date || !*server_date) { | 823 | } |
815 | xasprintf (msg, _("%sServer date unknown, "), *msg); | 824 | document_date = ss; |
816 | date_result = max_state_alt(STATE_UNKNOWN, date_result); | 825 | } |
817 | } else if (!document_date || !*document_date) { | 826 | } |
818 | xasprintf (msg, _("%sDocument modification date unknown, "), *msg); | 827 | free(ff); |
819 | date_result = max_state_alt(STATE_CRITICAL, date_result); | 828 | } |
820 | } else { | 829 | } |
821 | time_t srv_data = parse_time_string (server_date); | 830 | |
822 | time_t doc_data = parse_time_string (document_date); | 831 | /* Done parsing the body. Now check the dates we (hopefully) parsed. */ |
823 | 832 | if (!server_date || !*server_date) { | |
824 | if (srv_data <= 0) { | 833 | xasprintf(msg, _("%sServer date unknown, "), *msg); |
825 | xasprintf (msg, _("%sServer date \"%100s\" unparsable, "), *msg, server_date); | 834 | date_result = max_state_alt(STATE_UNKNOWN, date_result); |
826 | date_result = max_state_alt(STATE_CRITICAL, date_result); | 835 | } else if (!document_date || !*document_date) { |
827 | } else if (doc_data <= 0) { | 836 | xasprintf(msg, _("%sDocument modification date unknown, "), *msg); |
828 | xasprintf (msg, _("%sDocument date \"%100s\" unparsable, "), *msg, document_date); | 837 | date_result = max_state_alt(STATE_CRITICAL, date_result); |
829 | date_result = max_state_alt(STATE_CRITICAL, date_result); | 838 | } else { |
830 | } else if (doc_data > srv_data + 30) { | 839 | time_t srv_data = parse_time_string(server_date); |
831 | xasprintf (msg, _("%sDocument is %d seconds in the future, "), *msg, (int)doc_data - (int)srv_data); | 840 | time_t doc_data = parse_time_string(document_date); |
832 | date_result = max_state_alt(STATE_CRITICAL, date_result); | 841 | |
833 | } else if (doc_data < srv_data - maximum_age) { | 842 | if (srv_data <= 0) { |
834 | int n = (srv_data - doc_data); | 843 | xasprintf(msg, _("%sServer date \"%100s\" unparsable, "), *msg, server_date); |
835 | if (n > (60 * 60 * 24 * 2)) { | 844 | date_result = max_state_alt(STATE_CRITICAL, date_result); |
836 | xasprintf (msg, _("%sLast modified %.1f days ago, "), *msg, ((float) n) / (60 * 60 * 24)); | 845 | } else if (doc_data <= 0) { |
837 | date_result = max_state_alt(STATE_CRITICAL, date_result); | 846 | xasprintf(msg, _("%sDocument date \"%100s\" unparsable, "), *msg, document_date); |
838 | } else { | 847 | date_result = max_state_alt(STATE_CRITICAL, date_result); |
839 | xasprintf (msg, _("%sLast modified %d:%02d:%02d ago, "), *msg, n / (60 * 60), (n / 60) % 60, n % 60); | 848 | } else if (doc_data > srv_data + 30) { |
840 | date_result = max_state_alt(STATE_CRITICAL, date_result); | 849 | xasprintf(msg, _("%sDocument is %d seconds in the future, "), *msg, |
841 | } | 850 | (int)doc_data - (int)srv_data); |
842 | } | 851 | date_result = max_state_alt(STATE_CRITICAL, date_result); |
843 | free (server_date); | 852 | } else if (doc_data < srv_data - maximum_age) { |
844 | free (document_date); | 853 | int n = (srv_data - doc_data); |
845 | } | 854 | if (n > (60 * 60 * 24 * 2)) { |
846 | return date_result; | 855 | xasprintf(msg, _("%sLast modified %.1f days ago, "), *msg, |
856 | ((float)n) / (60 * 60 * 24)); | ||
857 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
858 | } else { | ||
859 | xasprintf(msg, _("%sLast modified %d:%02d:%02d ago, "), *msg, n / (60 * 60), | ||
860 | (n / 60) % 60, n % 60); | ||
861 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
862 | } | ||
863 | } | ||
864 | free(server_date); | ||
865 | free(document_date); | ||
866 | } | ||
867 | return date_result; | ||
847 | } | 868 | } |
848 | 869 | ||
849 | int | 870 | int get_content_length(const char *headers) { |
850 | get_content_length (const char *headers) | 871 | const char *s; |
851 | { | 872 | int content_length = 0; |
852 | const char *s; | 873 | |
853 | int content_length = 0; | 874 | s = headers; |
854 | 875 | while (*s) { | |
855 | s = headers; | 876 | const char *field = s; |
856 | while (*s) { | 877 | const char *value = 0; |
857 | const char *field = s; | 878 | |
858 | const char *value = 0; | 879 | /* Find the end of the header field */ |
859 | 880 | while (*s && !isspace(*s) && *s != ':') { | |
860 | /* Find the end of the header field */ | 881 | s++; |
861 | while (*s && !isspace(*s) && *s != ':') | 882 | } |
862 | s++; | 883 | |
863 | 884 | /* Remember the header value, if any. */ | |
864 | /* Remember the header value, if any. */ | 885 | if (*s == ':') { |
865 | if (*s == ':') | 886 | value = ++s; |
866 | value = ++s; | 887 | } |
867 | 888 | ||
868 | /* Skip to the end of the header, including continuation lines. */ | 889 | /* Skip to the end of the header, including continuation lines. */ |
869 | while (*s && !(*s == '\n' && (s[1] != ' ' && s[1] != '\t'))) | 890 | while (*s && !(*s == '\n' && (s[1] != ' ' && s[1] != '\t'))) { |
870 | s++; | 891 | s++; |
871 | 892 | } | |
872 | /* Avoid stepping over end-of-string marker */ | 893 | |
873 | if (*s) | 894 | /* Avoid stepping over end-of-string marker */ |
874 | s++; | 895 | if (*s) { |
875 | 896 | s++; | |
876 | /* Process this header. */ | 897 | } |
877 | if (value && value > field+2) { | 898 | |
878 | char *ff = (char *) malloc (value-field); | 899 | /* Process this header. */ |
879 | char *ss = ff; | 900 | if (value && value > field + 2) { |
880 | while (field < value-1) | 901 | char *ff = (char *)malloc(value - field); |
881 | *ss++ = tolower(*field++); | 902 | char *ss = ff; |
882 | *ss++ = 0; | 903 | while (field < value - 1) { |
883 | 904 | *ss++ = tolower(*field++); | |
884 | if (!strcmp (ff, "content-length")) { | 905 | } |
885 | const char *e; | 906 | *ss++ = 0; |
886 | while (*value && isspace (*value)) | 907 | |
887 | value++; | 908 | if (!strcmp(ff, "content-length")) { |
888 | for (e = value; *e && *e != '\r' && *e != '\n'; e++) | 909 | const char *e; |
889 | ; | 910 | while (*value && isspace(*value)) { |
890 | ss = (char *) malloc (e - value + 1); | 911 | value++; |
891 | strncpy (ss, value, e - value); | 912 | } |
892 | ss[e - value] = 0; | 913 | for (e = value; *e && *e != '\r' && *e != '\n'; e++) { |
893 | content_length = atoi(ss); | 914 | ; |
894 | free (ss); | 915 | } |
895 | } | 916 | ss = (char *)malloc(e - value + 1); |
896 | free (ff); | 917 | strncpy(ss, value, e - value); |
897 | } | 918 | ss[e - value] = 0; |
898 | } | 919 | content_length = atoi(ss); |
899 | return (content_length); | 920 | free(ss); |
921 | } | ||
922 | free(ff); | ||
923 | } | ||
924 | } | ||
925 | return (content_length); | ||
900 | } | 926 | } |
901 | 927 | ||
902 | char * | 928 | char *prepend_slash(char *path) { |
903 | prepend_slash (char *path) | 929 | char *newpath; |
904 | { | ||
905 | char *newpath; | ||
906 | 930 | ||
907 | if (path[0] == '/') | 931 | if (path[0] == '/') { |
908 | return path; | 932 | return path; |
933 | } | ||
909 | 934 | ||
910 | if ((newpath = malloc (strlen(path) + 2)) == NULL) | 935 | if ((newpath = malloc(strlen(path) + 2)) == NULL) { |
911 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); | 936 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); |
912 | newpath[0] = '/'; | 937 | } |
913 | strcpy (newpath + 1, path); | 938 | newpath[0] = '/'; |
914 | free (path); | 939 | strcpy(newpath + 1, path); |
915 | return newpath; | 940 | free(path); |
941 | return newpath; | ||
916 | } | 942 | } |
917 | 943 | ||
918 | int | 944 | int check_http(void) { |
919 | check_http (void) | 945 | char *msg; |
920 | { | 946 | char *status_line; |
921 | char *msg; | 947 | char *status_code; |
922 | char *status_line; | 948 | char *header; |
923 | char *status_code; | 949 | char *page; |
924 | char *header; | 950 | char *auth; |
925 | char *page; | 951 | int http_status; |
926 | char *auth; | 952 | int i = 0; |
927 | int http_status; | 953 | size_t pagesize = 0; |
928 | int i = 0; | 954 | char *full_page; |
929 | size_t pagesize = 0; | 955 | char *full_page_new; |
930 | char *full_page; | 956 | char *buf; |
931 | char *full_page_new; | 957 | char *pos; |
932 | char *buf; | 958 | long microsec = 0L; |
933 | char *pos; | 959 | double elapsed_time = 0.0; |
934 | long microsec = 0L; | 960 | long microsec_connect = 0L; |
935 | double elapsed_time = 0.0; | 961 | double elapsed_time_connect = 0.0; |
936 | long microsec_connect = 0L; | 962 | long microsec_ssl = 0L; |
937 | double elapsed_time_connect = 0.0; | 963 | double elapsed_time_ssl = 0.0; |
938 | long microsec_ssl = 0L; | 964 | long microsec_firstbyte = 0L; |
939 | double elapsed_time_ssl = 0.0; | 965 | double elapsed_time_firstbyte = 0.0; |
940 | long microsec_firstbyte = 0L; | 966 | long microsec_headers = 0L; |
941 | double elapsed_time_firstbyte = 0.0; | 967 | double elapsed_time_headers = 0.0; |
942 | long microsec_headers = 0L; | 968 | long microsec_transfer = 0L; |
943 | double elapsed_time_headers = 0.0; | 969 | double elapsed_time_transfer = 0.0; |
944 | long microsec_transfer = 0L; | 970 | int page_len = 0; |
945 | double elapsed_time_transfer = 0.0; | 971 | int result = STATE_OK; |
946 | int page_len = 0; | 972 | char *force_host_header = NULL; |
947 | int result = STATE_OK; | 973 | |
948 | char *force_host_header = NULL; | 974 | /* try to connect to the host at the given port number */ |
949 | 975 | gettimeofday(&tv_temp, NULL); | |
950 | /* try to connect to the host at the given port number */ | 976 | if (my_tcp_connect(server_address, server_port, &sd) != STATE_OK) { |
951 | gettimeofday (&tv_temp, NULL); | 977 | die(STATE_CRITICAL, _("HTTP CRITICAL - Unable to open TCP socket\n")); |
952 | if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) | 978 | } |
953 | die (STATE_CRITICAL, _("HTTP CRITICAL - Unable to open TCP socket\n")); | 979 | microsec_connect = deltime(tv_temp); |
954 | microsec_connect = deltime (tv_temp); | 980 | |
955 | 981 | /* if we are called with the -I option, the -j method is CONNECT and */ | |
956 | /* if we are called with the -I option, the -j method is CONNECT and */ | 982 | /* we received -S for SSL, then we tunnel the request through a proxy*/ |
957 | /* we received -S for SSL, then we tunnel the request through a proxy*/ | 983 | /* @20100414, public[at]frank4dd.com, http://www.frank4dd.com/howto */ |
958 | /* @20100414, public[at]frank4dd.com, http://www.frank4dd.com/howto */ | 984 | |
959 | 985 | if (server_address != NULL && strcmp(http_method, "CONNECT") == 0 && host_name != NULL && | |
960 | if ( server_address != NULL && strcmp(http_method, "CONNECT") == 0 | 986 | use_ssl) { |
961 | && host_name != NULL && use_ssl == true) { | 987 | |
962 | 988 | if (verbose) { | |
963 | if (verbose) printf ("Entering CONNECT tunnel mode with proxy %s:%d to dst %s:%d\n", server_address, server_port, host_name, HTTPS_PORT); | 989 | printf("Entering CONNECT tunnel mode with proxy %s:%d to dst %s:%d\n", server_address, |
964 | asprintf (&buf, "%s %s:%d HTTP/1.1\r\n%s\r\n", http_method, host_name, HTTPS_PORT, user_agent); | 990 | server_port, host_name, HTTPS_PORT); |
965 | if (strlen(proxy_auth)) { | 991 | } |
966 | base64_encode_alloc (proxy_auth, strlen (proxy_auth), &auth); | 992 | asprintf(&buf, "%s %s:%d HTTP/1.1\r\n%s\r\n", http_method, host_name, HTTPS_PORT, |
967 | xasprintf (&buf, "%sProxy-Authorization: Basic %s\r\n", buf, auth); | 993 | user_agent); |
968 | } | 994 | if (strlen(proxy_auth)) { |
969 | /* optionally send any other header tag */ | 995 | base64_encode_alloc(proxy_auth, strlen(proxy_auth), &auth); |
970 | if (http_opt_headers_count) { | 996 | xasprintf(&buf, "%sProxy-Authorization: Basic %s\r\n", buf, auth); |
971 | for (i = 0; i < http_opt_headers_count ; i++) { | 997 | } |
972 | if (force_host_header != http_opt_headers[i]) { | 998 | /* optionally send any other header tag */ |
973 | xasprintf (&buf, "%s%s\r\n", buf, http_opt_headers[i]); | 999 | if (http_opt_headers_count) { |
974 | } | 1000 | for (i = 0; i < http_opt_headers_count; i++) { |
975 | } | 1001 | if (force_host_header != http_opt_headers[i]) { |
976 | /* This cannot be free'd here because a redirection will then try to access this and segfault */ | 1002 | xasprintf(&buf, "%s%s\r\n", buf, http_opt_headers[i]); |
977 | /* Covered in a testcase in tests/check_http.t */ | 1003 | } |
978 | /* free(http_opt_headers); */ | 1004 | } |
979 | } | 1005 | /* This cannot be free'd here because a redirection will then try to access this and |
980 | asprintf (&buf, "%sProxy-Connection: keep-alive\r\n", buf); | 1006 | * segfault */ |
981 | asprintf (&buf, "%sHost: %s\r\n", buf, host_name); | 1007 | /* Covered in a testcase in tests/check_http.t */ |
982 | /* we finished our request, send empty line with CRLF */ | 1008 | /* free(http_opt_headers); */ |
983 | asprintf (&buf, "%s%s", buf, CRLF); | 1009 | } |
984 | if (verbose) printf ("%s\n", buf); | 1010 | asprintf(&buf, "%sProxy-Connection: keep-alive\r\n", buf); |
985 | send(sd, buf, strlen (buf), 0); | 1011 | asprintf(&buf, "%sHost: %s\r\n", buf, host_name); |
986 | buf[0]='\0'; | 1012 | /* we finished our request, send empty line with CRLF */ |
987 | 1013 | asprintf(&buf, "%s%s", buf, CRLF); | |
988 | if (verbose) printf ("Receive response from proxy\n"); | 1014 | if (verbose) { |
989 | read (sd, buffer, MAX_INPUT_BUFFER-1); | 1015 | printf("%s\n", buf); |
990 | if (verbose) printf ("%s", buffer); | 1016 | } |
991 | /* Here we should check if we got HTTP/1.1 200 Connection established */ | 1017 | send(sd, buf, strlen(buf), 0); |
992 | } | 1018 | buf[0] = '\0'; |
1019 | |||
1020 | if (verbose) { | ||
1021 | printf("Receive response from proxy\n"); | ||
1022 | } | ||
1023 | read(sd, buffer, MAX_INPUT_BUFFER - 1); | ||
1024 | if (verbose) { | ||
1025 | printf("%s", buffer); | ||
1026 | } | ||
1027 | /* Here we should check if we got HTTP/1.1 200 Connection established */ | ||
1028 | } | ||
993 | #ifdef HAVE_SSL | 1029 | #ifdef HAVE_SSL |
994 | elapsed_time_connect = (double)microsec_connect / 1.0e6; | 1030 | elapsed_time_connect = (double)microsec_connect / 1.0e6; |
995 | if (use_ssl == true) { | 1031 | if (use_ssl) { |
996 | gettimeofday (&tv_temp, NULL); | 1032 | gettimeofday(&tv_temp, NULL); |
997 | result = np_net_ssl_init_with_hostname_version_and_cert(sd, (use_sni ? host_name : NULL), ssl_version, client_cert, client_privkey); | 1033 | result = np_net_ssl_init_with_hostname_version_and_cert( |
998 | if (verbose) printf ("SSL initialized\n"); | 1034 | sd, (use_sni ? host_name : NULL), ssl_version, client_cert, client_privkey); |
999 | if (result != STATE_OK) | 1035 | if (verbose) { |
1000 | die (STATE_CRITICAL, NULL); | 1036 | printf("SSL initialized\n"); |
1001 | microsec_ssl = deltime (tv_temp); | 1037 | } |
1002 | elapsed_time_ssl = (double)microsec_ssl / 1.0e6; | 1038 | if (result != STATE_OK) { |
1003 | if (check_cert == true) { | 1039 | die(STATE_CRITICAL, NULL); |
1004 | result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); | 1040 | } |
1005 | if (continue_after_check_cert == false) { | 1041 | microsec_ssl = deltime(tv_temp); |
1006 | if (sd) close(sd); | 1042 | elapsed_time_ssl = (double)microsec_ssl / 1.0e6; |
1007 | np_net_ssl_cleanup(); | 1043 | if (check_cert) { |
1008 | return result; | 1044 | result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); |
1009 | } | 1045 | if (!continue_after_check_cert) { |
1010 | } | 1046 | if (sd) { |
1011 | } | 1047 | close(sd); |
1048 | } | ||
1049 | np_net_ssl_cleanup(); | ||
1050 | return result; | ||
1051 | } | ||
1052 | } | ||
1053 | } | ||
1012 | #endif /* HAVE_SSL */ | 1054 | #endif /* HAVE_SSL */ |
1013 | 1055 | ||
1014 | if ( server_address != NULL && strcmp(http_method, "CONNECT") == 0 | 1056 | if (server_address != NULL && strcmp(http_method, "CONNECT") == 0 && host_name != NULL && |
1015 | && host_name != NULL && use_ssl == true) | 1057 | use_ssl) { |
1016 | asprintf (&buf, "%s %s %s\r\n%s\r\n", http_method_proxy, server_url, host_name ? "HTTP/1.1" : "HTTP/1.0", user_agent); | 1058 | asprintf(&buf, "%s %s %s\r\n%s\r\n", http_method_proxy, server_url, |
1017 | else | 1059 | host_name ? "HTTP/1.1" : "HTTP/1.0", user_agent); |
1018 | asprintf (&buf, "%s %s %s\r\n%s\r\n", http_method, server_url, host_name ? "HTTP/1.1" : "HTTP/1.0", user_agent); | 1060 | } else { |
1019 | 1061 | asprintf(&buf, "%s %s %s\r\n%s\r\n", http_method, server_url, | |
1020 | /* tell HTTP/1.1 servers not to keep the connection alive */ | 1062 | host_name ? "HTTP/1.1" : "HTTP/1.0", user_agent); |
1021 | xasprintf (&buf, "%sConnection: close\r\n", buf); | 1063 | } |
1022 | 1064 | ||
1023 | /* check if Host header is explicitly set in options */ | 1065 | /* tell HTTP/1.1 servers not to keep the connection alive */ |
1024 | if (http_opt_headers_count) { | 1066 | xasprintf(&buf, "%sConnection: close\r\n", buf); |
1025 | for (i = 0; i < http_opt_headers_count ; i++) { | 1067 | |
1026 | if (strncmp(http_opt_headers[i], "Host:", 5) == 0) { | 1068 | /* check if Host header is explicitly set in options */ |
1027 | force_host_header = http_opt_headers[i]; | 1069 | if (http_opt_headers_count) { |
1028 | } | 1070 | for (i = 0; i < http_opt_headers_count; i++) { |
1029 | } | 1071 | if (strncmp(http_opt_headers[i], "Host:", 5) == 0) { |
1030 | } | 1072 | force_host_header = http_opt_headers[i]; |
1031 | 1073 | } | |
1032 | /* optionally send the host header info */ | 1074 | } |
1033 | if (host_name) { | 1075 | } |
1034 | if (force_host_header) { | 1076 | |
1035 | xasprintf (&buf, "%s%s\r\n", buf, force_host_header); | 1077 | /* optionally send the host header info */ |
1036 | } | 1078 | if (host_name) { |
1037 | else { | 1079 | if (force_host_header) { |
1038 | /* | 1080 | xasprintf(&buf, "%s%s\r\n", buf, force_host_header); |
1039 | * Specify the port only if we're using a non-default port (see RFC 2616, | 1081 | } else { |
1040 | * 14.23). Some server applications/configurations cause trouble if the | 1082 | /* |
1041 | * (default) port is explicitly specified in the "Host:" header line. | 1083 | * Specify the port only if we're using a non-default port (see RFC 2616, |
1042 | */ | 1084 | * 14.23). Some server applications/configurations cause trouble if the |
1043 | if ((use_ssl == false && virtual_port == HTTP_PORT) || | 1085 | * (default) port is explicitly specified in the "Host:" header line. |
1044 | (use_ssl == true && virtual_port == HTTPS_PORT) || | 1086 | */ |
1045 | (server_address != NULL && strcmp(http_method, "CONNECT") == 0 | 1087 | if ((!use_ssl && virtual_port == HTTP_PORT) || |
1046 | && host_name != NULL && use_ssl == true)) | 1088 | (use_ssl && virtual_port == HTTPS_PORT) || |
1047 | xasprintf (&buf, "%sHost: %s\r\n", buf, host_name); | 1089 | (server_address != NULL && strcmp(http_method, "CONNECT") == 0 && |
1048 | else | 1090 | host_name != NULL && use_ssl)) { |
1049 | xasprintf (&buf, "%sHost: %s:%d\r\n", buf, host_name, virtual_port); | 1091 | xasprintf(&buf, "%sHost: %s\r\n", buf, host_name); |
1050 | } | 1092 | } else { |
1051 | } | 1093 | xasprintf(&buf, "%sHost: %s:%d\r\n", buf, host_name, virtual_port); |
1052 | 1094 | } | |
1053 | /* optionally send any other header tag */ | 1095 | } |
1054 | if (http_opt_headers_count) { | 1096 | } |
1055 | for (i = 0; i < http_opt_headers_count ; i++) { | 1097 | |
1056 | if (force_host_header != http_opt_headers[i]) { | 1098 | /* optionally send any other header tag */ |
1057 | xasprintf (&buf, "%s%s\r\n", buf, http_opt_headers[i]); | 1099 | if (http_opt_headers_count) { |
1058 | } | 1100 | for (i = 0; i < http_opt_headers_count; i++) { |
1059 | } | 1101 | if (force_host_header != http_opt_headers[i]) { |
1060 | /* This cannot be free'd here because a redirection will then try to access this and segfault */ | 1102 | xasprintf(&buf, "%s%s\r\n", buf, http_opt_headers[i]); |
1061 | /* Covered in a testcase in tests/check_http.t */ | 1103 | } |
1062 | /* free(http_opt_headers); */ | 1104 | } |
1063 | } | 1105 | /* This cannot be free'd here because a redirection will then try to access this and |
1064 | 1106 | * segfault */ | |
1065 | /* optionally send the authentication info */ | 1107 | /* Covered in a testcase in tests/check_http.t */ |
1066 | if (strlen(user_auth)) { | 1108 | /* free(http_opt_headers); */ |
1067 | base64_encode_alloc (user_auth, strlen (user_auth), &auth); | 1109 | } |
1068 | xasprintf (&buf, "%sAuthorization: Basic %s\r\n", buf, auth); | 1110 | |
1069 | } | 1111 | /* optionally send the authentication info */ |
1070 | 1112 | if (strlen(user_auth)) { | |
1071 | /* optionally send the proxy authentication info */ | 1113 | base64_encode_alloc(user_auth, strlen(user_auth), &auth); |
1072 | if (strlen(proxy_auth)) { | 1114 | xasprintf(&buf, "%sAuthorization: Basic %s\r\n", buf, auth); |
1073 | base64_encode_alloc (proxy_auth, strlen (proxy_auth), &auth); | 1115 | } |
1074 | xasprintf (&buf, "%sProxy-Authorization: Basic %s\r\n", buf, auth); | 1116 | |
1075 | } | 1117 | /* optionally send the proxy authentication info */ |
1076 | 1118 | if (strlen(proxy_auth)) { | |
1077 | /* either send http POST data (any data, not only POST)*/ | 1119 | base64_encode_alloc(proxy_auth, strlen(proxy_auth), &auth); |
1078 | if (http_post_data) { | 1120 | xasprintf(&buf, "%sProxy-Authorization: Basic %s\r\n", buf, auth); |
1079 | if (http_content_type) { | 1121 | } |
1080 | xasprintf (&buf, "%sContent-Type: %s\r\n", buf, http_content_type); | 1122 | |
1081 | } else { | 1123 | /* either send http POST data (any data, not only POST)*/ |
1082 | xasprintf (&buf, "%sContent-Type: application/x-www-form-urlencoded\r\n", buf); | 1124 | if (http_post_data) { |
1083 | } | 1125 | if (http_content_type) { |
1084 | 1126 | xasprintf(&buf, "%sContent-Type: %s\r\n", buf, http_content_type); | |
1085 | xasprintf (&buf, "%sContent-Length: %i\r\n\r\n", buf, (int)strlen (http_post_data)); | 1127 | } else { |
1086 | xasprintf (&buf, "%s%s", buf, http_post_data); | 1128 | xasprintf(&buf, "%sContent-Type: application/x-www-form-urlencoded\r\n", buf); |
1087 | } else { | 1129 | } |
1088 | /* or just a newline so the server knows we're done with the request */ | 1130 | |
1089 | xasprintf (&buf, "%s%s", buf, CRLF); | 1131 | xasprintf(&buf, "%sContent-Length: %i\r\n\r\n", buf, (int)strlen(http_post_data)); |
1090 | } | 1132 | xasprintf(&buf, "%s%s", buf, http_post_data); |
1091 | 1133 | } else { | |
1092 | if (verbose) printf ("%s\n", buf); | 1134 | /* or just a newline so the server knows we're done with the request */ |
1093 | gettimeofday (&tv_temp, NULL); | 1135 | xasprintf(&buf, "%s%s", buf, CRLF); |
1094 | my_send (buf, strlen (buf)); | 1136 | } |
1095 | microsec_headers = deltime (tv_temp); | 1137 | |
1096 | elapsed_time_headers = (double)microsec_headers / 1.0e6; | 1138 | if (verbose) { |
1097 | 1139 | printf("%s\n", buf); | |
1098 | /* fetch the page */ | 1140 | } |
1099 | full_page = strdup(""); | 1141 | gettimeofday(&tv_temp, NULL); |
1100 | gettimeofday (&tv_temp, NULL); | 1142 | my_send(buf, strlen(buf)); |
1101 | while ((i = my_recv (buffer, MAX_INPUT_BUFFER-1)) > 0) { | 1143 | microsec_headers = deltime(tv_temp); |
1102 | if ((i >= 1) && (elapsed_time_firstbyte <= 0.000001)) { | 1144 | elapsed_time_headers = (double)microsec_headers / 1.0e6; |
1103 | microsec_firstbyte = deltime (tv_temp); | 1145 | |
1104 | elapsed_time_firstbyte = (double)microsec_firstbyte / 1.0e6; | 1146 | /* fetch the page */ |
1105 | } | 1147 | full_page = strdup(""); |
1106 | while ((pos = memchr(buffer, '\0', i))) { | 1148 | gettimeofday(&tv_temp, NULL); |
1107 | /* replace nul character with a blank */ | 1149 | while ((i = my_recv(buffer, MAX_INPUT_BUFFER - 1)) > 0) { |
1108 | *pos = ' '; | 1150 | if ((i >= 1) && (elapsed_time_firstbyte <= 0.000001)) { |
1109 | } | 1151 | microsec_firstbyte = deltime(tv_temp); |
1110 | buffer[i] = '\0'; | 1152 | elapsed_time_firstbyte = (double)microsec_firstbyte / 1.0e6; |
1111 | 1153 | } | |
1112 | if ((full_page_new = realloc(full_page, pagesize + i + 1)) == NULL) | 1154 | while ((pos = memchr(buffer, '\0', i))) { |
1113 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate memory for full_page\n")); | 1155 | /* replace nul character with a blank */ |
1114 | 1156 | *pos = ' '; | |
1115 | memmove(&full_page_new[pagesize], buffer, i + 1); | 1157 | } |
1116 | 1158 | buffer[i] = '\0'; | |
1117 | full_page = full_page_new; | 1159 | |
1118 | 1160 | if ((full_page_new = realloc(full_page, pagesize + i + 1)) == NULL) { | |
1119 | pagesize += i; | 1161 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate memory for full_page\n")); |
1120 | 1162 | } | |
1121 | if (no_body && document_headers_done (full_page)) { | 1163 | |
1122 | i = 0; | 1164 | memmove(&full_page_new[pagesize], buffer, i + 1); |
1123 | break; | 1165 | |
1124 | } | 1166 | full_page = full_page_new; |
1125 | } | 1167 | |
1126 | microsec_transfer = deltime (tv_temp); | 1168 | pagesize += i; |
1127 | elapsed_time_transfer = (double)microsec_transfer / 1.0e6; | 1169 | |
1128 | 1170 | if (no_body && document_headers_done(full_page)) { | |
1129 | if (i < 0 && errno != ECONNRESET) { | 1171 | i = 0; |
1130 | die(STATE_CRITICAL, _("HTTP CRITICAL - Error on receive\n")); | 1172 | break; |
1131 | } | 1173 | } |
1132 | 1174 | } | |
1133 | /* return a CRITICAL status if we couldn't read any data */ | 1175 | microsec_transfer = deltime(tv_temp); |
1134 | if (pagesize == (size_t) 0) | 1176 | elapsed_time_transfer = (double)microsec_transfer / 1.0e6; |
1135 | die (STATE_CRITICAL, _("HTTP CRITICAL - No data received from host\n")); | 1177 | |
1136 | 1178 | if (i < 0 && errno != ECONNRESET) { | |
1137 | /* close the connection */ | 1179 | die(STATE_CRITICAL, _("HTTP CRITICAL - Error on receive\n")); |
1138 | if (sd) close(sd); | 1180 | } |
1181 | |||
1182 | /* return a CRITICAL status if we couldn't read any data */ | ||
1183 | if (pagesize == (size_t)0) { | ||
1184 | die(STATE_CRITICAL, _("HTTP CRITICAL - No data received from host\n")); | ||
1185 | } | ||
1186 | |||
1187 | /* close the connection */ | ||
1188 | if (sd) { | ||
1189 | close(sd); | ||
1190 | } | ||
1139 | #ifdef HAVE_SSL | 1191 | #ifdef HAVE_SSL |
1140 | np_net_ssl_cleanup(); | 1192 | np_net_ssl_cleanup(); |
1141 | #endif | 1193 | #endif |
1142 | 1194 | ||
1143 | /* Save check time */ | 1195 | /* Save check time */ |
1144 | microsec = deltime (tv); | 1196 | microsec = deltime(tv); |
1145 | elapsed_time = (double)microsec / 1.0e6; | 1197 | elapsed_time = (double)microsec / 1.0e6; |
1146 | 1198 | ||
1147 | /* leave full_page untouched so we can free it later */ | 1199 | /* leave full_page untouched so we can free it later */ |
1148 | page = full_page; | 1200 | page = full_page; |
1149 | 1201 | ||
1150 | if (verbose) | 1202 | if (verbose) { |
1151 | printf ("%s://%s:%d%s is %d characters\n", | 1203 | printf("%s://%s:%d%s is %d characters\n", use_ssl ? "https" : "http", server_address, |
1152 | use_ssl ? "https" : "http", server_address, | 1204 | server_port, server_url, (int)pagesize); |
1153 | server_port, server_url, (int)pagesize); | 1205 | } |
1154 | 1206 | ||
1155 | /* find status line and null-terminate it */ | 1207 | /* find status line and null-terminate it */ |
1156 | status_line = page; | 1208 | status_line = page; |
1157 | page += (size_t) strcspn (page, "\r\n"); | 1209 | page += (size_t)strcspn(page, "\r\n"); |
1158 | pos = page; | 1210 | pos = page; |
1159 | page += (size_t) strspn (page, "\r\n"); | 1211 | page += (size_t)strspn(page, "\r\n"); |
1160 | status_line[strcspn(status_line, "\r\n")] = 0; | 1212 | status_line[strcspn(status_line, "\r\n")] = 0; |
1161 | strip (status_line); | 1213 | strip(status_line); |
1162 | if (verbose) | 1214 | if (verbose) { |
1163 | printf ("STATUS: %s\n", status_line); | 1215 | printf("STATUS: %s\n", status_line); |
1164 | 1216 | } | |
1165 | /* find header info and null-terminate it */ | 1217 | |
1166 | header = page; | 1218 | /* find header info and null-terminate it */ |
1167 | while (strcspn (page, "\r\n") > 0) { | 1219 | header = page; |
1168 | page += (size_t) strcspn (page, "\r\n"); | 1220 | while (strcspn(page, "\r\n") > 0) { |
1169 | pos = page; | 1221 | page += (size_t)strcspn(page, "\r\n"); |
1170 | if ((strspn (page, "\r") == 1 && strspn (page, "\r\n") >= 2) || | 1222 | pos = page; |
1171 | (strspn (page, "\n") == 1 && strspn (page, "\r\n") >= 2)) | 1223 | if ((strspn(page, "\r") == 1 && strspn(page, "\r\n") >= 2) || |
1172 | page += (size_t) 2; | 1224 | (strspn(page, "\n") == 1 && strspn(page, "\r\n") >= 2)) { |
1173 | else | 1225 | page += (size_t)2; |
1174 | page += (size_t) 1; | 1226 | } else { |
1175 | } | 1227 | page += (size_t)1; |
1176 | page += (size_t) strspn (page, "\r\n"); | 1228 | } |
1177 | header[pos - header] = 0; | 1229 | } |
1178 | if (verbose) | 1230 | page += (size_t)strspn(page, "\r\n"); |
1179 | printf ("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header, | 1231 | header[pos - header] = 0; |
1180 | (no_body ? " [[ skipped ]]" : page)); | 1232 | if (verbose) { |
1181 | 1233 | printf("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header, | |
1182 | /* make sure the status line matches the response we are looking for */ | 1234 | (no_body ? " [[ skipped ]]" : page)); |
1183 | if (!expected_statuscode (status_line, server_expect)) { | 1235 | } |
1184 | if (server_port == HTTP_PORT) | 1236 | |
1185 | xasprintf (&msg, | 1237 | /* make sure the status line matches the response we are looking for */ |
1186 | _("Invalid HTTP response received from host: %s\n"), | 1238 | if (!expected_statuscode(status_line, server_expect)) { |
1187 | status_line); | 1239 | if (server_port == HTTP_PORT) { |
1188 | else | 1240 | xasprintf(&msg, _("Invalid HTTP response received from host: %s\n"), status_line); |
1189 | xasprintf (&msg, | 1241 | } else { |
1190 | _("Invalid HTTP response received from host on port %d: %s\n"), | 1242 | xasprintf(&msg, _("Invalid HTTP response received from host on port %d: %s\n"), |
1191 | server_port, status_line); | 1243 | server_port, status_line); |
1192 | if (show_body) | 1244 | } |
1193 | xasprintf (&msg, _("%s\n%s"), msg, page); | 1245 | if (show_body) { |
1194 | die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg); | 1246 | xasprintf(&msg, _("%s\n%s"), msg, page); |
1195 | } | 1247 | } |
1196 | 1248 | die(STATE_CRITICAL, "HTTP CRITICAL - %s", msg); | |
1197 | /* Bypass normal status line check if server_expect was set by user and not default */ | 1249 | } |
1198 | /* NOTE: After this if/else block msg *MUST* be an asprintf-allocated string */ | 1250 | |
1199 | if ( server_expect_yn ) { | 1251 | /* Bypass normal status line check if server_expect was set by user and not default */ |
1200 | xasprintf (&msg, | 1252 | /* NOTE: After this if/else block msg *MUST* be an asprintf-allocated string */ |
1201 | _("Status line output matched \"%s\" - "), server_expect); | 1253 | if (server_expect_yn) { |
1202 | if (verbose) | 1254 | xasprintf(&msg, _("Status line output matched \"%s\" - "), server_expect); |
1203 | printf ("%s\n",msg); | 1255 | if (verbose) { |
1204 | } | 1256 | printf("%s\n", msg); |
1205 | else { | 1257 | } |
1206 | /* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */ | 1258 | } else { |
1207 | /* HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT */ | 1259 | /* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */ |
1208 | /* Status-Code = 3 DIGITS */ | 1260 | /* HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT */ |
1209 | 1261 | /* Status-Code = 3 DIGITS */ | |
1210 | status_code = strchr (status_line, ' ') + sizeof (char); | 1262 | |
1211 | if (strspn (status_code, "1234567890") != 3) | 1263 | status_code = strchr(status_line, ' ') + sizeof(char); |
1212 | die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status Line (%s)\n"), status_line); | 1264 | if (strspn(status_code, "1234567890") != 3) { |
1213 | 1265 | die(STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status Line (%s)\n"), status_line); | |
1214 | http_status = atoi (status_code); | 1266 | } |
1215 | 1267 | ||
1216 | /* check the return code */ | 1268 | http_status = atoi(status_code); |
1217 | 1269 | ||
1218 | if (http_status >= 600 || http_status < 100) { | 1270 | /* check the return code */ |
1219 | die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%s)\n"), status_line); | 1271 | |
1220 | } | 1272 | if (http_status >= 600 || http_status < 100) { |
1221 | /* server errors result in a critical state */ | 1273 | die(STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%s)\n"), status_line); |
1222 | else if (http_status >= 500) { | 1274 | } |
1223 | xasprintf (&msg, _("%s - "), status_line); | 1275 | /* server errors result in a critical state */ |
1224 | result = STATE_CRITICAL; | 1276 | else if (http_status >= 500) { |
1225 | } | 1277 | xasprintf(&msg, _("%s - "), status_line); |
1226 | /* client errors result in a warning state */ | 1278 | result = STATE_CRITICAL; |
1227 | else if (http_status >= 400) { | 1279 | } |
1228 | xasprintf (&msg, _("%s - "), status_line); | 1280 | /* client errors result in a warning state */ |
1229 | result = max_state_alt(STATE_WARNING, result); | 1281 | else if (http_status >= 400) { |
1230 | } | 1282 | xasprintf(&msg, _("%s - "), status_line); |
1231 | /* check redirected page if specified */ | 1283 | result = max_state_alt(STATE_WARNING, result); |
1232 | else if (http_status >= 300) { | 1284 | } |
1233 | 1285 | /* check redirected page if specified */ | |
1234 | if (onredirect == STATE_DEPENDENT) | 1286 | else if (http_status >= 300) { |
1235 | redir (header, status_line); | 1287 | |
1236 | else | 1288 | if (onredirect == STATE_DEPENDENT) { |
1237 | result = max_state_alt(onredirect, result); | 1289 | redir(header, status_line); |
1238 | xasprintf (&msg, _("%s - "), status_line); | 1290 | } else { |
1239 | } /* end if (http_status >= 300) */ | 1291 | result = max_state_alt(onredirect, result); |
1240 | else { | 1292 | } |
1241 | /* Print OK status anyway */ | 1293 | xasprintf(&msg, _("%s - "), status_line); |
1242 | xasprintf (&msg, _("%s - "), status_line); | 1294 | } /* end if (http_status >= 300) */ |
1243 | } | 1295 | else { |
1244 | 1296 | /* Print OK status anyway */ | |
1245 | } /* end else (server_expect_yn) */ | 1297 | xasprintf(&msg, _("%s - "), status_line); |
1246 | 1298 | } | |
1247 | /* reset the alarm - must be called *after* redir or we'll never die on redirects! */ | 1299 | |
1248 | alarm (0); | 1300 | } /* end else (server_expect_yn) */ |
1249 | 1301 | ||
1250 | if (maximum_age >= 0) { | 1302 | /* reset the alarm - must be called *after* redir or we'll never die on redirects! */ |
1251 | result = max_state_alt(check_document_dates(header, &msg), result); | 1303 | alarm(0); |
1252 | } | 1304 | |
1253 | 1305 | if (maximum_age >= 0) { | |
1254 | /* Page and Header content checks go here */ | 1306 | result = max_state_alt(check_document_dates(header, &msg), result); |
1255 | if (strlen(header_expect) > 0) { | 1307 | } |
1256 | if (strstr(header, header_expect) == NULL) { | 1308 | |
1257 | // We did not find the header, the rest is for building the output and setting the state | 1309 | /* Page and Header content checks go here */ |
1258 | char output_header_search[30] = ""; | 1310 | if (strlen(header_expect) > 0) { |
1259 | 1311 | if (strstr(header, header_expect) == NULL) { | |
1260 | strncpy(&output_header_search[0], header_expect, | 1312 | // We did not find the header, the rest is for building the output and setting the state |
1261 | sizeof(output_header_search)); | 1313 | char output_header_search[30] = ""; |
1262 | 1314 | ||
1263 | if (output_header_search[sizeof(output_header_search) - 1] != '\0') { | 1315 | strncpy(&output_header_search[0], header_expect, sizeof(output_header_search)); |
1264 | bcopy("...", | 1316 | |
1265 | &output_header_search[sizeof(output_header_search) - 4], | 1317 | if (output_header_search[sizeof(output_header_search) - 1] != '\0') { |
1266 | 4); | 1318 | bcopy("...", &output_header_search[sizeof(output_header_search) - 4], 4); |
1267 | } | 1319 | } |
1268 | 1320 | ||
1269 | xasprintf (&msg, | 1321 | xasprintf(&msg, _("%sheader '%s' not found on '%s://%s:%d%s', "), msg, |
1270 | _("%sheader '%s' not found on '%s://%s:%d%s', "), | 1322 | output_header_search, use_ssl ? "https" : "http", |
1271 | msg, | 1323 | host_name ? host_name : server_address, server_port, server_url); |
1272 | output_header_search, use_ssl ? "https" : "http", | 1324 | |
1273 | host_name ? host_name : server_address, server_port, | 1325 | result = STATE_CRITICAL; |
1274 | server_url); | 1326 | } |
1275 | 1327 | } | |
1276 | result = STATE_CRITICAL; | 1328 | |
1277 | } | 1329 | // At this point we should test if the content is chunked and unchunk it, so |
1278 | } | 1330 | // it can be searched (and possibly printed) |
1279 | 1331 | const char *chunked_header_regex_string = "Transfer-Encoding: *chunked *"; | |
1280 | // At this point we should test if the content is chunked and unchunk it, so | 1332 | regex_t chunked_header_regex; |
1281 | // it can be searched (and possibly printed) | 1333 | |
1282 | const char *chunked_header_regex_string = "Transfer-Encoding: *chunked *"; | 1334 | if (regcomp(&chunked_header_regex, chunked_header_regex_string, REG_ICASE)) { |
1283 | regex_t chunked_header_regex; | 1335 | die(STATE_UNKNOWN, "HTTP %s: %s\n", state_text(STATE_UNKNOWN), |
1284 | 1336 | "Failed to compile chunked_header_regex regex"); | |
1285 | if (regcomp(&chunked_header_regex, chunked_header_regex_string, REG_ICASE)) { | 1337 | } |
1286 | die(STATE_UNKNOWN, "HTTP %s: %s\n", state_text(STATE_UNKNOWN), "Failed to compile chunked_header_regex regex"); | 1338 | |
1287 | } | 1339 | regmatch_t chre_pmatch[1]; // We actually do not care about this, since we only want to know IF |
1288 | 1340 | // it was found | |
1289 | regmatch_t chre_pmatch[1]; // We actually do not care about this, since we only want to know IF it was found | 1341 | |
1290 | 1342 | if (!no_body && regexec(&chunked_header_regex, header, 1, chre_pmatch, 0) == 0) { | |
1291 | if (!no_body && regexec(&chunked_header_regex, header, 1, chre_pmatch, 0) == 0) { | 1343 | if (verbose) { |
1292 | if (verbose) { | 1344 | printf("Found chunked content\n"); |
1293 | printf("Found chunked content\n"); | 1345 | } |
1294 | } | 1346 | // We actually found the chunked header |
1295 | // We actually found the chunked header | 1347 | char *tmp = unchunk_content(page); |
1296 | char *tmp = unchunk_content(page); | 1348 | if (tmp == NULL) { |
1297 | if (tmp == NULL) { | 1349 | die(STATE_UNKNOWN, "HTTP %s: %s\n", state_text(STATE_UNKNOWN), |
1298 | die(STATE_UNKNOWN, "HTTP %s: %s\n", state_text(STATE_UNKNOWN), "Failed to unchunk message body"); | 1350 | "Failed to unchunk message body"); |
1299 | } | 1351 | } |
1300 | page = tmp; | 1352 | page = tmp; |
1301 | } | 1353 | } |
1302 | 1354 | ||
1303 | if (strlen(string_expect) > 0) { | 1355 | if (strlen(string_expect) > 0) { |
1304 | if (!strstr(page, string_expect)) { | 1356 | if (!strstr(page, string_expect)) { |
1305 | // We found the string the body, the rest is for building the output | 1357 | // We found the string the body, the rest is for building the output |
1306 | char output_string_search[30] = ""; | 1358 | char output_string_search[30] = ""; |
1307 | strncpy(&output_string_search[0], string_expect, | 1359 | strncpy(&output_string_search[0], string_expect, sizeof(output_string_search)); |
1308 | sizeof(output_string_search)); | 1360 | if (output_string_search[sizeof(output_string_search) - 1] != '\0') { |
1309 | if (output_string_search[sizeof(output_string_search) - 1] != '\0') { | 1361 | bcopy("...", &output_string_search[sizeof(output_string_search) - 4], 4); |
1310 | bcopy("...", &output_string_search[sizeof(output_string_search) - 4], | 1362 | } |
1311 | 4); | 1363 | xasprintf(&msg, _("%sstring '%s' not found on '%s://%s:%d%s', "), msg, |
1312 | } | 1364 | output_string_search, use_ssl ? "https" : "http", |
1313 | xasprintf (&msg, _("%sstring '%s' not found on '%s://%s:%d%s', "), msg, output_string_search, use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, server_url); | 1365 | host_name ? host_name : server_address, server_port, server_url); |
1314 | result = STATE_CRITICAL; | 1366 | result = STATE_CRITICAL; |
1315 | } | 1367 | } |
1316 | } | 1368 | } |
1317 | 1369 | ||
1318 | if (strlen(regexp) > 0) { | 1370 | if (strlen(regexp) > 0) { |
1319 | errcode = regexec(&preg, page, REGS, pmatch, 0); | 1371 | errcode = regexec(&preg, page, REGS, pmatch, 0); |
1320 | if ((errcode == 0 && invert_regex == 0) || | 1372 | if ((errcode == 0 && invert_regex == 0) || (errcode == REG_NOMATCH && invert_regex == 1)) { |
1321 | (errcode == REG_NOMATCH && invert_regex == 1)) { | 1373 | /* OK - No-op to avoid changing the logic around it */ |
1322 | /* OK - No-op to avoid changing the logic around it */ | 1374 | result = max_state_alt(STATE_OK, result); |
1323 | result = max_state_alt(STATE_OK, result); | 1375 | } else if ((errcode == REG_NOMATCH && invert_regex == 0) || |
1324 | } | 1376 | (errcode == 0 && invert_regex == 1)) { |
1325 | else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) { | 1377 | if (invert_regex == 0) { |
1326 | if (invert_regex == 0) | 1378 | xasprintf(&msg, _("%spattern not found, "), msg); |
1327 | xasprintf (&msg, _("%spattern not found, "), msg); | 1379 | } else { |
1328 | else | 1380 | xasprintf(&msg, _("%spattern found, "), msg); |
1329 | xasprintf (&msg, _("%spattern found, "), msg); | 1381 | } |
1330 | result = state_regex; | 1382 | result = state_regex; |
1331 | } | 1383 | } else { |
1332 | else { | 1384 | /* FIXME: Shouldn't that be UNKNOWN? */ |
1333 | /* FIXME: Shouldn't that be UNKNOWN? */ | 1385 | regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); |
1334 | regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); | 1386 | xasprintf(&msg, _("%sExecute Error: %s, "), msg, errbuf); |
1335 | xasprintf (&msg, _("%sExecute Error: %s, "), msg, errbuf); | 1387 | result = STATE_CRITICAL; |
1336 | result = STATE_CRITICAL; | 1388 | } |
1337 | } | 1389 | } |
1338 | } | 1390 | |
1339 | 1391 | /* make sure the page is of an appropriate size */ | |
1340 | /* make sure the page is of an appropriate size */ | 1392 | /* page_len = get_content_length(header); */ |
1341 | /* page_len = get_content_length(header); */ | 1393 | /* FIXME: Will this work with -N ? IMHO we should use |
1342 | /* FIXME: Will this work with -N ? IMHO we should use | 1394 | * get_content_length(header) and always check if it's different than the |
1343 | * get_content_length(header) and always check if it's different than the | 1395 | * returned pagesize |
1344 | * returned pagesize | 1396 | */ |
1345 | */ | 1397 | /* FIXME: IIRC pagesize returns headers - shouldn't we make |
1346 | /* FIXME: IIRC pagesize returns headers - shouldn't we make | 1398 | * it == get_content_length(header) ?? |
1347 | * it == get_content_length(header) ?? | 1399 | */ |
1348 | */ | 1400 | page_len = pagesize; |
1349 | page_len = pagesize; | 1401 | if ((max_page_len > 0) && (page_len > max_page_len)) { |
1350 | if ((max_page_len > 0) && (page_len > max_page_len)) { | 1402 | xasprintf(&msg, _("%spage size %d too large, "), msg, page_len); |
1351 | xasprintf (&msg, _("%spage size %d too large, "), msg, page_len); | 1403 | result = max_state_alt(STATE_WARNING, result); |
1352 | result = max_state_alt(STATE_WARNING, result); | 1404 | } else if ((min_page_len > 0) && (page_len < min_page_len)) { |
1353 | } else if ((min_page_len > 0) && (page_len < min_page_len)) { | 1405 | xasprintf(&msg, _("%spage size %d too small, "), msg, page_len); |
1354 | xasprintf (&msg, _("%spage size %d too small, "), msg, page_len); | 1406 | result = max_state_alt(STATE_WARNING, result); |
1355 | result = max_state_alt(STATE_WARNING, result); | 1407 | } |
1356 | } | 1408 | |
1357 | 1409 | /* Cut-off trailing characters */ | |
1358 | /* Cut-off trailing characters */ | 1410 | if (msg[strlen(msg) - 2] == ',') { |
1359 | if(msg[strlen(msg)-2] == ',') | 1411 | msg[strlen(msg) - 2] = '\0'; |
1360 | msg[strlen(msg)-2] = '\0'; | 1412 | } else { |
1361 | else | 1413 | msg[strlen(msg) - 3] = '\0'; |
1362 | msg[strlen(msg)-3] = '\0'; | 1414 | } |
1363 | 1415 | ||
1364 | /* check elapsed time */ | 1416 | /* check elapsed time */ |
1365 | if (show_extended_perfdata) | 1417 | if (show_extended_perfdata) { |
1366 | xasprintf (&msg, | 1418 | xasprintf( |
1367 | _("%s - %d bytes in %.3f second response time %s|%s %s %s %s %s %s %s"), | 1419 | &msg, _("%s - %d bytes in %.3f second response time %s|%s %s %s %s %s %s %s"), msg, |
1368 | msg, page_len, elapsed_time, | 1420 | page_len, elapsed_time, (display_html ? "</A>" : ""), perfd_time(elapsed_time), |
1369 | (display_html ? "</A>" : ""), | 1421 | perfd_size(page_len), perfd_time_connect(elapsed_time_connect), |
1370 | perfd_time (elapsed_time), | 1422 | use_ssl ? perfd_time_ssl(elapsed_time_ssl) : "", |
1371 | perfd_size (page_len), | 1423 | perfd_time_headers(elapsed_time_headers), perfd_time_firstbyte(elapsed_time_firstbyte), |
1372 | perfd_time_connect (elapsed_time_connect), | 1424 | perfd_time_transfer(elapsed_time_transfer)); |
1373 | use_ssl == true ? perfd_time_ssl (elapsed_time_ssl) : "", | 1425 | } else { |
1374 | perfd_time_headers (elapsed_time_headers), | 1426 | xasprintf(&msg, _("%s - %d bytes in %.3f second response time %s|%s %s"), msg, page_len, |
1375 | perfd_time_firstbyte (elapsed_time_firstbyte), | 1427 | elapsed_time, (display_html ? "</A>" : ""), perfd_time(elapsed_time), |
1376 | perfd_time_transfer (elapsed_time_transfer)); | 1428 | perfd_size(page_len)); |
1377 | else | 1429 | } |
1378 | xasprintf (&msg, | 1430 | |
1379 | _("%s - %d bytes in %.3f second response time %s|%s %s"), | 1431 | if (show_body) { |
1380 | msg, page_len, elapsed_time, | 1432 | xasprintf(&msg, _("%s\n%s"), msg, page); |
1381 | (display_html ? "</A>" : ""), | 1433 | } |
1382 | perfd_time (elapsed_time), | 1434 | |
1383 | perfd_size (page_len)); | 1435 | result = max_state_alt(get_status(elapsed_time, thlds), result); |
1384 | 1436 | ||
1385 | if (show_body) | 1437 | die(result, "HTTP %s: %s\n", state_text(result), msg); |
1386 | xasprintf (&msg, _("%s\n%s"), msg, page); | 1438 | /* die failed? */ |
1387 | 1439 | return STATE_UNKNOWN; | |
1388 | result = max_state_alt(get_status(elapsed_time, thlds), result); | ||
1389 | |||
1390 | die (result, "HTTP %s: %s\n", state_text(result), msg); | ||
1391 | /* die failed? */ | ||
1392 | return STATE_UNKNOWN; | ||
1393 | } | 1440 | } |
1394 | 1441 | ||
1395 | /* Receivces a pointer to the beginning of the body of a HTTP message | 1442 | /* Receivces a pointer to the beginning of the body of a HTTP message |
@@ -1398,94 +1445,95 @@ check_http (void) | |||
1398 | * The result must be freed by the caller. | 1445 | * The result must be freed by the caller. |
1399 | */ | 1446 | */ |
1400 | char *unchunk_content(const char *content) { | 1447 | char *unchunk_content(const char *content) { |
1401 | // https://en.wikipedia.org/wiki/Chunked_transfer_encoding | 1448 | // https://en.wikipedia.org/wiki/Chunked_transfer_encoding |
1402 | // https://www.rfc-editor.org/rfc/rfc7230#section-4.1 | 1449 | // https://www.rfc-editor.org/rfc/rfc7230#section-4.1 |
1403 | char *result = NULL; | 1450 | char *result = NULL; |
1404 | char *start_of_chunk; | 1451 | char *start_of_chunk; |
1405 | char* end_of_chunk; | 1452 | char *end_of_chunk; |
1406 | long size_of_chunk; | 1453 | long size_of_chunk; |
1407 | const char *pointer = content; | 1454 | const char *pointer = content; |
1408 | char *endptr; | 1455 | char *endptr; |
1409 | long length_of_chunk = 0; | 1456 | long length_of_chunk = 0; |
1410 | size_t overall_size = 0; | 1457 | size_t overall_size = 0; |
1411 | 1458 | ||
1412 | while (true) { | 1459 | while (true) { |
1413 | size_of_chunk = strtol(pointer, &endptr, 16); | 1460 | size_of_chunk = strtol(pointer, &endptr, 16); |
1414 | if (size_of_chunk == LONG_MIN || size_of_chunk == LONG_MAX) { | 1461 | if (size_of_chunk == LONG_MIN || size_of_chunk == LONG_MAX) { |
1415 | // Apparently underflow or overflow, should not happen | 1462 | // Apparently underflow or overflow, should not happen |
1416 | if (verbose) { | 1463 | if (verbose) { |
1417 | printf("Got an underflow or overflow from strtol at: %u\n", __LINE__); | 1464 | printf("Got an underflow or overflow from strtol at: %u\n", __LINE__); |
1418 | } | 1465 | } |
1419 | return NULL; | 1466 | return NULL; |
1420 | } | 1467 | } |
1421 | if (endptr == pointer) { | 1468 | if (endptr == pointer) { |
1422 | // Apparently this was not a number | 1469 | // Apparently this was not a number |
1423 | if (verbose) { | 1470 | if (verbose) { |
1424 | printf("Chunked content did not start with a number at all (Line: %u)\n", __LINE__); | 1471 | printf("Chunked content did not start with a number at all (Line: %u)\n", __LINE__); |
1425 | } | 1472 | } |
1426 | return NULL; | 1473 | return NULL; |
1427 | } | 1474 | } |
1428 | 1475 | ||
1429 | // So, we got the length of the chunk | 1476 | // So, we got the length of the chunk |
1430 | if (*endptr == ';') { | 1477 | if (*endptr == ';') { |
1431 | // Chunk extension starts here | 1478 | // Chunk extension starts here |
1432 | while (*endptr != '\r') { | 1479 | while (*endptr != '\r') { |
1433 | endptr++; | 1480 | endptr++; |
1434 | } | 1481 | } |
1435 | } | 1482 | } |
1436 | 1483 | ||
1437 | start_of_chunk = endptr + 2; | 1484 | start_of_chunk = endptr + 2; |
1438 | end_of_chunk = start_of_chunk + size_of_chunk; | 1485 | end_of_chunk = start_of_chunk + size_of_chunk; |
1439 | length_of_chunk = (long)(end_of_chunk - start_of_chunk); | 1486 | length_of_chunk = (long)(end_of_chunk - start_of_chunk); |
1440 | pointer = end_of_chunk + 2; //Next number should be here | 1487 | pointer = end_of_chunk + 2; // Next number should be here |
1441 | 1488 | ||
1442 | if (length_of_chunk == 0) { | 1489 | if (length_of_chunk == 0) { |
1443 | // Chunk length is 0, so this is the last one | 1490 | // Chunk length is 0, so this is the last one |
1444 | break; | 1491 | break; |
1445 | } | 1492 | } |
1446 | 1493 | ||
1447 | overall_size += length_of_chunk; | 1494 | overall_size += length_of_chunk; |
1448 | 1495 | ||
1449 | if (result == NULL) { | 1496 | if (result == NULL) { |
1450 | // Size of the chunk plus the ending NULL byte | 1497 | // Size of the chunk plus the ending NULL byte |
1451 | result = (char *)malloc(length_of_chunk +1); | 1498 | result = (char *)malloc(length_of_chunk + 1); |
1452 | if (result == NULL) { | 1499 | if (result == NULL) { |
1453 | if (verbose) { | 1500 | if (verbose) { |
1454 | printf("Failed to allocate memory for unchunked body\n"); | 1501 | printf("Failed to allocate memory for unchunked body\n"); |
1455 | } | 1502 | } |
1456 | return NULL; | 1503 | return NULL; |
1457 | } | 1504 | } |
1458 | } else { | 1505 | } else { |
1459 | // Enlarge memory to the new size plus the ending NULL byte | 1506 | // Enlarge memory to the new size plus the ending NULL byte |
1460 | void *tmp = realloc(result, overall_size +1); | 1507 | void *tmp = realloc(result, overall_size + 1); |
1461 | if (tmp == NULL) { | 1508 | if (tmp == NULL) { |
1462 | if (verbose) { | 1509 | if (verbose) { |
1463 | printf("Failed to allocate memory for unchunked body\n"); | 1510 | printf("Failed to allocate memory for unchunked body\n"); |
1464 | } | 1511 | } |
1465 | return NULL; | 1512 | return NULL; |
1466 | } else { | 1513 | } |
1467 | result = tmp; | 1514 | result = tmp; |
1468 | } | 1515 | } |
1469 | } | 1516 | |
1470 | 1517 | memcpy(result + (overall_size - size_of_chunk), start_of_chunk, size_of_chunk); | |
1471 | memcpy(result + (overall_size - size_of_chunk), start_of_chunk, size_of_chunk); | 1518 | } |
1472 | } | 1519 | |
1473 | 1520 | if (overall_size == 0 && result == NULL) { | |
1474 | if (overall_size == 0 && result == NULL) { | 1521 | // We might just have received the end chunk without previous content, so result is never |
1475 | // We might just have received the end chunk without previous content, so result is never allocated | 1522 | // allocated |
1476 | result = calloc(1, sizeof(char)); | 1523 | result = calloc(1, sizeof(char)); |
1477 | // No error handling here, we can only return NULL anyway | 1524 | // No error handling here, we can only return NULL anyway |
1478 | } else { | 1525 | } else { |
1479 | result[overall_size] = '\0'; | 1526 | result[overall_size] = '\0'; |
1480 | } | 1527 | } |
1481 | return result; | 1528 | return result; |
1482 | } | 1529 | } |
1483 | 1530 | ||
1484 | /* per RFC 2396 */ | 1531 | /* per RFC 2396 */ |
1485 | #define URI_HTTP "%5[HTPShtps]" | 1532 | #define URI_HTTP "%5[HTPShtps]" |
1486 | #define URI_HOST "%255[-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]" | 1533 | #define URI_HOST "%255[-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]" |
1487 | #define URI_PORT "%6d" /* MAX_PORT's width is 5 chars, 6 to detect overflow */ | 1534 | #define URI_PORT "%6d" /* MAX_PORT's width is 5 chars, 6 to detect overflow */ |
1488 | #define URI_PATH "%[-_.!~*'();/?:@&=+$,%#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]" | 1535 | #define URI_PATH \ |
1536 | "%[-_.!~*'();/?:@&=+$,%#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]" | ||
1489 | #define HD1 URI_HTTP "://" URI_HOST ":" URI_PORT "/" URI_PATH | 1537 | #define HD1 URI_HTTP "://" URI_HOST ":" URI_PORT "/" URI_PATH |
1490 | #define HD2 URI_HTTP "://" URI_HOST "/" URI_PATH | 1538 | #define HD2 URI_HTTP "://" URI_HOST "/" URI_PATH |
1491 | #define HD3 URI_HTTP "://" URI_HOST ":" URI_PORT | 1539 | #define HD3 URI_HTTP "://" URI_HOST ":" URI_PORT |
@@ -1494,414 +1542,431 @@ char *unchunk_content(const char *content) { | |||
1494 | #define HD5 "//" URI_HOST "/" URI_PATH | 1542 | #define HD5 "//" URI_HOST "/" URI_PATH |
1495 | #define HD6 URI_PATH | 1543 | #define HD6 URI_PATH |
1496 | 1544 | ||
1497 | void | 1545 | void redir(char *pos, char *status_line) { |
1498 | redir (char *pos, char *status_line) | 1546 | int i = 0; |
1499 | { | 1547 | char *x; |
1500 | int i = 0; | 1548 | char xx[2]; |
1501 | char *x; | 1549 | char type[6]; |
1502 | char xx[2]; | 1550 | char *addr; |
1503 | char type[6]; | 1551 | char *url; |
1504 | char *addr; | 1552 | |
1505 | char *url; | 1553 | addr = malloc(MAX_IPV4_HOSTLENGTH + 1); |
1506 | 1554 | if (addr == NULL) { | |
1507 | addr = malloc (MAX_IPV4_HOSTLENGTH + 1); | 1555 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate addr\n")); |
1508 | if (addr == NULL) | 1556 | } |
1509 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate addr\n")); | 1557 | |
1510 | 1558 | memset(addr, 0, MAX_IPV4_HOSTLENGTH); | |
1511 | memset(addr, 0, MAX_IPV4_HOSTLENGTH); | 1559 | url = malloc(strcspn(pos, "\r\n")); |
1512 | url = malloc (strcspn (pos, "\r\n")); | 1560 | if (url == NULL) { |
1513 | if (url == NULL) | 1561 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate URL\n")); |
1514 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate URL\n")); | 1562 | } |
1515 | 1563 | ||
1516 | while (pos) { | 1564 | while (pos) { |
1517 | sscanf (pos, "%1[Ll]%*1[Oo]%*1[Cc]%*1[Aa]%*1[Tt]%*1[Ii]%*1[Oo]%*1[Nn]:%n", xx, &i); | 1565 | sscanf(pos, "%1[Ll]%*1[Oo]%*1[Cc]%*1[Aa]%*1[Tt]%*1[Ii]%*1[Oo]%*1[Nn]:%n", xx, &i); |
1518 | if (i == 0) { | 1566 | if (i == 0) { |
1519 | pos += (size_t) strcspn (pos, "\r\n"); | 1567 | pos += (size_t)strcspn(pos, "\r\n"); |
1520 | pos += (size_t) strspn (pos, "\r\n"); | 1568 | pos += (size_t)strspn(pos, "\r\n"); |
1521 | if (strlen(pos) == 0) | 1569 | if (strlen(pos) == 0) { |
1522 | die (STATE_UNKNOWN, | 1570 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not find redirect location - %s%s\n"), |
1523 | _("HTTP UNKNOWN - Could not find redirect location - %s%s\n"), | 1571 | status_line, (display_html ? "</A>" : "")); |
1524 | status_line, (display_html ? "</A>" : "")); | 1572 | } |
1525 | continue; | 1573 | continue; |
1526 | } | 1574 | } |
1527 | 1575 | ||
1528 | pos += i; | 1576 | pos += i; |
1529 | pos += strspn (pos, " \t"); | 1577 | pos += strspn(pos, " \t"); |
1530 | 1578 | ||
1531 | /* | 1579 | /* |
1532 | * RFC 2616 (4.2): ``Header fields can be extended over multiple lines by | 1580 | * RFC 2616 (4.2): ``Header fields can be extended over multiple lines by |
1533 | * preceding each extra line with at least one SP or HT.'' | 1581 | * preceding each extra line with at least one SP or HT.'' |
1534 | */ | 1582 | */ |
1535 | for (; (i = strspn (pos, "\r\n")); pos += i) { | 1583 | for (; (i = strspn(pos, "\r\n")); pos += i) { |
1536 | pos += i; | 1584 | pos += i; |
1537 | if (!(i = strspn (pos, " \t"))) { | 1585 | if (!(i = strspn(pos, " \t"))) { |
1538 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Empty redirect location%s\n"), | 1586 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Empty redirect location%s\n"), |
1539 | display_html ? "</A>" : ""); | 1587 | display_html ? "</A>" : ""); |
1540 | } | 1588 | } |
1541 | } | 1589 | } |
1542 | 1590 | ||
1543 | url = realloc (url, strcspn (pos, "\r\n") + 1); | 1591 | url = realloc(url, strcspn(pos, "\r\n") + 1); |
1544 | if (url == NULL) | 1592 | if (url == NULL) { |
1545 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate URL\n")); | 1593 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate URL\n")); |
1546 | 1594 | } | |
1547 | /* URI_HTTP, URI_HOST, URI_PORT, URI_PATH */ | 1595 | |
1548 | if (sscanf (pos, HD1, type, addr, &i, url) == 4) { | 1596 | /* URI_HTTP, URI_HOST, URI_PORT, URI_PATH */ |
1549 | url = prepend_slash (url); | 1597 | if (sscanf(pos, HD1, type, addr, &i, url) == 4) { |
1550 | use_ssl = server_type_check (type); | 1598 | url = prepend_slash(url); |
1551 | } | 1599 | use_ssl = server_type_check(type); |
1552 | 1600 | } | |
1553 | /* URI_HTTP URI_HOST URI_PATH */ | 1601 | |
1554 | else if (sscanf (pos, HD2, type, addr, url) == 3 ) { | 1602 | /* URI_HTTP URI_HOST URI_PATH */ |
1555 | url = prepend_slash (url); | 1603 | else if (sscanf(pos, HD2, type, addr, url) == 3) { |
1556 | use_ssl = server_type_check (type); | 1604 | url = prepend_slash(url); |
1557 | i = server_port_check (use_ssl); | 1605 | use_ssl = server_type_check(type); |
1558 | } | 1606 | i = server_port_check(use_ssl); |
1559 | 1607 | } | |
1560 | /* URI_HTTP URI_HOST URI_PORT */ | 1608 | |
1561 | else if (sscanf (pos, HD3, type, addr, &i) == 3) { | 1609 | /* URI_HTTP URI_HOST URI_PORT */ |
1562 | strcpy (url, HTTP_URL); | 1610 | else if (sscanf(pos, HD3, type, addr, &i) == 3) { |
1563 | use_ssl = server_type_check (type); | 1611 | strcpy(url, HTTP_URL); |
1564 | } | 1612 | use_ssl = server_type_check(type); |
1565 | 1613 | } | |
1566 | /* URI_HTTP URI_HOST */ | 1614 | |
1567 | else if (sscanf (pos, HD4, type, addr) == 2) { | 1615 | /* URI_HTTP URI_HOST */ |
1568 | strcpy (url, HTTP_URL); | 1616 | else if (sscanf(pos, HD4, type, addr) == 2) { |
1569 | use_ssl = server_type_check (type); | 1617 | strcpy(url, HTTP_URL); |
1570 | i = server_port_check (use_ssl); | 1618 | use_ssl = server_type_check(type); |
1571 | } | 1619 | i = server_port_check(use_ssl); |
1572 | /* URI_HTTP, URI_HOST, URI_PATH */ | 1620 | } |
1573 | else if (sscanf (pos, HD5, addr, url) == 2) { | 1621 | /* URI_HTTP, URI_HOST, URI_PATH */ |
1574 | if(use_ssl){ | 1622 | else if (sscanf(pos, HD5, addr, url) == 2) { |
1575 | strcpy (type,"https"); | 1623 | if (use_ssl) { |
1576 | } | 1624 | strcpy(type, "https"); |
1577 | else{ | 1625 | } else { |
1578 | strcpy (type, server_type); | 1626 | strcpy(type, server_type); |
1579 | } | 1627 | } |
1580 | xasprintf (&url, "/%s", url); | 1628 | xasprintf(&url, "/%s", url); |
1581 | use_ssl = server_type_check (type); | 1629 | use_ssl = server_type_check(type); |
1582 | i = server_port_check (use_ssl); | 1630 | i = server_port_check(use_ssl); |
1583 | } | 1631 | } |
1584 | 1632 | ||
1585 | /* URI_PATH */ | 1633 | /* URI_PATH */ |
1586 | else if (sscanf (pos, HD6, url) == 1) { | 1634 | else if (sscanf(pos, HD6, url) == 1) { |
1587 | /* relative url */ | 1635 | /* relative url */ |
1588 | if ((url[0] != '/')) { | 1636 | if ((url[0] != '/')) { |
1589 | if ((x = strrchr(server_url, '/'))) | 1637 | if ((x = strrchr(server_url, '/'))) { |
1590 | *x = '\0'; | 1638 | *x = '\0'; |
1591 | xasprintf (&url, "%s/%s", server_url, url); | 1639 | } |
1592 | } | 1640 | xasprintf(&url, "%s/%s", server_url, url); |
1593 | i = server_port; | 1641 | } |
1594 | strcpy (type, server_type); | 1642 | i = server_port; |
1595 | strcpy (addr, host_name ? host_name : server_address); | 1643 | strcpy(type, server_type); |
1596 | } | 1644 | strcpy(addr, host_name ? host_name : server_address); |
1597 | 1645 | } | |
1598 | else { | 1646 | |
1599 | die (STATE_UNKNOWN, | 1647 | else { |
1600 | _("HTTP UNKNOWN - Could not parse redirect location - %s%s\n"), | 1648 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Could not parse redirect location - %s%s\n"), pos, |
1601 | pos, (display_html ? "</A>" : "")); | 1649 | (display_html ? "</A>" : "")); |
1602 | } | 1650 | } |
1603 | 1651 | ||
1604 | break; | 1652 | break; |
1605 | 1653 | ||
1606 | } /* end while (pos) */ | 1654 | } /* end while (pos) */ |
1607 | 1655 | ||
1608 | if (++redir_depth > max_depth) | 1656 | if (++redir_depth > max_depth) { |
1609 | die (STATE_WARNING, | 1657 | die(STATE_WARNING, |
1610 | _("HTTP WARNING - maximum redirection depth %d exceeded - %s://%s:%d%s%s\n"), | 1658 | _("HTTP WARNING - maximum redirection depth %d exceeded - %s://%s:%d%s%s\n"), max_depth, |
1611 | max_depth, type, addr, i, url, (display_html ? "</A>" : "")); | 1659 | type, addr, i, url, (display_html ? "</A>" : "")); |
1612 | 1660 | } | |
1613 | if (server_port==i && | 1661 | |
1614 | !strncmp(server_address, addr, MAX_IPV4_HOSTLENGTH) && | 1662 | if (server_port == i && !strncmp(server_address, addr, MAX_IPV4_HOSTLENGTH) && |
1615 | (host_name && !strncmp(host_name, addr, MAX_IPV4_HOSTLENGTH)) && | 1663 | (host_name && !strncmp(host_name, addr, MAX_IPV4_HOSTLENGTH)) && !strcmp(server_url, url)) { |
1616 | !strcmp(server_url, url)) | 1664 | die(STATE_CRITICAL, |
1617 | die (STATE_CRITICAL, | 1665 | _("HTTP CRITICAL - redirection creates an infinite loop - %s://%s:%d%s%s\n"), type, |
1618 | _("HTTP CRITICAL - redirection creates an infinite loop - %s://%s:%d%s%s\n"), | 1666 | addr, i, url, (display_html ? "</A>" : "")); |
1619 | type, addr, i, url, (display_html ? "</A>" : "")); | 1667 | } |
1620 | 1668 | ||
1621 | strcpy (server_type, type); | 1669 | strcpy(server_type, type); |
1622 | 1670 | ||
1623 | free (host_name); | 1671 | free(host_name); |
1624 | host_name = strndup (addr, MAX_IPV4_HOSTLENGTH); | 1672 | host_name = strndup(addr, MAX_IPV4_HOSTLENGTH); |
1625 | 1673 | ||
1626 | if (!(followsticky & STICKY_HOST)) { | 1674 | if (!(followsticky & STICKY_HOST)) { |
1627 | free (server_address); | 1675 | free(server_address); |
1628 | server_address = strndup (addr, MAX_IPV4_HOSTLENGTH); | 1676 | server_address = strndup(addr, MAX_IPV4_HOSTLENGTH); |
1629 | } | 1677 | } |
1630 | if (!(followsticky & STICKY_PORT)) { | 1678 | if (!(followsticky & STICKY_PORT)) { |
1631 | server_port = i; | 1679 | server_port = i; |
1632 | } | 1680 | } |
1633 | 1681 | ||
1634 | free (server_url); | 1682 | free(server_url); |
1635 | server_url = url; | 1683 | server_url = url; |
1636 | 1684 | ||
1637 | if (server_port > MAX_PORT) | 1685 | if (server_port > MAX_PORT) { |
1638 | die (STATE_UNKNOWN, | 1686 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Redirection to port above %d - %s://%s:%d%s%s\n"), |
1639 | _("HTTP UNKNOWN - Redirection to port above %d - %s://%s:%d%s%s\n"), | 1687 | MAX_PORT, server_type, server_address, server_port, server_url, |
1640 | MAX_PORT, server_type, server_address, server_port, server_url, | 1688 | display_html ? "</A>" : ""); |
1641 | display_html ? "</A>" : ""); | 1689 | } |
1642 | |||
1643 | /* reset virtual port */ | ||
1644 | virtual_port = server_port; | ||
1645 | |||
1646 | if (verbose) | ||
1647 | printf (_("Redirection to %s://%s:%d%s\n"), server_type, | ||
1648 | host_name ? host_name : server_address, server_port, server_url); | ||
1649 | |||
1650 | free(addr); | ||
1651 | check_http (); | ||
1652 | } | ||
1653 | 1690 | ||
1691 | /* reset virtual port */ | ||
1692 | virtual_port = server_port; | ||
1654 | 1693 | ||
1655 | bool | 1694 | if (verbose) { |
1656 | server_type_check (const char *type) | 1695 | printf(_("Redirection to %s://%s:%d%s\n"), server_type, |
1657 | { | 1696 | host_name ? host_name : server_address, server_port, server_url); |
1658 | if (strcmp (type, "https")) | 1697 | } |
1659 | return false; | 1698 | |
1660 | else | 1699 | free(addr); |
1661 | return true; | 1700 | check_http(); |
1662 | } | 1701 | } |
1663 | 1702 | ||
1664 | int | 1703 | bool server_type_check(const char *type) { return (!(bool)strcmp(type, "https")); } |
1665 | server_port_check (int ssl_flag) | 1704 | |
1666 | { | 1705 | int server_port_check(int ssl_flag) { |
1667 | if (ssl_flag) | 1706 | if (ssl_flag) { |
1668 | return HTTPS_PORT; | 1707 | return HTTPS_PORT; |
1669 | else | 1708 | } |
1670 | return HTTP_PORT; | 1709 | return HTTP_PORT; |
1671 | } | 1710 | } |
1672 | 1711 | ||
1673 | char *perfd_time (double elapsed_time) | 1712 | char *perfd_time(double elapsed_time) { |
1674 | { | 1713 | return fperfdata("time", elapsed_time, "s", thlds->warning, |
1675 | return fperfdata ("time", elapsed_time, "s", | 1714 | thlds->warning ? thlds->warning->end : 0, thlds->critical, |
1676 | thlds->warning?true:false, thlds->warning?thlds->warning->end:0, | 1715 | thlds->critical ? thlds->critical->end : 0, true, 0, true, socket_timeout); |
1677 | thlds->critical?true:false, thlds->critical?thlds->critical->end:0, | ||
1678 | true, 0, true, socket_timeout); | ||
1679 | } | 1716 | } |
1680 | 1717 | ||
1681 | char *perfd_time_connect (double elapsed_time_connect) | 1718 | char *perfd_time_connect(double elapsed_time_connect) { |
1682 | { | 1719 | return fperfdata("time_connect", elapsed_time_connect, "s", false, 0, false, 0, false, 0, true, |
1683 | return fperfdata ("time_connect", elapsed_time_connect, "s", false, 0, false, 0, false, 0, true, socket_timeout); | 1720 | socket_timeout); |
1684 | } | 1721 | } |
1685 | 1722 | ||
1686 | char *perfd_time_ssl (double elapsed_time_ssl) | 1723 | char *perfd_time_ssl(double elapsed_time_ssl) { |
1687 | { | 1724 | return fperfdata("time_ssl", elapsed_time_ssl, "s", false, 0, false, 0, false, 0, true, |
1688 | return fperfdata ("time_ssl", elapsed_time_ssl, "s", false, 0, false, 0, false, 0, true, socket_timeout); | 1725 | socket_timeout); |
1689 | } | 1726 | } |
1690 | 1727 | ||
1691 | char *perfd_time_headers (double elapsed_time_headers) | 1728 | char *perfd_time_headers(double elapsed_time_headers) { |
1692 | { | 1729 | return fperfdata("time_headers", elapsed_time_headers, "s", false, 0, false, 0, false, 0, true, |
1693 | return fperfdata ("time_headers", elapsed_time_headers, "s", false, 0, false, 0, false, 0, true, socket_timeout); | 1730 | socket_timeout); |
1694 | } | 1731 | } |
1695 | 1732 | ||
1696 | char *perfd_time_firstbyte (double elapsed_time_firstbyte) | 1733 | char *perfd_time_firstbyte(double elapsed_time_firstbyte) { |
1697 | { | 1734 | return fperfdata("time_firstbyte", elapsed_time_firstbyte, "s", false, 0, false, 0, false, 0, |
1698 | return fperfdata ("time_firstbyte", elapsed_time_firstbyte, "s", false, 0, false, 0, false, 0, true, socket_timeout); | 1735 | true, socket_timeout); |
1699 | } | 1736 | } |
1700 | 1737 | ||
1701 | char *perfd_time_transfer (double elapsed_time_transfer) | 1738 | char *perfd_time_transfer(double elapsed_time_transfer) { |
1702 | { | 1739 | return fperfdata("time_transfer", elapsed_time_transfer, "s", false, 0, false, 0, false, 0, |
1703 | return fperfdata ("time_transfer", elapsed_time_transfer, "s", false, 0, false, 0, false, 0, true, socket_timeout); | 1740 | true, socket_timeout); |
1704 | } | 1741 | } |
1705 | 1742 | ||
1706 | char *perfd_size (int page_len) | 1743 | char *perfd_size(int page_len) { |
1707 | { | 1744 | return perfdata("size", page_len, "B", (min_page_len > 0), min_page_len, (min_page_len > 0), 0, |
1708 | return perfdata ("size", page_len, "B", | 1745 | true, 0, false, 0); |
1709 | (min_page_len>0?true:false), min_page_len, | ||
1710 | (min_page_len>0?true:false), 0, | ||
1711 | true, 0, false, 0); | ||
1712 | } | 1746 | } |
1713 | 1747 | ||
1714 | void | 1748 | void print_help(void) { |
1715 | print_help (void) | 1749 | print_revision(progname, NP_VERSION); |
1716 | { | ||
1717 | print_revision (progname, NP_VERSION); | ||
1718 | 1750 | ||
1719 | printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | 1751 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
1720 | printf (COPYRIGHT, copyright, email); | 1752 | printf(COPYRIGHT, copyright, email); |
1721 | 1753 | ||
1722 | printf ("%s\n", _("This plugin tests the HTTP service on the specified host. It can test")); | 1754 | printf("%s\n", _("This plugin tests the HTTP service on the specified host. It can test")); |
1723 | printf ("%s\n", _("normal (http) and secure (https) servers, follow redirects, search for")); | 1755 | printf("%s\n", _("normal (http) and secure (https) servers, follow redirects, search for")); |
1724 | printf ("%s\n", _("strings and regular expressions, check connection times, and report on")); | 1756 | printf("%s\n", _("strings and regular expressions, check connection times, and report on")); |
1725 | printf ("%s\n", _("certificate expiration times.")); | 1757 | printf("%s\n", _("certificate expiration times.")); |
1726 | 1758 | ||
1727 | printf ("\n\n"); | 1759 | printf("\n"); |
1760 | printf("%s\n", _("ATTENTION!")); | ||
1761 | printf("\n"); | ||
1762 | printf("%s\n", _("THIS PLUGIN IS DEPRECATED. The functionality was reimplemented by the")); | ||
1763 | printf("%s\n", _("check_curl plugin, which can be used as a drop-in replacement. You should")); | ||
1764 | printf("%s\n", _("migrate your checks over to check_curl, because check_http is going to be")); | ||
1765 | printf("%s\n", _("removed sooner than later. Just replace check_http with check_curl in your")); | ||
1766 | printf("%s\n", _("check command definitions.")); | ||
1767 | printf("%s\n", | ||
1768 | _("Report issues to: https://github.com/monitoring-plugins/monitoring-plugins/issues")); | ||
1728 | 1769 | ||
1729 | print_usage (); | 1770 | printf("\n\n"); |
1771 | |||
1772 | print_usage(); | ||
1730 | 1773 | ||
1731 | #ifdef HAVE_SSL | 1774 | #ifdef HAVE_SSL |
1732 | printf (_("In the first form, make an HTTP request.")); | 1775 | printf(_("In the first form, make an HTTP request.")); |
1733 | printf (_("In the second form, connect to the server and check the TLS certificate.")); | 1776 | printf(_("In the second form, connect to the server and check the TLS certificate.")); |
1734 | #endif | 1777 | #endif |
1735 | printf (_("NOTE: One or both of -H and -I must be specified")); | 1778 | printf(_("NOTE: One or both of -H and -I must be specified")); |
1736 | 1779 | ||
1737 | printf ("\n"); | 1780 | printf("\n"); |
1738 | 1781 | ||
1739 | printf (UT_HELP_VRSN); | 1782 | printf(UT_HELP_VRSN); |
1740 | printf (UT_EXTRA_OPTS); | 1783 | printf(UT_EXTRA_OPTS); |
1741 | 1784 | ||
1742 | printf (" %s\n", "-H, --hostname=ADDRESS"); | 1785 | printf(" %s\n", "-H, --hostname=ADDRESS"); |
1743 | printf (" %s\n", _("Host name argument for servers using host headers (virtual host)")); | 1786 | printf(" %s\n", _("Host name argument for servers using host headers (virtual host)")); |
1744 | printf (" %s\n", _("Append a port to include it in the header (eg: example.com:5000)")); | 1787 | printf(" %s\n", _("Append a port to include it in the header (eg: example.com:5000)")); |
1745 | printf (" %s\n", "-I, --IP-address=ADDRESS"); | 1788 | printf(" %s\n", "-I, --IP-address=ADDRESS"); |
1746 | printf (" %s\n", _("IP address or name (use numeric address if possible to bypass DNS lookup).")); | 1789 | printf(" %s\n", |
1747 | printf (" %s\n", "-p, --port=INTEGER"); | 1790 | _("IP address or name (use numeric address if possible to bypass DNS lookup).")); |
1748 | printf (" %s", _("Port number (default: ")); | 1791 | printf(" %s\n", "-p, --port=INTEGER"); |
1749 | printf ("%d)\n", HTTP_PORT); | 1792 | printf(" %s", _("Port number (default: ")); |
1793 | printf("%d)\n", HTTP_PORT); | ||
1750 | 1794 | ||
1751 | printf (UT_IPv46); | 1795 | printf(UT_IPv46); |
1752 | 1796 | ||
1753 | #ifdef HAVE_SSL | 1797 | #ifdef HAVE_SSL |
1754 | printf (" %s\n", "-S, --ssl=VERSION[+]"); | 1798 | printf(" %s\n", "-S, --ssl=VERSION[+]"); |
1755 | printf (" %s\n", _("Connect via SSL. Port defaults to 443. VERSION is optional, and prevents")); | 1799 | printf(" %s\n", |
1756 | printf (" %s\n", _("auto-negotiation (2 = SSLv2, 3 = SSLv3, 1 = TLSv1, 1.1 = TLSv1.1,")); | 1800 | _("Connect via SSL. Port defaults to 443. VERSION is optional, and prevents")); |
1757 | printf (" %s\n", _("1.2 = TLSv1.2). With a '+' suffix, newer versions are also accepted.")); | 1801 | printf(" %s\n", _("auto-negotiation (2 = SSLv2, 3 = SSLv3, 1 = TLSv1, 1.1 = TLSv1.1,")); |
1758 | printf (" %s\n", "--sni"); | 1802 | printf(" %s\n", _("1.2 = TLSv1.2). With a '+' suffix, newer versions are also accepted.")); |
1759 | printf (" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); | 1803 | printf(" %s\n", "--sni"); |
1760 | printf (" %s\n", "-C, --certificate=INTEGER[,INTEGER]"); | 1804 | printf(" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); |
1761 | printf (" %s\n", _("Minimum number of days a certificate has to be valid. Port defaults to 443")); | 1805 | printf(" %s\n", "-C, --certificate=INTEGER[,INTEGER]"); |
1762 | printf (" %s\n", _("(when this option is used the URL is not checked by default. You can use")); | 1806 | printf(" %s\n", |
1763 | printf (" %s\n", _(" --continue-after-certificate to override this behavior)")); | 1807 | _("Minimum number of days a certificate has to be valid. Port defaults to 443")); |
1764 | printf (" %s\n", "--continue-after-certificate"); | 1808 | printf(" %s\n", |
1765 | printf (" %s\n", _("Allows the HTTP check to continue after performing the certificate check.")); | 1809 | _("(when this option is used the URL is not checked by default. You can use")); |
1766 | printf (" %s\n", _("Does nothing unless -C is used.")); | 1810 | printf(" %s\n", _(" --continue-after-certificate to override this behavior)")); |
1767 | printf (" %s\n", "-J, --client-cert=FILE"); | 1811 | printf(" %s\n", "--continue-after-certificate"); |
1768 | printf (" %s\n", _("Name of file that contains the client certificate (PEM format)")); | 1812 | printf(" %s\n", |
1769 | printf (" %s\n", _("to be used in establishing the SSL session")); | 1813 | _("Allows the HTTP check to continue after performing the certificate check.")); |
1770 | printf (" %s\n", "-K, --private-key=FILE"); | 1814 | printf(" %s\n", _("Does nothing unless -C is used.")); |
1771 | printf (" %s\n", _("Name of file containing the private key (PEM format)")); | 1815 | printf(" %s\n", "-J, --client-cert=FILE"); |
1772 | printf (" %s\n", _("matching the client certificate")); | 1816 | printf(" %s\n", _("Name of file that contains the client certificate (PEM format)")); |
1817 | printf(" %s\n", _("to be used in establishing the SSL session")); | ||
1818 | printf(" %s\n", "-K, --private-key=FILE"); | ||
1819 | printf(" %s\n", _("Name of file containing the private key (PEM format)")); | ||
1820 | printf(" %s\n", _("matching the client certificate")); | ||
1773 | #endif | 1821 | #endif |
1774 | 1822 | ||
1775 | printf (" %s\n", "-e, --expect=STRING"); | 1823 | printf(" %s\n", "-e, --expect=STRING"); |
1776 | printf (" %s\n", _("Comma-delimited list of strings, at least one of them is expected in")); | 1824 | printf(" %s\n", _("Comma-delimited list of strings, at least one of them is expected in")); |
1777 | printf (" %s", _("the first (status) line of the server response (default: ")); | 1825 | printf(" %s", _("the first (status) line of the server response (default: ")); |
1778 | printf ("%s)\n", HTTP_EXPECT); | 1826 | printf("%s)\n", HTTP_EXPECT); |
1779 | printf (" %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)")); | 1827 | printf(" %s\n", |
1780 | printf (" %s\n", "-d, --header-string=STRING"); | 1828 | _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)")); |
1781 | printf (" %s\n", _("String to expect in the response headers")); | 1829 | printf(" %s\n", "-d, --header-string=STRING"); |
1782 | printf (" %s\n", "-s, --string=STRING"); | 1830 | printf(" %s\n", _("String to expect in the response headers")); |
1783 | printf (" %s\n", _("String to expect in the content")); | 1831 | printf(" %s\n", "-s, --string=STRING"); |
1784 | printf (" %s\n", "-u, --url=PATH"); | 1832 | printf(" %s\n", _("String to expect in the content")); |
1785 | printf (" %s\n", _("URL to GET or POST (default: /)")); | 1833 | printf(" %s\n", "-u, --url=PATH"); |
1786 | printf (" %s\n", "-P, --post=STRING"); | 1834 | printf(" %s\n", _("URL to GET or POST (default: /)")); |
1787 | printf (" %s\n", _("URL decoded http POST data")); | 1835 | printf(" %s\n", "-P, --post=STRING"); |
1788 | printf (" %s\n", "-j, --method=STRING (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, CONNECT, CONNECT:POST)"); | 1836 | printf(" %s\n", _("URL decoded http POST data")); |
1789 | printf (" %s\n", _("Set HTTP method.")); | 1837 | printf(" %s\n", "-j, --method=STRING (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, " |
1790 | printf (" %s\n", "-N, --no-body"); | 1838 | "CONNECT, CONNECT:POST)"); |
1791 | printf (" %s\n", _("Don't wait for document body: stop reading after headers.")); | 1839 | printf(" %s\n", _("Set HTTP method.")); |
1792 | printf (" %s\n", _("(Note that this still does an HTTP GET or POST, not a HEAD.)")); | 1840 | printf(" %s\n", "-N, --no-body"); |
1793 | printf (" %s\n", "-M, --max-age=SECONDS"); | 1841 | printf(" %s\n", _("Don't wait for document body: stop reading after headers.")); |
1794 | printf (" %s\n", _("Warn if document is more than SECONDS old. the number can also be of")); | 1842 | printf(" %s\n", _("(Note that this still does an HTTP GET or POST, not a HEAD.)")); |
1795 | printf (" %s\n", _("the form \"10m\" for minutes, \"10h\" for hours, or \"10d\" for days.")); | 1843 | printf(" %s\n", "-M, --max-age=SECONDS"); |
1796 | printf (" %s\n", "-T, --content-type=STRING"); | 1844 | printf(" %s\n", _("Warn if document is more than SECONDS old. the number can also be of")); |
1797 | printf (" %s\n", _("specify Content-Type header media type when POSTing\n")); | 1845 | printf(" %s\n", _("the form \"10m\" for minutes, \"10h\" for hours, or \"10d\" for days.")); |
1798 | 1846 | printf(" %s\n", "-T, --content-type=STRING"); | |
1799 | printf (" %s\n", "-l, --linespan"); | 1847 | printf(" %s\n", _("specify Content-Type header media type when POSTing\n")); |
1800 | printf (" %s\n", _("Allow regex to span newlines (must precede -r or -R)")); | 1848 | |
1801 | printf (" %s\n", "-r, --regex, --ereg=STRING"); | 1849 | printf(" %s\n", "-l, --linespan"); |
1802 | printf (" %s\n", _("Search page for regex STRING")); | 1850 | printf(" %s\n", _("Allow regex to span newlines (must precede -r or -R)")); |
1803 | printf (" %s\n", "-R, --eregi=STRING"); | 1851 | printf(" %s\n", "-r, --regex, --ereg=STRING"); |
1804 | printf (" %s\n", _("Search page for case-insensitive regex STRING")); | 1852 | printf(" %s\n", _("Search page for regex STRING")); |
1805 | printf (" %s\n", "--invert-regex"); | 1853 | printf(" %s\n", "-R, --eregi=STRING"); |
1806 | printf (" %s\n", _("Return STATE if found, OK if not (STATE is CRITICAL, per default)")); | 1854 | printf(" %s\n", _("Search page for case-insensitive regex STRING")); |
1807 | printf (" %s\n", _("can be changed with --state--regex)")); | 1855 | printf(" %s\n", "--invert-regex"); |
1808 | printf (" %s\n", "--state-regex=STATE"); | 1856 | printf(" %s\n", _("Return STATE if found, OK if not (STATE is CRITICAL, per default)")); |
1809 | printf (" %s\n", _("Return STATE if regex is found, OK if not\n")); | 1857 | printf(" %s\n", _("can be changed with --state--regex)")); |
1810 | 1858 | printf(" %s\n", "--state-regex=STATE"); | |
1811 | printf (" %s\n", "-a, --authorization=AUTH_PAIR"); | 1859 | printf(" %s\n", _("Return STATE if regex is found, OK if not\n")); |
1812 | printf (" %s\n", _("Username:password on sites with basic authentication")); | 1860 | |
1813 | printf (" %s\n", "-b, --proxy-authorization=AUTH_PAIR"); | 1861 | printf(" %s\n", "-a, --authorization=AUTH_PAIR"); |
1814 | printf (" %s\n", _("Username:password on proxy-servers with basic authentication")); | 1862 | printf(" %s\n", _("Username:password on sites with basic authentication")); |
1815 | printf (" %s\n", "-A, --useragent=STRING"); | 1863 | printf(" %s\n", "-b, --proxy-authorization=AUTH_PAIR"); |
1816 | printf (" %s\n", _("String to be sent in http header as \"User Agent\"")); | 1864 | printf(" %s\n", _("Username:password on proxy-servers with basic authentication")); |
1817 | printf (" %s\n", "-k, --header=STRING"); | 1865 | printf(" %s\n", "-A, --useragent=STRING"); |
1818 | printf (" %s\n", _("Any other tags to be sent in http header. Use multiple times for additional headers")); | 1866 | printf(" %s\n", _("String to be sent in http header as \"User Agent\"")); |
1819 | printf (" %s\n", "-E, --extended-perfdata"); | 1867 | printf(" %s\n", "-k, --header=STRING"); |
1820 | printf (" %s\n", _("Print additional performance data")); | 1868 | printf( |
1821 | printf (" %s\n", "-B, --show-body"); | 1869 | " %s\n", |
1822 | printf (" %s\n", _("Print body content below status line")); | 1870 | _("Any other tags to be sent in http header. Use multiple times for additional headers")); |
1823 | printf (" %s\n", "-L, --link"); | 1871 | printf(" %s\n", "-E, --extended-perfdata"); |
1824 | printf (" %s\n", _("Wrap output in HTML link (obsoleted by urlize)")); | 1872 | printf(" %s\n", _("Print additional performance data")); |
1825 | printf (" %s\n", "-f, --onredirect=<ok|warning|critical|follow|sticky|stickyport>"); | 1873 | printf(" %s\n", "-B, --show-body"); |
1826 | printf (" %s\n", _("How to handle redirected pages. sticky is like follow but stick to the")); | 1874 | printf(" %s\n", _("Print body content below status line")); |
1827 | printf (" %s\n", _("specified IP address. stickyport also ensures port stays the same.")); | 1875 | printf(" %s\n", "-L, --link"); |
1828 | printf (" %s\n", "--max-redirs=INTEGER"); | 1876 | printf(" %s\n", _("Wrap output in HTML link (obsoleted by urlize)")); |
1829 | printf (" %s", _("Maximal number of redirects (default: ")); | 1877 | printf(" %s\n", "-f, --onredirect=<ok|warning|critical|follow|sticky|stickyport>"); |
1830 | printf ("%d)\n", DEFAULT_MAX_REDIRS); | 1878 | printf(" %s\n", _("How to handle redirected pages. sticky is like follow but stick to the")); |
1831 | printf (" %s\n", "-m, --pagesize=INTEGER<:INTEGER>"); | 1879 | printf(" %s\n", _("specified IP address. stickyport also ensures port stays the same.")); |
1832 | printf (" %s\n", _("Minimum page size required (bytes) : Maximum page size required (bytes)")); | 1880 | printf(" %s\n", "--max-redirs=INTEGER"); |
1833 | printf (UT_WARN_CRIT); | 1881 | printf(" %s", _("Maximal number of redirects (default: ")); |
1834 | 1882 | printf("%d)\n", DEFAULT_MAX_REDIRS); | |
1835 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 1883 | printf(" %s\n", "-m, --pagesize=INTEGER<:INTEGER>"); |
1836 | 1884 | printf(" %s\n", | |
1837 | printf (UT_VERBOSE); | 1885 | _("Minimum page size required (bytes) : Maximum page size required (bytes)")); |
1838 | 1886 | printf(UT_WARN_CRIT); | |
1839 | printf ("\n"); | 1887 | |
1840 | printf ("%s\n", _("Notes:")); | 1888 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
1841 | printf (" %s\n", _("This plugin will attempt to open an HTTP connection with the host.")); | 1889 | |
1842 | printf (" %s\n", _("Successful connects return STATE_OK, refusals and timeouts return STATE_CRITICAL")); | 1890 | printf(UT_VERBOSE); |
1843 | printf (" %s\n", _("other errors return STATE_UNKNOWN. Successful connects, but incorrect response")); | 1891 | |
1844 | printf (" %s\n", _("messages from the host result in STATE_WARNING return values. If you are")); | 1892 | printf("\n"); |
1845 | printf (" %s\n", _("checking a virtual server that uses 'host headers' you must supply the FQDN")); | 1893 | printf("%s\n", _("Notes:")); |
1846 | printf (" %s\n", _("(fully qualified domain name) as the [host_name] argument.")); | 1894 | printf(" %s\n", _("This plugin will attempt to open an HTTP connection with the host.")); |
1895 | printf(" %s\n", | ||
1896 | _("Successful connects return STATE_OK, refusals and timeouts return STATE_CRITICAL")); | ||
1897 | printf(" %s\n", | ||
1898 | _("other errors return STATE_UNKNOWN. Successful connects, but incorrect response")); | ||
1899 | printf(" %s\n", _("messages from the host result in STATE_WARNING return values. If you are")); | ||
1900 | printf(" %s\n", | ||
1901 | _("checking a virtual server that uses 'host headers' you must supply the FQDN")); | ||
1902 | printf(" %s\n", _("(fully qualified domain name) as the [host_name] argument.")); | ||
1847 | 1903 | ||
1848 | #ifdef HAVE_SSL | 1904 | #ifdef HAVE_SSL |
1849 | printf ("\n"); | 1905 | printf("\n"); |
1850 | printf (" %s\n", _("This plugin can also check whether an SSL enabled web server is able to")); | 1906 | printf(" %s\n", _("This plugin can also check whether an SSL enabled web server is able to")); |
1851 | printf (" %s\n", _("serve content (optionally within a specified time) or whether the X509 ")); | 1907 | printf(" %s\n", _("serve content (optionally within a specified time) or whether the X509 ")); |
1852 | printf (" %s\n", _("certificate is still valid for the specified number of days.")); | 1908 | printf(" %s\n", _("certificate is still valid for the specified number of days.")); |
1853 | printf ("\n"); | 1909 | printf("\n"); |
1854 | printf (" %s\n", _("Please note that this plugin does not check if the presented server")); | 1910 | printf(" %s\n", _("Please note that this plugin does not check if the presented server")); |
1855 | printf (" %s\n", _("certificate matches the hostname of the server, or if the certificate")); | 1911 | printf(" %s\n", _("certificate matches the hostname of the server, or if the certificate")); |
1856 | printf (" %s\n", _("has a valid chain of trust to one of the locally installed CAs.")); | 1912 | printf(" %s\n", _("has a valid chain of trust to one of the locally installed CAs.")); |
1857 | printf ("\n"); | 1913 | printf("\n"); |
1858 | printf ("%s\n", _("Examples:")); | 1914 | printf("%s\n", _("Examples:")); |
1859 | printf (" %s\n\n", "CHECK CONTENT: check_http -w 5 -c 10 --ssl -H www.verisign.com"); | 1915 | printf(" %s\n\n", "CHECK CONTENT: check_http -w 5 -c 10 --ssl -H www.verisign.com"); |
1860 | printf (" %s\n", _("When the 'www.verisign.com' server returns its content within 5 seconds,")); | 1916 | printf(" %s\n", _("When the 'www.verisign.com' server returns its content within 5 seconds,")); |
1861 | printf (" %s\n", _("a STATE_OK will be returned. When the server returns its content but exceeds")); | 1917 | printf(" %s\n", |
1862 | printf (" %s\n", _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | 1918 | _("a STATE_OK will be returned. When the server returns its content but exceeds")); |
1863 | printf (" %s\n", _("a STATE_CRITICAL will be returned.")); | 1919 | printf(" %s\n", |
1864 | printf ("\n"); | 1920 | _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); |
1865 | printf (" %s\n\n", "CHECK CERTIFICATE: check_http -H www.verisign.com -C 14"); | 1921 | printf(" %s\n", _("a STATE_CRITICAL will be returned.")); |
1866 | printf (" %s\n", _("When the certificate of 'www.verisign.com' is valid for more than 14 days,")); | 1922 | printf("\n"); |
1867 | printf (" %s\n", _("a STATE_OK is returned. When the certificate is still valid, but for less than")); | 1923 | printf(" %s\n\n", "CHECK CERTIFICATE: check_http -H www.verisign.com -C 14"); |
1868 | printf (" %s\n", _("14 days, a STATE_WARNING is returned. A STATE_CRITICAL will be returned when")); | 1924 | printf(" %s\n", |
1869 | printf (" %s\n\n", _("the certificate is expired.")); | 1925 | _("When the certificate of 'www.verisign.com' is valid for more than 14 days,")); |
1870 | printf ("\n"); | 1926 | printf(" %s\n", |
1871 | printf (" %s\n\n", "CHECK CERTIFICATE: check_http -H www.verisign.com -C 30,14"); | 1927 | _("a STATE_OK is returned. When the certificate is still valid, but for less than")); |
1872 | printf (" %s\n", _("When the certificate of 'www.verisign.com' is valid for more than 30 days,")); | 1928 | printf(" %s\n", |
1873 | printf (" %s\n", _("a STATE_OK is returned. When the certificate is still valid, but for less than")); | 1929 | _("14 days, a STATE_WARNING is returned. A STATE_CRITICAL will be returned when")); |
1874 | printf (" %s\n", _("30 days, but more than 14 days, a STATE_WARNING is returned.")); | 1930 | printf(" %s\n\n", _("the certificate is expired.")); |
1875 | printf (" %s\n", _("A STATE_CRITICAL will be returned when certificate expires in less than 14 days")); | 1931 | printf("\n"); |
1876 | 1932 | printf(" %s\n\n", "CHECK CERTIFICATE: check_http -H www.verisign.com -C 30,14"); | |
1877 | printf (" %s\n\n", "CHECK SSL WEBSERVER CONTENT VIA PROXY USING HTTP 1.1 CONNECT: "); | 1933 | printf(" %s\n", |
1878 | printf (" %s\n", _("check_http -I 192.168.100.35 -p 80 -u https://www.verisign.com/ -S -j CONNECT -H www.verisign.com ")); | 1934 | _("When the certificate of 'www.verisign.com' is valid for more than 30 days,")); |
1879 | printf (" %s\n", _("all these options are needed: -I <proxy> -p <proxy-port> -u <check-url> -S(sl) -j CONNECT -H <webserver>")); | 1935 | printf(" %s\n", |
1880 | printf (" %s\n", _("a STATE_OK will be returned. When the server returns its content but exceeds")); | 1936 | _("a STATE_OK is returned. When the certificate is still valid, but for less than")); |
1881 | printf (" %s\n", _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | 1937 | printf(" %s\n", _("30 days, but more than 14 days, a STATE_WARNING is returned.")); |
1882 | printf (" %s\n", _("a STATE_CRITICAL will be returned. By adding a colon to the method you can set the method used")); | 1938 | printf(" %s\n", |
1883 | printf (" %s\n", _("inside the proxied connection: -j CONNECT:POST")); | 1939 | _("A STATE_CRITICAL will be returned when certificate expires in less than 14 days")); |
1940 | |||
1941 | printf(" %s\n\n", "CHECK SSL WEBSERVER CONTENT VIA PROXY USING HTTP 1.1 CONNECT: "); | ||
1942 | printf(" %s\n", _("check_http -I 192.168.100.35 -p 80 -u https://www.verisign.com/ -S -j " | ||
1943 | "CONNECT -H www.verisign.com ")); | ||
1944 | printf(" %s\n", _("all these options are needed: -I <proxy> -p <proxy-port> -u <check-url> " | ||
1945 | "-S(sl) -j CONNECT -H <webserver>")); | ||
1946 | printf(" %s\n", | ||
1947 | _("a STATE_OK will be returned. When the server returns its content but exceeds")); | ||
1948 | printf(" %s\n", | ||
1949 | _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | ||
1950 | printf(" %s\n", _("a STATE_CRITICAL will be returned. By adding a colon to the method you can " | ||
1951 | "set the method used")); | ||
1952 | printf(" %s\n", _("inside the proxied connection: -j CONNECT:POST")); | ||
1884 | 1953 | ||
1885 | #endif | 1954 | #endif |
1886 | 1955 | ||
1887 | printf (UT_SUPPORT); | 1956 | printf(UT_SUPPORT); |
1888 | |||
1889 | } | 1957 | } |
1890 | 1958 | ||
1891 | 1959 | void print_usage(void) { | |
1892 | 1960 | printf("%s\n", _("Usage:")); | |
1893 | void | 1961 | printf(" %s -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]\n", progname); |
1894 | print_usage (void) | 1962 | printf(" [-J <client certificate file>] [-K <private key>]\n"); |
1895 | { | 1963 | printf(" [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L] [-E] [-a auth]\n"); |
1896 | printf ("%s\n", _("Usage:")); | 1964 | printf(" [-b proxy_auth] [-f <ok|warning|critical|follow|sticky|stickyport>]\n"); |
1897 | printf (" %s -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]\n",progname); | 1965 | printf(" [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive " |
1898 | printf (" [-J <client certificate file>] [-K <private key>]\n"); | 1966 | "regex>]\n"); |
1899 | printf (" [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L] [-E] [-a auth]\n"); | 1967 | printf(" [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]\n"); |
1900 | printf (" [-b proxy_auth] [-f <ok|warning|critical|follow|sticky|stickyport>]\n"); | 1968 | printf(" [-A string] [-k string] [-S <version>] [--sni]\n"); |
1901 | printf (" [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n"); | 1969 | printf(" [-T <content-type>] [-j method]\n"); |
1902 | printf (" [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]\n"); | 1970 | printf(" %s -H <vhost> | -I <IP-address> -C <warn_age>[,<crit_age>]\n", progname); |
1903 | printf (" [-A string] [-k string] [-S <version>] [--sni]\n"); | 1971 | printf(" [-p <port>] [-t <timeout>] [-4|-6] [--sni]\n"); |
1904 | printf (" [-T <content-type>] [-j method]\n"); | ||
1905 | printf (" %s -H <vhost> | -I <IP-address> -C <warn_age>[,<crit_age>]\n",progname); | ||
1906 | printf (" [-p <port>] [-t <timeout>] [-4|-6] [--sni]\n"); | ||
1907 | } | 1972 | } |
diff --git a/plugins/check_ide_smart.c b/plugins/check_ide_smart.c index 9640ef70..16fe3d01 100644 --- a/plugins/check_ide_smart.c +++ b/plugins/check_ide_smart.c | |||
@@ -56,7 +56,6 @@ void print_usage(void); | |||
56 | # include <sys/device.h> | 56 | # include <sys/device.h> |
57 | # include <sys/param.h> | 57 | # include <sys/param.h> |
58 | # include <sys/sysctl.h> | 58 | # include <sys/sysctl.h> |
59 | # include <sys/videoio.h> /* for __u8 and friends */ | ||
60 | # include <sys/scsiio.h> | 59 | # include <sys/scsiio.h> |
61 | # include <sys/ataio.h> | 60 | # include <sys/ataio.h> |
62 | # include <dev/ata/atareg.h> | 61 | # include <dev/ata/atareg.h> |
@@ -79,48 +78,47 @@ void print_usage(void); | |||
79 | #define UNKNOWN -1 | 78 | #define UNKNOWN -1 |
80 | 79 | ||
81 | typedef struct threshold_s { | 80 | typedef struct threshold_s { |
82 | __u8 id; | 81 | uint8_t id; |
83 | __u8 threshold; | 82 | uint8_t threshold; |
84 | __u8 reserved[10]; | 83 | uint8_t reserved[10]; |
85 | } __attribute__((packed)) threshold_t; | 84 | } __attribute__((packed)) threshold_t; |
86 | 85 | ||
87 | typedef struct thresholds_s { | 86 | typedef struct thresholds_s { |
88 | __u16 revision; | 87 | uint16_t revision; |
89 | threshold_t thresholds[NR_ATTRIBUTES]; | 88 | threshold_t thresholds[NR_ATTRIBUTES]; |
90 | __u8 reserved[18]; | 89 | uint8_t reserved[18]; |
91 | __u8 vendor[131]; | 90 | uint8_t vendor[131]; |
92 | __u8 checksum; | 91 | uint8_t checksum; |
93 | } __attribute__((packed)) thresholds_t; | 92 | } __attribute__((packed)) thresholds_t; |
94 | 93 | ||
95 | typedef struct value_s { | 94 | typedef struct value_s { |
96 | __u8 id; | 95 | uint8_t id; |
97 | __u16 status; | 96 | uint16_t status; |
98 | __u8 value; | 97 | uint8_t value; |
99 | __u8 vendor[8]; | 98 | uint8_t vendor[8]; |
100 | } __attribute__((packed)) value_t; | 99 | } __attribute__((packed)) value_t; |
101 | 100 | ||
102 | typedef struct values_s { | 101 | typedef struct values_s { |
103 | __u16 revision; | 102 | uint16_t revision; |
104 | value_t values[NR_ATTRIBUTES]; | 103 | value_t values[NR_ATTRIBUTES]; |
105 | __u8 offline_status; | 104 | uint8_t offline_status; |
106 | __u8 vendor1; | 105 | uint8_t vendor1; |
107 | __u16 offline_timeout; | 106 | uint16_t offline_timeout; |
108 | __u8 vendor2; | 107 | uint8_t vendor2; |
109 | __u8 offline_capability; | 108 | uint8_t offline_capability; |
110 | __u16 smart_capability; | 109 | uint16_t smart_capability; |
111 | __u8 reserved[16]; | 110 | uint8_t reserved[16]; |
112 | __u8 vendor[125]; | 111 | uint8_t vendor[125]; |
113 | __u8 checksum; | 112 | uint8_t checksum; |
114 | } __attribute__((packed)) values_t; | 113 | } __attribute__((packed)) values_t; |
115 | 114 | ||
116 | static struct { | 115 | static struct { |
117 | __u8 value; | 116 | uint8_t value; |
118 | char *text; | 117 | char *text; |
119 | } offline_status_text[] = {{0x00, "NeverStarted"}, {0x02, "Completed"}, {0x04, "Suspended"}, | 118 | } offline_status_text[] = {{0x00, "NeverStarted"}, {0x02, "Completed"}, {0x04, "Suspended"}, {0x05, "Aborted"}, {0x06, "Failed"}, {0, 0}}; |
120 | {0x05, "Aborted"}, {0x06, "Failed"}, {0, 0}}; | ||
121 | 119 | ||
122 | static struct { | 120 | static struct { |
123 | __u8 value; | 121 | uint8_t value; |
124 | char *text; | 122 | char *text; |
125 | } smart_command[] = {{SMART_ENABLE, "SMART_ENABLE"}, | 123 | } smart_command[] = {{SMART_ENABLE, "SMART_ENABLE"}, |
126 | {SMART_DISABLE, "SMART_DISABLE"}, | 124 | {SMART_DISABLE, "SMART_DISABLE"}, |
@@ -140,7 +138,7 @@ static int smart_read_values(int, values_t *); | |||
140 | static int nagios(values_t *, thresholds_t *); | 138 | static int nagios(values_t *, thresholds_t *); |
141 | static void print_value(value_t *, threshold_t *); | 139 | static void print_value(value_t *, threshold_t *); |
142 | static void print_values(values_t *, thresholds_t *); | 140 | static void print_values(values_t *, thresholds_t *); |
143 | static int smart_cmd_simple(int, enum SmartCommand, __u8, bool); | 141 | static int smart_cmd_simple(int, enum SmartCommand, uint8_t, bool); |
144 | static int smart_read_thresholds(int, thresholds_t *); | 142 | static int smart_read_thresholds(int, thresholds_t *); |
145 | static bool verbose = false; | 143 | static bool verbose = false; |
146 | 144 | ||
@@ -175,8 +173,9 @@ int main(int argc, char *argv[]) { | |||
175 | 173 | ||
176 | o = getopt_long(argc, argv, "+d:iq10nhVv", longopts, &longindex); | 174 | o = getopt_long(argc, argv, "+d:iq10nhVv", longopts, &longindex); |
177 | 175 | ||
178 | if (o == -1 || o == EOF || o == 1) | 176 | if (o == -1 || o == EOF || o == 1) { |
179 | break; | 177 | break; |
178 | } | ||
180 | 179 | ||
181 | switch (o) { | 180 | switch (o) { |
182 | case 'd': | 181 | case 'd': |
@@ -234,8 +233,9 @@ int main(int argc, char *argv[]) { | |||
234 | smart_read_values(fd, &values); | 233 | smart_read_values(fd, &values); |
235 | smart_read_thresholds(fd, &thresholds); | 234 | smart_read_thresholds(fd, &thresholds); |
236 | retval = nagios(&values, &thresholds); | 235 | retval = nagios(&values, &thresholds); |
237 | if (verbose) | 236 | if (verbose) { |
238 | print_values(&values, &thresholds); | 237 | print_values(&values, &thresholds); |
238 | } | ||
239 | 239 | ||
240 | close(fd); | 240 | close(fd); |
241 | return retval; | 241 | return retval; |
@@ -254,7 +254,7 @@ char *get_offline_text(int status) { | |||
254 | int smart_read_values(int fd, values_t *values) { | 254 | int smart_read_values(int fd, values_t *values) { |
255 | #ifdef __linux__ | 255 | #ifdef __linux__ |
256 | int e; | 256 | int e; |
257 | __u8 args[4 + 512]; | 257 | uint8_t args[4 + 512]; |
258 | args[0] = WIN_SMART; | 258 | args[0] = WIN_SMART; |
259 | args[1] = 0; | 259 | args[1] = 0; |
260 | args[2] = SMART_READ_VALUES; | 260 | args[2] = SMART_READ_VALUES; |
@@ -282,8 +282,9 @@ int smart_read_values(int fd, values_t *values) { | |||
282 | req.cylinder = WDSMART_CYL; | 282 | req.cylinder = WDSMART_CYL; |
283 | 283 | ||
284 | if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { | 284 | if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { |
285 | if (req.retsts != ATACMD_OK) | 285 | if (req.retsts != ATACMD_OK) { |
286 | errno = ENODEV; | 286 | errno = ENODEV; |
287 | } | ||
287 | } | 288 | } |
288 | 289 | ||
289 | if (errno != 0) { | 290 | if (errno != 0) { |
@@ -370,22 +371,24 @@ void print_values(values_t *p, thresholds_t *t) { | |||
370 | p->smart_capability & 1 ? "SaveOnStandBy" : "", p->smart_capability & 2 ? "AutoSave" : ""); | 371 | p->smart_capability & 1 ? "SaveOnStandBy" : "", p->smart_capability & 2 ? "AutoSave" : ""); |
371 | } | 372 | } |
372 | 373 | ||
373 | int smart_cmd_simple(int fd, enum SmartCommand command, __u8 val0, bool show_error) { | 374 | int smart_cmd_simple(int fd, enum SmartCommand command, uint8_t val0, bool show_error) { |
374 | int e = STATE_UNKNOWN; | 375 | int e = STATE_UNKNOWN; |
375 | #ifdef __linux__ | 376 | #ifdef __linux__ |
376 | __u8 args[4]; | 377 | uint8_t args[4]; |
377 | args[0] = WIN_SMART; | 378 | args[0] = WIN_SMART; |
378 | args[1] = val0; | 379 | args[1] = val0; |
379 | args[2] = smart_command[command].value; | 380 | args[2] = smart_command[command].value; |
380 | args[3] = 0; | 381 | args[3] = 0; |
381 | if (ioctl(fd, HDIO_DRIVE_CMD, &args)) { | 382 | if (ioctl(fd, HDIO_DRIVE_CMD, &args)) { |
382 | e = STATE_CRITICAL; | 383 | e = STATE_CRITICAL; |
383 | if (show_error) | 384 | if (show_error) { |
384 | printf(_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror(errno)); | 385 | printf(_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror(errno)); |
386 | } | ||
385 | } else { | 387 | } else { |
386 | e = STATE_OK; | 388 | e = STATE_OK; |
387 | if (show_error) | 389 | if (show_error) { |
388 | printf(_("OK - Command sent (%s)\n"), smart_command[command].text); | 390 | printf(_("OK - Command sent (%s)\n"), smart_command[command].text); |
391 | } | ||
389 | } | 392 | } |
390 | 393 | ||
391 | #endif /* __linux__ */ | 394 | #endif /* __linux__ */ |
@@ -401,20 +404,24 @@ int smart_cmd_simple(int fd, enum SmartCommand command, __u8 val0, bool show_err | |||
401 | req.sec_count = val0; | 404 | req.sec_count = val0; |
402 | 405 | ||
403 | if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { | 406 | if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { |
404 | if (req.retsts != ATACMD_OK) | 407 | if (req.retsts != ATACMD_OK) { |
405 | errno = ENODEV; | 408 | errno = ENODEV; |
406 | if (req.cylinder != WDSMART_CYL) | 409 | } |
410 | if (req.cylinder != WDSMART_CYL) { | ||
407 | errno = ENODEV; | 411 | errno = ENODEV; |
412 | } | ||
408 | } | 413 | } |
409 | 414 | ||
410 | if (errno != 0) { | 415 | if (errno != 0) { |
411 | e = STATE_CRITICAL; | 416 | e = STATE_CRITICAL; |
412 | if (show_error) | 417 | if (show_error) { |
413 | printf(_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror(errno)); | 418 | printf(_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror(errno)); |
419 | } | ||
414 | } else { | 420 | } else { |
415 | e = STATE_OK; | 421 | e = STATE_OK; |
416 | if (show_error) | 422 | if (show_error) { |
417 | printf(_("OK - Command sent (%s)\n"), smart_command[command].text); | 423 | printf(_("OK - Command sent (%s)\n"), smart_command[command].text); |
424 | } | ||
418 | } | 425 | } |
419 | 426 | ||
420 | #endif /* __NetBSD__ */ | 427 | #endif /* __NetBSD__ */ |
@@ -424,7 +431,7 @@ int smart_cmd_simple(int fd, enum SmartCommand command, __u8 val0, bool show_err | |||
424 | int smart_read_thresholds(int fd, thresholds_t *thresholds) { | 431 | int smart_read_thresholds(int fd, thresholds_t *thresholds) { |
425 | #ifdef __linux__ | 432 | #ifdef __linux__ |
426 | int e; | 433 | int e; |
427 | __u8 args[4 + 512]; | 434 | uint8_t args[4 + 512]; |
428 | args[0] = WIN_SMART; | 435 | args[0] = WIN_SMART; |
429 | args[1] = 0; | 436 | args[1] = 0; |
430 | args[2] = SMART_READ_THRESHOLDS; | 437 | args[2] = SMART_READ_THRESHOLDS; |
@@ -452,8 +459,9 @@ int smart_read_thresholds(int fd, thresholds_t *thresholds) { | |||
452 | req.cylinder = WDSMART_CYL; | 459 | req.cylinder = WDSMART_CYL; |
453 | 460 | ||
454 | if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { | 461 | if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { |
455 | if (req.retsts != ATACMD_OK) | 462 | if (req.retsts != ATACMD_OK) { |
456 | errno = ENODEV; | 463 | errno = ENODEV; |
464 | } | ||
457 | } | 465 | } |
458 | 466 | ||
459 | if (errno != 0) { | 467 | if (errno != 0) { |
diff --git a/plugins/check_ldap.c b/plugins/check_ldap.c index 597644bd..77a33304 100644 --- a/plugins/check_ldap.c +++ b/plugins/check_ldap.c | |||
@@ -108,7 +108,8 @@ int main(int argc, char *argv[]) { | |||
108 | 108 | ||
109 | #ifdef HAVE_LDAP_SET_OPTION | 109 | #ifdef HAVE_LDAP_SET_OPTION |
110 | /* set ldap options */ | 110 | /* set ldap options */ |
111 | if (ldap_set_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &config.ld_protocol) != LDAP_OPT_SUCCESS) { | 111 | if (ldap_set_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &config.ld_protocol) != |
112 | LDAP_OPT_SUCCESS) { | ||
112 | printf(_("Could not set protocol version %d\n"), config.ld_protocol); | 113 | printf(_("Could not set protocol version %d\n"), config.ld_protocol); |
113 | return STATE_CRITICAL; | 114 | return STATE_CRITICAL; |
114 | } | 115 | } |
@@ -135,7 +136,8 @@ int main(int argc, char *argv[]) { | |||
135 | } else if (config.starttls) { | 136 | } else if (config.starttls) { |
136 | #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S) | 137 | #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S) |
137 | /* ldap with startTLS: set option version */ | 138 | /* ldap with startTLS: set option version */ |
138 | if (ldap_get_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS) { | 139 | if (ldap_get_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &version) == |
140 | LDAP_OPT_SUCCESS) { | ||
139 | if (version < LDAP_VERSION3) { | 141 | if (version < LDAP_VERSION3) { |
140 | version = LDAP_VERSION3; | 142 | version = LDAP_VERSION3; |
141 | ldap_set_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &version); | 143 | ldap_set_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &version); |
@@ -156,7 +158,8 @@ int main(int argc, char *argv[]) { | |||
156 | } | 158 | } |
157 | 159 | ||
158 | /* bind to the ldap server */ | 160 | /* bind to the ldap server */ |
159 | if (ldap_bind_s(ldap_connection, config.ld_binddn, config.ld_passwd, LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) { | 161 | if (ldap_bind_s(ldap_connection, config.ld_binddn, config.ld_passwd, LDAP_AUTH_SIMPLE) != |
162 | LDAP_SUCCESS) { | ||
160 | if (verbose) { | 163 | if (verbose) { |
161 | ldap_perror(ldap_connection, "ldap_bind"); | 164 | ldap_perror(ldap_connection, "ldap_bind"); |
162 | } | 165 | } |
@@ -168,8 +171,10 @@ int main(int argc, char *argv[]) { | |||
168 | int num_entries = 0; | 171 | int num_entries = 0; |
169 | /* do a search of all objectclasses in the base dn */ | 172 | /* do a search of all objectclasses in the base dn */ |
170 | if (ldap_search_s(ldap_connection, config.ld_base, | 173 | if (ldap_search_s(ldap_connection, config.ld_base, |
171 | (config.crit_entries != NULL || config.warn_entries != NULL) ? LDAP_SCOPE_SUBTREE : LDAP_SCOPE_BASE, config.ld_attr, | 174 | (config.crit_entries != NULL || config.warn_entries != NULL) |
172 | NULL, 0, &result) != LDAP_SUCCESS) { | 175 | ? LDAP_SCOPE_SUBTREE |
176 | : LDAP_SCOPE_BASE, | ||
177 | config.ld_attr, NULL, 0, &result) != LDAP_SUCCESS) { | ||
173 | if (verbose) { | 178 | if (verbose) { |
174 | ldap_perror(ldap_connection, "ldap_search"); | 179 | ldap_perror(ldap_connection, "ldap_search"); |
175 | } | 180 | } |
@@ -215,14 +220,16 @@ int main(int argc, char *argv[]) { | |||
215 | 220 | ||
216 | /* print out the result */ | 221 | /* print out the result */ |
217 | if (config.crit_entries != NULL || config.warn_entries != NULL) { | 222 | if (config.crit_entries != NULL || config.warn_entries != NULL) { |
218 | printf(_("LDAP %s - found %d entries in %.3f seconds|%s %s\n"), state_text(status), num_entries, elapsed_time, | 223 | printf(_("LDAP %s - found %d entries in %.3f seconds|%s %s\n"), state_text(status), |
219 | fperfdata("time", elapsed_time, "s", config.warn_time_set, config.warn_time, config.crit_time_set, config.crit_time, true, 0, | 224 | num_entries, elapsed_time, |
220 | false, 0), | 225 | fperfdata("time", elapsed_time, "s", config.warn_time_set, config.warn_time, |
221 | sperfdata("entries", (double)num_entries, "", config.warn_entries, config.crit_entries, true, 0.0, false, 0.0)); | 226 | config.crit_time_set, config.crit_time, true, 0, false, 0), |
227 | sperfdata("entries", (double)num_entries, "", config.warn_entries, | ||
228 | config.crit_entries, true, 0.0, false, 0.0)); | ||
222 | } else { | 229 | } else { |
223 | printf(_("LDAP %s - %.3f seconds response time|%s\n"), state_text(status), elapsed_time, | 230 | printf(_("LDAP %s - %.3f seconds response time|%s\n"), state_text(status), elapsed_time, |
224 | fperfdata("time", elapsed_time, "s", config.warn_time_set, config.warn_time, config.crit_time_set, config.crit_time, true, 0, | 231 | fperfdata("time", elapsed_time, "s", config.warn_time_set, config.warn_time, |
225 | false, 0)); | 232 | config.crit_time_set, config.crit_time, true, 0, false, 0)); |
226 | } | 233 | } |
227 | 234 | ||
228 | exit(status); | 235 | exit(status); |
@@ -273,7 +280,8 @@ check_ldap_config_wrapper process_arguments(int argc, char **argv) { | |||
273 | 280 | ||
274 | int option = 0; | 281 | int option = 0; |
275 | while (true) { | 282 | while (true) { |
276 | int option_index = getopt_long(argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:C:W:", longopts, &option); | 283 | int option_index = |
284 | getopt_long(argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:C:W:", longopts, &option); | ||
277 | 285 | ||
278 | if (option_index == -1 || option_index == EOF) { | 286 | if (option_index == -1 || option_index == EOF) { |
279 | break; | 287 | break; |
@@ -381,7 +389,8 @@ check_ldap_config_wrapper process_arguments(int argc, char **argv) { | |||
381 | result.config.ld_port = DEFAULT_PORT; | 389 | result.config.ld_port = DEFAULT_PORT; |
382 | } | 390 | } |
383 | 391 | ||
384 | if (strstr(argv[0], "check_ldaps") && !result.config.starttls && !result.config.ssl_on_connect) { | 392 | if (strstr(argv[0], "check_ldaps") && !result.config.starttls && |
393 | !result.config.ssl_on_connect) { | ||
385 | result.config.starttls = true; | 394 | result.config.starttls = true; |
386 | } | 395 | } |
387 | 396 | ||
@@ -398,7 +407,8 @@ check_ldap_config_wrapper validate_arguments(check_ldap_config_wrapper config_wr | |||
398 | } | 407 | } |
399 | 408 | ||
400 | if (config_wrapper.config.crit_entries != NULL || config_wrapper.config.warn_entries != NULL) { | 409 | if (config_wrapper.config.crit_entries != NULL || config_wrapper.config.warn_entries != NULL) { |
401 | set_thresholds(&config_wrapper.config.entries_thresholds, config_wrapper.config.warn_entries, config_wrapper.config.crit_entries); | 410 | set_thresholds(&config_wrapper.config.entries_thresholds, |
411 | config_wrapper.config.warn_entries, config_wrapper.config.crit_entries); | ||
402 | } | 412 | } |
403 | 413 | ||
404 | if (config_wrapper.config.ld_passwd == NULL) { | 414 | if (config_wrapper.config.ld_passwd == NULL) { |
@@ -435,11 +445,13 @@ void print_help(void) { | |||
435 | printf(" %s\n", "-D [--bind]"); | 445 | printf(" %s\n", "-D [--bind]"); |
436 | printf(" %s\n", _("ldap bind DN (if required)")); | 446 | printf(" %s\n", _("ldap bind DN (if required)")); |
437 | printf(" %s\n", "-P [--pass]"); | 447 | printf(" %s\n", "-P [--pass]"); |
438 | printf(" %s\n", _("ldap password (if required, or set the password through environment variable 'LDAP_PASSWORD')")); | 448 | printf(" %s\n", _("ldap password (if required, or set the password through environment " |
449 | "variable 'LDAP_PASSWORD')")); | ||
439 | printf(" %s\n", "-T [--starttls]"); | 450 | printf(" %s\n", "-T [--starttls]"); |
440 | printf(" %s\n", _("use starttls mechanism introduced in protocol version 3")); | 451 | printf(" %s\n", _("use starttls mechanism introduced in protocol version 3")); |
441 | printf(" %s\n", "-S [--ssl]"); | 452 | printf(" %s\n", "-S [--ssl]"); |
442 | printf(" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT); | 453 | printf(" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), |
454 | LDAPS_PORT); | ||
443 | 455 | ||
444 | #ifdef HAVE_LDAP_SET_OPTION | 456 | #ifdef HAVE_LDAP_SET_OPTION |
445 | printf(" %s\n", "-2 [--ver2]"); | 457 | printf(" %s\n", "-2 [--ver2]"); |
@@ -463,9 +475,11 @@ void print_help(void) { | |||
463 | printf("\n"); | 475 | printf("\n"); |
464 | printf("%s\n", _("Notes:")); | 476 | printf("%s\n", _("Notes:")); |
465 | printf(" %s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be")); | 477 | printf(" %s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be")); |
466 | printf(_(" implied (using default port %i) unless --port=636 is specified. In that case\n"), DEFAULT_PORT); | 478 | printf(_(" implied (using default port %i) unless --port=636 is specified. In that case\n"), |
479 | DEFAULT_PORT); | ||
467 | printf(" %s\n", _("'SSL on connect' will be used no matter how the plugin was called.")); | 480 | printf(" %s\n", _("'SSL on connect' will be used no matter how the plugin was called.")); |
468 | printf(" %s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags")); | 481 | printf(" %s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' " |
482 | "or '--ssl' flags")); | ||
469 | printf(" %s\n", _("to define the behaviour explicitly instead.")); | 483 | printf(" %s\n", _("to define the behaviour explicitly instead.")); |
470 | printf(" %s\n", _("The parameters --warn-entries and --crit-entries are optional.")); | 484 | printf(" %s\n", _("The parameters --warn-entries and --crit-entries are optional.")); |
471 | 485 | ||
diff --git a/plugins/check_load.c b/plugins/check_load.c index 1431d130..644cd604 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c | |||
@@ -1,350 +1,430 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_load plugin | 3 | * Monitoring check_load plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999-2007 Monitoring Plugins Development Team | 6 | * Copyright (c) 1999-2007 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains the check_load plugin | 10 | * This file contains the check_load plugin |
11 | * | 11 | * |
12 | * This plugin tests the current system load average. | 12 | * This plugin tests the current system load average. |
13 | * | 13 | * |
14 | * | 14 | * |
15 | * This program is free software: you can redistribute it and/or modify | 15 | * This program is free software: you can redistribute it and/or modify |
16 | * it under the terms of the GNU General Public License as published by | 16 | * it under the terms of the GNU General Public License as published by |
17 | * the Free Software Foundation, either version 3 of the License, or | 17 | * the Free Software Foundation, either version 3 of the License, or |
18 | * (at your option) any later version. | 18 | * (at your option) any later version. |
19 | * | 19 | * |
20 | * This program is distributed in the hope that it will be useful, | 20 | * This program is distributed in the hope that it will be useful, |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | * GNU General Public License for more details. | 23 | * GNU General Public License for more details. |
24 | * | 24 | * |
25 | * You should have received a copy of the GNU General Public License | 25 | * You should have received a copy of the GNU General Public License |
26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | * | 27 | * |
28 | * | 28 | * |
29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
30 | 30 | ||
31 | const char *progname = "check_load"; | 31 | const char *progname = "check_load"; |
32 | const char *copyright = "1999-2022"; | 32 | const char *copyright = "1999-2022"; |
33 | const char *email = "devel@monitoring-plugins.org"; | 33 | const char *email = "devel@monitoring-plugins.org"; |
34 | 34 | ||
35 | #include "./common.h" | 35 | #include "./common.h" |
36 | #include <string.h> | ||
36 | #include "./runcmd.h" | 37 | #include "./runcmd.h" |
37 | #include "./utils.h" | 38 | #include "./utils.h" |
38 | #include "./popen.h" | 39 | #include "./popen.h" |
40 | #include "../lib/states.h" | ||
41 | #include "../lib/output.h" | ||
42 | #include "../lib/perfdata.h" | ||
43 | #include "../lib/thresholds.h" | ||
44 | #include "check_load.d/config.h" | ||
39 | 45 | ||
40 | #include <string.h> | 46 | // getloadavg comes from gnulib |
41 | 47 | #include "../gl/stdlib.h" | |
42 | #ifdef HAVE_SYS_LOADAVG_H | ||
43 | #include <sys/loadavg.h> | ||
44 | #endif | ||
45 | 48 | ||
46 | /* needed for compilation under NetBSD, as suggested by Andy Doran */ | 49 | /* needed for compilation under NetBSD, as suggested by Andy Doran */ |
47 | #ifndef LOADAVG_1MIN | 50 | #ifndef LOADAVG_1MIN |
48 | #define LOADAVG_1MIN 0 | 51 | # define LOADAVG_1MIN 0 |
49 | #define LOADAVG_5MIN 1 | 52 | # define LOADAVG_5MIN 1 |
50 | #define LOADAVG_15MIN 2 | 53 | # define LOADAVG_15MIN 2 |
51 | #endif /* !defined LOADAVG_1MIN */ | 54 | #endif /* !defined LOADAVG_1MIN */ |
52 | 55 | ||
56 | typedef struct { | ||
57 | int errorcode; | ||
58 | check_load_config config; | ||
59 | } check_load_config_wrapper; | ||
60 | static check_load_config_wrapper process_arguments(int argc, char **argv); | ||
61 | |||
62 | void print_help(void); | ||
63 | void print_usage(void); | ||
64 | typedef struct { | ||
65 | int errorcode; | ||
66 | char **top_processes; | ||
67 | } top_processes_result; | ||
68 | static top_processes_result print_top_consuming_processes(unsigned long n_procs_to_show); | ||
69 | |||
70 | typedef struct { | ||
71 | mp_range load[3]; | ||
72 | } parsed_thresholds; | ||
73 | static parsed_thresholds get_threshold(char *arg) { | ||
74 | size_t index; | ||
75 | char *str = arg; | ||
76 | char *tmp_pointer; | ||
77 | bool valid = false; | ||
78 | |||
79 | parsed_thresholds result = { | ||
80 | .load = | ||
81 | { | ||
82 | mp_range_init(), | ||
83 | mp_range_init(), | ||
84 | mp_range_init(), | ||
85 | }, | ||
86 | }; | ||
53 | 87 | ||
54 | static int process_arguments (int argc, char **argv); | 88 | size_t arg_length = strlen(arg); |
55 | static int validate_arguments (void); | 89 | for (index = 0; index < 3; index++) { |
56 | void print_help (void); | 90 | double tmp = strtod(str, &tmp_pointer); |
57 | void print_usage (void); | 91 | if (tmp_pointer == str) { |
58 | static int print_top_consuming_processes(); | 92 | break; |
59 | 93 | } | |
60 | static int n_procs_to_show = 0; | ||
61 | |||
62 | /* strictly for pretty-print usage in loops */ | ||
63 | static const int nums[3] = { 1, 5, 15 }; | ||
64 | |||
65 | /* provide some fairly sane defaults */ | ||
66 | double wload[3] = { 0.0, 0.0, 0.0 }; | ||
67 | double cload[3] = { 0.0, 0.0, 0.0 }; | ||
68 | #define la1 la[0] | ||
69 | #define la5 la[1] | ||
70 | #define la15 la[2] | ||
71 | |||
72 | char *status_line; | ||
73 | bool take_into_account_cpus = false; | ||
74 | |||
75 | static void | ||
76 | get_threshold(char *arg, double *th) | ||
77 | { | ||
78 | size_t i, n; | ||
79 | int valid = 0; | ||
80 | char *str = arg, *p; | ||
81 | 94 | ||
82 | n = strlen(arg); | 95 | result.load[index] = mp_range_set_end(result.load[index], mp_create_pd_value(tmp)); |
83 | for(i = 0; i < 3; i++) { | ||
84 | th[i] = strtod(str, &p); | ||
85 | if(p == str) break; | ||
86 | 96 | ||
87 | valid = 1; | 97 | valid = true; |
88 | str = p + 1; | 98 | str = tmp_pointer + 1; |
89 | if(n <= (size_t)(str - arg)) break; | 99 | if (arg_length <= (size_t)(str - arg)) { |
100 | break; | ||
101 | } | ||
90 | } | 102 | } |
91 | 103 | ||
92 | /* empty argument or non-floatish, so warn about it and die */ | 104 | /* empty argument or non-floatish, so warn about it and die */ |
93 | if(!i && !valid) usage (_("Warning threshold must be float or float triplet!\n")); | 105 | if (!index && !valid) { |
106 | usage(_("Warning threshold must be float or float triplet!\n")); | ||
107 | } | ||
94 | 108 | ||
95 | if(i != 2) { | 109 | if (index != 2) { |
96 | /* one or more numbers were given, so fill array with last | 110 | /* one or more numbers were given, so fill array with last |
97 | * we got (most likely to NOT produce the least expected result) */ | 111 | * we got (most likely to NOT produce the least expected result) */ |
98 | for(n = i; n < 3; n++) th[n] = th[i]; | 112 | for (size_t tmp_index = index; tmp_index < 3; tmp_index++) { |
113 | result.load[tmp_index] = result.load[index]; | ||
114 | } | ||
99 | } | 115 | } |
116 | return result; | ||
100 | } | 117 | } |
101 | 118 | ||
102 | 119 | int main(int argc, char **argv) { | |
103 | int | 120 | setlocale(LC_ALL, ""); |
104 | main (int argc, char **argv) | 121 | bindtextdomain(PACKAGE, LOCALEDIR); |
105 | { | 122 | textdomain(PACKAGE); |
106 | int result = -1; | ||
107 | int i; | ||
108 | long numcpus; | ||
109 | |||
110 | double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about uninitialized arrays */ | ||
111 | #ifndef HAVE_GETLOADAVG | ||
112 | char input_buffer[MAX_INPUT_BUFFER]; | ||
113 | #endif | ||
114 | |||
115 | setlocale (LC_ALL, ""); | ||
116 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
117 | textdomain (PACKAGE); | ||
118 | setlocale(LC_NUMERIC, "POSIX"); | 123 | setlocale(LC_NUMERIC, "POSIX"); |
119 | 124 | ||
120 | /* Parse extra opts if any */ | 125 | /* Parse extra opts if any */ |
121 | argv = np_extra_opts (&argc, argv, progname); | 126 | argv = np_extra_opts(&argc, argv, progname); |
122 | 127 | ||
123 | if (process_arguments (argc, argv) == ERROR) | 128 | check_load_config_wrapper tmp_config = process_arguments(argc, argv); |
124 | usage4 (_("Could not parse arguments")); | 129 | if (tmp_config.errorcode == ERROR) { |
125 | 130 | usage4(_("Could not parse arguments")); | |
126 | #ifdef HAVE_GETLOADAVG | ||
127 | result = getloadavg (la, 3); | ||
128 | if (result != 3) | ||
129 | return STATE_UNKNOWN; | ||
130 | #else | ||
131 | child_process = spopen (PATH_TO_UPTIME); | ||
132 | if (child_process == NULL) { | ||
133 | printf (_("Error opening %s\n"), PATH_TO_UPTIME); | ||
134 | return STATE_UNKNOWN; | ||
135 | } | ||
136 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
137 | if (child_stderr == NULL) { | ||
138 | printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME); | ||
139 | } | ||
140 | fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
141 | if(strstr(input_buffer, "load average:")) { | ||
142 | sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15); | ||
143 | } | ||
144 | else if(strstr(input_buffer, "load averages:")) { | ||
145 | sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); | ||
146 | } | ||
147 | else { | ||
148 | printf (_("could not parse load from uptime %s: %d\n"), PATH_TO_UPTIME, result); | ||
149 | return STATE_UNKNOWN; | ||
150 | } | ||
151 | |||
152 | result = spclose (child_process); | ||
153 | if (result) { | ||
154 | printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME); | ||
155 | return STATE_UNKNOWN; | ||
156 | } | ||
157 | #endif | ||
158 | |||
159 | if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) { | ||
160 | #ifdef HAVE_GETLOADAVG | ||
161 | printf (_("Error in getloadavg()\n")); | ||
162 | #else | ||
163 | printf (_("Error processing %s\n"), PATH_TO_UPTIME); | ||
164 | #endif | ||
165 | return STATE_UNKNOWN; | ||
166 | } | 131 | } |
167 | 132 | ||
168 | /* we got this far, so assume OK until we've measured */ | 133 | const check_load_config config = tmp_config.config; |
169 | result = STATE_OK; | 134 | |
135 | double load_values[3] = {0, 0, 0}; | ||
170 | 136 | ||
171 | xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15); | 137 | // this should be getloadavg from gnulib, should work everywhereâ„¢ |
172 | xasprintf(&status_line, ("total %s"), status_line); | 138 | int error = getloadavg(load_values, 3); |
139 | if (error != 3) { | ||
140 | die(STATE_UNKNOWN, _("Failed to retrieve load values")); | ||
141 | } | ||
173 | 142 | ||
143 | mp_check overall = mp_check_init(); | ||
144 | if (config.output_format_set) { | ||
145 | mp_set_format(config.output_format); | ||
146 | } | ||
174 | 147 | ||
175 | double scaled_la[3] = { 0.0, 0.0, 0.0 }; | ||
176 | bool is_using_scaled_load_values = false; | 148 | bool is_using_scaled_load_values = false; |
177 | 149 | long numcpus; | |
178 | if (take_into_account_cpus == true && (numcpus = GET_NUMBER_OF_CPUS()) > 0) { | 150 | if (config.take_into_account_cpus && ((numcpus = GET_NUMBER_OF_CPUS()) > 0)) { |
179 | is_using_scaled_load_values = true; | 151 | is_using_scaled_load_values = true; |
180 | 152 | ||
181 | scaled_la[0] = la[0] / numcpus; | 153 | double scaled_la[3] = { |
182 | scaled_la[1] = la[1] / numcpus; | 154 | load_values[0] / numcpus, |
183 | scaled_la[2] = la[2] / numcpus; | 155 | load_values[1] / numcpus, |
156 | load_values[2] / numcpus, | ||
157 | }; | ||
158 | |||
159 | mp_subcheck scaled_load_sc = mp_subcheck_init(); | ||
160 | scaled_load_sc = mp_set_subcheck_default_state(scaled_load_sc, STATE_OK); | ||
161 | scaled_load_sc.output = "Scaled Load (divided by number of CPUs"; | ||
162 | |||
163 | mp_perfdata pd_scaled_load1 = perfdata_init(); | ||
164 | pd_scaled_load1.label = "scaled_load1"; | ||
165 | pd_scaled_load1 = mp_set_pd_value(pd_scaled_load1, scaled_la[0]); | ||
166 | pd_scaled_load1 = mp_pd_set_thresholds(pd_scaled_load1, config.th_load[0]); | ||
167 | |||
168 | mp_subcheck scaled_load_sc1 = mp_subcheck_init(); | ||
169 | scaled_load_sc1 = mp_set_subcheck_state(scaled_load_sc1, mp_get_pd_status(pd_scaled_load1)); | ||
170 | mp_add_perfdata_to_subcheck(&scaled_load_sc1, pd_scaled_load1); | ||
171 | xasprintf(&scaled_load_sc1.output, "1 Minute: %s", | ||
172 | pd_value_to_string(pd_scaled_load1.value)); | ||
173 | mp_add_subcheck_to_subcheck(&scaled_load_sc, scaled_load_sc1); | ||
174 | |||
175 | mp_perfdata pd_scaled_load5 = perfdata_init(); | ||
176 | pd_scaled_load5.label = "scaled_load5"; | ||
177 | pd_scaled_load5 = mp_set_pd_value(pd_scaled_load5, scaled_la[1]); | ||
178 | pd_scaled_load5 = mp_pd_set_thresholds(pd_scaled_load5, config.th_load[1]); | ||
179 | |||
180 | mp_subcheck scaled_load_sc5 = mp_subcheck_init(); | ||
181 | scaled_load_sc5 = mp_set_subcheck_state(scaled_load_sc5, mp_get_pd_status(pd_scaled_load5)); | ||
182 | mp_add_perfdata_to_subcheck(&scaled_load_sc5, pd_scaled_load5); | ||
183 | xasprintf(&scaled_load_sc5.output, "5 Minutes: %s", | ||
184 | pd_value_to_string(pd_scaled_load5.value)); | ||
185 | mp_add_subcheck_to_subcheck(&scaled_load_sc, scaled_load_sc5); | ||
186 | |||
187 | mp_perfdata pd_scaled_load15 = perfdata_init(); | ||
188 | pd_scaled_load15.label = "scaled_load15"; | ||
189 | pd_scaled_load15 = mp_set_pd_value(pd_scaled_load15, scaled_la[2]); | ||
190 | pd_scaled_load15 = mp_pd_set_thresholds(pd_scaled_load15, config.th_load[2]); | ||
191 | |||
192 | mp_subcheck scaled_load_sc15 = mp_subcheck_init(); | ||
193 | scaled_load_sc15 = | ||
194 | mp_set_subcheck_state(scaled_load_sc15, mp_get_pd_status(pd_scaled_load15)); | ||
195 | mp_add_perfdata_to_subcheck(&scaled_load_sc15, pd_scaled_load15); | ||
196 | xasprintf(&scaled_load_sc15.output, "15 Minutes: %s", | ||
197 | pd_value_to_string(pd_scaled_load15.value)); | ||
198 | mp_add_subcheck_to_subcheck(&scaled_load_sc, scaled_load_sc15); | ||
199 | |||
200 | mp_add_subcheck_to_check(&overall, scaled_load_sc); | ||
201 | } | ||
202 | |||
203 | mp_subcheck load_sc = mp_subcheck_init(); | ||
204 | load_sc = mp_set_subcheck_default_state(load_sc, STATE_OK); | ||
205 | load_sc.output = "Total Load"; | ||
184 | 206 | ||
185 | char *tmp = NULL; | 207 | mp_perfdata pd_load1 = perfdata_init(); |
186 | xasprintf(&tmp, _("load average: %.2f, %.2f, %.2f"), scaled_la[0], scaled_la[1], scaled_la[2]); | 208 | pd_load1.label = "load1"; |
187 | xasprintf(&status_line, "scaled %s - %s", tmp, status_line); | 209 | pd_load1 = mp_set_pd_value(pd_load1, load_values[0]); |
210 | if (!is_using_scaled_load_values) { | ||
211 | pd_load1 = mp_pd_set_thresholds(pd_load1, config.th_load[0]); | ||
188 | } | 212 | } |
189 | 213 | ||
190 | for(i = 0; i < 3; i++) { | 214 | mp_subcheck load_sc1 = mp_subcheck_init(); |
191 | if (is_using_scaled_load_values) { | 215 | load_sc1 = mp_set_subcheck_state(load_sc1, mp_get_pd_status(pd_load1)); |
192 | if(scaled_la[i] > cload[i]) { | 216 | mp_add_perfdata_to_subcheck(&load_sc1, pd_load1); |
193 | result = STATE_CRITICAL; | 217 | xasprintf(&load_sc1.output, "1 Minute: %s", pd_value_to_string(pd_load1.value)); |
194 | break; | 218 | mp_add_subcheck_to_subcheck(&load_sc, load_sc1); |
195 | } | 219 | |
196 | else if(scaled_la[i] > wload[i]) result = STATE_WARNING; | 220 | mp_perfdata pd_load5 = perfdata_init(); |
197 | } else { | 221 | pd_load5.label = "load5"; |
198 | if(la[i] > cload[i]) { | 222 | pd_load5 = mp_set_pd_value(pd_load5, load_values[1]); |
199 | result = STATE_CRITICAL; | 223 | if (!is_using_scaled_load_values) { |
200 | break; | 224 | pd_load5 = mp_pd_set_thresholds(pd_load5, config.th_load[1]); |
201 | } | ||
202 | else if(la[i] > wload[i]) result = STATE_WARNING; | ||
203 | } | ||
204 | } | 225 | } |
205 | 226 | ||
206 | printf("LOAD %s - %s|", state_text(result), status_line); | 227 | mp_subcheck load_sc5 = mp_subcheck_init(); |
207 | for(i = 0; i < 3; i++) { | 228 | load_sc5 = mp_set_subcheck_state(load_sc5, mp_get_pd_status(pd_load5)); |
208 | if (is_using_scaled_load_values) { | 229 | mp_add_perfdata_to_subcheck(&load_sc5, pd_load5); |
209 | printf("load%d=%.3f;;;0; ", nums[i], la[i]); | 230 | xasprintf(&load_sc5.output, "5 Minutes: %s", pd_value_to_string(pd_load5.value)); |
210 | printf("scaled_load%d=%.3f;%.3f;%.3f;0; ", nums[i], scaled_la[i], wload[i], cload[i]); | 231 | mp_add_subcheck_to_subcheck(&load_sc, load_sc5); |
211 | } else { | 232 | |
212 | printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); | 233 | mp_perfdata pd_load15 = perfdata_init(); |
213 | } | 234 | pd_load15.label = "load15"; |
235 | pd_load15 = mp_set_pd_value(pd_load15, load_values[2]); | ||
236 | if (!is_using_scaled_load_values) { | ||
237 | pd_load15 = mp_pd_set_thresholds(pd_load15, config.th_load[2]); | ||
214 | } | 238 | } |
215 | 239 | ||
216 | putchar('\n'); | 240 | mp_subcheck load_sc15 = mp_subcheck_init(); |
217 | if (n_procs_to_show > 0) { | 241 | load_sc15 = mp_set_subcheck_state(load_sc15, mp_get_pd_status(pd_load15)); |
218 | print_top_consuming_processes(); | 242 | mp_add_perfdata_to_subcheck(&load_sc15, pd_load15); |
243 | xasprintf(&load_sc15.output, "15 Minutes: %s", pd_value_to_string(pd_load15.value)); | ||
244 | mp_add_subcheck_to_subcheck(&load_sc, load_sc15); | ||
245 | |||
246 | mp_add_subcheck_to_check(&overall, load_sc); | ||
247 | |||
248 | if (config.n_procs_to_show > 0) { | ||
249 | mp_subcheck top_proc_sc = mp_subcheck_init(); | ||
250 | top_proc_sc = mp_set_subcheck_state(top_proc_sc, STATE_OK); | ||
251 | top_processes_result top_proc = print_top_consuming_processes(config.n_procs_to_show); | ||
252 | xasprintf(&top_proc_sc.output, "Top %lu CPU time consuming processes", | ||
253 | config.n_procs_to_show); | ||
254 | |||
255 | if (top_proc.errorcode == OK) { | ||
256 | for (unsigned long i = 0; i < config.n_procs_to_show; i++) { | ||
257 | xasprintf(&top_proc_sc.output, "%s\n%s", top_proc_sc.output, | ||
258 | top_proc.top_processes[i]); | ||
259 | } | ||
260 | } | ||
261 | |||
262 | mp_add_subcheck_to_check(&overall, top_proc_sc); | ||
219 | } | 263 | } |
220 | return result; | ||
221 | } | ||
222 | 264 | ||
265 | mp_exit(overall); | ||
266 | } | ||
223 | 267 | ||
224 | /* process command-line arguments */ | 268 | /* process command-line arguments */ |
225 | static int | 269 | static check_load_config_wrapper process_arguments(int argc, char **argv) { |
226 | process_arguments (int argc, char **argv) | 270 | |
227 | { | 271 | enum { |
228 | int c = 0; | 272 | output_format_index = CHAR_MAX + 1, |
229 | |||
230 | int option = 0; | ||
231 | static struct option longopts[] = { | ||
232 | {"warning", required_argument, 0, 'w'}, | ||
233 | {"critical", required_argument, 0, 'c'}, | ||
234 | {"percpu", no_argument, 0, 'r'}, | ||
235 | {"version", no_argument, 0, 'V'}, | ||
236 | {"help", no_argument, 0, 'h'}, | ||
237 | {"procs-to-show", required_argument, 0, 'n'}, | ||
238 | {0, 0, 0, 0} | ||
239 | }; | 273 | }; |
240 | 274 | ||
241 | if (argc < 2) | 275 | static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, |
242 | return ERROR; | 276 | {"critical", required_argument, 0, 'c'}, |
277 | {"percpu", no_argument, 0, 'r'}, | ||
278 | {"version", no_argument, 0, 'V'}, | ||
279 | {"help", no_argument, 0, 'h'}, | ||
280 | {"procs-to-show", required_argument, 0, 'n'}, | ||
281 | {"output-format", required_argument, 0, output_format_index}, | ||
282 | {0, 0, 0, 0}}; | ||
283 | |||
284 | check_load_config_wrapper result = { | ||
285 | .errorcode = OK, | ||
286 | .config = check_load_config_init(), | ||
287 | }; | ||
243 | 288 | ||
244 | while (1) { | 289 | if (argc < 2) { |
245 | c = getopt_long (argc, argv, "Vhrc:w:n:", longopts, &option); | 290 | result.errorcode = ERROR; |
291 | return result; | ||
292 | } | ||
246 | 293 | ||
247 | if (c == -1 || c == EOF) | 294 | while (true) { |
248 | break; | 295 | int option = 0; |
296 | int option_index = getopt_long(argc, argv, "Vhrc:w:n:", longopts, &option); | ||
249 | 297 | ||
250 | switch (c) { | 298 | if (option_index == -1 || option_index == EOF) { |
251 | case 'w': /* warning time threshold */ | ||
252 | get_threshold(optarg, wload); | ||
253 | break; | 299 | break; |
254 | case 'c': /* critical time threshold */ | 300 | } |
255 | get_threshold(optarg, cload); | 301 | |
302 | switch (option_index) { | ||
303 | case output_format_index: { | ||
304 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
305 | if (!parser.parsing_success) { | ||
306 | printf("Invalid output format: %s\n", optarg); | ||
307 | exit(STATE_UNKNOWN); | ||
308 | } | ||
309 | |||
310 | result.config.output_format_set = true; | ||
311 | result.config.output_format = parser.output_format; | ||
256 | break; | 312 | break; |
313 | } | ||
314 | case 'w': /* warning time threshold */ { | ||
315 | parsed_thresholds warning_range = get_threshold(optarg); | ||
316 | result.config.th_load[0].warning = warning_range.load[0]; | ||
317 | result.config.th_load[0].warning_is_set = true; | ||
318 | |||
319 | result.config.th_load[1].warning = warning_range.load[1]; | ||
320 | result.config.th_load[1].warning_is_set = true; | ||
321 | |||
322 | result.config.th_load[2].warning = warning_range.load[2]; | ||
323 | result.config.th_load[2].warning_is_set = true; | ||
324 | } break; | ||
325 | case 'c': /* critical time threshold */ { | ||
326 | parsed_thresholds critical_range = get_threshold(optarg); | ||
327 | result.config.th_load[0].critical = critical_range.load[0]; | ||
328 | result.config.th_load[0].critical_is_set = true; | ||
329 | |||
330 | result.config.th_load[1].critical = critical_range.load[1]; | ||
331 | result.config.th_load[1].critical_is_set = true; | ||
332 | |||
333 | result.config.th_load[2].critical = critical_range.load[2]; | ||
334 | result.config.th_load[2].critical_is_set = true; | ||
335 | } break; | ||
257 | case 'r': /* Divide load average by number of CPUs */ | 336 | case 'r': /* Divide load average by number of CPUs */ |
258 | take_into_account_cpus = true; | 337 | result.config.take_into_account_cpus = true; |
259 | break; | 338 | break; |
260 | case 'V': /* version */ | 339 | case 'V': /* version */ |
261 | print_revision (progname, NP_VERSION); | 340 | print_revision(progname, NP_VERSION); |
262 | exit (STATE_UNKNOWN); | 341 | exit(STATE_UNKNOWN); |
263 | case 'h': /* help */ | 342 | case 'h': /* help */ |
264 | print_help (); | 343 | print_help(); |
265 | exit (STATE_UNKNOWN); | 344 | exit(STATE_UNKNOWN); |
266 | case 'n': | 345 | case 'n': |
267 | n_procs_to_show = atoi(optarg); | 346 | result.config.n_procs_to_show = (unsigned long)atol(optarg); |
268 | break; | 347 | break; |
269 | case '?': /* help */ | 348 | case '?': /* help */ |
270 | usage5 (); | 349 | usage5(); |
271 | } | 350 | } |
272 | } | 351 | } |
273 | 352 | ||
274 | c = optind; | 353 | int index = optind; |
275 | if (c == argc) | 354 | if (index == argc) { |
276 | return validate_arguments (); | 355 | return result; |
356 | } | ||
277 | 357 | ||
278 | /* handle the case if both arguments are missing, | 358 | /* handle the case if both arguments are missing, |
279 | * but not if only one is given without -c or -w flag */ | 359 | * but not if only one is given without -c or -w flag */ |
280 | if(c - argc == 2) { | 360 | if (index - argc == 2) { |
281 | get_threshold(argv[c++], wload); | 361 | parsed_thresholds warning_range = get_threshold(argv[index++]); |
282 | get_threshold(argv[c++], cload); | 362 | result.config.th_load[0].warning = warning_range.load[0]; |
283 | } | 363 | result.config.th_load[0].warning_is_set = true; |
284 | else if(c - argc == 1) { | 364 | |
285 | get_threshold(argv[c++], cload); | 365 | result.config.th_load[1].warning = warning_range.load[1]; |
286 | } | 366 | result.config.th_load[1].warning_is_set = true; |
287 | 367 | ||
288 | return validate_arguments (); | 368 | result.config.th_load[2].warning = warning_range.load[2]; |
289 | } | 369 | result.config.th_load[2].warning_is_set = true; |
290 | 370 | parsed_thresholds critical_range = get_threshold(argv[index++]); | |
291 | 371 | result.config.th_load[0].critical = critical_range.load[0]; | |
292 | static int | 372 | result.config.th_load[0].critical_is_set = true; |
293 | validate_arguments (void) | 373 | |
294 | { | 374 | result.config.th_load[1].critical = critical_range.load[1]; |
295 | int i = 0; | 375 | result.config.th_load[1].critical_is_set = true; |
296 | 376 | ||
297 | /* match cload first, as it will give the most friendly error message | 377 | result.config.th_load[2].critical = critical_range.load[2]; |
298 | * if user hasn't given the -c switch properly */ | 378 | result.config.th_load[2].critical_is_set = true; |
299 | for(i = 0; i < 3; i++) { | 379 | } else if (index - argc == 1) { |
300 | if(cload[i] < 0) | 380 | parsed_thresholds critical_range = get_threshold(argv[index++]); |
301 | die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]); | 381 | result.config.th_load[0].critical = critical_range.load[0]; |
302 | if(wload[i] < 0) | 382 | result.config.th_load[0].critical_is_set = true; |
303 | die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]); | 383 | |
304 | if(wload[i] > cload[i]) | 384 | result.config.th_load[1].critical = critical_range.load[1]; |
305 | die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]); | 385 | result.config.th_load[1].critical_is_set = true; |
386 | |||
387 | result.config.th_load[2].critical = critical_range.load[2]; | ||
388 | result.config.th_load[2].critical_is_set = true; | ||
306 | } | 389 | } |
307 | 390 | ||
308 | return OK; | 391 | return result; |
309 | } | 392 | } |
310 | 393 | ||
394 | void print_help(void) { | ||
395 | print_revision(progname, NP_VERSION); | ||
311 | 396 | ||
312 | void | 397 | printf("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"); |
313 | print_help (void) | 398 | printf(COPYRIGHT, copyright, email); |
314 | { | ||
315 | print_revision (progname, NP_VERSION); | ||
316 | 399 | ||
317 | printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"); | 400 | printf(_("This plugin tests the current system load average.")); |
318 | printf (COPYRIGHT, copyright, email); | ||
319 | 401 | ||
320 | printf (_("This plugin tests the current system load average.")); | 402 | printf("\n\n"); |
321 | 403 | ||
322 | printf ("\n\n"); | 404 | print_usage(); |
323 | 405 | ||
324 | print_usage (); | 406 | printf(UT_HELP_VRSN); |
407 | printf(UT_EXTRA_OPTS); | ||
325 | 408 | ||
326 | printf (UT_HELP_VRSN); | 409 | printf(" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15"); |
327 | printf (UT_EXTRA_OPTS); | 410 | printf(" %s\n", _("Exit with WARNING status if load average exceeds WLOADn")); |
411 | printf(" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15"); | ||
412 | printf(" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn")); | ||
413 | printf(" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); | ||
414 | printf(" %s\n", "-r, --percpu"); | ||
415 | printf(" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); | ||
416 | printf(" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS"); | ||
417 | printf(" %s\n", _("Number of processes to show when printing the top consuming processes.")); | ||
418 | printf(" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0")); | ||
328 | 419 | ||
329 | printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15"); | 420 | printf(UT_OUTPUT_FORMAT); |
330 | printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn")); | 421 | printf(UT_SUPPORT); |
331 | printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15"); | ||
332 | printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn")); | ||
333 | printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); | ||
334 | printf (" %s\n", "-r, --percpu"); | ||
335 | printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); | ||
336 | printf (" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS"); | ||
337 | printf (" %s\n", _("Number of processes to show when printing the top consuming processes.")); | ||
338 | printf (" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0")); | ||
339 | |||
340 | printf (UT_SUPPORT); | ||
341 | } | 422 | } |
342 | 423 | ||
343 | void | 424 | void print_usage(void) { |
344 | print_usage (void) | 425 | printf("%s\n", _("Usage:")); |
345 | { | 426 | printf("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", |
346 | printf ("%s\n", _("Usage:")); | 427 | progname); |
347 | printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname); | ||
348 | } | 428 | } |
349 | 429 | ||
350 | #ifdef PS_USES_PROCPCPU | 430 | #ifdef PS_USES_PROCPCPU |
@@ -356,36 +436,52 @@ int cmpstringp(const void *p1, const void *p2) { | |||
356 | int procrss = 0; | 436 | int procrss = 0; |
357 | float procpcpu = 0; | 437 | float procpcpu = 0; |
358 | char procstat[8]; | 438 | char procstat[8]; |
359 | #ifdef PS_USES_PROCETIME | 439 | # ifdef PS_USES_PROCETIME |
360 | char procetime[MAX_INPUT_BUFFER]; | 440 | char procetime[MAX_INPUT_BUFFER]; |
361 | #endif /* PS_USES_PROCETIME */ | 441 | # endif /* PS_USES_PROCETIME */ |
362 | char procprog[MAX_INPUT_BUFFER]; | 442 | char procprog[MAX_INPUT_BUFFER]; |
363 | int pos; | 443 | int pos; |
364 | sscanf (* (char * const *) p1, PS_FORMAT, PS_VARLIST); | 444 | sscanf(*(char *const *)p1, PS_FORMAT, PS_VARLIST); |
365 | float procpcpu1 = procpcpu; | 445 | float procpcpu1 = procpcpu; |
366 | sscanf (* (char * const *) p2, PS_FORMAT, PS_VARLIST); | 446 | sscanf(*(char *const *)p2, PS_FORMAT, PS_VARLIST); |
367 | return procpcpu1 < procpcpu; | 447 | return procpcpu1 < procpcpu; |
368 | } | 448 | } |
369 | #endif /* PS_USES_PROCPCPU */ | 449 | #endif /* PS_USES_PROCPCPU */ |
370 | 450 | ||
371 | static int print_top_consuming_processes() { | 451 | static top_processes_result print_top_consuming_processes(unsigned long n_procs_to_show) { |
372 | int i = 0; | 452 | top_processes_result result = { |
373 | struct output chld_out, chld_err; | 453 | .errorcode = OK, |
374 | if(np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0){ | 454 | }; |
455 | output chld_out; | ||
456 | output chld_err; | ||
457 | if (np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0) { | ||
375 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); | 458 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); |
376 | return STATE_UNKNOWN; | 459 | result.errorcode = ERROR; |
460 | return result; | ||
377 | } | 461 | } |
462 | |||
378 | if (chld_out.lines < 2) { | 463 | if (chld_out.lines < 2) { |
379 | fprintf(stderr, _("some error occurred getting procs list.\n")); | 464 | fprintf(stderr, _("some error occurred getting procs list.\n")); |
380 | return STATE_UNKNOWN; | 465 | result.errorcode = ERROR; |
466 | return result; | ||
381 | } | 467 | } |
468 | |||
382 | #ifdef PS_USES_PROCPCPU | 469 | #ifdef PS_USES_PROCPCPU |
383 | qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp); | 470 | qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char *), cmpstringp); |
384 | #endif /* PS_USES_PROCPCPU */ | 471 | #endif /* PS_USES_PROCPCPU */ |
385 | int lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) | 472 | unsigned long lines_to_show = |
386 | ? (int)chld_out.lines : n_procs_to_show + 1; | 473 | chld_out.lines < (size_t)(n_procs_to_show + 1) ? chld_out.lines : n_procs_to_show + 1; |
387 | for (i = 0; i < lines_to_show; i += 1) { | 474 | |
388 | printf("%s\n", chld_out.line[i]); | 475 | result.top_processes = calloc(lines_to_show, sizeof(char *)); |
476 | if (result.top_processes == NULL) { | ||
477 | // Failed allocation | ||
478 | result.errorcode = ERROR; | ||
479 | return result; | ||
389 | } | 480 | } |
390 | return OK; | 481 | |
482 | for (unsigned long i = 0; i < lines_to_show; i += 1) { | ||
483 | xasprintf(&result.top_processes[i], "%s", chld_out.line[i]); | ||
484 | } | ||
485 | |||
486 | return result; | ||
391 | } | 487 | } |
diff --git a/plugins/check_load.d/config.h b/plugins/check_load.d/config.h new file mode 100644 index 00000000..fd735455 --- /dev/null +++ b/plugins/check_load.d/config.h | |||
@@ -0,0 +1,30 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "output.h" | ||
4 | #include "thresholds.h" | ||
5 | typedef struct { | ||
6 | mp_thresholds th_load[3]; | ||
7 | |||
8 | bool take_into_account_cpus; | ||
9 | unsigned long n_procs_to_show; | ||
10 | |||
11 | mp_output_format output_format; | ||
12 | bool output_format_set; | ||
13 | } check_load_config; | ||
14 | |||
15 | check_load_config check_load_config_init() { | ||
16 | check_load_config tmp = { | ||
17 | .th_load = | ||
18 | { | ||
19 | mp_thresholds_init(), | ||
20 | mp_thresholds_init(), | ||
21 | mp_thresholds_init(), | ||
22 | }, | ||
23 | |||
24 | .take_into_account_cpus = false, | ||
25 | .n_procs_to_show = 0, | ||
26 | |||
27 | .output_format_set = false, | ||
28 | }; | ||
29 | return tmp; | ||
30 | } | ||
diff --git a/plugins/check_mrtg.c b/plugins/check_mrtg.c index 5bd276dc..4a17049a 100644 --- a/plugins/check_mrtg.c +++ b/plugins/check_mrtg.c | |||
@@ -129,7 +129,8 @@ int main(int argc, char **argv) { | |||
129 | time_t current_time; | 129 | time_t current_time; |
130 | time(¤t_time); | 130 | time(¤t_time); |
131 | if (config.expire_minutes > 0 && (current_time - timestamp) > (config.expire_minutes * 60)) { | 131 | if (config.expire_minutes > 0 && (current_time - timestamp) > (config.expire_minutes * 60)) { |
132 | printf(_("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); | 132 | printf(_("MRTG data has expired (%d minutes old)\n"), |
133 | (int)((current_time - timestamp) / 60)); | ||
133 | return STATE_WARNING; | 134 | return STATE_WARNING; |
134 | } | 135 | } |
135 | 136 | ||
@@ -148,20 +149,29 @@ int main(int argc, char **argv) { | |||
148 | result = STATE_WARNING; | 149 | result = STATE_WARNING; |
149 | } | 150 | } |
150 | 151 | ||
151 | printf("%s. %s = %lu %s|%s\n", (config.use_average) ? _("Avg") : _("Max"), config.label, rate, config.units, | 152 | printf("%s. %s = %lu %s|%s\n", (config.use_average) ? _("Avg") : _("Max"), config.label, rate, |
152 | perfdata(config.label, (long)rate, config.units, config.value_warning_threshold_set, (long)config.value_warning_threshold, | 153 | config.units, |
153 | config.value_critical_threshold_set, (long)config.value_critical_threshold, 0, 0, 0, 0)); | 154 | perfdata(config.label, (long)rate, config.units, config.value_warning_threshold_set, |
155 | (long)config.value_warning_threshold, config.value_critical_threshold_set, | ||
156 | (long)config.value_critical_threshold, 0, 0, 0, 0)); | ||
154 | 157 | ||
155 | return result; | 158 | return result; |
156 | } | 159 | } |
157 | 160 | ||
158 | /* process command-line arguments */ | 161 | /* process command-line arguments */ |
159 | check_mrtg_config_wrapper process_arguments(int argc, char **argv) { | 162 | check_mrtg_config_wrapper process_arguments(int argc, char **argv) { |
160 | static struct option longopts[] = { | 163 | static struct option longopts[] = {{"logfile", required_argument, 0, 'F'}, |
161 | {"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, | 164 | {"expires", required_argument, 0, 'e'}, |
162 | {"variable", required_argument, 0, 'v'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, | 165 | {"aggregation", required_argument, 0, 'a'}, |
163 | {"label", required_argument, 0, 'l'}, {"units", required_argument, 0, 'u'}, {"variable", required_argument, 0, 'v'}, | 166 | {"variable", required_argument, 0, 'v'}, |
164 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | 167 | {"critical", required_argument, 0, 'c'}, |
168 | {"warning", required_argument, 0, 'w'}, | ||
169 | {"label", required_argument, 0, 'l'}, | ||
170 | {"units", required_argument, 0, 'u'}, | ||
171 | {"variable", required_argument, 0, 'v'}, | ||
172 | {"version", no_argument, 0, 'V'}, | ||
173 | {"help", no_argument, 0, 'h'}, | ||
174 | {0, 0, 0, 0}}; | ||
165 | 175 | ||
166 | check_mrtg_config_wrapper result = { | 176 | check_mrtg_config_wrapper result = { |
167 | .errorcode = OK, | 177 | .errorcode = OK, |
@@ -242,7 +252,9 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { | |||
242 | if (is_intpos(argv[option_char])) { | 252 | if (is_intpos(argv[option_char])) { |
243 | result.config.expire_minutes = atoi(argv[option_char++]); | 253 | result.config.expire_minutes = atoi(argv[option_char++]); |
244 | } else { | 254 | } else { |
245 | die(STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[option_char], progname); | 255 | die(STATE_UNKNOWN, |
256 | _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), | ||
257 | argv[option_char], progname); | ||
246 | } | 258 | } |
247 | } | 259 | } |
248 | 260 | ||
@@ -334,25 +346,32 @@ void print_help(void) { | |||
334 | printf(" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")")); | 346 | printf(" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")")); |
335 | 347 | ||
336 | printf("\n"); | 348 | printf("\n"); |
337 | printf(" %s\n", _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If")); | 349 | printf(" %s\n", |
350 | _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If")); | ||
338 | printf(" %s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If")); | 351 | printf(" %s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If")); |
339 | printf(" %s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING")); | 352 | printf(" %s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING")); |
340 | printf(" %s\n", _("status is returned and a warning message is printed.")); | 353 | printf(" %s\n", _("status is returned and a warning message is printed.")); |
341 | 354 | ||
342 | printf("\n"); | 355 | printf("\n"); |
343 | printf(" %s\n", _("This plugin is useful for monitoring MRTG data that does not correspond to")); | 356 | printf(" %s\n", |
344 | printf(" %s\n", _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth).")); | 357 | _("This plugin is useful for monitoring MRTG data that does not correspond to")); |
345 | printf(" %s\n", _("It can be used to monitor any kind of data that MRTG is monitoring - errors,")); | 358 | printf(" %s\n", |
346 | printf(" %s\n", _("packets/sec, etc. I use MRTG in conjunction with the Novell NLM that allows")); | 359 | _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth).")); |
360 | printf(" %s\n", | ||
361 | _("It can be used to monitor any kind of data that MRTG is monitoring - errors,")); | ||
362 | printf(" %s\n", | ||
363 | _("packets/sec, etc. I use MRTG in conjunction with the Novell NLM that allows")); | ||
347 | printf(" %s\n", _("me to track processor utilization, user connections, drive space, etc and")); | 364 | printf(" %s\n", _("me to track processor utilization, user connections, drive space, etc and")); |
348 | printf(" %s\n\n", _("this plugin works well for monitoring that kind of data as well.")); | 365 | printf(" %s\n\n", _("this plugin works well for monitoring that kind of data as well.")); |
349 | 366 | ||
350 | printf("%s\n", _("Notes:")); | 367 | printf("%s\n", _("Notes:")); |
351 | printf(" %s\n", _("- This plugin only monitors one of the two variables stored in the MRTG log")); | 368 | printf(" %s\n", |
369 | _("- This plugin only monitors one of the two variables stored in the MRTG log")); | ||
352 | printf(" %s\n", _("file. If you want to monitor both values you will have to define two")); | 370 | printf(" %s\n", _("file. If you want to monitor both values you will have to define two")); |
353 | printf(" %s\n", _("commands with different values for the <variable> argument. Of course,")); | 371 | printf(" %s\n", _("commands with different values for the <variable> argument. Of course,")); |
354 | printf(" %s\n", _("you can always hack the code to make this plugin work for you...")); | 372 | printf(" %s\n", _("you can always hack the code to make this plugin work for you...")); |
355 | printf(" %s\n", _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from")); | 373 | printf(" %s\n", |
374 | _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from")); | ||
356 | printf(" %s\n", "http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"); | 375 | printf(" %s\n", "http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"); |
357 | 376 | ||
358 | printf(UT_SUPPORT); | 377 | printf(UT_SUPPORT); |
diff --git a/plugins/check_mrtgtraf.c b/plugins/check_mrtgtraf.c index 8c7cf8aa..10ce936f 100644 --- a/plugins/check_mrtgtraf.c +++ b/plugins/check_mrtgtraf.c | |||
@@ -122,7 +122,8 @@ int main(int argc, char **argv) { | |||
122 | time_t current_time; | 122 | time_t current_time; |
123 | time(¤t_time); | 123 | time(¤t_time); |
124 | if ((config.expire_minutes > 0) && (current_time - timestamp) > (config.expire_minutes * 60)) { | 124 | if ((config.expire_minutes > 0) && (current_time - timestamp) > (config.expire_minutes * 60)) { |
125 | die(STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); | 125 | die(STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"), |
126 | (int)((current_time - timestamp) / 60)); | ||
126 | } | 127 | } |
127 | 128 | ||
128 | unsigned long incoming_rate = 0L; | 129 | unsigned long incoming_rate = 0L; |
@@ -177,21 +178,26 @@ int main(int argc, char **argv) { | |||
177 | } | 178 | } |
178 | 179 | ||
179 | int result = STATE_OK; | 180 | int result = STATE_OK; |
180 | if (incoming_rate > config.incoming_critical_threshold || outgoing_rate > config.outgoing_critical_threshold) { | 181 | if (incoming_rate > config.incoming_critical_threshold || |
182 | outgoing_rate > config.outgoing_critical_threshold) { | ||
181 | result = STATE_CRITICAL; | 183 | result = STATE_CRITICAL; |
182 | } else if (incoming_rate > config.incoming_warning_threshold || outgoing_rate > config.outgoing_warning_threshold) { | 184 | } else if (incoming_rate > config.incoming_warning_threshold || |
185 | outgoing_rate > config.outgoing_warning_threshold) { | ||
183 | result = STATE_WARNING; | 186 | result = STATE_WARNING; |
184 | } | 187 | } |
185 | 188 | ||
186 | char *error_message; | 189 | char *error_message; |
187 | xasprintf(&error_message, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), (config.use_average) ? _("Avg") : _("Max"), | 190 | xasprintf(&error_message, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), |
188 | adjusted_incoming_rate, incoming_speed_rating, (config.use_average) ? _("Avg") : _("Max"), adjusted_outgoing_rate, | 191 | (config.use_average) ? _("Avg") : _("Max"), adjusted_incoming_rate, |
189 | outgoing_speed_rating, | 192 | incoming_speed_rating, (config.use_average) ? _("Avg") : _("Max"), |
190 | fperfdata("in", adjusted_incoming_rate, incoming_speed_rating, (int)config.incoming_warning_threshold, | 193 | adjusted_outgoing_rate, outgoing_speed_rating, |
191 | config.incoming_warning_threshold, (int)config.incoming_critical_threshold, config.incoming_critical_threshold, | 194 | fperfdata("in", adjusted_incoming_rate, incoming_speed_rating, |
195 | (int)config.incoming_warning_threshold, config.incoming_warning_threshold, | ||
196 | (int)config.incoming_critical_threshold, config.incoming_critical_threshold, | ||
192 | true, 0, false, 0), | 197 | true, 0, false, 0), |
193 | fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating, (int)config.outgoing_warning_threshold, | 198 | fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating, |
194 | config.outgoing_warning_threshold, (int)config.outgoing_critical_threshold, config.outgoing_critical_threshold, | 199 | (int)config.outgoing_warning_threshold, config.outgoing_warning_threshold, |
200 | (int)config.outgoing_critical_threshold, config.outgoing_critical_threshold, | ||
195 | true, 0, false, 0)); | 201 | true, 0, false, 0)); |
196 | 202 | ||
197 | printf(_("Traffic %s - %s\n"), state_text(result), error_message); | 203 | printf(_("Traffic %s - %s\n"), state_text(result), error_message); |
@@ -249,10 +255,12 @@ check_mrtgtraf_config_wrapper process_arguments(int argc, char **argv) { | |||
249 | result.config.use_average = (bool)(strcmp(optarg, "MAX")); | 255 | result.config.use_average = (bool)(strcmp(optarg, "MAX")); |
250 | break; | 256 | break; |
251 | case 'c': /* warning threshold */ | 257 | case 'c': /* warning threshold */ |
252 | sscanf(optarg, "%lu,%lu", &result.config.incoming_critical_threshold, &result.config.outgoing_critical_threshold); | 258 | sscanf(optarg, "%lu,%lu", &result.config.incoming_critical_threshold, |
259 | &result.config.outgoing_critical_threshold); | ||
253 | break; | 260 | break; |
254 | case 'w': /* critical threshold */ | 261 | case 'w': /* critical threshold */ |
255 | sscanf(optarg, "%lu,%lu", &result.config.incoming_warning_threshold, &result.config.outgoing_warning_threshold); | 262 | sscanf(optarg, "%lu,%lu", &result.config.incoming_warning_threshold, |
263 | &result.config.outgoing_warning_threshold); | ||
256 | break; | 264 | break; |
257 | case 'V': /* version */ | 265 | case 'V': /* version */ |
258 | print_revision(progname, NP_VERSION); | 266 | print_revision(progname, NP_VERSION); |
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index ca3422b5..3d7ec4cd 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
@@ -50,15 +50,23 @@ static int verbose = 0; | |||
50 | 50 | ||
51 | #define LENGTH_METRIC_UNIT 6 | 51 | #define LENGTH_METRIC_UNIT 6 |
52 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { | 52 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { |
53 | "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", "Threads_connected", "Threads_running"}; | 53 | "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", |
54 | "Threads_connected", "Threads_running"}; | ||
54 | 55 | ||
55 | #define LENGTH_METRIC_COUNTER 9 | 56 | #define LENGTH_METRIC_COUNTER 9 |
56 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = { | 57 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = {"Connections", |
57 | "Connections", "Qcache_hits", "Qcache_inserts", "Qcache_lowmem_prunes", "Qcache_not_cached", "Queries", | 58 | "Qcache_hits", |
58 | "Questions", "Table_locks_waited", "Uptime"}; | 59 | "Qcache_inserts", |
59 | 60 | "Qcache_lowmem_prunes", | |
60 | #define MYSQLDUMP_THREADS_QUERY \ | 61 | "Qcache_not_cached", |
61 | "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'" | 62 | "Queries", |
63 | "Questions", | ||
64 | "Table_locks_waited", | ||
65 | "Uptime"}; | ||
66 | |||
67 | #define MYSQLDUMP_THREADS_QUERY \ | ||
68 | "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE " \ | ||
69 | "'SELECT /*!40001 SQL_NO_CACHE */%'" | ||
62 | 70 | ||
63 | typedef struct { | 71 | typedef struct { |
64 | int errorcode; | 72 | int errorcode; |
@@ -99,16 +107,19 @@ int main(int argc, char **argv) { | |||
99 | } | 107 | } |
100 | 108 | ||
101 | if (config.ssl) { | 109 | if (config.ssl) { |
102 | mysql_ssl_set(&mysql, config.key, config.cert, config.ca_cert, config.ca_dir, config.ciphers); | 110 | mysql_ssl_set(&mysql, config.key, config.cert, config.ca_cert, config.ca_dir, |
111 | config.ciphers); | ||
103 | } | 112 | } |
104 | /* establish a connection to the server and error checking */ | 113 | /* establish a connection to the server and error checking */ |
105 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, config.db_port, config.db_socket, 0)) { | 114 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, |
115 | config.db_port, config.db_socket, 0)) { | ||
106 | /* Depending on internally-selected auth plugin MySQL might return */ | 116 | /* Depending on internally-selected auth plugin MySQL might return */ |
107 | /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ | 117 | /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ |
108 | /* Semantically these errors are the same. */ | 118 | /* Semantically these errors are the same. */ |
109 | if (config.ignore_auth && | 119 | if (config.ignore_auth && (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || |
110 | (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) { | 120 | mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) { |
111 | printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql), mysql_get_proto_info(&mysql)); | 121 | printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql), |
122 | mysql_get_proto_info(&mysql)); | ||
112 | mysql_close(&mysql); | 123 | mysql_close(&mysql); |
113 | return STATE_OK; | 124 | return STATE_OK; |
114 | } | 125 | } |
@@ -157,13 +168,17 @@ int main(int argc, char **argv) { | |||
157 | while ((row = mysql_fetch_row(res)) != NULL) { | 168 | while ((row = mysql_fetch_row(res)) != NULL) { |
158 | for (int i = 0; i < LENGTH_METRIC_UNIT; i++) { | 169 | for (int i = 0; i < LENGTH_METRIC_UNIT; i++) { |
159 | if (strcmp(row[0], metric_unit[i]) == 0) { | 170 | if (strcmp(row[0], metric_unit[i]) == 0) { |
160 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], atol(row[1]), "", false, 0, false, 0, false, 0, false, 0)); | 171 | xasprintf(&perf, "%s%s ", perf, |
172 | perfdata(metric_unit[i], atol(row[1]), "", false, 0, false, 0, false, | ||
173 | 0, false, 0)); | ||
161 | continue; | 174 | continue; |
162 | } | 175 | } |
163 | } | 176 | } |
164 | for (int i = 0; i < LENGTH_METRIC_COUNTER; i++) { | 177 | for (int i = 0; i < LENGTH_METRIC_COUNTER; i++) { |
165 | if (strcmp(row[0], metric_counter[i]) == 0) { | 178 | if (strcmp(row[0], metric_counter[i]) == 0) { |
166 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], atol(row[1]), "c", false, 0, false, 0, false, 0, false, 0)); | 179 | xasprintf(&perf, "%s%s ", perf, |
180 | perfdata(metric_counter[i], atol(row[1]), "c", false, 0, false, 0, | ||
181 | false, 0, false, 0)); | ||
167 | continue; | 182 | continue; |
168 | } | 183 | } |
169 | } | 184 | } |
@@ -189,8 +204,8 @@ int main(int argc, char **argv) { | |||
189 | unsigned long minor_version = (server_verion_int % 10000) / 100; | 204 | unsigned long minor_version = (server_verion_int % 10000) / 100; |
190 | unsigned long patch_version = (server_verion_int % 100); | 205 | unsigned long patch_version = (server_verion_int % 100); |
191 | if (verbose) { | 206 | if (verbose) { |
192 | printf("Found MariaDB: %s, main version: %lu, minor version: %lu, patch version: %lu\n", server_version, major_version, | 207 | printf("Found MariaDB: %s, main version: %lu, minor version: %lu, patch version: %lu\n", |
193 | minor_version, patch_version); | 208 | server_version, major_version, minor_version, patch_version); |
194 | } | 209 | } |
195 | 210 | ||
196 | if (strstr(server_version, "MariaDB") != NULL) { | 211 | if (strstr(server_version, "MariaDB") != NULL) { |
@@ -292,11 +307,15 @@ int main(int argc, char **argv) { | |||
292 | } | 307 | } |
293 | 308 | ||
294 | /* Save replica status in replica_result */ | 309 | /* Save replica status in replica_result */ |
295 | snprintf(replica_result, REPLICA_RESULTSIZE, "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", row[replica_io_field], | 310 | snprintf(replica_result, REPLICA_RESULTSIZE, |
296 | row[replica_sql_field], seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); | 311 | "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", |
297 | 312 | row[replica_io_field], row[replica_sql_field], | |
298 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ | 313 | seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); |
299 | if (strcmp(row[replica_io_field], "Yes") != 0 || strcmp(row[replica_sql_field], "Yes") != 0) { | 314 | |
315 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no | ||
316 | * mysqldump threads running */ | ||
317 | if (strcmp(row[replica_io_field], "Yes") != 0 || | ||
318 | strcmp(row[replica_sql_field], "Yes") != 0) { | ||
300 | MYSQL_RES *res_mysqldump; | 319 | MYSQL_RES *res_mysqldump; |
301 | MYSQL_ROW row_mysqldump; | 320 | MYSQL_ROW row_mysqldump; |
302 | unsigned int mysqldump_threads = 0; | 321 | unsigned int mysqldump_threads = 0; |
@@ -325,20 +344,23 @@ int main(int argc, char **argv) { | |||
325 | if (seconds_behind_field == -1) { | 344 | if (seconds_behind_field == -1) { |
326 | printf("seconds_behind_field not found\n"); | 345 | printf("seconds_behind_field not found\n"); |
327 | } else { | 346 | } else { |
328 | printf("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); | 347 | printf("seconds_behind_field(index %d)=%s\n", seconds_behind_field, |
348 | row[seconds_behind_field]); | ||
329 | } | 349 | } |
330 | } | 350 | } |
331 | 351 | ||
332 | /* Check Seconds Behind against threshold */ | 352 | /* Check Seconds Behind against threshold */ |
333 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp(row[seconds_behind_field], "NULL") != 0)) { | 353 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && |
354 | strcmp(row[seconds_behind_field], "NULL") != 0)) { | ||
334 | double value = atof(row[seconds_behind_field]); | 355 | double value = atof(row[seconds_behind_field]); |
335 | int status; | 356 | int status; |
336 | 357 | ||
337 | status = get_status(value, config.my_threshold); | 358 | status = get_status(value, config.my_threshold); |
338 | 359 | ||
339 | xasprintf(&perf, "%s %s", perf, | 360 | xasprintf(&perf, "%s %s", perf, |
340 | fperfdata("seconds behind master", value, "s", true, (double)config.warning_time, true, | 361 | fperfdata("seconds behind master", value, "s", true, |
341 | (double)config.critical_time, false, 0, false, 0)); | 362 | (double)config.warning_time, true, (double)config.critical_time, |
363 | false, 0, false, 0)); | ||
342 | 364 | ||
343 | if (status == STATE_WARNING) { | 365 | if (status == STATE_WARNING) { |
344 | printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf); | 366 | printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf); |
@@ -410,7 +432,8 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) { | |||
410 | 432 | ||
411 | int option = 0; | 433 | int option = 0; |
412 | while (true) { | 434 | while (true) { |
413 | int option_index = getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); | 435 | int option_index = |
436 | getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); | ||
414 | 437 | ||
415 | if (option_index == -1 || option_index == EOF) { | 438 | if (option_index == -1 || option_index == EOF) { |
416 | break; | 439 | break; |
@@ -580,15 +603,17 @@ void print_help(void) { | |||
580 | printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); | 603 | printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); |
581 | printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); | 604 | printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); |
582 | printf(" %s\n", "-S, --check-slave"); | 605 | printf(" %s\n", "-S, --check-slave"); |
583 | printf(" %s\n", | 606 | printf(" %s\n", _("Check if the slave thread is running properly. This option is deprecated " |
584 | _("Check if the slave thread is running properly. This option is deprecated in favour of check-replica, which does the same")); | 607 | "in favour of check-replica, which does the same")); |
585 | printf(" %s\n", "--check-replica"); | 608 | printf(" %s\n", "--check-replica"); |
586 | printf(" %s\n", _("Check if the replica thread is running properly.")); | 609 | printf(" %s\n", _("Check if the replica thread is running properly.")); |
587 | printf(" %s\n", "-w, --warning"); | 610 | printf(" %s\n", "-w, --warning"); |
588 | printf(" %s\n", _("Exit with WARNING status if replica server is more than INTEGER seconds")); | 611 | printf(" %s\n", |
612 | _("Exit with WARNING status if replica server is more than INTEGER seconds")); | ||
589 | printf(" %s\n", _("behind master")); | 613 | printf(" %s\n", _("behind master")); |
590 | printf(" %s\n", "-c, --critical"); | 614 | printf(" %s\n", "-c, --critical"); |
591 | printf(" %s\n", _("Exit with CRITICAL status if replica server is more then INTEGER seconds")); | 615 | printf(" %s\n", |
616 | _("Exit with CRITICAL status if replica server is more then INTEGER seconds")); | ||
592 | printf(" %s\n", _("behind master")); | 617 | printf(" %s\n", _("behind master")); |
593 | printf(" %s\n", "-l, --ssl"); | 618 | printf(" %s\n", "-l, --ssl"); |
594 | printf(" %s\n", _("Use ssl encryption")); | 619 | printf(" %s\n", _("Use ssl encryption")); |
@@ -604,7 +629,8 @@ void print_help(void) { | |||
604 | printf(" %s\n", _("List of valid SSL ciphers")); | 629 | printf(" %s\n", _("List of valid SSL ciphers")); |
605 | 630 | ||
606 | printf("\n"); | 631 | printf("\n"); |
607 | printf(" %s\n", _("There are no required arguments. By default, the local database is checked")); | 632 | printf(" %s\n", |
633 | _("There are no required arguments. By default, the local database is checked")); | ||
608 | printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); | 634 | printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); |
609 | printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); | 635 | printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); |
610 | 636 | ||
diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index 5e04a94b..c7e84deb 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c | |||
@@ -47,7 +47,8 @@ typedef struct { | |||
47 | check_mysql_query_config config; | 47 | check_mysql_query_config config; |
48 | } check_mysql_query_config_wrapper; | 48 | } check_mysql_query_config_wrapper; |
49 | static check_mysql_query_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | 49 | static check_mysql_query_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
50 | static check_mysql_query_config_wrapper validate_arguments(check_mysql_query_config_wrapper /*config_wrapper*/); | 50 | static check_mysql_query_config_wrapper |
51 | validate_arguments(check_mysql_query_config_wrapper /*config_wrapper*/); | ||
51 | static void print_help(void); | 52 | static void print_help(void); |
52 | void print_usage(void); | 53 | void print_usage(void); |
53 | 54 | ||
@@ -83,7 +84,8 @@ int main(int argc, char **argv) { | |||
83 | } | 84 | } |
84 | 85 | ||
85 | /* establish a connection to the server and error checking */ | 86 | /* establish a connection to the server and error checking */ |
86 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, config.db_port, config.db_socket, 0)) { | 87 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, |
88 | config.db_port, config.db_socket, 0)) { | ||
87 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { | 89 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { |
88 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); | 90 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); |
89 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { | 91 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { |
@@ -155,8 +157,11 @@ int main(int argc, char **argv) { | |||
155 | printf("QUERY %s: ", _("CRITICAL")); | 157 | printf("QUERY %s: ", _("CRITICAL")); |
156 | } | 158 | } |
157 | printf(_("'%s' returned %f | %s"), config.sql_query, value, | 159 | printf(_("'%s' returned %f | %s"), config.sql_query, value, |
158 | fperfdata("result", value, "", config.my_thresholds->warning, config.my_thresholds->warning ? config.my_thresholds->warning->end : 0, | 160 | fperfdata("result", value, "", config.my_thresholds->warning, |
159 | config.my_thresholds->critical, config.my_thresholds->critical ? config.my_thresholds->critical->end : 0, false, 0, false, 0)); | 161 | config.my_thresholds->warning ? config.my_thresholds->warning->end : 0, |
162 | config.my_thresholds->critical, | ||
163 | config.my_thresholds->critical ? config.my_thresholds->critical->end : 0, | ||
164 | false, 0, false, 0)); | ||
160 | printf("\n"); | 165 | printf("\n"); |
161 | 166 | ||
162 | return status; | 167 | return status; |
@@ -164,12 +169,21 @@ int main(int argc, char **argv) { | |||
164 | 169 | ||
165 | /* process command-line arguments */ | 170 | /* process command-line arguments */ |
166 | check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { | 171 | check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { |
167 | static struct option longopts[] = { | 172 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
168 | {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, | 173 | {"socket", required_argument, 0, 's'}, |
169 | {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'}, | 174 | {"database", required_argument, 0, 'd'}, |
170 | {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'P'}, {"verbose", no_argument, 0, 'v'}, | 175 | {"username", required_argument, 0, 'u'}, |
171 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"query", required_argument, 0, 'q'}, | 176 | {"password", required_argument, 0, 'p'}, |
172 | {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {0, 0, 0, 0}}; | 177 | {"file", required_argument, 0, 'f'}, |
178 | {"group", required_argument, 0, 'g'}, | ||
179 | {"port", required_argument, 0, 'P'}, | ||
180 | {"verbose", no_argument, 0, 'v'}, | ||
181 | {"version", no_argument, 0, 'V'}, | ||
182 | {"help", no_argument, 0, 'h'}, | ||
183 | {"query", required_argument, 0, 'q'}, | ||
184 | {"warning", required_argument, 0, 'w'}, | ||
185 | {"critical", required_argument, 0, 'c'}, | ||
186 | {0, 0, 0, 0}}; | ||
173 | 187 | ||
174 | check_mysql_query_config_wrapper result = { | 188 | check_mysql_query_config_wrapper result = { |
175 | .errorcode = OK, | 189 | .errorcode = OK, |
@@ -255,7 +269,8 @@ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { | |||
255 | return validate_arguments(result); | 269 | return validate_arguments(result); |
256 | } | 270 | } |
257 | 271 | ||
258 | check_mysql_query_config_wrapper validate_arguments(check_mysql_query_config_wrapper config_wrapper) { | 272 | check_mysql_query_config_wrapper |
273 | validate_arguments(check_mysql_query_config_wrapper config_wrapper) { | ||
259 | if (config_wrapper.config.sql_query == NULL) { | 274 | if (config_wrapper.config.sql_query == NULL) { |
260 | usage("Must specify a SQL query to run"); | 275 | usage("Must specify a SQL query to run"); |
261 | } | 276 | } |
diff --git a/plugins/check_nt.c b/plugins/check_nt.c index 7dd23e5c..35ca92cd 100644 --- a/plugins/check_nt.c +++ b/plugins/check_nt.c | |||
@@ -96,7 +96,8 @@ int main(int argc, char **argv) { | |||
96 | xasprintf(&send_buffer, "%s&1", config.req_password); | 96 | xasprintf(&send_buffer, "%s&1", config.req_password); |
97 | fetch_data(config.server_address, config.server_port, send_buffer); | 97 | fetch_data(config.server_address, config.server_port, send_buffer); |
98 | if (config.value_list != NULL && strcmp(recv_buffer, config.value_list) != 0) { | 98 | if (config.value_list != NULL && strcmp(recv_buffer, config.value_list) != 0) { |
99 | xasprintf(&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, config.value_list); | 99 | xasprintf(&output_message, _("Wrong client version - running: %s, required: %s"), |
100 | recv_buffer, config.value_list); | ||
100 | return_code = STATE_WARNING; | 101 | return_code = STATE_WARNING; |
101 | } else { | 102 | } else { |
102 | xasprintf(&output_message, "%s", recv_buffer); | 103 | xasprintf(&output_message, "%s", recv_buffer); |
@@ -116,9 +117,12 @@ int main(int argc, char **argv) { | |||
116 | 117 | ||
117 | /* loop until one of the parameters is wrong or not present */ | 118 | /* loop until one of the parameters is wrong or not present */ |
118 | int offset = 0; | 119 | int offset = 0; |
119 | while (lvalue_list[0 + offset] > (unsigned long)0 && lvalue_list[0 + offset] <= (unsigned long)17280 && | 120 | while (lvalue_list[0 + offset] > (unsigned long)0 && |
120 | lvalue_list[1 + offset] > (unsigned long)0 && lvalue_list[1 + offset] <= (unsigned long)100 && | 121 | lvalue_list[0 + offset] <= (unsigned long)17280 && |
121 | lvalue_list[2 + offset] > (unsigned long)0 && lvalue_list[2 + offset] <= (unsigned long)100) { | 122 | lvalue_list[1 + offset] > (unsigned long)0 && |
123 | lvalue_list[1 + offset] <= (unsigned long)100 && | ||
124 | lvalue_list[2 + offset] > (unsigned long)0 && | ||
125 | lvalue_list[2 + offset] <= (unsigned long)100) { | ||
122 | 126 | ||
123 | /* Send request and retrieve data */ | 127 | /* Send request and retrieve data */ |
124 | xasprintf(&send_buffer, "%s&2&%lu", config.req_password, lvalue_list[0 + offset]); | 128 | xasprintf(&send_buffer, "%s&2&%lu", config.req_password, lvalue_list[0 + offset]); |
@@ -133,10 +137,12 @@ int main(int argc, char **argv) { | |||
133 | return_code = STATE_WARNING; | 137 | return_code = STATE_WARNING; |
134 | } | 138 | } |
135 | 139 | ||
136 | xasprintf(&output_message, _(" %lu%% (%lu min average)"), utilization, lvalue_list[0 + offset]); | 140 | xasprintf(&output_message, _(" %lu%% (%lu min average)"), utilization, |
141 | lvalue_list[0 + offset]); | ||
137 | xasprintf(&temp_string, "%s%s", temp_string, output_message); | 142 | xasprintf(&temp_string, "%s%s", temp_string, output_message); |
138 | xasprintf(&perfdata, _(" '%lu min avg Load'=%lu%%;%lu;%lu;0;100"), lvalue_list[0 + offset], utilization, | 143 | xasprintf(&perfdata, _(" '%lu min avg Load'=%lu%%;%lu;%lu;0;100"), |
139 | lvalue_list[1 + offset], lvalue_list[2 + offset]); | 144 | lvalue_list[0 + offset], utilization, lvalue_list[1 + offset], |
145 | lvalue_list[2 + offset]); | ||
140 | xasprintf(&temp_string_perf, "%s%s", temp_string_perf, perfdata); | 146 | xasprintf(&temp_string_perf, "%s%s", temp_string_perf, perfdata); |
141 | offset += 3; /* move across the array */ | 147 | offset += 3; /* move across the array */ |
142 | } | 148 | } |
@@ -154,8 +160,10 @@ int main(int argc, char **argv) { | |||
154 | if (config.value_list == NULL) { | 160 | if (config.value_list == NULL) { |
155 | tmp_value_list = "minutes"; | 161 | tmp_value_list = "minutes"; |
156 | } | 162 | } |
157 | if (strncmp(tmp_value_list, "seconds", strlen("seconds") + 1) && strncmp(tmp_value_list, "minutes", strlen("minutes") + 1) && | 163 | if (strncmp(tmp_value_list, "seconds", strlen("seconds") + 1) && |
158 | strncmp(config.value_list, "hours", strlen("hours") + 1) && strncmp(tmp_value_list, "days", strlen("days") + 1)) { | 164 | strncmp(tmp_value_list, "minutes", strlen("minutes") + 1) && |
165 | strncmp(config.value_list, "hours", strlen("hours") + 1) && | ||
166 | strncmp(tmp_value_list, "days", strlen("days") + 1)) { | ||
159 | 167 | ||
160 | output_message = strdup(_("wrong -l argument")); | 168 | output_message = strdup(_("wrong -l argument")); |
161 | } else { | 169 | } else { |
@@ -175,8 +183,9 @@ int main(int argc, char **argv) { | |||
175 | } | 183 | } |
176 | /* else uptime in seconds, nothing to do */ | 184 | /* else uptime in seconds, nothing to do */ |
177 | 185 | ||
178 | xasprintf(&output_message, _("System Uptime - %u day(s) %u hour(s) %u minute(s) |uptime=%lu"), updays, uphours, upminutes, | 186 | xasprintf(&output_message, |
179 | uptime); | 187 | _("System Uptime - %u day(s) %u hour(s) %u minute(s) |uptime=%lu"), updays, |
188 | uphours, upminutes, uptime); | ||
180 | 189 | ||
181 | if (config.check_critical_value && uptime <= config.critical_value) { | 190 | if (config.check_critical_value && uptime <= config.critical_value) { |
182 | return_code = STATE_CRITICAL; | 191 | return_code = STATE_CRITICAL; |
@@ -207,20 +216,27 @@ int main(int argc, char **argv) { | |||
207 | } | 216 | } |
208 | 217 | ||
209 | if (total_disk_space > 0 && free_disk_space >= 0) { | 218 | if (total_disk_space > 0 && free_disk_space >= 0) { |
210 | double percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100; | 219 | double percent_used_space = |
220 | ((total_disk_space - free_disk_space) / total_disk_space) * 100; | ||
211 | double warning_used_space = ((float)config.warning_value / 100) * total_disk_space; | 221 | double warning_used_space = ((float)config.warning_value / 100) * total_disk_space; |
212 | double critical_used_space = ((float)config.critical_value / 100) * total_disk_space; | 222 | double critical_used_space = |
213 | 223 | ((float)config.critical_value / 100) * total_disk_space; | |
214 | xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), config.value_list, | 224 | |
215 | total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space, | 225 | xasprintf( |
216 | free_disk_space / 1073741824, (free_disk_space / total_disk_space) * 100); | 226 | &temp_string, |
217 | xasprintf(&temp_string_perf, _("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), config.value_list, | 227 | _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), |
218 | (total_disk_space - free_disk_space) / 1073741824, warning_used_space / 1073741824, | 228 | config.value_list, total_disk_space / 1073741824, |
219 | critical_used_space / 1073741824, total_disk_space / 1073741824); | 229 | (total_disk_space - free_disk_space) / 1073741824, percent_used_space, |
230 | free_disk_space / 1073741824, (free_disk_space / total_disk_space) * 100); | ||
231 | xasprintf(&temp_string_perf, _("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), | ||
232 | config.value_list, (total_disk_space - free_disk_space) / 1073741824, | ||
233 | warning_used_space / 1073741824, critical_used_space / 1073741824, | ||
234 | total_disk_space / 1073741824); | ||
220 | 235 | ||
221 | if (config.check_critical_value && percent_used_space >= config.critical_value) { | 236 | if (config.check_critical_value && percent_used_space >= config.critical_value) { |
222 | return_code = STATE_CRITICAL; | 237 | return_code = STATE_CRITICAL; |
223 | } else if (config.check_warning_value && percent_used_space >= config.warning_value) { | 238 | } else if (config.check_warning_value && |
239 | percent_used_space >= config.warning_value) { | ||
224 | return_code = STATE_WARNING; | 240 | return_code = STATE_WARNING; |
225 | } else { | 241 | } else { |
226 | return_code = STATE_OK; | 242 | return_code = STATE_OK; |
@@ -239,8 +255,10 @@ int main(int argc, char **argv) { | |||
239 | if (config.value_list == NULL) { | 255 | if (config.value_list == NULL) { |
240 | output_message = strdup(_("No service/process specified")); | 256 | output_message = strdup(_("No service/process specified")); |
241 | } else { | 257 | } else { |
242 | preparelist(config.value_list); /* replace , between services with & to send the request */ | 258 | preparelist( |
243 | xasprintf(&send_buffer, "%s&%u&%s&%s", config.req_password, (config.vars_to_check == CHECK_SERVICESTATE) ? 5 : 6, | 259 | config.value_list); /* replace , between services with & to send the request */ |
260 | xasprintf(&send_buffer, "%s&%u&%s&%s", config.req_password, | ||
261 | (config.vars_to_check == CHECK_SERVICESTATE) ? 5 : 6, | ||
244 | (config.show_all) ? "ShowAll" : "ShowFail", config.value_list); | 262 | (config.show_all) ? "ShowAll" : "ShowFail", config.value_list); |
245 | fetch_data(config.server_address, config.server_port, send_buffer); | 263 | fetch_data(config.server_address, config.server_port, send_buffer); |
246 | char *numstr = strtok(recv_buffer, "&"); | 264 | char *numstr = strtok(recv_buffer, "&"); |
@@ -271,10 +289,14 @@ int main(int argc, char **argv) { | |||
271 | 289 | ||
272 | /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here, | 290 | /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here, |
273 | which equals RAM + Pagefiles. */ | 291 | which equals RAM + Pagefiles. */ |
274 | xasprintf(&output_message, _("Memory usage: total:%.2f MB - used: %.2f MB (%.0f%%) - free: %.2f MB (%.0f%%)"), | 292 | xasprintf( |
275 | mem_commitLimit / 1048567, mem_commitByte / 1048567, percent_used_space, (mem_commitLimit - mem_commitByte) / 1048567, | 293 | &output_message, |
276 | (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100); | 294 | _("Memory usage: total:%.2f MB - used: %.2f MB (%.0f%%) - free: %.2f MB (%.0f%%)"), |
277 | xasprintf(&perfdata, _("'Memory usage'=%.2fMB;%.2f;%.2f;0.00;%.2f"), mem_commitByte / 1048567, warning_used_space / 1048567, | 295 | mem_commitLimit / 1048567, mem_commitByte / 1048567, percent_used_space, |
296 | (mem_commitLimit - mem_commitByte) / 1048567, | ||
297 | (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100); | ||
298 | xasprintf(&perfdata, _("'Memory usage'=%.2fMB;%.2f;%.2f;0.00;%.2f"), | ||
299 | mem_commitByte / 1048567, warning_used_space / 1048567, | ||
278 | critical_used_space / 1048567, mem_commitLimit / 1048567); | 300 | critical_used_space / 1048567, mem_commitLimit / 1048567); |
279 | 301 | ||
280 | return_code = STATE_OK; | 302 | return_code = STATE_OK; |
@@ -302,16 +324,17 @@ int main(int argc, char **argv) { | |||
302 | the counter unit - that is, the dimensions of the counter you're getting. Examples: | 324 | the counter unit - that is, the dimensions of the counter you're getting. Examples: |
303 | pages/s, packets transferred, etc. | 325 | pages/s, packets transferred, etc. |
304 | 326 | ||
305 | 4) If you want, you may provide the minimum and maximum values to expect. They aren't mandatory, | 327 | 4) If you want, you may provide the minimum and maximum values to expect. They aren't |
306 | but once specified they MUST have the same order of magnitude and units of -w and -c; otherwise. | 328 | mandatory, but once specified they MUST have the same order of magnitude and units of -w and |
307 | strange things will happen when you make graphs of your data. | 329 | -c; otherwise. strange things will happen when you make graphs of your data. |
308 | */ | 330 | */ |
309 | 331 | ||
310 | double counter_value = 0.0; | 332 | double counter_value = 0.0; |
311 | if (config.value_list == NULL) { | 333 | if (config.value_list == NULL) { |
312 | output_message = strdup(_("No counter specified")); | 334 | output_message = strdup(_("No counter specified")); |
313 | } else { | 335 | } else { |
314 | preparelist(config.value_list); /* replace , between services with & to send the request */ | 336 | preparelist( |
337 | config.value_list); /* replace , between services with & to send the request */ | ||
315 | bool isPercent = (strchr(config.value_list, '%') != NULL); | 338 | bool isPercent = (strchr(config.value_list, '%') != NULL); |
316 | 339 | ||
317 | strtok(config.value_list, "&"); /* burn the first parameters */ | 340 | strtok(config.value_list, "&"); /* burn the first parameters */ |
@@ -358,15 +381,18 @@ int main(int argc, char **argv) { | |||
358 | if (allRight) { | 381 | if (allRight) { |
359 | /* Let's format the output string, finally... */ | 382 | /* Let's format the output string, finally... */ |
360 | if (strstr(description, "%") == NULL) { | 383 | if (strstr(description, "%") == NULL) { |
361 | xasprintf(&output_message, "%s = %.2f %s", description, counter_value, counter_unit); | 384 | xasprintf(&output_message, "%s = %.2f %s", description, counter_value, |
385 | counter_unit); | ||
362 | } else { | 386 | } else { |
363 | /* has formatting, will segv if wrong */ | 387 | /* has formatting, will segv if wrong */ |
364 | xasprintf(&output_message, description, counter_value); | 388 | xasprintf(&output_message, description, counter_value); |
365 | } | 389 | } |
366 | xasprintf(&output_message, "%s |", output_message); | 390 | xasprintf(&output_message, "%s |", output_message); |
367 | xasprintf(&output_message, "%s %s", output_message, | 391 | xasprintf(&output_message, "%s %s", output_message, |
368 | fperfdata(description, counter_value, counter_unit, 1, config.warning_value, 1, config.critical_value, | 392 | fperfdata(description, counter_value, counter_unit, 1, |
369 | (!(isPercent) && (minval != NULL)), fminval, (!(isPercent) && (minval != NULL)), fmaxval)); | 393 | config.warning_value, 1, config.critical_value, |
394 | (!(isPercent) && (minval != NULL)), fminval, | ||
395 | (!(isPercent) && (minval != NULL)), fmaxval)); | ||
370 | } | 396 | } |
371 | } | 397 | } |
372 | 398 | ||
@@ -391,7 +417,8 @@ int main(int argc, char **argv) { | |||
391 | if (config.value_list == NULL) { | 417 | if (config.value_list == NULL) { |
392 | output_message = strdup(_("No counter specified")); | 418 | output_message = strdup(_("No counter specified")); |
393 | } else { | 419 | } else { |
394 | preparelist(config.value_list); /* replace , between services with & to send the request */ | 420 | preparelist( |
421 | config.value_list); /* replace , between services with & to send the request */ | ||
395 | xasprintf(&send_buffer, "%s&9&%s", config.req_password, config.value_list); | 422 | xasprintf(&send_buffer, "%s&9&%s", config.req_password, config.value_list); |
396 | fetch_data(config.server_address, config.server_port, send_buffer); | 423 | fetch_data(config.server_address, config.server_port, send_buffer); |
397 | unsigned long age_in_minutes = atoi(strtok(recv_buffer, "&")); | 424 | unsigned long age_in_minutes = atoi(strtok(recv_buffer, "&")); |
@@ -724,25 +751,31 @@ void print_help(void) { | |||
724 | printf(" %s\n", "\"%%.f %%%% paging file used.\""); | 751 | printf(" %s\n", "\"%%.f %%%% paging file used.\""); |
725 | printf(" %s\n", "INSTANCES ="); | 752 | printf(" %s\n", "INSTANCES ="); |
726 | printf(" %s\n", _("Check any performance counter object of Windows NT/2000.")); | 753 | printf(" %s\n", _("Check any performance counter object of Windows NT/2000.")); |
727 | printf(" %s\n", _("Syntax: check_nt -H <hostname> -p <port> -v INSTANCES -l <counter object>")); | 754 | printf(" %s\n", |
755 | _("Syntax: check_nt -H <hostname> -p <port> -v INSTANCES -l <counter object>")); | ||
728 | printf(" %s\n", _("<counter object> is a Windows Perfmon Counter object (eg. Process),")); | 756 | printf(" %s\n", _("<counter object> is a Windows Perfmon Counter object (eg. Process),")); |
729 | printf(" %s\n", _("if it is two words, it should be enclosed in quotes")); | 757 | printf(" %s\n", _("if it is two words, it should be enclosed in quotes")); |
730 | printf(" %s\n", _("The returned results will be a comma-separated list of instances on ")); | 758 | printf(" %s\n", _("The returned results will be a comma-separated list of instances on ")); |
731 | printf(" %s\n", _(" the selected computer for that object.")); | 759 | printf(" %s\n", _(" the selected computer for that object.")); |
732 | printf(" %s\n", _("The purpose of this is to be run from command line to determine what instances")); | 760 | printf(" %s\n", |
733 | printf(" %s\n", _(" are available for monitoring without having to log onto the Windows server")); | 761 | _("The purpose of this is to be run from command line to determine what instances")); |
762 | printf(" %s\n", | ||
763 | _(" are available for monitoring without having to log onto the Windows server")); | ||
734 | printf(" %s\n", _(" to run Perfmon directly.")); | 764 | printf(" %s\n", _(" to run Perfmon directly.")); |
735 | printf(" %s\n", _("It can also be used in scripts that automatically create the monitoring service")); | 765 | printf(" %s\n", |
766 | _("It can also be used in scripts that automatically create the monitoring service")); | ||
736 | printf(" %s\n", _(" configuration files.")); | 767 | printf(" %s\n", _(" configuration files.")); |
737 | printf(" %s\n", _("Some examples:")); | 768 | printf(" %s\n", _("Some examples:")); |
738 | printf(" %s\n\n", _("check_nt -H 192.168.1.1 -p 1248 -v INSTANCES -l Process")); | 769 | printf(" %s\n\n", _("check_nt -H 192.168.1.1 -p 1248 -v INSTANCES -l Process")); |
739 | 770 | ||
740 | printf("%s\n", _("Notes:")); | 771 | printf("%s\n", _("Notes:")); |
741 | printf(" %s\n", _("- The NSClient service should be running on the server to get any information")); | 772 | printf(" %s\n", |
773 | _("- The NSClient service should be running on the server to get any information")); | ||
742 | printf(" %s\n", "(http://nsclient.ready2run.nl)."); | 774 | printf(" %s\n", "(http://nsclient.ready2run.nl)."); |
743 | printf(" %s\n", _("- Critical thresholds should be lower than warning thresholds")); | 775 | printf(" %s\n", _("- Critical thresholds should be lower than warning thresholds")); |
744 | printf(" %s\n", _("- Default port 1248 is sometimes in use by other services. The error")); | 776 | printf(" %s\n", _("- Default port 1248 is sometimes in use by other services. The error")); |
745 | printf(" %s\n", _("output when this happens contains \"Cannot map xxxxx to protocol number\".")); | 777 | printf(" %s\n", |
778 | _("output when this happens contains \"Cannot map xxxxx to protocol number\".")); | ||
746 | printf(" %s\n", _("One fix for this is to change the port to something else on check_nt ")); | 779 | printf(" %s\n", _("One fix for this is to change the port to something else on check_nt ")); |
747 | printf(" %s\n", _("and on the client service it\'s connecting to.")); | 780 | printf(" %s\n", _("and on the client service it\'s connecting to.")); |
748 | 781 | ||
diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c index d33f8786..b22cc3c1 100644 --- a/plugins/check_ntp.c +++ b/plugins/check_ntp.c | |||
@@ -1,34 +1,34 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_ntp plugin | 3 | * Monitoring check_ntp plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2006 Sean Finney <seanius@seanius.net> | 6 | * Copyright (c) 2006 Sean Finney <seanius@seanius.net> |
7 | * Copyright (c) 2006-2024 Monitoring Plugins Development Team | 7 | * Copyright (c) 2006-2024 Monitoring Plugins Development Team |
8 | * | 8 | * |
9 | * Description: | 9 | * Description: |
10 | * | 10 | * |
11 | * This file contains the check_ntp plugin | 11 | * This file contains the check_ntp plugin |
12 | * | 12 | * |
13 | * This plugin to check ntp servers independent of any commandline | 13 | * This plugin to check ntp servers independent of any commandline |
14 | * programs or external libraries. | 14 | * programs or external libraries. |
15 | * | 15 | * |
16 | * | 16 | * |
17 | * This program is free software: you can redistribute it and/or modify | 17 | * This program is free software: you can redistribute it and/or modify |
18 | * it under the terms of the GNU General Public License as published by | 18 | * it under the terms of the GNU General Public License as published by |
19 | * the Free Software Foundation, either version 3 of the License, or | 19 | * the Free Software Foundation, either version 3 of the License, or |
20 | * (at your option) any later version. | 20 | * (at your option) any later version. |
21 | * | 21 | * |
22 | * This program is distributed in the hope that it will be useful, | 22 | * This program is distributed in the hope that it will be useful, |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 | * GNU General Public License for more details. | 25 | * GNU General Public License for more details. |
26 | * | 26 | * |
27 | * You should have received a copy of the GNU General Public License | 27 | * You should have received a copy of the GNU General Public License |
28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
29 | * | 29 | * |
30 | * | 30 | * |
31 | *****************************************************************************/ | 31 | *****************************************************************************/ |
32 | 32 | ||
33 | const char *progname = "check_ntp"; | 33 | const char *progname = "check_ntp"; |
34 | const char *copyright = "2006-2024"; | 34 | const char *copyright = "2006-2024"; |
@@ -38,24 +38,24 @@ const char *email = "devel@monitoring-plugins.org"; | |||
38 | #include "netutils.h" | 38 | #include "netutils.h" |
39 | #include "utils.h" | 39 | #include "utils.h" |
40 | 40 | ||
41 | static char *server_address=NULL; | 41 | static char *server_address = NULL; |
42 | static int verbose=0; | 42 | static int verbose = 0; |
43 | static bool do_offset = false; | 43 | static bool do_offset = false; |
44 | static char *owarn="60"; | 44 | static char *owarn = "60"; |
45 | static char *ocrit="120"; | 45 | static char *ocrit = "120"; |
46 | static bool do_jitter = false; | 46 | static bool do_jitter = false; |
47 | static char *jwarn="5000"; | 47 | static char *jwarn = "5000"; |
48 | static char *jcrit="10000"; | 48 | static char *jcrit = "10000"; |
49 | 49 | ||
50 | static int process_arguments (int /*argc*/, char ** /*argv*/); | 50 | static int process_arguments(int /*argc*/, char ** /*argv*/); |
51 | static thresholds *offset_thresholds = NULL; | 51 | static thresholds *offset_thresholds = NULL; |
52 | static thresholds *jitter_thresholds = NULL; | 52 | static thresholds *jitter_thresholds = NULL; |
53 | static void print_help (void); | 53 | static void print_help(void); |
54 | void print_usage (void); | 54 | void print_usage(void); |
55 | 55 | ||
56 | /* number of times to perform each request to get a good average. */ | 56 | /* number of times to perform each request to get a good average. */ |
57 | #ifndef AVG_NUM | 57 | #ifndef AVG_NUM |
58 | #define AVG_NUM 4 | 58 | # define AVG_NUM 4 |
59 | #endif | 59 | #endif |
60 | 60 | ||
61 | /* max size of control message data */ | 61 | /* max size of control message data */ |
@@ -63,17 +63,17 @@ void print_usage (void); | |||
63 | 63 | ||
64 | /* this structure holds everything in an ntp request/response as per rfc1305 */ | 64 | /* this structure holds everything in an ntp request/response as per rfc1305 */ |
65 | typedef struct { | 65 | typedef struct { |
66 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | 66 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ |
67 | uint8_t stratum; /* clock stratum */ | 67 | uint8_t stratum; /* clock stratum */ |
68 | int8_t poll; /* polling interval */ | 68 | int8_t poll; /* polling interval */ |
69 | int8_t precision; /* precision of the local clock */ | 69 | int8_t precision; /* precision of the local clock */ |
70 | int32_t rtdelay; /* total rt delay, as a fixed point num. see macros */ | 70 | int32_t rtdelay; /* total rt delay, as a fixed point num. see macros */ |
71 | uint32_t rtdisp; /* like above, but for max err to primary src */ | 71 | uint32_t rtdisp; /* like above, but for max err to primary src */ |
72 | uint32_t refid; /* ref clock identifier */ | 72 | uint32_t refid; /* ref clock identifier */ |
73 | uint64_t refts; /* reference timestamp. local time local clock */ | 73 | uint64_t refts; /* reference timestamp. local time local clock */ |
74 | uint64_t origts; /* time at which request departed client */ | 74 | uint64_t origts; /* time at which request departed client */ |
75 | uint64_t rxts; /* time at which request arrived at server */ | 75 | uint64_t rxts; /* time at which request arrived at server */ |
76 | uint64_t txts; /* time at which request departed server */ | 76 | uint64_t txts; /* time at which request departed server */ |
77 | } ntp_message; | 77 | } ntp_message; |
78 | 78 | ||
79 | /* this structure holds data about results from querying offset from a peer */ | 79 | /* this structure holds data about results from querying offset from a peer */ |
@@ -84,20 +84,20 @@ typedef struct { | |||
84 | double rtdelay; /* converted from the ntp_message */ | 84 | double rtdelay; /* converted from the ntp_message */ |
85 | double rtdisp; /* converted from the ntp_message */ | 85 | double rtdisp; /* converted from the ntp_message */ |
86 | double offset[AVG_NUM]; /* offsets from each response */ | 86 | double offset[AVG_NUM]; /* offsets from each response */ |
87 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | 87 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ |
88 | } ntp_server_results; | 88 | } ntp_server_results; |
89 | 89 | ||
90 | /* this structure holds everything in an ntp control message as per rfc1305 */ | 90 | /* this structure holds everything in an ntp control message as per rfc1305 */ |
91 | typedef struct { | 91 | typedef struct { |
92 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | 92 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ |
93 | uint8_t op; /* R,E,M bits and Opcode */ | 93 | uint8_t op; /* R,E,M bits and Opcode */ |
94 | uint16_t seq; /* Packet sequence */ | 94 | uint16_t seq; /* Packet sequence */ |
95 | uint16_t status; /* Clock status */ | 95 | uint16_t status; /* Clock status */ |
96 | uint16_t assoc; /* Association */ | 96 | uint16_t assoc; /* Association */ |
97 | uint16_t offset; /* Similar to TCP sequence # */ | 97 | uint16_t offset; /* Similar to TCP sequence # */ |
98 | uint16_t count; /* # bytes of data */ | 98 | uint16_t count; /* # bytes of data */ |
99 | char data[MAX_CM_SIZE]; /* ASCII data of the request */ | 99 | char data[MAX_CM_SIZE]; /* ASCII data of the request */ |
100 | /* NB: not necessarily NULL terminated! */ | 100 | /* NB: not necessarily NULL terminated! */ |
101 | } ntp_control_message; | 101 | } ntp_control_message; |
102 | 102 | ||
103 | /* this is an association/status-word pair found in control packet responses */ | 103 | /* this is an association/status-word pair found in control packet responses */ |
@@ -108,38 +108,50 @@ typedef struct { | |||
108 | 108 | ||
109 | /* bits 1,2 are the leap indicator */ | 109 | /* bits 1,2 are the leap indicator */ |
110 | #define LI_MASK 0xc0 | 110 | #define LI_MASK 0xc0 |
111 | #define LI(x) ((x&LI_MASK)>>6) | 111 | #define LI(x) ((x & LI_MASK) >> 6) |
112 | #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0) | 112 | #define LI_SET(x, y) \ |
113 | do { \ | ||
114 | x |= ((y << 6) & LI_MASK); \ | ||
115 | } while (0) | ||
113 | /* and these are the values of the leap indicator */ | 116 | /* and these are the values of the leap indicator */ |
114 | #define LI_NOWARNING 0x00 | 117 | #define LI_NOWARNING 0x00 |
115 | #define LI_EXTRASEC 0x01 | 118 | #define LI_EXTRASEC 0x01 |
116 | #define LI_MISSINGSEC 0x02 | 119 | #define LI_MISSINGSEC 0x02 |
117 | #define LI_ALARM 0x03 | 120 | #define LI_ALARM 0x03 |
118 | /* bits 3,4,5 are the ntp version */ | 121 | /* bits 3,4,5 are the ntp version */ |
119 | #define VN_MASK 0x38 | 122 | #define VN_MASK 0x38 |
120 | #define VN(x) ((x&VN_MASK)>>3) | 123 | #define VN(x) ((x & VN_MASK) >> 3) |
121 | #define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0) | 124 | #define VN_SET(x, y) \ |
125 | do { \ | ||
126 | x |= ((y << 3) & VN_MASK); \ | ||
127 | } while (0) | ||
122 | #define VN_RESERVED 0x02 | 128 | #define VN_RESERVED 0x02 |
123 | /* bits 6,7,8 are the ntp mode */ | 129 | /* bits 6,7,8 are the ntp mode */ |
124 | #define MODE_MASK 0x07 | 130 | #define MODE_MASK 0x07 |
125 | #define MODE(x) (x&MODE_MASK) | 131 | #define MODE(x) (x & MODE_MASK) |
126 | #define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0) | 132 | #define MODE_SET(x, y) \ |
133 | do { \ | ||
134 | x |= (y & MODE_MASK); \ | ||
135 | } while (0) | ||
127 | /* here are some values */ | 136 | /* here are some values */ |
128 | #define MODE_CLIENT 0x03 | 137 | #define MODE_CLIENT 0x03 |
129 | #define MODE_CONTROLMSG 0x06 | 138 | #define MODE_CONTROLMSG 0x06 |
130 | /* In control message, bits 8-10 are R,E,M bits */ | 139 | /* In control message, bits 8-10 are R,E,M bits */ |
131 | #define REM_MASK 0xe0 | 140 | #define REM_MASK 0xe0 |
132 | #define REM_RESP 0x80 | 141 | #define REM_RESP 0x80 |
133 | #define REM_ERROR 0x40 | 142 | #define REM_ERROR 0x40 |
134 | #define REM_MORE 0x20 | 143 | #define REM_MORE 0x20 |
135 | /* In control message, bits 11 - 15 are opcode */ | 144 | /* In control message, bits 11 - 15 are opcode */ |
136 | #define OP_MASK 0x1f | 145 | #define OP_MASK 0x1f |
137 | #define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0) | 146 | #define OP_SET(x, y) \ |
147 | do { \ | ||
148 | x |= (y & OP_MASK); \ | ||
149 | } while (0) | ||
138 | #define OP_READSTAT 0x01 | 150 | #define OP_READSTAT 0x01 |
139 | #define OP_READVAR 0x02 | 151 | #define OP_READVAR 0x02 |
140 | /* In peer status bytes, bits 6,7,8 determine clock selection status */ | 152 | /* In peer status bytes, bits 6,7,8 determine clock selection status */ |
141 | #define PEER_SEL(x) ((ntohs(x)>>8)&0x07) | 153 | #define PEER_SEL(x) ((ntohs(x) >> 8) & 0x07) |
142 | #define PEER_INCLUDED 0x04 | 154 | #define PEER_INCLUDED 0x04 |
143 | #define PEER_SYNCSOURCE 0x06 | 155 | #define PEER_SYNCSOURCE 0x06 |
144 | 156 | ||
145 | /** | 157 | /** |
@@ -153,82 +165,92 @@ typedef struct { | |||
153 | 165 | ||
154 | /* macros to access the left/right 16 bits of a 32-bit ntp "fixed point" | 166 | /* macros to access the left/right 16 bits of a 32-bit ntp "fixed point" |
155 | number. note that these can be used as lvalues too */ | 167 | number. note that these can be used as lvalues too */ |
156 | #define L16(x) (((uint16_t*)&x)[0]) | 168 | #define L16(x) (((uint16_t *)&x)[0]) |
157 | #define R16(x) (((uint16_t*)&x)[1]) | 169 | #define R16(x) (((uint16_t *)&x)[1]) |
158 | /* macros to access the left/right 32 bits of a 64-bit ntp "fixed point" | 170 | /* macros to access the left/right 32 bits of a 64-bit ntp "fixed point" |
159 | number. these too can be used as lvalues */ | 171 | number. these too can be used as lvalues */ |
160 | #define L32(x) (((uint32_t*)&x)[0]) | 172 | #define L32(x) (((uint32_t *)&x)[0]) |
161 | #define R32(x) (((uint32_t*)&x)[1]) | 173 | #define R32(x) (((uint32_t *)&x)[1]) |
162 | 174 | ||
163 | /* ntp wants seconds since 1/1/00, epoch is 1/1/70. this is the difference */ | 175 | /* ntp wants seconds since 1/1/00, epoch is 1/1/70. this is the difference */ |
164 | #define EPOCHDIFF 0x83aa7e80UL | 176 | #define EPOCHDIFF 0x83aa7e80UL |
165 | 177 | ||
166 | /* extract a 32-bit ntp fixed point number into a double */ | 178 | /* extract a 32-bit ntp fixed point number into a double */ |
167 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + (double)ntohs(R16(x))/65536.0) | 179 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + (double)ntohs(R16(x)) / 65536.0) |
168 | 180 | ||
169 | /* likewise for a 64-bit ntp fp number */ | 181 | /* likewise for a 64-bit ntp fp number */ |
170 | #define NTP64asDOUBLE(n) (double)(((uint64_t)n)?\ | 182 | #define NTP64asDOUBLE(n) \ |
171 | (ntohl(L32(n))-EPOCHDIFF) + \ | 183 | (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + \ |
172 | (.00000001*(0.5+(double)(ntohl(R32(n))/42.94967296))):\ | 184 | (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) \ |
173 | 0) | 185 | : 0) |
174 | 186 | ||
175 | /* convert a struct timeval to a double */ | 187 | /* convert a struct timeval to a double */ |
176 | #define TVasDOUBLE(x) (double)(x.tv_sec+(0.000001*x.tv_usec)) | 188 | #define TVasDOUBLE(x) (double)(x.tv_sec + (0.000001 * x.tv_usec)) |
177 | 189 | ||
178 | /* convert an ntp 64-bit fp number to a struct timeval */ | 190 | /* convert an ntp 64-bit fp number to a struct timeval */ |
179 | #define NTP64toTV(n,t) \ | 191 | #define NTP64toTV(n, t) \ |
180 | do{ if(!n) t.tv_sec = t.tv_usec = 0; \ | 192 | do { \ |
181 | else { \ | 193 | if (!n) \ |
182 | t.tv_sec=ntohl(L32(n))-EPOCHDIFF; \ | 194 | t.tv_sec = t.tv_usec = 0; \ |
183 | t.tv_usec=(int)(0.5+(double)(ntohl(R32(n))/4294.967296)); \ | 195 | else { \ |
184 | } \ | 196 | t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \ |
185 | }while(0) | 197 | t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \ |
198 | } \ | ||
199 | } while (0) | ||
186 | 200 | ||
187 | /* convert a struct timeval to an ntp 64-bit fp number */ | 201 | /* convert a struct timeval to an ntp 64-bit fp number */ |
188 | #define TVtoNTP64(t,n) \ | 202 | #define TVtoNTP64(t, n) \ |
189 | do{ if(!t.tv_usec && !t.tv_sec) n=0x0UL; \ | 203 | do { \ |
190 | else { \ | 204 | if (!t.tv_usec && !t.tv_sec) \ |
191 | L32(n)=htonl(t.tv_sec + EPOCHDIFF); \ | 205 | n = 0x0UL; \ |
192 | R32(n)=htonl((uint64_t)((4294.967296*t.tv_usec)+.5)); \ | 206 | else { \ |
193 | } \ | 207 | L32(n) = htonl(t.tv_sec + EPOCHDIFF); \ |
194 | } while(0) | 208 | R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \ |
209 | } \ | ||
210 | } while (0) | ||
195 | 211 | ||
196 | /* NTP control message header is 12 bytes, plus any data in the data | 212 | /* NTP control message header is 12 bytes, plus any data in the data |
197 | * field, plus null padding to the nearest 32-bit boundary per rfc. | 213 | * field, plus null padding to the nearest 32-bit boundary per rfc. |
198 | */ | 214 | */ |
199 | #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0)) | 215 | #define SIZEOF_NTPCM(m) \ |
216 | (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0)) | ||
200 | 217 | ||
201 | /* finally, a little helper or two for debugging: */ | 218 | /* finally, a little helper or two for debugging: */ |
202 | #define DBG(x) do{if(verbose>1){ x; }}while(0); | 219 | #define DBG(x) \ |
203 | #define PRINTSOCKADDR(x) \ | 220 | do { \ |
204 | do{ \ | 221 | if (verbose > 1) { \ |
205 | printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\ | 222 | x; \ |
206 | }while(0); | 223 | } \ |
224 | } while (0); | ||
225 | #define PRINTSOCKADDR(x) \ | ||
226 | do { \ | ||
227 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ | ||
228 | } while (0); | ||
207 | 229 | ||
208 | /* calculate the offset of the local clock */ | 230 | /* calculate the offset of the local clock */ |
209 | static inline double calc_offset(const ntp_message *m, const struct timeval *t){ | 231 | static inline double calc_offset(const ntp_message *m, const struct timeval *t) { |
210 | double client_tx, peer_rx, peer_tx, client_rx; | 232 | double client_tx, peer_rx, peer_tx, client_rx; |
211 | client_tx = NTP64asDOUBLE(m->origts); | 233 | client_tx = NTP64asDOUBLE(m->origts); |
212 | peer_rx = NTP64asDOUBLE(m->rxts); | 234 | peer_rx = NTP64asDOUBLE(m->rxts); |
213 | peer_tx = NTP64asDOUBLE(m->txts); | 235 | peer_tx = NTP64asDOUBLE(m->txts); |
214 | client_rx=TVasDOUBLE((*t)); | 236 | client_rx = TVasDOUBLE((*t)); |
215 | return (.5*((peer_tx-client_rx)+(peer_rx-client_tx))); | 237 | return (.5 * ((peer_tx - client_rx) + (peer_rx - client_tx))); |
216 | } | 238 | } |
217 | 239 | ||
218 | /* print out a ntp packet in human readable/debuggable format */ | 240 | /* print out a ntp packet in human readable/debuggable format */ |
219 | void print_ntp_message(const ntp_message *p){ | 241 | void print_ntp_message(const ntp_message *p) { |
220 | struct timeval ref, orig, rx, tx; | 242 | struct timeval ref, orig, rx, tx; |
221 | 243 | ||
222 | NTP64toTV(p->refts,ref); | 244 | NTP64toTV(p->refts, ref); |
223 | NTP64toTV(p->origts,orig); | 245 | NTP64toTV(p->origts, orig); |
224 | NTP64toTV(p->rxts,rx); | 246 | NTP64toTV(p->rxts, rx); |
225 | NTP64toTV(p->txts,tx); | 247 | NTP64toTV(p->txts, tx); |
226 | 248 | ||
227 | printf("packet contents:\n"); | 249 | printf("packet contents:\n"); |
228 | printf("\tflags: 0x%.2x\n", p->flags); | 250 | printf("\tflags: 0x%.2x\n", p->flags); |
229 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK); | 251 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); |
230 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK); | 252 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); |
231 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK); | 253 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); |
232 | printf("\tstratum = %d\n", p->stratum); | 254 | printf("\tstratum = %d\n", p->stratum); |
233 | printf("\tpoll = %g\n", pow(2, p->poll)); | 255 | printf("\tpoll = %g\n", pow(2, p->poll)); |
234 | printf("\tprecision = %g\n", pow(2, p->precision)); | 256 | printf("\tprecision = %g\n", pow(2, p->precision)); |
@@ -241,32 +263,31 @@ void print_ntp_message(const ntp_message *p){ | |||
241 | printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); | 263 | printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); |
242 | } | 264 | } |
243 | 265 | ||
244 | void print_ntp_control_message(const ntp_control_message *p){ | 266 | void print_ntp_control_message(const ntp_control_message *p) { |
245 | int i=0, numpeers=0; | 267 | int i = 0, numpeers = 0; |
246 | const ntp_assoc_status_pair *peer=NULL; | 268 | const ntp_assoc_status_pair *peer = NULL; |
247 | 269 | ||
248 | printf("control packet contents:\n"); | 270 | printf("control packet contents:\n"); |
249 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); | 271 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); |
250 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK); | 272 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); |
251 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK); | 273 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); |
252 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK); | 274 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); |
253 | printf("\t response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP); | 275 | printf("\t response=%d (0x%.2x)\n", (p->op & REM_RESP) > 0, p->op & REM_RESP); |
254 | printf("\t more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE); | 276 | printf("\t more=%d (0x%.2x)\n", (p->op & REM_MORE) > 0, p->op & REM_MORE); |
255 | printf("\t error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR); | 277 | printf("\t error=%d (0x%.2x)\n", (p->op & REM_ERROR) > 0, p->op & REM_ERROR); |
256 | printf("\t op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK); | 278 | printf("\t op=%d (0x%.2x)\n", p->op & OP_MASK, p->op & OP_MASK); |
257 | printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); | 279 | printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); |
258 | printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); | 280 | printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); |
259 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); | 281 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); |
260 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); | 282 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); |
261 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); | 283 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); |
262 | numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair)); | 284 | numpeers = ntohs(p->count) / (sizeof(ntp_assoc_status_pair)); |
263 | if(p->op&REM_RESP && p->op&OP_READSTAT){ | 285 | if (p->op & REM_RESP && p->op & OP_READSTAT) { |
264 | peer=(ntp_assoc_status_pair*)p->data; | 286 | peer = (ntp_assoc_status_pair *)p->data; |
265 | for(i=0;i<numpeers;i++){ | 287 | for (i = 0; i < numpeers; i++) { |
266 | printf("\tpeer id %.2x status %.2x", | 288 | printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status)); |
267 | ntohs(peer[i].assoc), ntohs(peer[i].status)); | 289 | if (PEER_SEL(peer[i].status) >= PEER_INCLUDED) { |
268 | if (PEER_SEL(peer[i].status) >= PEER_INCLUDED){ | 290 | if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) { |
269 | if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){ | ||
270 | printf(" <-- current sync source"); | 291 | printf(" <-- current sync source"); |
271 | } else { | 292 | } else { |
272 | printf(" <-- current sync candidate"); | 293 | printf(" <-- current sync candidate"); |
@@ -277,41 +298,45 @@ void print_ntp_control_message(const ntp_control_message *p){ | |||
277 | } | 298 | } |
278 | } | 299 | } |
279 | 300 | ||
280 | void setup_request(ntp_message *p){ | 301 | void setup_request(ntp_message *p) { |
281 | struct timeval t; | 302 | struct timeval t; |
282 | 303 | ||
283 | memset(p, 0, sizeof(ntp_message)); | 304 | memset(p, 0, sizeof(ntp_message)); |
284 | LI_SET(p->flags, LI_ALARM); | 305 | LI_SET(p->flags, LI_ALARM); |
285 | VN_SET(p->flags, 4); | 306 | VN_SET(p->flags, 4); |
286 | MODE_SET(p->flags, MODE_CLIENT); | 307 | MODE_SET(p->flags, MODE_CLIENT); |
287 | p->poll=4; | 308 | p->poll = 4; |
288 | p->precision=(int8_t)0xfa; | 309 | p->precision = (int8_t)0xfa; |
289 | L16(p->rtdelay)=htons(1); | 310 | L16(p->rtdelay) = htons(1); |
290 | L16(p->rtdisp)=htons(1); | 311 | L16(p->rtdisp) = htons(1); |
291 | 312 | ||
292 | gettimeofday(&t, NULL); | 313 | gettimeofday(&t, NULL); |
293 | TVtoNTP64(t,p->txts); | 314 | TVtoNTP64(t, p->txts); |
294 | } | 315 | } |
295 | 316 | ||
296 | /* select the "best" server from a list of servers, and return its index. | 317 | /* select the "best" server from a list of servers, and return its index. |
297 | * this is done by filtering servers based on stratum, dispersion, and | 318 | * this is done by filtering servers based on stratum, dispersion, and |
298 | * finally round-trip delay. */ | 319 | * finally round-trip delay. */ |
299 | int best_offset_server(const ntp_server_results *slist, int nservers){ | 320 | int best_offset_server(const ntp_server_results *slist, int nservers) { |
300 | int cserver=0, best_server=-1; | 321 | int cserver = 0, best_server = -1; |
301 | 322 | ||
302 | /* for each server */ | 323 | /* for each server */ |
303 | for(cserver=0; cserver<nservers; cserver++){ | 324 | for (cserver = 0; cserver < nservers; cserver++) { |
304 | /* We don't want any servers that fails these tests */ | 325 | /* We don't want any servers that fails these tests */ |
305 | /* Sort out servers that didn't respond or responede with a 0 stratum; | 326 | /* Sort out servers that didn't respond or responede with a 0 stratum; |
306 | * stratum 0 is for reference clocks so no NTP server should ever report | 327 | * stratum 0 is for reference clocks so no NTP server should ever report |
307 | * a stratum 0 */ | 328 | * a stratum 0 */ |
308 | if ( slist[cserver].stratum == 0){ | 329 | if (slist[cserver].stratum == 0) { |
309 | if (verbose) printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum); | 330 | if (verbose) { |
331 | printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum); | ||
332 | } | ||
310 | continue; | 333 | continue; |
311 | } | 334 | } |
312 | /* Sort out servers with error flags */ | 335 | /* Sort out servers with error flags */ |
313 | if ( LI(slist[cserver].flags) == LI_ALARM ){ | 336 | if (LI(slist[cserver].flags) == LI_ALARM) { |
314 | if (verbose) printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags)); | 337 | if (verbose) { |
338 | printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags)); | ||
339 | } | ||
315 | continue; | 340 | continue; |
316 | } | 341 | } |
317 | 342 | ||
@@ -325,13 +350,13 @@ int best_offset_server(const ntp_server_results *slist, int nservers){ | |||
325 | /* compare the server to the best one we've seen so far */ | 350 | /* compare the server to the best one we've seen so far */ |
326 | /* does it have an equal or better stratum? */ | 351 | /* does it have an equal or better stratum? */ |
327 | DBG(printf("comparing peer %d with peer %d\n", cserver, best_server)); | 352 | DBG(printf("comparing peer %d with peer %d\n", cserver, best_server)); |
328 | if(slist[cserver].stratum <= slist[best_server].stratum){ | 353 | if (slist[cserver].stratum <= slist[best_server].stratum) { |
329 | DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server)); | 354 | DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server)); |
330 | /* does it have an equal or better dispersion? */ | 355 | /* does it have an equal or better dispersion? */ |
331 | if(slist[cserver].rtdisp <= slist[best_server].rtdisp){ | 356 | if (slist[cserver].rtdisp <= slist[best_server].rtdisp) { |
332 | DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server)); | 357 | DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server)); |
333 | /* does it have a better rtdelay? */ | 358 | /* does it have a better rtdelay? */ |
334 | if(slist[cserver].rtdelay < slist[best_server].rtdelay){ | 359 | if (slist[cserver].rtdelay < slist[best_server].rtdelay) { |
335 | DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server)); | 360 | DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server)); |
336 | best_server = cserver; | 361 | best_server = cserver; |
337 | DBG(printf("peer %d is now our best candidate\n", best_server)); | 362 | DBG(printf("peer %d is now our best candidate\n", best_server)); |
@@ -340,7 +365,7 @@ int best_offset_server(const ntp_server_results *slist, int nservers){ | |||
340 | } | 365 | } |
341 | } | 366 | } |
342 | 367 | ||
343 | if(best_server >= 0) { | 368 | if (best_server >= 0) { |
344 | DBG(printf("best server selected: peer %d\n", best_server)); | 369 | DBG(printf("best server selected: peer %d\n", best_server)); |
345 | return best_server; | 370 | return best_server; |
346 | } else { | 371 | } else { |
@@ -354,16 +379,16 @@ int best_offset_server(const ntp_server_results *slist, int nservers){ | |||
354 | * we don't waste time sitting around waiting for single packets. | 379 | * we don't waste time sitting around waiting for single packets. |
355 | * - we also "manually" handle resolving host names and connecting, because | 380 | * - we also "manually" handle resolving host names and connecting, because |
356 | * we have to do it in a way that our lazy macros don't handle currently :( */ | 381 | * we have to do it in a way that our lazy macros don't handle currently :( */ |
357 | double offset_request(const char *host, int *status){ | 382 | double offset_request(const char *host, int *status) { |
358 | int i=0, ga_result=0, num_hosts=0, *socklist=NULL, respnum=0; | 383 | int i = 0, ga_result = 0, num_hosts = 0, *socklist = NULL, respnum = 0; |
359 | int servers_completed=0, one_read=0, servers_readable=0, best_index=-1; | 384 | int servers_completed = 0, one_read = 0, servers_readable = 0, best_index = -1; |
360 | time_t now_time=0, start_ts=0; | 385 | time_t now_time = 0, start_ts = 0; |
361 | ntp_message *req=NULL; | 386 | ntp_message *req = NULL; |
362 | double avg_offset=0.; | 387 | double avg_offset = 0.; |
363 | struct timeval recv_time; | 388 | struct timeval recv_time; |
364 | struct addrinfo *ai=NULL, *ai_tmp=NULL, hints; | 389 | struct addrinfo *ai = NULL, *ai_tmp = NULL, hints; |
365 | struct pollfd *ufds=NULL; | 390 | struct pollfd *ufds = NULL; |
366 | ntp_server_results *servers=NULL; | 391 | ntp_server_results *servers = NULL; |
367 | 392 | ||
368 | /* setup hints to only return results from getaddrinfo that we'd like */ | 393 | /* setup hints to only return results from getaddrinfo that we'd like */ |
369 | memset(&hints, 0, sizeof(struct addrinfo)); | 394 | memset(&hints, 0, sizeof(struct addrinfo)); |
@@ -373,97 +398,112 @@ double offset_request(const char *host, int *status){ | |||
373 | 398 | ||
374 | /* fill in ai with the list of hosts resolved by the host name */ | 399 | /* fill in ai with the list of hosts resolved by the host name */ |
375 | ga_result = getaddrinfo(host, "123", &hints, &ai); | 400 | ga_result = getaddrinfo(host, "123", &hints, &ai); |
376 | if(ga_result!=0){ | 401 | if (ga_result != 0) { |
377 | die(STATE_UNKNOWN, "error getting address for %s: %s\n", | 402 | die(STATE_UNKNOWN, "error getting address for %s: %s\n", host, gai_strerror(ga_result)); |
378 | host, gai_strerror(ga_result)); | ||
379 | } | 403 | } |
380 | 404 | ||
381 | /* count the number of returned hosts, and allocate stuff accordingly */ | 405 | /* count the number of returned hosts, and allocate stuff accordingly */ |
382 | for(ai_tmp=ai; ai_tmp!=NULL; ai_tmp=ai_tmp->ai_next){ num_hosts++; } | 406 | for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { |
383 | req=(ntp_message*)malloc(sizeof(ntp_message)*num_hosts); | 407 | num_hosts++; |
384 | if(req==NULL) die(STATE_UNKNOWN, "can not allocate ntp message array"); | 408 | } |
385 | socklist=(int*)malloc(sizeof(int)*num_hosts); | 409 | req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts); |
386 | if(socklist==NULL) die(STATE_UNKNOWN, "can not allocate socket array"); | 410 | if (req == NULL) { |
387 | ufds=(struct pollfd*)malloc(sizeof(struct pollfd)*num_hosts); | 411 | die(STATE_UNKNOWN, "can not allocate ntp message array"); |
388 | if(ufds==NULL) die(STATE_UNKNOWN, "can not allocate socket array"); | 412 | } |
389 | servers=(ntp_server_results*)malloc(sizeof(ntp_server_results)*num_hosts); | 413 | socklist = (int *)malloc(sizeof(int) * num_hosts); |
390 | if(servers==NULL) die(STATE_UNKNOWN, "can not allocate server array"); | 414 | if (socklist == NULL) { |
391 | memset(servers, 0, sizeof(ntp_server_results)*num_hosts); | 415 | die(STATE_UNKNOWN, "can not allocate socket array"); |
416 | } | ||
417 | ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * num_hosts); | ||
418 | if (ufds == NULL) { | ||
419 | die(STATE_UNKNOWN, "can not allocate socket array"); | ||
420 | } | ||
421 | servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); | ||
422 | if (servers == NULL) { | ||
423 | die(STATE_UNKNOWN, "can not allocate server array"); | ||
424 | } | ||
425 | memset(servers, 0, sizeof(ntp_server_results) * num_hosts); | ||
392 | DBG(printf("Found %d peers to check\n", num_hosts)); | 426 | DBG(printf("Found %d peers to check\n", num_hosts)); |
393 | 427 | ||
394 | /* setup each socket for writing, and the corresponding struct pollfd */ | 428 | /* setup each socket for writing, and the corresponding struct pollfd */ |
395 | ai_tmp=ai; | 429 | ai_tmp = ai; |
396 | for(i=0;ai_tmp;i++){ | 430 | for (i = 0; ai_tmp; i++) { |
397 | socklist[i]=socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); | 431 | socklist[i] = socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); |
398 | if(socklist[i] == -1) { | 432 | if (socklist[i] == -1) { |
399 | perror(NULL); | 433 | perror(NULL); |
400 | die(STATE_UNKNOWN, "can not create new socket"); | 434 | die(STATE_UNKNOWN, "can not create new socket"); |
401 | } | 435 | } |
402 | if(connect(socklist[i], ai_tmp->ai_addr, ai_tmp->ai_addrlen)){ | 436 | if (connect(socklist[i], ai_tmp->ai_addr, ai_tmp->ai_addrlen)) { |
403 | /* don't die here, because it is enough if there is one server | 437 | /* don't die here, because it is enough if there is one server |
404 | answering in time. This also would break for dual ipv4/6 stacked | 438 | answering in time. This also would break for dual ipv4/6 stacked |
405 | ntp servers when the client only supports on of them. | 439 | ntp servers when the client only supports on of them. |
406 | */ | 440 | */ |
407 | DBG(printf("can't create socket connection on peer %i: %s\n", i, strerror(errno))); | 441 | DBG(printf("can't create socket connection on peer %i: %s\n", i, strerror(errno))); |
408 | } else { | 442 | } else { |
409 | ufds[i].fd=socklist[i]; | 443 | ufds[i].fd = socklist[i]; |
410 | ufds[i].events=POLLIN; | 444 | ufds[i].events = POLLIN; |
411 | ufds[i].revents=0; | 445 | ufds[i].revents = 0; |
412 | } | 446 | } |
413 | ai_tmp = ai_tmp->ai_next; | 447 | ai_tmp = ai_tmp->ai_next; |
414 | } | 448 | } |
415 | 449 | ||
416 | /* now do AVG_NUM checks to each host. we stop before timeout/2 seconds | 450 | /* now do AVG_NUM checks to each host. we stop before timeout/2 seconds |
417 | * have passed in order to ensure post-processing and jitter time. */ | 451 | * have passed in order to ensure post-processing and jitter time. */ |
418 | now_time=start_ts=time(NULL); | 452 | now_time = start_ts = time(NULL); |
419 | while(servers_completed<num_hosts && now_time-start_ts <= socket_timeout/2){ | 453 | while (servers_completed < num_hosts && now_time - start_ts <= socket_timeout / 2) { |
420 | /* loop through each server and find each one which hasn't | 454 | /* loop through each server and find each one which hasn't |
421 | * been touched in the past second or so and is still lacking | 455 | * been touched in the past second or so and is still lacking |
422 | * some responses. for each of these servers, send a new request, | 456 | * some responses. for each of these servers, send a new request, |
423 | * and update the "waiting" timestamp with the current time. */ | 457 | * and update the "waiting" timestamp with the current time. */ |
424 | now_time=time(NULL); | 458 | now_time = time(NULL); |
425 | 459 | ||
426 | for(i=0; i<num_hosts; i++){ | 460 | for (i = 0; i < num_hosts; i++) { |
427 | if(servers[i].waiting<now_time && servers[i].num_responses<AVG_NUM){ | 461 | if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { |
428 | if(verbose && servers[i].waiting != 0) printf("re-"); | 462 | if (verbose && servers[i].waiting != 0) { |
429 | if(verbose) printf("sending request to peer %d\n", i); | 463 | printf("re-"); |
464 | } | ||
465 | if (verbose) { | ||
466 | printf("sending request to peer %d\n", i); | ||
467 | } | ||
430 | setup_request(&req[i]); | 468 | setup_request(&req[i]); |
431 | write(socklist[i], &req[i], sizeof(ntp_message)); | 469 | write(socklist[i], &req[i], sizeof(ntp_message)); |
432 | servers[i].waiting=now_time; | 470 | servers[i].waiting = now_time; |
433 | break; | 471 | break; |
434 | } | 472 | } |
435 | } | 473 | } |
436 | 474 | ||
437 | /* quickly poll for any sockets with pending data */ | 475 | /* quickly poll for any sockets with pending data */ |
438 | servers_readable=poll(ufds, num_hosts, 100); | 476 | servers_readable = poll(ufds, num_hosts, 100); |
439 | if(servers_readable==-1){ | 477 | if (servers_readable == -1) { |
440 | perror("polling ntp sockets"); | 478 | perror("polling ntp sockets"); |
441 | die(STATE_UNKNOWN, "communication errors"); | 479 | die(STATE_UNKNOWN, "communication errors"); |
442 | } | 480 | } |
443 | 481 | ||
444 | /* read from any sockets with pending data */ | 482 | /* read from any sockets with pending data */ |
445 | for(i=0; servers_readable && i<num_hosts; i++){ | 483 | for (i = 0; servers_readable && i < num_hosts; i++) { |
446 | if(ufds[i].revents&POLLIN && servers[i].num_responses < AVG_NUM){ | 484 | if (ufds[i].revents & POLLIN && servers[i].num_responses < AVG_NUM) { |
447 | if(verbose) { | 485 | if (verbose) { |
448 | printf("response from peer %d: ", i); | 486 | printf("response from peer %d: ", i); |
449 | } | 487 | } |
450 | 488 | ||
451 | read(ufds[i].fd, &req[i], sizeof(ntp_message)); | 489 | read(ufds[i].fd, &req[i], sizeof(ntp_message)); |
452 | gettimeofday(&recv_time, NULL); | 490 | gettimeofday(&recv_time, NULL); |
453 | DBG(print_ntp_message(&req[i])); | 491 | DBG(print_ntp_message(&req[i])); |
454 | respnum=servers[i].num_responses++; | 492 | respnum = servers[i].num_responses++; |
455 | servers[i].offset[respnum]=calc_offset(&req[i], &recv_time); | 493 | servers[i].offset[respnum] = calc_offset(&req[i], &recv_time); |
456 | if(verbose) { | 494 | if (verbose) { |
457 | printf("offset %.10g\n", servers[i].offset[respnum]); | 495 | printf("offset %.10g\n", servers[i].offset[respnum]); |
458 | } | 496 | } |
459 | servers[i].stratum=req[i].stratum; | 497 | servers[i].stratum = req[i].stratum; |
460 | servers[i].rtdisp=NTP32asDOUBLE(req[i].rtdisp); | 498 | servers[i].rtdisp = NTP32asDOUBLE(req[i].rtdisp); |
461 | servers[i].rtdelay=NTP32asDOUBLE(req[i].rtdelay); | 499 | servers[i].rtdelay = NTP32asDOUBLE(req[i].rtdelay); |
462 | servers[i].waiting=0; | 500 | servers[i].waiting = 0; |
463 | servers[i].flags=req[i].flags; | 501 | servers[i].flags = req[i].flags; |
464 | servers_readable--; | 502 | servers_readable--; |
465 | one_read = 1; | 503 | one_read = 1; |
466 | if(servers[i].num_responses==AVG_NUM) servers_completed++; | 504 | if (servers[i].num_responses == AVG_NUM) { |
505 | servers_completed++; | ||
506 | } | ||
467 | } | 507 | } |
468 | } | 508 | } |
469 | /* lather, rinse, repeat. */ | 509 | /* lather, rinse, repeat. */ |
@@ -474,15 +514,15 @@ double offset_request(const char *host, int *status){ | |||
474 | } | 514 | } |
475 | 515 | ||
476 | /* now, pick the best server from the list */ | 516 | /* now, pick the best server from the list */ |
477 | best_index=best_offset_server(servers, num_hosts); | 517 | best_index = best_offset_server(servers, num_hosts); |
478 | if(best_index < 0){ | 518 | if (best_index < 0) { |
479 | *status=STATE_UNKNOWN; | 519 | *status = STATE_UNKNOWN; |
480 | } else { | 520 | } else { |
481 | /* finally, calculate the average offset */ | 521 | /* finally, calculate the average offset */ |
482 | for(i=0; i<servers[best_index].num_responses;i++){ | 522 | for (i = 0; i < servers[best_index].num_responses; i++) { |
483 | avg_offset+=servers[best_index].offset[i]; | 523 | avg_offset += servers[best_index].offset[i]; |
484 | } | 524 | } |
485 | avg_offset/=servers[best_index].num_responses; | 525 | avg_offset /= servers[best_index].num_responses; |
486 | } | 526 | } |
487 | 527 | ||
488 | /* cleanup */ | 528 | /* cleanup */ |
@@ -496,12 +536,13 @@ double offset_request(const char *host, int *status){ | |||
496 | free(req); | 536 | free(req); |
497 | freeaddrinfo(ai); | 537 | freeaddrinfo(ai); |
498 | 538 | ||
499 | if(verbose) printf("overall average offset: %.10g\n", avg_offset); | 539 | if (verbose) { |
540 | printf("overall average offset: %.10g\n", avg_offset); | ||
541 | } | ||
500 | return avg_offset; | 542 | return avg_offset; |
501 | } | 543 | } |
502 | 544 | ||
503 | void | 545 | void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq) { |
504 | setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ | ||
505 | memset(p, 0, sizeof(ntp_control_message)); | 546 | memset(p, 0, sizeof(ntp_control_message)); |
506 | LI_SET(p->flags, LI_NOWARNING); | 547 | LI_SET(p->flags, LI_NOWARNING); |
507 | VN_SET(p->flags, VN_RESERVED); | 548 | VN_SET(p->flags, VN_RESERVED); |
@@ -512,16 +553,16 @@ setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ | |||
512 | } | 553 | } |
513 | 554 | ||
514 | /* XXX handle responses with the error bit set */ | 555 | /* XXX handle responses with the error bit set */ |
515 | double jitter_request(int *status){ | 556 | double jitter_request(int *status) { |
516 | int conn=-1, i, npeers=0, num_candidates=0; | 557 | int conn = -1, i, npeers = 0, num_candidates = 0; |
517 | bool syncsource_found = false; | 558 | bool syncsource_found = false; |
518 | int run=0, min_peer_sel=PEER_INCLUDED, num_selected=0, num_valid=0; | 559 | int run = 0, min_peer_sel = PEER_INCLUDED, num_selected = 0, num_valid = 0; |
519 | int peers_size=0, peer_offset=0; | 560 | int peers_size = 0, peer_offset = 0; |
520 | ntp_assoc_status_pair *peers=NULL; | 561 | ntp_assoc_status_pair *peers = NULL; |
521 | ntp_control_message req; | 562 | ntp_control_message req; |
522 | const char *getvar = "jitter"; | 563 | const char *getvar = "jitter"; |
523 | double rval = 0.0, jitter = -1.0; | 564 | double rval = 0.0, jitter = -1.0; |
524 | char *startofvalue=NULL, *nptr=NULL; | 565 | char *startofvalue = NULL, *nptr = NULL; |
525 | void *tmp; | 566 | void *tmp; |
526 | 567 | ||
527 | /* Long-winded explanation: | 568 | /* Long-winded explanation: |
@@ -542,54 +583,62 @@ double jitter_request(int *status){ | |||
542 | 583 | ||
543 | /* keep sending requests until the server stops setting the | 584 | /* keep sending requests until the server stops setting the |
544 | * REM_MORE bit, though usually this is only 1 packet. */ | 585 | * REM_MORE bit, though usually this is only 1 packet. */ |
545 | do{ | 586 | do { |
546 | setup_control_request(&req, OP_READSTAT, 1); | 587 | setup_control_request(&req, OP_READSTAT, 1); |
547 | DBG(printf("sending READSTAT request")); | 588 | DBG(printf("sending READSTAT request")); |
548 | write(conn, &req, SIZEOF_NTPCM(req)); | 589 | write(conn, &req, SIZEOF_NTPCM(req)); |
549 | DBG(print_ntp_control_message(&req)); | 590 | DBG(print_ntp_control_message(&req)); |
550 | /* Attempt to read the largest size packet possible */ | 591 | /* Attempt to read the largest size packet possible */ |
551 | req.count=htons(MAX_CM_SIZE); | 592 | req.count = htons(MAX_CM_SIZE); |
552 | DBG(printf("receiving READSTAT response")) | 593 | DBG(printf("receiving READSTAT response")) |
553 | read(conn, &req, SIZEOF_NTPCM(req)); | 594 | read(conn, &req, SIZEOF_NTPCM(req)); |
554 | DBG(print_ntp_control_message(&req)); | 595 | DBG(print_ntp_control_message(&req)); |
555 | /* Each peer identifier is 4 bytes in the data section, which | 596 | /* Each peer identifier is 4 bytes in the data section, which |
556 | * we represent as a ntp_assoc_status_pair datatype. | 597 | * we represent as a ntp_assoc_status_pair datatype. |
557 | */ | 598 | */ |
558 | peers_size+=ntohs(req.count); | 599 | peers_size += ntohs(req.count); |
559 | if((tmp=realloc(peers, peers_size)) == NULL) | 600 | if ((tmp = realloc(peers, peers_size)) == NULL) { |
560 | free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); | 601 | free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); |
561 | peers=tmp; | 602 | } |
562 | memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count)); | 603 | peers = tmp; |
563 | npeers=peers_size/sizeof(ntp_assoc_status_pair); | 604 | memcpy((void *)((ptrdiff_t)peers + peer_offset), (void *)req.data, ntohs(req.count)); |
564 | peer_offset+=ntohs(req.count); | 605 | npeers = peers_size / sizeof(ntp_assoc_status_pair); |
565 | } while(req.op&REM_MORE); | 606 | peer_offset += ntohs(req.count); |
607 | } while (req.op & REM_MORE); | ||
566 | 608 | ||
567 | /* first, let's find out if we have a sync source, or if there are | 609 | /* first, let's find out if we have a sync source, or if there are |
568 | * at least some candidates. in the case of the latter we'll issue | 610 | * at least some candidates. in the case of the latter we'll issue |
569 | * a warning but go ahead with the check on them. */ | 611 | * a warning but go ahead with the check on them. */ |
570 | for (i = 0; i < npeers; i++){ | 612 | for (i = 0; i < npeers; i++) { |
571 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED){ | 613 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { |
572 | num_candidates++; | 614 | num_candidates++; |
573 | if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){ | 615 | if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) { |
574 | syncsource_found = true; | 616 | syncsource_found = true; |
575 | min_peer_sel=PEER_SYNCSOURCE; | 617 | min_peer_sel = PEER_SYNCSOURCE; |
576 | } | 618 | } |
577 | } | 619 | } |
578 | } | 620 | } |
579 | if(verbose) printf("%d candidate peers available\n", num_candidates); | 621 | if (verbose) { |
580 | if(verbose && syncsource_found) printf("synchronization source found\n"); | 622 | printf("%d candidate peers available\n", num_candidates); |
581 | if(! syncsource_found){ | 623 | } |
624 | if (verbose && syncsource_found) { | ||
625 | printf("synchronization source found\n"); | ||
626 | } | ||
627 | if (!syncsource_found) { | ||
582 | *status = STATE_UNKNOWN; | 628 | *status = STATE_UNKNOWN; |
583 | if(verbose) printf("warning: no synchronization source found\n"); | 629 | if (verbose) { |
630 | printf("warning: no synchronization source found\n"); | ||
631 | } | ||
584 | } | 632 | } |
585 | 633 | ||
586 | 634 | for (run = 0; run < AVG_NUM; run++) { | |
587 | for (run=0; run<AVG_NUM; run++){ | 635 | if (verbose) { |
588 | if(verbose) printf("jitter run %d of %d\n", run+1, AVG_NUM); | 636 | printf("jitter run %d of %d\n", run + 1, AVG_NUM); |
589 | for (i = 0; i < npeers; i++){ | 637 | } |
638 | for (i = 0; i < npeers; i++) { | ||
590 | /* Only query this server if it is the current sync source */ | 639 | /* Only query this server if it is the current sync source */ |
591 | if (PEER_SEL(peers[i].status) >= min_peer_sel){ | 640 | if (PEER_SEL(peers[i].status) >= min_peer_sel) { |
592 | char jitter_data[MAX_CM_SIZE+1]; | 641 | char jitter_data[MAX_CM_SIZE + 1]; |
593 | size_t jitter_data_count; | 642 | size_t jitter_data_count; |
594 | 643 | ||
595 | num_selected++; | 644 | num_selected++; |
@@ -602,7 +651,7 @@ double jitter_request(int *status){ | |||
602 | */ | 651 | */ |
603 | /* Older servers doesn't know what jitter is, so if we get an | 652 | /* Older servers doesn't know what jitter is, so if we get an |
604 | * error on the first pass we redo it with "dispersion" */ | 653 | * error on the first pass we redo it with "dispersion" */ |
605 | strncpy(req.data, getvar, MAX_CM_SIZE-1); | 654 | strncpy(req.data, getvar, MAX_CM_SIZE - 1); |
606 | req.count = htons(strlen(getvar)); | 655 | req.count = htons(strlen(getvar)); |
607 | DBG(printf("sending READVAR request...\n")); | 656 | DBG(printf("sending READVAR request...\n")); |
608 | write(conn, &req, SIZEOF_NTPCM(req)); | 657 | write(conn, &req, SIZEOF_NTPCM(req)); |
@@ -613,8 +662,11 @@ double jitter_request(int *status){ | |||
613 | read(conn, &req, SIZEOF_NTPCM(req)); | 662 | read(conn, &req, SIZEOF_NTPCM(req)); |
614 | DBG(print_ntp_control_message(&req)); | 663 | DBG(print_ntp_control_message(&req)); |
615 | 664 | ||
616 | if(req.op&REM_ERROR && strstr(getvar, "jitter")) { | 665 | if (req.op & REM_ERROR && strstr(getvar, "jitter")) { |
617 | if(verbose) printf("The 'jitter' command failed (old ntp server?)\nRestarting with 'dispersion'...\n"); | 666 | if (verbose) { |
667 | printf("The 'jitter' command failed (old ntp server?)\nRestarting with " | ||
668 | "'dispersion'...\n"); | ||
669 | } | ||
618 | getvar = "dispersion"; | 670 | getvar = "dispersion"; |
619 | num_selected--; | 671 | num_selected--; |
620 | i--; | 672 | i--; |
@@ -622,32 +674,33 @@ double jitter_request(int *status){ | |||
622 | } | 674 | } |
623 | 675 | ||
624 | /* get to the float value */ | 676 | /* get to the float value */ |
625 | if(verbose) { | 677 | if (verbose) { |
626 | printf("parsing jitter from peer %.2x: ", ntohs(peers[i].assoc)); | 678 | printf("parsing jitter from peer %.2x: ", ntohs(peers[i].assoc)); |
627 | } | 679 | } |
628 | if((jitter_data_count = ntohs(req.count)) >= sizeof(jitter_data)){ | 680 | if ((jitter_data_count = ntohs(req.count)) >= sizeof(jitter_data)) { |
629 | die(STATE_UNKNOWN, | 681 | die(STATE_UNKNOWN, _("jitter response too large (%lu bytes)\n"), |
630 | _("jitter response too large (%lu bytes)\n"), | 682 | (unsigned long)jitter_data_count); |
631 | (unsigned long)jitter_data_count); | ||
632 | } | 683 | } |
633 | memcpy(jitter_data, req.data, jitter_data_count); | 684 | memcpy(jitter_data, req.data, jitter_data_count); |
634 | jitter_data[jitter_data_count] = '\0'; | 685 | jitter_data[jitter_data_count] = '\0'; |
635 | startofvalue = strchr(jitter_data, '='); | 686 | startofvalue = strchr(jitter_data, '='); |
636 | if(startofvalue != NULL) { | 687 | if (startofvalue != NULL) { |
637 | startofvalue++; | 688 | startofvalue++; |
638 | jitter = strtod(startofvalue, &nptr); | 689 | jitter = strtod(startofvalue, &nptr); |
639 | } | 690 | } |
640 | if(startofvalue == NULL || startofvalue==nptr){ | 691 | if (startofvalue == NULL || startofvalue == nptr) { |
641 | printf("warning: unable to read server jitter response.\n"); | 692 | printf("warning: unable to read server jitter response.\n"); |
642 | *status = STATE_UNKNOWN; | 693 | *status = STATE_UNKNOWN; |
643 | } else { | 694 | } else { |
644 | if(verbose) printf("%g\n", jitter); | 695 | if (verbose) { |
696 | printf("%g\n", jitter); | ||
697 | } | ||
645 | num_valid++; | 698 | num_valid++; |
646 | rval += jitter; | 699 | rval += jitter; |
647 | } | 700 | } |
648 | } | 701 | } |
649 | } | 702 | } |
650 | if(verbose){ | 703 | if (verbose) { |
651 | printf("jitter parsed from %d/%d peers\n", num_valid, num_selected); | 704 | printf("jitter parsed from %d/%d peers\n", num_valid, num_selected); |
652 | } | 705 | } |
653 | } | 706 | } |
@@ -655,37 +708,33 @@ double jitter_request(int *status){ | |||
655 | rval = num_valid ? rval / num_valid : -1.0; | 708 | rval = num_valid ? rval / num_valid : -1.0; |
656 | 709 | ||
657 | close(conn); | 710 | close(conn); |
658 | if(peers!=NULL) free(peers); | 711 | if (peers != NULL) { |
712 | free(peers); | ||
713 | } | ||
659 | /* If we return -1.0, it means no synchronization source was found */ | 714 | /* If we return -1.0, it means no synchronization source was found */ |
660 | return rval; | 715 | return rval; |
661 | } | 716 | } |
662 | 717 | ||
663 | int process_arguments(int argc, char **argv){ | 718 | int process_arguments(int argc, char **argv) { |
664 | int c; | 719 | int c; |
665 | int option=0; | 720 | int option = 0; |
666 | static struct option longopts[] = { | 721 | static struct option longopts[] = { |
667 | {"version", no_argument, 0, 'V'}, | 722 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, |
668 | {"help", no_argument, 0, 'h'}, | 723 | {"verbose", no_argument, 0, 'v'}, {"use-ipv4", no_argument, 0, '4'}, |
669 | {"verbose", no_argument, 0, 'v'}, | 724 | {"use-ipv6", no_argument, 0, '6'}, {"warning", required_argument, 0, 'w'}, |
670 | {"use-ipv4", no_argument, 0, '4'}, | 725 | {"critical", required_argument, 0, 'c'}, {"jwarn", required_argument, 0, 'j'}, |
671 | {"use-ipv6", no_argument, 0, '6'}, | 726 | {"jcrit", required_argument, 0, 'k'}, {"timeout", required_argument, 0, 't'}, |
672 | {"warning", required_argument, 0, 'w'}, | 727 | {"hostname", required_argument, 0, 'H'}, {0, 0, 0, 0}}; |
673 | {"critical", required_argument, 0, 'c'}, | 728 | |
674 | {"jwarn", required_argument, 0, 'j'}, | 729 | if (argc < 2) { |
675 | {"jcrit", required_argument, 0, 'k'}, | 730 | usage("\n"); |
676 | {"timeout", required_argument, 0, 't'}, | 731 | } |
677 | {"hostname", required_argument, 0, 'H'}, | ||
678 | {0, 0, 0, 0} | ||
679 | }; | ||
680 | |||
681 | |||
682 | if (argc < 2) | ||
683 | usage ("\n"); | ||
684 | 732 | ||
685 | while (1) { | 733 | while (1) { |
686 | c = getopt_long (argc, argv, "Vhv46w:c:j:k:t:H:", longopts, &option); | 734 | c = getopt_long(argc, argv, "Vhv46w:c:j:k:t:H:", longopts, &option); |
687 | if (c == -1 || c == EOF || c == 1) | 735 | if (c == -1 || c == EOF || c == 1) { |
688 | break; | 736 | break; |
737 | } | ||
689 | 738 | ||
690 | switch (c) { | 739 | switch (c) { |
691 | case 'h': | 740 | case 'h': |
@@ -716,12 +765,13 @@ int process_arguments(int argc, char **argv){ | |||
716 | jcrit = optarg; | 765 | jcrit = optarg; |
717 | break; | 766 | break; |
718 | case 'H': | 767 | case 'H': |
719 | if(!is_host(optarg)) | 768 | if (!is_host(optarg)) { |
720 | usage2(_("Invalid hostname/address"), optarg); | 769 | usage2(_("Invalid hostname/address"), optarg); |
770 | } | ||
721 | server_address = strdup(optarg); | 771 | server_address = strdup(optarg); |
722 | break; | 772 | break; |
723 | case 't': | 773 | case 't': |
724 | socket_timeout=atoi(optarg); | 774 | socket_timeout = atoi(optarg); |
725 | break; | 775 | break; |
726 | case '4': | 776 | case '4': |
727 | address_family = AF_INET; | 777 | address_family = AF_INET; |
@@ -730,64 +780,59 @@ int process_arguments(int argc, char **argv){ | |||
730 | #ifdef USE_IPV6 | 780 | #ifdef USE_IPV6 |
731 | address_family = AF_INET6; | 781 | address_family = AF_INET6; |
732 | #else | 782 | #else |
733 | usage4 (_("IPv6 support not available")); | 783 | usage4(_("IPv6 support not available")); |
734 | #endif | 784 | #endif |
735 | break; | 785 | break; |
736 | case '?': | 786 | case '?': |
737 | /* print short usage statement if args not parsable */ | 787 | /* print short usage statement if args not parsable */ |
738 | usage5 (); | 788 | usage5(); |
739 | break; | 789 | break; |
740 | } | 790 | } |
741 | } | 791 | } |
742 | 792 | ||
743 | if(server_address == NULL){ | 793 | if (server_address == NULL) { |
744 | usage4(_("Hostname was not supplied")); | 794 | usage4(_("Hostname was not supplied")); |
745 | } | 795 | } |
746 | 796 | ||
747 | return 0; | 797 | return 0; |
748 | } | 798 | } |
749 | 799 | ||
750 | char *perfd_offset (double offset) | 800 | char *perfd_offset(double offset) { |
751 | { | 801 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, |
752 | return fperfdata ("offset", offset, "s", | 802 | offset_thresholds->critical->end, false, 0, false, 0); |
753 | true, offset_thresholds->warning->end, | ||
754 | true, offset_thresholds->critical->end, | ||
755 | false, 0, false, 0); | ||
756 | } | 803 | } |
757 | 804 | ||
758 | char *perfd_jitter (double jitter) | 805 | char *perfd_jitter(double jitter) { |
759 | { | 806 | return fperfdata("jitter", jitter, "s", do_jitter, jitter_thresholds->warning->end, do_jitter, |
760 | return fperfdata ("jitter", jitter, "s", | 807 | jitter_thresholds->critical->end, true, 0, false, 0); |
761 | do_jitter, jitter_thresholds->warning->end, | ||
762 | do_jitter, jitter_thresholds->critical->end, | ||
763 | true, 0, false, 0); | ||
764 | } | 808 | } |
765 | 809 | ||
766 | int main(int argc, char *argv[]){ | 810 | int main(int argc, char *argv[]) { |
767 | int result, offset_result, jitter_result; | 811 | int result, offset_result, jitter_result; |
768 | double offset=0, jitter=0; | 812 | double offset = 0, jitter = 0; |
769 | char *result_line, *perfdata_line; | 813 | char *result_line, *perfdata_line; |
770 | 814 | ||
771 | setlocale (LC_ALL, ""); | 815 | setlocale(LC_ALL, ""); |
772 | bindtextdomain (PACKAGE, LOCALEDIR); | 816 | bindtextdomain(PACKAGE, LOCALEDIR); |
773 | textdomain (PACKAGE); | 817 | textdomain(PACKAGE); |
774 | 818 | ||
775 | result = offset_result = jitter_result = STATE_OK; | 819 | result = offset_result = jitter_result = STATE_OK; |
776 | 820 | ||
777 | /* Parse extra opts if any */ | 821 | /* Parse extra opts if any */ |
778 | argv=np_extra_opts (&argc, argv, progname); | 822 | argv = np_extra_opts(&argc, argv, progname); |
779 | 823 | ||
780 | if (process_arguments (argc, argv) == ERROR) | 824 | if (process_arguments(argc, argv) == ERROR) { |
781 | usage4 (_("Could not parse arguments")); | 825 | usage4(_("Could not parse arguments")); |
826 | } | ||
782 | 827 | ||
783 | set_thresholds(&offset_thresholds, owarn, ocrit); | 828 | set_thresholds(&offset_thresholds, owarn, ocrit); |
784 | set_thresholds(&jitter_thresholds, jwarn, jcrit); | 829 | set_thresholds(&jitter_thresholds, jwarn, jcrit); |
785 | 830 | ||
786 | /* initialize alarm signal handling */ | 831 | /* initialize alarm signal handling */ |
787 | signal (SIGALRM, socket_timeout_alarm_handler); | 832 | signal(SIGALRM, socket_timeout_alarm_handler); |
788 | 833 | ||
789 | /* set socket timeout */ | 834 | /* set socket timeout */ |
790 | alarm (socket_timeout); | 835 | alarm(socket_timeout); |
791 | 836 | ||
792 | offset = offset_request(server_address, &offset_result); | 837 | offset = offset_request(server_address, &offset_result); |
793 | /* check_ntp used to always return CRITICAL if offset_result == STATE_UNKNOWN. | 838 | /* check_ntp used to always return CRITICAL if offset_result == STATE_UNKNOWN. |
@@ -803,31 +848,32 @@ int main(int argc, char *argv[]){ | |||
803 | * servers recognize. Trying to check the jitter on OpenNTPD | 848 | * servers recognize. Trying to check the jitter on OpenNTPD |
804 | * (for example) will result in an error | 849 | * (for example) will result in an error |
805 | */ | 850 | */ |
806 | if(do_jitter){ | 851 | if (do_jitter) { |
807 | jitter=jitter_request(&jitter_result); | 852 | jitter = jitter_request(&jitter_result); |
808 | result = max_state_alt(result, get_status(jitter, jitter_thresholds)); | 853 | result = max_state_alt(result, get_status(jitter, jitter_thresholds)); |
809 | /* -1 indicates that we couldn't calculate the jitter | 854 | /* -1 indicates that we couldn't calculate the jitter |
810 | * Only overrides STATE_OK from the offset */ | 855 | * Only overrides STATE_OK from the offset */ |
811 | if(jitter == -1.0 && result == STATE_OK) | 856 | if (jitter == -1.0 && result == STATE_OK) { |
812 | result = STATE_UNKNOWN; | 857 | result = STATE_UNKNOWN; |
858 | } | ||
813 | } | 859 | } |
814 | result = max_state_alt(result, jitter_result); | 860 | result = max_state_alt(result, jitter_result); |
815 | 861 | ||
816 | switch (result) { | 862 | switch (result) { |
817 | case STATE_CRITICAL : | 863 | case STATE_CRITICAL: |
818 | xasprintf(&result_line, _("NTP CRITICAL:")); | 864 | xasprintf(&result_line, _("NTP CRITICAL:")); |
819 | break; | 865 | break; |
820 | case STATE_WARNING : | 866 | case STATE_WARNING: |
821 | xasprintf(&result_line, _("NTP WARNING:")); | 867 | xasprintf(&result_line, _("NTP WARNING:")); |
822 | break; | 868 | break; |
823 | case STATE_OK : | 869 | case STATE_OK: |
824 | xasprintf(&result_line, _("NTP OK:")); | 870 | xasprintf(&result_line, _("NTP OK:")); |
825 | break; | 871 | break; |
826 | default : | 872 | default: |
827 | xasprintf(&result_line, _("NTP UNKNOWN:")); | 873 | xasprintf(&result_line, _("NTP UNKNOWN:")); |
828 | break; | 874 | break; |
829 | } | 875 | } |
830 | if(offset_result == STATE_UNKNOWN){ | 876 | if (offset_result == STATE_UNKNOWN) { |
831 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | 877 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); |
832 | xasprintf(&perfdata_line, ""); | 878 | xasprintf(&perfdata_line, ""); |
833 | } else { | 879 | } else { |
@@ -836,41 +882,41 @@ int main(int argc, char *argv[]){ | |||
836 | } | 882 | } |
837 | if (do_jitter) { | 883 | if (do_jitter) { |
838 | xasprintf(&result_line, "%s, jitter=%f", result_line, jitter); | 884 | xasprintf(&result_line, "%s, jitter=%f", result_line, jitter); |
839 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter)); | 885 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter)); |
840 | } | 886 | } |
841 | printf("%s|%s\n", result_line, perfdata_line); | 887 | printf("%s|%s\n", result_line, perfdata_line); |
842 | 888 | ||
843 | if(server_address!=NULL) free(server_address); | 889 | if (server_address != NULL) { |
890 | free(server_address); | ||
891 | } | ||
844 | return result; | 892 | return result; |
845 | } | 893 | } |
846 | 894 | ||
847 | 895 | void print_help(void) { | |
848 | |||
849 | void print_help(void){ | ||
850 | print_revision(progname, NP_VERSION); | 896 | print_revision(progname, NP_VERSION); |
851 | 897 | ||
852 | printf ("Copyright (c) 2006 Sean Finney\n"); | 898 | printf("Copyright (c) 2006 Sean Finney\n"); |
853 | printf (COPYRIGHT, copyright, email); | 899 | printf(COPYRIGHT, copyright, email); |
854 | 900 | ||
855 | printf ("%s\n", _("This plugin checks the selected ntp server")); | 901 | printf("%s\n", _("This plugin checks the selected ntp server")); |
856 | 902 | ||
857 | printf ("\n\n"); | 903 | printf("\n\n"); |
858 | 904 | ||
859 | print_usage(); | 905 | print_usage(); |
860 | printf (UT_HELP_VRSN); | 906 | printf(UT_HELP_VRSN); |
861 | printf (UT_EXTRA_OPTS); | 907 | printf(UT_EXTRA_OPTS); |
862 | printf (UT_HOST_PORT, 'p', "123"); | 908 | printf(UT_HOST_PORT, 'p', "123"); |
863 | printf (UT_IPv46); | 909 | printf(UT_IPv46); |
864 | printf (" %s\n", "-w, --warning=THRESHOLD"); | 910 | printf(" %s\n", "-w, --warning=THRESHOLD"); |
865 | printf (" %s\n", _("Offset to result in warning status (seconds)")); | 911 | printf(" %s\n", _("Offset to result in warning status (seconds)")); |
866 | printf (" %s\n", "-c, --critical=THRESHOLD"); | 912 | printf(" %s\n", "-c, --critical=THRESHOLD"); |
867 | printf (" %s\n", _("Offset to result in critical status (seconds)")); | 913 | printf(" %s\n", _("Offset to result in critical status (seconds)")); |
868 | printf (" %s\n", "-j, --jwarn=THRESHOLD"); | 914 | printf(" %s\n", "-j, --jwarn=THRESHOLD"); |
869 | printf (" %s\n", _("Warning threshold for jitter")); | 915 | printf(" %s\n", _("Warning threshold for jitter")); |
870 | printf (" %s\n", "-k, --jcrit=THRESHOLD"); | 916 | printf(" %s\n", "-k, --jcrit=THRESHOLD"); |
871 | printf (" %s\n", _("Critical threshold for jitter")); | 917 | printf(" %s\n", _("Critical threshold for jitter")); |
872 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 918 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
873 | printf (UT_VERBOSE); | 919 | printf(UT_VERBOSE); |
874 | 920 | ||
875 | printf("\n"); | 921 | printf("\n"); |
876 | printf("%s\n", _("Notes:")); | 922 | printf("%s\n", _("Notes:")); |
@@ -881,21 +927,21 @@ void print_help(void){ | |||
881 | printf(" %s\n", _("Normal offset check:")); | 927 | printf(" %s\n", _("Normal offset check:")); |
882 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1")); | 928 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1")); |
883 | printf("\n"); | 929 | printf("\n"); |
884 | printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available")); | 930 | printf(" %s\n", |
931 | _("Check jitter too, avoiding critical notifications if jitter isn't available")); | ||
885 | printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); | 932 | printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); |
886 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); | 933 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); |
887 | 934 | ||
888 | printf (UT_SUPPORT); | 935 | printf(UT_SUPPORT); |
889 | 936 | ||
890 | printf ("%s\n", _("WARNING: check_ntp is deprecated. Please use check_ntp_peer or")); | 937 | printf("%s\n", _("WARNING: check_ntp is deprecated. Please use check_ntp_peer or")); |
891 | printf ("%s\n\n", _("check_ntp_time instead.")); | 938 | printf("%s\n\n", _("check_ntp_time instead.")); |
892 | } | 939 | } |
893 | 940 | ||
894 | void | 941 | void print_usage(void) { |
895 | print_usage(void) | 942 | printf("%s\n", _("WARNING: check_ntp is deprecated. Please use check_ntp_peer or")); |
896 | { | 943 | printf("%s\n\n", _("check_ntp_time instead.")); |
897 | printf ("%s\n", _("WARNING: check_ntp is deprecated. Please use check_ntp_peer or")); | 944 | printf("%s\n", _("Usage:")); |
898 | printf ("%s\n\n", _("check_ntp_time instead.")); | 945 | printf(" %s -H <host> [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-4|-6] [-v verbose]\n", |
899 | printf ("%s\n", _("Usage:")); | 946 | progname); |
900 | printf(" %s -H <host> [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-4|-6] [-v verbose]\n", progname); | ||
901 | } | 947 | } |
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c index 6e76bf23..24d1c9b5 100644 --- a/plugins/check_ntp_peer.c +++ b/plugins/check_ntp_peer.c | |||
@@ -83,9 +83,9 @@ typedef struct { | |||
83 | /* bits 1,2 are the leap indicator */ | 83 | /* bits 1,2 are the leap indicator */ |
84 | #define LI_MASK 0xc0 | 84 | #define LI_MASK 0xc0 |
85 | #define LI(x) ((x & LI_MASK) >> 6) | 85 | #define LI(x) ((x & LI_MASK) >> 6) |
86 | #define LI_SET(x, y) \ | 86 | #define LI_SET(x, y) \ |
87 | do { \ | 87 | do { \ |
88 | x |= ((y << 6) & LI_MASK); \ | 88 | x |= ((y << 6) & LI_MASK); \ |
89 | } while (0) | 89 | } while (0) |
90 | /* and these are the values of the leap indicator */ | 90 | /* and these are the values of the leap indicator */ |
91 | #define LI_NOWARNING 0x00 | 91 | #define LI_NOWARNING 0x00 |
@@ -95,17 +95,17 @@ typedef struct { | |||
95 | /* bits 3,4,5 are the ntp version */ | 95 | /* bits 3,4,5 are the ntp version */ |
96 | #define VN_MASK 0x38 | 96 | #define VN_MASK 0x38 |
97 | #define VN(x) ((x & VN_MASK) >> 3) | 97 | #define VN(x) ((x & VN_MASK) >> 3) |
98 | #define VN_SET(x, y) \ | 98 | #define VN_SET(x, y) \ |
99 | do { \ | 99 | do { \ |
100 | x |= ((y << 3) & VN_MASK); \ | 100 | x |= ((y << 3) & VN_MASK); \ |
101 | } while (0) | 101 | } while (0) |
102 | #define VN_RESERVED 0x02 | 102 | #define VN_RESERVED 0x02 |
103 | /* bits 6,7,8 are the ntp mode */ | 103 | /* bits 6,7,8 are the ntp mode */ |
104 | #define MODE_MASK 0x07 | 104 | #define MODE_MASK 0x07 |
105 | #define MODE(x) (x & MODE_MASK) | 105 | #define MODE(x) (x & MODE_MASK) |
106 | #define MODE_SET(x, y) \ | 106 | #define MODE_SET(x, y) \ |
107 | do { \ | 107 | do { \ |
108 | x |= (y & MODE_MASK); \ | 108 | x |= (y & MODE_MASK); \ |
109 | } while (0) | 109 | } while (0) |
110 | /* here are some values */ | 110 | /* here are some values */ |
111 | #define MODE_CLIENT 0x03 | 111 | #define MODE_CLIENT 0x03 |
@@ -117,9 +117,9 @@ typedef struct { | |||
117 | #define REM_MORE 0x20 | 117 | #define REM_MORE 0x20 |
118 | /* In control message, bits 11 - 15 are opcode */ | 118 | /* In control message, bits 11 - 15 are opcode */ |
119 | #define OP_MASK 0x1f | 119 | #define OP_MASK 0x1f |
120 | #define OP_SET(x, y) \ | 120 | #define OP_SET(x, y) \ |
121 | do { \ | 121 | do { \ |
122 | x |= (y & OP_MASK); \ | 122 | x |= (y & OP_MASK); \ |
123 | } while (0) | 123 | } while (0) |
124 | #define OP_READSTAT 0x01 | 124 | #define OP_READSTAT 0x01 |
125 | #define OP_READVAR 0x02 | 125 | #define OP_READVAR 0x02 |
@@ -132,18 +132,19 @@ typedef struct { | |||
132 | /* NTP control message header is 12 bytes, plus any data in the data | 132 | /* NTP control message header is 12 bytes, plus any data in the data |
133 | * field, plus null padding to the nearest 32-bit boundary per rfc. | 133 | * field, plus null padding to the nearest 32-bit boundary per rfc. |
134 | */ | 134 | */ |
135 | #define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0)) | 135 | #define SIZEOF_NTPCM(m) \ |
136 | (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0)) | ||
136 | 137 | ||
137 | /* finally, a little helper or two for debugging: */ | 138 | /* finally, a little helper or two for debugging: */ |
138 | #define DBG(x) \ | 139 | #define DBG(x) \ |
139 | do { \ | 140 | do { \ |
140 | if (verbose > 1) { \ | 141 | if (verbose > 1) { \ |
141 | x; \ | 142 | x; \ |
142 | } \ | 143 | } \ |
143 | } while (0); | 144 | } while (0); |
144 | #define PRINTSOCKADDR(x) \ | 145 | #define PRINTSOCKADDR(x) \ |
145 | do { \ | 146 | do { \ |
146 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ | 147 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ |
147 | } while (0); | 148 | } while (0); |
148 | 149 | ||
149 | void print_ntp_control_message(const ntp_control_message *message) { | 150 | void print_ntp_control_message(const ntp_control_message *message) { |
@@ -360,7 +361,8 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) { | |||
360 | if (req.op & REM_ERROR) { | 361 | if (req.op & REM_ERROR) { |
361 | if (strstr(getvar, "jitter")) { | 362 | if (strstr(getvar, "jitter")) { |
362 | if (verbose) { | 363 | if (verbose) { |
363 | printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with " | 364 | printf("The command failed. This is usually caused by servers refusing the " |
365 | "'jitter'\nvariable. Restarting with " | ||
364 | "'dispersion'...\n"); | 366 | "'dispersion'...\n"); |
365 | } | 367 | } |
366 | getvar = "stratum,offset,dispersion"; | 368 | getvar = "stratum,offset,dispersion"; |
@@ -404,7 +406,8 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) { | |||
404 | if (verbose) { | 406 | if (verbose) { |
405 | printf("%.10g\n", tmp_offset); | 407 | printf("%.10g\n", tmp_offset); |
406 | } | 408 | } |
407 | if (result.offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(result.offset)) { | 409 | if (result.offset_result == STATE_UNKNOWN || |
410 | fabs(tmp_offset) < fabs(result.offset)) { | ||
408 | result.offset = tmp_offset; | 411 | result.offset = tmp_offset; |
409 | result.offset_result = STATE_OK; | 412 | result.offset_result = STATE_OK; |
410 | } else { | 413 | } else { |
@@ -416,10 +419,12 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) { | |||
416 | if (config.do_jitter) { | 419 | if (config.do_jitter) { |
417 | /* get the jitter */ | 420 | /* get the jitter */ |
418 | if (verbose) { | 421 | if (verbose) { |
419 | printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", | 422 | printf("parsing %s from peer %.2x: ", |
423 | strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", | ||
420 | ntohs(peers[i].assoc)); | 424 | ntohs(peers[i].assoc)); |
421 | } | 425 | } |
422 | value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); | 426 | value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" |
427 | : "jitter"); | ||
423 | nptr = NULL; | 428 | nptr = NULL; |
424 | /* Convert the value if we have one */ | 429 | /* Convert the value if we have one */ |
425 | if (value != NULL) { | 430 | if (value != NULL) { |
@@ -471,12 +476,15 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) { | |||
471 | 476 | ||
472 | check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) { | 477 | check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) { |
473 | static struct option longopts[] = { | 478 | static struct option longopts[] = { |
474 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, | 479 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, |
475 | {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, | 480 | {"verbose", no_argument, 0, 'v'}, {"use-ipv4", no_argument, 0, '4'}, |
476 | {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"swarn", required_argument, 0, 'W'}, | 481 | {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, |
477 | {"scrit", required_argument, 0, 'C'}, {"jwarn", required_argument, 0, 'j'}, {"jcrit", required_argument, 0, 'k'}, | 482 | {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, |
478 | {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'}, {"timeout", required_argument, 0, 't'}, | 483 | {"swarn", required_argument, 0, 'W'}, {"scrit", required_argument, 0, 'C'}, |
479 | {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}}; | 484 | {"jwarn", required_argument, 0, 'j'}, {"jcrit", required_argument, 0, 'k'}, |
485 | {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'}, | ||
486 | {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'}, | ||
487 | {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}}; | ||
480 | 488 | ||
481 | if (argc < 2) { | 489 | if (argc < 2) { |
482 | usage("\n"); | 490 | usage("\n"); |
@@ -489,7 +497,8 @@ check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) { | |||
489 | 497 | ||
490 | while (true) { | 498 | while (true) { |
491 | int option = 0; | 499 | int option = 0; |
492 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); | 500 | int option_char = |
501 | getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); | ||
493 | if (option_char == -1 || option_char == EOF || option_char == 1) { | 502 | if (option_char == -1 || option_char == EOF || option_char == 1) { |
494 | break; | 503 | break; |
495 | } | 504 | } |
@@ -581,22 +590,24 @@ check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) { | |||
581 | } | 590 | } |
582 | 591 | ||
583 | char *perfd_offset(double offset, thresholds *offset_thresholds) { | 592 | char *perfd_offset(double offset, thresholds *offset_thresholds) { |
584 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, | 593 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, |
585 | 0); | 594 | offset_thresholds->critical->end, false, 0, false, 0); |
586 | } | 595 | } |
587 | 596 | ||
588 | char *perfd_jitter(double jitter, bool do_jitter, thresholds *jitter_thresholds) { | 597 | char *perfd_jitter(double jitter, bool do_jitter, thresholds *jitter_thresholds) { |
589 | return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter, jitter_thresholds->critical->end, true, 0, | 598 | return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter, |
590 | false, 0); | 599 | jitter_thresholds->critical->end, true, 0, false, 0); |
591 | } | 600 | } |
592 | 601 | ||
593 | char *perfd_stratum(int stratum, bool do_stratum, thresholds *stratum_thresholds) { | 602 | char *perfd_stratum(int stratum, bool do_stratum, thresholds *stratum_thresholds) { |
594 | return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end, do_stratum, | 603 | return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end, |
595 | (int)stratum_thresholds->critical->end, true, 0, true, 16); | 604 | do_stratum, (int)stratum_thresholds->critical->end, true, 0, true, 16); |
596 | } | 605 | } |
597 | 606 | ||
598 | char *perfd_truechimers(int num_truechimers, const bool do_truechimers, thresholds *truechimer_thresholds) { | 607 | char *perfd_truechimers(int num_truechimers, const bool do_truechimers, |
599 | return perfdata("truechimers", num_truechimers, "", do_truechimers, (int)truechimer_thresholds->warning->end, do_truechimers, | 608 | thresholds *truechimer_thresholds) { |
609 | return perfdata("truechimers", num_truechimers, "", do_truechimers, | ||
610 | (int)truechimer_thresholds->warning->end, do_truechimers, | ||
600 | (int)truechimer_thresholds->critical->end, true, 0, false, 0); | 611 | (int)truechimer_thresholds->critical->end, true, 0, false, 0); |
601 | } | 612 | } |
602 | 613 | ||
@@ -686,9 +697,11 @@ int main(int argc, char *argv[]) { | |||
686 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | 697 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); |
687 | xasprintf(&perfdata_line, ""); | 698 | xasprintf(&perfdata_line, ""); |
688 | } else if (oresult == STATE_WARNING) { | 699 | } else if (oresult == STATE_WARNING) { |
689 | xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), ntp_res.offset); | 700 | xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), |
701 | ntp_res.offset); | ||
690 | } else if (oresult == STATE_CRITICAL) { | 702 | } else if (oresult == STATE_CRITICAL) { |
691 | xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), ntp_res.offset); | 703 | xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), |
704 | ntp_res.offset); | ||
692 | } else { | 705 | } else { |
693 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), ntp_res.offset); | 706 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), ntp_res.offset); |
694 | } | 707 | } |
@@ -702,30 +715,35 @@ int main(int argc, char *argv[]) { | |||
702 | } else { | 715 | } else { |
703 | xasprintf(&result_line, "%s, jitter=%f", result_line, ntp_res.jitter); | 716 | xasprintf(&result_line, "%s, jitter=%f", result_line, ntp_res.jitter); |
704 | } | 717 | } |
705 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(ntp_res.jitter, config.do_jitter, config.jitter_thresholds)); | 718 | xasprintf(&perfdata_line, "%s %s", perfdata_line, |
719 | perfd_jitter(ntp_res.jitter, config.do_jitter, config.jitter_thresholds)); | ||
706 | } | 720 | } |
707 | 721 | ||
708 | if (config.do_stratum) { | 722 | if (config.do_stratum) { |
709 | if (sresult == STATE_WARNING) { | 723 | if (sresult == STATE_WARNING) { |
710 | xasprintf(&result_line, "%s, stratum=%l (WARNING)", result_line, ntp_res.stratum); | 724 | xasprintf(&result_line, "%s, stratum=%li (WARNING)", result_line, ntp_res.stratum); |
711 | } else if (sresult == STATE_CRITICAL) { | 725 | } else if (sresult == STATE_CRITICAL) { |
712 | xasprintf(&result_line, "%s, stratum=%l (CRITICAL)", result_line, ntp_res.stratum); | 726 | xasprintf(&result_line, "%s, stratum=%li (CRITICAL)", result_line, ntp_res.stratum); |
713 | } else { | 727 | } else { |
714 | xasprintf(&result_line, "%s, stratum=%l", result_line, ntp_res.stratum); | 728 | xasprintf(&result_line, "%s, stratum=%li", result_line, ntp_res.stratum); |
715 | } | 729 | } |
716 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(ntp_res.stratum, config.do_stratum, config.stratum_thresholds)); | 730 | xasprintf(&perfdata_line, "%s %s", perfdata_line, |
731 | perfd_stratum(ntp_res.stratum, config.do_stratum, config.stratum_thresholds)); | ||
717 | } | 732 | } |
718 | 733 | ||
719 | if (config.do_truechimers) { | 734 | if (config.do_truechimers) { |
720 | if (tresult == STATE_WARNING) { | 735 | if (tresult == STATE_WARNING) { |
721 | xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, ntp_res.num_truechimers); | 736 | xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, |
737 | ntp_res.num_truechimers); | ||
722 | } else if (tresult == STATE_CRITICAL) { | 738 | } else if (tresult == STATE_CRITICAL) { |
723 | xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, ntp_res.num_truechimers); | 739 | xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, |
740 | ntp_res.num_truechimers); | ||
724 | } else { | 741 | } else { |
725 | xasprintf(&result_line, "%s, truechimers=%i", result_line, ntp_res.num_truechimers); | 742 | xasprintf(&result_line, "%s, truechimers=%i", result_line, ntp_res.num_truechimers); |
726 | } | 743 | } |
727 | xasprintf(&perfdata_line, "%s %s", perfdata_line, | 744 | xasprintf(&perfdata_line, "%s %s", perfdata_line, |
728 | perfd_truechimers(ntp_res.num_truechimers, config.do_truechimers, config.truechimer_thresholds)); | 745 | perfd_truechimers(ntp_res.num_truechimers, config.do_truechimers, |
746 | config.truechimer_thresholds)); | ||
729 | } | 747 | } |
730 | 748 | ||
731 | printf("%s|%s\n", result_line, perfdata_line); | 749 | printf("%s|%s\n", result_line, perfdata_line); |
@@ -753,7 +771,8 @@ void print_help(void) { | |||
753 | printf(UT_IPv46); | 771 | printf(UT_IPv46); |
754 | printf(UT_HOST_PORT, 'p', "123"); | 772 | printf(UT_HOST_PORT, 'p', "123"); |
755 | printf(" %s\n", "-q, --quiet"); | 773 | printf(" %s\n", "-q, --quiet"); |
756 | printf(" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized")); | 774 | printf(" %s\n", |
775 | _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized")); | ||
757 | printf(" %s\n", "-w, --warning=THRESHOLD"); | 776 | printf(" %s\n", "-w, --warning=THRESHOLD"); |
758 | printf(" %s\n", _("Offset to result in warning status (seconds)")); | 777 | printf(" %s\n", _("Offset to result in warning status (seconds)")); |
759 | printf(" %s\n", "-c, --critical=THRESHOLD"); | 778 | printf(" %s\n", "-c, --critical=THRESHOLD"); |
@@ -790,7 +809,8 @@ void print_help(void) { | |||
790 | printf(" %s\n", _("Simple NTP server check:")); | 809 | printf(" %s\n", _("Simple NTP server check:")); |
791 | printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1")); | 810 | printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1")); |
792 | printf("\n"); | 811 | printf("\n"); |
793 | printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available")); | 812 | printf(" %s\n", |
813 | _("Check jitter too, avoiding critical notifications if jitter isn't available")); | ||
794 | printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); | 814 | printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); |
795 | printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); | 815 | printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); |
796 | printf("\n"); | 816 | printf("\n"); |
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c index 31162883..ad69b804 100644 --- a/plugins/check_ntp_time.c +++ b/plugins/check_ntp_time.c | |||
@@ -93,9 +93,9 @@ typedef struct { | |||
93 | /* bits 1,2 are the leap indicator */ | 93 | /* bits 1,2 are the leap indicator */ |
94 | #define LI_MASK 0xc0 | 94 | #define LI_MASK 0xc0 |
95 | #define LI(x) ((x & LI_MASK) >> 6) | 95 | #define LI(x) ((x & LI_MASK) >> 6) |
96 | #define LI_SET(x, y) \ | 96 | #define LI_SET(x, y) \ |
97 | do { \ | 97 | do { \ |
98 | x |= ((y << 6) & LI_MASK); \ | 98 | x |= ((y << 6) & LI_MASK); \ |
99 | } while (0) | 99 | } while (0) |
100 | /* and these are the values of the leap indicator */ | 100 | /* and these are the values of the leap indicator */ |
101 | #define LI_NOWARNING 0x00 | 101 | #define LI_NOWARNING 0x00 |
@@ -105,17 +105,17 @@ typedef struct { | |||
105 | /* bits 3,4,5 are the ntp version */ | 105 | /* bits 3,4,5 are the ntp version */ |
106 | #define VN_MASK 0x38 | 106 | #define VN_MASK 0x38 |
107 | #define VN(x) ((x & VN_MASK) >> 3) | 107 | #define VN(x) ((x & VN_MASK) >> 3) |
108 | #define VN_SET(x, y) \ | 108 | #define VN_SET(x, y) \ |
109 | do { \ | 109 | do { \ |
110 | x |= ((y << 3) & VN_MASK); \ | 110 | x |= ((y << 3) & VN_MASK); \ |
111 | } while (0) | 111 | } while (0) |
112 | #define VN_RESERVED 0x02 | 112 | #define VN_RESERVED 0x02 |
113 | /* bits 6,7,8 are the ntp mode */ | 113 | /* bits 6,7,8 are the ntp mode */ |
114 | #define MODE_MASK 0x07 | 114 | #define MODE_MASK 0x07 |
115 | #define MODE(x) (x & MODE_MASK) | 115 | #define MODE(x) (x & MODE_MASK) |
116 | #define MODE_SET(x, y) \ | 116 | #define MODE_SET(x, y) \ |
117 | do { \ | 117 | do { \ |
118 | x |= (y & MODE_MASK); \ | 118 | x |= (y & MODE_MASK); \ |
119 | } while (0) | 119 | } while (0) |
120 | /* here are some values */ | 120 | /* here are some values */ |
121 | #define MODE_CLIENT 0x03 | 121 | #define MODE_CLIENT 0x03 |
@@ -127,9 +127,9 @@ typedef struct { | |||
127 | #define REM_MORE 0x20 | 127 | #define REM_MORE 0x20 |
128 | /* In control message, bits 11 - 15 are opcode */ | 128 | /* In control message, bits 11 - 15 are opcode */ |
129 | #define OP_MASK 0x1f | 129 | #define OP_MASK 0x1f |
130 | #define OP_SET(x, y) \ | 130 | #define OP_SET(x, y) \ |
131 | do { \ | 131 | do { \ |
132 | x |= (y & OP_MASK); \ | 132 | x |= (y & OP_MASK); \ |
133 | } while (0) | 133 | } while (0) |
134 | #define OP_READSTAT 0x01 | 134 | #define OP_READSTAT 0x01 |
135 | #define OP_READVAR 0x02 | 135 | #define OP_READVAR 0x02 |
@@ -163,32 +163,34 @@ typedef struct { | |||
163 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0)) | 163 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0)) |
164 | 164 | ||
165 | /* likewise for a 64-bit ntp fp number */ | 165 | /* likewise for a 64-bit ntp fp number */ |
166 | #define NTP64asDOUBLE(n) \ | 166 | #define NTP64asDOUBLE(n) \ |
167 | (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) : 0) | 167 | (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + \ |
168 | (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) \ | ||
169 | : 0) | ||
168 | 170 | ||
169 | /* convert a struct timeval to a double */ | 171 | /* convert a struct timeval to a double */ |
170 | #define TVasDOUBLE(x) (double)(x.tv_sec + (0.000001 * x.tv_usec)) | 172 | #define TVasDOUBLE(x) (double)(x.tv_sec + (0.000001 * x.tv_usec)) |
171 | 173 | ||
172 | /* convert an ntp 64-bit fp number to a struct timeval */ | 174 | /* convert an ntp 64-bit fp number to a struct timeval */ |
173 | #define NTP64toTV(n, t) \ | 175 | #define NTP64toTV(n, t) \ |
174 | do { \ | 176 | do { \ |
175 | if (!n) \ | 177 | if (!n) \ |
176 | t.tv_sec = t.tv_usec = 0; \ | 178 | t.tv_sec = t.tv_usec = 0; \ |
177 | else { \ | 179 | else { \ |
178 | t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \ | 180 | t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \ |
179 | t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \ | 181 | t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \ |
180 | } \ | 182 | } \ |
181 | } while (0) | 183 | } while (0) |
182 | 184 | ||
183 | /* convert a struct timeval to an ntp 64-bit fp number */ | 185 | /* convert a struct timeval to an ntp 64-bit fp number */ |
184 | #define TVtoNTP64(t, n) \ | 186 | #define TVtoNTP64(t, n) \ |
185 | do { \ | 187 | do { \ |
186 | if (!t.tv_usec && !t.tv_sec) \ | 188 | if (!t.tv_usec && !t.tv_sec) \ |
187 | n = 0x0UL; \ | 189 | n = 0x0UL; \ |
188 | else { \ | 190 | else { \ |
189 | L32(n) = htonl(t.tv_sec + EPOCHDIFF); \ | 191 | L32(n) = htonl(t.tv_sec + EPOCHDIFF); \ |
190 | R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \ | 192 | R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \ |
191 | } \ | 193 | } \ |
192 | } while (0) | 194 | } while (0) |
193 | 195 | ||
194 | /* NTP control message header is 12 bytes, plus any data in the data | 196 | /* NTP control message header is 12 bytes, plus any data in the data |
@@ -197,15 +199,15 @@ typedef struct { | |||
197 | #define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((m.count) ? 4 - (ntohs(m.count) % 4) : 0)) | 199 | #define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((m.count) ? 4 - (ntohs(m.count) % 4) : 0)) |
198 | 200 | ||
199 | /* finally, a little helper or two for debugging: */ | 201 | /* finally, a little helper or two for debugging: */ |
200 | #define DBG(x) \ | 202 | #define DBG(x) \ |
201 | do { \ | 203 | do { \ |
202 | if (verbose > 1) { \ | 204 | if (verbose > 1) { \ |
203 | x; \ | 205 | x; \ |
204 | } \ | 206 | } \ |
205 | } while (0); | 207 | } while (0); |
206 | #define PRINTSOCKADDR(x) \ | 208 | #define PRINTSOCKADDR(x) \ |
207 | do { \ | 209 | do { \ |
208 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ | 210 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ |
209 | } while (0); | 211 | } while (0); |
210 | 212 | ||
211 | /* calculate the offset of the local clock */ | 213 | /* calculate the offset of the local clock */ |
@@ -358,7 +360,8 @@ double offset_request(const char *host, const char *port, mp_state_enum *status, | |||
358 | die(STATE_UNKNOWN, "can not allocate socket array"); | 360 | die(STATE_UNKNOWN, "can not allocate socket array"); |
359 | } | 361 | } |
360 | 362 | ||
361 | ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); | 363 | ntp_server_results *servers = |
364 | (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); | ||
362 | if (servers == NULL) { | 365 | if (servers == NULL) { |
363 | die(STATE_UNKNOWN, "can not allocate server array"); | 366 | die(STATE_UNKNOWN, "can not allocate server array"); |
364 | } | 367 | } |
@@ -585,8 +588,8 @@ check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { | |||
585 | } | 588 | } |
586 | 589 | ||
587 | char *perfd_offset(double offset, thresholds *offset_thresholds) { | 590 | char *perfd_offset(double offset, thresholds *offset_thresholds) { |
588 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, | 591 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, |
589 | 0); | 592 | offset_thresholds->critical->end, false, 0, false, 0); |
590 | } | 593 | } |
591 | 594 | ||
592 | int main(int argc, char *argv[]) { | 595 | int main(int argc, char *argv[]) { |
@@ -613,7 +616,8 @@ int main(int argc, char *argv[]) { | |||
613 | 616 | ||
614 | mp_state_enum offset_result = STATE_OK; | 617 | mp_state_enum offset_result = STATE_OK; |
615 | mp_state_enum result = STATE_OK; | 618 | mp_state_enum result = STATE_OK; |
616 | double offset = offset_request(config.server_address, config.port, &offset_result, config.time_offset); | 619 | double offset = |
620 | offset_request(config.server_address, config.port, &offset_result, config.time_offset); | ||
617 | if (offset_result == STATE_UNKNOWN) { | 621 | if (offset_result == STATE_UNKNOWN) { |
618 | result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL); | 622 | result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL); |
619 | } else { | 623 | } else { |
@@ -701,5 +705,6 @@ void print_help(void) { | |||
701 | 705 | ||
702 | void print_usage(void) { | 706 | void print_usage(void) { |
703 | printf("%s\n", _("Usage:")); | 707 | printf("%s\n", _("Usage:")); |
704 | printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-v verbose] [-o <time offset>]\n", progname); | 708 | printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-v verbose] [-o <time offset>]\n", |
709 | progname); | ||
705 | } | 710 | } |
diff --git a/plugins/check_nwstat.c b/plugins/check_nwstat.c deleted file mode 100644 index 176dfbc8..00000000 --- a/plugins/check_nwstat.c +++ /dev/null | |||
@@ -1,1527 +0,0 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Monitoring check_nwstat plugin | ||
4 | * | ||
5 | * License: GPL | ||
6 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team | ||
7 | * | ||
8 | * Description: | ||
9 | * | ||
10 | * This file contains the check_nwstat plugin | ||
11 | * | ||
12 | * This plugin attempts to contact the MRTGEXT NLM running on a | ||
13 | * Novell server to gather the requested system information. | ||
14 | * | ||
15 | * | ||
16 | * This program is free software: you can redistribute it and/or modify | ||
17 | * it under the terms of the GNU General Public License as published by | ||
18 | * the Free Software Foundation, either version 3 of the License, or | ||
19 | * (at your option) any later version. | ||
20 | * | ||
21 | * This program is distributed in the hope that it will be useful, | ||
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
24 | * GNU General Public License for more details. | ||
25 | * | ||
26 | * You should have received a copy of the GNU General Public License | ||
27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
28 | * | ||
29 | * | ||
30 | *****************************************************************************/ | ||
31 | |||
32 | const char *progname = "check_nwstat"; | ||
33 | const char *copyright = "2000-2024"; | ||
34 | const char *email = "devel@monitoring-plugins.org"; | ||
35 | |||
36 | #include "common.h" | ||
37 | #include "netutils.h" | ||
38 | #include "utils.h" | ||
39 | |||
40 | enum checkvar { | ||
41 | NONE, | ||
42 | LOAD1, /* check 1 minute CPU load */ | ||
43 | LOAD5, /* check 5 minute CPU load */ | ||
44 | LOAD15, /* check 15 minute CPU load */ | ||
45 | CONNS, /* check number of connections */ | ||
46 | VPF, /* check % free space on volume */ | ||
47 | VMF, /* check MB free space on volume */ | ||
48 | VMU, /* check MB used space on volume */ | ||
49 | VPU, /* check % used space on volume */ | ||
50 | VMP, /* check MB purgeable space on volume */ | ||
51 | VKF, /* check KB free space on volume */ | ||
52 | LTCH, /* check long-term cache hit percentage */ | ||
53 | CBUFF, /* check total cache buffers */ | ||
54 | CDBUFF, /* check dirty cache buffers */ | ||
55 | LRUM, /* check LRU sitting time in minutes */ | ||
56 | DSDB, /* check to see if DS Database is open */ | ||
57 | LOGINS, /* check to see if logins are enabled */ | ||
58 | NRMH, /* check to see NRM Health Status */ | ||
59 | PUPRB, /* check % of used packet receive buffers */ | ||
60 | UPRB, /* check used packet receive buffers */ | ||
61 | SAPENTRIES, /* check SAP entries */ | ||
62 | OFILES, /* check number of open files */ | ||
63 | VKP, /* check KB purgeable space on volume */ | ||
64 | VPP, /* check % purgeable space on volume */ | ||
65 | VKNP, /* check KB not yet purgeable space on volume */ | ||
66 | VPNP, /* check % not yet purgeable space on volume */ | ||
67 | ABENDS, /* check abended thread count */ | ||
68 | CSPROCS, /* check number of current service processes */ | ||
69 | TSYNC, /* check timesync status 0=no 1=yes in sync to the network */ | ||
70 | LRUS, /* check LRU sitting time in seconds */ | ||
71 | DCB, /* check dirty cache buffers as a percentage of the total */ | ||
72 | TCB, /* check total cache buffers as a percentage of the original */ | ||
73 | DSVER, /* check NDS version */ | ||
74 | UPTIME, /* check server uptime */ | ||
75 | NLM, /* check NLM loaded */ | ||
76 | NRMP, /* check NRM Process Values */ | ||
77 | NRMM, /* check NRM Memory Values */ | ||
78 | NRMS, /* check NRM Values */ | ||
79 | NSS1, /* check Statistics from _Admin:Manage_NSS\GeneralStats.xml */ | ||
80 | NSS2, /* check Statistics from _Admin:Manage_NSS\BufferCache.xml */ | ||
81 | NSS3, /* check statistics from _Admin:Manage_NSS\NameCache.xml */ | ||
82 | NSS4, /* check statistics from _Admin:Manage_NSS\FileStats.xml */ | ||
83 | NSS5, /* check statistics from _Admin:Manage_NSS\ObjectCache.xml */ | ||
84 | NSS6, /* check statistics from _Admin:Manage_NSS\Thread.xml */ | ||
85 | NSS7 /* check statistics from _Admin:Manage_NSS\AuthorizationCache.xml */ | ||
86 | }; | ||
87 | |||
88 | enum { | ||
89 | PORT = 9999 | ||
90 | }; | ||
91 | |||
92 | static char *server_address = NULL; | ||
93 | static char *volume_name = NULL; | ||
94 | static char *nlm_name = NULL; | ||
95 | static char *nrmp_name = NULL; | ||
96 | static char *nrmm_name = NULL; | ||
97 | static char *nrms_name = NULL; | ||
98 | static char *nss1_name = NULL; | ||
99 | static char *nss2_name = NULL; | ||
100 | static char *nss3_name = NULL; | ||
101 | static char *nss4_name = NULL; | ||
102 | static char *nss5_name = NULL; | ||
103 | static char *nss6_name = NULL; | ||
104 | static char *nss7_name = NULL; | ||
105 | static int server_port = PORT; | ||
106 | static unsigned long warning_value = 0L; | ||
107 | static unsigned long critical_value = 0L; | ||
108 | static bool check_warning_value = false; | ||
109 | static bool check_critical_value = false; | ||
110 | static bool check_netware_version = false; | ||
111 | static enum checkvar vars_to_check = NONE; | ||
112 | static int sap_number = -1; | ||
113 | |||
114 | static int process_arguments(int /*argc*/, char ** /*argv*/); | ||
115 | static void print_help(void); | ||
116 | void print_usage(void); | ||
117 | |||
118 | int main(int argc, char **argv) { | ||
119 | int result = STATE_UNKNOWN; | ||
120 | int sd; | ||
121 | char *send_buffer = NULL; | ||
122 | char recv_buffer[MAX_INPUT_BUFFER]; | ||
123 | char *output_message = NULL; | ||
124 | char *temp_buffer = NULL; | ||
125 | char *netware_version = NULL; | ||
126 | |||
127 | int time_sync_status = 0; | ||
128 | int nrm_health_status = 0; | ||
129 | unsigned long total_cache_buffers = 0; | ||
130 | unsigned long dirty_cache_buffers = 0; | ||
131 | unsigned long open_files = 0; | ||
132 | unsigned long abended_threads = 0; | ||
133 | unsigned long max_service_processes = 0; | ||
134 | unsigned long current_service_processes = 0; | ||
135 | unsigned long free_disk_space = 0L; | ||
136 | unsigned long nrmp_value = 0L; | ||
137 | unsigned long nrmm_value = 0L; | ||
138 | unsigned long nrms_value = 0L; | ||
139 | unsigned long nss1_value = 0L; | ||
140 | unsigned long nss2_value = 0L; | ||
141 | unsigned long nss3_value = 0L; | ||
142 | unsigned long nss4_value = 0L; | ||
143 | unsigned long nss5_value = 0L; | ||
144 | unsigned long nss6_value = 0L; | ||
145 | unsigned long nss7_value = 0L; | ||
146 | unsigned long total_disk_space = 0L; | ||
147 | unsigned long used_disk_space = 0L; | ||
148 | unsigned long percent_used_disk_space = 0L; | ||
149 | unsigned long purgeable_disk_space = 0L; | ||
150 | unsigned long non_purgeable_disk_space = 0L; | ||
151 | unsigned long percent_free_space = 0; | ||
152 | unsigned long percent_purgeable_space = 0; | ||
153 | unsigned long percent_non_purgeable_space = 0; | ||
154 | unsigned long current_connections = 0L; | ||
155 | unsigned long utilization = 0L; | ||
156 | unsigned long cache_hits = 0; | ||
157 | unsigned long cache_buffers = 0L; | ||
158 | unsigned long lru_time = 0L; | ||
159 | unsigned long max_packet_receive_buffers = 0; | ||
160 | unsigned long used_packet_receive_buffers = 0; | ||
161 | unsigned long percent_used_packet_receive_buffers = 0L; | ||
162 | unsigned long sap_entries = 0; | ||
163 | char uptime[MAX_INPUT_BUFFER]; | ||
164 | |||
165 | setlocale(LC_ALL, ""); | ||
166 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
167 | textdomain(PACKAGE); | ||
168 | |||
169 | /* Parse extra opts if any */ | ||
170 | argv = np_extra_opts(&argc, argv, progname); | ||
171 | |||
172 | if (process_arguments(argc, argv) == ERROR) | ||
173 | usage4(_("Could not parse arguments")); | ||
174 | |||
175 | /* initialize alarm signal handling */ | ||
176 | signal(SIGALRM, socket_timeout_alarm_handler); | ||
177 | |||
178 | /* set socket timeout */ | ||
179 | alarm(socket_timeout); | ||
180 | |||
181 | /* open connection */ | ||
182 | my_tcp_connect(server_address, server_port, &sd); | ||
183 | |||
184 | /* get OS version string */ | ||
185 | if (check_netware_version) { | ||
186 | send_buffer = strdup("S19\r\n"); | ||
187 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
188 | if (result != STATE_OK) | ||
189 | return result; | ||
190 | if (!strcmp(recv_buffer, "-1\n")) | ||
191 | netware_version = strdup(""); | ||
192 | else { | ||
193 | recv_buffer[strlen(recv_buffer) - 1] = 0; | ||
194 | xasprintf(&netware_version, _("NetWare %s: "), recv_buffer); | ||
195 | } | ||
196 | } else | ||
197 | netware_version = strdup(""); | ||
198 | |||
199 | /* check CPU load */ | ||
200 | if (vars_to_check == LOAD1 || vars_to_check == LOAD5 || vars_to_check == LOAD15) { | ||
201 | |||
202 | switch (vars_to_check) { | ||
203 | case LOAD1: | ||
204 | temp_buffer = strdup("1"); | ||
205 | break; | ||
206 | case LOAD5: | ||
207 | temp_buffer = strdup("5"); | ||
208 | break; | ||
209 | default: | ||
210 | temp_buffer = strdup("15"); | ||
211 | break; | ||
212 | } | ||
213 | |||
214 | close(sd); | ||
215 | my_tcp_connect(server_address, server_port, &sd); | ||
216 | |||
217 | xasprintf(&send_buffer, "UTIL%s\r\n", temp_buffer); | ||
218 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
219 | if (result != STATE_OK) | ||
220 | return result; | ||
221 | utilization = strtoul(recv_buffer, NULL, 10); | ||
222 | |||
223 | close(sd); | ||
224 | my_tcp_connect(server_address, server_port, &sd); | ||
225 | |||
226 | send_buffer = strdup("UPTIME\r\n"); | ||
227 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
228 | if (result != STATE_OK) | ||
229 | return result; | ||
230 | recv_buffer[strlen(recv_buffer) - 1] = 0; | ||
231 | sprintf(uptime, _("Up %s,"), recv_buffer); | ||
232 | |||
233 | if (check_critical_value && utilization >= critical_value) | ||
234 | result = STATE_CRITICAL; | ||
235 | else if (check_warning_value && utilization >= warning_value) | ||
236 | result = STATE_WARNING; | ||
237 | |||
238 | xasprintf(&output_message, _("Load %s - %s %s-min load average = %lu%%|load%s=%lu;%lu;%lu;0;100"), state_text(result), uptime, | ||
239 | temp_buffer, utilization, temp_buffer, utilization, warning_value, critical_value); | ||
240 | |||
241 | /* check number of user connections */ | ||
242 | } else if (vars_to_check == CONNS) { | ||
243 | |||
244 | close(sd); | ||
245 | my_tcp_connect(server_address, server_port, &sd); | ||
246 | |||
247 | send_buffer = strdup("CONNECT\r\n"); | ||
248 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
249 | if (result != STATE_OK) | ||
250 | return result; | ||
251 | current_connections = strtoul(recv_buffer, NULL, 10); | ||
252 | |||
253 | if (check_critical_value && current_connections >= critical_value) | ||
254 | result = STATE_CRITICAL; | ||
255 | else if (check_warning_value && current_connections >= warning_value) | ||
256 | result = STATE_WARNING; | ||
257 | |||
258 | xasprintf(&output_message, _("Conns %s - %lu current connections|Conns=%lu;%lu;%lu;;"), state_text(result), current_connections, | ||
259 | current_connections, warning_value, critical_value); | ||
260 | |||
261 | /* check % long term cache hits */ | ||
262 | } else if (vars_to_check == LTCH) { | ||
263 | |||
264 | close(sd); | ||
265 | my_tcp_connect(server_address, server_port, &sd); | ||
266 | |||
267 | send_buffer = strdup("S1\r\n"); | ||
268 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
269 | if (result != STATE_OK) | ||
270 | return result; | ||
271 | cache_hits = atoi(recv_buffer); | ||
272 | |||
273 | if (check_critical_value && cache_hits <= critical_value) | ||
274 | result = STATE_CRITICAL; | ||
275 | else if (check_warning_value && cache_hits <= warning_value) | ||
276 | result = STATE_WARNING; | ||
277 | |||
278 | xasprintf(&output_message, _("%s: Long term cache hits = %lu%%"), state_text(result), cache_hits); | ||
279 | |||
280 | /* check cache buffers */ | ||
281 | } else if (vars_to_check == CBUFF) { | ||
282 | |||
283 | close(sd); | ||
284 | my_tcp_connect(server_address, server_port, &sd); | ||
285 | |||
286 | send_buffer = strdup("S2\r\n"); | ||
287 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
288 | if (result != STATE_OK) | ||
289 | return result; | ||
290 | cache_buffers = strtoul(recv_buffer, NULL, 10); | ||
291 | |||
292 | if (check_critical_value && cache_buffers <= critical_value) | ||
293 | result = STATE_CRITICAL; | ||
294 | else if (check_warning_value && cache_buffers <= warning_value) | ||
295 | result = STATE_WARNING; | ||
296 | |||
297 | xasprintf(&output_message, _("%s: Total cache buffers = %lu|Cachebuffers=%lu;%lu;%lu;;"), state_text(result), cache_buffers, | ||
298 | cache_buffers, warning_value, critical_value); | ||
299 | |||
300 | /* check dirty cache buffers */ | ||
301 | } else if (vars_to_check == CDBUFF) { | ||
302 | |||
303 | close(sd); | ||
304 | my_tcp_connect(server_address, server_port, &sd); | ||
305 | |||
306 | send_buffer = strdup("S3\r\n"); | ||
307 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
308 | if (result != STATE_OK) | ||
309 | return result; | ||
310 | cache_buffers = strtoul(recv_buffer, NULL, 10); | ||
311 | |||
312 | if (check_critical_value && cache_buffers >= critical_value) | ||
313 | result = STATE_CRITICAL; | ||
314 | else if (check_warning_value && cache_buffers >= warning_value) | ||
315 | result = STATE_WARNING; | ||
316 | |||
317 | xasprintf(&output_message, _("%s: Dirty cache buffers = %lu|Dirty-Cache-Buffers=%lu;%lu;%lu;;"), state_text(result), cache_buffers, | ||
318 | cache_buffers, warning_value, critical_value); | ||
319 | |||
320 | /* check LRU sitting time in minutes */ | ||
321 | } else if (vars_to_check == LRUM) { | ||
322 | |||
323 | close(sd); | ||
324 | my_tcp_connect(server_address, server_port, &sd); | ||
325 | |||
326 | send_buffer = strdup("S5\r\n"); | ||
327 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
328 | if (result != STATE_OK) | ||
329 | return result; | ||
330 | lru_time = strtoul(recv_buffer, NULL, 10); | ||
331 | |||
332 | if (check_critical_value && lru_time <= critical_value) | ||
333 | result = STATE_CRITICAL; | ||
334 | else if (check_warning_value && lru_time <= warning_value) | ||
335 | result = STATE_WARNING; | ||
336 | |||
337 | xasprintf(&output_message, _("%s: LRU sitting time = %lu minutes"), state_text(result), lru_time); | ||
338 | |||
339 | /* check KB free space on volume */ | ||
340 | } else if (vars_to_check == VKF) { | ||
341 | |||
342 | close(sd); | ||
343 | my_tcp_connect(server_address, server_port, &sd); | ||
344 | |||
345 | xasprintf(&send_buffer, "VKF%s\r\n", volume_name); | ||
346 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
347 | if (result != STATE_OK) | ||
348 | return result; | ||
349 | |||
350 | if (!strcmp(recv_buffer, "-1\n")) { | ||
351 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
352 | result = STATE_CRITICAL; | ||
353 | } else { | ||
354 | free_disk_space = strtoul(recv_buffer, NULL, 10); | ||
355 | if (check_critical_value && free_disk_space <= critical_value) | ||
356 | result = STATE_CRITICAL; | ||
357 | else if (check_warning_value && free_disk_space <= warning_value) | ||
358 | result = STATE_WARNING; | ||
359 | xasprintf(&output_message, _("%s%lu KB free on volume %s|KBFree%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), | ||
360 | free_disk_space, volume_name, volume_name, free_disk_space, warning_value, critical_value); | ||
361 | } | ||
362 | |||
363 | /* check MB free space on volume */ | ||
364 | } else if (vars_to_check == VMF) { | ||
365 | |||
366 | xasprintf(&send_buffer, "VMF%s\r\n", volume_name); | ||
367 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
368 | if (result != STATE_OK) | ||
369 | return result; | ||
370 | |||
371 | if (!strcmp(recv_buffer, "-1\n")) { | ||
372 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
373 | result = STATE_CRITICAL; | ||
374 | } else { | ||
375 | free_disk_space = strtoul(recv_buffer, NULL, 10); | ||
376 | if (check_critical_value && free_disk_space <= critical_value) | ||
377 | result = STATE_CRITICAL; | ||
378 | else if (check_warning_value && free_disk_space <= warning_value) | ||
379 | result = STATE_WARNING; | ||
380 | xasprintf(&output_message, _("%s%lu MB free on volume %s|MBFree%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), | ||
381 | free_disk_space, volume_name, volume_name, free_disk_space, warning_value, critical_value); | ||
382 | } | ||
383 | /* check MB used space on volume */ | ||
384 | } else if (vars_to_check == VMU) { | ||
385 | |||
386 | xasprintf(&send_buffer, "VMU%s\r\n", volume_name); | ||
387 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
388 | if (result != STATE_OK) | ||
389 | return result; | ||
390 | |||
391 | if (!strcmp(recv_buffer, "-1\n")) { | ||
392 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
393 | result = STATE_CRITICAL; | ||
394 | } else { | ||
395 | free_disk_space = strtoul(recv_buffer, NULL, 10); | ||
396 | if (check_critical_value && free_disk_space <= critical_value) | ||
397 | result = STATE_CRITICAL; | ||
398 | else if (check_warning_value && free_disk_space <= warning_value) | ||
399 | result = STATE_WARNING; | ||
400 | xasprintf(&output_message, _("%s%lu MB used on volume %s|MBUsed%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), | ||
401 | free_disk_space, volume_name, volume_name, free_disk_space, warning_value, critical_value); | ||
402 | } | ||
403 | /* check % used space on volume */ | ||
404 | } else if (vars_to_check == VPU) { | ||
405 | close(sd); | ||
406 | my_tcp_connect(server_address, server_port, &sd); | ||
407 | |||
408 | asprintf(&send_buffer, "VMU%s\r\n", volume_name); | ||
409 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
410 | |||
411 | if (result != STATE_OK) | ||
412 | return result; | ||
413 | |||
414 | if (!strcmp(recv_buffer, "-1\n")) { | ||
415 | asprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
416 | result = STATE_CRITICAL; | ||
417 | |||
418 | } else { | ||
419 | used_disk_space = strtoul(recv_buffer, NULL, 10); | ||
420 | close(sd); | ||
421 | my_tcp_connect(server_address, server_port, &sd); | ||
422 | /* get total volume in MB */ | ||
423 | asprintf(&send_buffer, "VMS%s\r\n", volume_name); | ||
424 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
425 | if (result != STATE_OK) | ||
426 | return result; | ||
427 | total_disk_space = strtoul(recv_buffer, NULL, 10); | ||
428 | /* calculate percent used on volume */ | ||
429 | percent_used_disk_space = (unsigned long)(((double)used_disk_space / (double)total_disk_space) * 100.0); | ||
430 | |||
431 | if (check_critical_value && percent_used_disk_space >= critical_value) | ||
432 | result = STATE_CRITICAL; | ||
433 | else if (check_warning_value && percent_used_disk_space >= warning_value) | ||
434 | result = STATE_WARNING; | ||
435 | |||
436 | asprintf(&output_message, _("%lu MB (%lu%%) used on volume %s - total %lu MB|Used space in percent on %s=%lu;%lu;%lu;0;100"), | ||
437 | used_disk_space, percent_used_disk_space, volume_name, total_disk_space, volume_name, percent_used_disk_space, | ||
438 | warning_value, critical_value); | ||
439 | } | ||
440 | |||
441 | /* check % free space on volume */ | ||
442 | } else if (vars_to_check == VPF) { | ||
443 | |||
444 | close(sd); | ||
445 | my_tcp_connect(server_address, server_port, &sd); | ||
446 | |||
447 | xasprintf(&send_buffer, "VKF%s\r\n", volume_name); | ||
448 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
449 | if (result != STATE_OK) | ||
450 | return result; | ||
451 | |||
452 | if (!strcmp(recv_buffer, "-1\n")) { | ||
453 | |||
454 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
455 | result = STATE_CRITICAL; | ||
456 | |||
457 | } else { | ||
458 | |||
459 | free_disk_space = strtoul(recv_buffer, NULL, 10); | ||
460 | |||
461 | close(sd); | ||
462 | my_tcp_connect(server_address, server_port, &sd); | ||
463 | |||
464 | xasprintf(&send_buffer, "VKS%s\r\n", volume_name); | ||
465 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
466 | if (result != STATE_OK) | ||
467 | return result; | ||
468 | total_disk_space = strtoul(recv_buffer, NULL, 10); | ||
469 | |||
470 | percent_free_space = (unsigned long)(((double)free_disk_space / (double)total_disk_space) * 100.0); | ||
471 | |||
472 | if (check_critical_value && percent_free_space <= critical_value) | ||
473 | result = STATE_CRITICAL; | ||
474 | else if (check_warning_value && percent_free_space <= warning_value) | ||
475 | result = STATE_WARNING; | ||
476 | free_disk_space /= 1024; | ||
477 | total_disk_space /= 1024; | ||
478 | xasprintf(&output_message, _("%lu MB (%lu%%) free on volume %s - total %lu MB|FreeMB%s=%lu;%lu;%lu;0;100"), free_disk_space, | ||
479 | percent_free_space, volume_name, total_disk_space, volume_name, percent_free_space, warning_value, critical_value); | ||
480 | } | ||
481 | |||
482 | /* check to see if DS Database is open or closed */ | ||
483 | } else if (vars_to_check == DSDB) { | ||
484 | |||
485 | close(sd); | ||
486 | my_tcp_connect(server_address, server_port, &sd); | ||
487 | |||
488 | send_buffer = strdup("S11\r\n"); | ||
489 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
490 | if (result != STATE_OK) | ||
491 | return result; | ||
492 | if (atoi(recv_buffer) == 1) | ||
493 | result = STATE_OK; | ||
494 | else | ||
495 | result = STATE_WARNING; | ||
496 | |||
497 | close(sd); | ||
498 | my_tcp_connect(server_address, server_port, &sd); | ||
499 | |||
500 | send_buffer = strdup("S13\r\n"); | ||
501 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
502 | temp_buffer = strtok(recv_buffer, "\r\n"); | ||
503 | |||
504 | xasprintf(&output_message, _("Directory Services Database is %s (DS version %s)"), (result == STATE_OK) ? "open" : "closed", | ||
505 | temp_buffer); | ||
506 | |||
507 | /* check to see if logins are enabled */ | ||
508 | } else if (vars_to_check == LOGINS) { | ||
509 | |||
510 | close(sd); | ||
511 | my_tcp_connect(server_address, server_port, &sd); | ||
512 | |||
513 | send_buffer = strdup("S12\r\n"); | ||
514 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
515 | if (result != STATE_OK) | ||
516 | return result; | ||
517 | if (atoi(recv_buffer) == 1) | ||
518 | result = STATE_OK; | ||
519 | else | ||
520 | result = STATE_WARNING; | ||
521 | |||
522 | xasprintf(&output_message, _("Logins are %s"), (result == STATE_OK) ? _("enabled") : _("disabled")); | ||
523 | |||
524 | /* check NRM Health Status Summary*/ | ||
525 | } else if (vars_to_check == NRMH) { | ||
526 | |||
527 | xasprintf(&send_buffer, "NRMH\r\n"); | ||
528 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
529 | if (result != STATE_OK) | ||
530 | return result; | ||
531 | |||
532 | nrm_health_status = atoi(recv_buffer); | ||
533 | |||
534 | if (nrm_health_status == 2) { | ||
535 | result = STATE_OK; | ||
536 | xasprintf(&output_message, _("CRITICAL - NRM Status is bad!")); | ||
537 | } else { | ||
538 | if (nrm_health_status == 1) { | ||
539 | result = STATE_WARNING; | ||
540 | xasprintf(&output_message, _("Warning - NRM Status is suspect!")); | ||
541 | } | ||
542 | |||
543 | xasprintf(&output_message, _("OK - NRM Status is good!")); | ||
544 | } | ||
545 | |||
546 | /* check packet receive buffers */ | ||
547 | } else if (vars_to_check == UPRB || vars_to_check == PUPRB) { | ||
548 | |||
549 | close(sd); | ||
550 | my_tcp_connect(server_address, server_port, &sd); | ||
551 | |||
552 | xasprintf(&send_buffer, "S15\r\n"); | ||
553 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
554 | if (result != STATE_OK) | ||
555 | return result; | ||
556 | |||
557 | used_packet_receive_buffers = atoi(recv_buffer); | ||
558 | |||
559 | close(sd); | ||
560 | my_tcp_connect(server_address, server_port, &sd); | ||
561 | |||
562 | xasprintf(&send_buffer, "S16\r\n"); | ||
563 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
564 | if (result != STATE_OK) | ||
565 | return result; | ||
566 | |||
567 | max_packet_receive_buffers = atoi(recv_buffer); | ||
568 | |||
569 | percent_used_packet_receive_buffers = | ||
570 | (unsigned long)(((double)used_packet_receive_buffers / (double)max_packet_receive_buffers) * 100.0); | ||
571 | |||
572 | if (vars_to_check == UPRB) { | ||
573 | if (check_critical_value && used_packet_receive_buffers >= critical_value) | ||
574 | result = STATE_CRITICAL; | ||
575 | else if (check_warning_value && used_packet_receive_buffers >= warning_value) | ||
576 | result = STATE_WARNING; | ||
577 | } else { | ||
578 | if (check_critical_value && percent_used_packet_receive_buffers >= critical_value) | ||
579 | result = STATE_CRITICAL; | ||
580 | else if (check_warning_value && percent_used_packet_receive_buffers >= warning_value) | ||
581 | result = STATE_WARNING; | ||
582 | } | ||
583 | |||
584 | xasprintf(&output_message, _("%lu of %lu (%lu%%) packet receive buffers used"), used_packet_receive_buffers, | ||
585 | max_packet_receive_buffers, percent_used_packet_receive_buffers); | ||
586 | |||
587 | /* check SAP table entries */ | ||
588 | } else if (vars_to_check == SAPENTRIES) { | ||
589 | |||
590 | close(sd); | ||
591 | my_tcp_connect(server_address, server_port, &sd); | ||
592 | |||
593 | if (sap_number == -1) | ||
594 | xasprintf(&send_buffer, "S9\r\n"); | ||
595 | else | ||
596 | xasprintf(&send_buffer, "S9.%d\r\n", sap_number); | ||
597 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
598 | if (result != STATE_OK) | ||
599 | return result; | ||
600 | |||
601 | sap_entries = atoi(recv_buffer); | ||
602 | |||
603 | if (check_critical_value && sap_entries >= critical_value) | ||
604 | result = STATE_CRITICAL; | ||
605 | else if (check_warning_value && sap_entries >= warning_value) | ||
606 | result = STATE_WARNING; | ||
607 | |||
608 | if (sap_number == -1) | ||
609 | xasprintf(&output_message, _("%lu entries in SAP table"), sap_entries); | ||
610 | else | ||
611 | xasprintf(&output_message, _("%lu entries in SAP table for SAP type %d"), sap_entries, sap_number); | ||
612 | |||
613 | /* check KB purgeable space on volume */ | ||
614 | } else if (vars_to_check == VKP) { | ||
615 | |||
616 | close(sd); | ||
617 | my_tcp_connect(server_address, server_port, &sd); | ||
618 | |||
619 | xasprintf(&send_buffer, "VKP%s\r\n", volume_name); | ||
620 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
621 | if (result != STATE_OK) | ||
622 | return result; | ||
623 | |||
624 | if (!strcmp(recv_buffer, "-1\n")) { | ||
625 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
626 | result = STATE_CRITICAL; | ||
627 | } else { | ||
628 | purgeable_disk_space = strtoul(recv_buffer, NULL, 10); | ||
629 | if (check_critical_value && purgeable_disk_space >= critical_value) | ||
630 | result = STATE_CRITICAL; | ||
631 | else if (check_warning_value && purgeable_disk_space >= warning_value) | ||
632 | result = STATE_WARNING; | ||
633 | xasprintf(&output_message, _("%s%lu KB purgeable on volume %s|Purge%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), | ||
634 | purgeable_disk_space, volume_name, volume_name, purgeable_disk_space, warning_value, critical_value); | ||
635 | } | ||
636 | /* check MB purgeable space on volume */ | ||
637 | } else if (vars_to_check == VMP) { | ||
638 | |||
639 | xasprintf(&send_buffer, "VMP%s\r\n", volume_name); | ||
640 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
641 | if (result != STATE_OK) | ||
642 | return result; | ||
643 | |||
644 | if (!strcmp(recv_buffer, "-1\n")) { | ||
645 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
646 | result = STATE_CRITICAL; | ||
647 | } else { | ||
648 | purgeable_disk_space = strtoul(recv_buffer, NULL, 10); | ||
649 | if (check_critical_value && purgeable_disk_space >= critical_value) | ||
650 | result = STATE_CRITICAL; | ||
651 | else if (check_warning_value && purgeable_disk_space >= warning_value) | ||
652 | result = STATE_WARNING; | ||
653 | xasprintf(&output_message, _("%s%lu MB purgeable on volume %s|Purge%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), | ||
654 | purgeable_disk_space, volume_name, volume_name, purgeable_disk_space, warning_value, critical_value); | ||
655 | } | ||
656 | |||
657 | /* check % purgeable space on volume */ | ||
658 | } else if (vars_to_check == VPP) { | ||
659 | |||
660 | close(sd); | ||
661 | my_tcp_connect(server_address, server_port, &sd); | ||
662 | |||
663 | xasprintf(&send_buffer, "VKP%s\r\n", volume_name); | ||
664 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
665 | if (result != STATE_OK) | ||
666 | return result; | ||
667 | |||
668 | if (!strcmp(recv_buffer, "-1\n")) { | ||
669 | |||
670 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
671 | result = STATE_CRITICAL; | ||
672 | |||
673 | } else { | ||
674 | |||
675 | purgeable_disk_space = strtoul(recv_buffer, NULL, 10); | ||
676 | |||
677 | close(sd); | ||
678 | my_tcp_connect(server_address, server_port, &sd); | ||
679 | |||
680 | xasprintf(&send_buffer, "VKS%s\r\n", volume_name); | ||
681 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
682 | if (result != STATE_OK) | ||
683 | return result; | ||
684 | total_disk_space = strtoul(recv_buffer, NULL, 10); | ||
685 | |||
686 | percent_purgeable_space = (unsigned long)(((double)purgeable_disk_space / (double)total_disk_space) * 100.0); | ||
687 | |||
688 | if (check_critical_value && percent_purgeable_space >= critical_value) | ||
689 | result = STATE_CRITICAL; | ||
690 | else if (check_warning_value && percent_purgeable_space >= warning_value) | ||
691 | result = STATE_WARNING; | ||
692 | purgeable_disk_space /= 1024; | ||
693 | xasprintf(&output_message, _("%lu MB (%lu%%) purgeable on volume %s|Purgeable%s=%lu;%lu;%lu;0;100"), purgeable_disk_space, | ||
694 | percent_purgeable_space, volume_name, volume_name, percent_purgeable_space, warning_value, critical_value); | ||
695 | } | ||
696 | |||
697 | /* check KB not yet purgeable space on volume */ | ||
698 | } else if (vars_to_check == VKNP) { | ||
699 | |||
700 | close(sd); | ||
701 | my_tcp_connect(server_address, server_port, &sd); | ||
702 | |||
703 | xasprintf(&send_buffer, "VKNP%s\r\n", volume_name); | ||
704 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
705 | if (result != STATE_OK) | ||
706 | return result; | ||
707 | |||
708 | if (!strcmp(recv_buffer, "-1\n")) { | ||
709 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
710 | result = STATE_CRITICAL; | ||
711 | } else { | ||
712 | non_purgeable_disk_space = strtoul(recv_buffer, NULL, 10); | ||
713 | if (check_critical_value && non_purgeable_disk_space >= critical_value) | ||
714 | result = STATE_CRITICAL; | ||
715 | else if (check_warning_value && non_purgeable_disk_space >= warning_value) | ||
716 | result = STATE_WARNING; | ||
717 | xasprintf(&output_message, _("%s%lu KB not yet purgeable on volume %s"), (result == STATE_OK) ? "" : _("Only "), | ||
718 | non_purgeable_disk_space, volume_name); | ||
719 | } | ||
720 | |||
721 | /* check % not yet purgeable space on volume */ | ||
722 | } else if (vars_to_check == VPNP) { | ||
723 | |||
724 | close(sd); | ||
725 | my_tcp_connect(server_address, server_port, &sd); | ||
726 | |||
727 | xasprintf(&send_buffer, "VKNP%s\r\n", volume_name); | ||
728 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
729 | if (result != STATE_OK) | ||
730 | return result; | ||
731 | |||
732 | if (!strcmp(recv_buffer, "-1\n")) { | ||
733 | |||
734 | xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); | ||
735 | result = STATE_CRITICAL; | ||
736 | |||
737 | } else { | ||
738 | |||
739 | non_purgeable_disk_space = strtoul(recv_buffer, NULL, 10); | ||
740 | |||
741 | close(sd); | ||
742 | my_tcp_connect(server_address, server_port, &sd); | ||
743 | |||
744 | xasprintf(&send_buffer, "VKS%s\r\n", volume_name); | ||
745 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
746 | if (result != STATE_OK) | ||
747 | return result; | ||
748 | total_disk_space = strtoul(recv_buffer, NULL, 10); | ||
749 | |||
750 | percent_non_purgeable_space = (unsigned long)(((double)non_purgeable_disk_space / (double)total_disk_space) * 100.0); | ||
751 | |||
752 | if (check_critical_value && percent_non_purgeable_space >= critical_value) | ||
753 | result = STATE_CRITICAL; | ||
754 | else if (check_warning_value && percent_non_purgeable_space >= warning_value) | ||
755 | result = STATE_WARNING; | ||
756 | purgeable_disk_space /= 1024; | ||
757 | xasprintf(&output_message, _("%lu MB (%lu%%) not yet purgeable on volume %s"), non_purgeable_disk_space, | ||
758 | percent_non_purgeable_space, volume_name); | ||
759 | } | ||
760 | |||
761 | /* check # of open files */ | ||
762 | } else if (vars_to_check == OFILES) { | ||
763 | |||
764 | close(sd); | ||
765 | my_tcp_connect(server_address, server_port, &sd); | ||
766 | |||
767 | xasprintf(&send_buffer, "S18\r\n"); | ||
768 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
769 | if (result != STATE_OK) | ||
770 | return result; | ||
771 | |||
772 | open_files = atoi(recv_buffer); | ||
773 | |||
774 | if (check_critical_value && open_files >= critical_value) | ||
775 | result = STATE_CRITICAL; | ||
776 | else if (check_warning_value && open_files >= warning_value) | ||
777 | result = STATE_WARNING; | ||
778 | |||
779 | xasprintf(&output_message, _("%lu open files|Openfiles=%lu;%lu;%lu;0,0"), open_files, open_files, warning_value, critical_value); | ||
780 | |||
781 | /* check # of abended threads (Netware > 5.x only) */ | ||
782 | } else if (vars_to_check == ABENDS) { | ||
783 | |||
784 | close(sd); | ||
785 | my_tcp_connect(server_address, server_port, &sd); | ||
786 | |||
787 | xasprintf(&send_buffer, "S17\r\n"); | ||
788 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
789 | if (result != STATE_OK) | ||
790 | return result; | ||
791 | |||
792 | abended_threads = atoi(recv_buffer); | ||
793 | |||
794 | if (check_critical_value && abended_threads >= critical_value) | ||
795 | result = STATE_CRITICAL; | ||
796 | else if (check_warning_value && abended_threads >= warning_value) | ||
797 | result = STATE_WARNING; | ||
798 | |||
799 | xasprintf(&output_message, _("%lu abended threads|Abends=%lu;%lu;%lu;;"), abended_threads, abended_threads, warning_value, | ||
800 | critical_value); | ||
801 | |||
802 | /* check # of current service processes (Netware 5.x only) */ | ||
803 | } else if (vars_to_check == CSPROCS) { | ||
804 | |||
805 | close(sd); | ||
806 | my_tcp_connect(server_address, server_port, &sd); | ||
807 | |||
808 | xasprintf(&send_buffer, "S20\r\n"); | ||
809 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
810 | if (result != STATE_OK) | ||
811 | return result; | ||
812 | |||
813 | max_service_processes = atoi(recv_buffer); | ||
814 | |||
815 | close(sd); | ||
816 | my_tcp_connect(server_address, server_port, &sd); | ||
817 | |||
818 | xasprintf(&send_buffer, "S21\r\n"); | ||
819 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
820 | if (result != STATE_OK) | ||
821 | return result; | ||
822 | |||
823 | current_service_processes = atoi(recv_buffer); | ||
824 | |||
825 | if (check_critical_value && current_service_processes >= critical_value) | ||
826 | result = STATE_CRITICAL; | ||
827 | else if (check_warning_value && current_service_processes >= warning_value) | ||
828 | result = STATE_WARNING; | ||
829 | |||
830 | xasprintf(&output_message, _("%lu current service processes (%lu max)|Processes=%lu;%lu;%lu;0;%lu"), current_service_processes, | ||
831 | max_service_processes, current_service_processes, warning_value, critical_value, max_service_processes); | ||
832 | |||
833 | /* check # Timesync Status */ | ||
834 | } else if (vars_to_check == TSYNC) { | ||
835 | |||
836 | close(sd); | ||
837 | my_tcp_connect(server_address, server_port, &sd); | ||
838 | |||
839 | xasprintf(&send_buffer, "S22\r\n"); | ||
840 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
841 | if (result != STATE_OK) | ||
842 | return result; | ||
843 | |||
844 | time_sync_status = atoi(recv_buffer); | ||
845 | |||
846 | if (time_sync_status == 0) { | ||
847 | result = STATE_CRITICAL; | ||
848 | xasprintf(&output_message, _("CRITICAL - Time not in sync with network!")); | ||
849 | } else { | ||
850 | xasprintf(&output_message, _("OK - Time in sync with network!")); | ||
851 | } | ||
852 | |||
853 | /* check LRU sitting time in secondss */ | ||
854 | } else if (vars_to_check == LRUS) { | ||
855 | |||
856 | close(sd); | ||
857 | my_tcp_connect(server_address, server_port, &sd); | ||
858 | |||
859 | send_buffer = strdup("S4\r\n"); | ||
860 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
861 | if (result != STATE_OK) | ||
862 | return result; | ||
863 | lru_time = strtoul(recv_buffer, NULL, 10); | ||
864 | |||
865 | if (check_critical_value && lru_time <= critical_value) | ||
866 | result = STATE_CRITICAL; | ||
867 | else if (check_warning_value && lru_time <= warning_value) | ||
868 | result = STATE_WARNING; | ||
869 | xasprintf(&output_message, _("LRU sitting time = %lu seconds"), lru_time); | ||
870 | |||
871 | /* check % dirty cacheobuffers as a percentage of the total*/ | ||
872 | } else if (vars_to_check == DCB) { | ||
873 | |||
874 | close(sd); | ||
875 | my_tcp_connect(server_address, server_port, &sd); | ||
876 | |||
877 | send_buffer = strdup("S6\r\n"); | ||
878 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
879 | if (result != STATE_OK) | ||
880 | return result; | ||
881 | dirty_cache_buffers = atoi(recv_buffer); | ||
882 | |||
883 | if (check_critical_value && dirty_cache_buffers <= critical_value) | ||
884 | result = STATE_CRITICAL; | ||
885 | else if (check_warning_value && dirty_cache_buffers <= warning_value) | ||
886 | result = STATE_WARNING; | ||
887 | xasprintf(&output_message, _("Dirty cache buffers = %lu%% of the total|DCB=%lu;%lu;%lu;0;100"), dirty_cache_buffers, | ||
888 | dirty_cache_buffers, warning_value, critical_value); | ||
889 | |||
890 | /* check % total cache buffers as a percentage of the original*/ | ||
891 | } else if (vars_to_check == TCB) { | ||
892 | |||
893 | close(sd); | ||
894 | my_tcp_connect(server_address, server_port, &sd); | ||
895 | |||
896 | send_buffer = strdup("S7\r\n"); | ||
897 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
898 | if (result != STATE_OK) | ||
899 | return result; | ||
900 | total_cache_buffers = atoi(recv_buffer); | ||
901 | |||
902 | if (check_critical_value && total_cache_buffers <= critical_value) | ||
903 | result = STATE_CRITICAL; | ||
904 | else if (check_warning_value && total_cache_buffers <= warning_value) | ||
905 | result = STATE_WARNING; | ||
906 | xasprintf(&output_message, _("Total cache buffers = %lu%% of the original|TCB=%lu;%lu;%lu;0;100"), total_cache_buffers, | ||
907 | total_cache_buffers, warning_value, critical_value); | ||
908 | |||
909 | } else if (vars_to_check == DSVER) { | ||
910 | |||
911 | close(sd); | ||
912 | my_tcp_connect(server_address, server_port, &sd); | ||
913 | |||
914 | xasprintf(&send_buffer, "S13\r\n"); | ||
915 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
916 | if (result != STATE_OK) | ||
917 | return result; | ||
918 | |||
919 | recv_buffer[strlen(recv_buffer) - 1] = 0; | ||
920 | |||
921 | xasprintf(&output_message, _("NDS Version %s"), recv_buffer); | ||
922 | |||
923 | } else if (vars_to_check == UPTIME) { | ||
924 | |||
925 | close(sd); | ||
926 | my_tcp_connect(server_address, server_port, &sd); | ||
927 | |||
928 | xasprintf(&send_buffer, "UPTIME\r\n"); | ||
929 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
930 | if (result != STATE_OK) | ||
931 | return result; | ||
932 | |||
933 | recv_buffer[sizeof(recv_buffer) - 1] = 0; | ||
934 | recv_buffer[strlen(recv_buffer) - 1] = 0; | ||
935 | |||
936 | xasprintf(&output_message, _("Up %s"), recv_buffer); | ||
937 | |||
938 | } else if (vars_to_check == NLM) { | ||
939 | |||
940 | close(sd); | ||
941 | my_tcp_connect(server_address, server_port, &sd); | ||
942 | |||
943 | xasprintf(&send_buffer, "S24:%s\r\n", nlm_name); | ||
944 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
945 | if (result != STATE_OK) | ||
946 | return result; | ||
947 | |||
948 | recv_buffer[strlen(recv_buffer) - 1] = 0; | ||
949 | if (strcmp(recv_buffer, "-1")) { | ||
950 | xasprintf(&output_message, _("Module %s version %s is loaded"), nlm_name, recv_buffer); | ||
951 | } else { | ||
952 | result = STATE_CRITICAL; | ||
953 | xasprintf(&output_message, _("Module %s is not loaded"), nlm_name); | ||
954 | } | ||
955 | } else if (vars_to_check == NRMP) { | ||
956 | |||
957 | xasprintf(&send_buffer, "NRMP:%s\r\n", nrmp_name); | ||
958 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
959 | if (result != STATE_OK) | ||
960 | return result; | ||
961 | |||
962 | if (!strcmp(recv_buffer, "-1\n")) { | ||
963 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nrmp_name); | ||
964 | result = STATE_CRITICAL; | ||
965 | } else { | ||
966 | nrmp_value = strtoul(recv_buffer, NULL, 10); | ||
967 | if (check_critical_value && nrmp_value <= critical_value) | ||
968 | result = STATE_CRITICAL; | ||
969 | else if (check_warning_value && nrmp_value <= warning_value) | ||
970 | result = STATE_WARNING; | ||
971 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nrmp_name, nrmp_value, nrmp_name, nrmp_value, warning_value, | ||
972 | critical_value); | ||
973 | } | ||
974 | |||
975 | } else if (vars_to_check == NRMM) { | ||
976 | |||
977 | xasprintf(&send_buffer, "NRMM:%s\r\n", nrmm_name); | ||
978 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
979 | if (result != STATE_OK) | ||
980 | return result; | ||
981 | |||
982 | if (!strcmp(recv_buffer, "-1\n")) { | ||
983 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nrmm_name); | ||
984 | result = STATE_CRITICAL; | ||
985 | } else { | ||
986 | nrmm_value = strtoul(recv_buffer, NULL, 10); | ||
987 | if (check_critical_value && nrmm_value <= critical_value) | ||
988 | result = STATE_CRITICAL; | ||
989 | else if (check_warning_value && nrmm_value <= warning_value) | ||
990 | result = STATE_WARNING; | ||
991 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nrmm_name, nrmm_value, nrmm_name, nrmm_value, warning_value, | ||
992 | critical_value); | ||
993 | } | ||
994 | |||
995 | } else if (vars_to_check == NRMS) { | ||
996 | |||
997 | xasprintf(&send_buffer, "NRMS:%s\r\n", nrms_name); | ||
998 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
999 | if (result != STATE_OK) | ||
1000 | return result; | ||
1001 | |||
1002 | if (!strcmp(recv_buffer, "-1\n")) { | ||
1003 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nrms_name); | ||
1004 | result = STATE_CRITICAL; | ||
1005 | } else { | ||
1006 | nrms_value = strtoul(recv_buffer, NULL, 10); | ||
1007 | if (check_critical_value && nrms_value >= critical_value) | ||
1008 | result = STATE_CRITICAL; | ||
1009 | else if (check_warning_value && nrms_value >= warning_value) | ||
1010 | result = STATE_WARNING; | ||
1011 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nrms_name, nrms_value, nrms_name, nrms_value, warning_value, | ||
1012 | critical_value); | ||
1013 | } | ||
1014 | |||
1015 | } else if (vars_to_check == NSS1) { | ||
1016 | |||
1017 | xasprintf(&send_buffer, "NSS1:%s\r\n", nss1_name); | ||
1018 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
1019 | if (result != STATE_OK) | ||
1020 | return result; | ||
1021 | |||
1022 | if (!strcmp(recv_buffer, "-1\n")) { | ||
1023 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss1_name); | ||
1024 | result = STATE_CRITICAL; | ||
1025 | } else { | ||
1026 | nss1_value = strtoul(recv_buffer, NULL, 10); | ||
1027 | if (check_critical_value && nss1_value >= critical_value) | ||
1028 | result = STATE_CRITICAL; | ||
1029 | else if (check_warning_value && nss1_value >= warning_value) | ||
1030 | result = STATE_WARNING; | ||
1031 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss1_name, nss1_value, nss1_name, nss1_value, warning_value, | ||
1032 | critical_value); | ||
1033 | } | ||
1034 | |||
1035 | } else if (vars_to_check == NSS2) { | ||
1036 | |||
1037 | xasprintf(&send_buffer, "NSS2:%s\r\n", nss2_name); | ||
1038 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
1039 | if (result != STATE_OK) | ||
1040 | return result; | ||
1041 | |||
1042 | if (!strcmp(recv_buffer, "-1\n")) { | ||
1043 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss2_name); | ||
1044 | result = STATE_CRITICAL; | ||
1045 | } else { | ||
1046 | nss2_value = strtoul(recv_buffer, NULL, 10); | ||
1047 | if (check_critical_value && nss2_value >= critical_value) | ||
1048 | result = STATE_CRITICAL; | ||
1049 | else if (check_warning_value && nss2_value >= warning_value) | ||
1050 | result = STATE_WARNING; | ||
1051 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss2_name, nss2_value, nss2_name, nss2_value, warning_value, | ||
1052 | critical_value); | ||
1053 | } | ||
1054 | |||
1055 | } else if (vars_to_check == NSS3) { | ||
1056 | |||
1057 | xasprintf(&send_buffer, "NSS3:%s\r\n", nss3_name); | ||
1058 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
1059 | if (result != STATE_OK) | ||
1060 | return result; | ||
1061 | |||
1062 | if (!strcmp(recv_buffer, "-1\n")) { | ||
1063 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss3_name); | ||
1064 | result = STATE_CRITICAL; | ||
1065 | } else { | ||
1066 | nss3_value = strtoul(recv_buffer, NULL, 10); | ||
1067 | if (check_critical_value && nss3_value >= critical_value) | ||
1068 | result = STATE_CRITICAL; | ||
1069 | else if (check_warning_value && nss3_value >= warning_value) | ||
1070 | result = STATE_WARNING; | ||
1071 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss3_name, nss3_value, nss3_name, nss3_value, warning_value, | ||
1072 | critical_value); | ||
1073 | } | ||
1074 | |||
1075 | } else if (vars_to_check == NSS4) { | ||
1076 | |||
1077 | xasprintf(&send_buffer, "NSS4:%s\r\n", nss4_name); | ||
1078 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
1079 | if (result != STATE_OK) | ||
1080 | return result; | ||
1081 | |||
1082 | if (!strcmp(recv_buffer, "-1\n")) { | ||
1083 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss4_name); | ||
1084 | result = STATE_CRITICAL; | ||
1085 | } else { | ||
1086 | nss4_value = strtoul(recv_buffer, NULL, 10); | ||
1087 | if (check_critical_value && nss4_value >= critical_value) | ||
1088 | result = STATE_CRITICAL; | ||
1089 | else if (check_warning_value && nss4_value >= warning_value) | ||
1090 | result = STATE_WARNING; | ||
1091 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss4_name, nss4_value, nss4_name, nss4_value, warning_value, | ||
1092 | critical_value); | ||
1093 | } | ||
1094 | |||
1095 | } else if (vars_to_check == NSS5) { | ||
1096 | |||
1097 | xasprintf(&send_buffer, "NSS5:%s\r\n", nss5_name); | ||
1098 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
1099 | if (result != STATE_OK) | ||
1100 | return result; | ||
1101 | |||
1102 | if (!strcmp(recv_buffer, "-1\n")) { | ||
1103 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss5_name); | ||
1104 | result = STATE_CRITICAL; | ||
1105 | } else { | ||
1106 | nss5_value = strtoul(recv_buffer, NULL, 10); | ||
1107 | if (check_critical_value && nss5_value >= critical_value) | ||
1108 | result = STATE_CRITICAL; | ||
1109 | else if (check_warning_value && nss5_value >= warning_value) | ||
1110 | result = STATE_WARNING; | ||
1111 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss5_name, nss5_value, nss5_name, nss5_value, warning_value, | ||
1112 | critical_value); | ||
1113 | } | ||
1114 | |||
1115 | } else if (vars_to_check == NSS6) { | ||
1116 | |||
1117 | xasprintf(&send_buffer, "NSS6:%s\r\n", nss6_name); | ||
1118 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
1119 | if (result != STATE_OK) | ||
1120 | return result; | ||
1121 | |||
1122 | if (!strcmp(recv_buffer, "-1\n")) { | ||
1123 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss6_name); | ||
1124 | result = STATE_CRITICAL; | ||
1125 | } else { | ||
1126 | nss6_value = strtoul(recv_buffer, NULL, 10); | ||
1127 | if (check_critical_value && nss6_value >= critical_value) | ||
1128 | result = STATE_CRITICAL; | ||
1129 | else if (check_warning_value && nss6_value >= warning_value) | ||
1130 | result = STATE_WARNING; | ||
1131 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss6_name, nss6_value, nss6_name, nss6_value, warning_value, | ||
1132 | critical_value); | ||
1133 | } | ||
1134 | |||
1135 | } else if (vars_to_check == NSS7) { | ||
1136 | |||
1137 | xasprintf(&send_buffer, "NSS7:%s\r\n", nss7_name); | ||
1138 | result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); | ||
1139 | if (result != STATE_OK) | ||
1140 | return result; | ||
1141 | |||
1142 | if (!strcmp(recv_buffer, "-1\n")) { | ||
1143 | xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss7_name); | ||
1144 | result = STATE_CRITICAL; | ||
1145 | } else { | ||
1146 | nss7_value = strtoul(recv_buffer, NULL, 10); | ||
1147 | if (check_critical_value && nss7_value >= critical_value) | ||
1148 | result = STATE_CRITICAL; | ||
1149 | else if (check_warning_value && nss7_value >= warning_value) | ||
1150 | result = STATE_WARNING; | ||
1151 | xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss7_name, nss7_value, nss7_name, nss7_value, warning_value, | ||
1152 | critical_value); | ||
1153 | } | ||
1154 | |||
1155 | } else { | ||
1156 | |||
1157 | output_message = strdup(_("Nothing to check!\n")); | ||
1158 | result = STATE_UNKNOWN; | ||
1159 | } | ||
1160 | |||
1161 | close(sd); | ||
1162 | |||
1163 | /* reset timeout */ | ||
1164 | alarm(0); | ||
1165 | |||
1166 | printf("%s%s\n", netware_version, output_message); | ||
1167 | |||
1168 | return result; | ||
1169 | } | ||
1170 | |||
1171 | /* process command-line arguments */ | ||
1172 | int process_arguments(int argc, char **argv) { | ||
1173 | int c; | ||
1174 | |||
1175 | int option = 0; | ||
1176 | static struct option longopts[] = {{"port", required_argument, 0, 'p'}, {"timeout", required_argument, 0, 't'}, | ||
1177 | {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, | ||
1178 | {"variable", required_argument, 0, 'v'}, {"hostname", required_argument, 0, 'H'}, | ||
1179 | {"osversion", no_argument, 0, 'o'}, {"version", no_argument, 0, 'V'}, | ||
1180 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | ||
1181 | |||
1182 | /* no options were supplied */ | ||
1183 | if (argc < 2) | ||
1184 | return ERROR; | ||
1185 | |||
1186 | /* backwards compatibility */ | ||
1187 | if (!is_option(argv[1])) { | ||
1188 | server_address = argv[1]; | ||
1189 | argv[1] = argv[0]; | ||
1190 | argv = &argv[1]; | ||
1191 | argc--; | ||
1192 | } | ||
1193 | |||
1194 | for (c = 1; c < argc; c++) { | ||
1195 | if (strcmp("-to", argv[c]) == 0) | ||
1196 | strcpy(argv[c], "-t"); | ||
1197 | else if (strcmp("-wv", argv[c]) == 0) | ||
1198 | strcpy(argv[c], "-w"); | ||
1199 | else if (strcmp("-cv", argv[c]) == 0) | ||
1200 | strcpy(argv[c], "-c"); | ||
1201 | } | ||
1202 | |||
1203 | while (1) { | ||
1204 | c = getopt_long(argc, argv, "+hoVH:t:c:w:p:v:", longopts, &option); | ||
1205 | |||
1206 | if (c == -1 || c == EOF || c == 1) | ||
1207 | break; | ||
1208 | |||
1209 | switch (c) { | ||
1210 | case '?': /* print short usage statement if args not parsable */ | ||
1211 | usage5(); | ||
1212 | case 'h': /* help */ | ||
1213 | print_help(); | ||
1214 | exit(STATE_UNKNOWN); | ||
1215 | case 'V': /* version */ | ||
1216 | print_revision(progname, NP_VERSION); | ||
1217 | exit(STATE_UNKNOWN); | ||
1218 | case 'H': /* hostname */ | ||
1219 | server_address = optarg; | ||
1220 | break; | ||
1221 | case 'o': /* display nos version */ | ||
1222 | check_netware_version = true; | ||
1223 | break; | ||
1224 | case 'p': /* port */ | ||
1225 | if (is_intnonneg(optarg)) | ||
1226 | server_port = atoi(optarg); | ||
1227 | else | ||
1228 | die(STATE_UNKNOWN, _("Server port an integer\n")); | ||
1229 | break; | ||
1230 | case 'v': | ||
1231 | if (strlen(optarg) < 3) | ||
1232 | return ERROR; | ||
1233 | if (!strcmp(optarg, "LOAD1")) | ||
1234 | vars_to_check = LOAD1; | ||
1235 | else if (!strcmp(optarg, "LOAD5")) | ||
1236 | vars_to_check = LOAD5; | ||
1237 | else if (!strcmp(optarg, "LOAD15")) | ||
1238 | vars_to_check = LOAD15; | ||
1239 | else if (!strcmp(optarg, "CONNS")) | ||
1240 | vars_to_check = CONNS; | ||
1241 | else if (!strcmp(optarg, "LTCH")) | ||
1242 | vars_to_check = LTCH; | ||
1243 | else if (!strcmp(optarg, "DCB")) | ||
1244 | vars_to_check = DCB; | ||
1245 | else if (!strcmp(optarg, "TCB")) | ||
1246 | vars_to_check = TCB; | ||
1247 | else if (!strcmp(optarg, "CBUFF")) | ||
1248 | vars_to_check = CBUFF; | ||
1249 | else if (!strcmp(optarg, "CDBUFF")) | ||
1250 | vars_to_check = CDBUFF; | ||
1251 | else if (!strcmp(optarg, "LRUM")) | ||
1252 | vars_to_check = LRUM; | ||
1253 | else if (!strcmp(optarg, "LRUS")) | ||
1254 | vars_to_check = LRUS; | ||
1255 | else if (strncmp(optarg, "VPF", 3) == 0) { | ||
1256 | vars_to_check = VPF; | ||
1257 | volume_name = strdup(optarg + 3); | ||
1258 | if (!strcmp(volume_name, "")) | ||
1259 | volume_name = strdup("SYS"); | ||
1260 | } else if (strncmp(optarg, "VKF", 3) == 0) { | ||
1261 | vars_to_check = VKF; | ||
1262 | volume_name = strdup(optarg + 3); | ||
1263 | if (!strcmp(volume_name, "")) | ||
1264 | volume_name = strdup("SYS"); | ||
1265 | } else if (strncmp(optarg, "VMF", 3) == 0) { | ||
1266 | vars_to_check = VMF; | ||
1267 | volume_name = strdup(optarg + 3); | ||
1268 | if (!strcmp(volume_name, "")) | ||
1269 | volume_name = strdup("SYS"); | ||
1270 | } else if (!strcmp(optarg, "DSDB")) | ||
1271 | vars_to_check = DSDB; | ||
1272 | else if (!strcmp(optarg, "LOGINS")) | ||
1273 | vars_to_check = LOGINS; | ||
1274 | else if (!strcmp(optarg, "NRMH")) | ||
1275 | vars_to_check = NRMH; | ||
1276 | else if (!strcmp(optarg, "UPRB")) | ||
1277 | vars_to_check = UPRB; | ||
1278 | else if (!strcmp(optarg, "PUPRB")) | ||
1279 | vars_to_check = PUPRB; | ||
1280 | else if (!strncmp(optarg, "SAPENTRIES", 10)) { | ||
1281 | vars_to_check = SAPENTRIES; | ||
1282 | if (strlen(optarg) > 10) | ||
1283 | sap_number = atoi(optarg + 10); | ||
1284 | else | ||
1285 | sap_number = -1; | ||
1286 | } else if (!strcmp(optarg, "OFILES")) | ||
1287 | vars_to_check = OFILES; | ||
1288 | else if (strncmp(optarg, "VKP", 3) == 0) { | ||
1289 | vars_to_check = VKP; | ||
1290 | volume_name = strdup(optarg + 3); | ||
1291 | if (!strcmp(volume_name, "")) | ||
1292 | volume_name = strdup("SYS"); | ||
1293 | } else if (strncmp(optarg, "VMP", 3) == 0) { | ||
1294 | vars_to_check = VMP; | ||
1295 | volume_name = strdup(optarg + 3); | ||
1296 | if (!strcmp(volume_name, "")) | ||
1297 | volume_name = strdup("SYS"); | ||
1298 | } else if (strncmp(optarg, "VMU", 3) == 0) { | ||
1299 | vars_to_check = VMU; | ||
1300 | volume_name = strdup(optarg + 3); | ||
1301 | if (!strcmp(volume_name, "")) | ||
1302 | volume_name = strdup("SYS"); | ||
1303 | } else if (strncmp(optarg, "VPU", 3) == 0) { | ||
1304 | vars_to_check = VPU; | ||
1305 | volume_name = strdup(optarg + 3); | ||
1306 | if (!strcmp(volume_name, "")) | ||
1307 | volume_name = strdup("SYS"); | ||
1308 | } else if (strncmp(optarg, "VPP", 3) == 0) { | ||
1309 | vars_to_check = VPP; | ||
1310 | volume_name = strdup(optarg + 3); | ||
1311 | if (!strcmp(volume_name, "")) | ||
1312 | volume_name = strdup("SYS"); | ||
1313 | } else if (strncmp(optarg, "VKNP", 4) == 0) { | ||
1314 | vars_to_check = VKNP; | ||
1315 | volume_name = strdup(optarg + 4); | ||
1316 | if (!strcmp(volume_name, "")) | ||
1317 | volume_name = strdup("SYS"); | ||
1318 | } else if (strncmp(optarg, "VPNP", 4) == 0) { | ||
1319 | vars_to_check = VPNP; | ||
1320 | volume_name = strdup(optarg + 4); | ||
1321 | if (!strcmp(volume_name, "")) | ||
1322 | volume_name = strdup("SYS"); | ||
1323 | } else if (!strcmp(optarg, "ABENDS")) | ||
1324 | vars_to_check = ABENDS; | ||
1325 | else if (!strcmp(optarg, "CSPROCS")) | ||
1326 | vars_to_check = CSPROCS; | ||
1327 | else if (!strcmp(optarg, "TSYNC")) | ||
1328 | vars_to_check = TSYNC; | ||
1329 | else if (!strcmp(optarg, "DSVER")) | ||
1330 | vars_to_check = DSVER; | ||
1331 | else if (!strcmp(optarg, "UPTIME")) { | ||
1332 | vars_to_check = UPTIME; | ||
1333 | } else if (strncmp(optarg, "NLM:", 4) == 0) { | ||
1334 | vars_to_check = NLM; | ||
1335 | nlm_name = strdup(optarg + 4); | ||
1336 | } else if (strncmp(optarg, "NRMP", 4) == 0) { | ||
1337 | vars_to_check = NRMP; | ||
1338 | nrmp_name = strdup(optarg + 4); | ||
1339 | if (!strcmp(nrmp_name, "")) | ||
1340 | nrmp_name = strdup("AVAILABLE_MEMORY"); | ||
1341 | } else if (strncmp(optarg, "NRMM", 4) == 0) { | ||
1342 | vars_to_check = NRMM; | ||
1343 | nrmm_name = strdup(optarg + 4); | ||
1344 | if (!strcmp(nrmm_name, "")) | ||
1345 | nrmm_name = strdup("AVAILABLE_CACHE_MEMORY"); | ||
1346 | |||
1347 | } | ||
1348 | |||
1349 | else if (strncmp(optarg, "NRMS", 4) == 0) { | ||
1350 | vars_to_check = NRMS; | ||
1351 | nrms_name = strdup(optarg + 4); | ||
1352 | if (!strcmp(nrms_name, "")) | ||
1353 | nrms_name = strdup("USED_SWAP_SPACE"); | ||
1354 | |||
1355 | } | ||
1356 | |||
1357 | else if (strncmp(optarg, "NSS1", 4) == 0) { | ||
1358 | vars_to_check = NSS1; | ||
1359 | nss1_name = strdup(optarg + 4); | ||
1360 | if (!strcmp(nss1_name, "")) | ||
1361 | nss1_name = strdup("CURRENTBUFFERCACHESIZE"); | ||
1362 | |||
1363 | } | ||
1364 | |||
1365 | else if (strncmp(optarg, "NSS2", 4) == 0) { | ||
1366 | vars_to_check = NSS2; | ||
1367 | nss2_name = strdup(optarg + 4); | ||
1368 | if (!strcmp(nss2_name, "")) | ||
1369 | nss2_name = strdup("CACHEHITS"); | ||
1370 | |||
1371 | } | ||
1372 | |||
1373 | else if (strncmp(optarg, "NSS3", 4) == 0) { | ||
1374 | vars_to_check = NSS3; | ||
1375 | nss3_name = strdup(optarg + 4); | ||
1376 | if (!strcmp(nss3_name, "")) | ||
1377 | nss3_name = strdup("CACHEGITPERCENT"); | ||
1378 | |||
1379 | } | ||
1380 | |||
1381 | else if (strncmp(optarg, "NSS4", 4) == 0) { | ||
1382 | vars_to_check = NSS4; | ||
1383 | nss4_name = strdup(optarg + 4); | ||
1384 | if (!strcmp(nss4_name, "")) | ||
1385 | nss4_name = strdup("CURRENTOPENCOUNT"); | ||
1386 | |||
1387 | } | ||
1388 | |||
1389 | else if (strncmp(optarg, "NSS5", 4) == 0) { | ||
1390 | vars_to_check = NSS5; | ||
1391 | nss5_name = strdup(optarg + 4); | ||
1392 | if (!strcmp(nss5_name, "")) | ||
1393 | nss5_name = strdup("CACHEMISSES"); | ||
1394 | |||
1395 | } | ||
1396 | |||
1397 | else if (strncmp(optarg, "NSS6", 4) == 0) { | ||
1398 | vars_to_check = NSS6; | ||
1399 | nss6_name = strdup(optarg + 4); | ||
1400 | if (!strcmp(nss6_name, "")) | ||
1401 | nss6_name = strdup("PENDINGWORKSCOUNT"); | ||
1402 | |||
1403 | } | ||
1404 | |||
1405 | else if (strncmp(optarg, "NSS7", 4) == 0) { | ||
1406 | vars_to_check = NSS7; | ||
1407 | nss7_name = strdup(optarg + 4); | ||
1408 | if (!strcmp(nss7_name, "")) | ||
1409 | nss7_name = strdup("CACHESIZE"); | ||
1410 | |||
1411 | } | ||
1412 | |||
1413 | else | ||
1414 | return ERROR; | ||
1415 | break; | ||
1416 | case 'w': /* warning threshold */ | ||
1417 | warning_value = strtoul(optarg, NULL, 10); | ||
1418 | check_warning_value = true; | ||
1419 | break; | ||
1420 | case 'c': /* critical threshold */ | ||
1421 | critical_value = strtoul(optarg, NULL, 10); | ||
1422 | check_critical_value = true; | ||
1423 | break; | ||
1424 | case 't': /* timeout */ | ||
1425 | socket_timeout = atoi(optarg); | ||
1426 | if (socket_timeout <= 0) | ||
1427 | return ERROR; | ||
1428 | } | ||
1429 | } | ||
1430 | |||
1431 | return OK; | ||
1432 | } | ||
1433 | |||
1434 | void print_help(void) { | ||
1435 | char *myport; | ||
1436 | xasprintf(&myport, "%d", PORT); | ||
1437 | |||
1438 | print_revision(progname, NP_VERSION); | ||
1439 | |||
1440 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | ||
1441 | printf(COPYRIGHT, copyright, email); | ||
1442 | |||
1443 | printf("%s\n", _("This plugin attempts to contact the MRTGEXT NLM running on a")); | ||
1444 | printf("%s\n", _("Novell server to gather the requested system information.")); | ||
1445 | |||
1446 | printf("\n\n"); | ||
1447 | |||
1448 | print_usage(); | ||
1449 | |||
1450 | printf(UT_HELP_VRSN); | ||
1451 | printf(UT_EXTRA_OPTS); | ||
1452 | |||
1453 | printf(UT_HOST_PORT, 'p', myport); | ||
1454 | |||
1455 | printf(" %s\n", "-v, --variable=STRING"); | ||
1456 | printf(" %s\n", _("Variable to check. Valid variables include:")); | ||
1457 | printf(" %s\n", _("LOAD1 = 1 minute average CPU load")); | ||
1458 | printf(" %s\n", _("LOAD5 = 5 minute average CPU load")); | ||
1459 | printf(" %s\n", _("LOAD15 = 15 minute average CPU load")); | ||
1460 | printf(" %s\n", _("CSPROCS = number of current service processes (NW 5.x only)")); | ||
1461 | printf(" %s\n", _("ABENDS = number of abended threads (NW 5.x only)")); | ||
1462 | printf(" %s\n", _("UPTIME = server uptime")); | ||
1463 | printf(" %s\n", _("LTCH = percent long term cache hits")); | ||
1464 | printf(" %s\n", _("CBUFF = current number of cache buffers")); | ||
1465 | printf(" %s\n", _("CDBUFF = current number of dirty cache buffers")); | ||
1466 | printf(" %s\n", _("DCB = dirty cache buffers as a percentage of the total")); | ||
1467 | printf(" %s\n", _("TCB = dirty cache buffers as a percentage of the original")); | ||
1468 | printf(" %s\n", _("OFILES = number of open files")); | ||
1469 | printf(" %s\n", _(" VMF<vol> = MB of free space on Volume <vol>")); | ||
1470 | printf(" %s\n", _(" VMU<vol> = MB used space on Volume <vol>")); | ||
1471 | printf(" %s\n", _(" VPU<vol> = percent used space on Volume <vol>")); | ||
1472 | printf(" %s\n", _(" VMP<vol> = MB of purgeable space on Volume <vol>")); | ||
1473 | printf(" %s\n", _(" VPF<vol> = percent free space on volume <vol>")); | ||
1474 | printf(" %s\n", _(" VKF<vol> = KB of free space on volume <vol>")); | ||
1475 | printf(" %s\n", _(" VPP<vol> = percent purgeable space on volume <vol>")); | ||
1476 | printf(" %s\n", _(" VKP<vol> = KB of purgeable space on volume <vol>")); | ||
1477 | printf(" %s\n", _(" VPNP<vol> = percent not yet purgeable space on volume <vol>")); | ||
1478 | printf(" %s\n", _(" VKNP<vol> = KB of not yet purgeable space on volume <vol>")); | ||
1479 | printf(" %s\n", _(" LRUM = LRU sitting time in minutes")); | ||
1480 | printf(" %s\n", _(" LRUS = LRU sitting time in seconds")); | ||
1481 | printf(" %s\n", _(" DSDB = check to see if DS Database is open")); | ||
1482 | printf(" %s\n", _(" DSVER = NDS version")); | ||
1483 | printf(" %s\n", _(" UPRB = used packet receive buffers")); | ||
1484 | printf(" %s\n", _(" PUPRB = percent (of max) used packet receive buffers")); | ||
1485 | printf(" %s\n", _(" SAPENTRIES = number of entries in the SAP table")); | ||
1486 | printf(" %s\n", _(" SAPENTRIES<n> = number of entries in the SAP table for SAP type <n>")); | ||
1487 | printf(" %s\n", _(" TSYNC = timesync status")); | ||
1488 | printf(" %s\n", _(" LOGINS = check to see if logins are enabled")); | ||
1489 | printf(" %s\n", _(" CONNS = number of currently licensed connections")); | ||
1490 | printf(" %s\n", _(" NRMH = NRM Summary Status")); | ||
1491 | printf(" %s\n", _(" NRMP<stat> = Returns the current value for a NRM health item")); | ||
1492 | printf(" %s\n", _(" NRMM<stat> = Returns the current memory stats from NRM")); | ||
1493 | printf(" %s\n", _(" NRMS<stat> = Returns the current Swapfile stats from NRM")); | ||
1494 | printf(" %s\n", _(" NSS1<stat> = Statistics from _Admin:Manage_NSS\\GeneralStats.xml")); | ||
1495 | printf(" %s\n", _(" NSS3<stat> = Statistics from _Admin:Manage_NSS\\NameCache.xml")); | ||
1496 | printf(" %s\n", _(" NSS4<stat> = Statistics from _Admin:Manage_NSS\\FileStats.xml")); | ||
1497 | printf(" %s\n", _(" NSS5<stat> = Statistics from _Admin:Manage_NSS\\ObjectCache.xml")); | ||
1498 | printf(" %s\n", _(" NSS6<stat> = Statistics from _Admin:Manage_NSS\\Thread.xml")); | ||
1499 | printf(" %s\n", _(" NSS7<stat> = Statistics from _Admin:Manage_NSS\\AuthorizationCache.xml")); | ||
1500 | printf(" %s\n", _(" NLM:<nlm> = check if NLM is loaded and report version")); | ||
1501 | printf(" %s\n", _(" (e.g. NLM:TSANDS.NLM)")); | ||
1502 | printf("\n"); | ||
1503 | printf(" %s\n", "-w, --warning=INTEGER"); | ||
1504 | printf(" %s\n", _("Threshold which will result in a warning status")); | ||
1505 | printf(" %s\n", "-c, --critical=INTEGER"); | ||
1506 | printf(" %s\n", _("Threshold which will result in a critical status")); | ||
1507 | printf(" %s\n", "-o, --osversion"); | ||
1508 | printf(" %s\n", _("Include server version string in results")); | ||
1509 | |||
1510 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | ||
1511 | |||
1512 | printf("\n"); | ||
1513 | printf("%s\n", _("Notes:")); | ||
1514 | printf(" %s\n", _("- This plugin requires that the MRTGEXT.NLM file from James Drews' MRTG")); | ||
1515 | printf(" %s\n", _(" extension for NetWare be loaded on the Novell servers you wish to check.")); | ||
1516 | printf(" %s\n", _(" (available from http://www.engr.wisc.edu/~drews/mrtg/)")); | ||
1517 | printf(" %s\n", _("- Values for critical thresholds should be lower than warning thresholds")); | ||
1518 | printf(" %s\n", _(" when the following variables are checked: VPF, VKF, LTCH, CBUFF, DCB, ")); | ||
1519 | printf(" %s\n", _(" TCB, LRUS and LRUM.")); | ||
1520 | |||
1521 | printf(UT_SUPPORT); | ||
1522 | } | ||
1523 | |||
1524 | void print_usage(void) { | ||
1525 | printf("%s\n", _("Usage:")); | ||
1526 | printf("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname); | ||
1527 | } | ||
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c index 84305adb..793a686f 100644 --- a/plugins/check_pgsql.c +++ b/plugins/check_pgsql.c | |||
@@ -46,14 +46,15 @@ const char *email = "devel@monitoring-plugins.org"; | |||
46 | #define DEFAULT_HOST "127.0.0.1" | 46 | #define DEFAULT_HOST "127.0.0.1" |
47 | 47 | ||
48 | /* return the PSQL server version as a 3-tuple */ | 48 | /* return the PSQL server version as a 3-tuple */ |
49 | #define PSQL_SERVER_VERSION3(server_version) \ | 49 | #define PSQL_SERVER_VERSION3(server_version) \ |
50 | (server_version) / 10000, (server_version) / 100 - (int)((server_version) / 10000) * 100, \ | 50 | (server_version) / 10000, (server_version) / 100 - (int)((server_version) / 10000) * 100, \ |
51 | (server_version) - (int)((server_version) / 100) * 100 | 51 | (server_version) - (int)((server_version) / 100) * 100 |
52 | /* return true if the given host is a UNIX domain socket */ | 52 | /* return true if the given host is a UNIX domain socket */ |
53 | #define PSQL_IS_UNIX_DOMAIN_SOCKET(host) ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host))) | 53 | #define PSQL_IS_UNIX_DOMAIN_SOCKET(host) ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host))) |
54 | /* return a 3-tuple identifying a host/port independent of the socket type */ | 54 | /* return a 3-tuple identifying a host/port independent of the socket type */ |
55 | #define PSQL_SOCKET3(host, port) \ | 55 | #define PSQL_SOCKET3(host, port) \ |
56 | ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, PSQL_IS_UNIX_DOMAIN_SOCKET(host) ? "/.s.PGSQL." : ":", port | 56 | ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, \ |
57 | PSQL_IS_UNIX_DOMAIN_SOCKET(host) ? "/.s.PGSQL." : ":", port | ||
57 | 58 | ||
58 | typedef struct { | 59 | typedef struct { |
59 | int errorcode; | 60 | int errorcode; |
@@ -63,8 +64,9 @@ static check_pgsql_config_wrapper process_arguments(int /*argc*/, char ** /*argv | |||
63 | 64 | ||
64 | static void print_help(void); | 65 | static void print_help(void); |
65 | static bool is_pg_logname(char * /*username*/); | 66 | static bool is_pg_logname(char * /*username*/); |
66 | static mp_state_enum do_query(PGconn * /*conn*/, char * /*query*/, const char /*pgqueryname*/[], thresholds * /*qthresholds*/, | 67 | static mp_state_enum do_query(PGconn * /*conn*/, char * /*query*/, const char /*pgqueryname*/[], |
67 | char * /*query_warning*/, char * /*query_critical*/); | 68 | thresholds * /*qthresholds*/, char * /*query_warning*/, |
69 | char * /*query_critical*/); | ||
68 | void print_usage(void); | 70 | void print_usage(void); |
69 | 71 | ||
70 | static int verbose = 0; | 72 | static int verbose = 0; |
@@ -167,7 +169,8 @@ int main(int argc, char **argv) { | |||
167 | } | 169 | } |
168 | 170 | ||
169 | if (verbose) { /* do not include password (see right below) in output */ | 171 | if (verbose) { /* do not include password (see right below) in output */ |
170 | printf("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, config.pgpasswd ? " password = <hidden>" : ""); | 172 | printf("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, |
173 | config.pgpasswd ? " password = <hidden>" : ""); | ||
171 | } | 174 | } |
172 | 175 | ||
173 | if (config.pgpasswd) { | 176 | if (config.pgpasswd) { |
@@ -185,8 +188,8 @@ int main(int argc, char **argv) { | |||
185 | --end_timeval.tv_sec; | 188 | --end_timeval.tv_sec; |
186 | end_timeval.tv_usec += 1000000; | 189 | end_timeval.tv_usec += 1000000; |
187 | } | 190 | } |
188 | double elapsed_time = | 191 | double elapsed_time = (double)(end_timeval.tv_sec - start_timeval.tv_sec) + |
189 | (double)(end_timeval.tv_sec - start_timeval.tv_sec) + ((double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0); | 192 | ((double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0); |
190 | 193 | ||
191 | if (verbose) { | 194 | if (verbose) { |
192 | printf("Time elapsed: %f\n", elapsed_time); | 195 | printf("Time elapsed: %f\n", elapsed_time); |
@@ -218,16 +221,18 @@ int main(int argc, char **argv) { | |||
218 | printf("Successfully connected to database %s (user %s) " | 221 | printf("Successfully connected to database %s (user %s) " |
219 | "at server %s%s%s (server version: %d.%d.%d, " | 222 | "at server %s%s%s (server version: %d.%d.%d, " |
220 | "protocol version: %d, pid: %d)\n", | 223 | "protocol version: %d, pid: %d)\n", |
221 | PQdb(conn), PQuser(conn), PSQL_SOCKET3(server_host, PQport(conn)), PSQL_SERVER_VERSION3(server_version), | 224 | PQdb(conn), PQuser(conn), PSQL_SOCKET3(server_host, PQport(conn)), |
222 | PQprotocolVersion(conn), PQbackendPID(conn)); | 225 | PSQL_SERVER_VERSION3(server_version), PQprotocolVersion(conn), PQbackendPID(conn)); |
223 | } | 226 | } |
224 | 227 | ||
225 | printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), config.dbName, elapsed_time, | 228 | printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), config.dbName, elapsed_time, |
226 | fperfdata("time", elapsed_time, "s", (config.twarn > 0.0), config.twarn, (config.tcrit > 0.0), config.tcrit, true, 0, false, 0)); | 229 | fperfdata("time", elapsed_time, "s", (config.twarn > 0.0), config.twarn, |
230 | (config.tcrit > 0.0), config.tcrit, true, 0, false, 0)); | ||
227 | 231 | ||
228 | mp_state_enum query_status = STATE_UNKNOWN; | 232 | mp_state_enum query_status = STATE_UNKNOWN; |
229 | if (config.pgquery) { | 233 | if (config.pgquery) { |
230 | query_status = do_query(conn, config.pgquery, config.pgqueryname, config.qthresholds, config.query_warning, config.query_critical); | 234 | query_status = do_query(conn, config.pgquery, config.pgqueryname, config.qthresholds, |
235 | config.query_warning, config.query_critical); | ||
231 | } | 236 | } |
232 | 237 | ||
233 | if (verbose) { | 238 | if (verbose) { |
@@ -265,7 +270,8 @@ check_pgsql_config_wrapper process_arguments(int argc, char **argv) { | |||
265 | 270 | ||
266 | while (true) { | 271 | while (true) { |
267 | int option = 0; | 272 | int option = 0; |
268 | int option_char = getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option); | 273 | int option_char = |
274 | getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option); | ||
269 | 275 | ||
270 | if (option_char == EOF) { | 276 | if (option_char == EOF) { |
271 | break; | 277 | break; |
@@ -357,7 +363,8 @@ check_pgsql_config_wrapper process_arguments(int argc, char **argv) { | |||
357 | } | 363 | } |
358 | } | 364 | } |
359 | 365 | ||
360 | set_thresholds(&result.config.qthresholds, result.config.query_warning, result.config.query_critical); | 366 | set_thresholds(&result.config.qthresholds, result.config.query_warning, |
367 | result.config.query_critical); | ||
361 | 368 | ||
362 | return result; | 369 | return result; |
363 | } | 370 | } |
@@ -457,29 +464,39 @@ void print_help(void) { | |||
457 | 464 | ||
458 | printf(" %s\n", _("If a query is specified using the -q option, it will be executed after")); | 465 | printf(" %s\n", _("If a query is specified using the -q option, it will be executed after")); |
459 | printf(" %s\n", _("connecting to the server. The result from the query has to be numeric.")); | 466 | printf(" %s\n", _("connecting to the server. The result from the query has to be numeric.")); |
460 | printf(" %s\n", _("Multiple SQL commands, separated by semicolon, are allowed but the result ")); | 467 | printf(" %s\n", |
468 | _("Multiple SQL commands, separated by semicolon, are allowed but the result ")); | ||
461 | printf(" %s\n", _("of the last command is taken into account only. The value of the first")); | 469 | printf(" %s\n", _("of the last command is taken into account only. The value of the first")); |
462 | printf(" %s\n", _("column in the first row is used as the check result. If a second column is")); | 470 | printf(" %s\n", |
471 | _("column in the first row is used as the check result. If a second column is")); | ||
463 | printf(" %s\n", _("present in the result set, this is added to the plugin output with a")); | 472 | printf(" %s\n", _("present in the result set, this is added to the plugin output with a")); |
464 | printf(" %s\n", _("prefix of \"Extra Info:\". This information can be displayed in the system")); | 473 | printf(" %s\n", |
474 | _("prefix of \"Extra Info:\". This information can be displayed in the system")); | ||
465 | printf(" %s\n\n", _("executing the plugin.")); | 475 | printf(" %s\n\n", _("executing the plugin.")); |
466 | 476 | ||
467 | printf(" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual")); | 477 | printf(" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual")); |
468 | printf(" %s\n\n", _("for details about how to access internal statistics of the database server.")); | 478 | printf(" %s\n\n", |
479 | _("for details about how to access internal statistics of the database server.")); | ||
469 | 480 | ||
470 | printf(" %s\n", _("For a list of available connection parameters which may be used with the -o")); | 481 | printf(" %s\n", |
471 | printf(" %s\n", _("command line option, see the documentation for PQconnectdb() in the chapter")); | 482 | _("For a list of available connection parameters which may be used with the -o")); |
483 | printf(" %s\n", | ||
484 | _("command line option, see the documentation for PQconnectdb() in the chapter")); | ||
472 | printf(" %s\n", _("\"libpq - C Library\" of the PostgreSQL manual. For example, this may be")); | 485 | printf(" %s\n", _("\"libpq - C Library\" of the PostgreSQL manual. For example, this may be")); |
473 | printf(" %s\n", _("used to specify a service name in pg_service.conf to be used for additional")); | 486 | printf(" %s\n", |
487 | _("used to specify a service name in pg_service.conf to be used for additional")); | ||
474 | printf(" %s\n", _("connection parameters: -o 'service=<name>' or to specify the SSL mode:")); | 488 | printf(" %s\n", _("connection parameters: -o 'service=<name>' or to specify the SSL mode:")); |
475 | printf(" %s\n\n", _("-o 'sslmode=require'.")); | 489 | printf(" %s\n\n", _("-o 'sslmode=require'.")); |
476 | 490 | ||
477 | printf(" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To")); | 491 | printf(" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To")); |
478 | printf(" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP")); | 492 | printf(" %s\n", |
493 | _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP")); | ||
479 | printf(" %s\n\n", _("connections (start the postmaster with the -i option).")); | 494 | printf(" %s\n\n", _("connections (start the postmaster with the -i option).")); |
480 | 495 | ||
481 | printf(" %s\n", _("Typically, the monitoring user (unless the --logname option is used) should be")); | 496 | printf(" %s\n", |
482 | printf(" %s\n", _("able to connect to the database without a password. The plugin can also send")); | 497 | _("Typically, the monitoring user (unless the --logname option is used) should be")); |
498 | printf(" %s\n", | ||
499 | _("able to connect to the database without a password. The plugin can also send")); | ||
483 | printf(" %s\n", _("a password, but no effort is made to obscure or encrypt the password.")); | 500 | printf(" %s\n", _("a password, but no effort is made to obscure or encrypt the password.")); |
484 | 501 | ||
485 | printf(UT_SUPPORT); | 502 | printf(UT_SUPPORT); |
@@ -492,15 +509,16 @@ void print_usage(void) { | |||
492 | "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n"); | 509 | "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n"); |
493 | } | 510 | } |
494 | 511 | ||
495 | mp_state_enum do_query(PGconn *conn, char *query, const char pgqueryname[], thresholds *qthresholds, char *query_warning, | 512 | mp_state_enum do_query(PGconn *conn, char *query, const char pgqueryname[], thresholds *qthresholds, |
496 | char *query_critical) { | 513 | char *query_warning, char *query_critical) { |
497 | if (verbose) { | 514 | if (verbose) { |
498 | printf("Executing SQL query \"%s\".\n", query); | 515 | printf("Executing SQL query \"%s\".\n", query); |
499 | } | 516 | } |
500 | PGresult *res = PQexec(conn, query); | 517 | PGresult *res = PQexec(conn, query); |
501 | 518 | ||
502 | if (PGRES_TUPLES_OK != PQresultStatus(res)) { | 519 | if (PGRES_TUPLES_OK != PQresultStatus(res)) { |
503 | printf(_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"), PQerrorMessage(conn)); | 520 | printf(_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"), |
521 | PQerrorMessage(conn)); | ||
504 | return STATE_CRITICAL; | 522 | return STATE_CRITICAL; |
505 | } | 523 | } |
506 | 524 | ||
@@ -548,7 +566,8 @@ mp_state_enum do_query(PGconn *conn, char *query, const char pgqueryname[], thre | |||
548 | printf(_("'%s' returned %f"), query, value); | 566 | printf(_("'%s' returned %f"), query, value); |
549 | } | 567 | } |
550 | 568 | ||
551 | printf("|query=%f;%s;%s;;\n", value, query_warning ? query_warning : "", query_critical ? query_critical : ""); | 569 | printf("|query=%f;%s;%s;;\n", value, query_warning ? query_warning : "", |
570 | query_critical ? query_critical : ""); | ||
552 | if (PQnfields(res) > 1) { | 571 | if (PQnfields(res) > 1) { |
553 | char *extra_info = PQgetvalue(res, 0, 1); | 572 | char *extra_info = PQgetvalue(res, 0, 1); |
554 | if (extra_info != NULL) { | 573 | if (extra_info != NULL) { |
diff --git a/plugins/check_ping.c b/plugins/check_ping.c index 940b9475..61feb958 100644 --- a/plugins/check_ping.c +++ b/plugins/check_ping.c | |||
@@ -116,10 +116,10 @@ int main(int argc, char **argv) { | |||
116 | # ifdef PING_HAS_TIMEOUT | 116 | # ifdef PING_HAS_TIMEOUT |
117 | xasprintf(&cmd, rawcmd, timeout_interval, config.max_packets, config.addresses[i]); | 117 | xasprintf(&cmd, rawcmd, timeout_interval, config.max_packets, config.addresses[i]); |
118 | # else | 118 | # else |
119 | xasprintf(&cmd, rawcmd, config.max_packets, addresses[i]); | 119 | xasprintf(&cmd, rawcmd, config.max_packets, config.addresses[i]); |
120 | # endif | 120 | # endif |
121 | #else | 121 | #else |
122 | xasprintf(&cmd, rawcmd, addresses[i], config.max_packets); | 122 | xasprintf(&cmd, rawcmd, config.addresses[i], config.max_packets); |
123 | #endif | 123 | #endif |
124 | 124 | ||
125 | if (verbose >= 2) { | 125 | if (verbose >= 2) { |
@@ -135,7 +135,8 @@ int main(int argc, char **argv) { | |||
135 | die(STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n")); | 135 | die(STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n")); |
136 | } | 136 | } |
137 | 137 | ||
138 | if (pinged.packet_loss >= config.cpl || pinged.round_trip_average >= config.crta || pinged.round_trip_average < 0) { | 138 | if (pinged.packet_loss >= config.cpl || pinged.round_trip_average >= config.crta || |
139 | pinged.round_trip_average < 0) { | ||
139 | pinged.state = STATE_CRITICAL; | 140 | pinged.state = STATE_CRITICAL; |
140 | } else if (pinged.packet_loss >= config.wpl || pinged.round_trip_average >= config.wrta) { | 141 | } else if (pinged.packet_loss >= config.wpl || pinged.round_trip_average >= config.wrta) { |
141 | pinged.state = STATE_WARNING; | 142 | pinged.state = STATE_WARNING; |
@@ -151,10 +152,11 @@ int main(int argc, char **argv) { | |||
151 | printf("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, config.addresses[i]); | 152 | printf("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, config.addresses[i]); |
152 | } | 153 | } |
153 | if (pinged.packet_loss == 100) { | 154 | if (pinged.packet_loss == 100) { |
154 | printf(_("PING %s - %sPacket loss = %d%%"), state_text(pinged.state), warn_text, pinged.packet_loss); | 155 | printf(_("PING %s - %sPacket loss = %d%%"), state_text(pinged.state), warn_text, |
156 | pinged.packet_loss); | ||
155 | } else { | 157 | } else { |
156 | printf(_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text(pinged.state), warn_text, pinged.packet_loss, | 158 | printf(_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text(pinged.state), |
157 | pinged.round_trip_average); | 159 | warn_text, pinged.packet_loss, pinged.round_trip_average); |
158 | } | 160 | } |
159 | if (config.display_html) { | 161 | if (config.display_html) { |
160 | printf("</A>"); | 162 | printf("</A>"); |
@@ -162,14 +164,16 @@ int main(int argc, char **argv) { | |||
162 | 164 | ||
163 | /* Print performance data */ | 165 | /* Print performance data */ |
164 | if (pinged.packet_loss != 100) { | 166 | if (pinged.packet_loss != 100) { |
165 | printf("|%s", fperfdata("rta", pinged.round_trip_average, "ms", (bool)(config.wrta > 0), config.wrta, (bool)(config.crta > 0), | 167 | printf("|%s", |
166 | config.crta, true, 0, false, 0)); | 168 | fperfdata("rta", pinged.round_trip_average, "ms", (bool)(config.wrta > 0), |
169 | config.wrta, (bool)(config.crta > 0), config.crta, true, 0, false, 0)); | ||
167 | } else { | 170 | } else { |
168 | printf("| rta=U;%f;%f;;", config.wrta, config.crta); | 171 | printf("| rta=U;%f;%f;;", config.wrta, config.crta); |
169 | } | 172 | } |
170 | 173 | ||
171 | printf(" %s\n", perfdata("pl", (long)pinged.packet_loss, "%", (bool)(config.wpl > 0), config.wpl, (bool)(config.cpl > 0), | 174 | printf(" %s\n", |
172 | config.cpl, true, 0, false, 0)); | 175 | perfdata("pl", (long)pinged.packet_loss, "%", (bool)(config.wpl > 0), config.wpl, |
176 | (bool)(config.cpl > 0), config.cpl, true, 0, false, 0)); | ||
173 | 177 | ||
174 | if (verbose >= 2) { | 178 | if (verbose >= 2) { |
175 | printf("%f:%d%% %f:%d%%\n", config.wrta, config.wpl, config.crta, config.cpl); | 179 | printf("%f:%d%% %f:%d%%\n", config.wrta, config.wpl, config.crta, config.cpl); |
@@ -254,7 +258,8 @@ check_ping_config_wrapper process_arguments(int argc, char **argv) { | |||
254 | result.config.n_addresses++; | 258 | result.config.n_addresses++; |
255 | if (result.config.n_addresses > max_addr) { | 259 | if (result.config.n_addresses > max_addr) { |
256 | max_addr *= 2; | 260 | max_addr *= 2; |
257 | result.config.addresses = realloc(result.config.addresses, sizeof(char *) * max_addr); | 261 | result.config.addresses = |
262 | realloc(result.config.addresses, sizeof(char *) * max_addr); | ||
258 | if (result.config.addresses == NULL) { | 263 | if (result.config.addresses == NULL) { |
259 | die(STATE_UNKNOWN, _("Could not realloc() addresses\n")); | 264 | die(STATE_UNKNOWN, _("Could not realloc() addresses\n")); |
260 | } | 265 | } |
@@ -411,13 +416,15 @@ check_ping_config_wrapper validate_arguments(check_ping_config_wrapper config_wr | |||
411 | } | 416 | } |
412 | 417 | ||
413 | if (config_wrapper.config.wrta > config_wrapper.config.crta) { | 418 | if (config_wrapper.config.wrta > config_wrapper.config.crta) { |
414 | printf(_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), config_wrapper.config.wrta, config_wrapper.config.crta); | 419 | printf(_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), config_wrapper.config.wrta, |
420 | config_wrapper.config.crta); | ||
415 | config_wrapper.errorcode = ERROR; | 421 | config_wrapper.errorcode = ERROR; |
416 | return config_wrapper; | 422 | return config_wrapper; |
417 | } | 423 | } |
418 | 424 | ||
419 | if (config_wrapper.config.wpl > config_wrapper.config.cpl) { | 425 | if (config_wrapper.config.wpl > config_wrapper.config.cpl) { |
420 | printf(_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), config_wrapper.config.wpl, config_wrapper.config.cpl); | 426 | printf(_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), config_wrapper.config.wpl, |
427 | config_wrapper.config.cpl); | ||
421 | config_wrapper.errorcode = ERROR; | 428 | config_wrapper.errorcode = ERROR; |
422 | return config_wrapper; | 429 | return config_wrapper; |
423 | } | 430 | } |
@@ -426,7 +433,8 @@ check_ping_config_wrapper validate_arguments(check_ping_config_wrapper config_wr | |||
426 | config_wrapper.config.max_packets = DEFAULT_MAX_PACKETS; | 433 | config_wrapper.config.max_packets = DEFAULT_MAX_PACKETS; |
427 | } | 434 | } |
428 | 435 | ||
429 | double max_seconds = (config_wrapper.config.crta / 1000.0 * config_wrapper.config.max_packets) + config_wrapper.config.max_packets; | 436 | double max_seconds = (config_wrapper.config.crta / 1000.0 * config_wrapper.config.max_packets) + |
437 | config_wrapper.config.max_packets; | ||
430 | if (max_seconds > timeout_interval) { | 438 | if (max_seconds > timeout_interval) { |
431 | timeout_interval = (unsigned int)max_seconds; | 439 | timeout_interval = (unsigned int)max_seconds; |
432 | } | 440 | } |
@@ -470,37 +478,70 @@ ping_result run_ping(const char *cmd, const char *addr, double crta) { | |||
470 | 478 | ||
471 | /* get the percent loss statistics */ | 479 | /* get the percent loss statistics */ |
472 | int match = 0; | 480 | int match = 0; |
473 | if ((sscanf(buf, "%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) == | 481 | if ((sscanf( |
474 | 1 && | 482 | buf, |
483 | "%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss%n", | ||
484 | &result.packet_loss, &match) == 1 && | ||
475 | match) || | 485 | match) || |
476 | (sscanf(buf, "%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss%n", &result.packet_loss, | 486 | (sscanf(buf, |
477 | &match) == 1 && | 487 | "%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet " |
488 | "loss%n", | ||
489 | &result.packet_loss, &match) == 1 && | ||
490 | match) || | ||
491 | (sscanf(buf, | ||
492 | "%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss%n", | ||
493 | &result.packet_loss, &match) == 1 && | ||
494 | match) || | ||
495 | (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% packet loss%n", | ||
496 | &result.packet_loss, &match) == 1 && | ||
497 | match) || | ||
498 | (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% loss, time%n", | ||
499 | &result.packet_loss, &match) == 1 && | ||
478 | match) || | 500 | match) || |
479 | (sscanf(buf, "%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss%n", &result.packet_loss, &match) == 1 && | 501 | (sscanf(buf, "%*d packets transmitted, %*d received, %d%% loss, time%n", |
502 | &result.packet_loss, &match) == 1 && | ||
480 | match) || | 503 | match) || |
481 | (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% packet loss%n", &result.packet_loss, &match) == 1 && match) || | 504 | (sscanf(buf, "%*d packets transmitted, %*d received, %d%% packet loss, time%n", |
482 | (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% loss, time%n", &result.packet_loss, &match) == 1 && match) || | 505 | &result.packet_loss, &match) == 1 && |
483 | (sscanf(buf, "%*d packets transmitted, %*d received, %d%% loss, time%n", &result.packet_loss, &match) == 1 && match) || | 506 | match) == 1 || |
484 | (sscanf(buf, "%*d packets transmitted, %*d received, %d%% packet loss, time%n", &result.packet_loss, &match) == 1 && match) == | 507 | (sscanf(buf, "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n", |
485 | 1 || | 508 | &result.packet_loss, &match) == 1 && |
486 | (sscanf(buf, "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) == 1 && | ||
487 | match) || | 509 | match) || |
488 | (sscanf(buf, "%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) == 1 && | 510 | (sscanf(buf, "%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n", |
511 | &result.packet_loss, &match) == 1 && | ||
489 | match) || | 512 | match) || |
490 | (sscanf(buf, "%*[^(](%d%% %*[^)])%n", &result.packet_loss, &match) == 1 && match)) { | 513 | (sscanf(buf, "%*[^(](%d%% %*[^)])%n", &result.packet_loss, &match) == 1 && match)) { |
491 | continue; | 514 | continue; |
492 | } | 515 | } |
493 | 516 | ||
494 | /* get the round trip average */ | 517 | /* get the round trip average */ |
495 | if ((sscanf(buf, "round-trip min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average, &match) == 1 && match) || | 518 | if ((sscanf(buf, "round-trip min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average, |
496 | (sscanf(buf, "round-trip min/avg/max/mdev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || | 519 | &match) == 1 && |
497 | (sscanf(buf, "round-trip min/avg/max/sdev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || | 520 | match) || |
498 | (sscanf(buf, "round-trip min/avg/max/stddev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || | 521 | (sscanf(buf, "round-trip min/avg/max/mdev = %*f/%lf/%*f/%*f%n", |
499 | (sscanf(buf, "round-trip min/avg/max/std-dev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || | 522 | &result.round_trip_average, &match) == 1 && |
500 | (sscanf(buf, "round-trip (ms) min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average, &match) == 1 && match) || | 523 | match) || |
501 | (sscanf(buf, "round-trip (ms) min/avg/max/stddev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || | 524 | (sscanf(buf, "round-trip min/avg/max/sdev = %*f/%lf/%*f/%*f%n", |
502 | (sscanf(buf, "rtt min/avg/max/mdev = %*f/%lf/%*f/%*f ms%n", &result.round_trip_average, &match) == 1 && match) || | 525 | &result.round_trip_average, &match) == 1 && |
503 | (sscanf(buf, "%*[^=] = %*fms, %*[^=] = %*fms, %*[^=] = %lfms%n", &result.round_trip_average, &match) == 1 && match)) { | 526 | match) || |
527 | (sscanf(buf, "round-trip min/avg/max/stddev = %*f/%lf/%*f/%*f%n", | ||
528 | &result.round_trip_average, &match) == 1 && | ||
529 | match) || | ||
530 | (sscanf(buf, "round-trip min/avg/max/std-dev = %*f/%lf/%*f/%*f%n", | ||
531 | &result.round_trip_average, &match) == 1 && | ||
532 | match) || | ||
533 | (sscanf(buf, "round-trip (ms) min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average, | ||
534 | &match) == 1 && | ||
535 | match) || | ||
536 | (sscanf(buf, "round-trip (ms) min/avg/max/stddev = %*f/%lf/%*f/%*f%n", | ||
537 | &result.round_trip_average, &match) == 1 && | ||
538 | match) || | ||
539 | (sscanf(buf, "rtt min/avg/max/mdev = %*f/%lf/%*f/%*f ms%n", &result.round_trip_average, | ||
540 | &match) == 1 && | ||
541 | match) || | ||
542 | (sscanf(buf, "%*[^=] = %*fms, %*[^=] = %*fms, %*[^=] = %lfms%n", | ||
543 | &result.round_trip_average, &match) == 1 && | ||
544 | match)) { | ||
504 | continue; | 545 | continue; |
505 | } | 546 | } |
506 | } | 547 | } |
@@ -513,7 +554,8 @@ ping_result run_ping(const char *cmd, const char *addr, double crta) { | |||
513 | /* check stderr, setting at least WARNING if there is output here */ | 554 | /* check stderr, setting at least WARNING if there is output here */ |
514 | /* Add warning into warn_text */ | 555 | /* Add warning into warn_text */ |
515 | while (fgets(buf, MAX_INPUT_BUFFER - 1, child_stderr)) { | 556 | while (fgets(buf, MAX_INPUT_BUFFER - 1, child_stderr)) { |
516 | if (!strstr(buf, "WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP") && !strstr(buf, "Warning: time of day goes back") | 557 | if (!strstr(buf, "WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP") && |
558 | !strstr(buf, "Warning: time of day goes back") | ||
517 | 559 | ||
518 | ) { | 560 | ) { |
519 | if (verbose >= 3) { | 561 | if (verbose >= 3) { |
@@ -524,7 +566,8 @@ ping_result run_ping(const char *cmd, const char *addr, double crta) { | |||
524 | if (warn_text == NULL) { | 566 | if (warn_text == NULL) { |
525 | warn_text = strdup(_("System call sent warnings to stderr ")); | 567 | warn_text = strdup(_("System call sent warnings to stderr ")); |
526 | } else { | 568 | } else { |
527 | xasprintf(&warn_text, "%s %s", warn_text, _("System call sent warnings to stderr ")); | 569 | xasprintf(&warn_text, "%s %s", warn_text, |
570 | _("System call sent warnings to stderr ")); | ||
528 | } | 571 | } |
529 | } | 572 | } |
530 | } | 573 | } |
@@ -542,7 +585,8 @@ ping_result run_ping(const char *cmd, const char *addr, double crta) { | |||
542 | } | 585 | } |
543 | 586 | ||
544 | mp_state_enum error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) { | 587 | mp_state_enum error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) { |
545 | if (strstr(buf, "Network is unreachable") || strstr(buf, "Destination Net Unreachable") || strstr(buf, "No route")) { | 588 | if (strstr(buf, "Network is unreachable") || strstr(buf, "Destination Net Unreachable") || |
589 | strstr(buf, "No route")) { | ||
546 | die(STATE_CRITICAL, _("CRITICAL - Network Unreachable (%s)\n"), addr); | 590 | die(STATE_CRITICAL, _("CRITICAL - Network Unreachable (%s)\n"), addr); |
547 | } else if (strstr(buf, "Destination Host Unreachable") || strstr(buf, "Address unreachable")) { | 591 | } else if (strstr(buf, "Destination Host Unreachable") || strstr(buf, "Address unreachable")) { |
548 | die(STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)\n"), addr); | 592 | die(STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)\n"), addr); |
@@ -567,7 +611,8 @@ mp_state_enum error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) { | |||
567 | if (strstr(buf, "(DUP!)") || strstr(buf, "DUPLICATES FOUND")) { | 611 | if (strstr(buf, "(DUP!)") || strstr(buf, "DUPLICATES FOUND")) { |
568 | if (warn_text == NULL) { | 612 | if (warn_text == NULL) { |
569 | warn_text = strdup(_(WARN_DUPLICATES)); | 613 | warn_text = strdup(_(WARN_DUPLICATES)); |
570 | } else if (!strstr(warn_text, _(WARN_DUPLICATES)) && xasprintf(&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1) { | 614 | } else if (!strstr(warn_text, _(WARN_DUPLICATES)) && |
615 | xasprintf(&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1) { | ||
571 | die(STATE_UNKNOWN, _("Unable to realloc warn_text\n")); | 616 | die(STATE_UNKNOWN, _("Unable to realloc warn_text\n")); |
572 | } | 617 | } |
573 | return STATE_WARNING; | 618 | return STATE_WARNING; |
@@ -613,8 +658,10 @@ void print_help(void) { | |||
613 | printf("%s\n", _("percentage of packet loss to trigger an alarm state.")); | 658 | printf("%s\n", _("percentage of packet loss to trigger an alarm state.")); |
614 | 659 | ||
615 | printf("\n"); | 660 | printf("\n"); |
616 | printf("%s\n", _("This plugin uses the ping command to probe the specified host for packet loss")); | 661 | printf("%s\n", |
617 | printf("%s\n", _("(percentage) and round trip average (milliseconds). It can produce HTML output.")); | 662 | _("This plugin uses the ping command to probe the specified host for packet loss")); |
663 | printf("%s\n", | ||
664 | _("(percentage) and round trip average (milliseconds). It can produce HTML output.")); | ||
618 | 665 | ||
619 | printf(UT_SUPPORT); | 666 | printf(UT_SUPPORT); |
620 | } | 667 | } |
diff --git a/plugins/check_procs.c b/plugins/check_procs.c index 1d78ccee..ae6e9c23 100644 --- a/plugins/check_procs.c +++ b/plugins/check_procs.c | |||
@@ -1,41 +1,41 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_procs plugin | 3 | * Monitoring check_procs plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team | 6 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains the check_procs plugin | 10 | * This file contains the check_procs plugin |
11 | * | 11 | * |
12 | * Checks all processes and generates WARNING or CRITICAL states if the | 12 | * Checks all processes and generates WARNING or CRITICAL states if the |
13 | * specified metric is outside the required threshold ranges. The metric | 13 | * specified metric is outside the required threshold ranges. The metric |
14 | * defaults to number of processes. Search filters can be applied to limit | 14 | * defaults to number of processes. Search filters can be applied to limit |
15 | * the processes to check. | 15 | * the processes to check. |
16 | * | 16 | * |
17 | * The parent process, check_procs itself and any child process of | 17 | * The parent process, check_procs itself and any child process of |
18 | * check_procs (ps) are excluded from any checks to prevent false positives. | 18 | * check_procs (ps) are excluded from any checks to prevent false positives. |
19 | * | 19 | * |
20 | * | 20 | * |
21 | * This program is free software: you can redistribute it and/or modify | 21 | * This program is free software: you can redistribute it and/or modify |
22 | * it under the terms of the GNU General Public License as published by | 22 | * it under the terms of the GNU General Public License as published by |
23 | * the Free Software Foundation, either version 3 of the License, or | 23 | * the Free Software Foundation, either version 3 of the License, or |
24 | * (at your option) any later version. | 24 | * (at your option) any later version. |
25 | * | 25 | * |
26 | * This program is distributed in the hope that it will be useful, | 26 | * This program is distributed in the hope that it will be useful, |
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
29 | * GNU General Public License for more details. | 29 | * GNU General Public License for more details. |
30 | * | 30 | * |
31 | * You should have received a copy of the GNU General Public License | 31 | * You should have received a copy of the GNU General Public License |
32 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 32 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
33 | * | 33 | * |
34 | * | 34 | * |
35 | *****************************************************************************/ | 35 | *****************************************************************************/ |
36 | 36 | ||
37 | const char *progname = "check_procs"; | 37 | const char *progname = "check_procs"; |
38 | const char *program_name = "check_procs"; /* Required for coreutils libs */ | 38 | const char *program_name = "check_procs"; /* Required for coreutils libs */ |
39 | const char *copyright = "2000-2024"; | 39 | const char *copyright = "2000-2024"; |
40 | const char *email = "devel@monitoring-plugins.org"; | 40 | const char *email = "devel@monitoring-plugins.org"; |
41 | 41 | ||
@@ -43,313 +43,297 @@ const char *email = "devel@monitoring-plugins.org"; | |||
43 | #include "utils.h" | 43 | #include "utils.h" |
44 | #include "utils_cmd.h" | 44 | #include "utils_cmd.h" |
45 | #include "regex.h" | 45 | #include "regex.h" |
46 | #include "states.h" | ||
47 | #include "check_procs.d/config.h" | ||
46 | 48 | ||
47 | #include <pwd.h> | 49 | #include <pwd.h> |
48 | #include <errno.h> | 50 | #include <errno.h> |
49 | 51 | ||
50 | #ifdef HAVE_SYS_STAT_H | 52 | #ifdef HAVE_SYS_STAT_H |
51 | #include <sys/stat.h> | 53 | # include <sys/stat.h> |
52 | #endif | 54 | #endif |
53 | 55 | ||
54 | static int process_arguments (int /*argc*/, char ** /*argv*/); | 56 | typedef struct { |
55 | static int validate_arguments (void); | 57 | int errorcode; |
56 | static int convert_to_seconds (char * /*etime*/); | 58 | check_procs_config config; |
57 | static void print_help (void); | 59 | } check_procs_config_wrapper; |
58 | void print_usage (void); | 60 | static check_procs_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
59 | 61 | static check_procs_config_wrapper validate_arguments(check_procs_config_wrapper /*config_wrapper*/); | |
60 | static char *warning_range = NULL; | 62 | |
61 | static char *critical_range = NULL; | 63 | static int convert_to_seconds(char * /*etime*/, enum metric /*metric*/); |
62 | static thresholds *procs_thresholds = NULL; | 64 | static void print_help(void); |
63 | 65 | void print_usage(void); | |
64 | static int options = 0; /* bitmask of filter criteria to test against */ | 66 | |
65 | #define ALL 1 | 67 | #define ALL 1 |
66 | #define STAT 2 | 68 | #define STAT 2 |
67 | #define PPID 4 | 69 | #define PPID 4 |
68 | #define USER 8 | 70 | #define USER 8 |
69 | #define PROG 16 | 71 | #define PROG 16 |
70 | #define ARGS 32 | 72 | #define ARGS 32 |
71 | #define VSZ 64 | 73 | #define VSZ 64 |
72 | #define RSS 128 | 74 | #define RSS 128 |
73 | #define PCPU 256 | 75 | #define PCPU 256 |
74 | #define ELAPSED 512 | 76 | #define ELAPSED 512 |
75 | #define EREG_ARGS 1024 | 77 | #define EREG_ARGS 1024 |
76 | #define EXCLUDE_PROGS 2048 | 78 | #define EXCLUDE_PROGS 2048 |
77 | 79 | ||
78 | #define KTHREAD_PARENT "kthreadd" /* the parent process of kernel threads: | 80 | #define KTHREAD_PARENT \ |
79 | ppid of procs are compared to pid of this proc*/ | 81 | "kthreadd" /* the parent process of kernel threads: \ |
80 | 82 | ppid of procs are compared to pid of this proc*/ | |
81 | /* Different metrics */ | ||
82 | char *metric_name; | ||
83 | enum metric { | ||
84 | METRIC_PROCS, | ||
85 | METRIC_VSZ, | ||
86 | METRIC_RSS, | ||
87 | METRIC_CPU, | ||
88 | METRIC_ELAPSED | ||
89 | }; | ||
90 | enum metric metric = METRIC_PROCS; | ||
91 | 83 | ||
92 | static int verbose = 0; | 84 | static int verbose = 0; |
93 | static int uid; | 85 | |
94 | static pid_t ppid; | 86 | static int stat_exe(const pid_t pid, struct stat *buf) { |
95 | static int vsz; | ||
96 | static int rss; | ||
97 | static float pcpu; | ||
98 | static char *statopts; | ||
99 | static char *prog; | ||
100 | static char *exclude_progs; | ||
101 | static char **exclude_progs_arr = NULL; | ||
102 | static char exclude_progs_counter = 0; | ||
103 | static char *args; | ||
104 | static char *input_filename = NULL; | ||
105 | static regex_t re_args; | ||
106 | static char *fmt; | ||
107 | static char *fails; | ||
108 | static char tmp[MAX_INPUT_BUFFER]; | ||
109 | static int kthread_filter = 0; | ||
110 | static int usepid = 0; /* whether to test for pid or /proc/pid/exe */ | ||
111 | |||
112 | static int | ||
113 | stat_exe (const pid_t pid, struct stat *buf) { | ||
114 | char *path; | 87 | char *path; |
115 | int ret; | ||
116 | xasprintf(&path, "/proc/%d/exe", pid); | 88 | xasprintf(&path, "/proc/%d/exe", pid); |
117 | ret = stat(path, buf); | 89 | int ret = stat(path, buf); |
118 | free(path); | 90 | free(path); |
119 | return ret; | 91 | return ret; |
120 | } | 92 | } |
121 | 93 | ||
122 | 94 | int main(int argc, char **argv) { | |
123 | int | 95 | setlocale(LC_ALL, ""); |
124 | main (int argc, char **argv) | ||
125 | { | ||
126 | char *input_buffer; | ||
127 | char *input_line; | ||
128 | char *procprog; | ||
129 | |||
130 | pid_t mypid = 0; | ||
131 | pid_t myppid = 0; | ||
132 | struct stat statbuf; | ||
133 | dev_t mydev = 0; | ||
134 | ino_t myino = 0; | ||
135 | int procuid = 0; | ||
136 | pid_t procpid = 0; | ||
137 | pid_t procppid = 0; | ||
138 | pid_t kthread_ppid = 0; | ||
139 | int procvsz = 0; | ||
140 | int procrss = 0; | ||
141 | int procseconds = 0; | ||
142 | float procpcpu = 0; | ||
143 | char procstat[8]; | ||
144 | char procetime[MAX_INPUT_BUFFER] = { '\0' }; | ||
145 | char *procargs; | ||
146 | |||
147 | const char *zombie = "Z"; | ||
148 | |||
149 | int resultsum = 0; /* bitmask of the filter criteria met by a process */ | ||
150 | int found = 0; /* counter for number of lines returned in `ps` output */ | ||
151 | int procs = 0; /* counter for number of processes meeting filter criteria */ | ||
152 | int pos; /* number of spaces before 'args' in `ps` output */ | ||
153 | int cols; /* number of columns in ps output */ | ||
154 | int expected_cols = PS_COLS - 1; | ||
155 | int warn = 0; /* number of processes in warn state */ | ||
156 | int crit = 0; /* number of processes in crit state */ | ||
157 | int i = 0; | ||
158 | int result = STATE_UNKNOWN; | ||
159 | int ret = 0; | ||
160 | output chld_out, chld_err; | ||
161 | |||
162 | setlocale (LC_ALL, ""); | ||
163 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
164 | textdomain (PACKAGE); | ||
165 | setlocale(LC_NUMERIC, "POSIX"); | 96 | setlocale(LC_NUMERIC, "POSIX"); |
166 | 97 | bindtextdomain(PACKAGE, LOCALEDIR); | |
167 | input_buffer = malloc (MAX_INPUT_BUFFER); | 98 | textdomain(PACKAGE); |
168 | procprog = malloc (MAX_INPUT_BUFFER); | ||
169 | |||
170 | xasprintf (&metric_name, "PROCS"); | ||
171 | metric = METRIC_PROCS; | ||
172 | 99 | ||
173 | /* Parse extra opts if any */ | 100 | /* Parse extra opts if any */ |
174 | argv=np_extra_opts (&argc, argv, progname); | 101 | argv = np_extra_opts(&argc, argv, progname); |
102 | |||
103 | check_procs_config_wrapper tmp_config = process_arguments(argc, argv); | ||
104 | if (tmp_config.errorcode == ERROR) { | ||
105 | usage4(_("Could not parse arguments")); | ||
106 | } | ||
175 | 107 | ||
176 | if (process_arguments (argc, argv) == ERROR) | 108 | check_procs_config config = tmp_config.config; |
177 | usage4 (_("Could not parse arguments")); | ||
178 | 109 | ||
179 | /* find ourself */ | 110 | /* find ourself */ |
180 | mypid = getpid(); | 111 | pid_t mypid = getpid(); |
181 | myppid = getppid(); | 112 | pid_t myppid = getppid(); |
182 | if (usepid || stat_exe(mypid, &statbuf) == -1) { | 113 | dev_t mydev = 0; |
114 | ino_t myino = 0; | ||
115 | struct stat statbuf; | ||
116 | if (config.usepid || stat_exe(mypid, &statbuf) == -1) { | ||
183 | /* usepid might have been set by -T */ | 117 | /* usepid might have been set by -T */ |
184 | usepid = 1; | 118 | config.usepid = true; |
185 | } else { | 119 | } else { |
186 | usepid = 0; | 120 | config.usepid = false; |
187 | mydev = statbuf.st_dev; | 121 | mydev = statbuf.st_dev; |
188 | myino = statbuf.st_ino; | 122 | myino = statbuf.st_ino; |
189 | } | 123 | } |
190 | 124 | ||
191 | /* Set signal handling and alarm timeout */ | 125 | /* Set signal handling and alarm timeout */ |
192 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 126 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
193 | die (STATE_UNKNOWN, _("Cannot catch SIGALRM")); | 127 | die(STATE_UNKNOWN, _("Cannot catch SIGALRM")); |
194 | } | 128 | } |
195 | (void) alarm ((unsigned) timeout_interval); | 129 | (void)alarm(timeout_interval); |
196 | 130 | ||
197 | if (verbose >= 2) | 131 | if (verbose >= 2) { |
198 | printf (_("CMD: %s\n"), PS_COMMAND); | 132 | printf(_("CMD: %s\n"), PS_COMMAND); |
133 | } | ||
199 | 134 | ||
200 | if (input_filename == NULL) { | 135 | output chld_out; |
201 | result = cmd_run( PS_COMMAND, &chld_out, &chld_err, 0); | 136 | output chld_err; |
137 | mp_state_enum result = STATE_UNKNOWN; | ||
138 | if (config.input_filename == NULL) { | ||
139 | result = cmd_run(PS_COMMAND, &chld_out, &chld_err, 0); | ||
202 | if (chld_err.lines > 0) { | 140 | if (chld_err.lines > 0) { |
203 | printf ("%s: %s", _("System call sent warnings to stderr"), chld_err.line[0]); | 141 | printf("%s: %s", _("System call sent warnings to stderr"), chld_err.line[0]); |
204 | exit(STATE_WARNING); | 142 | exit(STATE_WARNING); |
205 | } | 143 | } |
206 | } else { | 144 | } else { |
207 | result = cmd_file_read( input_filename, &chld_out, 0); | 145 | result = cmd_file_read(config.input_filename, &chld_out, 0); |
208 | } | 146 | } |
209 | 147 | ||
148 | int pos; /* number of spaces before 'args' in `ps` output */ | ||
149 | uid_t procuid = 0; | ||
150 | pid_t procpid = 0; | ||
151 | pid_t procppid = 0; | ||
152 | pid_t kthread_ppid = 0; | ||
153 | int warn = 0; /* number of processes in warn state */ | ||
154 | int crit = 0; /* number of processes in crit state */ | ||
155 | int procvsz = 0; | ||
156 | int procrss = 0; | ||
157 | int procseconds = 0; | ||
158 | float procpcpu = 0; | ||
159 | char procstat[8]; | ||
160 | char procetime[MAX_INPUT_BUFFER] = {'\0'}; | ||
161 | int resultsum = 0; /* bitmask of the filter criteria met by a process */ | ||
162 | int found = 0; /* counter for number of lines returned in `ps` output */ | ||
163 | int procs = 0; /* counter for number of processes meeting filter criteria */ | ||
164 | char *input_buffer = malloc(MAX_INPUT_BUFFER); | ||
165 | char *procprog = malloc(MAX_INPUT_BUFFER); | ||
166 | const int expected_cols = PS_COLS - 1; | ||
167 | |||
210 | /* flush first line: j starts at 1 */ | 168 | /* flush first line: j starts at 1 */ |
211 | for (size_t j = 1; j < chld_out.lines; j++) { | 169 | for (size_t j = 1; j < chld_out.lines; j++) { |
212 | input_line = chld_out.line[j]; | 170 | char *input_line = chld_out.line[j]; |
213 | 171 | ||
214 | if (verbose >= 3) | 172 | if (verbose >= 3) { |
215 | printf ("%s", input_line); | 173 | printf("%s", input_line); |
174 | } | ||
216 | 175 | ||
217 | strcpy (procprog, ""); | 176 | strcpy(procprog, ""); |
218 | xasprintf (&procargs, "%s", ""); | 177 | char *procargs; |
178 | xasprintf(&procargs, "%s", ""); | ||
219 | 179 | ||
220 | cols = sscanf (input_line, PS_FORMAT, PS_VARLIST); | 180 | /* number of columns in ps output */ |
181 | int cols = sscanf(input_line, PS_FORMAT, PS_VARLIST); | ||
221 | 182 | ||
222 | /* Zombie processes do not give a procprog command */ | 183 | /* Zombie processes do not give a procprog command */ |
223 | if ( cols < expected_cols && strstr(procstat, zombie) ) { | 184 | const char *zombie = "Z"; |
185 | if (cols < expected_cols && strstr(procstat, zombie)) { | ||
224 | cols = expected_cols; | 186 | cols = expected_cols; |
225 | } | 187 | } |
226 | if ( cols >= expected_cols ) { | 188 | if (cols >= expected_cols) { |
227 | resultsum = 0; | 189 | resultsum = 0; |
228 | xasprintf (&procargs, "%s", input_line + pos); | 190 | xasprintf(&procargs, "%s", input_line + pos); |
229 | strip (procargs); | 191 | strip(procargs); |
230 | 192 | ||
231 | /* Some ps return full pathname for command. This removes path */ | 193 | /* Some ps return full pathname for command. This removes path */ |
232 | strcpy(procprog, base_name(procprog)); | 194 | strcpy(procprog, base_name(procprog)); |
233 | 195 | ||
234 | /* we need to convert the elapsed time to seconds */ | 196 | /* we need to convert the elapsed time to seconds */ |
235 | procseconds = convert_to_seconds(procetime); | 197 | procseconds = convert_to_seconds(procetime, config.metric); |
236 | 198 | ||
237 | if (verbose >= 3) | 199 | if (verbose >= 3) { |
238 | printf ("proc#=%d uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", | 200 | printf("proc#=%d uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s " |
239 | procs, procuid, procvsz, procrss, | 201 | "prog=%s args=%s\n", |
240 | procpid, procppid, procpcpu, procstat, | 202 | procs, procuid, procvsz, procrss, procpid, procppid, procpcpu, procstat, |
241 | procetime, procprog, procargs); | 203 | procetime, procprog, procargs); |
204 | } | ||
242 | 205 | ||
243 | /* Ignore self */ | 206 | /* Ignore self */ |
244 | if ((usepid && mypid == procpid) || | 207 | int ret = 0; |
245 | ( ((!usepid) && ((ret = stat_exe(procpid, &statbuf) != -1) && statbuf.st_dev == mydev && statbuf.st_ino == myino)) || | 208 | if ((config.usepid && mypid == procpid) || |
246 | (ret == -1 && errno == ENOENT)) | 209 | (((!config.usepid) && ((ret = stat_exe(procpid, &statbuf) != -1) && |
247 | ) { | 210 | statbuf.st_dev == mydev && statbuf.st_ino == myino)) || |
248 | if (verbose >= 3) | 211 | (ret == -1 && errno == ENOENT))) { |
249 | printf("not considering - is myself or gone\n"); | 212 | if (verbose >= 3) { |
213 | printf("not considering - is myself or gone\n"); | ||
214 | } | ||
250 | continue; | 215 | continue; |
251 | } | 216 | } |
252 | /* Ignore parent*/ | 217 | /* Ignore parent*/ |
253 | else if (myppid == procpid) { | 218 | if (myppid == procpid) { |
254 | if (verbose >= 3) | 219 | if (verbose >= 3) { |
255 | printf("not considering - is parent\n"); | 220 | printf("not considering - is parent\n"); |
221 | } | ||
256 | continue; | 222 | continue; |
257 | } | 223 | } |
258 | 224 | ||
259 | /* Ignore our own children */ | 225 | /* Ignore our own children */ |
260 | if (procppid == mypid) { | 226 | if (procppid == mypid) { |
261 | if (verbose >= 3) | 227 | if (verbose >= 3) { |
262 | printf("not considering - is our child\n"); | 228 | printf("not considering - is our child\n"); |
229 | } | ||
263 | continue; | 230 | continue; |
264 | } | 231 | } |
265 | 232 | ||
266 | /* Ignore excluded processes by name */ | 233 | /* Ignore excluded processes by name */ |
267 | if(options & EXCLUDE_PROGS) { | 234 | if (config.options & EXCLUDE_PROGS) { |
268 | int found = 0; | 235 | bool found = false; |
269 | int i = 0; | 236 | for (int i = 0; i < (config.exclude_progs_counter); i++) { |
270 | 237 | if (!strcmp(procprog, config.exclude_progs_arr[i])) { | |
271 | for(i=0; i < (exclude_progs_counter); i++) { | 238 | found = true; |
272 | if(!strcmp(procprog, exclude_progs_arr[i])) { | 239 | } |
273 | found = 1; | 240 | } |
274 | } | 241 | if (!found) { |
275 | } | 242 | resultsum |= EXCLUDE_PROGS; |
276 | if(found == 0) { | 243 | } else { |
277 | resultsum |= EXCLUDE_PROGS; | 244 | if (verbose >= 3) { |
278 | } else | 245 | printf("excluding - by ignorelist\n"); |
279 | { | 246 | } |
280 | if(verbose >= 3) | 247 | } |
281 | printf("excluding - by ignorelist\n"); | ||
282 | } | ||
283 | } | 248 | } |
284 | 249 | ||
285 | /* filter kernel threads (children of KTHREAD_PARENT)*/ | 250 | /* filter kernel threads (children of KTHREAD_PARENT)*/ |
286 | /* TODO adapt for other OSes than GNU/Linux | 251 | /* TODO adapt for other OSes than GNU/Linux |
287 | sorry for not doing that, but I've no other OSes to test :-( */ | 252 | sorry for not doing that, but I've no other OSes to test :-( */ |
288 | if (kthread_filter == 1) { | 253 | if (config.kthread_filter) { |
289 | /* get pid KTHREAD_PARENT */ | 254 | /* get pid KTHREAD_PARENT */ |
290 | if (kthread_ppid == 0 && !strcmp(procprog, KTHREAD_PARENT) ) | 255 | if (kthread_ppid == 0 && !strcmp(procprog, KTHREAD_PARENT)) { |
291 | kthread_ppid = procpid; | 256 | kthread_ppid = procpid; |
257 | } | ||
292 | 258 | ||
293 | if (kthread_ppid == procppid) { | 259 | if (kthread_ppid == procppid) { |
294 | if (verbose >= 2) | 260 | if (verbose >= 2) { |
295 | printf ("Ignore kernel thread: pid=%d ppid=%d prog=%s args=%s\n", procpid, procppid, procprog, procargs); | 261 | printf("Ignore kernel thread: pid=%d ppid=%d prog=%s args=%s\n", procpid, |
262 | procppid, procprog, procargs); | ||
263 | } | ||
296 | continue; | 264 | continue; |
297 | } | 265 | } |
298 | } | 266 | } |
299 | 267 | ||
300 | if ((options & STAT) && (strstr (procstat, statopts))) | 268 | if ((config.options & STAT) && (strstr(procstat, config.statopts))) { |
301 | resultsum |= STAT; | 269 | resultsum |= STAT; |
302 | if ((options & ARGS) && procargs && (strstr (procargs, args) != NULL)) | 270 | } |
271 | if ((config.options & ARGS) && procargs && (strstr(procargs, config.args) != NULL)) { | ||
303 | resultsum |= ARGS; | 272 | resultsum |= ARGS; |
304 | if ((options & EREG_ARGS) && procargs && (regexec(&re_args, procargs, (size_t) 0, NULL, 0) == 0)) | 273 | } |
274 | if ((config.options & EREG_ARGS) && procargs && | ||
275 | (regexec(&config.re_args, procargs, (size_t)0, NULL, 0) == 0)) { | ||
305 | resultsum |= EREG_ARGS; | 276 | resultsum |= EREG_ARGS; |
306 | if ((options & PROG) && procprog && (strcmp (prog, procprog) == 0)) | 277 | } |
278 | if ((config.options & PROG) && procprog && (strcmp(config.prog, procprog) == 0)) { | ||
307 | resultsum |= PROG; | 279 | resultsum |= PROG; |
308 | if ((options & PPID) && (procppid == ppid)) | 280 | } |
281 | if ((config.options & PPID) && (procppid == config.ppid)) { | ||
309 | resultsum |= PPID; | 282 | resultsum |= PPID; |
310 | if ((options & USER) && (procuid == uid)) | 283 | } |
284 | if ((config.options & USER) && (procuid == config.uid)) { | ||
311 | resultsum |= USER; | 285 | resultsum |= USER; |
312 | if ((options & VSZ) && (procvsz >= vsz)) | 286 | } |
287 | if ((config.options & VSZ) && (procvsz >= config.vsz)) { | ||
313 | resultsum |= VSZ; | 288 | resultsum |= VSZ; |
314 | if ((options & RSS) && (procrss >= rss)) | 289 | } |
290 | if ((config.options & RSS) && (procrss >= config.rss)) { | ||
315 | resultsum |= RSS; | 291 | resultsum |= RSS; |
316 | if ((options & PCPU) && (procpcpu >= pcpu)) | 292 | } |
293 | if ((config.options & PCPU) && (procpcpu >= config.pcpu)) { | ||
317 | resultsum |= PCPU; | 294 | resultsum |= PCPU; |
295 | } | ||
318 | 296 | ||
319 | found++; | 297 | found++; |
320 | 298 | ||
321 | /* Next line if filters not matched */ | 299 | /* Next line if filters not matched */ |
322 | if (!(options == resultsum || options == ALL)) | 300 | if (!(config.options == resultsum || config.options == ALL)) { |
323 | continue; | 301 | continue; |
302 | } | ||
324 | 303 | ||
325 | procs++; | 304 | procs++; |
326 | if (verbose >= 2) { | 305 | if (verbose >= 2) { |
327 | printf ("Matched: uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", | 306 | printf("Matched: uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s " |
328 | procuid, procvsz, procrss, | 307 | "prog=%s args=%s\n", |
329 | procpid, procppid, procpcpu, procstat, | 308 | procuid, procvsz, procrss, procpid, procppid, procpcpu, procstat, procetime, |
330 | procetime, procprog, procargs); | 309 | procprog, procargs); |
331 | } | 310 | } |
332 | 311 | ||
333 | if (metric == METRIC_VSZ) | 312 | mp_state_enum temporary_result = STATE_OK; |
334 | i = get_status ((double)procvsz, procs_thresholds); | 313 | if (config.metric == METRIC_VSZ) { |
335 | else if (metric == METRIC_RSS) | 314 | temporary_result = get_status((double)procvsz, config.procs_thresholds); |
336 | i = get_status ((double)procrss, procs_thresholds); | 315 | } else if (config.metric == METRIC_RSS) { |
316 | temporary_result = get_status((double)procrss, config.procs_thresholds); | ||
317 | } | ||
337 | /* TODO? float thresholds for --metric=CPU */ | 318 | /* TODO? float thresholds for --metric=CPU */ |
338 | else if (metric == METRIC_CPU) | 319 | else if (config.metric == METRIC_CPU) { |
339 | i = get_status (procpcpu, procs_thresholds); | 320 | temporary_result = get_status(procpcpu, config.procs_thresholds); |
340 | else if (metric == METRIC_ELAPSED) | 321 | } else if (config.metric == METRIC_ELAPSED) { |
341 | i = get_status ((double)procseconds, procs_thresholds); | 322 | temporary_result = get_status((double)procseconds, config.procs_thresholds); |
323 | } | ||
342 | 324 | ||
343 | if (metric != METRIC_PROCS) { | 325 | if (config.metric != METRIC_PROCS) { |
344 | if (i == STATE_WARNING) { | 326 | if (temporary_result == STATE_WARNING) { |
345 | warn++; | 327 | warn++; |
346 | xasprintf (&fails, "%s%s%s", fails, (strcmp(fails,"") ? ", " : ""), procprog); | 328 | xasprintf(&config.fails, "%s%s%s", config.fails, |
347 | result = max_state (result, i); | 329 | (strcmp(config.fails, "") ? ", " : ""), procprog); |
330 | result = max_state(result, temporary_result); | ||
348 | } | 331 | } |
349 | if (i == STATE_CRITICAL) { | 332 | if (temporary_result == STATE_CRITICAL) { |
350 | crit++; | 333 | crit++; |
351 | xasprintf (&fails, "%s%s%s", fails, (strcmp(fails,"") ? ", " : ""), procprog); | 334 | xasprintf(&config.fails, "%s%s%s", config.fails, |
352 | result = max_state (result, i); | 335 | (strcmp(config.fails, "") ? ", " : ""), procprog); |
336 | result = max_state(result, temporary_result); | ||
353 | } | 337 | } |
354 | } | 338 | } |
355 | } | 339 | } |
@@ -359,339 +343,366 @@ main (int argc, char **argv) | |||
359 | } | 343 | } |
360 | } | 344 | } |
361 | 345 | ||
362 | if (found == 0) { /* no process lines parsed so return STATE_UNKNOWN */ | 346 | if (found == 0) { /* no process lines parsed so return STATE_UNKNOWN */ |
363 | printf (_("Unable to read output\n")); | 347 | printf(_("Unable to read output\n")); |
364 | return STATE_UNKNOWN; | 348 | return STATE_UNKNOWN; |
365 | } | 349 | } |
366 | 350 | ||
367 | if ( result == STATE_UNKNOWN ) | 351 | if (result == STATE_UNKNOWN) { |
368 | result = STATE_OK; | 352 | result = STATE_OK; |
353 | } | ||
369 | 354 | ||
370 | /* Needed if procs found, but none match filter */ | 355 | /* Needed if procs found, but none match filter */ |
371 | if ( metric == METRIC_PROCS ) { | 356 | if (config.metric == METRIC_PROCS) { |
372 | result = max_state (result, get_status ((double)procs, procs_thresholds) ); | 357 | result = max_state(result, get_status((double)procs, config.procs_thresholds)); |
373 | } | 358 | } |
374 | 359 | ||
375 | if ( result == STATE_OK ) { | 360 | if (result == STATE_OK) { |
376 | printf ("%s %s: ", metric_name, _("OK")); | 361 | printf("%s %s: ", config.metric_name, _("OK")); |
377 | } else if (result == STATE_WARNING) { | 362 | } else if (result == STATE_WARNING) { |
378 | printf ("%s %s: ", metric_name, _("WARNING")); | 363 | printf("%s %s: ", config.metric_name, _("WARNING")); |
379 | if ( metric != METRIC_PROCS ) { | 364 | if (config.metric != METRIC_PROCS) { |
380 | printf (_("%d warn out of "), warn); | 365 | printf(_("%d warn out of "), warn); |
381 | } | 366 | } |
382 | } else if (result == STATE_CRITICAL) { | 367 | } else if (result == STATE_CRITICAL) { |
383 | printf ("%s %s: ", metric_name, _("CRITICAL")); | 368 | printf("%s %s: ", config.metric_name, _("CRITICAL")); |
384 | if (metric != METRIC_PROCS) { | 369 | if (config.metric != METRIC_PROCS) { |
385 | printf (_("%d crit, %d warn out of "), crit, warn); | 370 | printf(_("%d crit, %d warn out of "), crit, warn); |
386 | } | 371 | } |
387 | } | 372 | } |
388 | printf (ngettext ("%d process", "%d processes", (unsigned long) procs), procs); | 373 | printf(ngettext("%d process", "%d processes", (unsigned long)procs), procs); |
389 | 374 | ||
390 | if (strcmp(fmt,"") != 0) { | 375 | if (strcmp(config.fmt, "") != 0) { |
391 | printf (_(" with %s"), fmt); | 376 | printf(_(" with %s"), config.fmt); |
392 | } | 377 | } |
393 | 378 | ||
394 | if ( verbose >= 1 && strcmp(fails,"") ) | 379 | if (verbose >= 1 && strcmp(config.fails, "")) { |
395 | printf (" [%s]", fails); | 380 | printf(" [%s]", config.fails); |
381 | } | ||
396 | 382 | ||
397 | if (metric == METRIC_PROCS) | 383 | if (config.metric == METRIC_PROCS) { |
398 | printf (" | procs=%d;%s;%s;0;", procs, | 384 | printf(" | procs=%d;%s;%s;0;", procs, config.warning_range ? config.warning_range : "", |
399 | warning_range ? warning_range : "", | 385 | config.critical_range ? config.critical_range : ""); |
400 | critical_range ? critical_range : ""); | 386 | } else { |
401 | else | 387 | printf(" | procs=%d;;;0; procs_warn=%d;;;0; procs_crit=%d;;;0;", procs, warn, crit); |
402 | printf (" | procs=%d;;;0; procs_warn=%d;;;0; procs_crit=%d;;;0;", procs, warn, crit); | 388 | } |
403 | 389 | ||
404 | printf ("\n"); | 390 | printf("\n"); |
405 | return result; | 391 | exit(result); |
406 | } | 392 | } |
407 | 393 | ||
408 | |||
409 | |||
410 | /* process command-line arguments */ | 394 | /* process command-line arguments */ |
411 | int | 395 | check_procs_config_wrapper process_arguments(int argc, char **argv) { |
412 | process_arguments (int argc, char **argv) | 396 | static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, |
413 | { | 397 | {"critical", required_argument, 0, 'c'}, |
414 | int c = 1; | 398 | {"metric", required_argument, 0, 'm'}, |
415 | char *user; | 399 | {"timeout", required_argument, 0, 't'}, |
416 | struct passwd *pw; | 400 | {"status", required_argument, 0, 's'}, |
417 | int option = 0; | 401 | {"ppid", required_argument, 0, 'p'}, |
418 | int err; | 402 | {"user", required_argument, 0, 'u'}, |
419 | int cflags = REG_NOSUB | REG_EXTENDED; | 403 | {"command", required_argument, 0, 'C'}, |
420 | char errbuf[MAX_INPUT_BUFFER]; | 404 | {"vsz", required_argument, 0, 'z'}, |
421 | char *temp_string; | 405 | {"rss", required_argument, 0, 'r'}, |
422 | int i=0; | 406 | {"pcpu", required_argument, 0, 'P'}, |
423 | static struct option longopts[] = { | 407 | {"elapsed", required_argument, 0, 'e'}, |
424 | {"warning", required_argument, 0, 'w'}, | 408 | {"argument-array", required_argument, 0, 'a'}, |
425 | {"critical", required_argument, 0, 'c'}, | 409 | {"help", no_argument, 0, 'h'}, |
426 | {"metric", required_argument, 0, 'm'}, | 410 | {"version", no_argument, 0, 'V'}, |
427 | {"timeout", required_argument, 0, 't'}, | 411 | {"verbose", no_argument, 0, 'v'}, |
428 | {"status", required_argument, 0, 's'}, | 412 | {"ereg-argument-array", required_argument, 0, CHAR_MAX + 1}, |
429 | {"ppid", required_argument, 0, 'p'}, | 413 | {"input-file", required_argument, 0, CHAR_MAX + 2}, |
430 | {"user", required_argument, 0, 'u'}, | 414 | {"no-kthreads", required_argument, 0, 'k'}, |
431 | {"command", required_argument, 0, 'C'}, | 415 | {"traditional-filter", no_argument, 0, 'T'}, |
432 | {"vsz", required_argument, 0, 'z'}, | 416 | {"exclude-process", required_argument, 0, 'X'}, |
433 | {"rss", required_argument, 0, 'r'}, | 417 | {0, 0, 0, 0}}; |
434 | {"pcpu", required_argument, 0, 'P'}, | 418 | |
435 | {"elapsed", required_argument, 0, 'e'}, | 419 | for (int index = 1; index < argc; index++) { |
436 | {"argument-array", required_argument, 0, 'a'}, | 420 | if (strcmp("-to", argv[index]) == 0) { |
437 | {"help", no_argument, 0, 'h'}, | 421 | strcpy(argv[index], "-t"); |
438 | {"version", no_argument, 0, 'V'}, | 422 | } |
439 | {"verbose", no_argument, 0, 'v'}, | 423 | } |
440 | {"ereg-argument-array", required_argument, 0, CHAR_MAX+1}, | ||
441 | {"input-file", required_argument, 0, CHAR_MAX+2}, | ||
442 | {"no-kthreads", required_argument, 0, 'k'}, | ||
443 | {"traditional-filter", no_argument, 0, 'T'}, | ||
444 | {"exclude-process", required_argument, 0, 'X'}, | ||
445 | {0, 0, 0, 0} | ||
446 | }; | ||
447 | 424 | ||
448 | for (c = 1; c < argc; c++) | 425 | check_procs_config_wrapper result = { |
449 | if (strcmp ("-to", argv[c]) == 0) | 426 | .errorcode = OK, |
450 | strcpy (argv[c], "-t"); | 427 | .config = check_procs_config_init(), |
428 | }; | ||
451 | 429 | ||
452 | while (1) { | 430 | while (true) { |
453 | c = getopt_long (argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:T:X:", | 431 | int option = 0; |
454 | longopts, &option); | 432 | int option_index = |
433 | getopt_long(argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:T:X:", longopts, &option); | ||
455 | 434 | ||
456 | if (c == -1 || c == EOF) | 435 | if (option_index == -1 || option_index == EOF) { |
457 | break; | 436 | break; |
437 | } | ||
458 | 438 | ||
459 | switch (c) { | 439 | switch (option_index) { |
460 | case '?': /* help */ | 440 | case '?': /* help */ |
461 | usage5 (); | 441 | usage5(); |
462 | case 'h': /* help */ | 442 | case 'h': /* help */ |
463 | print_help (); | 443 | print_help(); |
464 | exit (STATE_UNKNOWN); | 444 | exit(STATE_UNKNOWN); |
465 | case 'V': /* version */ | 445 | case 'V': /* version */ |
466 | print_revision (progname, NP_VERSION); | 446 | print_revision(progname, NP_VERSION); |
467 | exit (STATE_UNKNOWN); | 447 | exit(STATE_UNKNOWN); |
468 | case 't': /* timeout period */ | 448 | case 't': /* timeout period */ |
469 | if (!is_integer (optarg)) | 449 | if (!is_integer(optarg)) { |
470 | usage2 (_("Timeout interval must be a positive integer"), optarg); | 450 | usage2(_("Timeout interval must be a positive integer"), optarg); |
471 | else | 451 | } else { |
472 | timeout_interval = atoi (optarg); | 452 | timeout_interval = atoi(optarg); |
453 | } | ||
473 | break; | 454 | break; |
474 | case 'c': /* critical threshold */ | 455 | case 'c': /* critical threshold */ |
475 | critical_range = optarg; | 456 | result.config.critical_range = optarg; |
476 | break; | 457 | break; |
477 | case 'w': /* warning threshold */ | 458 | case 'w': /* warning threshold */ |
478 | warning_range = optarg; | 459 | result.config.warning_range = optarg; |
479 | break; | 460 | break; |
480 | case 'p': /* process id */ | 461 | case 'p': { /* process id */ |
481 | if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) { | 462 | static char tmp[MAX_INPUT_BUFFER]; |
482 | xasprintf (&fmt, "%s%sPPID = %d", (fmt ? fmt : "") , (options ? ", " : ""), ppid); | 463 | if (sscanf(optarg, "%d%[^0-9]", &result.config.ppid, tmp) == 1) { |
483 | options |= PPID; | 464 | xasprintf(&result.config.fmt, "%s%sPPID = %d", |
465 | (result.config.fmt ? result.config.fmt : ""), | ||
466 | (result.config.options ? ", " : ""), result.config.ppid); | ||
467 | result.config.options |= PPID; | ||
484 | break; | 468 | break; |
485 | } | 469 | } |
486 | usage4 (_("Parent Process ID must be an integer!")); | 470 | usage4(_("Parent Process ID must be an integer!")); |
487 | case 's': /* status */ | 471 | } |
488 | if (statopts) | 472 | case 's': /* status */ |
473 | if (result.config.statopts) { | ||
489 | break; | 474 | break; |
490 | else | 475 | } else { |
491 | statopts = optarg; | 476 | result.config.statopts = optarg; |
492 | xasprintf (&fmt, _("%s%sSTATE = %s"), (fmt ? fmt : ""), (options ? ", " : ""), statopts); | 477 | } |
493 | options |= STAT; | 478 | xasprintf(&result.config.fmt, _("%s%sSTATE = %s"), |
479 | (result.config.fmt ? result.config.fmt : ""), | ||
480 | (result.config.options ? ", " : ""), result.config.statopts); | ||
481 | result.config.options |= STAT; | ||
494 | break; | 482 | break; |
495 | case 'u': /* user or user id */ | 483 | case 'u': /* user or user id */ { |
496 | if (is_integer (optarg)) { | 484 | struct passwd *pw; |
497 | uid = atoi (optarg); | 485 | if (is_integer(optarg)) { |
498 | pw = getpwuid ((uid_t) uid); | 486 | result.config.uid = atoi(optarg); |
487 | pw = getpwuid(result.config.uid); | ||
499 | /* check to be sure user exists */ | 488 | /* check to be sure user exists */ |
500 | if (pw == NULL) | 489 | if (pw == NULL) { |
501 | usage2 (_("UID was not found"), optarg); | 490 | usage2(_("UID was not found"), optarg); |
502 | } | 491 | } |
503 | else { | 492 | } else { |
504 | pw = getpwnam (optarg); | 493 | pw = getpwnam(optarg); |
505 | /* check to be sure user exists */ | 494 | /* check to be sure user exists */ |
506 | if (pw == NULL) | 495 | if (pw == NULL) { |
507 | usage2 (_("User name was not found"), optarg); | 496 | usage2(_("User name was not found"), optarg); |
497 | } | ||
508 | /* then get uid */ | 498 | /* then get uid */ |
509 | uid = pw->pw_uid; | 499 | result.config.uid = pw->pw_uid; |
510 | } | 500 | } |
511 | user = pw->pw_name; | 501 | |
512 | xasprintf (&fmt, "%s%sUID = %d (%s)", (fmt ? fmt : ""), (options ? ", " : ""), | 502 | char *user = pw->pw_name; |
513 | uid, user); | 503 | xasprintf(&result.config.fmt, "%s%sUID = %d (%s)", |
514 | options |= USER; | 504 | (result.config.fmt ? result.config.fmt : ""), |
515 | break; | 505 | (result.config.options ? ", " : ""), result.config.uid, user); |
516 | case 'C': /* command */ | 506 | result.config.options |= USER; |
507 | } break; | ||
508 | case 'C': /* command */ | ||
517 | /* TODO: allow this to be passed in with --metric */ | 509 | /* TODO: allow this to be passed in with --metric */ |
518 | if (prog) | 510 | if (result.config.prog) { |
519 | break; | 511 | break; |
520 | else | 512 | } else { |
521 | prog = optarg; | 513 | result.config.prog = optarg; |
522 | xasprintf (&fmt, _("%s%scommand name '%s'"), (fmt ? fmt : ""), (options ? ", " : ""), | 514 | } |
523 | prog); | 515 | xasprintf(&result.config.fmt, _("%s%scommand name '%s'"), |
524 | options |= PROG; | 516 | (result.config.fmt ? result.config.fmt : ""), |
517 | (result.config.options ? ", " : ""), result.config.prog); | ||
518 | result.config.options |= PROG; | ||
525 | break; | 519 | break; |
526 | case 'X': | 520 | case 'X': |
527 | if(exclude_progs) | 521 | if (result.config.exclude_progs) { |
528 | break; | 522 | break; |
529 | else | 523 | } else { |
530 | exclude_progs = optarg; | 524 | result.config.exclude_progs = optarg; |
531 | xasprintf (&fmt, _("%s%sexclude progs '%s'"), (fmt ? fmt : ""), (options ? ", " : ""), | 525 | } |
532 | exclude_progs); | 526 | xasprintf(&result.config.fmt, _("%s%sexclude progs '%s'"), |
533 | char *p = strtok(exclude_progs, ","); | 527 | (result.config.fmt ? result.config.fmt : ""), |
534 | 528 | (result.config.options ? ", " : ""), result.config.exclude_progs); | |
535 | while(p){ | 529 | char *tmp_pointer = strtok(result.config.exclude_progs, ","); |
536 | exclude_progs_arr = realloc(exclude_progs_arr, sizeof(char*) * ++exclude_progs_counter); | 530 | |
537 | exclude_progs_arr[exclude_progs_counter-1] = p; | 531 | while (tmp_pointer) { |
538 | p = strtok(NULL, ","); | 532 | result.config.exclude_progs_arr = |
533 | realloc(result.config.exclude_progs_arr, | ||
534 | sizeof(char *) * ++result.config.exclude_progs_counter); | ||
535 | result.config.exclude_progs_arr[result.config.exclude_progs_counter - 1] = | ||
536 | tmp_pointer; | ||
537 | tmp_pointer = strtok(NULL, ","); | ||
539 | } | 538 | } |
540 | 539 | ||
541 | options |= EXCLUDE_PROGS; | 540 | result.config.options |= EXCLUDE_PROGS; |
542 | break; | 541 | break; |
543 | case 'a': /* args (full path name with args) */ | 542 | case 'a': /* args (full path name with args) */ |
544 | /* TODO: allow this to be passed in with --metric */ | 543 | /* TODO: allow this to be passed in with --metric */ |
545 | if (args) | 544 | if (result.config.args) { |
546 | break; | 545 | break; |
547 | else | 546 | } else { |
548 | args = optarg; | 547 | result.config.args = optarg; |
549 | xasprintf (&fmt, "%s%sargs '%s'", (fmt ? fmt : ""), (options ? ", " : ""), args); | 548 | } |
550 | options |= ARGS; | 549 | xasprintf(&result.config.fmt, "%s%sargs '%s'", |
550 | (result.config.fmt ? result.config.fmt : ""), | ||
551 | (result.config.options ? ", " : ""), result.config.args); | ||
552 | result.config.options |= ARGS; | ||
551 | break; | 553 | break; |
552 | case CHAR_MAX+1: | 554 | case CHAR_MAX + 1: { |
553 | err = regcomp(&re_args, optarg, cflags); | 555 | int cflags = REG_NOSUB | REG_EXTENDED; |
556 | int err = regcomp(&result.config.re_args, optarg, cflags); | ||
554 | if (err != 0) { | 557 | if (err != 0) { |
555 | regerror (err, &re_args, errbuf, MAX_INPUT_BUFFER); | 558 | char errbuf[MAX_INPUT_BUFFER]; |
556 | die (STATE_UNKNOWN, "PROCS %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 559 | regerror(err, &result.config.re_args, errbuf, MAX_INPUT_BUFFER); |
560 | die(STATE_UNKNOWN, "PROCS %s: %s - %s\n", _("UNKNOWN"), | ||
561 | _("Could not compile regular expression"), errbuf); | ||
557 | } | 562 | } |
558 | /* Strip off any | within the regex optarg */ | 563 | /* Strip off any | within the regex optarg */ |
559 | temp_string = strdup(optarg); | 564 | char *temp_string = strdup(optarg); |
560 | while(temp_string[i]!='\0'){ | 565 | int index = 0; |
561 | if(temp_string[i]=='|') | 566 | while (temp_string[index] != '\0') { |
562 | temp_string[i]=','; | 567 | if (temp_string[index] == '|') { |
563 | i++; | 568 | temp_string[index] = ','; |
564 | } | 569 | } |
565 | xasprintf (&fmt, "%s%sregex args '%s'", (fmt ? fmt : ""), (options ? ", " : ""), temp_string); | 570 | index++; |
566 | options |= EREG_ARGS; | 571 | } |
567 | break; | 572 | xasprintf(&result.config.fmt, "%s%sregex args '%s'", |
568 | case 'r': /* RSS */ | 573 | (result.config.fmt ? result.config.fmt : ""), |
569 | if (sscanf (optarg, "%d%[^0-9]", &rss, tmp) == 1) { | 574 | (result.config.options ? ", " : ""), temp_string); |
570 | xasprintf (&fmt, "%s%sRSS >= %d", (fmt ? fmt : ""), (options ? ", " : ""), rss); | 575 | result.config.options |= EREG_ARGS; |
571 | options |= RSS; | 576 | } break; |
577 | case 'r': { /* RSS */ | ||
578 | static char tmp[MAX_INPUT_BUFFER]; | ||
579 | if (sscanf(optarg, "%d%[^0-9]", &result.config.rss, tmp) == 1) { | ||
580 | xasprintf(&result.config.fmt, "%s%sRSS >= %d", | ||
581 | (result.config.fmt ? result.config.fmt : ""), | ||
582 | (result.config.options ? ", " : ""), result.config.rss); | ||
583 | result.config.options |= RSS; | ||
572 | break; | 584 | break; |
573 | } | 585 | } |
574 | usage4 (_("RSS must be an integer!")); | 586 | usage4(_("RSS must be an integer!")); |
575 | case 'z': /* VSZ */ | 587 | } |
576 | if (sscanf (optarg, "%d%[^0-9]", &vsz, tmp) == 1) { | 588 | case 'z': { /* VSZ */ |
577 | xasprintf (&fmt, "%s%sVSZ >= %d", (fmt ? fmt : ""), (options ? ", " : ""), vsz); | 589 | static char tmp[MAX_INPUT_BUFFER]; |
578 | options |= VSZ; | 590 | if (sscanf(optarg, "%d%[^0-9]", &result.config.vsz, tmp) == 1) { |
591 | xasprintf(&result.config.fmt, "%s%sVSZ >= %d", | ||
592 | (result.config.fmt ? result.config.fmt : ""), | ||
593 | (result.config.options ? ", " : ""), result.config.vsz); | ||
594 | result.config.options |= VSZ; | ||
579 | break; | 595 | break; |
580 | } | 596 | } |
581 | usage4 (_("VSZ must be an integer!")); | 597 | usage4(_("VSZ must be an integer!")); |
582 | case 'P': /* PCPU */ | 598 | } |
599 | case 'P': { /* PCPU */ | ||
583 | /* TODO: -P 1.5.5 is accepted */ | 600 | /* TODO: -P 1.5.5 is accepted */ |
584 | if (sscanf (optarg, "%f%[^0-9.]", &pcpu, tmp) == 1) { | 601 | static char tmp[MAX_INPUT_BUFFER]; |
585 | xasprintf (&fmt, "%s%sPCPU >= %.2f", (fmt ? fmt : ""), (options ? ", " : ""), pcpu); | 602 | if (sscanf(optarg, "%f%[^0-9.]", &result.config.pcpu, tmp) == 1) { |
586 | options |= PCPU; | 603 | xasprintf(&result.config.fmt, "%s%sPCPU >= %.2f", |
604 | (result.config.fmt ? result.config.fmt : ""), | ||
605 | (result.config.options ? ", " : ""), result.config.pcpu); | ||
606 | result.config.options |= PCPU; | ||
587 | break; | 607 | break; |
588 | } | 608 | } |
589 | usage4 (_("PCPU must be a float!")); | 609 | usage4(_("PCPU must be a float!")); |
610 | } | ||
590 | case 'm': | 611 | case 'm': |
591 | xasprintf (&metric_name, "%s", optarg); | 612 | xasprintf(&result.config.metric_name, "%s", optarg); |
592 | if ( strcmp(optarg, "PROCS") == 0) { | 613 | if (strcmp(optarg, "PROCS") == 0) { |
593 | metric = METRIC_PROCS; | 614 | result.config.metric = METRIC_PROCS; |
594 | break; | 615 | break; |
595 | } | 616 | } |
596 | else if ( strcmp(optarg, "VSZ") == 0) { | 617 | if (strcmp(optarg, "VSZ") == 0) { |
597 | metric = METRIC_VSZ; | 618 | result.config.metric = METRIC_VSZ; |
598 | break; | 619 | break; |
599 | } | 620 | } |
600 | else if ( strcmp(optarg, "RSS") == 0 ) { | 621 | if (strcmp(optarg, "RSS") == 0) { |
601 | metric = METRIC_RSS; | 622 | result.config.metric = METRIC_RSS; |
602 | break; | 623 | break; |
603 | } | 624 | } |
604 | else if ( strcmp(optarg, "CPU") == 0 ) { | 625 | if (strcmp(optarg, "CPU") == 0) { |
605 | metric = METRIC_CPU; | 626 | result.config.metric = METRIC_CPU; |
606 | break; | 627 | break; |
607 | } | 628 | } |
608 | else if ( strcmp(optarg, "ELAPSED") == 0) { | 629 | if (strcmp(optarg, "ELAPSED") == 0) { |
609 | metric = METRIC_ELAPSED; | 630 | result.config.metric = METRIC_ELAPSED; |
610 | break; | 631 | break; |
611 | } | 632 | } |
612 | 633 | ||
613 | usage4 (_("Metric must be one of PROCS, VSZ, RSS, CPU, ELAPSED!")); | 634 | usage4(_("Metric must be one of PROCS, VSZ, RSS, CPU, ELAPSED!")); |
614 | case 'k': /* linux kernel thread filter */ | 635 | case 'k': /* linux kernel thread filter */ |
615 | kthread_filter = 1; | 636 | result.config.kthread_filter = true; |
616 | break; | 637 | break; |
617 | case 'v': /* command */ | 638 | case 'v': /* command */ |
618 | verbose++; | 639 | verbose++; |
619 | break; | 640 | break; |
620 | case 'T': | 641 | case 'T': |
621 | usepid = 1; | 642 | result.config.usepid = true; |
622 | break; | 643 | break; |
623 | case CHAR_MAX+2: | 644 | case CHAR_MAX + 2: |
624 | input_filename = optarg; | 645 | result.config.input_filename = optarg; |
625 | break; | 646 | break; |
626 | } | 647 | } |
627 | } | 648 | } |
628 | 649 | ||
629 | c = optind; | 650 | int index = optind; |
630 | if ((! warning_range) && argv[c]) | 651 | if ((!result.config.warning_range) && argv[index]) { |
631 | warning_range = argv[c++]; | 652 | result.config.warning_range = argv[index++]; |
632 | if ((! critical_range) && argv[c]) | 653 | } |
633 | critical_range = argv[c++]; | 654 | if ((!result.config.critical_range) && argv[index]) { |
634 | if (statopts == NULL && argv[c]) { | 655 | result.config.critical_range = argv[index++]; |
635 | xasprintf (&statopts, "%s", argv[c++]); | 656 | } |
636 | xasprintf (&fmt, _("%s%sSTATE = %s"), (fmt ? fmt : ""), (options ? ", " : ""), statopts); | 657 | if (result.config.statopts == NULL && argv[index]) { |
637 | options |= STAT; | 658 | xasprintf(&result.config.statopts, "%s", argv[index++]); |
659 | xasprintf(&result.config.fmt, _("%s%sSTATE = %s"), | ||
660 | (result.config.fmt ? result.config.fmt : ""), (result.config.options ? ", " : ""), | ||
661 | result.config.statopts); | ||
662 | result.config.options |= STAT; | ||
638 | } | 663 | } |
639 | 664 | ||
640 | /* this will abort in case of invalid ranges */ | 665 | /* this will abort in case of invalid ranges */ |
641 | set_thresholds (&procs_thresholds, warning_range, critical_range); | 666 | set_thresholds(&result.config.procs_thresholds, result.config.warning_range, |
667 | result.config.critical_range); | ||
642 | 668 | ||
643 | return validate_arguments (); | 669 | return validate_arguments(result); |
644 | } | 670 | } |
645 | 671 | ||
672 | check_procs_config_wrapper validate_arguments(check_procs_config_wrapper config_wrapper) { | ||
673 | if (config_wrapper.config.options == 0) { | ||
674 | config_wrapper.config.options = ALL; | ||
675 | } | ||
646 | 676 | ||
677 | if (config_wrapper.config.statopts == NULL) { | ||
678 | config_wrapper.config.statopts = strdup(""); | ||
679 | } | ||
647 | 680 | ||
648 | int | 681 | if (config_wrapper.config.prog == NULL) { |
649 | validate_arguments () | 682 | config_wrapper.config.prog = strdup(""); |
650 | { | 683 | } |
651 | if (options == 0) | ||
652 | options = ALL; | ||
653 | |||
654 | if (statopts==NULL) | ||
655 | statopts = strdup(""); | ||
656 | |||
657 | if (prog==NULL) | ||
658 | prog = strdup(""); | ||
659 | 684 | ||
660 | if (args==NULL) | 685 | if (config_wrapper.config.args == NULL) { |
661 | args = strdup(""); | 686 | config_wrapper.config.args = strdup(""); |
687 | } | ||
662 | 688 | ||
663 | if (fmt==NULL) | 689 | if (config_wrapper.config.fmt == NULL) { |
664 | fmt = strdup(""); | 690 | config_wrapper.config.fmt = strdup(""); |
691 | } | ||
665 | 692 | ||
666 | if (fails==NULL) | 693 | if (config_wrapper.config.fails == NULL) { |
667 | fails = strdup(""); | 694 | config_wrapper.config.fails = strdup(""); |
695 | } | ||
668 | 696 | ||
669 | return options; | 697 | // return options; |
698 | return config_wrapper; | ||
670 | } | 699 | } |
671 | 700 | ||
672 | |||
673 | /* convert the elapsed time to seconds */ | 701 | /* convert the elapsed time to seconds */ |
674 | int | 702 | int convert_to_seconds(char *etime, enum metric metric) { |
675 | convert_to_seconds(char *etime) { | 703 | int hyphcnt = 0; |
676 | 704 | int coloncnt = 0; | |
677 | char *ptr; | 705 | for (char *ptr = etime; *ptr != '\0'; ptr++) { |
678 | int total; | ||
679 | |||
680 | int hyphcnt; | ||
681 | int coloncnt; | ||
682 | int days; | ||
683 | int hours; | ||
684 | int minutes; | ||
685 | int seconds; | ||
686 | |||
687 | hyphcnt = 0; | ||
688 | coloncnt = 0; | ||
689 | days = 0; | ||
690 | hours = 0; | ||
691 | minutes = 0; | ||
692 | seconds = 0; | ||
693 | |||
694 | for (ptr = etime; *ptr != '\0'; ptr++) { | ||
695 | 706 | ||
696 | if (*ptr == '-') { | 707 | if (*ptr == '-') { |
697 | hyphcnt++; | 708 | hyphcnt++; |
@@ -703,9 +714,12 @@ convert_to_seconds(char *etime) { | |||
703 | } | 714 | } |
704 | } | 715 | } |
705 | 716 | ||
717 | int days = 0; | ||
718 | int hours = 0; | ||
719 | int minutes = 0; | ||
720 | int seconds = 0; | ||
706 | if (hyphcnt > 0) { | 721 | if (hyphcnt > 0) { |
707 | sscanf(etime, "%d-%d:%d:%d", | 722 | sscanf(etime, "%d-%d:%d:%d", &days, &hours, &minutes, &seconds); |
708 | &days, &hours, &minutes, &seconds); | ||
709 | /* linux 2.6.5/2.6.6 reporting some processes with infinite | 723 | /* linux 2.6.5/2.6.6 reporting some processes with infinite |
710 | * elapsed times for some reason */ | 724 | * elapsed times for some reason */ |
711 | if (days == 49710) { | 725 | if (days == 49710) { |
@@ -713,135 +727,129 @@ convert_to_seconds(char *etime) { | |||
713 | } | 727 | } |
714 | } else { | 728 | } else { |
715 | if (coloncnt == 2) { | 729 | if (coloncnt == 2) { |
716 | sscanf(etime, "%d:%d:%d", | 730 | sscanf(etime, "%d:%d:%d", &hours, &minutes, &seconds); |
717 | &hours, &minutes, &seconds); | ||
718 | } else if (coloncnt == 1) { | 731 | } else if (coloncnt == 1) { |
719 | sscanf(etime, "%d:%d", | 732 | sscanf(etime, "%d:%d", &minutes, &seconds); |
720 | &minutes, &seconds); | ||
721 | } | 733 | } |
722 | } | 734 | } |
723 | 735 | ||
724 | total = (days * 86400) + | 736 | int total = (days * 86400) + (hours * 3600) + (minutes * 60) + seconds; |
725 | (hours * 3600) + | ||
726 | (minutes * 60) + | ||
727 | seconds; | ||
728 | 737 | ||
729 | if (verbose >= 3 && metric == METRIC_ELAPSED) { | 738 | if (verbose >= 3 && metric == METRIC_ELAPSED) { |
730 | printf("seconds: %d\n", total); | 739 | printf("seconds: %d\n", total); |
731 | } | 740 | } |
732 | return total; | 741 | return total; |
733 | } | 742 | } |
734 | 743 | ||
735 | 744 | void print_help(void) { | |
736 | void | 745 | print_revision(progname, NP_VERSION); |
737 | print_help (void) | 746 | |
738 | { | 747 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
739 | print_revision (progname, NP_VERSION); | 748 | printf(COPYRIGHT, copyright, email); |
740 | 749 | ||
741 | printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | 750 | printf("%s\n", |
742 | printf (COPYRIGHT, copyright, email); | 751 | _("Checks all processes and generates WARNING or CRITICAL states if the specified")); |
743 | 752 | printf("%s\n", | |
744 | printf ("%s\n", _("Checks all processes and generates WARNING or CRITICAL states if the specified")); | 753 | _("metric is outside the required threshold ranges. The metric defaults to number")); |
745 | printf ("%s\n", _("metric is outside the required threshold ranges. The metric defaults to number")); | 754 | printf("%s\n", |
746 | printf ("%s\n", _("of processes. Search filters can be applied to limit the processes to check.")); | 755 | _("of processes. Search filters can be applied to limit the processes to check.")); |
747 | 756 | ||
748 | printf ("\n\n"); | 757 | printf("\n\n"); |
749 | 758 | ||
750 | printf ("%s\n", _("The parent process, check_procs itself and any child process of check_procs (ps)")); | 759 | printf("%s\n", |
751 | printf ("%s\n", _("are excluded from any checks to prevent false positives.")); | 760 | _("The parent process, check_procs itself and any child process of check_procs (ps)")); |
752 | 761 | printf("%s\n", _("are excluded from any checks to prevent false positives.")); | |
753 | printf ("\n\n"); | 762 | |
754 | 763 | printf("\n\n"); | |
755 | print_usage (); | 764 | |
756 | 765 | print_usage(); | |
757 | printf (UT_HELP_VRSN); | 766 | |
758 | printf (UT_EXTRA_OPTS); | 767 | printf(UT_HELP_VRSN); |
759 | printf (" %s\n", "-w, --warning=RANGE"); | 768 | printf(UT_EXTRA_OPTS); |
760 | printf (" %s\n", _("Generate warning state if metric is outside this range")); | 769 | printf(" %s\n", "-w, --warning=RANGE"); |
761 | printf (" %s\n", "-c, --critical=RANGE"); | 770 | printf(" %s\n", _("Generate warning state if metric is outside this range")); |
762 | printf (" %s\n", _("Generate critical state if metric is outside this range")); | 771 | printf(" %s\n", "-c, --critical=RANGE"); |
763 | printf (" %s\n", "-m, --metric=TYPE"); | 772 | printf(" %s\n", _("Generate critical state if metric is outside this range")); |
764 | printf (" %s\n", _("Check thresholds against metric. Valid types:")); | 773 | printf(" %s\n", "-m, --metric=TYPE"); |
765 | printf (" %s\n", _("PROCS - number of processes (default)")); | 774 | printf(" %s\n", _("Check thresholds against metric. Valid types:")); |
766 | printf (" %s\n", _("VSZ - virtual memory size")); | 775 | printf(" %s\n", _("PROCS - number of processes (default)")); |
767 | printf (" %s\n", _("RSS - resident set memory size")); | 776 | printf(" %s\n", _("VSZ - virtual memory size")); |
768 | printf (" %s\n", _("CPU - percentage CPU")); | 777 | printf(" %s\n", _("RSS - resident set memory size")); |
778 | printf(" %s\n", _("CPU - percentage CPU")); | ||
769 | /* only linux etime is support currently */ | 779 | /* only linux etime is support currently */ |
770 | #if defined( __linux__ ) | 780 | #if defined(__linux__) |
771 | printf (" %s\n", _("ELAPSED - time elapsed in seconds")); | 781 | printf(" %s\n", _("ELAPSED - time elapsed in seconds")); |
772 | #endif /* defined(__linux__) */ | 782 | #endif /* defined(__linux__) */ |
773 | printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 783 | printf(UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
774 | 784 | ||
775 | printf (" %s\n", "-v, --verbose"); | 785 | printf(" %s\n", "-v, --verbose"); |
776 | printf (" %s\n", _("Extra information. Up to 3 verbosity levels")); | 786 | printf(" %s\n", _("Extra information. Up to 3 verbosity levels")); |
777 | 787 | ||
778 | printf (" %s\n", "-T, --traditional"); | 788 | printf(" %s\n", "-T, --traditional"); |
779 | printf (" %s\n", _("Filter own process the traditional way by PID instead of /proc/pid/exe")); | 789 | printf(" %s\n", _("Filter own process the traditional way by PID instead of /proc/pid/exe")); |
780 | 790 | ||
781 | printf ("\n"); | 791 | printf("\n"); |
782 | printf ("%s\n", "Filters:"); | 792 | printf("%s\n", "Filters:"); |
783 | printf (" %s\n", "-s, --state=STATUSFLAGS"); | 793 | printf(" %s\n", "-s, --state=STATUSFLAGS"); |
784 | printf (" %s\n", _("Only scan for processes that have, in the output of `ps`, one or")); | 794 | printf(" %s\n", _("Only scan for processes that have, in the output of `ps`, one or")); |
785 | printf (" %s\n", _("more of the status flags you specify (for example R, Z, S, RS,")); | 795 | printf(" %s\n", _("more of the status flags you specify (for example R, Z, S, RS,")); |
786 | printf (" %s\n", _("RSZDT, plus others based on the output of your 'ps' command).")); | 796 | printf(" %s\n", _("RSZDT, plus others based on the output of your 'ps' command).")); |
787 | printf (" %s\n", "-p, --ppid=PPID"); | 797 | printf(" %s\n", "-p, --ppid=PPID"); |
788 | printf (" %s\n", _("Only scan for children of the parent process ID indicated.")); | 798 | printf(" %s\n", _("Only scan for children of the parent process ID indicated.")); |
789 | printf (" %s\n", "-z, --vsz=VSZ"); | 799 | printf(" %s\n", "-z, --vsz=VSZ"); |
790 | printf (" %s\n", _("Only scan for processes with VSZ higher than indicated.")); | 800 | printf(" %s\n", _("Only scan for processes with VSZ higher than indicated.")); |
791 | printf (" %s\n", "-r, --rss=RSS"); | 801 | printf(" %s\n", "-r, --rss=RSS"); |
792 | printf (" %s\n", _("Only scan for processes with RSS higher than indicated.")); | 802 | printf(" %s\n", _("Only scan for processes with RSS higher than indicated.")); |
793 | printf (" %s\n", "-P, --pcpu=PCPU"); | 803 | printf(" %s\n", "-P, --pcpu=PCPU"); |
794 | printf (" %s\n", _("Only scan for processes with PCPU higher than indicated.")); | 804 | printf(" %s\n", _("Only scan for processes with PCPU higher than indicated.")); |
795 | printf (" %s\n", "-u, --user=USER"); | 805 | printf(" %s\n", "-u, --user=USER"); |
796 | printf (" %s\n", _("Only scan for processes with user name or ID indicated.")); | 806 | printf(" %s\n", _("Only scan for processes with user name or ID indicated.")); |
797 | printf (" %s\n", "-a, --argument-array=STRING"); | 807 | printf(" %s\n", "-a, --argument-array=STRING"); |
798 | printf (" %s\n", _("Only scan for processes with args that contain STRING.")); | 808 | printf(" %s\n", _("Only scan for processes with args that contain STRING.")); |
799 | printf (" %s\n", "--ereg-argument-array=STRING"); | 809 | printf(" %s\n", "--ereg-argument-array=STRING"); |
800 | printf (" %s\n", _("Only scan for processes with args that contain the regex STRING.")); | 810 | printf(" %s\n", _("Only scan for processes with args that contain the regex STRING.")); |
801 | printf (" %s\n", "-C, --command=COMMAND"); | 811 | printf(" %s\n", "-C, --command=COMMAND"); |
802 | printf (" %s\n", _("Only scan for exact matches of COMMAND (without path).")); | 812 | printf(" %s\n", _("Only scan for exact matches of COMMAND (without path).")); |
803 | printf (" %s\n", "-X, --exclude-process"); | 813 | printf(" %s\n", "-X, --exclude-process"); |
804 | printf (" %s\n", _("Exclude processes which match this comma separated list")); | 814 | printf(" %s\n", _("Exclude processes which match this comma separated list")); |
805 | printf (" %s\n", "-k, --no-kthreads"); | 815 | printf(" %s\n", "-k, --no-kthreads"); |
806 | printf (" %s\n", _("Only scan for non kernel threads (works on Linux only).")); | 816 | printf(" %s\n", _("Only scan for non kernel threads (works on Linux only).")); |
807 | 817 | ||
808 | printf(_("\n\ | 818 | printf(_("\n\ |
809 | RANGEs are specified 'min:max' or 'min:' or ':max' (or 'max'). If\n\ | 819 | RANGEs are specified 'min:max' or 'min:' or ':max' (or 'max'). If\n\ |
810 | specified 'max:min', a warning status will be generated if the\n\ | 820 | specified 'max:min', a warning status will be generated if the\n\ |
811 | count is inside the specified range\n\n")); | 821 | count is inside the specified range\n\n")); |
812 | 822 | ||
813 | printf(_("\ | 823 | printf(_("\ |
814 | This plugin checks the number of currently running processes and\n\ | 824 | This plugin checks the number of currently running processes and\n\ |
815 | generates WARNING or CRITICAL states if the process count is outside\n\ | 825 | generates WARNING or CRITICAL states if the process count is outside\n\ |
816 | the specified threshold ranges. The process count can be filtered by\n\ | 826 | the specified threshold ranges. The process count can be filtered by\n\ |
817 | process owner, parent process PID, current state (e.g., 'Z'), or may\n\ | 827 | process owner, parent process PID, current state (e.g., 'Z'), or may\n\ |
818 | be the total number of running processes\n\n")); | 828 | be the total number of running processes\n\n")); |
819 | 829 | ||
820 | printf ("%s\n", _("Examples:")); | 830 | printf("%s\n", _("Examples:")); |
821 | printf (" %s\n", "check_procs -w 2:2 -c 2:1024 -C portsentry"); | 831 | printf(" %s\n", "check_procs -w 2:2 -c 2:1024 -C portsentry"); |
822 | printf (" %s\n", _("Warning if not two processes with command name portsentry.")); | 832 | printf(" %s\n", _("Warning if not two processes with command name portsentry.")); |
823 | printf (" %s\n\n", _("Critical if < 2 or > 1024 processes")); | 833 | printf(" %s\n\n", _("Critical if < 2 or > 1024 processes")); |
824 | printf (" %s\n", "check_procs -c 1: -C sshd"); | 834 | printf(" %s\n", "check_procs -c 1: -C sshd"); |
825 | printf (" %s\n", _("Critical if not at least 1 process with command sshd")); | 835 | printf(" %s\n", _("Critical if not at least 1 process with command sshd")); |
826 | printf (" %s\n", "check_procs -w 1024 -c 1: -C sshd"); | 836 | printf(" %s\n", "check_procs -w 1024 -c 1: -C sshd"); |
827 | printf (" %s\n", _("Warning if > 1024 processes with command name sshd.")); | 837 | printf(" %s\n", _("Warning if > 1024 processes with command name sshd.")); |
828 | printf (" %s\n\n", _("Critical if < 1 processes with command name sshd.")); | 838 | printf(" %s\n\n", _("Critical if < 1 processes with command name sshd.")); |
829 | printf (" %s\n", "check_procs -w 10 -a '/usr/local/bin/perl' -u root"); | 839 | printf(" %s\n", "check_procs -w 10 -a '/usr/local/bin/perl' -u root"); |
830 | printf (" %s\n", _("Warning alert if > 10 processes with command arguments containing")); | 840 | printf(" %s\n", _("Warning alert if > 10 processes with command arguments containing")); |
831 | printf (" %s\n\n", _("'/usr/local/bin/perl' and owned by root")); | 841 | printf(" %s\n\n", _("'/usr/local/bin/perl' and owned by root")); |
832 | printf (" %s\n", "check_procs -w 50000 -c 100000 --metric=VSZ"); | 842 | printf(" %s\n", "check_procs -w 50000 -c 100000 --metric=VSZ"); |
833 | printf (" %s\n\n", _("Alert if VSZ of any processes over 50K or 100K")); | 843 | printf(" %s\n\n", _("Alert if VSZ of any processes over 50K or 100K")); |
834 | printf (" %s\n", "check_procs -w 10 -c 20 --metric=CPU"); | 844 | printf(" %s\n", "check_procs -w 10 -c 20 --metric=CPU"); |
835 | printf (" %s\n", _("Alert if CPU of any processes over 10%% or 20%%")); | 845 | printf(" %s\n", _("Alert if CPU of any processes over 10%% or 20%%")); |
836 | 846 | ||
837 | printf (UT_SUPPORT); | 847 | printf(UT_SUPPORT); |
838 | } | 848 | } |
839 | 849 | ||
840 | void | 850 | void print_usage(void) { |
841 | print_usage (void) | 851 | printf("%s\n", _("Usage:")); |
842 | { | 852 | printf("%s -w <range> -c <range> [-m metric] [-s state] [-p ppid]\n", progname); |
843 | printf ("%s\n", _("Usage:")); | 853 | printf(" [-u user] [-r rss] [-z vsz] [-P %%cpu] [-a argument-array]\n"); |
844 | printf ("%s -w <range> -c <range> [-m metric] [-s state] [-p ppid]\n", progname); | 854 | printf(" [-C command] [-X process_to_exclude] [-k] [-t timeout] [-v]\n"); |
845 | printf (" [-u user] [-r rss] [-z vsz] [-P %%cpu] [-a argument-array]\n"); | ||
846 | printf (" [-C command] [-X process_to_exclude] [-k] [-t timeout] [-v]\n"); | ||
847 | } | 855 | } |
diff --git a/plugins/check_procs.d/config.h b/plugins/check_procs.d/config.h new file mode 100644 index 00000000..e32ca066 --- /dev/null +++ b/plugins/check_procs.d/config.h | |||
@@ -0,0 +1,75 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include "regex.h" | ||
5 | #include "thresholds.h" | ||
6 | #include <stddef.h> | ||
7 | #include <string.h> | ||
8 | #include <sys/types.h> | ||
9 | |||
10 | enum metric { | ||
11 | METRIC_PROCS, | ||
12 | METRIC_VSZ, | ||
13 | METRIC_RSS, | ||
14 | METRIC_CPU, | ||
15 | METRIC_ELAPSED | ||
16 | }; | ||
17 | |||
18 | typedef struct { | ||
19 | int options; /* bitmask of filter criteria to test against */ | ||
20 | enum metric metric; | ||
21 | char *metric_name; | ||
22 | char *input_filename; | ||
23 | char *prog; | ||
24 | char *args; | ||
25 | char *fmt; | ||
26 | char *fails; | ||
27 | char *exclude_progs; | ||
28 | char **exclude_progs_arr; | ||
29 | char exclude_progs_counter; | ||
30 | regex_t re_args; | ||
31 | |||
32 | bool kthread_filter; | ||
33 | bool usepid; /* whether to test for pid or /proc/pid/exe */ | ||
34 | uid_t uid; | ||
35 | pid_t ppid; | ||
36 | int vsz; | ||
37 | int rss; | ||
38 | float pcpu; | ||
39 | char *statopts; | ||
40 | |||
41 | char *warning_range; | ||
42 | char *critical_range; | ||
43 | thresholds *procs_thresholds; | ||
44 | } check_procs_config; | ||
45 | |||
46 | check_procs_config check_procs_config_init() { | ||
47 | check_procs_config tmp = { | ||
48 | .options = 0, | ||
49 | .metric = METRIC_PROCS, | ||
50 | .metric_name = strdup("PROCS"), | ||
51 | .input_filename = NULL, | ||
52 | .prog = NULL, | ||
53 | .args = NULL, | ||
54 | .fmt = NULL, | ||
55 | .fails = NULL, | ||
56 | .exclude_progs = NULL, | ||
57 | .exclude_progs_arr = NULL, | ||
58 | .exclude_progs_counter = 0, | ||
59 | .re_args = {0}, | ||
60 | |||
61 | .kthread_filter = false, | ||
62 | .usepid = false, | ||
63 | .uid = 0, | ||
64 | .ppid = 0, | ||
65 | .vsz = 0, | ||
66 | .rss = 0, | ||
67 | .pcpu = 0, | ||
68 | .statopts = NULL, | ||
69 | |||
70 | .warning_range = NULL, | ||
71 | .critical_range = NULL, | ||
72 | .procs_thresholds = NULL, | ||
73 | }; | ||
74 | return tmp; | ||
75 | } | ||
diff --git a/plugins/check_radius.c b/plugins/check_radius.c index cc846709..d26f7cf3 100644 --- a/plugins/check_radius.c +++ b/plugins/check_radius.c | |||
@@ -56,7 +56,8 @@ static check_radius_config_wrapper process_arguments(int /*argc*/, char ** /*arg | |||
56 | static void print_help(void); | 56 | static void print_help(void); |
57 | void print_usage(void); | 57 | void print_usage(void); |
58 | 58 | ||
59 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) | 59 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || \ |
60 | defined(HAVE_LIBRADCLI) | ||
60 | # define my_rc_conf_str(a) rc_conf_str(rch, a) | 61 | # define my_rc_conf_str(a) rc_conf_str(rch, a) |
61 | # if defined(HAVE_LIBRADCLI) | 62 | # if defined(HAVE_LIBRADCLI) |
62 | # define my_rc_send_server(a, b) rc_send_server(rch, a, b, AUTH) | 63 | # define my_rc_send_server(a, b) rc_send_server(rch, a, b, AUTH) |
@@ -157,12 +158,14 @@ int main(int argc, char **argv) { | |||
157 | 158 | ||
158 | check_radius_config config = tmp_config.config; | 159 | check_radius_config config = tmp_config.config; |
159 | 160 | ||
160 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) | 161 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || \ |
162 | defined(HAVE_LIBRADCLI) | ||
161 | rc_handle *rch = NULL; | 163 | rc_handle *rch = NULL; |
162 | #endif | 164 | #endif |
163 | 165 | ||
164 | char *str = strdup("dictionary"); | 166 | char *str = strdup("dictionary"); |
165 | if ((config.config_file && my_rc_read_config(config.config_file, &rch)) || my_rc_read_dictionary(my_rc_conf_str(str))) { | 167 | if ((config.config_file && my_rc_read_config(config.config_file, &rch)) || |
168 | my_rc_read_dictionary(my_rc_conf_str(str))) { | ||
166 | die(STATE_UNKNOWN, _("Config file error\n")); | 169 | die(STATE_UNKNOWN, _("Config file error\n")); |
167 | } | 170 | } |
168 | 171 | ||
@@ -200,7 +203,8 @@ int main(int argc, char **argv) { | |||
200 | die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); | 203 | die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); |
201 | } | 204 | } |
202 | 205 | ||
203 | my_rc_buildreq(&data, PW_ACCESS_REQUEST, config.server, config.port, (int)timeout_interval, config.retries); | 206 | my_rc_buildreq(&data, PW_ACCESS_REQUEST, config.server, config.port, (int)timeout_interval, |
207 | config.retries); | ||
204 | 208 | ||
205 | #ifdef RC_BUFFER_LEN | 209 | #ifdef RC_BUFFER_LEN |
206 | char msg[RC_BUFFER_LEN]; | 210 | char msg[RC_BUFFER_LEN]; |
@@ -251,13 +255,14 @@ int main(int argc, char **argv) { | |||
251 | 255 | ||
252 | /* process command-line arguments */ | 256 | /* process command-line arguments */ |
253 | check_radius_config_wrapper process_arguments(int argc, char **argv) { | 257 | check_radius_config_wrapper process_arguments(int argc, char **argv) { |
254 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'P'}, | 258 | static struct option longopts[] = { |
255 | {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, | 259 | {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'P'}, |
256 | {"nas-id", required_argument, 0, 'n'}, {"nas-ip-address", required_argument, 0, 'N'}, | 260 | {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, |
257 | {"filename", required_argument, 0, 'F'}, {"expect", required_argument, 0, 'e'}, | 261 | {"nas-id", required_argument, 0, 'n'}, {"nas-ip-address", required_argument, 0, 'N'}, |
258 | {"retries", required_argument, 0, 'r'}, {"timeout", required_argument, 0, 't'}, | 262 | {"filename", required_argument, 0, 'F'}, {"expect", required_argument, 0, 'e'}, |
259 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 263 | {"retries", required_argument, 0, 'r'}, {"timeout", required_argument, 0, 't'}, |
260 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | 264 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
265 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | ||
261 | 266 | ||
262 | check_radius_config_wrapper result = { | 267 | check_radius_config_wrapper result = { |
263 | .errorcode = OK, | 268 | .errorcode = OK, |
@@ -397,9 +402,11 @@ void print_help(void) { | |||
397 | printf("%s\n", _("name and password. A configuration file must be present. The format of")); | 402 | printf("%s\n", _("name and password. A configuration file must be present. The format of")); |
398 | printf("%s\n", _("the configuration file is described in the radiusclient library sources.")); | 403 | printf("%s\n", _("the configuration file is described in the radiusclient library sources.")); |
399 | printf("%s\n", _("The password option presents a substantial security issue because the")); | 404 | printf("%s\n", _("The password option presents a substantial security issue because the")); |
400 | printf("%s\n", _("password can possibly be determined by careful watching of the command line")); | 405 | printf("%s\n", |
406 | _("password can possibly be determined by careful watching of the command line")); | ||
401 | printf("%s\n", _("in a process listing. This risk is exacerbated because the plugin will")); | 407 | printf("%s\n", _("in a process listing. This risk is exacerbated because the plugin will")); |
402 | printf("%s\n", _("typically be executed at regular predictable intervals. Please be sure that")); | 408 | printf("%s\n", |
409 | _("typically be executed at regular predictable intervals. Please be sure that")); | ||
403 | printf("%s\n", _("the password used does not allow access to sensitive system resources.")); | 410 | printf("%s\n", _("the password used does not allow access to sensitive system resources.")); |
404 | 411 | ||
405 | printf(UT_SUPPORT); | 412 | printf(UT_SUPPORT); |
@@ -414,7 +421,8 @@ void print_usage(void) { | |||
414 | } | 421 | } |
415 | 422 | ||
416 | int my_rc_read_config(char *config_file_name, rc_handle **rch) { | 423 | int my_rc_read_config(char *config_file_name, rc_handle **rch) { |
417 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) | 424 | #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || \ |
425 | defined(HAVE_LIBRADCLI) | ||
418 | *rch = rc_read_config(config_file_name); | 426 | *rch = rc_read_config(config_file_name); |
419 | return (rch == NULL) ? 1 : 0; | 427 | return (rch == NULL) ? 1 : 0; |
420 | #else | 428 | #else |
diff --git a/plugins/check_real.c b/plugins/check_real.c index ec0928ed..66d07f8f 100644 --- a/plugins/check_real.c +++ b/plugins/check_real.c | |||
@@ -78,7 +78,8 @@ int main(int argc, char **argv) { | |||
78 | /* try to connect to the host at the given port number */ | 78 | /* try to connect to the host at the given port number */ |
79 | int socket; | 79 | int socket; |
80 | if (my_tcp_connect(config.server_address, config.server_port, &socket) != STATE_OK) { | 80 | if (my_tcp_connect(config.server_address, config.server_port, &socket) != STATE_OK) { |
81 | die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), config.server_address, config.server_port); | 81 | die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), config.server_address, |
82 | config.server_port); | ||
82 | } | 83 | } |
83 | 84 | ||
84 | /* Part I - Server Check */ | 85 | /* Part I - Server Check */ |
@@ -166,7 +167,8 @@ int main(int argc, char **argv) { | |||
166 | /* Part I - Server Check */ | 167 | /* Part I - Server Check */ |
167 | 168 | ||
168 | /* send the DESCRIBE request */ | 169 | /* send the DESCRIBE request */ |
169 | sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", config.host_name, config.server_port, config.server_url); | 170 | sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", config.host_name, |
171 | config.server_port, config.server_url); | ||
170 | 172 | ||
171 | ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0); | 173 | ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0); |
172 | if (sent_bytes == -1) { | 174 | if (sent_bytes == -1) { |
@@ -200,7 +202,8 @@ int main(int argc, char **argv) { | |||
200 | if (config.server_port == PORT) { | 202 | if (config.server_port == PORT) { |
201 | printf("%s\n", _("Invalid REAL response received from host")); | 203 | printf("%s\n", _("Invalid REAL response received from host")); |
202 | } else { | 204 | } else { |
203 | printf(_("Invalid REAL response received from host on port %d\n"), config.server_port); | 205 | printf(_("Invalid REAL response received from host on port %d\n"), |
206 | config.server_port); | ||
204 | } | 207 | } |
205 | } else { | 208 | } else { |
206 | 209 | ||
@@ -256,7 +259,8 @@ int main(int argc, char **argv) { | |||
256 | } | 259 | } |
257 | 260 | ||
258 | /* Put some HTML in here to create a dynamic link */ | 261 | /* Put some HTML in here to create a dynamic link */ |
259 | printf(_("REAL %s - %d second response time\n"), state_text(result), (int)(end_time - start_time)); | 262 | printf(_("REAL %s - %d second response time\n"), state_text(result), |
263 | (int)(end_time - start_time)); | ||
260 | } else { | 264 | } else { |
261 | printf("%s\n", status_line); | 265 | printf("%s\n", status_line); |
262 | } | 266 | } |
@@ -272,12 +276,13 @@ int main(int argc, char **argv) { | |||
272 | 276 | ||
273 | /* process command-line arguments */ | 277 | /* process command-line arguments */ |
274 | check_real_config_wrapper process_arguments(int argc, char **argv) { | 278 | check_real_config_wrapper process_arguments(int argc, char **argv) { |
275 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"IPaddress", required_argument, 0, 'I'}, | 279 | static struct option longopts[] = { |
276 | {"expect", required_argument, 0, 'e'}, {"url", required_argument, 0, 'u'}, | 280 | {"hostname", required_argument, 0, 'H'}, {"IPaddress", required_argument, 0, 'I'}, |
277 | {"port", required_argument, 0, 'p'}, {"critical", required_argument, 0, 'c'}, | 281 | {"expect", required_argument, 0, 'e'}, {"url", required_argument, 0, 'u'}, |
278 | {"warning", required_argument, 0, 'w'}, {"timeout", required_argument, 0, 't'}, | 282 | {"port", required_argument, 0, 'p'}, {"critical", required_argument, 0, 'c'}, |
279 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 283 | {"warning", required_argument, 0, 'w'}, {"timeout", required_argument, 0, 't'}, |
280 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | 284 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
285 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | ||
281 | 286 | ||
282 | check_real_config_wrapper result = { | 287 | check_real_config_wrapper result = { |
283 | .errorcode = OK, | 288 | .errorcode = OK, |
@@ -427,7 +432,8 @@ void print_help(void) { | |||
427 | printf("%s\n", _("This plugin will attempt to open an RTSP connection with the host.")); | 432 | printf("%s\n", _("This plugin will attempt to open an RTSP connection with the host.")); |
428 | printf("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); | 433 | printf("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); |
429 | printf("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,")); | 434 | printf("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,")); |
430 | printf("%s\n", _("but incorrect response messages from the host result in STATE_WARNING return")); | 435 | printf("%s\n", |
436 | _("but incorrect response messages from the host result in STATE_WARNING return")); | ||
431 | printf("%s\n", _("values.")); | 437 | printf("%s\n", _("values.")); |
432 | 438 | ||
433 | printf(UT_SUPPORT); | 439 | printf(UT_SUPPORT); |
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c index 44b735f9..83ad575c 100644 --- a/plugins/check_smtp.c +++ b/plugins/check_smtp.c | |||
@@ -58,7 +58,8 @@ typedef struct { | |||
58 | } check_smtp_config_wrapper; | 58 | } check_smtp_config_wrapper; |
59 | static check_smtp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | 59 | static check_smtp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
60 | 60 | ||
61 | int my_recv(check_smtp_config config, void *buf, int num, int socket_descriptor, bool ssl_established) { | 61 | int my_recv(check_smtp_config config, void *buf, int num, int socket_descriptor, |
62 | bool ssl_established) { | ||
62 | #ifdef HAVE_SSL | 63 | #ifdef HAVE_SSL |
63 | if ((config.use_starttls || config.use_ssl) && ssl_established) { | 64 | if ((config.use_starttls || config.use_ssl) && ssl_established) { |
64 | return np_net_ssl_read(buf, num); | 65 | return np_net_ssl_read(buf, num); |
@@ -69,7 +70,8 @@ int my_recv(check_smtp_config config, void *buf, int num, int socket_descriptor, | |||
69 | #endif | 70 | #endif |
70 | } | 71 | } |
71 | 72 | ||
72 | int my_send(check_smtp_config config, void *buf, int num, int socket_descriptor, bool ssl_established) { | 73 | int my_send(check_smtp_config config, void *buf, int num, int socket_descriptor, |
74 | bool ssl_established) { | ||
73 | #ifdef HAVE_SSL | 75 | #ifdef HAVE_SSL |
74 | if ((config.use_starttls || config.use_ssl) && ssl_established) { | 76 | if ((config.use_starttls || config.use_ssl) && ssl_established) { |
75 | 77 | ||
@@ -83,10 +85,12 @@ int my_send(check_smtp_config config, void *buf, int num, int socket_descriptor, | |||
83 | 85 | ||
84 | static void print_help(void); | 86 | static void print_help(void); |
85 | void print_usage(void); | 87 | void print_usage(void); |
86 | static char *smtp_quit(check_smtp_config /*config*/, char /*buffer*/[MAX_INPUT_BUFFER], int /*socket_descriptor*/, | 88 | static char *smtp_quit(check_smtp_config /*config*/, char /*buffer*/[MAX_INPUT_BUFFER], |
87 | bool /*ssl_established*/); | 89 | int /*socket_descriptor*/, bool /*ssl_established*/); |
88 | static int recvline(char * /*buf*/, size_t /*bufsize*/, check_smtp_config /*config*/, int /*socket_descriptor*/, bool /*ssl_established*/); | 90 | static int recvline(char * /*buf*/, size_t /*bufsize*/, check_smtp_config /*config*/, |
89 | static int recvlines(check_smtp_config /*config*/, char * /*buf*/, size_t /*bufsize*/, int /*socket_descriptor*/, bool /*ssl_established*/); | 91 | int /*socket_descriptor*/, bool /*ssl_established*/); |
92 | static int recvlines(check_smtp_config /*config*/, char * /*buf*/, size_t /*bufsize*/, | ||
93 | int /*socket_descriptor*/, bool /*ssl_established*/); | ||
90 | static int my_close(int /*socket_descriptor*/); | 94 | static int my_close(int /*socket_descriptor*/); |
91 | 95 | ||
92 | static int verbose = 0; | 96 | static int verbose = 0; |
@@ -158,7 +162,8 @@ int main(int argc, char **argv) { | |||
158 | 162 | ||
159 | int socket_descriptor = 0; | 163 | int socket_descriptor = 0; |
160 | /* try to connect to the host at the given port number */ | 164 | /* try to connect to the host at the given port number */ |
161 | mp_state_enum result = my_tcp_connect(config.server_address, config.server_port, &socket_descriptor); | 165 | mp_state_enum result = |
166 | my_tcp_connect(config.server_address, config.server_port, &socket_descriptor); | ||
162 | 167 | ||
163 | char *error_msg = ""; | 168 | char *error_msg = ""; |
164 | char buffer[MAX_INPUT_BUFFER]; | 169 | char buffer[MAX_INPUT_BUFFER]; |
@@ -174,7 +179,8 @@ int main(int argc, char **argv) { | |||
174 | 179 | ||
175 | #ifdef HAVE_SSL | 180 | #ifdef HAVE_SSL |
176 | if (config.use_ssl) { | 181 | if (config.use_ssl) { |
177 | result = np_net_ssl_init_with_hostname(socket_descriptor, (config.use_sni ? config.server_address : NULL)); | 182 | result = np_net_ssl_init_with_hostname(socket_descriptor, |
183 | (config.use_sni ? config.server_address : NULL)); | ||
178 | if (result != STATE_OK) { | 184 | if (result != STATE_OK) { |
179 | printf(_("CRITICAL - Cannot create SSL context.\n")); | 185 | printf(_("CRITICAL - Cannot create SSL context.\n")); |
180 | close(socket_descriptor); | 186 | close(socket_descriptor); |
@@ -223,14 +229,16 @@ int main(int argc, char **argv) { | |||
223 | /* send the STARTTLS command */ | 229 | /* send the STARTTLS command */ |
224 | send(socket_descriptor, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0); | 230 | send(socket_descriptor, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0); |
225 | 231 | ||
226 | recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established); /* wait for it */ | 232 | recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, |
233 | ssl_established); /* wait for it */ | ||
227 | if (!strstr(buffer, SMTP_EXPECT)) { | 234 | if (!strstr(buffer, SMTP_EXPECT)) { |
228 | printf(_("Server does not support STARTTLS\n")); | 235 | printf(_("Server does not support STARTTLS\n")); |
229 | smtp_quit(config, buffer, socket_descriptor, ssl_established); | 236 | smtp_quit(config, buffer, socket_descriptor, ssl_established); |
230 | exit(STATE_UNKNOWN); | 237 | exit(STATE_UNKNOWN); |
231 | } | 238 | } |
232 | 239 | ||
233 | result = np_net_ssl_init_with_hostname(socket_descriptor, (config.use_sni ? config.server_address : NULL)); | 240 | result = np_net_ssl_init_with_hostname(socket_descriptor, |
241 | (config.use_sni ? config.server_address : NULL)); | ||
234 | if (result != STATE_OK) { | 242 | if (result != STATE_OK) { |
235 | printf(_("CRITICAL - Cannot create SSL context.\n")); | 243 | printf(_("CRITICAL - Cannot create SSL context.\n")); |
236 | close(socket_descriptor); | 244 | close(socket_descriptor); |
@@ -251,7 +259,8 @@ int main(int argc, char **argv) { | |||
251 | * reason, some MTAs will not allow an AUTH LOGIN command before | 259 | * reason, some MTAs will not allow an AUTH LOGIN command before |
252 | * we resent EHLO via TLS. | 260 | * we resent EHLO via TLS. |
253 | */ | 261 | */ |
254 | if (my_send(config, helocmd, strlen(helocmd), socket_descriptor, ssl_established) <= 0) { | 262 | if (my_send(config, helocmd, strlen(helocmd), socket_descriptor, ssl_established) <= |
263 | 0) { | ||
255 | printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS.")); | 264 | printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS.")); |
256 | my_close(socket_descriptor); | 265 | my_close(socket_descriptor); |
257 | exit(STATE_UNKNOWN); | 266 | exit(STATE_UNKNOWN); |
@@ -261,7 +270,8 @@ int main(int argc, char **argv) { | |||
261 | printf(_("sent %s"), helocmd); | 270 | printf(_("sent %s"), helocmd); |
262 | } | 271 | } |
263 | 272 | ||
264 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { | 273 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= |
274 | 0) { | ||
265 | printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS.")); | 275 | printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS.")); |
266 | my_close(socket_descriptor); | 276 | my_close(socket_descriptor); |
267 | exit(STATE_UNKNOWN); | 277 | exit(STATE_UNKNOWN); |
@@ -273,7 +283,8 @@ int main(int argc, char **argv) { | |||
273 | 283 | ||
274 | # ifdef USE_OPENSSL | 284 | # ifdef USE_OPENSSL |
275 | if (config.check_cert) { | 285 | if (config.check_cert) { |
276 | result = np_net_ssl_check_cert(config.days_till_exp_warn, config.days_till_exp_crit); | 286 | result = |
287 | np_net_ssl_check_cert(config.days_till_exp_warn, config.days_till_exp_crit); | ||
277 | smtp_quit(config, buffer, socket_descriptor, ssl_established); | 288 | smtp_quit(config, buffer, socket_descriptor, ssl_established); |
278 | my_close(socket_descriptor); | 289 | my_close(socket_descriptor); |
279 | exit(result); | 290 | exit(result); |
@@ -296,14 +307,17 @@ int main(int argc, char **argv) { | |||
296 | if (config.server_port == SMTP_PORT) { | 307 | if (config.server_port == SMTP_PORT) { |
297 | printf(_("Invalid SMTP response received from host: %s\n"), server_response); | 308 | printf(_("Invalid SMTP response received from host: %s\n"), server_response); |
298 | } else { | 309 | } else { |
299 | printf(_("Invalid SMTP response received from host on port %d: %s\n"), config.server_port, server_response); | 310 | printf(_("Invalid SMTP response received from host on port %d: %s\n"), |
311 | config.server_port, server_response); | ||
300 | } | 312 | } |
301 | exit(STATE_WARNING); | 313 | exit(STATE_WARNING); |
302 | } | 314 | } |
303 | 315 | ||
304 | if (config.send_mail_from) { | 316 | if (config.send_mail_from) { |
305 | my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); | 317 | my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); |
306 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 && verbose) { | 318 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= |
319 | 1 && | ||
320 | verbose) { | ||
307 | printf("%s", buffer); | 321 | printf("%s", buffer); |
308 | } | 322 | } |
309 | } | 323 | } |
@@ -312,7 +326,9 @@ int main(int argc, char **argv) { | |||
312 | while (counter < config.ncommands) { | 326 | while (counter < config.ncommands) { |
313 | xasprintf(&cmd_str, "%s%s", config.commands[counter], "\r\n"); | 327 | xasprintf(&cmd_str, "%s%s", config.commands[counter], "\r\n"); |
314 | my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); | 328 | my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); |
315 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 && verbose) { | 329 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= |
330 | 1 && | ||
331 | verbose) { | ||
316 | printf("%s", buffer); | 332 | printf("%s", buffer); |
317 | } | 333 | } |
318 | strip(buffer); | 334 | strip(buffer); |
@@ -334,7 +350,8 @@ int main(int argc, char **argv) { | |||
334 | result = STATE_OK; | 350 | result = STATE_OK; |
335 | } else if (excode == REG_NOMATCH) { | 351 | } else if (excode == REG_NOMATCH) { |
336 | result = STATE_WARNING; | 352 | result = STATE_WARNING; |
337 | printf(_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text(result), buffer, config.commands[counter]); | 353 | printf(_("SMTP %s - Invalid response '%s' to command '%s'\n"), |
354 | state_text(result), buffer, config.commands[counter]); | ||
338 | } else { | 355 | } else { |
339 | regerror(excode, &preg, errbuf, MAX_INPUT_BUFFER); | 356 | regerror(excode, &preg, errbuf, MAX_INPUT_BUFFER); |
340 | printf(_("Execute Error: %s\n"), errbuf); | 357 | printf(_("Execute Error: %s\n"), errbuf); |
@@ -361,12 +378,14 @@ int main(int argc, char **argv) { | |||
361 | } | 378 | } |
362 | 379 | ||
363 | /* send AUTH LOGIN */ | 380 | /* send AUTH LOGIN */ |
364 | my_send(config, SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN), socket_descriptor, ssl_established); | 381 | my_send(config, SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN), socket_descriptor, |
382 | ssl_established); | ||
365 | if (verbose) { | 383 | if (verbose) { |
366 | printf(_("sent %s\n"), "AUTH LOGIN"); | 384 | printf(_("sent %s\n"), "AUTH LOGIN"); |
367 | } | 385 | } |
368 | 386 | ||
369 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { | 387 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, |
388 | ssl_established)) <= 0) { | ||
370 | xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, ")); | 389 | xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, ")); |
371 | result = STATE_WARNING; | 390 | result = STATE_WARNING; |
372 | break; | 391 | break; |
@@ -389,7 +408,8 @@ int main(int argc, char **argv) { | |||
389 | printf(_("sent %s\n"), abuf); | 408 | printf(_("sent %s\n"), abuf); |
390 | } | 409 | } |
391 | 410 | ||
392 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { | 411 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, |
412 | ssl_established)) <= 0) { | ||
393 | result = STATE_CRITICAL; | 413 | result = STATE_CRITICAL; |
394 | xasprintf(&error_msg, _("recv() failed after sending authuser, ")); | 414 | xasprintf(&error_msg, _("recv() failed after sending authuser, ")); |
395 | break; | 415 | break; |
@@ -409,7 +429,8 @@ int main(int argc, char **argv) { | |||
409 | if (verbose) { | 429 | if (verbose) { |
410 | printf(_("sent %s\n"), abuf); | 430 | printf(_("sent %s\n"), abuf); |
411 | } | 431 | } |
412 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { | 432 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, |
433 | ssl_established)) <= 0) { | ||
413 | result = STATE_CRITICAL; | 434 | result = STATE_CRITICAL; |
414 | xasprintf(&error_msg, _("recv() failed after sending authpass, ")); | 435 | xasprintf(&error_msg, _("recv() failed after sending authpass, ")); |
415 | break; | 436 | break; |
@@ -451,10 +472,10 @@ int main(int argc, char **argv) { | |||
451 | } | 472 | } |
452 | } | 473 | } |
453 | 474 | ||
454 | printf(_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), state_text(result), error_msg, elapsed_time, verbose ? ", " : "", | 475 | printf(_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), state_text(result), error_msg, |
455 | verbose ? buffer : "", | 476 | elapsed_time, verbose ? ", " : "", verbose ? buffer : "", |
456 | fperfdata("time", elapsed_time, "s", config.check_warning_time, config.warning_time, config.check_critical_time, | 477 | fperfdata("time", elapsed_time, "s", config.check_warning_time, config.warning_time, |
457 | config.critical_time, true, 0, false, 0)); | 478 | config.check_critical_time, config.critical_time, true, 0, false, 0)); |
458 | 479 | ||
459 | exit(result); | 480 | exit(result); |
460 | } | 481 | } |
@@ -519,7 +540,8 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) { | |||
519 | bool implicit_tls = false; | 540 | bool implicit_tls = false; |
520 | int server_port_option = 0; | 541 | int server_port_option = 0; |
521 | while (true) { | 542 | while (true) { |
522 | int opt_index = getopt_long(argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", longopts, &option); | 543 | int opt_index = |
544 | getopt_long(argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", longopts, &option); | ||
523 | 545 | ||
524 | if (opt_index == -1 || opt_index == EOF) { | 546 | if (opt_index == -1 || opt_index == EOF) { |
525 | break; | 547 | break; |
@@ -546,7 +568,8 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) { | |||
546 | break; | 568 | break; |
547 | case 'f': /* from argument */ | 569 | case 'f': /* from argument */ |
548 | result.config.from_arg = optarg + strspn(optarg, "<"); | 570 | result.config.from_arg = optarg + strspn(optarg, "<"); |
549 | result.config.from_arg = strndup(result.config.from_arg, strcspn(result.config.from_arg, ">")); | 571 | result.config.from_arg = |
572 | strndup(result.config.from_arg, strcspn(result.config.from_arg, ">")); | ||
550 | result.config.send_mail_from = true; | 573 | result.config.send_mail_from = true; |
551 | break; | 574 | break; |
552 | case 'A': | 575 | case 'A': |
@@ -565,9 +588,11 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) { | |||
565 | case 'C': /* commands */ | 588 | case 'C': /* commands */ |
566 | if (result.config.ncommands >= command_size) { | 589 | if (result.config.ncommands >= command_size) { |
567 | command_size += 8; | 590 | command_size += 8; |
568 | result.config.commands = realloc(result.config.commands, sizeof(char *) * command_size); | 591 | result.config.commands = |
592 | realloc(result.config.commands, sizeof(char *) * command_size); | ||
569 | if (result.config.commands == NULL) { | 593 | if (result.config.commands == NULL) { |
570 | die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), result.config.ncommands); | 594 | die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), |
595 | result.config.ncommands); | ||
571 | } | 596 | } |
572 | } | 597 | } |
573 | result.config.commands[result.config.ncommands] = (char *)malloc(sizeof(char) * 255); | 598 | result.config.commands[result.config.ncommands] = (char *)malloc(sizeof(char) * 255); |
@@ -577,9 +602,11 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) { | |||
577 | case 'R': /* server responses */ | 602 | case 'R': /* server responses */ |
578 | if (result.config.nresponses >= response_size) { | 603 | if (result.config.nresponses >= response_size) { |
579 | response_size += 8; | 604 | response_size += 8; |
580 | result.config.responses = realloc(result.config.responses, sizeof(char *) * response_size); | 605 | result.config.responses = |
606 | realloc(result.config.responses, sizeof(char *) * response_size); | ||
581 | if (result.config.responses == NULL) { | 607 | if (result.config.responses == NULL) { |
582 | die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), result.config.nresponses); | 608 | die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), |
609 | result.config.nresponses); | ||
583 | } | 610 | } |
584 | } | 611 | } |
585 | result.config.responses[result.config.nresponses] = (char *)malloc(sizeof(char) * 255); | 612 | result.config.responses[result.config.nresponses] = (char *)malloc(sizeof(char) * 255); |
@@ -718,8 +745,10 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) { | |||
718 | return result; | 745 | return result; |
719 | } | 746 | } |
720 | 747 | ||
721 | char *smtp_quit(check_smtp_config config, char buffer[MAX_INPUT_BUFFER], int socket_descriptor, bool ssl_established) { | 748 | char *smtp_quit(check_smtp_config config, char buffer[MAX_INPUT_BUFFER], int socket_descriptor, |
722 | int sent_bytes = my_send(config, SMTP_QUIT, strlen(SMTP_QUIT), socket_descriptor, ssl_established); | 749 | bool ssl_established) { |
750 | int sent_bytes = | ||
751 | my_send(config, SMTP_QUIT, strlen(SMTP_QUIT), socket_descriptor, ssl_established); | ||
723 | if (sent_bytes < 0) { | 752 | if (sent_bytes < 0) { |
724 | if (config.ignore_send_quit_failure) { | 753 | if (config.ignore_send_quit_failure) { |
725 | if (verbose) { | 754 | if (verbose) { |
@@ -759,7 +788,8 @@ char *smtp_quit(check_smtp_config config, char buffer[MAX_INPUT_BUFFER], int soc | |||
759 | * function which buffers the data, move that to netutils.c and change | 788 | * function which buffers the data, move that to netutils.c and change |
760 | * check_smtp and other plugins to use that. Also, remove (\r)\n. | 789 | * check_smtp and other plugins to use that. Also, remove (\r)\n. |
761 | */ | 790 | */ |
762 | int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_descriptor, bool ssl_established) { | 791 | int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_descriptor, |
792 | bool ssl_established) { | ||
763 | int result; | 793 | int result; |
764 | int counter; | 794 | int counter; |
765 | 795 | ||
@@ -789,13 +819,16 @@ int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_des | |||
789 | * | 819 | * |
790 | * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n. | 820 | * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n. |
791 | */ | 821 | */ |
792 | int recvlines(check_smtp_config config, char *buf, size_t bufsize, int socket_descriptor, bool ssl_established) { | 822 | int recvlines(check_smtp_config config, char *buf, size_t bufsize, int socket_descriptor, |
823 | bool ssl_established) { | ||
793 | int result; | 824 | int result; |
794 | int counter; | 825 | int counter; |
795 | 826 | ||
796 | for (counter = 0; /* forever */; counter += result) { | 827 | for (counter = 0; /* forever */; counter += result) { |
797 | if (!((result = recvline(buf + counter, bufsize - counter, config, socket_descriptor, ssl_established)) > 3 && | 828 | if (!((result = recvline(buf + counter, bufsize - counter, config, socket_descriptor, |
798 | isdigit((int)buf[counter]) && isdigit((int)buf[counter + 1]) && isdigit((int)buf[counter + 2]) && buf[counter + 3] == '-')) { | 829 | ssl_established)) > 3 && |
830 | isdigit((int)buf[counter]) && isdigit((int)buf[counter + 1]) && | ||
831 | isdigit((int)buf[counter + 2]) && buf[counter + 3] == '-')) { | ||
799 | break; | 832 | break; |
800 | } | 833 | } |
801 | } | 834 | } |
@@ -835,13 +868,15 @@ void print_help(void) { | |||
835 | printf(UT_IPv46); | 868 | printf(UT_IPv46); |
836 | 869 | ||
837 | printf(" %s\n", "-e, --expect=STRING"); | 870 | printf(" %s\n", "-e, --expect=STRING"); |
838 | printf(_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT); | 871 | printf(_(" String to expect in first line of server response (default: '%s')\n"), |
872 | SMTP_EXPECT); | ||
839 | printf(" %s\n", "-C, --command=STRING"); | 873 | printf(" %s\n", "-C, --command=STRING"); |
840 | printf(" %s\n", _("SMTP command (may be used repeatedly)")); | 874 | printf(" %s\n", _("SMTP command (may be used repeatedly)")); |
841 | printf(" %s\n", "-R, --response=STRING"); | 875 | printf(" %s\n", "-R, --response=STRING"); |
842 | printf(" %s\n", _("Expected response to command (may be used repeatedly)")); | 876 | printf(" %s\n", _("Expected response to command (may be used repeatedly)")); |
843 | printf(" %s\n", "-f, --from=STRING"); | 877 | printf(" %s\n", "-f, --from=STRING"); |
844 | printf(" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")), printf(" %s\n", "-F, --fqdn=STRING"); | 878 | printf(" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")), |
879 | printf(" %s\n", "-F, --fqdn=STRING"); | ||
845 | printf(" %s\n", _("FQDN used for HELO")); | 880 | printf(" %s\n", _("FQDN used for HELO")); |
846 | printf(" %s\n", "-r, --proxy"); | 881 | printf(" %s\n", "-r, --proxy"); |
847 | printf(" %s\n", _("Use PROXY protocol prefix for the connection.")); | 882 | printf(" %s\n", _("Use PROXY protocol prefix for the connection.")); |
@@ -885,7 +920,9 @@ void print_help(void) { | |||
885 | 920 | ||
886 | void print_usage(void) { | 921 | void print_usage(void) { |
887 | printf("%s\n", _("Usage:")); | 922 | printf("%s\n", _("Usage:")); |
888 | printf("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n", progname); | 923 | printf("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n", |
924 | progname); | ||
889 | printf("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n"); | 925 | printf("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n"); |
890 | printf("[-F fqdn] [-S] [-L] [-D warn days cert expire[,crit days cert expire]] [-r] [--sni] [-v] \n"); | 926 | printf("[-F fqdn] [-S] [-L] [-D warn days cert expire[,crit days cert expire]] [-r] [--sni] " |
927 | "[-v] \n"); | ||
891 | } | 928 | } |
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index c1d8e2dd..a5a7afe8 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c | |||
@@ -32,716 +32,494 @@ const char *progname = "check_snmp"; | |||
32 | const char *copyright = "1999-2024"; | 32 | const char *copyright = "1999-2024"; |
33 | const char *email = "devel@monitoring-plugins.org"; | 33 | const char *email = "devel@monitoring-plugins.org"; |
34 | 34 | ||
35 | #include "common.h" | 35 | #include "./common.h" |
36 | #include "runcmd.h" | 36 | #include "./runcmd.h" |
37 | #include "utils.h" | 37 | #include "./utils.h" |
38 | #include "utils_cmd.h" | 38 | #include "../lib/states.h" |
39 | 39 | ||
40 | #define DEFAULT_COMMUNITY "public" | 40 | #include "../lib/utils_base.h" |
41 | #define DEFAULT_PORT "161" | 41 | #include "../lib/output.h" |
42 | #define DEFAULT_MIBLIST "ALL" | 42 | #include "check_snmp.d/check_snmp_helpers.h" |
43 | #define DEFAULT_PROTOCOL "1" | 43 | |
44 | #define DEFAULT_RETRIES 5 | 44 | #include <bits/getopt_core.h> |
45 | #define DEFAULT_AUTH_PROTOCOL "MD5" | 45 | #include <bits/getopt_ext.h> |
46 | #define DEFAULT_PRIV_PROTOCOL "DES" | 46 | #include <strings.h> |
47 | #define DEFAULT_DELIMITER "=" | 47 | #include <stdint.h> |
48 | #define DEFAULT_OUTPUT_DELIMITER " " | 48 | |
49 | #define DEFAULT_BUFFER_SIZE 100 | 49 | #include "check_snmp.d/config.h" |
50 | 50 | #include <stdlib.h> | |
51 | #define mark(a) ((a) != 0 ? "*" : "") | 51 | #include <arpa/inet.h> |
52 | 52 | #include <net-snmp/library/parse.h> | |
53 | #define CHECK_UNDEF 0 | 53 | #include <net-snmp/net-snmp-config.h> |
54 | #define CRIT_PRESENT 1 | 54 | #include <net-snmp/net-snmp-includes.h> |
55 | #define CRIT_STRING 2 | 55 | #include <net-snmp/library/snmp.h> |
56 | #define CRIT_REGEX 4 | 56 | #include <net-snmp/library/keytools.h> |
57 | #define WARN_PRESENT 8 | 57 | #include <net-snmp/library/snmp_api.h> |
58 | 58 | #include <net-snmp/session_api.h> | |
59 | #define OID_COUNT_STEP 8 | 59 | #include <net-snmp/definitions.h> |
60 | 60 | #include <net-snmp/library/asn1.h> | |
61 | /* Longopts only arguments */ | 61 | #include <net-snmp/mib_api.h> |
62 | #define L_CALCULATE_RATE CHAR_MAX + 1 | 62 | #include <net-snmp/library/snmp_impl.h> |
63 | #define L_RATE_MULTIPLIER CHAR_MAX + 2 | 63 | #include <string.h> |
64 | #define L_INVERT_SEARCH CHAR_MAX + 3 | 64 | #include "../gl/regex.h" |
65 | #define L_OFFSET CHAR_MAX + 4 | 65 | #include "../gl/base64.h" |
66 | #define L_IGNORE_MIB_PARSING_ERRORS CHAR_MAX + 5 | 66 | #include <assert.h> |
67 | 67 | ||
68 | /* Gobble to string - stop incrementing c when c[0] match one of the | 68 | const char DEFAULT_COMMUNITY[] = "public"; |
69 | * characters in s */ | 69 | const char DEFAULT_MIBLIST[] = "ALL"; |
70 | #define GOBBLE_TOS(c, s) \ | 70 | #define DEFAULT_AUTH_PROTOCOL "MD5" |
71 | while (c[0] != '\0' && strchr(s, c[0]) == NULL) { \ | 71 | |
72 | c++; \ | 72 | #ifdef HAVE_USM_DES_PRIV_PROTOCOL |
73 | # define DEFAULT_PRIV_PROTOCOL "DES" | ||
74 | #else | ||
75 | # define DEFAULT_PRIV_PROTOCOL "AES" | ||
76 | #endif | ||
77 | |||
78 | typedef struct proces_arguments_wrapper { | ||
79 | int errorcode; | ||
80 | check_snmp_config config; | ||
81 | } process_arguments_wrapper; | ||
82 | |||
83 | static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
84 | static char *trim_whitespaces_and_check_quoting(char *str); | ||
85 | static char *get_next_argument(char *str); | ||
86 | void print_usage(void); | ||
87 | void print_help(void); | ||
88 | |||
89 | int verbose = 0; | ||
90 | |||
91 | typedef struct { | ||
92 | int errorcode; | ||
93 | char *state_string; | ||
94 | } gen_state_string_type; | ||
95 | gen_state_string_type gen_state_string(check_snmp_state_entry *entries, size_t num_of_entries) { | ||
96 | char *encoded_string = NULL; | ||
97 | gen_state_string_type result = {.errorcode = OK, .state_string = NULL}; | ||
98 | |||
99 | if (verbose > 1) { | ||
100 | printf("%s:\n", __FUNCTION__); | ||
101 | for (size_t i = 0; i < num_of_entries; i++) { | ||
102 | printf("Entry timestamp %lu: %s", entries[i].timestamp, ctime(&entries[i].timestamp)); | ||
103 | switch (entries[i].type) { | ||
104 | case ASN_GAUGE: | ||
105 | printf("Type GAUGE\n"); | ||
106 | break; | ||
107 | case ASN_TIMETICKS: | ||
108 | printf("Type TIMETICKS\n"); | ||
109 | break; | ||
110 | case ASN_COUNTER: | ||
111 | printf("Type COUNTER\n"); | ||
112 | break; | ||
113 | case ASN_UINTEGER: | ||
114 | printf("Type UINTEGER\n"); | ||
115 | break; | ||
116 | case ASN_COUNTER64: | ||
117 | printf("Type COUNTER64\n"); | ||
118 | break; | ||
119 | case ASN_FLOAT: | ||
120 | printf("Type FLOAT\n"); | ||
121 | case ASN_DOUBLE: | ||
122 | printf("Type DOUBLE\n"); | ||
123 | break; | ||
124 | case ASN_INTEGER: | ||
125 | printf("Type INTEGER\n"); | ||
126 | break; | ||
127 | } | ||
128 | |||
129 | switch (entries[i].type) { | ||
130 | case ASN_GAUGE: | ||
131 | case ASN_TIMETICKS: | ||
132 | case ASN_COUNTER: | ||
133 | case ASN_UINTEGER: | ||
134 | case ASN_COUNTER64: | ||
135 | printf("Value %llu\n", entries[i].value.uIntVal); | ||
136 | break; | ||
137 | case ASN_FLOAT: | ||
138 | case ASN_DOUBLE: | ||
139 | printf("Value %f\n", entries[i].value.doubleVal); | ||
140 | break; | ||
141 | case ASN_INTEGER: | ||
142 | printf("Value %lld\n", entries[i].value.intVal); | ||
143 | break; | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | |||
148 | idx_t encoded = base64_encode_alloc((const char *)entries, | ||
149 | (idx_t)(num_of_entries * sizeof(check_snmp_state_entry)), | ||
150 | &encoded_string); | ||
151 | |||
152 | if (encoded > 0 && encoded_string != NULL) { | ||
153 | // success | ||
154 | if (verbose > 1) { | ||
155 | printf("encoded string: %s\n", encoded_string); | ||
156 | printf("encoded string length: %lu\n", strlen(encoded_string)); | ||
157 | } | ||
158 | result.state_string = encoded_string; | ||
159 | return result; | ||
73 | } | 160 | } |
74 | /* Given c, keep track of backslashes (bk) and double-quotes (dq) | 161 | result.errorcode = ERROR; |
75 | * from c[0] */ | 162 | return result; |
76 | #define COUNT_SEQ(c, bk, dq) \ | 163 | } |
77 | switch (c[0]) { \ | 164 | |
78 | case '\\': \ | 165 | typedef struct { |
79 | if (bk) \ | 166 | int errorcode; |
80 | bk--; \ | 167 | check_snmp_state_entry *state; |
81 | else \ | 168 | } recover_state_data_type; |
82 | bk++; \ | 169 | recover_state_data_type recover_state_data(char *state_string, idx_t state_string_length) { |
83 | break; \ | 170 | recover_state_data_type result = {.errorcode = OK, .state = NULL}; |
84 | case '"': \ | 171 | |
85 | if (!dq) { \ | 172 | if (verbose > 1) { |
86 | dq++; \ | 173 | printf("%s:\n", __FUNCTION__); |
87 | } else if (!bk) { \ | 174 | printf("State string: %s\n", state_string); |
88 | dq--; \ | 175 | printf("State string length: %lu\n", state_string_length); |
89 | } else { \ | ||
90 | bk--; \ | ||
91 | } \ | ||
92 | break; \ | ||
93 | } | 176 | } |
94 | 177 | ||
95 | static int process_arguments(int, char **); | 178 | idx_t outlen = 0; |
96 | static int validate_arguments(void); | 179 | bool decoded = |
97 | static char *thisarg(char *str); | 180 | base64_decode_alloc(state_string, state_string_length, (char **)&result.state, &outlen); |
98 | static char *nextarg(char *str); | 181 | |
99 | void print_usage(void); | 182 | if (!decoded) { |
100 | static void print_help(void); | 183 | if (verbose) { |
101 | static char *multiply(char *str); | 184 | printf("Failed to decode state string\n"); |
102 | 185 | } | |
103 | #include "regex.h" | 186 | // failure to decode |
104 | static char regex_expect[MAX_INPUT_BUFFER] = ""; | 187 | result.errorcode = ERROR; |
105 | static regex_t preg; | 188 | return result; |
106 | static regmatch_t pmatch[10]; | 189 | } |
107 | static char errbuf[MAX_INPUT_BUFFER] = ""; | 190 | |
108 | static char perfstr[MAX_INPUT_BUFFER] = "| "; | 191 | if (result.state == NULL) { |
109 | static int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | 192 | // Memory Error? |
110 | static int eflags = 0; | 193 | result.errorcode = ERROR; |
111 | static int errcode, excode; | 194 | return result; |
112 | 195 | } | |
113 | static char *server_address = NULL; | 196 | |
114 | static char *community = NULL; | 197 | if (verbose > 1) { |
115 | static char **contextargs = NULL; | 198 | printf("Recovered %lu entries of size %lu\n", |
116 | static char *context = NULL; | 199 | (size_t)outlen / sizeof(check_snmp_state_entry), outlen); |
117 | static char **authpriv = NULL; | 200 | |
118 | static char *proto = NULL; | 201 | for (size_t i = 0; i < (size_t)outlen / sizeof(check_snmp_state_entry); i++) { |
119 | static char *seclevel = NULL; | 202 | printf("Entry timestamp %lu: %s", result.state[i].timestamp, |
120 | static char *secname = NULL; | 203 | ctime(&result.state[i].timestamp)); |
121 | static char *authproto = NULL; | 204 | switch (result.state[i].type) { |
122 | static char *privproto = NULL; | 205 | case ASN_GAUGE: |
123 | static char *authpasswd = NULL; | 206 | printf("Type GAUGE\n"); |
124 | static char *privpasswd = NULL; | 207 | break; |
125 | static int nulloid = STATE_UNKNOWN; | 208 | case ASN_TIMETICKS: |
126 | static char **oids = NULL; | 209 | printf("Type TIMETICKS\n"); |
127 | static size_t oids_size = 0; | 210 | break; |
128 | static char *label; | 211 | case ASN_COUNTER: |
129 | static char *units; | 212 | printf("Type COUNTER\n"); |
130 | static char *port; | 213 | break; |
131 | static char *snmpcmd; | 214 | case ASN_UINTEGER: |
132 | static char string_value[MAX_INPUT_BUFFER] = ""; | 215 | printf("Type UINTEGER\n"); |
133 | static int invert_search = 0; | 216 | break; |
134 | static char **labels = NULL; | 217 | case ASN_COUNTER64: |
135 | static char **unitv = NULL; | 218 | printf("Type COUNTER64\n"); |
136 | static size_t nlabels = 0; | 219 | break; |
137 | static size_t labels_size = OID_COUNT_STEP; | 220 | case ASN_FLOAT: |
138 | static size_t nunits = 0; | 221 | printf("Type FLOAT\n"); |
139 | static size_t unitv_size = OID_COUNT_STEP; | 222 | case ASN_DOUBLE: |
140 | static size_t numoids = 0; | 223 | printf("Type DOUBLE\n"); |
141 | static int numauthpriv = 0; | 224 | break; |
142 | static int numcontext = 0; | 225 | case ASN_INTEGER: |
143 | static int verbose = 0; | 226 | printf("Type INTEGER\n"); |
144 | static bool usesnmpgetnext = false; | 227 | break; |
145 | static char *warning_thresholds = NULL; | 228 | } |
146 | static char *critical_thresholds = NULL; | 229 | |
147 | static thresholds **thlds; | 230 | switch (result.state[i].type) { |
148 | static size_t thlds_size = OID_COUNT_STEP; | 231 | case ASN_GAUGE: |
149 | static double *response_value; | 232 | case ASN_TIMETICKS: |
150 | static size_t response_size = OID_COUNT_STEP; | 233 | case ASN_COUNTER: |
151 | static int retries = 0; | 234 | case ASN_UINTEGER: |
152 | static int *eval_method; | 235 | case ASN_COUNTER64: |
153 | static size_t eval_size = OID_COUNT_STEP; | 236 | printf("Value %llu\n", result.state[i].value.uIntVal); |
154 | static char *delimiter; | 237 | break; |
155 | static char *output_delim; | 238 | case ASN_FLOAT: |
156 | static char *miblist = NULL; | 239 | case ASN_DOUBLE: |
157 | static bool needmibs = false; | 240 | printf("Value %f\n", result.state[i].value.doubleVal); |
158 | static int calculate_rate = 0; | 241 | break; |
159 | static double offset = 0.0; | 242 | case ASN_INTEGER: |
160 | static int rate_multiplier = 1; | 243 | printf("Value %lld\n", result.state[i].value.intVal); |
161 | static state_data *previous_state; | 244 | break; |
162 | static double *previous_value; | 245 | } |
163 | static size_t previous_size = OID_COUNT_STEP; | 246 | } |
164 | static int perf_labels = 1; | 247 | } |
165 | static char *ip_version = ""; | 248 | |
166 | static double multiplier = 1.0; | 249 | return result; |
167 | static char *fmtstr = ""; | ||
168 | static bool fmtstr_set = false; | ||
169 | static char buffer[DEFAULT_BUFFER_SIZE]; | ||
170 | static bool ignore_mib_parsing_errors = false; | ||
171 | |||
172 | static char *fix_snmp_range(char *th) { | ||
173 | double left; | ||
174 | double right; | ||
175 | char *colon; | ||
176 | char *ret; | ||
177 | |||
178 | if ((colon = strchr(th, ':')) == NULL || *(colon + 1) == '\0') | ||
179 | return th; | ||
180 | |||
181 | left = strtod(th, NULL); | ||
182 | right = strtod(colon + 1, NULL); | ||
183 | if (right >= left) | ||
184 | return th; | ||
185 | |||
186 | if ((ret = malloc(strlen(th) + 2)) == NULL) | ||
187 | die(STATE_UNKNOWN, _("Cannot malloc")); | ||
188 | *colon = '\0'; | ||
189 | sprintf(ret, "@%s:%s", colon + 1, th); | ||
190 | free(th); | ||
191 | return ret; | ||
192 | } | 250 | } |
193 | 251 | ||
194 | int main(int argc, char **argv) { | 252 | int main(int argc, char **argv) { |
195 | int len; | ||
196 | int total_oids; | ||
197 | size_t line; | ||
198 | unsigned int bk_count = 0; | ||
199 | unsigned int dq_count = 0; | ||
200 | int iresult = STATE_UNKNOWN; | ||
201 | int result = STATE_UNKNOWN; | ||
202 | int return_code = 0; | ||
203 | int external_error = 0; | ||
204 | char **command_line = NULL; | ||
205 | char *cl_hidden_auth = NULL; | ||
206 | char *oidname = NULL; | ||
207 | char *response = NULL; | ||
208 | char *mult_resp = NULL; | ||
209 | char *outbuff; | ||
210 | char *ptr = NULL; | ||
211 | char *show = NULL; | ||
212 | char *th_warn = NULL; | ||
213 | char *th_crit = NULL; | ||
214 | char type[8] = ""; | ||
215 | output chld_out; | ||
216 | output chld_err; | ||
217 | char *previous_string = NULL; | ||
218 | char *ap = NULL; | ||
219 | char *state_string = NULL; | ||
220 | size_t response_length; | ||
221 | size_t current_length; | ||
222 | size_t string_length; | ||
223 | char *temp_string = NULL; | ||
224 | char *quote_string = NULL; | ||
225 | time_t current_time; | ||
226 | double temp_double; | ||
227 | time_t duration; | ||
228 | char *conv = "12345678"; | ||
229 | int is_counter = 0; | ||
230 | |||
231 | setlocale(LC_ALL, ""); | 253 | setlocale(LC_ALL, ""); |
232 | bindtextdomain(PACKAGE, LOCALEDIR); | 254 | bindtextdomain(PACKAGE, LOCALEDIR); |
233 | textdomain(PACKAGE); | 255 | textdomain(PACKAGE); |
234 | 256 | ||
235 | labels = malloc(labels_size * sizeof(*labels)); | ||
236 | unitv = malloc(unitv_size * sizeof(*unitv)); | ||
237 | thlds = malloc(thlds_size * sizeof(*thlds)); | ||
238 | response_value = malloc(response_size * sizeof(*response_value)); | ||
239 | previous_value = malloc(previous_size * sizeof(*previous_value)); | ||
240 | eval_method = calloc(eval_size, sizeof(*eval_method)); | ||
241 | oids = calloc(oids_size, sizeof(char *)); | ||
242 | |||
243 | label = strdup("SNMP"); | ||
244 | units = strdup(""); | ||
245 | port = strdup(DEFAULT_PORT); | ||
246 | outbuff = strdup(""); | ||
247 | delimiter = strdup(" = "); | ||
248 | output_delim = strdup(DEFAULT_OUTPUT_DELIMITER); | ||
249 | timeout_interval = DEFAULT_SOCKET_TIMEOUT; | 257 | timeout_interval = DEFAULT_SOCKET_TIMEOUT; |
250 | retries = DEFAULT_RETRIES; | ||
251 | 258 | ||
252 | np_init((char *)progname, argc, argv); | 259 | np_init((char *)progname, argc, argv); |
253 | 260 | ||
261 | state_key stateKey = np_enable_state(NULL, 1, progname, argc, argv); | ||
262 | |||
254 | /* Parse extra opts if any */ | 263 | /* Parse extra opts if any */ |
255 | argv = np_extra_opts(&argc, argv, progname); | 264 | argv = np_extra_opts(&argc, argv, progname); |
256 | 265 | ||
257 | np_set_args(argc, argv); | 266 | np_set_args(argc, argv); |
258 | 267 | ||
259 | time(¤t_time); | 268 | // Initialize net-snmp before touching the session we are going to use |
269 | init_snmp("check_snmp"); | ||
260 | 270 | ||
261 | if (process_arguments(argc, argv) == ERROR) | 271 | process_arguments_wrapper paw_tmp = process_arguments(argc, argv); |
272 | if (paw_tmp.errorcode == ERROR) { | ||
262 | usage4(_("Could not parse arguments")); | 273 | usage4(_("Could not parse arguments")); |
263 | |||
264 | if (calculate_rate) { | ||
265 | if (!strcmp(label, "SNMP")) | ||
266 | label = strdup("SNMP RATE"); | ||
267 | |||
268 | size_t i = 0; | ||
269 | |||
270 | previous_state = np_state_read(); | ||
271 | if (previous_state != NULL) { | ||
272 | /* Split colon separated values */ | ||
273 | previous_string = strdup((char *)previous_state->data); | ||
274 | while ((ap = strsep(&previous_string, ":")) != NULL) { | ||
275 | if (verbose > 2) | ||
276 | printf("State for %zd=%s\n", i, ap); | ||
277 | while (i >= previous_size) { | ||
278 | previous_size += OID_COUNT_STEP; | ||
279 | previous_value = realloc(previous_value, previous_size * sizeof(*previous_value)); | ||
280 | } | ||
281 | previous_value[i++] = strtod(ap, NULL); | ||
282 | } | ||
283 | } | ||
284 | } | 274 | } |
285 | 275 | ||
286 | /* Populate the thresholds */ | 276 | check_snmp_config config = paw_tmp.config; |
287 | th_warn = warning_thresholds; | ||
288 | th_crit = critical_thresholds; | ||
289 | for (size_t i = 0; i < numoids; i++) { | ||
290 | char *w = th_warn ? strndup(th_warn, strcspn(th_warn, ",")) : NULL; | ||
291 | char *c = th_crit ? strndup(th_crit, strcspn(th_crit, ",")) : NULL; | ||
292 | /* translate "2:1" to "@1:2" for backwards compatibility */ | ||
293 | w = w ? fix_snmp_range(w) : NULL; | ||
294 | c = c ? fix_snmp_range(c) : NULL; | ||
295 | |||
296 | while (i >= thlds_size) { | ||
297 | thlds_size += OID_COUNT_STEP; | ||
298 | thlds = realloc(thlds, thlds_size * sizeof(*thlds)); | ||
299 | } | ||
300 | |||
301 | /* Skip empty thresholds, while avoiding segfault */ | ||
302 | set_thresholds(&thlds[i], w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL, c ? strpbrk(c, NP_THRESHOLDS_CHARS) : NULL); | ||
303 | if (w) { | ||
304 | th_warn = strchr(th_warn, ','); | ||
305 | if (th_warn) | ||
306 | th_warn++; | ||
307 | free(w); | ||
308 | } | ||
309 | if (c) { | ||
310 | th_crit = strchr(th_crit, ','); | ||
311 | if (th_crit) | ||
312 | th_crit++; | ||
313 | free(c); | ||
314 | } | ||
315 | } | ||
316 | 277 | ||
317 | /* Create the command array to execute */ | 278 | if (config.output_format_is_set) { |
318 | if (usesnmpgetnext) { | 279 | mp_set_format(config.output_format); |
319 | snmpcmd = strdup(PATH_TO_SNMPGETNEXT); | ||
320 | } else { | ||
321 | snmpcmd = strdup(PATH_TO_SNMPGET); | ||
322 | } | 280 | } |
323 | 281 | ||
324 | /* 10 arguments to pass before context and authpriv options + 1 for host and numoids. Add one for terminating NULL */ | 282 | /* Set signal handling and alarm */ |
325 | 283 | if (signal(SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) { | |
326 | unsigned index = 0; | 284 | usage4(_("Cannot catch SIGALRM")); |
327 | command_line = calloc(11 + numcontext + numauthpriv + 1 + numoids + 1, sizeof(char *)); | ||
328 | |||
329 | command_line[index++] = snmpcmd; | ||
330 | command_line[index++] = strdup("-Le"); | ||
331 | command_line[index++] = strdup("-t"); | ||
332 | xasprintf(&command_line[index++], "%d", timeout_interval); | ||
333 | command_line[index++] = strdup("-r"); | ||
334 | xasprintf(&command_line[index++], "%d", retries); | ||
335 | command_line[index++] = strdup("-m"); | ||
336 | command_line[index++] = strdup(miblist); | ||
337 | command_line[index++] = "-v"; | ||
338 | command_line[index++] = strdup(proto); | ||
339 | |||
340 | xasprintf(&cl_hidden_auth, "%s -Le -t %d -r %d -m %s -v %s", snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", | ||
341 | proto); | ||
342 | |||
343 | if (ignore_mib_parsing_errors) { | ||
344 | command_line[index++] = "-Pe"; | ||
345 | xasprintf(&cl_hidden_auth, "%s -Pe", cl_hidden_auth); | ||
346 | } | 285 | } |
347 | 286 | ||
348 | for (int i = 0; i < numcontext; i++) { | 287 | time_t current_time; |
349 | command_line[index++] = contextargs[i]; | 288 | time(¤t_time); |
350 | } | ||
351 | 289 | ||
352 | for (int i = 0; i < numauthpriv; i++) { | 290 | if (verbose > 2) { |
353 | command_line[index++] = authpriv[i]; | 291 | printf("current time: %s (timestamp: %lu)\n", ctime(¤t_time), current_time); |
354 | } | 292 | } |
355 | 293 | ||
356 | xasprintf(&command_line[index++], "%s:%s", server_address, port); | 294 | snmp_responces response = do_snmp_query(config.snmp_params); |
357 | 295 | ||
358 | xasprintf(&cl_hidden_auth, "%s [context] [authpriv] %s:%s", cl_hidden_auth, server_address, port); | 296 | mp_check overall = mp_check_init(); |
359 | 297 | ||
360 | for (size_t i = 0; i < numoids; i++) { | 298 | if (response.errorcode == OK) { |
361 | command_line[index++] = oids[i]; | 299 | mp_subcheck sc_successfull_query = mp_subcheck_init(); |
362 | xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); | 300 | xasprintf(&sc_successfull_query.output, "SNMP query was successful"); |
301 | sc_successfull_query = mp_set_subcheck_state(sc_successfull_query, STATE_OK); | ||
302 | mp_add_subcheck_to_check(&overall, sc_successfull_query); | ||
303 | } else { | ||
304 | // Error treatment here, either partial or whole | ||
305 | mp_subcheck sc_failed_query = mp_subcheck_init(); | ||
306 | xasprintf(&sc_failed_query.output, "SNMP query failed"); | ||
307 | sc_failed_query = mp_set_subcheck_state(sc_failed_query, STATE_OK); | ||
308 | mp_add_subcheck_to_check(&overall, sc_failed_query); | ||
309 | mp_exit(overall); | ||
363 | } | 310 | } |
364 | 311 | ||
365 | command_line[index++] = NULL; | 312 | check_snmp_state_entry *prev_state = NULL; |
313 | bool have_previous_state = false; | ||
366 | 314 | ||
367 | if (verbose) { | 315 | if (config.evaluation_params.calculate_rate) { |
368 | printf("%s\n", cl_hidden_auth); | 316 | state_data *previous_state = np_state_read(stateKey); |
369 | } | 317 | if (previous_state == NULL) { |
318 | // failed to recover state | ||
319 | // or no previous state | ||
320 | have_previous_state = false; | ||
321 | } else { | ||
322 | // sanity check | ||
323 | recover_state_data_type prev_state_wrapper = | ||
324 | recover_state_data(previous_state->data, (idx_t)previous_state->length); | ||
370 | 325 | ||
371 | /* Set signal handling and alarm */ | 326 | if (prev_state_wrapper.errorcode == OK) { |
372 | if (signal(SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) { | 327 | have_previous_state = true; |
373 | usage4(_("Cannot catch SIGALRM")); | 328 | prev_state = prev_state_wrapper.state; |
374 | } | 329 | } else { |
375 | alarm(timeout_interval * retries + 5); | 330 | have_previous_state = false; |
376 | 331 | prev_state = NULL; | |
377 | /* Run the command */ | ||
378 | return_code = cmd_run_array(command_line, &chld_out, &chld_err, 0); | ||
379 | |||
380 | /* disable alarm again */ | ||
381 | alarm(0); | ||
382 | |||
383 | /* Due to net-snmp sometimes showing stderr messages with poorly formed MIBs, | ||
384 | only return state unknown if return code is non zero or there is no stdout. | ||
385 | Do this way so that if there is stderr, will get added to output, which helps problem diagnosis | ||
386 | */ | ||
387 | if (return_code != 0) | ||
388 | external_error = 1; | ||
389 | if (chld_out.lines == 0) | ||
390 | external_error = 1; | ||
391 | if (external_error) { | ||
392 | if (chld_err.lines > 0) { | ||
393 | printf(_("External command error: %s\n"), chld_err.line[0]); | ||
394 | for (size_t i = 1; i < chld_err.lines; i++) { | ||
395 | printf("%s\n", chld_err.line[i]); | ||
396 | } | 332 | } |
397 | } else { | ||
398 | printf(_("External command error with no output (return code: %d)\n"), return_code); | ||
399 | } | 333 | } |
400 | exit(STATE_UNKNOWN); | ||
401 | } | 334 | } |
402 | 335 | ||
403 | if (verbose) { | 336 | check_snmp_state_entry *new_state = NULL; |
404 | for (size_t i = 0; i < chld_out.lines; i++) { | 337 | if (config.evaluation_params.calculate_rate) { |
405 | printf("%s\n", chld_out.line[i]); | 338 | new_state = calloc(config.snmp_params.num_of_test_units, sizeof(check_snmp_state_entry)); |
339 | if (new_state == NULL) { | ||
340 | die(STATE_UNKNOWN, "memory allocation failed"); | ||
406 | } | 341 | } |
407 | } | 342 | } |
408 | 343 | ||
409 | line = 0; | 344 | // We got the the query results, now process them |
410 | total_oids = 0; | 345 | for (size_t loop_index = 0; loop_index < config.snmp_params.num_of_test_units; loop_index++) { |
411 | for (size_t i = 0; line < chld_out.lines && i < numoids; line++, i++, total_oids++) { | 346 | if (verbose > 0) { |
412 | if (calculate_rate) | 347 | printf("loop_index: %zu\n", loop_index); |
413 | conv = "%.10g"; | ||
414 | else | ||
415 | conv = "%.0f"; | ||
416 | |||
417 | ptr = chld_out.line[line]; | ||
418 | oidname = strpcpy(oidname, ptr, delimiter); | ||
419 | response = strstr(ptr, delimiter); | ||
420 | if (response == NULL) | ||
421 | break; | ||
422 | |||
423 | if (verbose > 2) { | ||
424 | printf("Processing oid %zi (line %zi)\n oidname: %s\n response: %s\n", i + 1, line + 1, oidname, response); | ||
425 | } | 348 | } |
426 | 349 | ||
427 | /* Clean up type array - Sol10 does not necessarily zero it out */ | 350 | check_snmp_state_entry previous_unit_state = {}; |
428 | bzero(type, sizeof(type)); | 351 | if (config.evaluation_params.calculate_rate && have_previous_state) { |
429 | 352 | previous_unit_state = prev_state[loop_index]; | |
430 | is_counter = 0; | 353 | } |
431 | /* We strip out the datatype indicator for PHBs */ | ||
432 | if (strstr(response, "Gauge: ")) { | ||
433 | show = multiply(strstr(response, "Gauge: ") + 7); | ||
434 | } else if (strstr(response, "Gauge32: ")) { | ||
435 | show = multiply(strstr(response, "Gauge32: ") + 9); | ||
436 | } else if (strstr(response, "Counter32: ")) { | ||
437 | show = strstr(response, "Counter32: ") + 11; | ||
438 | is_counter = 1; | ||
439 | if (!calculate_rate) | ||
440 | strcpy(type, "c"); | ||
441 | } else if (strstr(response, "Counter64: ")) { | ||
442 | show = strstr(response, "Counter64: ") + 11; | ||
443 | is_counter = 1; | ||
444 | if (!calculate_rate) | ||
445 | strcpy(type, "c"); | ||
446 | } else if (strstr(response, "INTEGER: ")) { | ||
447 | show = multiply(strstr(response, "INTEGER: ") + 9); | ||
448 | |||
449 | if (fmtstr_set) { | ||
450 | conv = fmtstr; | ||
451 | } | ||
452 | } else if (strstr(response, "OID: ")) { | ||
453 | show = strstr(response, "OID: ") + 5; | ||
454 | } else if (strstr(response, "STRING: ")) { | ||
455 | show = strstr(response, "STRING: ") + 8; | ||
456 | conv = "%.10g"; | ||
457 | |||
458 | /* Get the rest of the string on multi-line strings */ | ||
459 | ptr = show; | ||
460 | COUNT_SEQ(ptr, bk_count, dq_count) | ||
461 | while (dq_count && ptr[0] != '\n' && ptr[0] != '\0') { | ||
462 | ptr++; | ||
463 | GOBBLE_TOS(ptr, "\n\"\\") | ||
464 | COUNT_SEQ(ptr, bk_count, dq_count) | ||
465 | } | ||
466 | |||
467 | if (dq_count) { /* unfinished line */ | ||
468 | /* copy show verbatim first */ | ||
469 | if (!mult_resp) | ||
470 | mult_resp = strdup(""); | ||
471 | xasprintf(&mult_resp, "%s%s:\n%s\n", mult_resp, oids[i], show); | ||
472 | /* then strip out unmatched double-quote from single-line output */ | ||
473 | if (show[0] == '"') | ||
474 | show++; | ||
475 | |||
476 | /* Keep reading until we match end of double-quoted string */ | ||
477 | for (line++; line < chld_out.lines; line++) { | ||
478 | ptr = chld_out.line[line]; | ||
479 | xasprintf(&mult_resp, "%s%s\n", mult_resp, ptr); | ||
480 | |||
481 | COUNT_SEQ(ptr, bk_count, dq_count) | ||
482 | while (dq_count && ptr[0] != '\n' && ptr[0] != '\0') { | ||
483 | ptr++; | ||
484 | GOBBLE_TOS(ptr, "\n\"\\") | ||
485 | COUNT_SEQ(ptr, bk_count, dq_count) | ||
486 | } | ||
487 | /* Break for loop before next line increment when done */ | ||
488 | if (!dq_count) | ||
489 | break; | ||
490 | } | ||
491 | } | ||
492 | |||
493 | } else if (strstr(response, "Timeticks: ")) { | ||
494 | show = strstr(response, "Timeticks: "); | ||
495 | } else | ||
496 | show = response + 3; | ||
497 | 354 | ||
498 | iresult = STATE_DEPENDENT; | 355 | check_snmp_evaluation single_eval = |
356 | evaluate_single_unit(response.response_values[loop_index], config.evaluation_params, | ||
357 | config.snmp_params.test_units[loop_index], current_time, | ||
358 | previous_unit_state, have_previous_state); | ||
499 | 359 | ||
500 | /* Process this block for numeric comparisons */ | 360 | if (config.evaluation_params.calculate_rate && |
501 | /* Make some special values,like Timeticks numeric only if a threshold is defined */ | 361 | mp_compute_subcheck_state(single_eval.sc) != STATE_UNKNOWN) { |
502 | if (thlds[i]->warning || thlds[i]->critical || calculate_rate) { | 362 | new_state[loop_index] = single_eval.state; |
503 | if (verbose > 2) { | ||
504 | print_thresholds(" thresholds", thlds[i]); | ||
505 | } | ||
506 | ptr = strpbrk(show, "-0123456789"); | ||
507 | if (ptr == NULL) { | ||
508 | if (nulloid == 3) | ||
509 | die(STATE_UNKNOWN, _("No valid data returned (%s)\n"), show); | ||
510 | else if (nulloid == 0) | ||
511 | die(STATE_OK, _("No valid data returned (%s)\n"), show); | ||
512 | else if (nulloid == 1) | ||
513 | die(STATE_WARNING, _("No valid data returned (%s)\n"), show); | ||
514 | else if (nulloid == 2) | ||
515 | die(STATE_CRITICAL, _("No valid data returned (%s)\n"), show); | ||
516 | } | ||
517 | while (i >= response_size) { | ||
518 | response_size += OID_COUNT_STEP; | ||
519 | response_value = realloc(response_value, response_size * sizeof(*response_value)); | ||
520 | } | ||
521 | response_value[i] = strtod(ptr, NULL) + offset; | ||
522 | |||
523 | if (calculate_rate) { | ||
524 | if (previous_state != NULL) { | ||
525 | duration = current_time - previous_state->time; | ||
526 | if (duration <= 0) | ||
527 | die(STATE_UNKNOWN, _("Time duration between plugin calls is invalid")); | ||
528 | temp_double = response_value[i] - previous_value[i]; | ||
529 | /* Simple overflow catcher (same as in rrdtool, rrd_update.c) */ | ||
530 | if (is_counter) { | ||
531 | if (temp_double < (double)0.0) | ||
532 | temp_double += (double)4294967296.0; /* 2^32 */ | ||
533 | if (temp_double < (double)0.0) | ||
534 | temp_double += (double)18446744069414584320.0; /* 2^64-2^32 */ | ||
535 | ; | ||
536 | } | ||
537 | /* Convert to per second, then use multiplier */ | ||
538 | temp_double = temp_double / duration * rate_multiplier; | ||
539 | iresult = get_status(temp_double, thlds[i]); | ||
540 | xasprintf(&show, conv, temp_double); | ||
541 | } | ||
542 | } else { | ||
543 | iresult = get_status(response_value[i], thlds[i]); | ||
544 | xasprintf(&show, conv, response_value[i]); | ||
545 | } | ||
546 | } | 363 | } |
547 | 364 | ||
548 | /* Process this block for string matching */ | 365 | mp_add_subcheck_to_check(&overall, single_eval.sc); |
549 | else if (eval_size > i && eval_method[i] & CRIT_STRING) { | 366 | } |
550 | if (strcmp(show, string_value)) | ||
551 | iresult = (invert_search == 0) ? STATE_CRITICAL : STATE_OK; | ||
552 | else | ||
553 | iresult = (invert_search == 0) ? STATE_OK : STATE_CRITICAL; | ||
554 | } | ||
555 | 367 | ||
556 | /* Process this block for regex matching */ | 368 | if (config.evaluation_params.calculate_rate) { |
557 | else if (eval_size > i && eval_method[i] & CRIT_REGEX) { | 369 | // store state |
558 | excode = regexec(&preg, response, 10, pmatch, eflags); | 370 | gen_state_string_type current_state_wrapper = |
559 | if (excode == 0) { | 371 | gen_state_string(new_state, config.snmp_params.num_of_test_units); |
560 | iresult = (invert_search == 0) ? STATE_OK : STATE_CRITICAL; | ||
561 | } else if (excode != REG_NOMATCH) { | ||
562 | regerror(excode, &preg, errbuf, MAX_INPUT_BUFFER); | ||
563 | printf(_("Execute Error: %s\n"), errbuf); | ||
564 | exit(STATE_CRITICAL); | ||
565 | } else { | ||
566 | iresult = (invert_search == 0) ? STATE_CRITICAL : STATE_OK; | ||
567 | } | ||
568 | } | ||
569 | 372 | ||
570 | /* Process this block for existence-nonexistence checks */ | 373 | if (current_state_wrapper.errorcode == OK) { |
571 | /* TV: Should this be outside of this else block? */ | 374 | np_state_write_string(stateKey, current_time, current_state_wrapper.state_string); |
572 | else { | 375 | } else { |
573 | if (eval_size > i && eval_method[i] & CRIT_PRESENT) | 376 | die(STATE_UNKNOWN, "failed to create state string"); |
574 | iresult = STATE_CRITICAL; | ||
575 | else if (eval_size > i && eval_method[i] & WARN_PRESENT) | ||
576 | iresult = STATE_WARNING; | ||
577 | else if (response && iresult == STATE_DEPENDENT) | ||
578 | iresult = STATE_OK; | ||
579 | } | 377 | } |
378 | } | ||
379 | mp_exit(overall); | ||
380 | } | ||
580 | 381 | ||
581 | /* Result is the worst outcome of all the OIDs tested */ | 382 | /* process command-line arguments */ |
582 | result = max_state(result, iresult); | 383 | static process_arguments_wrapper process_arguments(int argc, char **argv) { |
583 | 384 | enum { | |
584 | /* Prepend a label for this OID if there is one */ | 385 | /* Longopts only arguments */ |
585 | if (nlabels >= (size_t)1 && (size_t)i < nlabels && labels[i] != NULL) | 386 | invert_search_index = CHAR_MAX + 1, |
586 | xasprintf(&outbuff, "%s%s%s %s%s%s", outbuff, (i == 0) ? " " : output_delim, labels[i], mark(iresult), show, mark(iresult)); | 387 | offset_index, |
587 | else | 388 | ignore_mib_parsing_errors_index, |
588 | xasprintf(&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim, mark(iresult), show, mark(iresult)); | 389 | connection_prefix_index, |
589 | 390 | output_format_index, | |
590 | /* Append a unit string for this OID if there is one */ | 391 | calculate_rate, |
591 | if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL) | 392 | rate_multiplier |
592 | xasprintf(&outbuff, "%s %s", outbuff, unitv[i]); | 393 | }; |
593 | 394 | ||
594 | /* Write perfdata with whatever can be parsed by strtod, if possible */ | 395 | static struct option longopts[] = { |
595 | ptr = NULL; | 396 | STD_LONG_OPTS, |
596 | strtod(show, &ptr); | 397 | {"community", required_argument, 0, 'C'}, |
597 | if (ptr > show) { | 398 | {"oid", required_argument, 0, 'o'}, |
598 | if (perf_labels && nlabels >= (size_t)1 && (size_t)i < nlabels && labels[i] != NULL) | 399 | {"object", required_argument, 0, 'o'}, |
599 | temp_string = labels[i]; | 400 | {"delimiter", required_argument, 0, 'd'}, |
600 | else | 401 | {"nulloid", required_argument, 0, 'z'}, |
601 | temp_string = oidname; | 402 | {"output-delimiter", required_argument, 0, 'D'}, |
602 | if (strpbrk(temp_string, " ='\"") == NULL) { | 403 | {"string", required_argument, 0, 's'}, |
603 | strncat(perfstr, temp_string, sizeof(perfstr) - strlen(perfstr) - 1); | 404 | {"timeout", required_argument, 0, 't'}, |
604 | } else { | 405 | {"regex", required_argument, 0, 'r'}, |
605 | if (strpbrk(temp_string, "'") == NULL) { | 406 | {"ereg", required_argument, 0, 'r'}, |
606 | quote_string = "'"; | 407 | {"eregi", required_argument, 0, 'R'}, |
607 | } else { | 408 | {"label", required_argument, 0, 'l'}, |
608 | quote_string = "\""; | 409 | {"units", required_argument, 0, 'u'}, |
609 | } | 410 | {"port", required_argument, 0, 'p'}, |
610 | strncat(perfstr, quote_string, sizeof(perfstr) - strlen(perfstr) - 1); | 411 | {"retries", required_argument, 0, 'e'}, |
611 | strncat(perfstr, temp_string, sizeof(perfstr) - strlen(perfstr) - 1); | 412 | {"miblist", required_argument, 0, 'm'}, |
612 | strncat(perfstr, quote_string, sizeof(perfstr) - strlen(perfstr) - 1); | 413 | {"protocol", required_argument, 0, 'P'}, |
613 | } | 414 | {"context", required_argument, 0, 'N'}, |
614 | strncat(perfstr, "=", sizeof(perfstr) - strlen(perfstr) - 1); | 415 | {"seclevel", required_argument, 0, 'L'}, |
615 | len = sizeof(perfstr) - strlen(perfstr) - 1; | 416 | {"secname", required_argument, 0, 'U'}, |
616 | strncat(perfstr, show, len > ptr - show ? ptr - show : len); | 417 | {"authproto", required_argument, 0, 'a'}, |
418 | {"privproto", required_argument, 0, 'x'}, | ||
419 | {"authpasswd", required_argument, 0, 'A'}, | ||
420 | {"privpasswd", required_argument, 0, 'X'}, | ||
421 | {"next", no_argument, 0, 'n'}, | ||
422 | {"offset", required_argument, 0, offset_index}, | ||
423 | {"invert-search", no_argument, 0, invert_search_index}, | ||
424 | {"perf-oids", no_argument, 0, 'O'}, | ||
425 | {"ipv4", no_argument, 0, '4'}, | ||
426 | {"ipv6", no_argument, 0, '6'}, | ||
427 | {"multiplier", required_argument, 0, 'M'}, | ||
428 | {"ignore-mib-parsing-errors", no_argument, 0, ignore_mib_parsing_errors_index}, | ||
429 | {"connection-prefix", required_argument, 0, connection_prefix_index}, | ||
430 | {"output-format", required_argument, 0, output_format_index}, | ||
431 | {"rate", no_argument, 0, calculate_rate}, | ||
432 | {"rate-multiplier", required_argument, 0, rate_multiplier}, | ||
433 | {0, 0, 0, 0}}; | ||
434 | |||
435 | if (argc < 2) { | ||
436 | process_arguments_wrapper result = { | ||
437 | .errorcode = ERROR, | ||
438 | }; | ||
439 | return result; | ||
440 | } | ||
617 | 441 | ||
618 | if (strcmp(type, "") != 0) { | 442 | // Count number of OIDs here first |
619 | strncat(perfstr, type, sizeof(perfstr) - strlen(perfstr) - 1); | 443 | int option = 0; |
620 | } | 444 | size_t oid_counter = 0; |
445 | while (true) { | ||
446 | int option_char = getopt_long( | ||
447 | argc, argv, | ||
448 | "nhvVO46t:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:N:L:U:a:x:A:X:M:f:z:", longopts, &option); | ||
621 | 449 | ||
622 | if (warning_thresholds) { | 450 | if (option_char == -1 || option_char == EOF) { |
623 | strncat(perfstr, ";", sizeof(perfstr) - strlen(perfstr) - 1); | 451 | break; |
624 | if (thlds[i]->warning && thlds[i]->warning->text) | 452 | } |
625 | strncat(perfstr, thlds[i]->warning->text, sizeof(perfstr) - strlen(perfstr) - 1); | ||
626 | } | ||
627 | 453 | ||
628 | if (critical_thresholds) { | 454 | switch (option_char) { |
629 | if (!warning_thresholds) | 455 | case 'o': { |
630 | strncat(perfstr, ";", sizeof(perfstr) - strlen(perfstr) - 1); | 456 | // we are going to parse this again, so we work on a copy of that string |
631 | strncat(perfstr, ";", sizeof(perfstr) - strlen(perfstr) - 1); | 457 | char *tmp_oids = strdup(optarg); |
632 | if (thlds[i]->critical && thlds[i]->critical->text) | 458 | if (tmp_oids == NULL) { |
633 | strncat(perfstr, thlds[i]->critical->text, sizeof(perfstr) - strlen(perfstr) - 1); | 459 | die(STATE_UNKNOWN, "strdup failed"); |
634 | } | 460 | } |
635 | 461 | ||
636 | strncat(perfstr, " ", sizeof(perfstr) - strlen(perfstr) - 1); | 462 | for (char *ptr = strtok(tmp_oids, ", "); ptr != NULL; |
637 | } | 463 | ptr = strtok(NULL, ", "), oid_counter++) { |
638 | } | ||
639 | |||
640 | /* Save state data, as all data collected now */ | ||
641 | if (calculate_rate) { | ||
642 | string_length = 1024; | ||
643 | state_string = malloc(string_length); | ||
644 | if (state_string == NULL) | ||
645 | die(STATE_UNKNOWN, _("Cannot malloc")); | ||
646 | |||
647 | current_length = 0; | ||
648 | for (int i = 0; i < total_oids; i++) { | ||
649 | xasprintf(&temp_string, "%.0f", response_value[i]); | ||
650 | if (temp_string == NULL) | ||
651 | die(STATE_UNKNOWN, _("Cannot asprintf()")); | ||
652 | response_length = strlen(temp_string); | ||
653 | if (current_length + response_length > string_length) { | ||
654 | string_length = current_length + 1024; | ||
655 | state_string = realloc(state_string, string_length); | ||
656 | if (state_string == NULL) | ||
657 | die(STATE_UNKNOWN, _("Cannot realloc()")); | ||
658 | } | 464 | } |
659 | strcpy(&state_string[current_length], temp_string); | 465 | break; |
660 | current_length = current_length + response_length; | ||
661 | state_string[current_length] = ':'; | ||
662 | current_length++; | ||
663 | free(temp_string); | ||
664 | } | 466 | } |
665 | state_string[--current_length] = '\0'; | 467 | case '?': /* usage */ |
666 | if (verbose > 2) | 468 | usage5(); |
667 | printf("State string=%s\n", state_string); | 469 | // fallthrough |
470 | case 'h': /* help */ | ||
471 | print_help(); | ||
472 | exit(STATE_UNKNOWN); | ||
473 | case 'V': /* version */ | ||
474 | print_revision(progname, NP_VERSION); | ||
475 | exit(STATE_UNKNOWN); | ||
668 | 476 | ||
669 | /* This is not strictly the same as time now, but any subtle variations will cancel out */ | 477 | default: |
670 | np_state_write_string(current_time, state_string); | 478 | continue; |
671 | if (previous_state == NULL) { | ||
672 | /* Or should this be highest state? */ | ||
673 | die(STATE_OK, _("No previous data to calculate rate - assume okay")); | ||
674 | } | 479 | } |
675 | } | 480 | } |
676 | 481 | ||
677 | printf("%s %s -%s %s\n", label, state_text(result), outbuff, perfstr); | 482 | /* Check whether at least one OID was given */ |
678 | if (mult_resp) | 483 | if (oid_counter == 0) { |
679 | printf("%s", mult_resp); | 484 | die(STATE_UNKNOWN, _("No OIDs specified\n")); |
485 | } | ||
680 | 486 | ||
681 | return result; | 487 | // Allocate space for test units |
682 | } | 488 | check_snmp_test_unit *tmp = calloc(oid_counter, sizeof(check_snmp_test_unit)); |
489 | if (tmp == NULL) { | ||
490 | die(STATE_UNKNOWN, "Failed to calloc"); | ||
491 | } | ||
683 | 492 | ||
684 | /* process command-line arguments */ | 493 | for (size_t i = 0; i < oid_counter; i++) { |
685 | int process_arguments(int argc, char **argv) { | 494 | tmp[i] = check_snmp_test_unit_init(); |
686 | static struct option longopts[] = {STD_LONG_OPTS, | ||
687 | {"community", required_argument, 0, 'C'}, | ||
688 | {"oid", required_argument, 0, 'o'}, | ||
689 | {"object", required_argument, 0, 'o'}, | ||
690 | {"delimiter", required_argument, 0, 'd'}, | ||
691 | {"nulloid", required_argument, 0, 'z'}, | ||
692 | {"output-delimiter", required_argument, 0, 'D'}, | ||
693 | {"string", required_argument, 0, 's'}, | ||
694 | {"timeout", required_argument, 0, 't'}, | ||
695 | {"regex", required_argument, 0, 'r'}, | ||
696 | {"ereg", required_argument, 0, 'r'}, | ||
697 | {"eregi", required_argument, 0, 'R'}, | ||
698 | {"label", required_argument, 0, 'l'}, | ||
699 | {"units", required_argument, 0, 'u'}, | ||
700 | {"port", required_argument, 0, 'p'}, | ||
701 | {"retries", required_argument, 0, 'e'}, | ||
702 | {"miblist", required_argument, 0, 'm'}, | ||
703 | {"protocol", required_argument, 0, 'P'}, | ||
704 | {"context", required_argument, 0, 'N'}, | ||
705 | {"seclevel", required_argument, 0, 'L'}, | ||
706 | {"secname", required_argument, 0, 'U'}, | ||
707 | {"authproto", required_argument, 0, 'a'}, | ||
708 | {"privproto", required_argument, 0, 'x'}, | ||
709 | {"authpasswd", required_argument, 0, 'A'}, | ||
710 | {"privpasswd", required_argument, 0, 'X'}, | ||
711 | {"next", no_argument, 0, 'n'}, | ||
712 | {"rate", no_argument, 0, L_CALCULATE_RATE}, | ||
713 | {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER}, | ||
714 | {"offset", required_argument, 0, L_OFFSET}, | ||
715 | {"invert-search", no_argument, 0, L_INVERT_SEARCH}, | ||
716 | {"perf-oids", no_argument, 0, 'O'}, | ||
717 | {"ipv4", no_argument, 0, '4'}, | ||
718 | {"ipv6", no_argument, 0, '6'}, | ||
719 | {"multiplier", required_argument, 0, 'M'}, | ||
720 | {"fmtstr", required_argument, 0, 'f'}, | ||
721 | {"ignore-mib-parsing-errors", no_argument, false, L_IGNORE_MIB_PARSING_ERRORS}, | ||
722 | {0, 0, 0, 0}}; | ||
723 | |||
724 | if (argc < 2) | ||
725 | return ERROR; | ||
726 | |||
727 | /* reverse compatibility for very old non-POSIX usage forms */ | ||
728 | for (int c = 1; c < argc; c++) { | ||
729 | if (strcmp("-to", argv[c]) == 0) | ||
730 | strcpy(argv[c], "-t"); | ||
731 | if (strcmp("-wv", argv[c]) == 0) | ||
732 | strcpy(argv[c], "-w"); | ||
733 | if (strcmp("-cv", argv[c]) == 0) | ||
734 | strcpy(argv[c], "-c"); | ||
735 | } | 495 | } |
736 | 496 | ||
737 | size_t j = 0; | 497 | check_snmp_config config = check_snmp_config_init(); |
738 | size_t jj = 0; | 498 | config.snmp_params.test_units = tmp; |
499 | config.snmp_params.num_of_test_units = oid_counter; | ||
500 | |||
501 | option = 0; | ||
502 | optind = 1; // Reset argument scanner | ||
503 | size_t tmp_oid_counter = 0; | ||
504 | size_t eval_counter = 0; | ||
505 | size_t unitv_counter = 0; | ||
506 | size_t labels_counter = 0; | ||
507 | unsigned char *authpasswd = NULL; | ||
508 | unsigned char *privpasswd = NULL; | ||
509 | int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | ||
510 | char *port = NULL; | ||
511 | char *miblist = NULL; | ||
512 | char *connection_prefix = NULL; | ||
513 | bool snmp_version_set_explicitely = false; | ||
514 | // TODO error checking | ||
739 | while (true) { | 515 | while (true) { |
740 | int option = 0; | 516 | int option_char = getopt_long( |
741 | int option_char = getopt_long(argc, argv, "nhvVO46t:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:N:L:U:a:x:A:X:M:f:z:", longopts, &option); | 517 | argc, argv, |
518 | "nhvVO46t:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:N:L:U:a:x:A:X:M:f:z:", longopts, &option); | ||
742 | 519 | ||
743 | if (option_char == -1 || option_char == EOF) | 520 | if (option_char == -1 || option_char == EOF) { |
744 | break; | 521 | break; |
522 | } | ||
745 | 523 | ||
746 | switch (option_char) { | 524 | switch (option_char) { |
747 | case '?': /* usage */ | 525 | case '?': /* usage */ |
@@ -758,64 +536,155 @@ int process_arguments(int argc, char **argv) { | |||
758 | 536 | ||
759 | /* Connection info */ | 537 | /* Connection info */ |
760 | case 'C': /* group or community */ | 538 | case 'C': /* group or community */ |
761 | community = optarg; | 539 | config.snmp_params.snmp_session.community = (unsigned char *)optarg; |
540 | config.snmp_params.snmp_session.community_len = strlen(optarg); | ||
762 | break; | 541 | break; |
763 | case 'H': /* Host or server */ | 542 | case 'H': /* Host or server */ |
764 | server_address = optarg; | 543 | config.snmp_params.snmp_session.peername = optarg; |
765 | break; | 544 | break; |
766 | case 'p': /* TCP port number */ | 545 | case 'p': /*port number */ |
546 | // Add port to "peername" below to not rely on argument order | ||
767 | port = optarg; | 547 | port = optarg; |
768 | break; | 548 | break; |
769 | case 'm': /* List of MIBS */ | 549 | case 'm': /* List of MIBS */ |
770 | miblist = optarg; | 550 | miblist = optarg; |
771 | break; | 551 | break; |
772 | case 'n': /* usesnmpgetnext */ | 552 | case 'n': /* use_getnext instead of get */ |
773 | usesnmpgetnext = true; | 553 | config.snmp_params.use_getnext = true; |
774 | break; | 554 | break; |
775 | case 'P': /* SNMP protocol version */ | 555 | case 'P': /* SNMP protocol version */ |
776 | proto = optarg; | 556 | if (strcasecmp("1", optarg) == 0) { |
557 | config.snmp_params.snmp_session.version = SNMP_VERSION_1; | ||
558 | } else if (strcasecmp("2c", optarg) == 0) { | ||
559 | config.snmp_params.snmp_session.version = SNMP_VERSION_2c; | ||
560 | } else if (strcasecmp("3", optarg) == 0) { | ||
561 | config.snmp_params.snmp_session.version = SNMP_VERSION_3; | ||
562 | } else { | ||
563 | die(STATE_UNKNOWN, "invalid SNMP version/protocol: %s", optarg); | ||
564 | } | ||
565 | snmp_version_set_explicitely = true; | ||
566 | |||
777 | break; | 567 | break; |
778 | case 'N': /* SNMPv3 context */ | 568 | case 'N': /* SNMPv3 context name */ |
779 | context = optarg; | 569 | config.snmp_params.snmp_session.contextName = optarg; |
570 | config.snmp_params.snmp_session.contextNameLen = strlen(optarg); | ||
780 | break; | 571 | break; |
781 | case 'L': /* security level */ | 572 | case 'L': /* security level */ |
782 | seclevel = optarg; | 573 | if (strcasecmp("noAuthNoPriv", optarg) == 0) { |
574 | config.snmp_params.snmp_session.securityLevel = SNMP_SEC_LEVEL_NOAUTH; | ||
575 | } else if (strcasecmp("authNoPriv", optarg) == 0) { | ||
576 | config.snmp_params.snmp_session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV; | ||
577 | } else if (strcasecmp("authPriv", optarg) == 0) { | ||
578 | config.snmp_params.snmp_session.securityLevel = SNMP_SEC_LEVEL_AUTHPRIV; | ||
579 | } else { | ||
580 | die(STATE_UNKNOWN, "invalid security level: %s", optarg); | ||
581 | } | ||
783 | break; | 582 | break; |
784 | case 'U': /* security username */ | 583 | case 'U': /* security username */ |
785 | secname = optarg; | 584 | config.snmp_params.snmp_session.securityName = optarg; |
585 | config.snmp_params.snmp_session.securityNameLen = strlen(optarg); | ||
786 | break; | 586 | break; |
787 | case 'a': /* auth protocol */ | 587 | case 'a': /* auth protocol */ |
788 | authproto = optarg; | 588 | // SNMPv3: SHA or MD5 |
589 | // TODO Test for availability of individual protocols | ||
590 | if (strcasecmp("MD5", optarg) == 0) { | ||
591 | config.snmp_params.snmp_session.securityAuthProto = usmHMACMD5AuthProtocol; | ||
592 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
593 | OID_LENGTH(usmHMACMD5AuthProtocol); | ||
594 | } else if (strcasecmp("SHA", optarg) == 0) { | ||
595 | config.snmp_params.snmp_session.securityAuthProto = usmHMACSHA1AuthProtocol; | ||
596 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
597 | OID_LENGTH(usmHMACSHA1AuthProtocol); | ||
598 | } else if (strcasecmp("SHA224", optarg) == 0) { | ||
599 | config.snmp_params.snmp_session.securityAuthProto = usmHMAC128SHA224AuthProtocol; | ||
600 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
601 | OID_LENGTH(usmHMAC128SHA224AuthProtocol); | ||
602 | } else if (strcasecmp("SHA256", optarg) == 0) { | ||
603 | config.snmp_params.snmp_session.securityAuthProto = usmHMAC192SHA256AuthProtocol; | ||
604 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
605 | OID_LENGTH(usmHMAC192SHA256AuthProtocol); | ||
606 | } else if (strcasecmp("SHA384", optarg) == 0) { | ||
607 | config.snmp_params.snmp_session.securityAuthProto = usmHMAC256SHA384AuthProtocol; | ||
608 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
609 | OID_LENGTH(usmHMAC256SHA384AuthProtocol); | ||
610 | } else if (strcasecmp("SHA512", optarg) == 0) { | ||
611 | config.snmp_params.snmp_session.securityAuthProto = usmHMAC384SHA512AuthProtocol; | ||
612 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
613 | OID_LENGTH(usmHMAC384SHA512AuthProtocol); | ||
614 | } else { | ||
615 | die(STATE_UNKNOWN, "Unknown authentication protocol"); | ||
616 | } | ||
789 | break; | 617 | break; |
790 | case 'x': /* priv protocol */ | 618 | case 'x': /* priv protocol */ |
791 | privproto = optarg; | 619 | if (strcasecmp("DES", optarg) == 0) { |
620 | #ifdef HAVE_USM_DES_PRIV_PROTOCOL | ||
621 | config.snmp_params.snmp_session.securityAuthProto = usmDESPrivProtocol; | ||
622 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
623 | OID_LENGTH(usmDESPrivProtocol); | ||
624 | #else | ||
625 | die(STATE_UNKNOWN, "DES Privacy Protocol not available on this platform"); | ||
626 | #endif | ||
627 | } else if (strcasecmp("AES", optarg) == 0) { | ||
628 | config.snmp_params.snmp_session.securityAuthProto = usmAESPrivProtocol; | ||
629 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
630 | OID_LENGTH(usmAESPrivProtocol); | ||
631 | // } else if (strcasecmp("AES128", optarg)) { | ||
632 | // config.snmp_session.securityAuthProto = usmAES128PrivProtocol; | ||
633 | // config.snmp_session.securityAuthProtoLen = OID_LENGTH(usmAES128PrivProtocol) | ||
634 | // / OID_LENGTH(oid); | ||
635 | } else if (strcasecmp("AES192", optarg) == 0) { | ||
636 | config.snmp_params.snmp_session.securityAuthProto = usmAES192PrivProtocol; | ||
637 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
638 | OID_LENGTH(usmAES192PrivProtocol); | ||
639 | } else if (strcasecmp("AES256", optarg) == 0) { | ||
640 | config.snmp_params.snmp_session.securityAuthProto = usmAES256PrivProtocol; | ||
641 | config.snmp_params.snmp_session.securityAuthProtoLen = | ||
642 | OID_LENGTH(usmAES256PrivProtocol); | ||
643 | // } else if (strcasecmp("AES192Cisco", optarg)) { | ||
644 | // config.snmp_session.securityAuthProto = usmAES192CiscoPrivProtocol; | ||
645 | // config.snmp_session.securityAuthProtoLen = | ||
646 | // sizeof(usmAES192CiscoPrivProtocol) / sizeof(oid); } else if | ||
647 | // (strcasecmp("AES256Cisco", optarg)) { config.snmp_session.securityAuthProto = | ||
648 | // usmAES256CiscoPrivProtocol; config.snmp_session.securityAuthProtoLen = | ||
649 | // sizeof(usmAES256CiscoPrivProtocol) / sizeof(oid); } else if | ||
650 | // (strcasecmp("AES192Cisco2", optarg)) { config.snmp_session.securityAuthProto | ||
651 | // = usmAES192Cisco2PrivProtocol; config.snmp_session.securityAuthProtoLen = | ||
652 | // sizeof(usmAES192Cisco2PrivProtocol) / sizeof(oid); } else if | ||
653 | // (strcasecmp("AES256Cisco2", optarg)) { config.snmp_session.securityAuthProto | ||
654 | // = usmAES256Cisco2PrivProtocol; config.snmp_session.securityAuthProtoLen = | ||
655 | // sizeof(usmAES256Cisco2PrivProtocol) / sizeof(oid); | ||
656 | } else { | ||
657 | die(STATE_UNKNOWN, "Unknown privacy protocol"); | ||
658 | } | ||
792 | break; | 659 | break; |
793 | case 'A': /* auth passwd */ | 660 | case 'A': /* auth passwd */ |
794 | authpasswd = optarg; | 661 | authpasswd = (unsigned char *)optarg; |
795 | break; | 662 | break; |
796 | case 'X': /* priv passwd */ | 663 | case 'X': /* priv passwd */ |
797 | privpasswd = optarg; | 664 | privpasswd = (unsigned char *)optarg; |
665 | break; | ||
666 | case 'e': | ||
667 | case 'E': | ||
668 | if (!is_integer(optarg)) { | ||
669 | usage2(_("Retries interval must be a positive integer"), optarg); | ||
670 | } else { | ||
671 | config.snmp_params.snmp_session.retries = atoi(optarg); | ||
672 | } | ||
798 | break; | 673 | break; |
799 | case 't': /* timeout period */ | 674 | case 't': /* timeout period */ |
800 | if (!is_integer(optarg)) | 675 | if (!is_integer(optarg)) { |
801 | usage2(_("Timeout interval must be a positive integer"), optarg); | 676 | usage2(_("Timeout interval must be a positive integer"), optarg); |
802 | else | 677 | } else { |
803 | timeout_interval = atoi(optarg); | 678 | timeout_interval = (unsigned int)atoi(optarg); |
679 | } | ||
804 | break; | 680 | break; |
805 | 681 | ||
806 | /* Test parameters */ | 682 | /* Test parameters */ |
807 | case 'c': /* critical threshold */ | 683 | case 'c': /* critical threshold */ |
808 | critical_thresholds = optarg; | 684 | check_snmp_set_thresholds(optarg, config.snmp_params.test_units, oid_counter, true); |
809 | break; | 685 | break; |
810 | case 'w': /* warning threshold */ | 686 | case 'w': /* warning threshold */ |
811 | warning_thresholds = optarg; | 687 | check_snmp_set_thresholds(optarg, config.snmp_params.test_units, oid_counter, false); |
812 | break; | ||
813 | case 'e': /* PRELIMINARY - may change */ | ||
814 | case 'E': /* PRELIMINARY - may change */ | ||
815 | if (!is_integer(optarg)) | ||
816 | usage2(_("Retries interval must be a positive integer"), optarg); | ||
817 | else | ||
818 | retries = atoi(optarg); | ||
819 | break; | 688 | break; |
820 | case 'o': /* object identifier */ | 689 | case 'o': /* object identifier */ |
821 | if (strspn(optarg, "0123456789.,") != strlen(optarg)) { | 690 | if (strspn(optarg, "0123456789.,") != strlen(optarg)) { |
@@ -824,306 +693,292 @@ int process_arguments(int argc, char **argv) { | |||
824 | * so we have a mib variable, rather than just an SNMP OID, | 693 | * so we have a mib variable, rather than just an SNMP OID, |
825 | * so we have to actually read the mib files | 694 | * so we have to actually read the mib files |
826 | */ | 695 | */ |
827 | needmibs = true; | 696 | config.snmp_params.need_mibs = true; |
828 | } | ||
829 | for (char *ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", "), j++) { | ||
830 | while (j >= oids_size) { | ||
831 | oids_size += OID_COUNT_STEP; | ||
832 | oids = realloc(oids, oids_size * sizeof(*oids)); | ||
833 | } | ||
834 | oids[j] = strdup(ptr); | ||
835 | } | 697 | } |
836 | numoids = j; | 698 | |
837 | if (option_char == 'E' || option_char == 'e') { | 699 | for (char *ptr = strtok(optarg, ", "); ptr != NULL; |
838 | jj++; | 700 | ptr = strtok(NULL, ", "), tmp_oid_counter++) { |
839 | while (j + 1 >= eval_size) { | 701 | config.snmp_params.test_units[tmp_oid_counter].oid = strdup(ptr); |
840 | eval_size += OID_COUNT_STEP; | ||
841 | eval_method = realloc(eval_method, eval_size * sizeof(*eval_method)); | ||
842 | memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8); | ||
843 | } | ||
844 | if (option_char == 'E') | ||
845 | eval_method[j + 1] |= WARN_PRESENT; | ||
846 | else if (option_char == 'e') | ||
847 | eval_method[j + 1] |= CRIT_PRESENT; | ||
848 | } | 702 | } |
849 | break; | 703 | break; |
850 | case 'z': /* Null OID Return Check */ | 704 | case 'z': /* Null OID Return Check */ |
851 | if (!is_integer(optarg)) | 705 | if (!is_integer(optarg)) { |
852 | usage2(_("Exit status must be a positive integer"), optarg); | 706 | usage2(_("Exit status must be a positive integer"), optarg); |
853 | else | 707 | } else { |
854 | nulloid = atoi(optarg); | 708 | config.evaluation_params.nulloid_result = atoi(optarg); |
709 | } | ||
855 | break; | 710 | break; |
856 | case 's': /* string or substring */ | 711 | case 's': /* string or substring */ |
857 | strncpy(string_value, optarg, sizeof(string_value) - 1); | 712 | strncpy(config.evaluation_params.string_cmp_value, optarg, |
858 | string_value[sizeof(string_value) - 1] = 0; | 713 | sizeof(config.evaluation_params.string_cmp_value) - 1); |
859 | while (jj >= eval_size) { | 714 | config.evaluation_params |
860 | eval_size += OID_COUNT_STEP; | 715 | .string_cmp_value[sizeof(config.evaluation_params.string_cmp_value) - 1] = 0; |
861 | eval_method = realloc(eval_method, eval_size * sizeof(*eval_method)); | 716 | config.snmp_params.test_units[eval_counter++].eval_mthd.crit_string = true; |
862 | memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8); | ||
863 | } | ||
864 | eval_method[jj++] = CRIT_STRING; | ||
865 | break; | 717 | break; |
866 | case 'R': /* regex */ | 718 | case 'R': /* regex */ |
867 | cflags = REG_ICASE; | 719 | cflags = REG_ICASE; |
868 | // fall through | 720 | // fall through |
869 | case 'r': /* regex */ | 721 | case 'r': /* regex */ |
722 | { | ||
723 | char regex_expect[MAX_INPUT_BUFFER] = ""; | ||
870 | cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | 724 | cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; |
871 | strncpy(regex_expect, optarg, sizeof(regex_expect) - 1); | 725 | strncpy(regex_expect, optarg, sizeof(regex_expect) - 1); |
872 | regex_expect[sizeof(regex_expect) - 1] = 0; | 726 | regex_expect[sizeof(regex_expect) - 1] = 0; |
873 | errcode = regcomp(&preg, regex_expect, cflags); | 727 | int errcode = regcomp(&config.evaluation_params.regex_cmp_value, regex_expect, cflags); |
874 | if (errcode != 0) { | 728 | if (errcode != 0) { |
875 | regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); | 729 | char errbuf[MAX_INPUT_BUFFER] = ""; |
876 | printf(_("Could Not Compile Regular Expression")); | 730 | regerror(errcode, &config.evaluation_params.regex_cmp_value, errbuf, |
877 | return ERROR; | 731 | MAX_INPUT_BUFFER); |
878 | } | 732 | printf("Could Not Compile Regular Expression: %s", errbuf); |
879 | while (jj >= eval_size) { | 733 | process_arguments_wrapper result = { |
880 | eval_size += OID_COUNT_STEP; | 734 | .errorcode = ERROR, |
881 | eval_method = realloc(eval_method, eval_size * sizeof(*eval_method)); | 735 | }; |
882 | memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8); | 736 | return result; |
883 | } | 737 | } |
884 | eval_method[jj++] = CRIT_REGEX; | 738 | config.snmp_params.test_units[eval_counter++].eval_mthd.crit_regex = true; |
885 | break; | 739 | } break; |
886 | |||
887 | /* Format */ | ||
888 | case 'd': /* delimiter */ | ||
889 | delimiter = strscpy(delimiter, optarg); | ||
890 | break; | ||
891 | case 'D': /* output-delimiter */ | ||
892 | output_delim = strscpy(output_delim, optarg); | ||
893 | break; | ||
894 | case 'l': /* label */ | 740 | case 'l': /* label */ |
895 | nlabels++; | 741 | { |
896 | if (nlabels > labels_size) { | 742 | if (labels_counter >= config.snmp_params.num_of_test_units) { |
897 | labels_size += 8; | 743 | break; |
898 | labels = realloc(labels, labels_size * sizeof(*labels)); | 744 | } |
899 | if (labels == NULL) | 745 | char *ptr = trim_whitespaces_and_check_quoting(optarg); |
900 | die(STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels); | 746 | if (ptr[0] == '\'') { |
747 | config.snmp_params.test_units[labels_counter].label = ptr + 1; | ||
748 | } else { | ||
749 | config.snmp_params.test_units[labels_counter].label = ptr; | ||
901 | } | 750 | } |
902 | labels[nlabels - 1] = optarg; | 751 | |
903 | char *ptr = thisarg(optarg); | 752 | while (ptr && (ptr = get_next_argument(ptr))) { |
904 | labels[nlabels - 1] = ptr; | 753 | labels_counter++; |
905 | if (ptr[0] == '\'') | 754 | ptr = trim_whitespaces_and_check_quoting(ptr); |
906 | labels[nlabels - 1] = ptr + 1; | 755 | if (ptr[0] == '\'') { |
907 | while (ptr && (ptr = nextarg(ptr))) { | 756 | config.snmp_params.test_units[labels_counter].label = ptr + 1; |
908 | nlabels++; | 757 | } else { |
909 | if (nlabels > labels_size) { | 758 | config.snmp_params.test_units[labels_counter].label = ptr; |
910 | labels_size += 8; | ||
911 | labels = realloc(labels, labels_size * sizeof(*labels)); | ||
912 | if (labels == NULL) | ||
913 | die(STATE_UNKNOWN, _("Could not reallocate labels\n")); | ||
914 | } | 759 | } |
915 | ptr = thisarg(ptr); | ||
916 | if (ptr[0] == '\'') | ||
917 | labels[nlabels - 1] = ptr + 1; | ||
918 | else | ||
919 | labels[nlabels - 1] = ptr; | ||
920 | } | 760 | } |
921 | break; | 761 | labels_counter++; |
762 | } break; | ||
922 | case 'u': /* units */ | 763 | case 'u': /* units */ |
923 | units = optarg; | 764 | { |
924 | nunits++; | 765 | if (unitv_counter >= config.snmp_params.num_of_test_units) { |
925 | if (nunits > unitv_size) { | 766 | break; |
926 | unitv_size += 8; | ||
927 | unitv = realloc(unitv, unitv_size * sizeof(*unitv)); | ||
928 | if (unitv == NULL) | ||
929 | die(STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits); | ||
930 | } | 767 | } |
931 | unitv[nunits - 1] = optarg; | 768 | char *ptr = trim_whitespaces_and_check_quoting(optarg); |
932 | ptr = thisarg(optarg); | 769 | if (ptr[0] == '\'') { |
933 | unitv[nunits - 1] = ptr; | 770 | config.snmp_params.test_units[unitv_counter].unit_value = ptr + 1; |
934 | if (ptr[0] == '\'') | 771 | } else { |
935 | unitv[nunits - 1] = ptr + 1; | 772 | config.snmp_params.test_units[unitv_counter].unit_value = ptr; |
936 | while (ptr && (ptr = nextarg(ptr))) { | 773 | } |
937 | if (nunits > unitv_size) { | 774 | while (ptr && (ptr = get_next_argument(ptr))) { |
938 | unitv_size += 8; | 775 | unitv_counter++; |
939 | unitv = realloc(unitv, unitv_size * sizeof(*unitv)); | 776 | ptr = trim_whitespaces_and_check_quoting(ptr); |
940 | if (units == NULL) | 777 | if (ptr[0] == '\'') { |
941 | die(STATE_UNKNOWN, _("Could not realloc() units\n")); | 778 | config.snmp_params.test_units[unitv_counter].unit_value = ptr + 1; |
779 | } else { | ||
780 | config.snmp_params.test_units[unitv_counter].unit_value = ptr; | ||
942 | } | 781 | } |
943 | nunits++; | ||
944 | ptr = thisarg(ptr); | ||
945 | if (ptr[0] == '\'') | ||
946 | unitv[nunits - 1] = ptr + 1; | ||
947 | else | ||
948 | unitv[nunits - 1] = ptr; | ||
949 | } | 782 | } |
783 | unitv_counter++; | ||
784 | } break; | ||
785 | case offset_index: | ||
786 | config.evaluation_params.offset = strtod(optarg, NULL); | ||
787 | config.evaluation_params.offset_set = true; | ||
950 | break; | 788 | break; |
951 | case L_CALCULATE_RATE: | 789 | case invert_search_index: |
952 | if (calculate_rate == 0) | 790 | config.evaluation_params.invert_search = false; |
953 | np_enable_state(NULL, 1); | ||
954 | calculate_rate = 1; | ||
955 | break; | ||
956 | case L_RATE_MULTIPLIER: | ||
957 | if (!is_integer(optarg) || ((rate_multiplier = atoi(optarg)) <= 0)) | ||
958 | usage2(_("Rate multiplier must be a positive integer"), optarg); | ||
959 | break; | ||
960 | case L_OFFSET: | ||
961 | offset = strtod(optarg, NULL); | ||
962 | break; | ||
963 | case L_INVERT_SEARCH: | ||
964 | invert_search = 1; | ||
965 | break; | 791 | break; |
966 | case 'O': | 792 | case 'O': |
967 | perf_labels = 0; | 793 | config.evaluation_params.use_oid_as_perf_data_label = true; |
968 | break; | 794 | break; |
969 | case '4': | 795 | case '4': |
796 | // The default, do something here to be exclusive to -6 instead of doing nothing? | ||
797 | connection_prefix = "udp"; | ||
970 | break; | 798 | break; |
971 | case '6': | 799 | case '6': |
972 | xasprintf(&ip_version, "udp6:"); | 800 | connection_prefix = "udp6"; |
973 | if (verbose > 2) | 801 | break; |
974 | printf("IPv6 detected! Will pass \"udp6:\" to snmpget.\n"); | 802 | case connection_prefix_index: |
803 | connection_prefix = optarg; | ||
975 | break; | 804 | break; |
976 | case 'M': | 805 | case 'M': |
977 | if (strspn(optarg, "0123456789.,") == strlen(optarg)) { | 806 | if (strspn(optarg, "0123456789.,") == strlen(optarg)) { |
978 | multiplier = strtod(optarg, NULL); | 807 | config.evaluation_params.multiplier = strtod(optarg, NULL); |
808 | config.evaluation_params.multiplier_set = true; | ||
979 | } | 809 | } |
980 | break; | 810 | break; |
981 | case 'f': | 811 | case ignore_mib_parsing_errors_index: |
982 | if (multiplier != 1.0) { | 812 | config.snmp_params.ignore_mib_parsing_errors = true; |
983 | fmtstr = optarg; | 813 | break; |
984 | fmtstr_set = true; | 814 | case 'f': // Deprecated format option for floating point values |
815 | break; | ||
816 | case output_format_index: { | ||
817 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
818 | if (!parser.parsing_success) { | ||
819 | // TODO List all available formats here, maybe add anothoer usage function | ||
820 | printf("Invalid output format: %s\n", optarg); | ||
821 | exit(STATE_UNKNOWN); | ||
822 | } | ||
823 | |||
824 | config.output_format_is_set = true; | ||
825 | config.output_format = parser.output_format; | ||
826 | break; | ||
827 | } | ||
828 | case calculate_rate: | ||
829 | config.evaluation_params.calculate_rate = true; | ||
830 | break; | ||
831 | case rate_multiplier: | ||
832 | if (!is_integer(optarg) || | ||
833 | ((config.evaluation_params.rate_multiplier = (unsigned int)atoi(optarg)) <= 0)) { | ||
834 | usage2(_("Rate multiplier must be a positive integer"), optarg); | ||
985 | } | 835 | } |
986 | break; | 836 | break; |
987 | case L_IGNORE_MIB_PARSING_ERRORS: | 837 | default: |
988 | ignore_mib_parsing_errors = true; | 838 | die(STATE_UNKNOWN, "Unknown option"); |
989 | } | 839 | } |
990 | } | 840 | } |
991 | 841 | ||
992 | if (server_address == NULL) | 842 | if (config.snmp_params.snmp_session.peername == NULL) { |
993 | server_address = argv[optind]; | 843 | config.snmp_params.snmp_session.peername = argv[optind]; |
994 | 844 | } | |
995 | if (community == NULL) | ||
996 | community = strdup(DEFAULT_COMMUNITY); | ||
997 | |||
998 | return validate_arguments(); | ||
999 | } | ||
1000 | |||
1001 | /****************************************************************************** | ||
1002 | |||
1003 | @@- | ||
1004 | <sect3> | ||
1005 | <title>validate_arguments</title> | ||
1006 | |||
1007 | <para>&PROTO_validate_arguments;</para> | ||
1008 | |||
1009 | <para>Checks to see if the default miblist needs to be loaded. Also verifies | ||
1010 | the authentication and authorization combinations based on protocol version | ||
1011 | selected.</para> | ||
1012 | |||
1013 | <para></para> | ||
1014 | |||
1015 | </sect3> | ||
1016 | -@@ | ||
1017 | ******************************************************************************/ | ||
1018 | 845 | ||
1019 | static int validate_arguments() { | 846 | // Build true peername here if necessary |
1020 | /* check whether to load locally installed MIBS (CPU/disk intensive) */ | 847 | if (connection_prefix != NULL) { |
1021 | if (miblist == NULL) { | 848 | // We got something in the connection prefix |
1022 | if (needmibs) { | 849 | if (strcasecmp(connection_prefix, "udp") == 0) { |
1023 | miblist = strdup(DEFAULT_MIBLIST); | 850 | // The default, do nothing |
851 | } else if (strcasecmp(connection_prefix, "tcp") == 0) { | ||
852 | // use tcp/ipv4 | ||
853 | xasprintf(&config.snmp_params.snmp_session.peername, "tcp:%s", | ||
854 | config.snmp_params.snmp_session.peername); | ||
855 | } else if (strcasecmp(connection_prefix, "tcp6") == 0 || | ||
856 | strcasecmp(connection_prefix, "tcpv6") == 0 || | ||
857 | strcasecmp(connection_prefix, "tcpipv6") == 0 || | ||
858 | strcasecmp(connection_prefix, "udp6") == 0 || | ||
859 | strcasecmp(connection_prefix, "udpipv6") == 0 || | ||
860 | strcasecmp(connection_prefix, "udpv6") == 0) { | ||
861 | // Man page (or net-snmp) code says IPv6 addresses should be wrapped in [], but it | ||
862 | // works anyway therefore do nothing here | ||
863 | xasprintf(&config.snmp_params.snmp_session.peername, "%s:%s", connection_prefix, | ||
864 | config.snmp_params.snmp_session.peername); | ||
865 | } else if (strcmp(connection_prefix, "tls") == 0) { | ||
866 | // TODO: Anything else to do here? | ||
867 | xasprintf(&config.snmp_params.snmp_session.peername, "tls:%s", | ||
868 | config.snmp_params.snmp_session.peername); | ||
869 | } else if (strcmp(connection_prefix, "dtls") == 0) { | ||
870 | // TODO: Anything else to do here? | ||
871 | xasprintf(&config.snmp_params.snmp_session.peername, "dtls:%s", | ||
872 | config.snmp_params.snmp_session.peername); | ||
873 | } else if (strcmp(connection_prefix, "unix") == 0) { | ||
874 | // TODO: Check whether this is a valid path? | ||
875 | xasprintf(&config.snmp_params.snmp_session.peername, "unix:%s", | ||
876 | config.snmp_params.snmp_session.peername); | ||
877 | } else if (strcmp(connection_prefix, "ipx") == 0) { | ||
878 | xasprintf(&config.snmp_params.snmp_session.peername, "ipx:%s", | ||
879 | config.snmp_params.snmp_session.peername); | ||
1024 | } else { | 880 | } else { |
1025 | miblist = ""; /* don't read any mib files for numeric oids */ | 881 | // Don't know that prefix, die here |
882 | die(STATE_UNKNOWN, "Unknown connection prefix"); | ||
1026 | } | 883 | } |
1027 | } | 884 | } |
1028 | 885 | ||
1029 | /* Check server_address is given */ | 886 | /* Check server_address is given */ |
1030 | if (server_address == NULL) | 887 | if (config.snmp_params.snmp_session.peername == NULL) { |
1031 | die(STATE_UNKNOWN, _("No host specified\n")); | 888 | die(STATE_UNKNOWN, _("No host specified\n")); |
889 | } | ||
1032 | 890 | ||
1033 | /* Check oid is given */ | 891 | if (port != NULL) { |
1034 | if (numoids == 0) | 892 | xasprintf(&config.snmp_params.snmp_session.peername, "%s:%s", |
1035 | die(STATE_UNKNOWN, _("No OIDs specified\n")); | 893 | config.snmp_params.snmp_session.peername, port); |
894 | } | ||
1036 | 895 | ||
1037 | if (proto == NULL) | 896 | /* check whether to load locally installed MIBS (CPU/disk intensive) */ |
1038 | xasprintf(&proto, DEFAULT_PROTOCOL); | 897 | if (miblist == NULL) { |
1039 | 898 | if (config.snmp_params.need_mibs) { | |
1040 | if ((strcmp(proto, "1") == 0) || (strcmp(proto, "2c") == 0)) { /* snmpv1 or snmpv2c */ | 899 | setenv("MIBLS", DEFAULT_MIBLIST, 1); |
1041 | numauthpriv = 2; | 900 | } else { |
1042 | authpriv = calloc(numauthpriv, sizeof(char *)); | 901 | setenv("MIBLS", "NONE", 1); |
1043 | authpriv[0] = strdup("-c"); | 902 | miblist = ""; /* don't read any mib files for numeric oids */ |
1044 | authpriv[1] = strdup(community); | ||
1045 | } else if (strcmp(proto, "3") == 0) { /* snmpv3 args */ | ||
1046 | if (!(context == NULL)) { | ||
1047 | numcontext = 2; | ||
1048 | contextargs = calloc(numcontext, sizeof(char *)); | ||
1049 | contextargs[0] = strdup("-n"); | ||
1050 | contextargs[1] = strdup(context); | ||
1051 | } | 903 | } |
904 | } else { | ||
905 | // Blatantly stolen from snmplib/snmp_parse_args | ||
906 | setenv("MIBS", miblist, 1); | ||
907 | } | ||
1052 | 908 | ||
1053 | if (seclevel == NULL) | 909 | // Historical default is SNMP v2c |
1054 | xasprintf(&seclevel, "noAuthNoPriv"); | 910 | if (!snmp_version_set_explicitely && config.snmp_params.snmp_session.community != NULL) { |
911 | config.snmp_params.snmp_session.version = SNMP_VERSION_2c; | ||
912 | } | ||
1055 | 913 | ||
1056 | if (secname == NULL) | 914 | if ((config.snmp_params.snmp_session.version == SNMP_VERSION_1) || |
915 | (config.snmp_params.snmp_session.version == SNMP_VERSION_2c)) { /* snmpv1 or snmpv2c */ | ||
916 | /* | ||
917 | config.numauthpriv = 2; | ||
918 | config.authpriv = calloc(config.numauthpriv, sizeof(char *)); | ||
919 | config.authpriv[0] = strdup("-c"); | ||
920 | config.authpriv[1] = strdup(community); | ||
921 | */ | ||
922 | } else if (config.snmp_params.snmp_session.version == SNMP_VERSION_3) { /* snmpv3 args */ | ||
923 | // generate keys for priv and auth here (if demanded) | ||
924 | |||
925 | if (config.snmp_params.snmp_session.securityName == NULL) { | ||
1057 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "secname"); | 926 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "secname"); |
927 | } | ||
1058 | 928 | ||
1059 | if (strcmp(seclevel, "noAuthNoPriv") == 0) { | 929 | switch (config.snmp_params.snmp_session.securityLevel) { |
1060 | numauthpriv = 4; | 930 | case SNMP_SEC_LEVEL_AUTHPRIV: { |
1061 | authpriv = calloc(numauthpriv, sizeof(char *)); | 931 | if (authpasswd == NULL) { |
1062 | authpriv[0] = strdup("-l"); | 932 | die(STATE_UNKNOWN, |
1063 | authpriv[1] = strdup("noAuthNoPriv"); | 933 | "No authentication passphrase was given, but authorization was requested"); |
1064 | authpriv[2] = strdup("-u"); | ||
1065 | authpriv[3] = strdup(secname); | ||
1066 | } else { | ||
1067 | if (!((strcmp(seclevel, "authNoPriv") == 0) || (strcmp(seclevel, "authPriv") == 0))) { | ||
1068 | usage2(_("Invalid seclevel"), seclevel); | ||
1069 | } | 934 | } |
1070 | 935 | // auth and priv | |
1071 | if (authproto == NULL) | 936 | int priv_key_generated = generate_Ku( |
1072 | xasprintf(&authproto, DEFAULT_AUTH_PROTOCOL); | 937 | config.snmp_params.snmp_session.securityPrivProto, |
1073 | 938 | (unsigned int)config.snmp_params.snmp_session.securityPrivProtoLen, authpasswd, | |
1074 | if (authpasswd == NULL) | 939 | strlen((const char *)authpasswd), config.snmp_params.snmp_session.securityPrivKey, |
1075 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "authpasswd"); | 940 | &config.snmp_params.snmp_session.securityPrivKeyLen); |
1076 | 941 | ||
1077 | if (strcmp(seclevel, "authNoPriv") == 0) { | 942 | if (priv_key_generated != SNMPERR_SUCCESS) { |
1078 | numauthpriv = 8; | 943 | die(STATE_UNKNOWN, "Failed to generate privacy key"); |
1079 | authpriv = calloc(numauthpriv, sizeof(char *)); | ||
1080 | authpriv[0] = strdup("-l"); | ||
1081 | authpriv[1] = strdup("authNoPriv"); | ||
1082 | authpriv[2] = strdup("-a"); | ||
1083 | authpriv[3] = strdup(authproto); | ||
1084 | authpriv[4] = strdup("-u"); | ||
1085 | authpriv[5] = strdup(secname); | ||
1086 | authpriv[6] = strdup("-A"); | ||
1087 | authpriv[7] = strdup(authpasswd); | ||
1088 | } else if (strcmp(seclevel, "authPriv") == 0) { | ||
1089 | if (privproto == NULL) | ||
1090 | xasprintf(&privproto, DEFAULT_PRIV_PROTOCOL); | ||
1091 | |||
1092 | if (privpasswd == NULL) | ||
1093 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "privpasswd"); | ||
1094 | |||
1095 | numauthpriv = 12; | ||
1096 | authpriv = calloc(numauthpriv, sizeof(char *)); | ||
1097 | authpriv[0] = strdup("-l"); | ||
1098 | authpriv[1] = strdup("authPriv"); | ||
1099 | authpriv[2] = strdup("-a"); | ||
1100 | authpriv[3] = strdup(authproto); | ||
1101 | authpriv[4] = strdup("-u"); | ||
1102 | authpriv[5] = strdup(secname); | ||
1103 | authpriv[6] = strdup("-A"); | ||
1104 | authpriv[7] = strdup(authpasswd); | ||
1105 | authpriv[8] = strdup("-x"); | ||
1106 | authpriv[9] = strdup(privproto); | ||
1107 | authpriv[10] = strdup("-X"); | ||
1108 | authpriv[11] = strdup(privpasswd); | ||
1109 | } | 944 | } |
1110 | } | 945 | } |
1111 | 946 | // fall through | |
1112 | } else { | 947 | case SNMP_SEC_LEVEL_AUTHNOPRIV: { |
1113 | usage2(_("Invalid SNMP version"), proto); | 948 | if (privpasswd == NULL) { |
949 | die(STATE_UNKNOWN, "No privacy passphrase was given, but privacy was requested"); | ||
950 | } | ||
951 | int auth_key_generated = generate_Ku( | ||
952 | config.snmp_params.snmp_session.securityAuthProto, | ||
953 | (unsigned int)config.snmp_params.snmp_session.securityAuthProtoLen, privpasswd, | ||
954 | strlen((const char *)privpasswd), config.snmp_params.snmp_session.securityAuthKey, | ||
955 | &config.snmp_params.snmp_session.securityAuthKeyLen); | ||
956 | |||
957 | if (auth_key_generated != SNMPERR_SUCCESS) { | ||
958 | die(STATE_UNKNOWN, "Failed to generate privacy key"); | ||
959 | } | ||
960 | } break; | ||
961 | case SNMP_SEC_LEVEL_NOAUTH: | ||
962 | // No auth, no priv, not much todo | ||
963 | break; | ||
964 | } | ||
1114 | } | 965 | } |
1115 | 966 | ||
1116 | return OK; | 967 | process_arguments_wrapper result = { |
968 | .config = config, | ||
969 | .errorcode = OK, | ||
970 | }; | ||
971 | return result; | ||
1117 | } | 972 | } |
1118 | 973 | ||
1119 | /* trim leading whitespace | 974 | /* trim leading whitespace |
1120 | if there is a leading quote, make sure it balances */ | 975 | if there is a leading quote, make sure it balances */ |
1121 | 976 | char *trim_whitespaces_and_check_quoting(char *str) { | |
1122 | static char *thisarg(char *str) { | ||
1123 | str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ | 977 | str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ |
1124 | if (str[0] == '\'') { /* handle SIMPLE quoted strings */ | 978 | if (str[0] == '\'') { /* handle SIMPLE quoted strings */ |
1125 | if (strlen(str) == 1 || !strstr(str + 1, "'")) | 979 | if (strlen(str) == 1 || !strstr(str + 1, "'")) { |
1126 | die(STATE_UNKNOWN, _("Unbalanced quotes\n")); | 980 | die(STATE_UNKNOWN, _("Unbalanced quotes\n")); |
981 | } | ||
1127 | } | 982 | } |
1128 | return str; | 983 | return str; |
1129 | } | 984 | } |
@@ -1132,23 +987,21 @@ static char *thisarg(char *str) { | |||
1132 | set the trailing quote to '\x0' | 987 | set the trailing quote to '\x0' |
1133 | if the string continues, advance beyond the comma */ | 988 | if the string continues, advance beyond the comma */ |
1134 | 989 | ||
1135 | static char *nextarg(char *str) { | 990 | char *get_next_argument(char *str) { |
1136 | if (str[0] == '\'') { | 991 | if (str[0] == '\'') { |
1137 | str[0] = 0; | 992 | str[0] = 0; |
1138 | if (strlen(str) > 1) { | 993 | if (strlen(str) > 1) { |
1139 | str = strstr(str + 1, "'"); | 994 | str = strstr(str + 1, "'"); |
1140 | return (++str); | 995 | return (++str); |
1141 | } else { | ||
1142 | return NULL; | ||
1143 | } | 996 | } |
997 | return NULL; | ||
1144 | } | 998 | } |
1145 | if (str[0] == ',') { | 999 | if (str[0] == ',') { |
1146 | str[0] = 0; | 1000 | str[0] = 0; |
1147 | if (strlen(str) > 1) { | 1001 | if (strlen(str) > 1) { |
1148 | return (++str); | 1002 | return (++str); |
1149 | } else { | ||
1150 | return NULL; | ||
1151 | } | 1003 | } |
1004 | return NULL; | ||
1152 | } | 1005 | } |
1153 | if ((str = strstr(str, ",")) && strlen(str) > 1) { | 1006 | if ((str = strstr(str, ",")) && strlen(str) > 1) { |
1154 | str[0] = 0; | 1007 | str[0] = 0; |
@@ -1157,41 +1010,7 @@ static char *nextarg(char *str) { | |||
1157 | return NULL; | 1010 | return NULL; |
1158 | } | 1011 | } |
1159 | 1012 | ||
1160 | /* multiply result (values 0 < n < 1 work as divider) */ | 1013 | void print_help(void) { |
1161 | static char *multiply(char *str) { | ||
1162 | if (multiplier == 1) | ||
1163 | return (str); | ||
1164 | |||
1165 | if (verbose > 2) | ||
1166 | printf(" multiply input: %s\n", str); | ||
1167 | |||
1168 | char *endptr; | ||
1169 | double val = strtod(str, &endptr); | ||
1170 | if ((val == 0.0) && (endptr == str)) { | ||
1171 | die(STATE_UNKNOWN, _("multiplier set (%.1f), but input is not a number: %s"), multiplier, str); | ||
1172 | } | ||
1173 | |||
1174 | if (verbose > 2) | ||
1175 | printf(" multiply extracted double: %f\n", val); | ||
1176 | |||
1177 | val *= multiplier; | ||
1178 | char *conv = "%f"; | ||
1179 | if (fmtstr_set) { | ||
1180 | conv = fmtstr; | ||
1181 | } | ||
1182 | if (val == (int)val) { | ||
1183 | snprintf(buffer, DEFAULT_BUFFER_SIZE, "%.0f", val); | ||
1184 | } else { | ||
1185 | if (verbose > 2) | ||
1186 | printf(" multiply using format: %s\n", conv); | ||
1187 | snprintf(buffer, DEFAULT_BUFFER_SIZE, conv, val); | ||
1188 | } | ||
1189 | if (verbose > 2) | ||
1190 | printf(" multiply result: %s\n", buffer); | ||
1191 | return buffer; | ||
1192 | } | ||
1193 | |||
1194 | static void print_help(void) { | ||
1195 | print_revision(progname, NP_VERSION); | 1014 | print_revision(progname, NP_VERSION); |
1196 | 1015 | ||
1197 | printf(COPYRIGHT, copyright, email); | 1016 | printf(COPYRIGHT, copyright, email); |
@@ -1204,8 +1023,6 @@ static void print_help(void) { | |||
1204 | 1023 | ||
1205 | printf(UT_HELP_VRSN); | 1024 | printf(UT_HELP_VRSN); |
1206 | printf(UT_EXTRA_OPTS); | 1025 | printf(UT_EXTRA_OPTS); |
1207 | printf(UT_IPv46); | ||
1208 | |||
1209 | printf(UT_HOST_PORT, 'p', DEFAULT_PORT); | 1026 | printf(UT_HOST_PORT, 'p', DEFAULT_PORT); |
1210 | 1027 | ||
1211 | /* SNMP and Authentication Protocol */ | 1028 | /* SNMP and Authentication Protocol */ |
@@ -1217,13 +1034,15 @@ static void print_help(void) { | |||
1217 | printf(" %s\n", _("SNMPv3 context")); | 1034 | printf(" %s\n", _("SNMPv3 context")); |
1218 | printf(" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]"); | 1035 | printf(" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]"); |
1219 | printf(" %s\n", _("SNMPv3 securityLevel")); | 1036 | printf(" %s\n", _("SNMPv3 securityLevel")); |
1220 | printf(" %s\n", "-a, --authproto=AUTHENTICATION_PROTOCOL"); | 1037 | printf(" %s\n", "-a, --authproto=[MD5|SHA]"); |
1221 | printf(" %s\n", | 1038 | printf(" %s\n", _("SNMPv3 auth proto")); |
1222 | _("SNMPv3 authentication protocol (default MD5), available options depend on the specific version of the net-snmp tools")); | 1039 | #ifdef HAVE_USM_DES_PRIV_PROTOCOL |
1223 | printf(" %s\n", _("if < 5.8 SHA (1) and MD5 should be available, if >= 5.8 additionally SHA-224, SHA-256, SHA-384 and SHA-512")); | 1040 | printf(" %s\n", "-x, --privproto=[DES|AES]"); |
1224 | printf(" %s\n", "-x, --privproto=PRIVACY_PROTOCOL"); | 1041 | printf(" %s\n", _("SNMPv3 priv proto (default DES)")); |
1225 | printf(" %s\n", _("SNMPv3 privacy protocol (default DES), available options depend on the specific version of the net-snmp tools")); | 1042 | #else |
1226 | printf(" %s\n", _("if < 5.8 DES and AES should be available, if >= 5.8 additionally AES-192 and AES-256")); | 1043 | printf(" %s\n", "-x, --privproto=[AES]"); |
1044 | printf(" %s\n", _("SNMPv3 priv proto (default AES)")); | ||
1045 | #endif | ||
1227 | 1046 | ||
1228 | /* Authentication Tokens*/ | 1047 | /* Authentication Tokens*/ |
1229 | printf(" %s\n", "-C, --community=STRING"); | 1048 | printf(" %s\n", "-C, --community=STRING"); |
@@ -1235,15 +1054,18 @@ static void print_help(void) { | |||
1235 | printf(" %s\n", _("SNMPv3 authentication password")); | 1054 | printf(" %s\n", _("SNMPv3 authentication password")); |
1236 | printf(" %s\n", "-X, --privpasswd=PASSWORD"); | 1055 | printf(" %s\n", "-X, --privpasswd=PASSWORD"); |
1237 | printf(" %s\n", _("SNMPv3 privacy password")); | 1056 | printf(" %s\n", _("SNMPv3 privacy password")); |
1057 | printf(" %s\n", "--connection-prefix"); | ||
1058 | printf(" Connection prefix, may be one of udp, udp6, tcp, unix, ipx, udp6, udpv6, udpipv6, " | ||
1059 | "tcp6, tcpv6, tcpipv6, tls, dtls - " | ||
1060 | "default is \"udp\"\n"); | ||
1238 | 1061 | ||
1239 | /* OID Stuff */ | 1062 | /* OID Stuff */ |
1240 | printf(" %s\n", "-o, --oid=OID(s)"); | 1063 | printf(" %s\n", "-o, --oid=OID(s)"); |
1241 | printf(" %s\n", _("Object identifier(s) or SNMP variables whose value you wish to query")); | 1064 | printf(" %s\n", _("Object identifier(s) or SNMP variables whose value you wish to query")); |
1242 | printf(" %s\n", "-m, --miblist=STRING"); | 1065 | printf(" %s\n", "-m, --miblist=STRING"); |
1243 | printf(" %s\n", _("List of MIBS to be loaded (default = none if using numeric OIDs or 'ALL'")); | 1066 | printf(" %s\n", |
1067 | _("List of MIBS to be loaded (default = none if using numeric OIDs or 'ALL'")); | ||
1244 | printf(" %s\n", _("for symbolic OIDs.)")); | 1068 | printf(" %s\n", _("for symbolic OIDs.)")); |
1245 | printf(" %s\n", "-d, --delimiter=STRING"); | ||
1246 | printf(" %s \"%s\"\n", _("Delimiter to use when parsing returned data. Default is"), DEFAULT_DELIMITER); | ||
1247 | printf(" %s\n", _("Any data on the right hand side of the delimiter is considered")); | 1069 | printf(" %s\n", _("Any data on the right hand side of the delimiter is considered")); |
1248 | printf(" %s\n", _("to be the data that should be used in the evaluation.")); | 1070 | printf(" %s\n", _("to be the data that should be used in the evaluation.")); |
1249 | printf(" %s\n", "-z, --nulloid=#"); | 1071 | printf(" %s\n", "-z, --nulloid=#"); |
@@ -1260,10 +1082,6 @@ static void print_help(void) { | |||
1260 | printf(" %s\n", _("Warning threshold range(s)")); | 1082 | printf(" %s\n", _("Warning threshold range(s)")); |
1261 | printf(" %s\n", "-c, --critical=THRESHOLD(s)"); | 1083 | printf(" %s\n", "-c, --critical=THRESHOLD(s)"); |
1262 | printf(" %s\n", _("Critical threshold range(s)")); | 1084 | printf(" %s\n", _("Critical threshold range(s)")); |
1263 | printf(" %s\n", "--rate"); | ||
1264 | printf(" %s\n", _("Enable rate calculation. See 'Rate Calculation' below")); | ||
1265 | printf(" %s\n", "--rate-multiplier"); | ||
1266 | printf(" %s\n", _("Converts rate per second. For example, set to 60 to convert to per minute")); | ||
1267 | printf(" %s\n", "--offset=OFFSET"); | 1085 | printf(" %s\n", "--offset=OFFSET"); |
1268 | printf(" %s\n", _("Add/subtract the specified OFFSET to numeric sensor data")); | 1086 | printf(" %s\n", _("Add/subtract the specified OFFSET to numeric sensor data")); |
1269 | 1087 | ||
@@ -1271,9 +1089,11 @@ static void print_help(void) { | |||
1271 | printf(" %s\n", "-s, --string=STRING"); | 1089 | printf(" %s\n", "-s, --string=STRING"); |
1272 | printf(" %s\n", _("Return OK state (for that OID) if STRING is an exact match")); | 1090 | printf(" %s\n", _("Return OK state (for that OID) if STRING is an exact match")); |
1273 | printf(" %s\n", "-r, --ereg=REGEX"); | 1091 | printf(" %s\n", "-r, --ereg=REGEX"); |
1274 | printf(" %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches")); | 1092 | printf(" %s\n", |
1093 | _("Return OK state (for that OID) if extended regular expression REGEX matches")); | ||
1275 | printf(" %s\n", "-R, --eregi=REGEX"); | 1094 | printf(" %s\n", "-R, --eregi=REGEX"); |
1276 | printf(" %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches")); | 1095 | printf(" %s\n", |
1096 | _("Return OK state (for that OID) if case-insensitive extended REGEX matches")); | ||
1277 | printf(" %s\n", "--invert-search"); | 1097 | printf(" %s\n", "--invert-search"); |
1278 | printf(" %s\n", _("Invert search result (CRITICAL if found)")); | 1098 | printf(" %s\n", _("Invert search result (CRITICAL if found)")); |
1279 | 1099 | ||
@@ -1282,53 +1102,46 @@ static void print_help(void) { | |||
1282 | printf(" %s\n", _("Prefix label for output from plugin")); | 1102 | printf(" %s\n", _("Prefix label for output from plugin")); |
1283 | printf(" %s\n", "-u, --units=STRING"); | 1103 | printf(" %s\n", "-u, --units=STRING"); |
1284 | printf(" %s\n", _("Units label(s) for output data (e.g., 'sec.').")); | 1104 | printf(" %s\n", _("Units label(s) for output data (e.g., 'sec.').")); |
1285 | printf(" %s\n", "-D, --output-delimiter=STRING"); | ||
1286 | printf(" %s\n", _("Separates output on multiple OID requests")); | ||
1287 | printf(" %s\n", "-M, --multiplier=FLOAT"); | 1105 | printf(" %s\n", "-M, --multiplier=FLOAT"); |
1288 | printf(" %s\n", _("Multiplies current value, 0 < n < 1 works as divider, defaults to 1")); | 1106 | printf(" %s\n", _("Multiplies current value, 0 < n < 1 works as divider, defaults to 1")); |
1289 | printf(" %s\n", "-f, --fmtstr=STRING"); | 1107 | printf(UT_OUTPUT_FORMAT); |
1290 | printf(" %s\n", _("C-style format string for float values (see option -M)")); | ||
1291 | 1108 | ||
1292 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 1109 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
1293 | printf(" %s\n", _("NOTE the final timeout value is calculated using this formula: timeout_interval * retries + 5")); | 1110 | printf(" %s\n", _("NOTE the final timeout value is calculated using this formula: " |
1111 | "timeout_interval * retries + 5")); | ||
1294 | printf(" %s\n", "-e, --retries=INTEGER"); | 1112 | printf(" %s\n", "-e, --retries=INTEGER"); |
1295 | printf(" %s%i\n", _("Number of retries to be used in the requests, default: "), DEFAULT_RETRIES); | 1113 | printf(" %s%i\n", _("Number of retries to be used in the requests, default: "), |
1114 | DEFAULT_RETRIES); | ||
1296 | 1115 | ||
1297 | printf(" %s\n", "-O, --perf-oids"); | 1116 | printf(" %s\n", "-O, --perf-oids"); |
1298 | printf(" %s\n", _("Label performance data with OIDs instead of --label's")); | 1117 | printf(" %s\n", _("Label performance data with OIDs instead of --label's")); |
1299 | 1118 | ||
1300 | printf(" %s\n", "--ignore-mib-parsing-errors"); | 1119 | printf(" %s\n", "--ignore-mib-parsing-errors"); |
1301 | printf(" %s\n", _("Tell snmpget to not print errors encountered when parsing MIB files")); | 1120 | printf(" %s\n", _("Do to not print errors encountered when parsing MIB files")); |
1302 | 1121 | ||
1303 | printf(UT_VERBOSE); | 1122 | printf(UT_VERBOSE); |
1304 | 1123 | ||
1305 | printf("\n"); | 1124 | printf("\n"); |
1306 | printf("%s\n", _("This plugin uses the 'snmpget' command included with the NET-SNMP package.")); | 1125 | printf("%s\n", _("This plugin relies (links against) on the NET-SNMP libraries.")); |
1307 | printf("%s\n", _("if you don't have the package installed, you will need to download it from")); | 1126 | printf("%s\n", |
1127 | _("if you don't have the libraries installed, you will need to download them from")); | ||
1308 | printf("%s\n", _("http://net-snmp.sourceforge.net before you can use this plugin.")); | 1128 | printf("%s\n", _("http://net-snmp.sourceforge.net before you can use this plugin.")); |
1309 | 1129 | ||
1310 | printf("\n"); | 1130 | printf("\n"); |
1311 | printf("%s\n", _("Notes:")); | 1131 | printf("%s\n", _("Notes:")); |
1312 | printf(" %s\n", _("- Multiple OIDs (and labels) may be indicated by a comma or space-delimited ")); | 1132 | printf(" %s\n", |
1133 | _("- Multiple OIDs (and labels) may be indicated by a comma or space-delimited ")); | ||
1313 | printf(" %s\n", _("list (lists with internal spaces must be quoted).")); | 1134 | printf(" %s\n", _("list (lists with internal spaces must be quoted).")); |
1314 | 1135 | ||
1315 | printf(" -%s", UT_THRESHOLDS_NOTES); | 1136 | printf(" -%s", UT_THRESHOLDS_NOTES); |
1316 | 1137 | ||
1317 | printf(" %s\n", _("- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'")); | 1138 | printf(" %s\n", |
1139 | _("- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'")); | ||
1318 | printf(" %s\n", _("- Note that only one string and one regex may be checked at present")); | 1140 | printf(" %s\n", _("- Note that only one string and one regex may be checked at present")); |
1319 | printf(" %s\n", _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value")); | 1141 | printf(" %s\n", |
1142 | _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value")); | ||
1320 | printf(" %s\n", _("returned from the SNMP query is an unsigned integer.")); | 1143 | printf(" %s\n", _("returned from the SNMP query is an unsigned integer.")); |
1321 | 1144 | ||
1322 | printf("\n"); | ||
1323 | printf("%s\n", _("Rate Calculation:")); | ||
1324 | printf(" %s\n", _("In many places, SNMP returns counters that are only meaningful when")); | ||
1325 | printf(" %s\n", _("calculating the counter difference since the last check. check_snmp")); | ||
1326 | printf(" %s\n", _("saves the last state information in a file so that the rate per second")); | ||
1327 | printf(" %s\n", _("can be calculated. Use the --rate option to save state information.")); | ||
1328 | printf(" %s\n", _("On the first run, there will be no prior state - this will return with OK.")); | ||
1329 | printf(" %s\n", _("The state is uniquely determined by the arguments to the plugin, so")); | ||
1330 | printf(" %s\n", _("changing the arguments will create a new state file.")); | ||
1331 | |||
1332 | printf(UT_SUPPORT); | 1145 | printf(UT_SUPPORT); |
1333 | } | 1146 | } |
1334 | 1147 | ||
@@ -1339,5 +1152,5 @@ void print_usage(void) { | |||
1339 | printf("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n"); | 1152 | printf("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n"); |
1340 | printf("[-m miblist] [-P snmp version] [-N context] [-L seclevel] [-U secname]\n"); | 1153 | printf("[-m miblist] [-P snmp version] [-N context] [-L seclevel] [-U secname]\n"); |
1341 | printf("[-a authproto] [-A authpasswd] [-x privproto] [-X privpasswd] [-4|6]\n"); | 1154 | printf("[-a authproto] [-A authpasswd] [-x privproto] [-X privpasswd] [-4|6]\n"); |
1342 | printf("[-M multiplier [-f format]]\n"); | 1155 | printf("[-M multiplier]\n"); |
1343 | } | 1156 | } |
diff --git a/plugins/check_snmp.d/check_snmp_helpers.c b/plugins/check_snmp.d/check_snmp_helpers.c new file mode 100644 index 00000000..ecbfc5dd --- /dev/null +++ b/plugins/check_snmp.d/check_snmp_helpers.c | |||
@@ -0,0 +1,934 @@ | |||
1 | #include "./check_snmp_helpers.h" | ||
2 | #include <string.h> | ||
3 | #include "../../lib/utils_base.h" | ||
4 | #include "config.h" | ||
5 | #include <assert.h> | ||
6 | #include "../utils.h" | ||
7 | #include "output.h" | ||
8 | #include "states.h" | ||
9 | #include <sys/stat.h> | ||
10 | #include <ctype.h> | ||
11 | |||
12 | extern int verbose; | ||
13 | |||
14 | check_snmp_test_unit check_snmp_test_unit_init() { | ||
15 | check_snmp_test_unit tmp = { | ||
16 | .threshold = mp_thresholds_init(), | ||
17 | }; | ||
18 | return tmp; | ||
19 | } | ||
20 | |||
21 | int check_snmp_set_thresholds(const char *threshold_string, check_snmp_test_unit test_units[], | ||
22 | size_t max_test_units, bool is_critical) { | ||
23 | |||
24 | if (threshold_string == NULL || strlen(threshold_string) == 0) { | ||
25 | // No input, do nothing | ||
26 | return 0; | ||
27 | } | ||
28 | |||
29 | if (strchr(threshold_string, ',') != NULL) { | ||
30 | // Got a comma in the string, should be multiple values | ||
31 | size_t tu_index = 0; | ||
32 | |||
33 | while (threshold_string[0] == ',') { | ||
34 | // got commas at the beginning, so skip some values | ||
35 | tu_index++; | ||
36 | threshold_string++; | ||
37 | } | ||
38 | |||
39 | for (char *ptr = strtok(threshold_string, ", "); ptr != NULL; | ||
40 | ptr = strtok(NULL, ", "), tu_index++) { | ||
41 | |||
42 | if (tu_index > max_test_units) { | ||
43 | // More thresholds then values, just ignore them | ||
44 | return 0; | ||
45 | } | ||
46 | |||
47 | // edge case: maybe we got `,,` to skip a value | ||
48 | if (strlen(ptr) == 0) { | ||
49 | // no threshold given, do not set it then | ||
50 | continue; | ||
51 | } | ||
52 | |||
53 | mp_range_parsed tmp = mp_parse_range_string(ptr); | ||
54 | if (tmp.error != MP_PARSING_SUCCES) { | ||
55 | die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", ptr); | ||
56 | } | ||
57 | |||
58 | if (is_critical) { | ||
59 | test_units[tu_index].threshold.critical = tmp.range; | ||
60 | test_units[tu_index].threshold.critical_is_set = true; | ||
61 | } else { | ||
62 | test_units[tu_index].threshold.warning = tmp.range; | ||
63 | test_units[tu_index].threshold.warning_is_set = true; | ||
64 | } | ||
65 | } | ||
66 | |||
67 | } else { | ||
68 | // Single value | ||
69 | // only valid for the first test unit | ||
70 | mp_range_parsed tmp = mp_parse_range_string(threshold_string); | ||
71 | if (tmp.error != MP_PARSING_SUCCES) { | ||
72 | die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", threshold_string); | ||
73 | } | ||
74 | |||
75 | if (is_critical) { | ||
76 | test_units[0].threshold.critical = tmp.range; | ||
77 | test_units[0].threshold.critical_is_set = true; | ||
78 | } else { | ||
79 | test_units[0].threshold.warning = tmp.range; | ||
80 | test_units[0].threshold.warning_is_set = true; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | return 0; | ||
85 | } | ||
86 | |||
87 | const int DEFAULT_PROTOCOL = SNMP_VERSION_1; | ||
88 | const char DEFAULT_OUTPUT_DELIMITER[] = " "; | ||
89 | |||
90 | const int RANDOM_STATE_DATA_LENGTH_PREDICTION = 8192; | ||
91 | |||
92 | check_snmp_config check_snmp_config_init() { | ||
93 | check_snmp_config tmp = { | ||
94 | .snmp_params = | ||
95 | { | ||
96 | .use_getnext = false, | ||
97 | |||
98 | .ignore_mib_parsing_errors = false, | ||
99 | .need_mibs = false, | ||
100 | |||
101 | .test_units = NULL, | ||
102 | .num_of_test_units = 0, | ||
103 | }, | ||
104 | |||
105 | .evaluation_params = | ||
106 | { | ||
107 | .nulloid_result = STATE_UNKNOWN, // state to return if no result for query | ||
108 | |||
109 | .invert_search = true, | ||
110 | .regex_cmp_value = {}, | ||
111 | .string_cmp_value = "", | ||
112 | |||
113 | .multiplier = 1.0, | ||
114 | .multiplier_set = false, | ||
115 | .offset = 0, | ||
116 | .offset_set = false, | ||
117 | |||
118 | .use_oid_as_perf_data_label = false, | ||
119 | |||
120 | .calculate_rate = false, | ||
121 | .rate_multiplier = 1, | ||
122 | }, | ||
123 | }; | ||
124 | |||
125 | snmp_sess_init(&tmp.snmp_params.snmp_session); | ||
126 | |||
127 | tmp.snmp_params.snmp_session.retries = DEFAULT_RETRIES; | ||
128 | tmp.snmp_params.snmp_session.version = DEFAULT_SNMP_VERSION; | ||
129 | tmp.snmp_params.snmp_session.securityLevel = SNMP_SEC_LEVEL_NOAUTH; | ||
130 | tmp.snmp_params.snmp_session.community = (unsigned char *)"public"; | ||
131 | tmp.snmp_params.snmp_session.community_len = strlen("public"); | ||
132 | return tmp; | ||
133 | } | ||
134 | |||
135 | snmp_responces do_snmp_query(check_snmp_config_snmp_parameters parameters) { | ||
136 | if (parameters.ignore_mib_parsing_errors) { | ||
137 | char *opt_toggle_res = snmp_mib_toggle_options("e"); | ||
138 | if (opt_toggle_res != NULL) { | ||
139 | die(STATE_UNKNOWN, "Unable to disable MIB parsing errors"); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | struct snmp_pdu *pdu = NULL; | ||
144 | if (parameters.use_getnext) { | ||
145 | pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); | ||
146 | } else { | ||
147 | pdu = snmp_pdu_create(SNMP_MSG_GET); | ||
148 | } | ||
149 | |||
150 | for (size_t i = 0; i < parameters.num_of_test_units; i++) { | ||
151 | assert(parameters.test_units[i].oid != NULL); | ||
152 | if (verbose > 0) { | ||
153 | printf("OID %zu to parse: %s\n", i, parameters.test_units[i].oid); | ||
154 | } | ||
155 | |||
156 | oid tmp_OID[MAX_OID_LEN]; | ||
157 | size_t tmp_OID_len = MAX_OID_LEN; | ||
158 | if (snmp_parse_oid(parameters.test_units[i].oid, tmp_OID, &tmp_OID_len) != NULL) { | ||
159 | // success | ||
160 | snmp_add_null_var(pdu, tmp_OID, tmp_OID_len); | ||
161 | } else { | ||
162 | // failed | ||
163 | snmp_perror("Parsing failure"); | ||
164 | die(STATE_UNKNOWN, "Failed to parse OID\n"); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | const int timeout_safety_tolerance = 5; | ||
169 | alarm((timeout_interval * (unsigned int)parameters.snmp_session.retries) + | ||
170 | timeout_safety_tolerance); | ||
171 | |||
172 | struct snmp_session *active_session = snmp_open(¶meters.snmp_session); | ||
173 | if (active_session == NULL) { | ||
174 | int pcliberr = 0; | ||
175 | int psnmperr = 0; | ||
176 | char *pperrstring = NULL; | ||
177 | snmp_error(¶meters.snmp_session, &pcliberr, &psnmperr, &pperrstring); | ||
178 | die(STATE_UNKNOWN, "Failed to open SNMP session: %s\n", pperrstring); | ||
179 | } | ||
180 | |||
181 | struct snmp_pdu *response = NULL; | ||
182 | int snmp_query_status = snmp_synch_response(active_session, pdu, &response); | ||
183 | |||
184 | if (!(snmp_query_status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR)) { | ||
185 | int pcliberr = 0; | ||
186 | int psnmperr = 0; | ||
187 | char *pperrstring = NULL; | ||
188 | snmp_error(active_session, &pcliberr, &psnmperr, &pperrstring); | ||
189 | |||
190 | if (psnmperr == SNMPERR_TIMEOUT) { | ||
191 | // We exit with critical here for some historical reason | ||
192 | die(STATE_CRITICAL, "SNMP query ran into a timeout\n"); | ||
193 | } | ||
194 | die(STATE_UNKNOWN, "SNMP query failed: %s\n", pperrstring); | ||
195 | } | ||
196 | |||
197 | snmp_close(active_session); | ||
198 | |||
199 | /* disable alarm again */ | ||
200 | alarm(0); | ||
201 | |||
202 | snmp_responces result = { | ||
203 | .errorcode = OK, | ||
204 | .response_values = calloc(parameters.num_of_test_units, sizeof(response_value)), | ||
205 | }; | ||
206 | |||
207 | if (result.response_values == NULL) { | ||
208 | result.errorcode = ERROR; | ||
209 | return result; | ||
210 | } | ||
211 | |||
212 | // We got the the query results, now process them | ||
213 | size_t loop_index = 0; | ||
214 | for (netsnmp_variable_list *vars = response->variables; vars; | ||
215 | vars = vars->next_variable, loop_index++) { | ||
216 | |||
217 | for (size_t jdx = 0; jdx < vars->name_length; jdx++) { | ||
218 | result.response_values[loop_index].oid[jdx] = vars->name[jdx]; | ||
219 | } | ||
220 | result.response_values[loop_index].oid_length = vars->name_length; | ||
221 | |||
222 | switch (vars->type) { | ||
223 | case ASN_OCTET_STR: { | ||
224 | result.response_values[loop_index].string_response = strdup((char *)vars->val.string); | ||
225 | result.response_values[loop_index].type = vars->type; | ||
226 | if (verbose) { | ||
227 | printf("Debug: Got a string as response: %s\n", vars->val.string); | ||
228 | } | ||
229 | } | ||
230 | continue; | ||
231 | case ASN_OPAQUE: | ||
232 | if (verbose) { | ||
233 | printf("Debug: Got OPAQUE\n"); | ||
234 | } | ||
235 | break; | ||
236 | /* Numerical values */ | ||
237 | case ASN_COUNTER64: { | ||
238 | if (verbose) { | ||
239 | printf("Debug: Got counter64\n"); | ||
240 | } | ||
241 | struct counter64 tmp = *(vars->val.counter64); | ||
242 | uint64_t counter = (tmp.high << 32) + tmp.low; | ||
243 | result.response_values[loop_index].value.uIntVal = counter; | ||
244 | result.response_values[loop_index].type = vars->type; | ||
245 | } break; | ||
246 | case ASN_GAUGE: // same as ASN_UNSIGNED | ||
247 | case ASN_TIMETICKS: | ||
248 | case ASN_COUNTER: | ||
249 | case ASN_UINTEGER: { | ||
250 | if (verbose) { | ||
251 | printf("Debug: Got a Integer like\n"); | ||
252 | } | ||
253 | result.response_values[loop_index].value.uIntVal = (unsigned long)*(vars->val.integer); | ||
254 | result.response_values[loop_index].type = vars->type; | ||
255 | } break; | ||
256 | case ASN_INTEGER: { | ||
257 | if (verbose) { | ||
258 | printf("Debug: Got a Integer\n"); | ||
259 | } | ||
260 | result.response_values[loop_index].value.intVal = *(vars->val.integer); | ||
261 | result.response_values[loop_index].type = vars->type; | ||
262 | } break; | ||
263 | case ASN_FLOAT: { | ||
264 | if (verbose) { | ||
265 | printf("Debug: Got a float\n"); | ||
266 | } | ||
267 | result.response_values[loop_index].value.doubleVal = *(vars->val.floatVal); | ||
268 | result.response_values[loop_index].type = vars->type; | ||
269 | } break; | ||
270 | case ASN_DOUBLE: { | ||
271 | if (verbose) { | ||
272 | printf("Debug: Got a double\n"); | ||
273 | } | ||
274 | result.response_values[loop_index].value.doubleVal = *(vars->val.doubleVal); | ||
275 | result.response_values[loop_index].type = vars->type; | ||
276 | } break; | ||
277 | case ASN_IPADDRESS: | ||
278 | if (verbose) { | ||
279 | printf("Debug: Got an IP address\n"); | ||
280 | } | ||
281 | result.response_values[loop_index].type = vars->type; | ||
282 | |||
283 | // TODO: print address here, state always ok? or regex match? | ||
284 | break; | ||
285 | default: | ||
286 | if (verbose) { | ||
287 | printf("Debug: Got a unmatched result type: %hhu\n", vars->type); | ||
288 | } | ||
289 | // TODO: Error here? | ||
290 | break; | ||
291 | } | ||
292 | } | ||
293 | |||
294 | return result; | ||
295 | } | ||
296 | |||
297 | check_snmp_evaluation evaluate_single_unit(response_value response, | ||
298 | check_snmp_evaluation_parameters eval_params, | ||
299 | check_snmp_test_unit test_unit, time_t query_timestamp, | ||
300 | check_snmp_state_entry prev_state, | ||
301 | bool have_previous_state) { | ||
302 | mp_subcheck sc_oid_test = mp_subcheck_init(); | ||
303 | |||
304 | if ((test_unit.label != NULL) && (strcmp(test_unit.label, "") != 0)) { | ||
305 | xasprintf(&sc_oid_test.output, "%s - ", test_unit.label); | ||
306 | } else { | ||
307 | sc_oid_test.output = strdup(""); | ||
308 | } | ||
309 | |||
310 | char oid_string[(MAX_OID_LEN * 2) + 1] = {}; | ||
311 | |||
312 | int oid_string_result = | ||
313 | snprint_objid(oid_string, (MAX_OID_LEN * 2) + 1, response.oid, response.oid_length); | ||
314 | if (oid_string_result <= 0) { | ||
315 | // TODO error here | ||
316 | die(STATE_UNKNOWN, "snprint_objid failed\n"); | ||
317 | } | ||
318 | |||
319 | xasprintf(&sc_oid_test.output, "%sOID: %s", sc_oid_test.output, oid_string); | ||
320 | sc_oid_test = mp_set_subcheck_default_state(sc_oid_test, STATE_OK); | ||
321 | |||
322 | if (verbose > 2) { | ||
323 | printf("Processing oid %s\n", oid_string); | ||
324 | } | ||
325 | |||
326 | bool got_a_numerical_value = false; | ||
327 | mp_perfdata_value pd_result_val = {0}; | ||
328 | |||
329 | check_snmp_state_entry result_state = { | ||
330 | .timestamp = query_timestamp, | ||
331 | .oid_length = response.oid_length, | ||
332 | .type = response.type, | ||
333 | }; | ||
334 | |||
335 | for (size_t i = 0; i < response.oid_length; i++) { | ||
336 | result_state.oid[i] = response.oid[i]; | ||
337 | } | ||
338 | |||
339 | if (have_previous_state) { | ||
340 | if (query_timestamp == prev_state.timestamp) { | ||
341 | // somehow we have the same timestamp again, that can't be good | ||
342 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, STATE_UNKNOWN); | ||
343 | xasprintf(&sc_oid_test.output, "Time duration between plugin calls is invalid"); | ||
344 | |||
345 | check_snmp_evaluation result = { | ||
346 | .sc = sc_oid_test, | ||
347 | .state = result_state, | ||
348 | }; | ||
349 | |||
350 | return result; | ||
351 | } | ||
352 | } | ||
353 | // compute rate time difference | ||
354 | double timeDiff = 0; | ||
355 | if (have_previous_state) { | ||
356 | if (verbose) { | ||
357 | printf("Previous timestamp: %s", ctime(&prev_state.timestamp)); | ||
358 | printf("Current timestamp: %s", ctime(&query_timestamp)); | ||
359 | } | ||
360 | timeDiff = difftime(query_timestamp, prev_state.timestamp) / eval_params.rate_multiplier; | ||
361 | } | ||
362 | |||
363 | mp_perfdata pd_num_val = {}; | ||
364 | |||
365 | switch (response.type) { | ||
366 | case ASN_OCTET_STR: { | ||
367 | char *tmp = response.string_response; | ||
368 | if (strchr(tmp, '"') != NULL) { | ||
369 | // got double quote in the string | ||
370 | if (strchr(tmp, '\'') != NULL) { | ||
371 | // got single quote in the string too | ||
372 | // dont quote that at all to avoid even more confusion | ||
373 | xasprintf(&sc_oid_test.output, "%s - Value: %s", sc_oid_test.output, tmp); | ||
374 | } else { | ||
375 | // quote with single quotes | ||
376 | xasprintf(&sc_oid_test.output, "%s - Value: '%s'", sc_oid_test.output, tmp); | ||
377 | } | ||
378 | } else { | ||
379 | // quote with double quotes | ||
380 | xasprintf(&sc_oid_test.output, "%s - Value: \"%s\"", sc_oid_test.output, tmp); | ||
381 | } | ||
382 | |||
383 | if (strlen(tmp) == 0) { | ||
384 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, eval_params.nulloid_result); | ||
385 | } | ||
386 | |||
387 | // String matching test | ||
388 | if ((test_unit.eval_mthd.crit_string)) { | ||
389 | if (strcmp(tmp, eval_params.string_cmp_value)) { | ||
390 | sc_oid_test = mp_set_subcheck_state( | ||
391 | sc_oid_test, (eval_params.invert_search) ? STATE_CRITICAL : STATE_OK); | ||
392 | } else { | ||
393 | sc_oid_test = mp_set_subcheck_state( | ||
394 | sc_oid_test, (eval_params.invert_search) ? STATE_OK : STATE_CRITICAL); | ||
395 | } | ||
396 | } else if (test_unit.eval_mthd.crit_regex) { | ||
397 | const size_t nmatch = eval_params.regex_cmp_value.re_nsub + 1; | ||
398 | regmatch_t pmatch[nmatch]; | ||
399 | memset(pmatch, '\0', sizeof(regmatch_t) * nmatch); | ||
400 | |||
401 | int excode = regexec(&eval_params.regex_cmp_value, tmp, nmatch, pmatch, 0); | ||
402 | if (excode == 0) { | ||
403 | sc_oid_test = mp_set_subcheck_state( | ||
404 | sc_oid_test, (eval_params.invert_search) ? STATE_OK : STATE_CRITICAL); | ||
405 | } else if (excode != REG_NOMATCH) { | ||
406 | char errbuf[MAX_INPUT_BUFFER] = ""; | ||
407 | regerror(excode, &eval_params.regex_cmp_value, errbuf, MAX_INPUT_BUFFER); | ||
408 | printf(_("Execute Error: %s\n"), errbuf); | ||
409 | exit(STATE_CRITICAL); | ||
410 | } else { // REG_NOMATCH | ||
411 | sc_oid_test = mp_set_subcheck_state( | ||
412 | sc_oid_test, eval_params.invert_search ? STATE_CRITICAL : STATE_OK); | ||
413 | } | ||
414 | } | ||
415 | } break; | ||
416 | case ASN_COUNTER64: | ||
417 | got_a_numerical_value = true; | ||
418 | |||
419 | result_state.value.uIntVal = response.value.uIntVal; | ||
420 | result_state.type = response.type; | ||
421 | |||
422 | // TODO: perfdata unit counter | ||
423 | if (eval_params.calculate_rate && have_previous_state) { | ||
424 | if (prev_state.value.uIntVal > response.value.uIntVal) { | ||
425 | // overflow | ||
426 | unsigned long long tmp = | ||
427 | (UINT64_MAX - prev_state.value.uIntVal) + response.value.uIntVal; | ||
428 | |||
429 | tmp /= timeDiff; | ||
430 | pd_result_val = mp_create_pd_value(tmp); | ||
431 | } else { | ||
432 | pd_result_val = mp_create_pd_value( | ||
433 | (response.value.uIntVal - prev_state.value.uIntVal) / timeDiff); | ||
434 | } | ||
435 | } else { | ||
436 | // It's only a counter if we cont compute rate | ||
437 | pd_num_val.uom = "c"; | ||
438 | pd_result_val = mp_create_pd_value(response.value.uIntVal); | ||
439 | } | ||
440 | break; | ||
441 | case ASN_GAUGE: // same as ASN_UNSIGNED | ||
442 | case ASN_TIMETICKS: | ||
443 | case ASN_COUNTER: | ||
444 | case ASN_UINTEGER: { | ||
445 | got_a_numerical_value = true; | ||
446 | long long treated_value = (long long)response.value.uIntVal; | ||
447 | |||
448 | if (eval_params.multiplier_set || eval_params.offset_set) { | ||
449 | double processed = 0; | ||
450 | if (eval_params.offset_set) { | ||
451 | processed += eval_params.offset; | ||
452 | } | ||
453 | |||
454 | if (eval_params.multiplier_set) { | ||
455 | processed = processed * eval_params.multiplier; | ||
456 | } | ||
457 | |||
458 | treated_value = lround(processed); | ||
459 | } | ||
460 | |||
461 | result_state.value.intVal = treated_value; | ||
462 | |||
463 | if (eval_params.calculate_rate && have_previous_state) { | ||
464 | if (verbose > 2) { | ||
465 | printf("%s: Rate calculation (int/counter/gauge): prev: %lli\n", __FUNCTION__, | ||
466 | prev_state.value.intVal); | ||
467 | printf("%s: Rate calculation (int/counter/gauge): current: %lli\n", __FUNCTION__, | ||
468 | treated_value); | ||
469 | } | ||
470 | double rate = (treated_value - prev_state.value.intVal) / timeDiff; | ||
471 | pd_result_val = mp_create_pd_value(rate); | ||
472 | } else { | ||
473 | pd_result_val = mp_create_pd_value(treated_value); | ||
474 | |||
475 | if (response.type == ASN_COUNTER) { | ||
476 | pd_num_val.uom = "c"; | ||
477 | } | ||
478 | } | ||
479 | |||
480 | } break; | ||
481 | case ASN_INTEGER: { | ||
482 | if (eval_params.multiplier_set || eval_params.offset_set) { | ||
483 | double processed = 0; | ||
484 | if (eval_params.multiplier_set) { | ||
485 | processed = (double)response.value.intVal * eval_params.multiplier; | ||
486 | } | ||
487 | |||
488 | if (eval_params.offset_set) { | ||
489 | processed += eval_params.offset; | ||
490 | } | ||
491 | |||
492 | result_state.value.doubleVal = processed; | ||
493 | |||
494 | if (eval_params.calculate_rate && have_previous_state) { | ||
495 | pd_result_val = | ||
496 | mp_create_pd_value((processed - prev_state.value.doubleVal) / timeDiff); | ||
497 | } else { | ||
498 | pd_result_val = mp_create_pd_value(processed); | ||
499 | } | ||
500 | } else { | ||
501 | result_state.value.intVal = response.value.intVal; | ||
502 | |||
503 | if (eval_params.calculate_rate && have_previous_state) { | ||
504 | pd_result_val = mp_create_pd_value( | ||
505 | (response.value.intVal - prev_state.value.intVal) / timeDiff); | ||
506 | } else { | ||
507 | pd_result_val = mp_create_pd_value(response.value.intVal); | ||
508 | } | ||
509 | } | ||
510 | |||
511 | got_a_numerical_value = true; | ||
512 | } break; | ||
513 | case ASN_FLOAT: // fallthrough | ||
514 | case ASN_DOUBLE: { | ||
515 | got_a_numerical_value = true; | ||
516 | double tmp = response.value.doubleVal; | ||
517 | if (eval_params.offset_set) { | ||
518 | tmp += eval_params.offset; | ||
519 | } | ||
520 | |||
521 | if (eval_params.multiplier_set) { | ||
522 | tmp *= eval_params.multiplier; | ||
523 | } | ||
524 | |||
525 | if (eval_params.calculate_rate && have_previous_state) { | ||
526 | pd_result_val = mp_create_pd_value((tmp - prev_state.value.doubleVal) / timeDiff); | ||
527 | } else { | ||
528 | pd_result_val = mp_create_pd_value(tmp); | ||
529 | } | ||
530 | got_a_numerical_value = true; | ||
531 | |||
532 | result_state.value.doubleVal = tmp; | ||
533 | } break; | ||
534 | case ASN_IPADDRESS: | ||
535 | // TODO | ||
536 | break; | ||
537 | } | ||
538 | |||
539 | if (got_a_numerical_value) { | ||
540 | if (eval_params.use_oid_as_perf_data_label) { | ||
541 | // Use oid for perdata label | ||
542 | pd_num_val.label = strdup(oid_string); | ||
543 | // TODO strdup error checking | ||
544 | } else if (test_unit.label != NULL && strcmp(test_unit.label, "") != 0) { | ||
545 | pd_num_val.label = strdup(test_unit.label); | ||
546 | } else { | ||
547 | pd_num_val.label = strdup(test_unit.oid); | ||
548 | } | ||
549 | |||
550 | if (!(eval_params.calculate_rate && !have_previous_state)) { | ||
551 | // some kind of numerical value | ||
552 | if (test_unit.unit_value != NULL && strcmp(test_unit.unit_value, "") != 0) { | ||
553 | pd_num_val.uom = test_unit.unit_value; | ||
554 | } | ||
555 | |||
556 | pd_num_val.value = pd_result_val; | ||
557 | |||
558 | xasprintf(&sc_oid_test.output, "%s Value: %s", sc_oid_test.output, | ||
559 | pd_value_to_string(pd_result_val)); | ||
560 | |||
561 | if (test_unit.unit_value != NULL && strcmp(test_unit.unit_value, "") != 0) { | ||
562 | xasprintf(&sc_oid_test.output, "%s%s", sc_oid_test.output, test_unit.unit_value); | ||
563 | } | ||
564 | |||
565 | if (test_unit.threshold.warning_is_set || test_unit.threshold.critical_is_set) { | ||
566 | pd_num_val = mp_pd_set_thresholds(pd_num_val, test_unit.threshold); | ||
567 | mp_state_enum tmp_state = mp_get_pd_status(pd_num_val); | ||
568 | |||
569 | if (tmp_state == STATE_WARNING) { | ||
570 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, STATE_WARNING); | ||
571 | xasprintf(&sc_oid_test.output, "%s - number violates warning threshold", | ||
572 | sc_oid_test.output); | ||
573 | } else if (tmp_state == STATE_CRITICAL) { | ||
574 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, STATE_CRITICAL); | ||
575 | xasprintf(&sc_oid_test.output, "%s - number violates critical threshold", | ||
576 | sc_oid_test.output); | ||
577 | } | ||
578 | } | ||
579 | |||
580 | mp_add_perfdata_to_subcheck(&sc_oid_test, pd_num_val); | ||
581 | } else { | ||
582 | // should calculate rate, but there is no previous state, so first run | ||
583 | // exit with ok now | ||
584 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, STATE_OK); | ||
585 | xasprintf(&sc_oid_test.output, "%s - No previous data to calculate rate - assume okay", | ||
586 | sc_oid_test.output); | ||
587 | } | ||
588 | } | ||
589 | |||
590 | check_snmp_evaluation result = { | ||
591 | .sc = sc_oid_test, | ||
592 | .state = result_state, | ||
593 | }; | ||
594 | |||
595 | return result; | ||
596 | } | ||
597 | |||
598 | char *_np_state_generate_key(int argc, char **argv); | ||
599 | |||
600 | /* | ||
601 | * If time=NULL, use current time. Create state file, with state format | ||
602 | * version, default text. Writes version, time, and data. Avoid locking | ||
603 | * problems - use mv to write and then swap. Possible loss of state data if | ||
604 | * two things writing to same key at same time. | ||
605 | * Will die with UNKNOWN if errors | ||
606 | */ | ||
607 | void np_state_write_string(state_key stateKey, time_t timestamp, char *stringToStore) { | ||
608 | time_t current_time; | ||
609 | if (timestamp == 0) { | ||
610 | time(¤t_time); | ||
611 | } else { | ||
612 | current_time = timestamp; | ||
613 | } | ||
614 | |||
615 | int result = 0; | ||
616 | |||
617 | /* If file doesn't currently exist, create directories */ | ||
618 | if (access(stateKey._filename, F_OK) != 0) { | ||
619 | char *directories = NULL; | ||
620 | result = asprintf(&directories, "%s", stateKey._filename); | ||
621 | if (result < 0) { | ||
622 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
623 | } | ||
624 | |||
625 | for (char *p = directories + 1; *p; p++) { | ||
626 | if (*p == '/') { | ||
627 | *p = '\0'; | ||
628 | if ((access(directories, F_OK) != 0) && (mkdir(directories, S_IRWXU) != 0)) { | ||
629 | /* Can't free this! Otherwise error message is wrong! */ | ||
630 | /* np_free(directories); */ | ||
631 | die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); | ||
632 | } | ||
633 | *p = '/'; | ||
634 | } | ||
635 | } | ||
636 | |||
637 | if (directories) { | ||
638 | free(directories); | ||
639 | } | ||
640 | } | ||
641 | |||
642 | char *temp_file = NULL; | ||
643 | result = asprintf(&temp_file, "%s.XXXXXX", stateKey._filename); | ||
644 | if (result < 0) { | ||
645 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
646 | } | ||
647 | |||
648 | int temp_file_desc = 0; | ||
649 | if ((temp_file_desc = mkstemp(temp_file)) == -1) { | ||
650 | if (temp_file) { | ||
651 | free(temp_file); | ||
652 | } | ||
653 | die(STATE_UNKNOWN, _("Cannot create temporary filename")); | ||
654 | } | ||
655 | |||
656 | FILE *temp_file_pointer = fdopen(temp_file_desc, "w"); | ||
657 | if (temp_file_pointer == NULL) { | ||
658 | close(temp_file_desc); | ||
659 | unlink(temp_file); | ||
660 | if (temp_file) { | ||
661 | free(temp_file); | ||
662 | } | ||
663 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); | ||
664 | } | ||
665 | |||
666 | fprintf(temp_file_pointer, "# NP State file\n"); | ||
667 | fprintf(temp_file_pointer, "%d\n", NP_STATE_FORMAT_VERSION); | ||
668 | fprintf(temp_file_pointer, "%d\n", stateKey.data_version); | ||
669 | fprintf(temp_file_pointer, "%lu\n", current_time); | ||
670 | fprintf(temp_file_pointer, "%s\n", stringToStore); | ||
671 | |||
672 | fchmod(temp_file_desc, S_IRUSR | S_IWUSR | S_IRGRP); | ||
673 | |||
674 | fflush(temp_file_pointer); | ||
675 | |||
676 | result = fclose(temp_file_pointer); | ||
677 | |||
678 | fsync(temp_file_desc); | ||
679 | |||
680 | if (result != 0) { | ||
681 | unlink(temp_file); | ||
682 | if (temp_file) { | ||
683 | free(temp_file); | ||
684 | } | ||
685 | die(STATE_UNKNOWN, _("Error writing temp file")); | ||
686 | } | ||
687 | |||
688 | if (rename(temp_file, stateKey._filename) != 0) { | ||
689 | unlink(temp_file); | ||
690 | if (temp_file) { | ||
691 | free(temp_file); | ||
692 | } | ||
693 | die(STATE_UNKNOWN, _("Cannot rename state temp file")); | ||
694 | } | ||
695 | |||
696 | if (temp_file) { | ||
697 | free(temp_file); | ||
698 | } | ||
699 | } | ||
700 | |||
701 | /* | ||
702 | * Read the state file | ||
703 | */ | ||
704 | bool _np_state_read_file(FILE *state_file, state_key stateKey) { | ||
705 | time_t current_time; | ||
706 | time(¤t_time); | ||
707 | |||
708 | /* Note: This introduces a limit of 8192 bytes in the string data */ | ||
709 | char *line = (char *)calloc(1, 8192); | ||
710 | if (line == NULL) { | ||
711 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
712 | } | ||
713 | |||
714 | bool status = false; | ||
715 | enum { | ||
716 | STATE_FILE_VERSION, | ||
717 | STATE_DATA_VERSION, | ||
718 | STATE_DATA_TIME, | ||
719 | STATE_DATA_TEXT, | ||
720 | STATE_DATA_END | ||
721 | } expected = STATE_FILE_VERSION; | ||
722 | |||
723 | int failure = 0; | ||
724 | while (!failure && (fgets(line, 8192, state_file)) != NULL) { | ||
725 | size_t pos = strlen(line); | ||
726 | if (line[pos - 1] == '\n') { | ||
727 | line[pos - 1] = '\0'; | ||
728 | } | ||
729 | |||
730 | if (line[0] == '#') { | ||
731 | continue; | ||
732 | } | ||
733 | |||
734 | switch (expected) { | ||
735 | case STATE_FILE_VERSION: { | ||
736 | int i = atoi(line); | ||
737 | if (i != NP_STATE_FORMAT_VERSION) { | ||
738 | failure++; | ||
739 | } else { | ||
740 | expected = STATE_DATA_VERSION; | ||
741 | } | ||
742 | } break; | ||
743 | case STATE_DATA_VERSION: { | ||
744 | int i = atoi(line); | ||
745 | if (i != stateKey.data_version) { | ||
746 | failure++; | ||
747 | } else { | ||
748 | expected = STATE_DATA_TIME; | ||
749 | } | ||
750 | } break; | ||
751 | case STATE_DATA_TIME: { | ||
752 | /* If time > now, error */ | ||
753 | time_t data_time = strtoul(line, NULL, 10); | ||
754 | if (data_time > current_time) { | ||
755 | failure++; | ||
756 | } else { | ||
757 | stateKey.state_data->time = data_time; | ||
758 | expected = STATE_DATA_TEXT; | ||
759 | } | ||
760 | } break; | ||
761 | case STATE_DATA_TEXT: | ||
762 | stateKey.state_data->data = strdup(line); | ||
763 | if (stateKey.state_data->data == NULL) { | ||
764 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
765 | } | ||
766 | stateKey.state_data->length = strlen(line); | ||
767 | expected = STATE_DATA_END; | ||
768 | status = true; | ||
769 | break; | ||
770 | case STATE_DATA_END:; | ||
771 | } | ||
772 | } | ||
773 | |||
774 | if (line) { | ||
775 | free(line); | ||
776 | } | ||
777 | return status; | ||
778 | } | ||
779 | /* | ||
780 | * Will return NULL if no data is available (first run). If key currently | ||
781 | * exists, read data. If state file format version is not expected, return | ||
782 | * as if no data. Get state data version number and compares to expected. | ||
783 | * If numerically lower, then return as no previous state. die with UNKNOWN | ||
784 | * if exceptional error. | ||
785 | */ | ||
786 | state_data *np_state_read(state_key stateKey) { | ||
787 | /* Open file. If this fails, no previous state found */ | ||
788 | FILE *statefile = fopen(stateKey._filename, "r"); | ||
789 | state_data *this_state_data = (state_data *)calloc(1, sizeof(state_data)); | ||
790 | if (statefile != NULL) { | ||
791 | |||
792 | if (this_state_data == NULL) { | ||
793 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
794 | } | ||
795 | |||
796 | this_state_data->data = NULL; | ||
797 | stateKey.state_data = this_state_data; | ||
798 | |||
799 | if (_np_state_read_file(statefile, stateKey)) { | ||
800 | this_state_data->errorcode = OK; | ||
801 | } else { | ||
802 | this_state_data->errorcode = ERROR; | ||
803 | } | ||
804 | |||
805 | fclose(statefile); | ||
806 | } else { | ||
807 | // Failed to open state file | ||
808 | this_state_data->errorcode = ERROR; | ||
809 | } | ||
810 | |||
811 | return stateKey.state_data; | ||
812 | } | ||
813 | |||
814 | /* | ||
815 | * Internal function. Returns either: | ||
816 | * envvar NAGIOS_PLUGIN_STATE_DIRECTORY | ||
817 | * statically compiled shared state directory | ||
818 | */ | ||
819 | char *_np_state_calculate_location_prefix(void) { | ||
820 | char *env_dir; | ||
821 | |||
822 | /* Do not allow passing MP_STATE_PATH in setuid plugins | ||
823 | * for security reasons */ | ||
824 | if (!mp_suid()) { | ||
825 | env_dir = getenv("MP_STATE_PATH"); | ||
826 | if (env_dir && env_dir[0] != '\0') { | ||
827 | return env_dir; | ||
828 | } | ||
829 | /* This is the former ENV, for backward-compatibility */ | ||
830 | env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); | ||
831 | if (env_dir && env_dir[0] != '\0') { | ||
832 | return env_dir; | ||
833 | } | ||
834 | } | ||
835 | |||
836 | return NP_STATE_DIR_PREFIX; | ||
837 | } | ||
838 | |||
839 | /* | ||
840 | * Initiatializer for state routines. | ||
841 | * Sets variables. Generates filename. Returns np_state_key. die with | ||
842 | * UNKNOWN if exception | ||
843 | */ | ||
844 | state_key np_enable_state(char *keyname, int expected_data_version, char *plugin_name, int argc, | ||
845 | char **argv) { | ||
846 | state_key *this_state = (state_key *)calloc(1, sizeof(state_key)); | ||
847 | if (this_state == NULL) { | ||
848 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
849 | } | ||
850 | |||
851 | char *temp_keyname = NULL; | ||
852 | if (keyname == NULL) { | ||
853 | temp_keyname = _np_state_generate_key(argc, argv); | ||
854 | } else { | ||
855 | temp_keyname = strdup(keyname); | ||
856 | if (temp_keyname == NULL) { | ||
857 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
858 | } | ||
859 | } | ||
860 | |||
861 | /* Die if invalid characters used for keyname */ | ||
862 | char *tmp_char = temp_keyname; | ||
863 | while (*tmp_char != '\0') { | ||
864 | if (!(isalnum(*tmp_char) || *tmp_char == '_')) { | ||
865 | die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'")); | ||
866 | } | ||
867 | tmp_char++; | ||
868 | } | ||
869 | this_state->name = temp_keyname; | ||
870 | this_state->plugin_name = plugin_name; | ||
871 | this_state->data_version = expected_data_version; | ||
872 | this_state->state_data = NULL; | ||
873 | |||
874 | /* Calculate filename */ | ||
875 | char *temp_filename = NULL; | ||
876 | int error = asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), | ||
877 | (unsigned long)geteuid(), plugin_name, this_state->name); | ||
878 | if (error < 0) { | ||
879 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
880 | } | ||
881 | |||
882 | this_state->_filename = temp_filename; | ||
883 | |||
884 | return *this_state; | ||
885 | } | ||
886 | |||
887 | /* | ||
888 | * Returns a string to use as a keyname, based on an md5 hash of argv, thus | ||
889 | * hopefully a unique key per service/plugin invocation. Use the extra-opts | ||
890 | * parse of argv, so that uniqueness in parameters are reflected there. | ||
891 | */ | ||
892 | char *_np_state_generate_key(int argc, char **argv) { | ||
893 | unsigned char result[256]; | ||
894 | |||
895 | #ifdef USE_OPENSSL | ||
896 | /* | ||
897 | * This code path is chosen if openssl is available (which should be the most common | ||
898 | * scenario). Alternatively, the gnulib implementation/ | ||
899 | * | ||
900 | */ | ||
901 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | ||
902 | |||
903 | EVP_DigestInit(ctx, EVP_sha256()); | ||
904 | |||
905 | for (int i = 0; i < argc; i++) { | ||
906 | EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); | ||
907 | } | ||
908 | |||
909 | EVP_DigestFinal(ctx, result, NULL); | ||
910 | #else | ||
911 | |||
912 | struct sha256_ctx ctx; | ||
913 | |||
914 | for (int i = 0; i < this_monitoring_plugin->argc; i++) { | ||
915 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); | ||
916 | } | ||
917 | |||
918 | sha256_finish_ctx(&ctx, result); | ||
919 | #endif // FOUNDOPENSSL | ||
920 | |||
921 | char keyname[41]; | ||
922 | for (int i = 0; i < 20; ++i) { | ||
923 | sprintf(&keyname[2 * i], "%02x", result[i]); | ||
924 | } | ||
925 | |||
926 | keyname[40] = '\0'; | ||
927 | |||
928 | char *keyname_copy = strdup(keyname); | ||
929 | if (keyname_copy == NULL) { | ||
930 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
931 | } | ||
932 | |||
933 | return keyname_copy; | ||
934 | } | ||
diff --git a/plugins/check_snmp.d/check_snmp_helpers.h b/plugins/check_snmp.d/check_snmp_helpers.h new file mode 100644 index 00000000..0f7780b1 --- /dev/null +++ b/plugins/check_snmp.d/check_snmp_helpers.h | |||
@@ -0,0 +1,71 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "./config.h" | ||
4 | #include <net-snmp/library/asn1.h> | ||
5 | |||
6 | check_snmp_test_unit check_snmp_test_unit_init(); | ||
7 | int check_snmp_set_thresholds(const char *, check_snmp_test_unit[], size_t, bool); | ||
8 | check_snmp_config check_snmp_config_init(); | ||
9 | |||
10 | typedef struct { | ||
11 | oid oid[MAX_OID_LEN]; | ||
12 | size_t oid_length; | ||
13 | unsigned char type; | ||
14 | union { | ||
15 | uint64_t uIntVal; | ||
16 | int64_t intVal; | ||
17 | double doubleVal; | ||
18 | } value; | ||
19 | char *string_response; | ||
20 | } response_value; | ||
21 | |||
22 | typedef struct { | ||
23 | int errorcode; | ||
24 | response_value *response_values; | ||
25 | } snmp_responces; | ||
26 | snmp_responces do_snmp_query(check_snmp_config_snmp_parameters parameters); | ||
27 | |||
28 | // state is similar to response, but only numerics and a timestamp | ||
29 | typedef struct { | ||
30 | time_t timestamp; | ||
31 | oid oid[MAX_OID_LEN]; | ||
32 | size_t oid_length; | ||
33 | unsigned char type; | ||
34 | union { | ||
35 | unsigned long long uIntVal; | ||
36 | long long intVal; | ||
37 | double doubleVal; | ||
38 | } value; | ||
39 | } check_snmp_state_entry; | ||
40 | |||
41 | typedef struct { | ||
42 | check_snmp_state_entry state; | ||
43 | mp_subcheck sc; | ||
44 | } check_snmp_evaluation; | ||
45 | check_snmp_evaluation evaluate_single_unit(response_value response, | ||
46 | check_snmp_evaluation_parameters eval_params, | ||
47 | check_snmp_test_unit test_unit, time_t query_timestamp, | ||
48 | check_snmp_state_entry prev_state, | ||
49 | bool have_previous_state); | ||
50 | |||
51 | #define NP_STATE_FORMAT_VERSION 1 | ||
52 | |||
53 | typedef struct state_data_struct { | ||
54 | time_t time; | ||
55 | void *data; | ||
56 | size_t length; /* Of binary data */ | ||
57 | int errorcode; | ||
58 | } state_data; | ||
59 | |||
60 | typedef struct state_key_struct { | ||
61 | char *name; | ||
62 | char *plugin_name; | ||
63 | int data_version; | ||
64 | char *_filename; | ||
65 | state_data *state_data; | ||
66 | } state_key; | ||
67 | |||
68 | state_data *np_state_read(state_key stateKey); | ||
69 | state_key np_enable_state(char *keyname, int expected_data_version, char *plugin_name, int argc, | ||
70 | char **argv); | ||
71 | void np_state_write_string(state_key stateKey, time_t timestamp, char *stringToStore); | ||
diff --git a/plugins/check_snmp.d/config.h b/plugins/check_snmp.d/config.h new file mode 100644 index 00000000..e7b6d1b3 --- /dev/null +++ b/plugins/check_snmp.d/config.h | |||
@@ -0,0 +1,81 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../lib/thresholds.h" | ||
4 | #include "../../lib/states.h" | ||
5 | #include <stdlib.h> | ||
6 | #include <stdbool.h> | ||
7 | #include <regex.h> | ||
8 | #include "../common.h" | ||
9 | |||
10 | // defines for snmp libs | ||
11 | #define u_char unsigned char | ||
12 | #define u_long unsigned long | ||
13 | #define u_short unsigned short | ||
14 | #define u_int unsigned int | ||
15 | |||
16 | #include <net-snmp/net-snmp-config.h> | ||
17 | #include <net-snmp/net-snmp-includes.h> | ||
18 | #include <net-snmp/library/snmp.h> | ||
19 | #include <net-snmp/session_api.h> | ||
20 | |||
21 | #define DEFAULT_PORT "161" | ||
22 | #define DEFAULT_RETRIES 5 | ||
23 | |||
24 | typedef struct eval_method { | ||
25 | bool crit_string; | ||
26 | bool crit_regex; | ||
27 | } eval_method; | ||
28 | |||
29 | typedef struct check_snmp_test_unit { | ||
30 | char *oid; | ||
31 | char *label; | ||
32 | char *unit_value; | ||
33 | eval_method eval_mthd; | ||
34 | mp_thresholds threshold; | ||
35 | } check_snmp_test_unit; | ||
36 | |||
37 | typedef struct { | ||
38 | struct snmp_session snmp_session; | ||
39 | // use getnet instead of get | ||
40 | bool use_getnext; | ||
41 | |||
42 | // TODO actually make these useful | ||
43 | bool ignore_mib_parsing_errors; | ||
44 | bool need_mibs; | ||
45 | |||
46 | check_snmp_test_unit *test_units; | ||
47 | size_t num_of_test_units; | ||
48 | } check_snmp_config_snmp_parameters; | ||
49 | |||
50 | typedef struct { | ||
51 | // State if an empty value is encountered | ||
52 | mp_state_enum nulloid_result; | ||
53 | |||
54 | // String evaluation stuff | ||
55 | bool invert_search; | ||
56 | regex_t regex_cmp_value; // regex to match query results against | ||
57 | char string_cmp_value[MAX_INPUT_BUFFER]; | ||
58 | |||
59 | // Modify data | ||
60 | double multiplier; | ||
61 | bool multiplier_set; | ||
62 | double offset; | ||
63 | bool offset_set; | ||
64 | |||
65 | // Modify output | ||
66 | bool use_oid_as_perf_data_label; | ||
67 | |||
68 | // activate rate calculation | ||
69 | bool calculate_rate; | ||
70 | unsigned int rate_multiplier; | ||
71 | } check_snmp_evaluation_parameters; | ||
72 | |||
73 | typedef struct check_snmp_config { | ||
74 | // SNMP session to use | ||
75 | check_snmp_config_snmp_parameters snmp_params; | ||
76 | |||
77 | check_snmp_evaluation_parameters evaluation_params; | ||
78 | |||
79 | mp_output_format output_format; | ||
80 | bool output_format_is_set; | ||
81 | } check_snmp_config; | ||
diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 9d0d7cde..f6c8d551 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c | |||
@@ -57,7 +57,8 @@ static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv* | |||
57 | static void print_help(void); | 57 | static void print_help(void); |
58 | void print_usage(void); | 58 | void print_usage(void); |
59 | 59 | ||
60 | static int ssh_connect(mp_check *overall, char *haddr, int hport, char *remote_version, char *remote_protocol); | 60 | static int ssh_connect(mp_check *overall, char *haddr, int hport, char *remote_version, |
61 | char *remote_protocol); | ||
61 | 62 | ||
62 | int main(int argc, char **argv) { | 63 | int main(int argc, char **argv) { |
63 | setlocale(LC_ALL, ""); | 64 | setlocale(LC_ALL, ""); |
@@ -85,7 +86,8 @@ int main(int argc, char **argv) { | |||
85 | alarm(socket_timeout); | 86 | alarm(socket_timeout); |
86 | 87 | ||
87 | /* ssh_connect exits if error is found */ | 88 | /* ssh_connect exits if error is found */ |
88 | ssh_connect(&overall, config.server_name, config.port, config.remote_version, config.remote_protocol); | 89 | ssh_connect(&overall, config.server_name, config.port, config.remote_version, |
90 | config.remote_protocol); | ||
89 | 91 | ||
90 | alarm(0); | 92 | alarm(0); |
91 | 93 | ||
@@ -96,19 +98,20 @@ int main(int argc, char **argv) { | |||
96 | 98 | ||
97 | /* process command-line arguments */ | 99 | /* process command-line arguments */ |
98 | process_arguments_wrapper process_arguments(int argc, char **argv) { | 100 | process_arguments_wrapper process_arguments(int argc, char **argv) { |
99 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, | 101 | static struct option longopts[] = { |
100 | {"version", no_argument, 0, 'V'}, | 102 | {"help", no_argument, 0, 'h'}, |
101 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ | 103 | {"version", no_argument, 0, 'V'}, |
102 | {"hostname", required_argument, 0, 'H'}, | 104 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ |
103 | {"port", required_argument, 0, 'p'}, | 105 | {"hostname", required_argument, 0, 'H'}, |
104 | {"use-ipv4", no_argument, 0, '4'}, | 106 | {"port", required_argument, 0, 'p'}, |
105 | {"use-ipv6", no_argument, 0, '6'}, | 107 | {"use-ipv4", no_argument, 0, '4'}, |
106 | {"timeout", required_argument, 0, 't'}, | 108 | {"use-ipv6", no_argument, 0, '6'}, |
107 | {"verbose", no_argument, 0, 'v'}, | 109 | {"timeout", required_argument, 0, 't'}, |
108 | {"remote-version", required_argument, 0, 'r'}, | 110 | {"verbose", no_argument, 0, 'v'}, |
109 | {"remote-protocol", required_argument, 0, 'P'}, | 111 | {"remote-version", required_argument, 0, 'r'}, |
110 | {"output-format", required_argument, 0, output_format_index}, | 112 | {"remote-protocol", required_argument, 0, 'P'}, |
111 | {0, 0, 0, 0}}; | 113 | {"output-format", required_argument, 0, output_format_index}, |
114 | {0, 0, 0, 0}}; | ||
112 | 115 | ||
113 | process_arguments_wrapper result = { | 116 | process_arguments_wrapper result = { |
114 | .config = check_ssh_config_init(), | 117 | .config = check_ssh_config_init(), |
@@ -228,7 +231,8 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { | |||
228 | * | 231 | * |
229 | *-----------------------------------------------------------------------*/ | 232 | *-----------------------------------------------------------------------*/ |
230 | 233 | ||
231 | int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_version, char *desired_remote_protocol) { | 234 | int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_version, |
235 | char *desired_remote_protocol) { | ||
232 | struct timeval tv; | 236 | struct timeval tv; |
233 | gettimeofday(&tv, NULL); | 237 | gettimeofday(&tv, NULL); |
234 | 238 | ||
@@ -238,32 +242,34 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ | |||
238 | mp_subcheck connection_sc = mp_subcheck_init(); | 242 | mp_subcheck connection_sc = mp_subcheck_init(); |
239 | if (result != STATE_OK) { | 243 | if (result != STATE_OK) { |
240 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); | 244 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); |
241 | xasprintf(&connection_sc.output, "Failed to establish TCP connection to Host %s and Port %d", haddr, hport); | 245 | xasprintf(&connection_sc.output, |
246 | "Failed to establish TCP connection to Host %s and Port %d", haddr, hport); | ||
242 | mp_add_subcheck_to_check(overall, connection_sc); | 247 | mp_add_subcheck_to_check(overall, connection_sc); |
243 | return result; | 248 | return result; |
244 | } | 249 | } |
245 | 250 | ||
246 | char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); | 251 | char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); |
247 | char *buffer = NULL; | 252 | char *buffer = NULL; |
248 | size_t recv_ret = 0; | 253 | ssize_t recv_ret = 0; |
249 | char *version_control_string = NULL; | 254 | char *version_control_string = NULL; |
250 | size_t byte_offset = 0; | 255 | size_t byte_offset = 0; |
251 | while ((version_control_string == NULL) && | 256 | while ((version_control_string == NULL) && |
252 | (recv_ret = recv(socket, output + byte_offset, (unsigned long)(BUFF_SZ - byte_offset), 0) > 0)) { | 257 | (recv_ret = recv(socket, output + byte_offset, (unsigned long)(BUFF_SZ - byte_offset), |
258 | 0) > 0)) { | ||
253 | 259 | ||
254 | if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ | 260 | if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ |
255 | byte_offset = 0; | 261 | byte_offset = 0; |
256 | 262 | ||
257 | char *index = NULL; | 263 | char *index = NULL; |
258 | unsigned long len = 0; | ||
259 | while ((index = strchr(output + byte_offset, '\n')) != NULL) { | 264 | while ((index = strchr(output + byte_offset, '\n')) != NULL) { |
260 | /*Partition the buffer so that this line is a separate string, | 265 | /*Partition the buffer so that this line is a separate string, |
261 | * by replacing the newline with NUL*/ | 266 | * by replacing the newline with NUL*/ |
262 | output[(index - output)] = '\0'; | 267 | output[(index - output)] = '\0'; |
263 | len = strlen(output + byte_offset); | 268 | size_t len = strlen(output + byte_offset); |
264 | 269 | ||
265 | if ((len >= 4) && (strncmp(output + byte_offset, "SSH-", 4) == 0)) { | 270 | if ((len >= 4) && (strncmp(output + byte_offset, "SSH-", 4) == 0)) { |
266 | /*if the string starts with SSH-, this _should_ be a valid version control string*/ | 271 | /*if the string starts with SSH-, this _should_ be a valid version control |
272 | * string*/ | ||
267 | version_control_string = output + byte_offset; | 273 | version_control_string = output + byte_offset; |
268 | break; | 274 | break; |
269 | } | 275 | } |
@@ -273,21 +279,23 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ | |||
273 | } | 279 | } |
274 | 280 | ||
275 | if (version_control_string == NULL) { | 281 | if (version_control_string == NULL) { |
276 | /* move unconsumed data to beginning of buffer, null rest */ | 282 | /* move unconsumed data to beginning of buffer */ |
277 | memmove((void *)output, (void *)(output + byte_offset + 1), BUFF_SZ - len + 1); | 283 | memmove((void *)output, (void *)(output + byte_offset), BUFF_SZ - byte_offset); |
278 | memset(output + byte_offset + 1, 0, BUFF_SZ - byte_offset + 1); | ||
279 | 284 | ||
280 | /*start reading from end of current line chunk on next recv*/ | 285 | /*start reading from end of current line chunk on next recv*/ |
281 | byte_offset = strlen(output); | 286 | byte_offset = strlen(output); |
287 | |||
288 | /* NUL the rest of the buffer */ | ||
289 | memset(output + byte_offset, 0, BUFF_SZ - byte_offset); | ||
282 | } | 290 | } |
283 | } else { | 291 | } else { |
284 | byte_offset += recv_ret; | 292 | byte_offset += (size_t)recv_ret; |
285 | } | 293 | } |
286 | } | 294 | } |
287 | 295 | ||
288 | if (recv_ret < 0) { | 296 | if (recv_ret < 0) { |
289 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); | 297 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); |
290 | xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - %s", strerror(errno)); | 298 | xasprintf(&connection_sc.output, "%s - %s", "SSH CRITICAL - ", strerror(errno)); |
291 | mp_add_subcheck_to_check(overall, connection_sc); | 299 | mp_add_subcheck_to_check(overall, connection_sc); |
292 | return OK; | 300 | return OK; |
293 | } | 301 | } |
@@ -333,7 +341,8 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ | |||
333 | * "1.x" (e.g., "1.5" or "1.3")." | 341 | * "1.x" (e.g., "1.5" or "1.3")." |
334 | * - RFC 4253:5 | 342 | * - RFC 4253:5 |
335 | */ | 343 | */ |
336 | char *ssh_server = ssh_proto + strspn(ssh_proto, "0123456789.") + 1; /* (+1 for the '-' separating protoversion from softwareversion) */ | 344 | char *ssh_server = ssh_proto + strspn(ssh_proto, "0123456789.") + |
345 | 1; /* (+1 for the '-' separating protoversion from softwareversion) */ | ||
337 | 346 | ||
338 | /* If there's a space in the version string, whatever's after the space is a comment | 347 | /* If there's a space in the version string, whatever's after the space is a comment |
339 | * (which is NOT part of the server name/version)*/ | 348 | * (which is NOT part of the server name/version)*/ |
@@ -345,13 +354,15 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ | |||
345 | mp_subcheck protocol_validity_sc = mp_subcheck_init(); | 354 | mp_subcheck protocol_validity_sc = mp_subcheck_init(); |
346 | if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) { | 355 | if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) { |
347 | protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_CRITICAL); | 356 | protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_CRITICAL); |
348 | xasprintf(&protocol_validity_sc.output, "Invalid protocol version control string %s", version_control_string); | 357 | xasprintf(&protocol_validity_sc.output, "Invalid protocol version control string %s", |
358 | version_control_string); | ||
349 | mp_add_subcheck_to_check(overall, protocol_validity_sc); | 359 | mp_add_subcheck_to_check(overall, protocol_validity_sc); |
350 | return OK; | 360 | return OK; |
351 | } | 361 | } |
352 | 362 | ||
353 | protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_OK); | 363 | protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_OK); |
354 | xasprintf(&protocol_validity_sc.output, "Valid protocol version control string %s", version_control_string); | 364 | xasprintf(&protocol_validity_sc.output, "Valid protocol version control string %s", |
365 | version_control_string); | ||
355 | mp_add_subcheck_to_check(overall, protocol_validity_sc); | 366 | mp_add_subcheck_to_check(overall, protocol_validity_sc); |
356 | 367 | ||
357 | ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; | 368 | ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; |
@@ -366,8 +377,8 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ | |||
366 | if (desired_remote_version && strcmp(desired_remote_version, ssh_server)) { | 377 | if (desired_remote_version && strcmp(desired_remote_version, ssh_server)) { |
367 | mp_subcheck remote_version_sc = mp_subcheck_init(); | 378 | mp_subcheck remote_version_sc = mp_subcheck_init(); |
368 | remote_version_sc = mp_set_subcheck_state(remote_version_sc, STATE_CRITICAL); | 379 | remote_version_sc = mp_set_subcheck_state(remote_version_sc, STATE_CRITICAL); |
369 | xasprintf(&remote_version_sc.output, _("%s (protocol %s) version mismatch, expected '%s'"), ssh_server, ssh_proto, | 380 | xasprintf(&remote_version_sc.output, _("%s (protocol %s) version mismatch, expected '%s'"), |
370 | desired_remote_version); | 381 | ssh_server, ssh_proto, desired_remote_version); |
371 | close(socket); | 382 | close(socket); |
372 | mp_add_subcheck_to_check(overall, remote_version_sc); | 383 | mp_add_subcheck_to_check(overall, remote_version_sc); |
373 | return OK; | 384 | return OK; |
@@ -385,11 +396,13 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ | |||
385 | 396 | ||
386 | if (desired_remote_protocol && strcmp(desired_remote_protocol, ssh_proto)) { | 397 | if (desired_remote_protocol && strcmp(desired_remote_protocol, ssh_proto)) { |
387 | protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_CRITICAL); | 398 | protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_CRITICAL); |
388 | xasprintf(&protocol_version_sc.output, _("%s (protocol %s) protocol version mismatch, expected '%s'"), ssh_server, ssh_proto, | 399 | xasprintf(&protocol_version_sc.output, |
389 | desired_remote_protocol); | 400 | _("%s (protocol %s) protocol version mismatch, expected '%s'"), ssh_server, |
401 | ssh_proto, desired_remote_protocol); | ||
390 | } else { | 402 | } else { |
391 | protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_OK); | 403 | protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_OK); |
392 | xasprintf(&protocol_version_sc.output, "SSH server version: %s (protocol version: %s)", ssh_server, ssh_proto); | 404 | xasprintf(&protocol_version_sc.output, "SSH server version: %s (protocol version: %s)", |
405 | ssh_server, ssh_proto); | ||
393 | } | 406 | } |
394 | 407 | ||
395 | mp_add_subcheck_to_check(overall, protocol_version_sc); | 408 | mp_add_subcheck_to_check(overall, protocol_version_sc); |
@@ -422,7 +435,8 @@ void print_help(void) { | |||
422 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 435 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
423 | 436 | ||
424 | printf(" %s\n", "-r, --remote-version=STRING"); | 437 | printf(" %s\n", "-r, --remote-version=STRING"); |
425 | printf(" %s\n", _("Alert if string doesn't match expected server version (ex: OpenSSH_3.9p1)")); | 438 | printf(" %s\n", |
439 | _("Alert if string doesn't match expected server version (ex: OpenSSH_3.9p1)")); | ||
426 | 440 | ||
427 | printf(" %s\n", "-P, --remote-protocol=STRING"); | 441 | printf(" %s\n", "-P, --remote-protocol=STRING"); |
428 | printf(" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)")); | 442 | printf(" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)")); |
@@ -435,5 +449,6 @@ void print_help(void) { | |||
435 | 449 | ||
436 | void print_usage(void) { | 450 | void print_usage(void) { |
437 | printf("%s\n", _("Usage:")); | 451 | printf("%s\n", _("Usage:")); |
438 | printf("%s [-4|-6] [-t <timeout>] [-r <remote version>] [-p <port>] --hostname <host>\n", progname); | 452 | printf("%s [-4|-6] [-t <timeout>] [-r <remote version>] [-p <port>] --hostname <host>\n", |
453 | progname); | ||
439 | } | 454 | } |
diff --git a/plugins/check_swap.c b/plugins/check_swap.c index cb95949a..dbf53a00 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c | |||
@@ -90,6 +90,14 @@ int main(int argc, char **argv) { | |||
90 | exit(STATE_UNKNOWN); | 90 | exit(STATE_UNKNOWN); |
91 | } | 91 | } |
92 | 92 | ||
93 | if (verbose) { | ||
94 | printf("Swap retrieval result:\n" | ||
95 | "\tFree: %llu\n" | ||
96 | "\tUsed: %llu\n" | ||
97 | "\tTotal: %llu\n", | ||
98 | data.metrics.free, data.metrics.used, data.metrics.total); | ||
99 | } | ||
100 | |||
93 | double percent_used; | 101 | double percent_used; |
94 | mp_check overall = mp_check_init(); | 102 | mp_check overall = mp_check_init(); |
95 | if (config.output_format_is_set) { | 103 | if (config.output_format_is_set) { |
@@ -164,7 +172,8 @@ int main(int argc, char **argv) { | |||
164 | } | 172 | } |
165 | 173 | ||
166 | if (config.warn_is_set) { | 174 | if (config.warn_is_set) { |
167 | if ((config.warn.is_percentage && (percent_used >= (100 - (double)config.warn.value))) || config.warn.value >= data.metrics.free) { | 175 | if ((config.warn.is_percentage && (percent_used >= (100 - (double)config.warn.value))) || |
176 | config.warn.value >= data.metrics.free) { | ||
168 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | 177 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); |
169 | } | 178 | } |
170 | } | 179 | } |
@@ -174,13 +183,14 @@ int main(int argc, char **argv) { | |||
174 | } | 183 | } |
175 | 184 | ||
176 | if (config.crit_is_set) { | 185 | if (config.crit_is_set) { |
177 | if ((config.crit.is_percentage && (percent_used >= (100 - (double)config.crit.value))) || config.crit.value >= data.metrics.free) { | 186 | if ((config.crit.is_percentage && (percent_used >= (100 - (double)config.crit.value))) || |
187 | config.crit.value >= data.metrics.free) { | ||
178 | sc1 = mp_set_subcheck_state(sc1, STATE_CRITICAL); | 188 | sc1 = mp_set_subcheck_state(sc1, STATE_CRITICAL); |
179 | } | 189 | } |
180 | } | 190 | } |
181 | 191 | ||
182 | xasprintf(&sc1.output, _("%g%% free (%lluMiB out of %lluMiB)"), (100 - percent_used), data.metrics.free >> 20, | 192 | xasprintf(&sc1.output, _("%g%% free (%lluMiB out of %lluMiB)"), (100 - percent_used), |
183 | data.metrics.total >> 20); | 193 | data.metrics.free >> 20, data.metrics.total >> 20); |
184 | 194 | ||
185 | overall.summary = "Swap"; | 195 | overall.summary = "Swap"; |
186 | mp_add_subcheck_to_check(&overall, sc1); | 196 | mp_add_subcheck_to_check(&overall, sc1); |
@@ -193,7 +203,9 @@ int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { | |||
193 | return config.no_swap_state; | 203 | return config.no_swap_state; |
194 | } | 204 | } |
195 | 205 | ||
196 | uint64_t free_swap = (uint64_t)(free_swap_mb * (1024 * 1024)); /* Convert back to bytes as warn and crit specified in bytes */ | 206 | uint64_t free_swap = |
207 | (uint64_t)(free_swap_mb * | ||
208 | (1024 * 1024)); /* Convert back to bytes as warn and crit specified in bytes */ | ||
197 | 209 | ||
198 | if (!config.crit.is_percentage && config.crit.value >= free_swap) { | 210 | if (!config.crit.is_percentage && config.crit.value >= free_swap) { |
199 | return STATE_CRITICAL; | 211 | return STATE_CRITICAL; |
@@ -202,13 +214,16 @@ int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { | |||
202 | return STATE_WARNING; | 214 | return STATE_WARNING; |
203 | } | 215 | } |
204 | 216 | ||
205 | uint64_t usage_percentage = (uint64_t)((total_swap_mb - free_swap_mb) / total_swap_mb) * HUNDRED_PERCENT; | 217 | uint64_t usage_percentage = |
218 | (uint64_t)((total_swap_mb - free_swap_mb) / total_swap_mb) * HUNDRED_PERCENT; | ||
206 | 219 | ||
207 | if (config.crit.is_percentage && config.crit.value != 0 && usage_percentage >= (HUNDRED_PERCENT - config.crit.value)) { | 220 | if (config.crit.is_percentage && config.crit.value != 0 && |
221 | usage_percentage >= (HUNDRED_PERCENT - config.crit.value)) { | ||
208 | return STATE_CRITICAL; | 222 | return STATE_CRITICAL; |
209 | } | 223 | } |
210 | 224 | ||
211 | if (config.warn.is_percentage && config.warn.value != 0 && usage_percentage >= (HUNDRED_PERCENT - config.warn.value)) { | 225 | if (config.warn.is_percentage && config.warn.value != 0 && |
226 | usage_percentage >= (HUNDRED_PERCENT - config.warn.value)) { | ||
212 | return STATE_WARNING; | 227 | return STATE_WARNING; |
213 | } | 228 | } |
214 | 229 | ||
diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 1000fc9e..8d3c7fcf 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h | |||
@@ -1,7 +1,8 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #include "../common.h" | 3 | #include "../common.h" |
4 | #include "output.h" | 4 | #include "../../lib/output.h" |
5 | #include "../../lib/states.h" | ||
5 | 6 | ||
6 | #ifndef SWAP_CONVERSION | 7 | #ifndef SWAP_CONVERSION |
7 | # define SWAP_CONVERSION 1 | 8 | # define SWAP_CONVERSION 1 |
@@ -26,7 +27,7 @@ typedef struct { | |||
26 | 27 | ||
27 | typedef struct { | 28 | typedef struct { |
28 | bool allswaps; | 29 | bool allswaps; |
29 | int no_swap_state; | 30 | mp_state_enum no_swap_state; |
30 | bool warn_is_set; | 31 | bool warn_is_set; |
31 | check_swap_threshold warn; | 32 | check_swap_threshold warn; |
32 | bool crit_is_set; | 33 | bool crit_is_set; |
@@ -42,6 +43,7 @@ swap_config swap_config_init(void); | |||
42 | 43 | ||
43 | swap_result get_swap_data(swap_config config); | 44 | swap_result get_swap_data(swap_config config); |
44 | swap_result getSwapFromProcMeminfo(char path_to_proc_meminfo[]); | 45 | swap_result getSwapFromProcMeminfo(char path_to_proc_meminfo[]); |
45 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]); | 46 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], |
47 | const char swap_format[]); | ||
46 | swap_result getSwapFromSwapctl_BSD(swap_config config); | 48 | swap_result getSwapFromSwapctl_BSD(swap_config config); |
47 | swap_result getSwapFromSwap_SRV4(swap_config config); | 49 | swap_result getSwapFromSwap_SRV4(swap_config config); |
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 180d5037..5b654197 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c | |||
@@ -68,7 +68,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
68 | FILE *meminfo_file_ptr; | 68 | FILE *meminfo_file_ptr; |
69 | meminfo_file_ptr = fopen(proc_meminfo, "r"); | 69 | meminfo_file_ptr = fopen(proc_meminfo, "r"); |
70 | 70 | ||
71 | swap_result result = {0}; | 71 | swap_result result = {}; |
72 | result.errorcode = STATE_UNKNOWN; | 72 | result.errorcode = STATE_UNKNOWN; |
73 | 73 | ||
74 | if (meminfo_file_ptr == NULL) { | 74 | if (meminfo_file_ptr == NULL) { |
@@ -78,90 +78,81 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
78 | return result; | 78 | return result; |
79 | } | 79 | } |
80 | 80 | ||
81 | uint64_t swap_total = 0; | 81 | unsigned long swap_total = 0; |
82 | uint64_t swap_used = 0; | 82 | unsigned long swap_used = 0; |
83 | uint64_t swap_free = 0; | 83 | unsigned long swap_free = 0; |
84 | 84 | ||
85 | bool found_total = false; | 85 | bool found_total = false; |
86 | bool found_used = false; | ||
87 | bool found_free = false; | 86 | bool found_free = false; |
88 | 87 | ||
89 | char input_buffer[MAX_INPUT_BUFFER]; | 88 | char input_buffer[MAX_INPUT_BUFFER]; |
90 | char str[32]; | 89 | char str[32]; |
91 | 90 | ||
92 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { | 91 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { |
93 | uint64_t tmp_KB = 0; | ||
94 | 92 | ||
95 | /* | 93 | /* |
96 | * The following sscanf call looks for a line looking like: "Swap: 123 | 94 | * The following sscanf call looks for a line looking like: "Swap: 123 |
97 | * 123 123" On which kind of system this format exists, I can not say, | 95 | * 123 123" which exists on NetBSD (at least), |
98 | * but I wanted to document this for people who are not adapt with | 96 | * The unit should be Bytes |
99 | * sscanf anymore, like me | ||
100 | * Also the units used here are unclear and probably wrong | ||
101 | */ | 97 | */ |
102 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) { | 98 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, |
103 | 99 | &swap_free) == 3) { | |
104 | result.metrics.total += swap_total; | ||
105 | result.metrics.used += swap_used; | ||
106 | result.metrics.free += swap_free; | ||
107 | |||
108 | found_total = true; | 100 | found_total = true; |
109 | found_free = true; | 101 | found_free = true; |
110 | found_used = true; | ||
111 | |||
112 | // Set error | 102 | // Set error |
113 | result.errorcode = STATE_OK; | 103 | result.errorcode = STATE_OK; |
104 | // Break out of fgets here, since both scanf expressions might match (NetBSD for | ||
105 | // example) | ||
106 | break; | ||
107 | } | ||
108 | |||
109 | /* | ||
110 | * The following sscanf call looks for lines looking like: | ||
111 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | ||
112 | * on Debian Linux with a 5.* kernel | ||
113 | */ | ||
114 | unsigned long tmp_KB = 0; | ||
115 | int sscanf_result = sscanf(input_buffer, | ||
116 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | ||
117 | "%*[k]%*[B]", | ||
118 | str, &tmp_KB); | ||
119 | |||
120 | if (sscanf_result == 2) { | ||
121 | |||
122 | if (verbose >= 3) { | ||
123 | printf("Got %s with %lu\n", str, tmp_KB); | ||
124 | } | ||
114 | 125 | ||
115 | /* | 126 | /* I think this part is always in Kb, so convert to bytes */ |
116 | * The following sscanf call looks for lines looking like: | 127 | if (strcmp("Total", str) == 0) { |
117 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | 128 | swap_total = tmp_KB * 1000; |
118 | * on Debian Linux with a 5.* kernel | 129 | found_total = true; |
119 | */ | 130 | } else if (strcmp("Free", str) == 0) { |
120 | } else { | 131 | swap_free += tmp_KB * 1000; |
121 | int sscanf_result = sscanf(input_buffer, | 132 | found_free = true; |
122 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | 133 | } else if (strcmp("Cached", str) == 0) { |
123 | "%*[k]%*[B]", | 134 | swap_free += tmp_KB * 1000; |
124 | str, &tmp_KB); | ||
125 | |||
126 | if (sscanf_result == 2) { | ||
127 | |||
128 | if (verbose >= 3) { | ||
129 | printf("Got %s with %lu\n", str, tmp_KB); | ||
130 | } | ||
131 | |||
132 | /* I think this part is always in Kb, so convert to bytes */ | ||
133 | if (strcmp("Total", str) == 0) { | ||
134 | swap_total = tmp_KB * 1000; | ||
135 | found_total = true; | ||
136 | } else if (strcmp("Free", str) == 0) { | ||
137 | swap_free = swap_free + tmp_KB * 1000; | ||
138 | found_free = true; | ||
139 | found_used = true; // No explicit used metric available | ||
140 | } else if (strcmp("Cached", str) == 0) { | ||
141 | swap_free = swap_free + tmp_KB * 1000; | ||
142 | found_free = true; | ||
143 | found_used = true; // No explicit used metric available | ||
144 | } | ||
145 | |||
146 | result.errorcode = STATE_OK; | ||
147 | } | 135 | } |
136 | |||
137 | result.errorcode = STATE_OK; | ||
148 | } | 138 | } |
149 | } | 139 | } |
150 | 140 | ||
151 | fclose(meminfo_file_ptr); | 141 | fclose(meminfo_file_ptr); |
152 | 142 | ||
153 | result.metrics.total = swap_total; | 143 | result.metrics.total = swap_total; |
154 | result.metrics.used = swap_total - swap_free; | ||
155 | result.metrics.free = swap_free; | 144 | result.metrics.free = swap_free; |
145 | result.metrics.used = swap_total - swap_free; | ||
156 | 146 | ||
157 | if (!found_free || !found_total || !found_used) { | 147 | if (!found_free || !found_total) { |
158 | result.errorcode = STATE_UNKNOWN; | 148 | result.errorcode = STATE_UNKNOWN; |
159 | } | 149 | } |
160 | 150 | ||
161 | return result; | 151 | return result; |
162 | } | 152 | } |
163 | 153 | ||
164 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]) { | 154 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], |
155 | const char swap_format[]) { | ||
165 | swap_result result = {0}; | 156 | swap_result result = {0}; |
166 | 157 | ||
167 | char *temp_buffer; | 158 | char *temp_buffer; |
@@ -224,7 +215,8 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] | |||
224 | used_swap_mb = total_swap_mb - free_swap_mb; | 215 | used_swap_mb = total_swap_mb - free_swap_mb; |
225 | 216 | ||
226 | if (verbose >= 3) { | 217 | if (verbose >= 3) { |
227 | printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); | 218 | printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, |
219 | free_swap_mb); | ||
228 | } | 220 | } |
229 | } else { | 221 | } else { |
230 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 222 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { |
@@ -297,8 +289,14 @@ struct swapent { | |||
297 | }; | 289 | }; |
298 | 290 | ||
299 | #else | 291 | #else |
292 | |||
293 | // Includes for NetBSD | ||
294 | # include <unistd.h> | ||
295 | # include <sys/swap.h> | ||
296 | |||
300 | # define bsd_swapctl swapctl | 297 | # define bsd_swapctl swapctl |
301 | #endif | 298 | |
299 | #endif // CHECK_SWAP_SWAPCTL_BSD | ||
302 | 300 | ||
303 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | 301 | swap_result getSwapFromSwapctl_BSD(swap_config config) { |
304 | /* get the number of active swap devices */ | 302 | /* get the number of active swap devices */ |
@@ -322,8 +320,8 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { | |||
322 | unsigned long long used_swap_mb = 0; | 320 | unsigned long long used_swap_mb = 0; |
323 | 321 | ||
324 | for (int i = 0; i < nswaps; i++) { | 322 | for (int i = 0; i < nswaps; i++) { |
325 | dsktotal_mb = (float)ent[i].se_nblks / (float)config.conversion_factor; | 323 | dsktotal_mb = (double)ent[i].se_nblks / (double)config.conversion_factor; |
326 | dskused_mb = (float)ent[i].se_inuse / (float)config.conversion_factor; | 324 | dskused_mb = (double)ent[i].se_inuse / (double)config.conversion_factor; |
327 | dskfree_mb = (dsktotal_mb - dskused_mb); | 325 | dskfree_mb = (dsktotal_mb - dskused_mb); |
328 | 326 | ||
329 | if (config.allswaps && dsktotal_mb > 0) { | 327 | if (config.allswaps && dsktotal_mb > 0) { |
@@ -404,7 +402,8 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { | |||
404 | } | 402 | } |
405 | 403 | ||
406 | /* initialize swap table + entries */ | 404 | /* initialize swap table + entries */ |
407 | swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * (unsigned long)nswaps)); | 405 | swaptbl_t *tbl = |
406 | (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * (unsigned long)nswaps)); | ||
408 | 407 | ||
409 | if (tbl == NULL) { | 408 | if (tbl == NULL) { |
410 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | 409 | die(STATE_UNKNOWN, _("malloc() failed!\n")); |
@@ -439,7 +438,8 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { | |||
439 | dskused_mb = (dsktotal_mb - dskfree_mb); | 438 | dskused_mb = (dsktotal_mb - dskfree_mb); |
440 | 439 | ||
441 | if (verbose >= 3) { | 440 | if (verbose >= 3) { |
442 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); | 441 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, |
442 | dskused_mb); | ||
443 | } | 443 | } |
444 | 444 | ||
445 | if (config.allswaps && dsktotal_mb > 0) { | 445 | if (config.allswaps && dsktotal_mb > 0) { |
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 22dcc74e..09806373 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c | |||
@@ -65,7 +65,8 @@ typedef struct { | |||
65 | int errorcode; | 65 | int errorcode; |
66 | check_tcp_config config; | 66 | check_tcp_config config; |
67 | } check_tcp_config_wrapper; | 67 | } check_tcp_config_wrapper; |
68 | static check_tcp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/, check_tcp_config /*config*/); | 68 | static check_tcp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/, |
69 | check_tcp_config /*config*/); | ||
69 | void print_help(const char *service); | 70 | void print_help(const char *service); |
70 | void print_usage(void); | 71 | void print_usage(void); |
71 | 72 | ||
@@ -137,7 +138,8 @@ int main(int argc, char **argv) { | |||
137 | config.server_expect[0] = "220"; | 138 | config.server_expect[0] = "220"; |
138 | config.quit = "QUIT\r\n"; | 139 | config.quit = "QUIT\r\n"; |
139 | config.server_port = DEFAULT_FTP_PORT; | 140 | config.server_port = DEFAULT_FTP_PORT; |
140 | } else if (!strncmp(config.service, "POP", strlen("POP")) || !strncmp(config.service, "POP3", strlen("POP3"))) { | 141 | } else if (!strncmp(config.service, "POP", strlen("POP")) || |
142 | !strncmp(config.service, "POP3", strlen("POP3"))) { | ||
141 | config.server_expect[0] = "+OK"; | 143 | config.server_expect[0] = "+OK"; |
142 | config.quit = "QUIT\r\n"; | 144 | config.quit = "QUIT\r\n"; |
143 | config.server_port = DEFAULT_POP_PORT; | 145 | config.server_port = DEFAULT_POP_PORT; |
@@ -167,7 +169,8 @@ int main(int argc, char **argv) { | |||
167 | config.use_tls = true; | 169 | config.use_tls = true; |
168 | config.server_port = DEFAULT_SSMTP_PORT; | 170 | config.server_port = DEFAULT_SSMTP_PORT; |
169 | } else if (!strncmp(config.service, "JABBER", strlen("JABBER"))) { | 171 | } else if (!strncmp(config.service, "JABBER", strlen("JABBER"))) { |
170 | config.send = "<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n"; | 172 | config.send = "<stream:stream to=\'host\' xmlns=\'jabber:client\' " |
173 | "xmlns:stream=\'http://etherx.jabber.org/streams\'>\n"; | ||
171 | config.server_expect[0] = "<?xml version=\'1.0\'"; | 174 | config.server_expect[0] = "<?xml version=\'1.0\'"; |
172 | config.quit = "</stream:stream>\n"; | 175 | config.quit = "</stream:stream>\n"; |
173 | config.hide_output = true; | 176 | config.hide_output = true; |
@@ -246,46 +249,58 @@ int main(int argc, char **argv) { | |||
246 | mp_subcheck inital_connect_result = mp_subcheck_init(); | 249 | mp_subcheck inital_connect_result = mp_subcheck_init(); |
247 | 250 | ||
248 | // Try initial connection | 251 | // Try initial connection |
249 | if (np_net_connect(config.server_address, config.server_port, &socket_descriptor, config.protocol) == STATE_CRITICAL) { | 252 | if (np_net_connect(config.server_address, config.server_port, &socket_descriptor, |
253 | config.protocol) == STATE_CRITICAL) { | ||
250 | // Early exit here, we got connection refused | 254 | // Early exit here, we got connection refused |
251 | inital_connect_result = mp_set_subcheck_state(inital_connect_result, config.econn_refuse_state); | 255 | inital_connect_result = |
252 | xasprintf(&inital_connect_result.output, "Connection to %s on port %i was REFUSED", config.server_address, config.server_port); | 256 | mp_set_subcheck_state(inital_connect_result, config.econn_refuse_state); |
257 | xasprintf(&inital_connect_result.output, "Connection to %s on port %i was REFUSED", | ||
258 | config.server_address, config.server_port); | ||
253 | mp_add_subcheck_to_check(&overall, inital_connect_result); | 259 | mp_add_subcheck_to_check(&overall, inital_connect_result); |
254 | mp_exit(overall); | 260 | mp_exit(overall); |
255 | } else { | 261 | } else { |
256 | inital_connect_result = mp_set_subcheck_state(inital_connect_result, STATE_OK); | 262 | inital_connect_result = mp_set_subcheck_state(inital_connect_result, STATE_OK); |
257 | xasprintf(&inital_connect_result.output, "Connection to %s on port %i was a SUCCESS", config.server_address, config.server_port); | 263 | xasprintf(&inital_connect_result.output, "Connection to %s on port %i was a SUCCESS", |
264 | config.server_address, config.server_port); | ||
258 | mp_add_subcheck_to_check(&overall, inital_connect_result); | 265 | mp_add_subcheck_to_check(&overall, inital_connect_result); |
259 | } | 266 | } |
260 | 267 | ||
261 | #ifdef HAVE_SSL | 268 | #ifdef HAVE_SSL |
262 | if (config.use_tls) { | 269 | if (config.use_tls) { |
263 | mp_subcheck tls_connection_result = mp_subcheck_init(); | 270 | mp_subcheck tls_connection_result = mp_subcheck_init(); |
264 | mp_state_enum result = np_net_ssl_init_with_hostname(socket_descriptor, (config.sni_specified ? config.sni : NULL)); | 271 | mp_state_enum result = np_net_ssl_init_with_hostname( |
272 | socket_descriptor, (config.sni_specified ? config.sni : NULL)); | ||
265 | tls_connection_result = mp_set_subcheck_default_state(tls_connection_result, result); | 273 | tls_connection_result = mp_set_subcheck_default_state(tls_connection_result, result); |
266 | 274 | ||
267 | if (result == STATE_OK) { | 275 | if (result == STATE_OK) { |
268 | xasprintf(&tls_connection_result.output, "TLS connection succeeded"); | 276 | xasprintf(&tls_connection_result.output, "TLS connection succeeded"); |
269 | 277 | ||
270 | if (config.check_cert) { | 278 | if (config.check_cert) { |
271 | result = np_net_ssl_check_cert(config.days_till_exp_warn, config.days_till_exp_crit); | 279 | result = |
280 | np_net_ssl_check_cert(config.days_till_exp_warn, config.days_till_exp_crit); | ||
272 | 281 | ||
273 | mp_subcheck tls_certificate_lifetime_result = mp_subcheck_init(); | 282 | mp_subcheck tls_certificate_lifetime_result = mp_subcheck_init(); |
274 | tls_certificate_lifetime_result = mp_set_subcheck_state(tls_certificate_lifetime_result, result); | 283 | tls_certificate_lifetime_result = |
284 | mp_set_subcheck_state(tls_certificate_lifetime_result, result); | ||
275 | 285 | ||
276 | if (result == STATE_OK) { | 286 | if (result == STATE_OK) { |
277 | xasprintf(&tls_certificate_lifetime_result.output, "Certificate lifetime is within thresholds"); | 287 | xasprintf(&tls_certificate_lifetime_result.output, |
288 | "Certificate lifetime is within thresholds"); | ||
278 | } else if (result == STATE_WARNING) { | 289 | } else if (result == STATE_WARNING) { |
279 | xasprintf(&tls_certificate_lifetime_result.output, "Certificate lifetime is violating warning threshold (%i)", | 290 | xasprintf(&tls_certificate_lifetime_result.output, |
291 | "Certificate lifetime is violating warning threshold (%i)", | ||
280 | config.days_till_exp_warn); | 292 | config.days_till_exp_warn); |
281 | } else if (result == STATE_CRITICAL) { | 293 | } else if (result == STATE_CRITICAL) { |
282 | xasprintf(&tls_certificate_lifetime_result.output, "Certificate lifetime is violating critical threshold (%i)", | 294 | xasprintf(&tls_certificate_lifetime_result.output, |
295 | "Certificate lifetime is violating critical threshold (%i)", | ||
283 | config.days_till_exp_crit); | 296 | config.days_till_exp_crit); |
284 | } else { | 297 | } else { |
285 | xasprintf(&tls_certificate_lifetime_result.output, "Certificate lifetime is somehow unknown"); | 298 | xasprintf(&tls_certificate_lifetime_result.output, |
299 | "Certificate lifetime is somehow unknown"); | ||
286 | } | 300 | } |
287 | 301 | ||
288 | mp_add_subcheck_to_subcheck(&tls_connection_result, tls_certificate_lifetime_result); | 302 | mp_add_subcheck_to_subcheck(&tls_connection_result, |
303 | tls_certificate_lifetime_result); | ||
289 | } | 304 | } |
290 | 305 | ||
291 | mp_add_subcheck_to_check(&overall, tls_connection_result); | 306 | mp_add_subcheck_to_check(&overall, tls_connection_result); |
@@ -336,7 +351,8 @@ int main(int argc, char **argv) { | |||
336 | char buffer[MAXBUF]; | 351 | char buffer[MAXBUF]; |
337 | 352 | ||
338 | /* watch for the expect string */ | 353 | /* watch for the expect string */ |
339 | while ((received = my_recv(socket_descriptor, buffer, sizeof(buffer), config.use_tls)) > 0) { | 354 | while ((received = my_recv(socket_descriptor, buffer, sizeof(buffer), config.use_tls)) > |
355 | 0) { | ||
340 | received_buffer = realloc(received_buffer, len + received + 1); | 356 | received_buffer = realloc(received_buffer, len + received + 1); |
341 | 357 | ||
342 | if (received_buffer == NULL) { | 358 | if (received_buffer == NULL) { |
@@ -352,7 +368,8 @@ int main(int argc, char **argv) { | |||
352 | break; | 368 | break; |
353 | } | 369 | } |
354 | 370 | ||
355 | if ((match = np_expect_match(received_buffer, config.server_expect, config.server_expect_count, config.match_flags)) != | 371 | if ((match = np_expect_match(received_buffer, config.server_expect, |
372 | config.server_expect_count, config.match_flags)) != | ||
356 | NP_MATCH_RETRY) { | 373 | NP_MATCH_RETRY) { |
357 | break; | 374 | break; |
358 | } | 375 | } |
@@ -385,7 +402,8 @@ int main(int argc, char **argv) { | |||
385 | 402 | ||
386 | /* print raw output if we're debugging */ | 403 | /* print raw output if we're debugging */ |
387 | if (verbosity > 0) { | 404 | if (verbosity > 0) { |
388 | printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n", (int)len + 1, received_buffer); | 405 | printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n", |
406 | (int)len + 1, received_buffer); | ||
389 | } | 407 | } |
390 | /* strip whitespace from end of output */ | 408 | /* strip whitespace from end of output */ |
391 | while (--len > 0 && isspace(received_buffer[len])) { | 409 | while (--len > 0 && isspace(received_buffer[len])) { |
@@ -415,7 +433,9 @@ int main(int argc, char **argv) { | |||
415 | time_pd.uom = "s"; | 433 | time_pd.uom = "s"; |
416 | 434 | ||
417 | if (config.critical_time_set && elapsed_time > config.critical_time) { | 435 | if (config.critical_time_set && elapsed_time > config.critical_time) { |
418 | xasprintf(&elapsed_time_result.output, "Connection time %fs exceeded critical threshold (%f)", elapsed_time, config.critical_time); | 436 | xasprintf(&elapsed_time_result.output, |
437 | "Connection time %fs exceeded critical threshold (%f)", elapsed_time, | ||
438 | config.critical_time); | ||
419 | 439 | ||
420 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_CRITICAL); | 440 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_CRITICAL); |
421 | time_pd.crit_present = true; | 441 | time_pd.crit_present = true; |
@@ -426,7 +446,9 @@ int main(int argc, char **argv) { | |||
426 | 446 | ||
427 | time_pd.crit = crit_val; | 447 | time_pd.crit = crit_val; |
428 | } else if (config.warning_time_set && elapsed_time > config.warning_time) { | 448 | } else if (config.warning_time_set && elapsed_time > config.warning_time) { |
429 | xasprintf(&elapsed_time_result.output, "Connection time %fs exceeded warning threshold (%f)", elapsed_time, config.critical_time); | 449 | xasprintf(&elapsed_time_result.output, |
450 | "Connection time %fs exceeded warning threshold (%f)", elapsed_time, | ||
451 | config.critical_time); | ||
430 | 452 | ||
431 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_WARNING); | 453 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_WARNING); |
432 | time_pd.warn_present = true; | 454 | time_pd.warn_present = true; |
@@ -437,7 +459,8 @@ int main(int argc, char **argv) { | |||
437 | time_pd.warn = warn_val; | 459 | time_pd.warn = warn_val; |
438 | } else { | 460 | } else { |
439 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_OK); | 461 | elapsed_time_result = mp_set_subcheck_state(elapsed_time_result, STATE_OK); |
440 | xasprintf(&elapsed_time_result.output, "Connection time %fs is within thresholds", elapsed_time); | 462 | xasprintf(&elapsed_time_result.output, "Connection time %fs is within thresholds", |
463 | elapsed_time); | ||
441 | } | 464 | } |
442 | 465 | ||
443 | mp_add_perfdata_to_subcheck(&elapsed_time_result, time_pd); | 466 | mp_add_perfdata_to_subcheck(&elapsed_time_result, time_pd); |
@@ -445,7 +468,8 @@ int main(int argc, char **argv) { | |||
445 | 468 | ||
446 | /* did we get the response we hoped? */ | 469 | /* did we get the response we hoped? */ |
447 | if (match == NP_MATCH_FAILURE) { | 470 | if (match == NP_MATCH_FAILURE) { |
448 | expected_data_result = mp_set_subcheck_state(expected_data_result, config.expect_mismatch_state); | 471 | expected_data_result = |
472 | mp_set_subcheck_state(expected_data_result, config.expect_mismatch_state); | ||
449 | xasprintf(&expected_data_result.output, "Answer failed to match expectation"); | 473 | xasprintf(&expected_data_result.output, "Answer failed to match expectation"); |
450 | mp_add_subcheck_to_check(&overall, expected_data_result); | 474 | mp_add_subcheck_to_check(&overall, expected_data_result); |
451 | } else if (match == NP_MATCH_SUCCESS) { | 475 | } else if (match == NP_MATCH_SUCCESS) { |
@@ -467,34 +491,35 @@ static check_tcp_config_wrapper process_arguments(int argc, char **argv, check_t | |||
467 | output_format_index, | 491 | output_format_index, |
468 | }; | 492 | }; |
469 | 493 | ||
470 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 494 | static struct option longopts[] = { |
471 | {"critical", required_argument, 0, 'c'}, | 495 | {"hostname", required_argument, 0, 'H'}, |
472 | {"warning", required_argument, 0, 'w'}, | 496 | {"critical", required_argument, 0, 'c'}, |
473 | {"critical-codes", required_argument, 0, 'C'}, | 497 | {"warning", required_argument, 0, 'w'}, |
474 | {"warning-codes", required_argument, 0, 'W'}, | 498 | {"critical-codes", required_argument, 0, 'C'}, |
475 | {"timeout", required_argument, 0, 't'}, | 499 | {"warning-codes", required_argument, 0, 'W'}, |
476 | {"protocol", required_argument, 0, 'P'}, /* FIXME: Unhandled */ | 500 | {"timeout", required_argument, 0, 't'}, |
477 | {"port", required_argument, 0, 'p'}, | 501 | {"protocol", required_argument, 0, 'P'}, /* FIXME: Unhandled */ |
478 | {"escape", no_argument, 0, 'E'}, | 502 | {"port", required_argument, 0, 'p'}, |
479 | {"all", no_argument, 0, 'A'}, | 503 | {"escape", no_argument, 0, 'E'}, |
480 | {"send", required_argument, 0, 's'}, | 504 | {"all", no_argument, 0, 'A'}, |
481 | {"expect", required_argument, 0, 'e'}, | 505 | {"send", required_argument, 0, 's'}, |
482 | {"maxbytes", required_argument, 0, 'm'}, | 506 | {"expect", required_argument, 0, 'e'}, |
483 | {"quit", required_argument, 0, 'q'}, | 507 | {"maxbytes", required_argument, 0, 'm'}, |
484 | {"jail", no_argument, 0, 'j'}, | 508 | {"quit", required_argument, 0, 'q'}, |
485 | {"delay", required_argument, 0, 'd'}, | 509 | {"jail", no_argument, 0, 'j'}, |
486 | {"refuse", required_argument, 0, 'r'}, | 510 | {"delay", required_argument, 0, 'd'}, |
487 | {"mismatch", required_argument, 0, 'M'}, | 511 | {"refuse", required_argument, 0, 'r'}, |
488 | {"use-ipv4", no_argument, 0, '4'}, | 512 | {"mismatch", required_argument, 0, 'M'}, |
489 | {"use-ipv6", no_argument, 0, '6'}, | 513 | {"use-ipv4", no_argument, 0, '4'}, |
490 | {"verbose", no_argument, 0, 'v'}, | 514 | {"use-ipv6", no_argument, 0, '6'}, |
491 | {"version", no_argument, 0, 'V'}, | 515 | {"verbose", no_argument, 0, 'v'}, |
492 | {"help", no_argument, 0, 'h'}, | 516 | {"version", no_argument, 0, 'V'}, |
493 | {"ssl", no_argument, 0, 'S'}, | 517 | {"help", no_argument, 0, 'h'}, |
494 | {"sni", required_argument, 0, SNI_OPTION}, | 518 | {"ssl", no_argument, 0, 'S'}, |
495 | {"certificate", required_argument, 0, 'D'}, | 519 | {"sni", required_argument, 0, SNI_OPTION}, |
496 | {"output-format", required_argument, 0, output_format_index}, | 520 | {"certificate", required_argument, 0, 'D'}, |
497 | {0, 0, 0, 0}}; | 521 | {"output-format", required_argument, 0, output_format_index}, |
522 | {0, 0, 0, 0}}; | ||
498 | 523 | ||
499 | if (argc < 2) { | 524 | if (argc < 2) { |
500 | usage4(_("No arguments found")); | 525 | usage4(_("No arguments found")); |
@@ -522,7 +547,8 @@ static check_tcp_config_wrapper process_arguments(int argc, char **argv, check_t | |||
522 | 547 | ||
523 | while (true) { | 548 | while (true) { |
524 | int option = 0; | 549 | int option = 0; |
525 | int option_index = getopt_long(argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); | 550 | int option_index = |
551 | getopt_long(argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); | ||
526 | 552 | ||
527 | if (option_index == -1 || option_index == EOF || option_index == 1) { | 553 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
528 | break; | 554 | break; |
@@ -595,7 +621,8 @@ static check_tcp_config_wrapper process_arguments(int argc, char **argv, check_t | |||
595 | if (config.server_expect_count == 0) { | 621 | if (config.server_expect_count == 0) { |
596 | config.server_expect = malloc(sizeof(char *) * (++config.server_expect_count)); | 622 | config.server_expect = malloc(sizeof(char *) * (++config.server_expect_count)); |
597 | } else { | 623 | } else { |
598 | config.server_expect = realloc(config.server_expect, sizeof(char *) * (++config.server_expect_count)); | 624 | config.server_expect = |
625 | realloc(config.server_expect, sizeof(char *) * (++config.server_expect_count)); | ||
599 | } | 626 | } |
600 | 627 | ||
601 | if (config.server_expect == NULL) { | 628 | if (config.server_expect == NULL) { |
@@ -718,8 +745,8 @@ static check_tcp_config_wrapper process_arguments(int argc, char **argv, check_t | |||
718 | if (config.server_address == NULL) { | 745 | if (config.server_address == NULL) { |
719 | usage4(_("You must provide a server address")); | 746 | usage4(_("You must provide a server address")); |
720 | } else if (config.server_address[0] != '/' && !is_host(config.server_address)) { | 747 | } else if (config.server_address[0] != '/' && !is_host(config.server_address)) { |
721 | die(STATE_CRITICAL, "%s %s - %s: %s\n", config.service, state_text(STATE_CRITICAL), _("Invalid hostname, address or socket"), | 748 | die(STATE_CRITICAL, "%s %s - %s: %s\n", config.service, state_text(STATE_CRITICAL), |
722 | config.server_address); | 749 | _("Invalid hostname, address or socket"), config.server_address); |
723 | } | 750 | } |
724 | 751 | ||
725 | check_tcp_config_wrapper result = { | 752 | check_tcp_config_wrapper result = { |
@@ -735,7 +762,8 @@ void print_help(const char *service) { | |||
735 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | 762 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
736 | printf(COPYRIGHT, copyright, email); | 763 | printf(COPYRIGHT, copyright, email); |
737 | 764 | ||
738 | printf(_("This plugin tests %s connections with the specified host (or unix socket).\n\n"), service); | 765 | printf(_("This plugin tests %s connections with the specified host (or unix socket).\n\n"), |
766 | service); | ||
739 | 767 | ||
740 | print_usage(); | 768 | print_usage(); |
741 | 769 | ||
@@ -747,7 +775,8 @@ void print_help(const char *service) { | |||
747 | printf(UT_IPv46); | 775 | printf(UT_IPv46); |
748 | 776 | ||
749 | printf(" %s\n", "-E, --escape"); | 777 | printf(" %s\n", "-E, --escape"); |
750 | printf(" %s\n", _("Can use \\n, \\r, \\t or \\\\ in send or quit string. Must come before send or quit option")); | 778 | printf(" %s\n", _("Can use \\n, \\r, \\t or \\\\ in send or quit string. Must come before " |
779 | "send or quit option")); | ||
751 | printf(" %s\n", _("Default: nothing added to send, \\r\\n added to end of quit")); | 780 | printf(" %s\n", _("Default: nothing added to send, \\r\\n added to end of quit")); |
752 | printf(" %s\n", "-s, --send=STRING"); | 781 | printf(" %s\n", "-s, --send=STRING"); |
753 | printf(" %s\n", _("String to send to the server")); | 782 | printf(" %s\n", _("String to send to the server")); |
@@ -760,7 +789,8 @@ void print_help(const char *service) { | |||
760 | printf(" %s\n", "-r, --refuse=ok|warn|crit"); | 789 | printf(" %s\n", "-r, --refuse=ok|warn|crit"); |
761 | printf(" %s\n", _("Accept TCP refusals with states ok, warn, crit (default: crit)")); | 790 | printf(" %s\n", _("Accept TCP refusals with states ok, warn, crit (default: crit)")); |
762 | printf(" %s\n", "-M, --mismatch=ok|warn|crit"); | 791 | printf(" %s\n", "-M, --mismatch=ok|warn|crit"); |
763 | printf(" %s\n", _("Accept expected string mismatches with states ok, warn, crit (default: warn)")); | 792 | printf(" %s\n", |
793 | _("Accept expected string mismatches with states ok, warn, crit (default: warn)")); | ||
764 | printf(" %s\n", "-j, --jail"); | 794 | printf(" %s\n", "-j, --jail"); |
765 | printf(" %s\n", _("Hide output from TCP socket")); | 795 | printf(" %s\n", _("Hide output from TCP socket")); |
766 | printf(" %s\n", "-m, --maxbytes=INTEGER"); | 796 | printf(" %s\n", "-m, --maxbytes=INTEGER"); |
@@ -790,7 +820,8 @@ void print_help(const char *service) { | |||
790 | 820 | ||
791 | void print_usage(void) { | 821 | void print_usage(void) { |
792 | printf("%s\n", _("Usage:")); | 822 | printf("%s\n", _("Usage:")); |
793 | printf("%s -H host -p port [-w <warning time>] [-c <critical time>] [-s <send string>]\n", progname); | 823 | printf("%s -H host -p port [-w <warning time>] [-c <critical time>] [-s <send string>]\n", |
824 | progname); | ||
794 | printf("[-e <expect string>] [-q <quit string>][-m <maximum bytes>] [-d <delay>]\n"); | 825 | printf("[-e <expect string>] [-q <quit string>][-m <maximum bytes>] [-d <delay>]\n"); |
795 | printf("[-t <timeout seconds>] [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n"); | 826 | printf("[-t <timeout seconds>] [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n"); |
796 | printf("[-D <warn days cert expire>[,<crit days cert expire>]] [-S <use SSL>] [-E]\n"); | 827 | printf("[-D <warn days cert expire>[,<crit days cert expire>]] [-S <use SSL>] [-E]\n"); |
diff --git a/plugins/check_time.c b/plugins/check_time.c index debf59f3..fc9ba3f9 100644 --- a/plugins/check_time.c +++ b/plugins/check_time.c | |||
@@ -87,7 +87,8 @@ int main(int argc, char **argv) { | |||
87 | } else { | 87 | } else { |
88 | result = STATE_UNKNOWN; | 88 | result = STATE_UNKNOWN; |
89 | } | 89 | } |
90 | die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), config.server_address, config.server_port); | 90 | die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), |
91 | config.server_address, config.server_port); | ||
91 | } | 92 | } |
92 | 93 | ||
93 | if (config.use_udp) { | 94 | if (config.use_udp) { |
@@ -99,7 +100,8 @@ int main(int argc, char **argv) { | |||
99 | } else { | 100 | } else { |
100 | result = STATE_UNKNOWN; | 101 | result = STATE_UNKNOWN; |
101 | } | 102 | } |
102 | die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), config.server_address, config.server_port); | 103 | die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), |
104 | config.server_address, config.server_port); | ||
103 | } | 105 | } |
104 | } | 106 | } |
105 | 107 | ||
@@ -123,7 +125,8 @@ int main(int argc, char **argv) { | |||
123 | } else { | 125 | } else { |
124 | result = STATE_UNKNOWN; | 126 | result = STATE_UNKNOWN; |
125 | } | 127 | } |
126 | die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), config.server_address, config.server_port); | 128 | die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), |
129 | config.server_address, config.server_port); | ||
127 | } | 130 | } |
128 | 131 | ||
129 | result = STATE_OK; | 132 | result = STATE_OK; |
@@ -137,7 +140,8 @@ int main(int argc, char **argv) { | |||
137 | 140 | ||
138 | if (result != STATE_OK) { | 141 | if (result != STATE_OK) { |
139 | die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, | 142 | die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, |
140 | perfdata("time", (long)conntime, "s", config.check_warning_time, (long)config.warning_time, config.check_critical_time, | 143 | perfdata("time", (long)conntime, "s", config.check_warning_time, |
144 | (long)config.warning_time, config.check_critical_time, | ||
141 | (long)config.critical_time, true, 0, false, 0)); | 145 | (long)config.critical_time, true, 0, false, 0)); |
142 | } | 146 | } |
143 | 147 | ||
@@ -157,10 +161,11 @@ int main(int argc, char **argv) { | |||
157 | } | 161 | } |
158 | 162 | ||
159 | printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, | 163 | printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, |
160 | perfdata("time", (long)conntime, "s", config.check_warning_time, (long)config.warning_time, config.check_critical_time, | 164 | perfdata("time", (long)conntime, "s", config.check_warning_time, |
165 | (long)config.warning_time, config.check_critical_time, | ||
161 | (long)config.critical_time, true, 0, false, 0), | 166 | (long)config.critical_time, true, 0, false, 0), |
162 | perfdata("offset", diff_time, "s", config.check_warning_diff, config.warning_diff, config.check_critical_diff, | 167 | perfdata("offset", diff_time, "s", config.check_warning_diff, config.warning_diff, |
163 | config.critical_diff, true, 0, false, 0)); | 168 | config.check_critical_diff, config.critical_diff, true, 0, false, 0)); |
164 | return result; | 169 | return result; |
165 | } | 170 | } |
166 | 171 | ||
@@ -230,7 +235,8 @@ check_time_config_wrapper process_arguments(int argc, char **argv) { | |||
230 | result.config.warning_diff = strtoul(optarg, NULL, 10); | 235 | result.config.warning_diff = strtoul(optarg, NULL, 10); |
231 | result.config.check_warning_diff = true; | 236 | result.config.check_warning_diff = true; |
232 | } else if (strspn(optarg, "0123456789:,") > 0) { | 237 | } else if (strspn(optarg, "0123456789:,") > 0) { |
233 | if (sscanf(optarg, "%lu%*[:,]%d", &result.config.warning_diff, &result.config.warning_time) == 2) { | 238 | if (sscanf(optarg, "%lu%*[:,]%d", &result.config.warning_diff, |
239 | &result.config.warning_time) == 2) { | ||
234 | result.config.check_warning_diff = true; | 240 | result.config.check_warning_diff = true; |
235 | result.config.check_warning_time = true; | 241 | result.config.check_warning_time = true; |
236 | } else { | 242 | } else { |
@@ -245,7 +251,8 @@ check_time_config_wrapper process_arguments(int argc, char **argv) { | |||
245 | result.config.critical_diff = strtoul(optarg, NULL, 10); | 251 | result.config.critical_diff = strtoul(optarg, NULL, 10); |
246 | result.config.check_critical_diff = true; | 252 | result.config.check_critical_diff = true; |
247 | } else if (strspn(optarg, "0123456789:,") > 0) { | 253 | } else if (strspn(optarg, "0123456789:,") > 0) { |
248 | if (sscanf(optarg, "%lu%*[:,]%d", &result.config.critical_diff, &result.config.critical_time) == 2) { | 254 | if (sscanf(optarg, "%lu%*[:,]%d", &result.config.critical_diff, |
255 | &result.config.critical_time) == 2) { | ||
249 | result.config.check_critical_diff = true; | 256 | result.config.check_critical_diff = true; |
250 | result.config.check_critical_time = true; | 257 | result.config.check_critical_time = true; |
251 | } else { | 258 | } else { |
diff --git a/plugins/check_ups.c b/plugins/check_ups.c index ecc0760f..54decce3 100644 --- a/plugins/check_ups.c +++ b/plugins/check_ups.c | |||
@@ -106,7 +106,8 @@ int main(int argc, char **argv) { | |||
106 | if (ups_status_flags & UPSSTATUS_OFF) { | 106 | if (ups_status_flags & UPSSTATUS_OFF) { |
107 | xasprintf(&ups_status, "Off"); | 107 | xasprintf(&ups_status, "Off"); |
108 | result = STATE_CRITICAL; | 108 | result = STATE_CRITICAL; |
109 | } else if ((ups_status_flags & (UPSSTATUS_OB | UPSSTATUS_LB)) == (UPSSTATUS_OB | UPSSTATUS_LB)) { | 109 | } else if ((ups_status_flags & (UPSSTATUS_OB | UPSSTATUS_LB)) == |
110 | (UPSSTATUS_OB | UPSSTATUS_LB)) { | ||
110 | xasprintf(&ups_status, _("On Battery, Low Battery")); | 111 | xasprintf(&ups_status, _("On Battery, Low Battery")); |
111 | result = STATE_CRITICAL; | 112 | result = STATE_CRITICAL; |
112 | } else { | 113 | } else { |
@@ -192,11 +193,14 @@ int main(int argc, char **argv) { | |||
192 | result = max_state(result, STATE_WARNING); | 193 | result = max_state(result, STATE_WARNING); |
193 | } | 194 | } |
194 | xasprintf(&performance_data, "%s", | 195 | xasprintf(&performance_data, "%s", |
195 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", config.check_warn, (long)(1000 * config.warning_value), | 196 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", |
196 | config.check_crit, (long)(1000 * config.critical_value), true, 0, false, 0)); | 197 | config.check_warn, (long)(1000 * config.warning_value), |
198 | config.check_crit, (long)(1000 * config.critical_value), true, 0, | ||
199 | false, 0)); | ||
197 | } else { | 200 | } else { |
198 | xasprintf(&performance_data, "%s", | 201 | xasprintf(&performance_data, "%s", |
199 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, 0, true, 0, false, 0)); | 202 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, |
203 | 0, true, 0, false, 0)); | ||
200 | } | 204 | } |
201 | } | 205 | } |
202 | 206 | ||
@@ -220,11 +224,13 @@ int main(int argc, char **argv) { | |||
220 | result = max_state(result, STATE_WARNING); | 224 | result = max_state(result, STATE_WARNING); |
221 | } | 225 | } |
222 | xasprintf(&performance_data, "%s %s", performance_data, | 226 | xasprintf(&performance_data, "%s %s", performance_data, |
223 | perfdata("battery", (long)ups_battery_percent, "%", config.check_warn, (long)(config.warning_value), | 227 | perfdata("battery", (long)ups_battery_percent, "%", config.check_warn, |
224 | config.check_crit, (long)(config.critical_value), true, 0, true, 100)); | 228 | (long)(config.warning_value), config.check_crit, |
229 | (long)(config.critical_value), true, 0, true, 100)); | ||
225 | } else { | 230 | } else { |
226 | xasprintf(&performance_data, "%s %s", performance_data, | 231 | xasprintf(&performance_data, "%s %s", performance_data, |
227 | perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, 0, true, 100)); | 232 | perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, |
233 | 0, true, 100)); | ||
228 | } | 234 | } |
229 | } | 235 | } |
230 | 236 | ||
@@ -248,11 +254,13 @@ int main(int argc, char **argv) { | |||
248 | result = max_state(result, STATE_WARNING); | 254 | result = max_state(result, STATE_WARNING); |
249 | } | 255 | } |
250 | xasprintf(&performance_data, "%s %s", performance_data, | 256 | xasprintf(&performance_data, "%s %s", performance_data, |
251 | perfdata("load", (long)ups_load_percent, "%", config.check_warn, (long)(config.warning_value), config.check_crit, | 257 | perfdata("load", (long)ups_load_percent, "%", config.check_warn, |
258 | (long)(config.warning_value), config.check_crit, | ||
252 | (long)(config.critical_value), true, 0, true, 100)); | 259 | (long)(config.critical_value), true, 0, true, 100)); |
253 | } else { | 260 | } else { |
254 | xasprintf(&performance_data, "%s %s", performance_data, | 261 | xasprintf(&performance_data, "%s %s", performance_data, |
255 | perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, true, 100)); | 262 | perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, |
263 | true, 100)); | ||
256 | } | 264 | } |
257 | } | 265 | } |
258 | 266 | ||
@@ -285,11 +293,13 @@ int main(int argc, char **argv) { | |||
285 | result = max_state(result, STATE_WARNING); | 293 | result = max_state(result, STATE_WARNING); |
286 | } | 294 | } |
287 | xasprintf(&performance_data, "%s %s", performance_data, | 295 | xasprintf(&performance_data, "%s %s", performance_data, |
288 | perfdata("temp", (long)ups_temperature, tunits, config.check_warn, (long)(config.warning_value), config.check_crit, | 296 | perfdata("temp", (long)ups_temperature, tunits, config.check_warn, |
297 | (long)(config.warning_value), config.check_crit, | ||
289 | (long)(config.critical_value), true, 0, false, 0)); | 298 | (long)(config.critical_value), true, 0, false, 0)); |
290 | } else { | 299 | } else { |
291 | xasprintf(&performance_data, "%s %s", performance_data, | 300 | xasprintf(&performance_data, "%s %s", performance_data, |
292 | perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, false, 0)); | 301 | perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, |
302 | false, 0)); | ||
293 | } | 303 | } |
294 | } | 304 | } |
295 | 305 | ||
@@ -312,11 +322,13 @@ int main(int argc, char **argv) { | |||
312 | result = max_state(result, STATE_WARNING); | 322 | result = max_state(result, STATE_WARNING); |
313 | } | 323 | } |
314 | xasprintf(&performance_data, "%s %s", performance_data, | 324 | xasprintf(&performance_data, "%s %s", performance_data, |
315 | perfdata("realpower", (long)ups_realpower, "W", config.check_warn, (long)(config.warning_value), config.check_crit, | 325 | perfdata("realpower", (long)ups_realpower, "W", config.check_warn, |
326 | (long)(config.warning_value), config.check_crit, | ||
316 | (long)(config.critical_value), true, 0, false, 0)); | 327 | (long)(config.critical_value), true, 0, false, 0)); |
317 | } else { | 328 | } else { |
318 | xasprintf(&performance_data, "%s %s", performance_data, | 329 | xasprintf(&performance_data, "%s %s", performance_data, |
319 | perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, false, 0)); | 330 | perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, |
331 | false, 0)); | ||
320 | } | 332 | } |
321 | } | 333 | } |
322 | 334 | ||
@@ -401,7 +413,8 @@ int get_ups_variable(const char *varname, char *buf, const check_ups_config conf | |||
401 | 413 | ||
402 | /* create the command string to send to the UPS daemon */ | 414 | /* create the command string to send to the UPS daemon */ |
403 | /* Add LOGOUT to avoid read failure logs */ | 415 | /* Add LOGOUT to avoid read failure logs */ |
404 | int res = snprintf(send_buffer, sizeof(send_buffer), "GET VAR %s %s\nLOGOUT\n", config.ups_name, varname); | 416 | int res = snprintf(send_buffer, sizeof(send_buffer), "GET VAR %s %s\nLOGOUT\n", config.ups_name, |
417 | varname); | ||
405 | if ((res > 0) && ((size_t)res >= sizeof(send_buffer))) { | 418 | if ((res > 0) && ((size_t)res >= sizeof(send_buffer))) { |
406 | printf("%s\n", _("UPS name to long for buffer")); | 419 | printf("%s\n", _("UPS name to long for buffer")); |
407 | return ERROR; | 420 | return ERROR; |
@@ -410,7 +423,8 @@ int get_ups_variable(const char *varname, char *buf, const check_ups_config conf | |||
410 | char temp_buffer[MAX_INPUT_BUFFER]; | 423 | char temp_buffer[MAX_INPUT_BUFFER]; |
411 | 424 | ||
412 | /* send the command to the daemon and get a response back */ | 425 | /* send the command to the daemon and get a response back */ |
413 | if (process_tcp_request(config.server_address, config.server_port, send_buffer, temp_buffer, sizeof(temp_buffer)) != STATE_OK) { | 426 | if (process_tcp_request(config.server_address, config.server_port, send_buffer, temp_buffer, |
427 | sizeof(temp_buffer)) != STATE_OK) { | ||
414 | printf("%s\n", _("Invalid response received from host")); | 428 | printf("%s\n", _("Invalid response received from host")); |
415 | return ERROR; | 429 | return ERROR; |
416 | } | 430 | } |
@@ -630,7 +644,8 @@ void print_help(void) { | |||
630 | printf(" %s\n", "-T, --temperature"); | 644 | printf(" %s\n", "-T, --temperature"); |
631 | printf(" %s\n", _("Output of temperatures in Celsius")); | 645 | printf(" %s\n", _("Output of temperatures in Celsius")); |
632 | printf(" %s\n", "-v, --variable=STRING"); | 646 | printf(" %s\n", "-v, --variable=STRING"); |
633 | printf(" %s %s\n", _("Valid values for STRING are"), "LINE, TEMP, BATTPCT, LOADPCT or REALPOWER"); | 647 | printf(" %s %s\n", _("Valid values for STRING are"), |
648 | "LINE, TEMP, BATTPCT, LOADPCT or REALPOWER"); | ||
634 | 649 | ||
635 | printf(UT_WARN_CRIT); | 650 | printf(UT_WARN_CRIT); |
636 | 651 | ||
diff --git a/plugins/check_ups.d/config.h b/plugins/check_ups.d/config.h index 353104f2..e05edceb 100644 --- a/plugins/check_ups.d/config.h +++ b/plugins/check_ups.d/config.h | |||
@@ -52,4 +52,3 @@ check_ups_config check_ups_config_init(void) { | |||
52 | 52 | ||
53 | return tmp; | 53 | return tmp; |
54 | } | 54 | } |
55 | |||
diff --git a/plugins/check_users.c b/plugins/check_users.c index f1e1c39d..2340eae4 100644 --- a/plugins/check_users.c +++ b/plugins/check_users.c | |||
@@ -34,8 +34,15 @@ const char *progname = "check_users"; | |||
34 | const char *copyright = "2000-2024"; | 34 | const char *copyright = "2000-2024"; |
35 | const char *email = "devel@monitoring-plugins.org"; | 35 | const char *email = "devel@monitoring-plugins.org"; |
36 | 36 | ||
37 | #include "common.h" | 37 | #include "check_users.d/users.h" |
38 | #include "utils.h" | 38 | #include "output.h" |
39 | #include "perfdata.h" | ||
40 | #include "states.h" | ||
41 | #include "utils_base.h" | ||
42 | #include "./common.h" | ||
43 | #include "./utils.h" | ||
44 | #include "check_users.d/config.h" | ||
45 | #include "thresholds.h" | ||
39 | 46 | ||
40 | #if HAVE_WTSAPI32_H | 47 | #if HAVE_WTSAPI32_H |
41 | # include <windows.h> | 48 | # include <windows.h> |
@@ -53,29 +60,16 @@ const char *email = "devel@monitoring-plugins.org"; | |||
53 | # include <systemd/sd-login.h> | 60 | # include <systemd/sd-login.h> |
54 | #endif | 61 | #endif |
55 | 62 | ||
56 | #define possibly_set(a, b) ((a) == 0 ? (b) : 0) | 63 | typedef struct process_argument_wrapper { |
64 | int errorcode; | ||
65 | check_users_config config; | ||
66 | } check_users_config_wrapper; | ||
67 | check_users_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
57 | 68 | ||
58 | static int process_arguments(int, char **); | 69 | void print_help(void); |
59 | static void print_help(void); | ||
60 | void print_usage(void); | 70 | void print_usage(void); |
61 | 71 | ||
62 | static char *warning_range = NULL; | ||
63 | static char *critical_range = NULL; | ||
64 | static thresholds *thlds = NULL; | ||
65 | |||
66 | int main(int argc, char **argv) { | 72 | int main(int argc, char **argv) { |
67 | int users = -1; | ||
68 | int result = STATE_UNKNOWN; | ||
69 | #if HAVE_WTSAPI32_H | ||
70 | WTS_SESSION_INFO *wtsinfo; | ||
71 | DWORD wtscount; | ||
72 | DWORD index; | ||
73 | #elif HAVE_UTMPX_H | ||
74 | struct utmpx *putmpx; | ||
75 | #else | ||
76 | char input_buffer[MAX_INPUT_BUFFER]; | ||
77 | #endif | ||
78 | |||
79 | setlocale(LC_ALL, ""); | 73 | setlocale(LC_ALL, ""); |
80 | bindtextdomain(PACKAGE, LOCALEDIR); | 74 | bindtextdomain(PACKAGE, LOCALEDIR); |
81 | textdomain(PACKAGE); | 75 | textdomain(PACKAGE); |
@@ -83,121 +77,104 @@ int main(int argc, char **argv) { | |||
83 | /* Parse extra opts if any */ | 77 | /* Parse extra opts if any */ |
84 | argv = np_extra_opts(&argc, argv, progname); | 78 | argv = np_extra_opts(&argc, argv, progname); |
85 | 79 | ||
86 | if (process_arguments(argc, argv) == ERROR) | 80 | check_users_config_wrapper tmp_config = process_arguments(argc, argv); |
87 | usage4(_("Could not parse arguments")); | ||
88 | |||
89 | users = 0; | ||
90 | |||
91 | #ifdef HAVE_LIBSYSTEMD | ||
92 | if (sd_booted() > 0) | ||
93 | users = sd_get_sessions(NULL); | ||
94 | else { | ||
95 | #endif | ||
96 | #if HAVE_WTSAPI32_H | ||
97 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { | ||
98 | printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); | ||
99 | return STATE_UNKNOWN; | ||
100 | } | ||
101 | |||
102 | for (index = 0; index < wtscount; index++) { | ||
103 | LPTSTR username; | ||
104 | DWORD size; | ||
105 | int len; | ||
106 | |||
107 | if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) | ||
108 | continue; | ||
109 | |||
110 | len = lstrlen(username); | ||
111 | |||
112 | WTSFreeMemory(username); | ||
113 | |||
114 | if (len == 0) | ||
115 | continue; | ||
116 | 81 | ||
117 | if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) | 82 | if (tmp_config.errorcode == ERROR) { |
118 | users++; | 83 | usage4(_("Could not parse arguments")); |
119 | } | ||
120 | |||
121 | WTSFreeMemory(wtsinfo); | ||
122 | #elif HAVE_UTMPX_H | ||
123 | /* get currently logged users from utmpx */ | ||
124 | setutxent(); | ||
125 | |||
126 | while ((putmpx = getutxent()) != NULL) | ||
127 | if (putmpx->ut_type == USER_PROCESS) | ||
128 | users++; | ||
129 | |||
130 | endutxent(); | ||
131 | #else | ||
132 | /* run the command */ | ||
133 | child_process = spopen(WHO_COMMAND); | ||
134 | if (child_process == NULL) { | ||
135 | printf(_("Could not open pipe: %s\n"), WHO_COMMAND); | ||
136 | return STATE_UNKNOWN; | ||
137 | } | 84 | } |
138 | 85 | ||
139 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); | 86 | check_users_config config = tmp_config.config; |
140 | if (child_stderr == NULL) | ||
141 | printf(_("Could not open stderr for %s\n"), WHO_COMMAND); | ||
142 | |||
143 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
144 | /* increment 'users' on all lines except total user count */ | ||
145 | if (input_buffer[0] != '#') { | ||
146 | users++; | ||
147 | continue; | ||
148 | } | ||
149 | 87 | ||
150 | /* get total logged in users */ | 88 | #ifdef _WIN32 |
151 | if (sscanf(input_buffer, _("# users=%d"), &users) == 1) | 89 | # if HAVE_WTSAPI32_H |
152 | break; | 90 | get_num_of_users_wrapper user_wrapper = get_num_of_users_windows(); |
91 | # else | ||
92 | # error Did not find WTSAPI32 | ||
93 | # endif // HAVE_WTSAPI32_H | ||
94 | #else | ||
95 | # ifdef HAVE_LIBSYSTEMD | ||
96 | get_num_of_users_wrapper user_wrapper = get_num_of_users_systemd(); | ||
97 | # elif HAVE_UTMPX_H | ||
98 | get_num_of_users_wrapper user_wrapper = get_num_of_users_utmp(); | ||
99 | # else // !HAVE_LIBSYSTEMD && !HAVE_UTMPX_H | ||
100 | get_num_of_users_wrapper user_wrapper = get_num_of_users_who_command(); | ||
101 | # endif // HAVE_LIBSYSTEMD | ||
102 | #endif // _WIN32 | ||
103 | |||
104 | mp_check overall = mp_check_init(); | ||
105 | if (config.output_format_is_set) { | ||
106 | mp_set_format(config.output_format); | ||
153 | } | 107 | } |
108 | mp_subcheck sc_users = mp_subcheck_init(); | ||
154 | 109 | ||
155 | /* check STDERR */ | 110 | if (user_wrapper.errorcode != 0) { |
156 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | 111 | sc_users = mp_set_subcheck_state(sc_users, STATE_UNKNOWN); |
157 | result = possibly_set(result, STATE_UNKNOWN); | 112 | sc_users.output = "Failed to retrieve number of users"; |
158 | (void)fclose(child_stderr); | 113 | mp_add_subcheck_to_check(&overall, sc_users); |
159 | 114 | mp_exit(overall); | |
160 | /* close the pipe */ | ||
161 | if (spclose(child_process)) | ||
162 | result = possibly_set(result, STATE_UNKNOWN); | ||
163 | #endif | ||
164 | #ifdef HAVE_LIBSYSTEMD | ||
165 | } | 115 | } |
166 | #endif | ||
167 | |||
168 | /* check the user count against warning and critical thresholds */ | 116 | /* check the user count against warning and critical thresholds */ |
169 | result = get_status((double)users, thlds); | ||
170 | 117 | ||
171 | if (result == STATE_UNKNOWN) | 118 | mp_perfdata users_pd = { |
172 | printf("%s\n", _("Unable to read output")); | 119 | .label = "users", |
173 | else { | 120 | .value = mp_create_pd_value(user_wrapper.users), |
174 | printf(_("USERS %s - %d users currently logged in |%s\n"), state_text(result), users, | 121 | }; |
175 | sperfdata_int("users", users, "", warning_range, critical_range, true, 0, false, 0)); | 122 | |
123 | users_pd = mp_pd_set_thresholds(users_pd, config.thresholds); | ||
124 | mp_add_perfdata_to_subcheck(&sc_users, users_pd); | ||
125 | |||
126 | int tmp_status = mp_get_pd_status(users_pd); | ||
127 | sc_users = mp_set_subcheck_state(sc_users, tmp_status); | ||
128 | |||
129 | switch (tmp_status) { | ||
130 | case STATE_WARNING: | ||
131 | xasprintf(&sc_users.output, | ||
132 | "%d users currently logged in. This violates the warning threshold", | ||
133 | user_wrapper.users); | ||
134 | break; | ||
135 | case STATE_CRITICAL: | ||
136 | xasprintf(&sc_users.output, | ||
137 | "%d users currently logged in. This violates the critical threshold", | ||
138 | user_wrapper.users); | ||
139 | break; | ||
140 | default: | ||
141 | xasprintf(&sc_users.output, "%d users currently logged in", user_wrapper.users); | ||
176 | } | 142 | } |
177 | 143 | ||
178 | return result; | 144 | mp_add_subcheck_to_check(&overall, sc_users); |
145 | mp_exit(overall); | ||
179 | } | 146 | } |
180 | 147 | ||
148 | #define output_format_index CHAR_MAX + 1 | ||
149 | |||
181 | /* process command-line arguments */ | 150 | /* process command-line arguments */ |
182 | int process_arguments(int argc, char **argv) { | 151 | check_users_config_wrapper process_arguments(int argc, char **argv) { |
183 | static struct option longopts[] = {{"critical", required_argument, 0, 'c'}, | 152 | static struct option longopts[] = {{"critical", required_argument, 0, 'c'}, |
184 | {"warning", required_argument, 0, 'w'}, | 153 | {"warning", required_argument, 0, 'w'}, |
185 | {"version", no_argument, 0, 'V'}, | 154 | {"version", no_argument, 0, 'V'}, |
186 | {"help", no_argument, 0, 'h'}, | 155 | {"help", no_argument, 0, 'h'}, |
156 | {"output-format", required_argument, 0, output_format_index}, | ||
187 | {0, 0, 0, 0}}; | 157 | {0, 0, 0, 0}}; |
188 | 158 | ||
189 | if (argc < 2) | 159 | if (argc < 2) { |
190 | usage("\n"); | 160 | usage(progname); |
161 | } | ||
162 | |||
163 | char *warning_range = NULL; | ||
164 | char *critical_range = NULL; | ||
165 | check_users_config_wrapper result = { | ||
166 | .config = check_users_config_init(), | ||
167 | .errorcode = OK, | ||
168 | }; | ||
191 | 169 | ||
192 | int option_char; | ||
193 | while (true) { | 170 | while (true) { |
194 | int option = 0; | 171 | int counter = getopt_long(argc, argv, "+hVvc:w:", longopts, NULL); |
195 | option_char = getopt_long(argc, argv, "+hVvc:w:", longopts, &option); | ||
196 | 172 | ||
197 | if (option_char == -1 || option_char == EOF || option_char == 1) | 173 | if (counter == -1 || counter == EOF || counter == 1) { |
198 | break; | 174 | break; |
175 | } | ||
199 | 176 | ||
200 | switch (option_char) { | 177 | switch (counter) { |
201 | case '?': /* print short usage statement if args not parsable */ | 178 | case '?': /* print short usage statement if args not parsable */ |
202 | usage5(); | 179 | usage5(); |
203 | case 'h': /* help */ | 180 | case 'h': /* help */ |
@@ -212,29 +189,66 @@ int process_arguments(int argc, char **argv) { | |||
212 | case 'w': /* warning */ | 189 | case 'w': /* warning */ |
213 | warning_range = optarg; | 190 | warning_range = optarg; |
214 | break; | 191 | break; |
192 | case output_format_index: { | ||
193 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
194 | if (!parser.parsing_success) { | ||
195 | // TODO List all available formats here, maybe add anothoer usage function | ||
196 | printf("Invalid output format: %s\n", optarg); | ||
197 | exit(STATE_UNKNOWN); | ||
198 | } | ||
199 | |||
200 | result.config.output_format_is_set = true; | ||
201 | result.config.output_format = parser.output_format; | ||
202 | break; | ||
203 | } | ||
215 | } | 204 | } |
216 | } | 205 | } |
217 | 206 | ||
218 | option_char = optind; | 207 | int option_char = optind; |
219 | 208 | ||
220 | if (warning_range == NULL && argc > option_char) | 209 | if (warning_range == NULL && argc > option_char) { |
221 | warning_range = argv[option_char++]; | 210 | warning_range = argv[option_char++]; |
211 | } | ||
222 | 212 | ||
223 | if (critical_range == NULL && argc > option_char) | 213 | if (critical_range == NULL && argc > option_char) { |
224 | critical_range = argv[option_char++]; | 214 | critical_range = argv[option_char++]; |
215 | } | ||
225 | 216 | ||
226 | /* this will abort in case of invalid ranges */ | 217 | // TODO add proper verification for ranges here! |
227 | set_thresholds(&thlds, warning_range, critical_range); | 218 | mp_range_parsed tmp; |
219 | if (warning_range) { | ||
220 | tmp = mp_parse_range_string(warning_range); | ||
221 | } else { | ||
222 | printf("Warning threshold missing\n"); | ||
223 | print_usage(); | ||
224 | exit(STATE_UNKNOWN); | ||
225 | } | ||
228 | 226 | ||
229 | if (!thlds->warning) { | 227 | if (tmp.error == MP_PARSING_SUCCES) { |
230 | usage4(_("Warning threshold must be a valid range expression")); | 228 | result.config.thresholds.warning = tmp.range; |
229 | result.config.thresholds.warning_is_set = true; | ||
230 | } else { | ||
231 | printf("Failed to parse warning range: %s", warning_range); | ||
232 | exit(STATE_UNKNOWN); | ||
231 | } | 233 | } |
232 | 234 | ||
233 | if (!thlds->critical) { | 235 | if (critical_range) { |
234 | usage4(_("Critical threshold must be a valid range expression")); | 236 | tmp = mp_parse_range_string(critical_range); |
237 | } else { | ||
238 | printf("Critical threshold missing\n"); | ||
239 | print_usage(); | ||
240 | exit(STATE_UNKNOWN); | ||
235 | } | 241 | } |
236 | 242 | ||
237 | return OK; | 243 | if (tmp.error == MP_PARSING_SUCCES) { |
244 | result.config.thresholds.critical = tmp.range; | ||
245 | result.config.thresholds.critical_is_set = true; | ||
246 | } else { | ||
247 | printf("Failed to parse critical range: %s", critical_range); | ||
248 | exit(STATE_UNKNOWN); | ||
249 | } | ||
250 | |||
251 | return result; | ||
238 | } | 252 | } |
239 | 253 | ||
240 | void print_help(void) { | 254 | void print_help(void) { |
@@ -244,7 +258,8 @@ void print_help(void) { | |||
244 | printf(COPYRIGHT, copyright, email); | 258 | printf(COPYRIGHT, copyright, email); |
245 | 259 | ||
246 | printf("%s\n", _("This plugin checks the number of users currently logged in on the local")); | 260 | printf("%s\n", _("This plugin checks the number of users currently logged in on the local")); |
247 | printf("%s\n", _("system and generates an error if the number exceeds the thresholds specified.")); | 261 | printf("%s\n", |
262 | _("system and generates an error if the number exceeds the thresholds specified.")); | ||
248 | 263 | ||
249 | printf("\n\n"); | 264 | printf("\n\n"); |
250 | 265 | ||
@@ -254,9 +269,12 @@ void print_help(void) { | |||
254 | printf(UT_EXTRA_OPTS); | 269 | printf(UT_EXTRA_OPTS); |
255 | 270 | ||
256 | printf(" %s\n", "-w, --warning=RANGE_EXPRESSION"); | 271 | printf(" %s\n", "-w, --warning=RANGE_EXPRESSION"); |
257 | printf(" %s\n", _("Set WARNING status if number of logged in users violates RANGE_EXPRESSION")); | 272 | printf(" %s\n", |
273 | _("Set WARNING status if number of logged in users violates RANGE_EXPRESSION")); | ||
258 | printf(" %s\n", "-c, --critical=RANGE_EXPRESSION"); | 274 | printf(" %s\n", "-c, --critical=RANGE_EXPRESSION"); |
259 | printf(" %s\n", _("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION")); | 275 | printf(" %s\n", |
276 | _("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION")); | ||
277 | printf(UT_OUTPUT_FORMAT); | ||
260 | 278 | ||
261 | printf(UT_SUPPORT); | 279 | printf(UT_SUPPORT); |
262 | } | 280 | } |
diff --git a/plugins/check_users.d/config.h b/plugins/check_users.d/config.h new file mode 100644 index 00000000..26d3ee70 --- /dev/null +++ b/plugins/check_users.d/config.h | |||
@@ -0,0 +1,20 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "output.h" | ||
4 | #include "thresholds.h" | ||
5 | |||
6 | typedef struct check_users_config { | ||
7 | mp_thresholds thresholds; | ||
8 | |||
9 | bool output_format_is_set; | ||
10 | mp_output_format output_format; | ||
11 | } check_users_config; | ||
12 | |||
13 | check_users_config check_users_config_init() { | ||
14 | check_users_config tmp = { | ||
15 | .thresholds = mp_thresholds_init(), | ||
16 | |||
17 | .output_format_is_set = false, | ||
18 | }; | ||
19 | return tmp; | ||
20 | } | ||
diff --git a/plugins/check_users.d/users.c b/plugins/check_users.d/users.c new file mode 100644 index 00000000..f37819b1 --- /dev/null +++ b/plugins/check_users.d/users.c | |||
@@ -0,0 +1,169 @@ | |||
1 | #include "./users.h" | ||
2 | |||
3 | #ifdef _WIN32 | ||
4 | # ifdef HAVE_WTSAPI32_H | ||
5 | # include <windows.h> | ||
6 | # include <wtsapi32.h> | ||
7 | # undef ERROR | ||
8 | # define ERROR -1 | ||
9 | |||
10 | get_num_of_users_wrapper get_num_of_users_windows() { | ||
11 | WTS_SESSION_INFO *wtsinfo; | ||
12 | DWORD wtscount; | ||
13 | |||
14 | get_num_of_users_wrapper result = {}; | ||
15 | |||
16 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { | ||
17 | // printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); | ||
18 | result.error = WINDOWS_COULD_NOT_ENUMERATE_SESSIONS; | ||
19 | return result; | ||
20 | } | ||
21 | |||
22 | for (DWORD index = 0; index < wtscount; index++) { | ||
23 | LPTSTR username; | ||
24 | DWORD size; | ||
25 | |||
26 | if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, | ||
27 | WTSUserName, &username, &size)) { | ||
28 | continue; | ||
29 | } | ||
30 | |||
31 | int len = lstrlen(username); | ||
32 | |||
33 | WTSFreeMemory(username); | ||
34 | |||
35 | if (len == 0) { | ||
36 | continue; | ||
37 | } | ||
38 | |||
39 | if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) { | ||
40 | result.users++; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | WTSFreeMemory(wtsinfo); | ||
45 | return result; | ||
46 | } | ||
47 | # else // HAVE_WTSAPI32_H | ||
48 | # error On windows but without the WTSAPI32 lib | ||
49 | # endif // HAVE_WTSAPI32_H | ||
50 | |||
51 | #else // _WIN32 | ||
52 | |||
53 | # include "../../config.h" | ||
54 | # include <stddef.h> | ||
55 | |||
56 | # ifdef HAVE_LIBSYSTEMD | ||
57 | # include <systemd/sd-daemon.h> | ||
58 | # include <systemd/sd-login.h> | ||
59 | |||
60 | get_num_of_users_wrapper get_num_of_users_systemd() { | ||
61 | get_num_of_users_wrapper result = {}; | ||
62 | |||
63 | // Test whether we booted with systemd | ||
64 | if (sd_booted() > 0) { | ||
65 | int users = sd_get_uids(NULL); | ||
66 | if (users >= 0) { | ||
67 | // Success | ||
68 | result.users = users; | ||
69 | return result; | ||
70 | } | ||
71 | |||
72 | // Failure! return the error code | ||
73 | result.errorcode = users; | ||
74 | return result; | ||
75 | } | ||
76 | |||
77 | // Looks like we are not running systemd, | ||
78 | // return with error here | ||
79 | result.errorcode = NO_SYSTEMD_ERROR; | ||
80 | return result; | ||
81 | } | ||
82 | # endif | ||
83 | |||
84 | # ifdef HAVE_UTMPX_H | ||
85 | # include <utmpx.h> | ||
86 | |||
87 | get_num_of_users_wrapper get_num_of_users_utmp() { | ||
88 | int users = 0; | ||
89 | |||
90 | /* get currently logged users from utmpx */ | ||
91 | setutxent(); | ||
92 | |||
93 | struct utmpx *putmpx; | ||
94 | while ((putmpx = getutxent()) != NULL) { | ||
95 | if (putmpx->ut_type == USER_PROCESS) { | ||
96 | users++; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | endutxent(); | ||
101 | |||
102 | get_num_of_users_wrapper result = { | ||
103 | .errorcode = 0, | ||
104 | .users = users, | ||
105 | }; | ||
106 | |||
107 | return result; | ||
108 | } | ||
109 | # endif | ||
110 | |||
111 | # ifndef HAVE_WTSAPI32_H | ||
112 | # ifndef HAVE_LIBSYSTEMD | ||
113 | # ifndef HAVE_UTMPX_H | ||
114 | // Fall back option here for the others (probably still not on windows) | ||
115 | |||
116 | # include "../popen.h" | ||
117 | # include "../common.h" | ||
118 | # include "../utils.h" | ||
119 | |||
120 | get_num_of_users_wrapper get_num_of_users_who_command() { | ||
121 | /* run the command */ | ||
122 | child_process = spopen(WHO_COMMAND); | ||
123 | if (child_process == NULL) { | ||
124 | // printf(_("Could not open pipe: %s\n"), WHO_COMMAND); | ||
125 | get_num_of_users_wrapper result = { | ||
126 | .errorcode = COULD_NOT_OPEN_PIPE, | ||
127 | }; | ||
128 | return result; | ||
129 | } | ||
130 | |||
131 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); | ||
132 | if (child_stderr == NULL) { | ||
133 | // printf(_("Could not open stderr for %s\n"), WHO_COMMAND); | ||
134 | // TODO this error should probably be reported | ||
135 | } | ||
136 | |||
137 | get_num_of_users_wrapper result = {}; | ||
138 | char input_buffer[MAX_INPUT_BUFFER]; | ||
139 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
140 | /* increment 'users' on all lines except total user count */ | ||
141 | if (input_buffer[0] != '#') { | ||
142 | result.users++; | ||
143 | continue; | ||
144 | } | ||
145 | |||
146 | /* get total logged in users */ | ||
147 | if (sscanf(input_buffer, _("# users=%d"), &result.users) == 1) { | ||
148 | break; | ||
149 | } | ||
150 | } | ||
151 | |||
152 | /* check STDERR */ | ||
153 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
154 | // if this fails, something broke and the result can not be relied upon or so is the theorie | ||
155 | // here | ||
156 | result.errorcode = STDERR_COULD_NOT_BE_READ; | ||
157 | } | ||
158 | (void)fclose(child_stderr); | ||
159 | |||
160 | /* close the pipe */ | ||
161 | spclose(child_process); | ||
162 | |||
163 | return result; | ||
164 | } | ||
165 | |||
166 | # endif | ||
167 | # endif | ||
168 | # endif | ||
169 | #endif | ||
diff --git a/plugins/check_users.d/users.h b/plugins/check_users.d/users.h new file mode 100644 index 00000000..aacba775 --- /dev/null +++ b/plugins/check_users.d/users.h | |||
@@ -0,0 +1,18 @@ | |||
1 | #pragma once | ||
2 | |||
3 | typedef struct get_num_of_users_wrapper { | ||
4 | int errorcode; | ||
5 | int users; | ||
6 | } get_num_of_users_wrapper; | ||
7 | |||
8 | enum { | ||
9 | NO_SYSTEMD_ERROR = 64, | ||
10 | WINDOWS_COULD_NOT_ENUMERATE_SESSIONS, | ||
11 | COULD_NOT_OPEN_PIPE, | ||
12 | STDERR_COULD_NOT_BE_READ, | ||
13 | }; | ||
14 | |||
15 | get_num_of_users_wrapper get_num_of_users_systemd(); | ||
16 | get_num_of_users_wrapper get_num_of_users_utmp(); | ||
17 | get_num_of_users_wrapper get_num_of_users_windows(); | ||
18 | get_num_of_users_wrapper get_num_of_users_who_command(); | ||
diff --git a/plugins/common.h b/plugins/common.h index 603bae55..ef888d08 100644 --- a/plugins/common.h +++ b/plugins/common.h | |||
@@ -1,121 +1,122 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring Plugins common include file | 3 | * Monitoring Plugins common include file |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) | 6 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) |
7 | * Copyright (c) 2003-2007 Monitoring Plugins Development Team | 7 | * Copyright (c) 2003-2007 Monitoring Plugins Development Team |
8 | * | 8 | * |
9 | * Description: | 9 | * Description: |
10 | * | 10 | * |
11 | * This file contains common include files and defines used in many of | 11 | * This file contains common include files and defines used in many of |
12 | * the plugins. | 12 | * the plugins. |
13 | * | 13 | * |
14 | * | 14 | * |
15 | * This program is free software: you can redistribute it and/or modify | 15 | * This program is free software: you can redistribute it and/or modify |
16 | * it under the terms of the GNU General Public License as published by | 16 | * it under the terms of the GNU General Public License as published by |
17 | * the Free Software Foundation, either version 3 of the License, or | 17 | * the Free Software Foundation, either version 3 of the License, or |
18 | * (at your option) any later version. | 18 | * (at your option) any later version. |
19 | * | 19 | * |
20 | * This program is distributed in the hope that it will be useful, | 20 | * This program is distributed in the hope that it will be useful, |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | * GNU General Public License for more details. | 23 | * GNU General Public License for more details. |
24 | * | 24 | * |
25 | * You should have received a copy of the GNU General Public License | 25 | * You should have received a copy of the GNU General Public License |
26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | * | 27 | * |
28 | * | 28 | * |
29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
30 | 30 | ||
31 | #ifndef _COMMON_H_ | 31 | #ifndef _COMMON_H_ |
32 | #define _COMMON_H_ | 32 | #define _COMMON_H_ |
33 | 33 | ||
34 | #include "config.h" | 34 | #include "../config.h" |
35 | #include "../lib/monitoringplug.h" | 35 | #include "../lib/monitoringplug.h" |
36 | 36 | ||
37 | #ifdef HAVE_FEATURES_H | 37 | #ifdef HAVE_FEATURES_H |
38 | #include <features.h> | 38 | # include <features.h> |
39 | #endif | 39 | #endif |
40 | 40 | ||
41 | #include <stdio.h> /* obligatory includes */ | 41 | #include <stdio.h> /* obligatory includes */ |
42 | #include <stdlib.h> | 42 | #include <stdlib.h> |
43 | #include <errno.h> | 43 | #include <errno.h> |
44 | 44 | ||
45 | /* This block provides uintmax_t - should be reported to coreutils that this should be added to fsuage.h */ | 45 | /* This block provides uintmax_t - should be reported to coreutils that this should be added to |
46 | * fsuage.h */ | ||
46 | #if HAVE_INTTYPES_H | 47 | #if HAVE_INTTYPES_H |
47 | # include <inttypes.h> | 48 | # include <inttypes.h> |
48 | #endif | 49 | #endif |
49 | #if HAVE_STDINT_H | 50 | #if HAVE_STDINT_H |
50 | # include <stdint.h> | 51 | # include <stdint.h> |
51 | #endif | 52 | #endif |
52 | #include <unistd.h> | 53 | #include <unistd.h> |
53 | #ifndef UINTMAX_MAX | 54 | #ifndef UINTMAX_MAX |
54 | # define UINTMAX_MAX ((uintmax_t) -1) | 55 | # define UINTMAX_MAX ((uintmax_t) - 1) |
55 | #endif | 56 | #endif |
56 | 57 | ||
57 | #include <limits.h> /* This is assumed true, because coreutils assume it too */ | 58 | #include <limits.h> /* This is assumed true, because coreutils assume it too */ |
58 | 59 | ||
59 | #ifdef HAVE_MATH_H | 60 | #ifdef HAVE_MATH_H |
60 | #include <math.h> | 61 | # include <math.h> |
61 | #endif | 62 | #endif |
62 | 63 | ||
63 | #ifdef _AIX | 64 | #ifdef _AIX |
64 | #ifdef HAVE_MP_H | 65 | # ifdef HAVE_MP_H |
65 | #include <mp.h> | 66 | # include <mp.h> |
66 | #endif | 67 | # endif |
67 | #endif | 68 | #endif |
68 | 69 | ||
69 | #ifdef HAVE_STRINGS_H | 70 | #ifdef HAVE_STRINGS_H |
70 | #include <strings.h> | 71 | # include <strings.h> |
71 | #endif | 72 | #endif |
72 | #ifdef HAVE_STRING_H | 73 | #ifdef HAVE_STRING_H |
73 | #include <string.h> | 74 | # include <string.h> |
74 | #endif | 75 | #endif |
75 | 76 | ||
76 | #ifdef HAVE_UNISTD_H | 77 | #ifdef HAVE_UNISTD_H |
77 | #include <unistd.h> | 78 | # include <unistd.h> |
78 | #endif | 79 | #endif |
79 | 80 | ||
80 | /* GET_NUMBER_OF_CPUS is a macro to return | 81 | /* GET_NUMBER_OF_CPUS is a macro to return |
81 | number of CPUs, if we can get that data. | 82 | number of CPUs, if we can get that data. |
82 | Use configure.in to test for various OS ways of | 83 | Use configure.in to test for various OS ways of |
83 | getting that data | 84 | getting that data |
84 | Will return -1 if cannot get data | 85 | Will return -1 if cannot get data |
85 | */ | 86 | */ |
86 | #if defined(HAVE_SYSCONF__SC_NPROCESSORS_ONLN) | 87 | #if defined(HAVE_SYSCONF__SC_NPROCESSORS_ONLN) |
87 | # define GET_NUMBER_OF_CPUS() sysconf(_SC_NPROCESSORS_ONLN) | 88 | # define GET_NUMBER_OF_CPUS() sysconf(_SC_NPROCESSORS_ONLN) |
88 | #elif defined (HAVE_SYSCONF__SC_NPROCESSORS_CONF) | 89 | #elif defined(HAVE_SYSCONF__SC_NPROCESSORS_CONF) |
89 | # define GET_NUMBER_OF_CPUS() sysconf(_SC_NPROCESSORS_CONF) | 90 | # define GET_NUMBER_OF_CPUS() sysconf(_SC_NPROCESSORS_CONF) |
90 | #else | 91 | #else |
91 | # define GET_NUMBER_OF_CPUS() -1 | 92 | # define GET_NUMBER_OF_CPUS() -1 |
92 | #endif | 93 | #endif |
93 | 94 | ||
94 | #ifdef HAVE_SYS_TIME_H | 95 | #ifdef HAVE_SYS_TIME_H |
95 | # include <sys/time.h> | 96 | # include <sys/time.h> |
96 | #endif | 97 | #endif |
97 | #include <time.h> | 98 | #include <time.h> |
98 | 99 | ||
99 | #ifdef HAVE_SYS_TYPES_H | 100 | #ifdef HAVE_SYS_TYPES_H |
100 | #include <sys/types.h> | 101 | # include <sys/types.h> |
101 | #endif | 102 | #endif |
102 | 103 | ||
103 | #ifdef HAVE_SYS_SOCKET_H | 104 | #ifdef HAVE_SYS_SOCKET_H |
104 | #include <sys/socket.h> | 105 | # include <sys/socket.h> |
105 | #endif | 106 | #endif |
106 | 107 | ||
107 | #ifdef HAVE_SIGNAL_H | 108 | #ifdef HAVE_SIGNAL_H |
108 | #include <signal.h> | 109 | # include <signal.h> |
109 | #endif | 110 | #endif |
110 | 111 | ||
111 | /* GNU Libraries */ | 112 | /* GNU Libraries */ |
112 | #include <getopt.h> | 113 | #include <getopt.h> |
113 | #include "dirname.h" | 114 | #include "../gl/dirname.h" |
114 | 115 | ||
115 | #include <locale.h> | 116 | #include <locale.h> |
116 | 117 | ||
117 | #ifdef HAVE_SYS_POLL_H | 118 | #ifdef HAVE_SYS_POLL_H |
118 | # include "sys/poll.h" | 119 | # include "sys/poll.h" |
119 | #endif | 120 | #endif |
120 | 121 | ||
121 | /* | 122 | /* |
@@ -125,42 +126,42 @@ | |||
125 | */ | 126 | */ |
126 | 127 | ||
127 | #ifndef HAVE_STRTOL | 128 | #ifndef HAVE_STRTOL |
128 | # define strtol(a,b,c) atol((a)) | 129 | # define strtol(a, b, c) atol((a)) |
129 | #endif | 130 | #endif |
130 | 131 | ||
131 | #ifndef HAVE_STRTOUL | 132 | #ifndef HAVE_STRTOUL |
132 | # define strtoul(a,b,c) (unsigned long)atol((a)) | 133 | # define strtoul(a, b, c) (unsigned long)atol((a)) |
133 | #endif | 134 | #endif |
134 | 135 | ||
135 | /* SSL implementations */ | 136 | /* SSL implementations */ |
136 | #ifdef HAVE_GNUTLS_OPENSSL_H | 137 | #ifdef HAVE_GNUTLS_OPENSSL_H |
137 | # include <gnutls/openssl.h> | 138 | # include <gnutls/openssl.h> |
138 | #else | 139 | #else |
139 | # define OPENSSL_LOAD_CONF /* See the OPENSSL_config(3) man page. */ | 140 | # define OPENSSL_LOAD_CONF /* See the OPENSSL_config(3) man page. */ |
140 | # ifdef HAVE_SSL_H | 141 | # ifdef HAVE_SSL_H |
141 | # include <rsa.h> | 142 | # include <rsa.h> |
142 | # include <crypto.h> | 143 | # include <crypto.h> |
143 | # include <x509.h> | 144 | # include <x509.h> |
144 | # include <pem.h> | 145 | # include <pem.h> |
145 | # include <ssl.h> | 146 | # include <ssl.h> |
146 | # include <err.h> | 147 | # include <err.h> |
147 | # else | 148 | # else |
148 | # ifdef HAVE_OPENSSL_SSL_H | 149 | # ifdef HAVE_OPENSSL_SSL_H |
149 | # include <openssl/rsa.h> | 150 | # include <openssl/rsa.h> |
150 | # include <openssl/crypto.h> | 151 | # include <openssl/crypto.h> |
151 | # include <openssl/x509.h> | 152 | # include <openssl/x509.h> |
152 | # include <openssl/pem.h> | 153 | # include <openssl/pem.h> |
153 | # include <openssl/ssl.h> | 154 | # include <openssl/ssl.h> |
154 | # include <openssl/err.h> | 155 | # include <openssl/err.h> |
155 | # endif | 156 | # endif |
156 | # endif | 157 | # endif |
157 | #endif | 158 | #endif |
158 | 159 | ||
159 | /* openssl 1.1 does not set OPENSSL_NO_SSL2 by default but ships without ssl2 */ | 160 | /* openssl 1.1 does not set OPENSSL_NO_SSL2 by default but ships without ssl2 */ |
160 | #ifdef OPENSSL_VERSION_NUMBER | 161 | #ifdef OPENSSL_VERSION_NUMBER |
161 | # if OPENSSL_VERSION_NUMBER >= 0x10100000 | 162 | # if OPENSSL_VERSION_NUMBER >= 0x10100000 |
162 | # define OPENSSL_NO_SSL2 | 163 | # define OPENSSL_NO_SSL2 |
163 | # endif | 164 | # endif |
164 | #endif | 165 | #endif |
165 | 166 | ||
166 | /* | 167 | /* |
@@ -171,7 +172,7 @@ | |||
171 | 172 | ||
172 | /* MariaDB 10.2 client does not set MYSQL_PORT */ | 173 | /* MariaDB 10.2 client does not set MYSQL_PORT */ |
173 | #ifndef MYSQL_PORT | 174 | #ifndef MYSQL_PORT |
174 | # define MYSQL_PORT 3306 | 175 | # define MYSQL_PORT 3306 |
175 | #endif | 176 | #endif |
176 | 177 | ||
177 | enum { | 178 | enum { |
@@ -180,9 +181,9 @@ enum { | |||
180 | }; | 181 | }; |
181 | 182 | ||
182 | enum { | 183 | enum { |
183 | DEFAULT_SOCKET_TIMEOUT = 10, /* timeout after 10 seconds */ | 184 | DEFAULT_SOCKET_TIMEOUT = 10, /* timeout after 10 seconds */ |
184 | MAX_INPUT_BUFFER = 8192, /* max size of most buffers we use */ | 185 | MAX_INPUT_BUFFER = 8192, /* max size of most buffers we use */ |
185 | MAX_HOST_ADDRESS_LENGTH = 256 /* max size of a host address */ | 186 | MAX_HOST_ADDRESS_LENGTH = 256 /* max size of a host address */ |
186 | }; | 187 | }; |
187 | 188 | ||
188 | /* | 189 | /* |
@@ -190,18 +191,18 @@ enum { | |||
190 | * Internationalization | 191 | * Internationalization |
191 | * | 192 | * |
192 | */ | 193 | */ |
193 | #include "gettext.h" | 194 | #include "../gl/gettext.h" |
194 | #define _(String) gettext (String) | 195 | #define _(String) gettext(String) |
195 | #if ! ENABLE_NLS | 196 | #if !ENABLE_NLS |
196 | # undef textdomain | 197 | # undef textdomain |
197 | # define textdomain(Domainname) /* empty */ | 198 | # define textdomain(Domainname) /* empty */ |
198 | # undef bindtextdomain | 199 | # undef bindtextdomain |
199 | # define bindtextdomain(Domainname, Dirname) /* empty */ | 200 | # define bindtextdomain(Domainname, Dirname) /* empty */ |
200 | #endif | 201 | #endif |
201 | 202 | ||
202 | /* For non-GNU compilers to ignore __attribute__ */ | 203 | /* For non-GNU compilers to ignore __attribute__ */ |
203 | #ifndef __GNUC__ | 204 | #ifndef __GNUC__ |
204 | # define __attribute__(x) /* do nothing */ | 205 | # define __attribute__(x) /* do nothing */ |
205 | #endif | 206 | #endif |
206 | 207 | ||
207 | #endif /* _COMMON_H_ */ | 208 | #endif /* _COMMON_H_ */ |
diff --git a/plugins/negate.c b/plugins/negate.c index 0520d298..a42a6c59 100644 --- a/plugins/negate.c +++ b/plugins/negate.c | |||
@@ -105,7 +105,8 @@ int main(int argc, char **argv) { | |||
105 | *sub = '\0'; | 105 | *sub = '\0'; |
106 | sub += strlen(state_text(result)); | 106 | sub += strlen(state_text(result)); |
107 | /* then put everything back together */ | 107 | /* then put everything back together */ |
108 | xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text(config.state[result]), sub); | 108 | xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], |
109 | state_text(config.state[result]), sub); | ||
109 | } | 110 | } |
110 | } | 111 | } |
111 | printf("%s\n", chld_out.line[i]); | 112 | printf("%s\n", chld_out.line[i]); |
@@ -120,11 +121,12 @@ int main(int argc, char **argv) { | |||
120 | 121 | ||
121 | /* process command-line arguments */ | 122 | /* process command-line arguments */ |
122 | static negate_config_wrapper process_arguments(int argc, char **argv) { | 123 | static negate_config_wrapper process_arguments(int argc, char **argv) { |
123 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, | 124 | static struct option longopts[] = { |
124 | {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, | 125 | {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, |
125 | {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, | 126 | {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, |
126 | {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, | 127 | {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, |
127 | {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; | 128 | {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, |
129 | {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; | ||
128 | 130 | ||
129 | negate_config_wrapper result = { | 131 | negate_config_wrapper result = { |
130 | .errorcode = OK, | 132 | .errorcode = OK, |
@@ -159,31 +161,36 @@ static negate_config_wrapper process_arguments(int argc, char **argv) { | |||
159 | break; | 161 | break; |
160 | case 'T': /* Result to return on timeouts */ | 162 | case 'T': /* Result to return on timeouts */ |
161 | if ((timeout_state = mp_translate_state(optarg)) == ERROR) { | 163 | if ((timeout_state = mp_translate_state(optarg)) == ERROR) { |
162 | usage4(_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 164 | usage4(_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, " |
165 | "UNKNOWN) or integer (0-3).")); | ||
163 | } | 166 | } |
164 | break; | 167 | break; |
165 | case 'o': /* replacement for OK */ | 168 | case 'o': /* replacement for OK */ |
166 | if ((result.config.state[STATE_OK] = mp_translate_state(optarg)) == ERROR) { | 169 | if ((result.config.state[STATE_OK] = mp_translate_state(optarg)) == ERROR) { |
167 | usage4(_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 170 | usage4(_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or " |
171 | "integer (0-3).")); | ||
168 | } | 172 | } |
169 | permute = false; | 173 | permute = false; |
170 | break; | 174 | break; |
171 | 175 | ||
172 | case 'w': /* replacement for WARNING */ | 176 | case 'w': /* replacement for WARNING */ |
173 | if ((result.config.state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) { | 177 | if ((result.config.state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) { |
174 | usage4(_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 178 | usage4(_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or " |
179 | "integer (0-3).")); | ||
175 | } | 180 | } |
176 | permute = false; | 181 | permute = false; |
177 | break; | 182 | break; |
178 | case 'c': /* replacement for CRITICAL */ | 183 | case 'c': /* replacement for CRITICAL */ |
179 | if ((result.config.state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) { | 184 | if ((result.config.state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) { |
180 | usage4(_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 185 | usage4(_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or " |
186 | "integer (0-3).")); | ||
181 | } | 187 | } |
182 | permute = false; | 188 | permute = false; |
183 | break; | 189 | break; |
184 | case 'u': /* replacement for UNKNOWN */ | 190 | case 'u': /* replacement for UNKNOWN */ |
185 | if ((result.config.state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) { | 191 | if ((result.config.state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) { |
186 | usage4(_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 192 | usage4(_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or " |
193 | "integer (0-3).")); | ||
187 | } | 194 | } |
188 | permute = false; | 195 | permute = false; |
189 | break; | 196 | break; |
@@ -208,7 +215,8 @@ negate_config_wrapper validate_arguments(negate_config_wrapper config_wrapper) { | |||
208 | usage4(_("Could not parse arguments")); | 215 | usage4(_("Could not parse arguments")); |
209 | } | 216 | } |
210 | 217 | ||
211 | if (strncmp(config_wrapper.config.command_line[0], "/", 1) != 0 && strncmp(config_wrapper.config.command_line[0], "./", 2) != 0) { | 218 | if (strncmp(config_wrapper.config.command_line[0], "/", 1) != 0 && |
219 | strncmp(config_wrapper.config.command_line[0], "./", 2) != 0) { | ||
212 | usage4(_("Require path to command")); | 220 | usage4(_("Require path to command")); |
213 | } | 221 | } |
214 | 222 | ||
@@ -220,7 +228,8 @@ void print_help(void) { | |||
220 | 228 | ||
221 | printf(COPYRIGHT, copyright, email); | 229 | printf(COPYRIGHT, copyright, email); |
222 | 230 | ||
223 | printf("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and vice-versa) by default.")); | 231 | printf("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and " |
232 | "vice-versa) by default.")); | ||
224 | printf("%s\n", _("Additional switches can be used to control:\n")); | 233 | printf("%s\n", _("Additional switches can be used to control:\n")); |
225 | printf("\t - which state becomes what\n"); | 234 | printf("\t - which state becomes what\n"); |
226 | printf("\t - changing the plugin output text to match the return code"); | 235 | printf("\t - changing the plugin output text to match the return code"); |
@@ -250,17 +259,20 @@ void print_help(void) { | |||
250 | printf("%s\n", _("Examples:")); | 259 | printf("%s\n", _("Examples:")); |
251 | printf(" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host"); | 260 | printf(" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host"); |
252 | printf(" %s\n", _("Run check_ping and invert result. Must use full path to plugin")); | 261 | printf(" %s\n", _("Run check_ping and invert result. Must use full path to plugin")); |
253 | printf(" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'"); | 262 | printf(" %s\n", |
263 | "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'"); | ||
254 | printf(" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL")); | 264 | printf(" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL")); |
255 | printf("\n"); | 265 | printf("\n"); |
256 | printf("%s\n", _("Notes:")); | 266 | printf("%s\n", _("Notes:")); |
257 | printf(" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); | 267 | printf(" %s\n", |
268 | _("This plugin is a wrapper to take the output of another plugin and invert it.")); | ||
258 | printf(" %s\n", _("The full path of the plugin must be provided.")); | 269 | printf(" %s\n", _("The full path of the plugin must be provided.")); |
259 | printf(" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL.")); | 270 | printf(" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL.")); |
260 | printf(" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK.")); | 271 | printf(" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK.")); |
261 | printf(" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); | 272 | printf(" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); |
262 | printf("\n"); | 273 | printf("\n"); |
263 | printf(" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a")); | 274 | printf(" %s\n", |
275 | _("Using timeout-result, it is possible to override the timeout behaviour or a")); | ||
264 | printf(" %s\n", _("plugin by setting the negate timeout a bit lower.")); | 276 | printf(" %s\n", _("plugin by setting the negate timeout a bit lower.")); |
265 | 277 | ||
266 | printf(UT_SUPPORT); | 278 | printf(UT_SUPPORT); |
diff --git a/plugins/netutils.c b/plugins/netutils.c index e2916c65..b4c6ff0a 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c | |||
@@ -30,13 +30,14 @@ | |||
30 | #include "common.h" | 30 | #include "common.h" |
31 | #include "output.h" | 31 | #include "output.h" |
32 | #include "states.h" | 32 | #include "states.h" |
33 | #include <sys/types.h> | ||
33 | #include "netutils.h" | 34 | #include "netutils.h" |
34 | 35 | ||
35 | unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; | 36 | unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; |
36 | unsigned int socket_timeout_state = STATE_CRITICAL; | 37 | mp_state_enum socket_timeout_state = STATE_CRITICAL; |
37 | 38 | mp_state_enum econn_refuse_state = STATE_CRITICAL; | |
38 | int econn_refuse_state = STATE_CRITICAL; | ||
39 | bool was_refused = false; | 39 | bool was_refused = false; |
40 | |||
40 | #if USE_IPV6 | 41 | #if USE_IPV6 |
41 | int address_family = AF_UNSPEC; | 42 | int address_family = AF_UNSPEC; |
42 | #else | 43 | #else |
@@ -63,38 +64,40 @@ void socket_timeout_alarm_handler(int sig) { | |||
63 | /* connects to a host on a specified tcp port, sends a string, and gets a | 64 | /* connects to a host on a specified tcp port, sends a string, and gets a |
64 | response. loops on select-recv until timeout or eof to get all of a | 65 | response. loops on select-recv until timeout or eof to get all of a |
65 | multi-packet answer */ | 66 | multi-packet answer */ |
66 | int process_tcp_request2(const char *server_address, int server_port, const char *send_buffer, char *recv_buffer, int recv_size) { | 67 | mp_state_enum process_tcp_request2(const char *server_address, const int server_port, |
68 | const char *send_buffer, char *recv_buffer, | ||
69 | const int recv_size) { | ||
67 | 70 | ||
68 | int result; | 71 | int socket; |
69 | int send_result; | ||
70 | int recv_result; | ||
71 | int sd; | ||
72 | struct timeval tv; | ||
73 | fd_set readfds; | ||
74 | int recv_length = 0; | ||
75 | 72 | ||
76 | result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); | 73 | mp_state_enum connect_result = |
77 | if (result != STATE_OK) { | 74 | np_net_connect(server_address, server_port, &socket, IPPROTO_TCP); |
75 | if (connect_result != STATE_OK) { | ||
78 | return STATE_CRITICAL; | 76 | return STATE_CRITICAL; |
79 | } | 77 | } |
80 | 78 | ||
81 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); | 79 | mp_state_enum result; |
80 | ssize_t send_result = send(socket, send_buffer, strlen(send_buffer), 0); | ||
82 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { | 81 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { |
83 | // printf("%s\n", _("Send failed")); | 82 | // printf("%s\n", _("Send failed")); |
84 | result = STATE_WARNING; | 83 | result = STATE_WARNING; |
85 | } | 84 | } |
86 | 85 | ||
87 | while (1) { | 86 | fd_set readfds; |
87 | ssize_t recv_length = 0; | ||
88 | while (true) { | ||
88 | /* wait up to the number of seconds for socket timeout | 89 | /* wait up to the number of seconds for socket timeout |
89 | minus one for data from the host */ | 90 | minus one for data from the host */ |
90 | tv.tv_sec = socket_timeout - 1; | 91 | struct timeval timeout = { |
91 | tv.tv_usec = 0; | 92 | .tv_sec = socket_timeout - 1, |
93 | .tv_usec = 0, | ||
94 | }; | ||
92 | FD_ZERO(&readfds); | 95 | FD_ZERO(&readfds); |
93 | FD_SET(sd, &readfds); | 96 | FD_SET(socket, &readfds); |
94 | select(sd + 1, &readfds, NULL, NULL, &tv); | 97 | select(socket + 1, &readfds, NULL, NULL, &timeout); |
95 | 98 | ||
96 | /* make sure some data has arrived */ | 99 | /* make sure some data has arrived */ |
97 | if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ | 100 | if (!FD_ISSET(socket, &readfds)) { /* it hasn't */ |
98 | if (!recv_length) { | 101 | if (!recv_length) { |
99 | strcpy(recv_buffer, ""); | 102 | strcpy(recv_buffer, ""); |
100 | // printf("%s\n", _("No data was received from host!")); | 103 | // printf("%s\n", _("No data was received from host!")); |
@@ -103,70 +106,69 @@ int process_tcp_request2(const char *server_address, int server_port, const char | |||
103 | recv_buffer[recv_length] = 0; | 106 | recv_buffer[recv_length] = 0; |
104 | } | 107 | } |
105 | break; | 108 | break; |
106 | } else { /* it has */ | 109 | } /* it has */ |
107 | recv_result = recv(sd, recv_buffer + recv_length, (size_t)recv_size - recv_length - 1, 0); | 110 | |
108 | if (recv_result == -1) { | 111 | ssize_t recv_result = |
109 | /* recv failed, bail out */ | 112 | recv(socket, recv_buffer + recv_length, (size_t)(recv_size - recv_length - 1), 0); |
110 | strcpy(recv_buffer + recv_length, ""); | 113 | if (recv_result == -1) { |
111 | result = STATE_WARNING; | 114 | /* recv failed, bail out */ |
112 | break; | 115 | strcpy(recv_buffer + recv_length, ""); |
113 | } else if (recv_result == 0) { | 116 | result = STATE_WARNING; |
114 | /* end of file ? */ | 117 | break; |
115 | recv_buffer[recv_length] = 0; | 118 | } |
116 | break; | 119 | |
117 | } else { /* we got data! */ | 120 | if (recv_result == 0) { |
118 | recv_length += recv_result; | 121 | /* end of file ? */ |
119 | if (recv_length >= recv_size - 1) { | 122 | recv_buffer[recv_length] = 0; |
120 | /* buffer full, we're done */ | 123 | break; |
121 | recv_buffer[recv_size - 1] = 0; | 124 | } |
122 | break; | 125 | |
123 | } | 126 | /* we got data! */ |
124 | } | 127 | recv_length += recv_result; |
128 | if (recv_length >= recv_size - 1) { | ||
129 | /* buffer full, we're done */ | ||
130 | recv_buffer[recv_size - 1] = 0; | ||
131 | break; | ||
125 | } | 132 | } |
126 | /* end if(!FD_ISSET(sd,&readfds)) */ | 133 | /* end if(!FD_ISSET(sd,&readfds)) */ |
127 | } | 134 | } |
128 | /* end while(1) */ | ||
129 | 135 | ||
130 | close(sd); | 136 | close(socket); |
131 | return result; | 137 | return result; |
132 | } | 138 | } |
133 | 139 | ||
134 | /* connects to a host on a specified port, sends a string, and gets a | 140 | /* connects to a host on a specified port, sends a string, and gets a |
135 | response */ | 141 | response */ |
136 | int process_request(const char *server_address, int server_port, int proto, const char *send_buffer, char *recv_buffer, int recv_size) { | 142 | mp_state_enum process_request(const char *server_address, const int server_port, const int proto, |
137 | int result; | 143 | const char *send_buffer, char *recv_buffer, const int recv_size) { |
138 | int sd; | ||
139 | 144 | ||
140 | result = STATE_OK; | 145 | mp_state_enum result = STATE_OK; |
141 | 146 | int socket; | |
142 | result = np_net_connect(server_address, server_port, &sd, proto); | 147 | result = np_net_connect(server_address, server_port, &socket, proto); |
143 | if (result != STATE_OK) { | 148 | if (result != STATE_OK) { |
144 | return STATE_CRITICAL; | 149 | return STATE_CRITICAL; |
145 | } | 150 | } |
146 | 151 | ||
147 | result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); | 152 | result = send_request(socket, proto, send_buffer, recv_buffer, recv_size); |
148 | 153 | ||
149 | close(sd); | 154 | close(socket); |
150 | 155 | ||
151 | return result; | 156 | return result; |
152 | } | 157 | } |
153 | 158 | ||
154 | /* opens a tcp or udp connection to a remote host or local socket */ | 159 | /* opens a tcp or udp connection to a remote host or local socket */ |
155 | int np_net_connect(const char *host_name, int port, int *sd, int proto) { | 160 | mp_state_enum np_net_connect(const char *host_name, int port, int *socketDescriptor, |
161 | const int proto) { | ||
156 | /* send back STATE_UNKOWN if there's an error | 162 | /* send back STATE_UNKOWN if there's an error |
157 | send back STATE_OK if we connect | 163 | send back STATE_OK if we connect |
158 | send back STATE_CRITICAL if we can't connect. | 164 | send back STATE_CRITICAL if we can't connect. |
159 | Let upstream figure out what to send to the user. */ | 165 | Let upstream figure out what to send to the user. */ |
160 | struct addrinfo hints; | 166 | bool is_socket = (host_name[0] == '/'); |
161 | struct addrinfo *r, *res; | 167 | int socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; |
162 | struct sockaddr_un su; | ||
163 | char port_str[6], host[MAX_HOST_ADDRESS_LENGTH]; | ||
164 | size_t len; | ||
165 | int socktype, result; | ||
166 | short is_socket = (host_name[0] == '/'); | ||
167 | |||
168 | socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; | ||
169 | 168 | ||
169 | struct addrinfo hints = {}; | ||
170 | struct addrinfo *res = NULL; | ||
171 | int result; | ||
170 | /* as long as it doesn't start with a '/', it's assumed a host or ip */ | 172 | /* as long as it doesn't start with a '/', it's assumed a host or ip */ |
171 | if (!is_socket) { | 173 | if (!is_socket) { |
172 | memset(&hints, 0, sizeof(hints)); | 174 | memset(&hints, 0, sizeof(hints)); |
@@ -174,38 +176,46 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
174 | hints.ai_protocol = proto; | 176 | hints.ai_protocol = proto; |
175 | hints.ai_socktype = socktype; | 177 | hints.ai_socktype = socktype; |
176 | 178 | ||
177 | len = strlen(host_name); | 179 | size_t len = strlen(host_name); |
178 | /* check for an [IPv6] address (and strip the brackets) */ | 180 | /* check for an [IPv6] address (and strip the brackets) */ |
179 | if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') { | 181 | if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') { |
180 | host_name++; | 182 | host_name++; |
181 | len -= 2; | 183 | len -= 2; |
182 | } | 184 | } |
185 | |||
186 | char host[MAX_HOST_ADDRESS_LENGTH]; | ||
187 | |||
183 | if (len >= sizeof(host)) { | 188 | if (len >= sizeof(host)) { |
184 | return STATE_UNKNOWN; | 189 | return STATE_UNKNOWN; |
185 | } | 190 | } |
191 | |||
186 | memcpy(host, host_name, len); | 192 | memcpy(host, host_name, len); |
187 | host[len] = '\0'; | 193 | host[len] = '\0'; |
194 | |||
195 | char port_str[6]; | ||
188 | snprintf(port_str, sizeof(port_str), "%d", port); | 196 | snprintf(port_str, sizeof(port_str), "%d", port); |
189 | result = getaddrinfo(host, port_str, &hints, &res); | 197 | int getaddrinfo_err = getaddrinfo(host, port_str, &hints, &res); |
190 | 198 | ||
191 | if (result != 0) { | 199 | if (getaddrinfo_err != 0) { |
192 | // printf("%s\n", gai_strerror(result)); | 200 | // printf("%s\n", gai_strerror(result)); |
193 | return STATE_UNKNOWN; | 201 | return STATE_UNKNOWN; |
194 | } | 202 | } |
195 | 203 | ||
196 | r = res; | 204 | struct addrinfo *addressPointer = res; |
197 | while (r) { | 205 | while (addressPointer) { |
198 | /* attempt to create a socket */ | 206 | /* attempt to create a socket */ |
199 | *sd = socket(r->ai_family, socktype, r->ai_protocol); | 207 | *socketDescriptor = |
208 | socket(addressPointer->ai_family, socktype, addressPointer->ai_protocol); | ||
200 | 209 | ||
201 | if (*sd < 0) { | 210 | if (*socketDescriptor < 0) { |
202 | // printf("%s\n", _("Socket creation failed")); | 211 | // printf("%s\n", _("Socket creation failed")); |
203 | freeaddrinfo(r); | 212 | freeaddrinfo(addressPointer); |
204 | return STATE_UNKNOWN; | 213 | return STATE_UNKNOWN; |
205 | } | 214 | } |
206 | 215 | ||
207 | /* attempt to open a connection */ | 216 | /* attempt to open a connection */ |
208 | result = connect(*sd, r->ai_addr, r->ai_addrlen); | 217 | result = |
218 | connect(*socketDescriptor, addressPointer->ai_addr, addressPointer->ai_addrlen); | ||
209 | 219 | ||
210 | if (result == 0) { | 220 | if (result == 0) { |
211 | was_refused = false; | 221 | was_refused = false; |
@@ -220,24 +230,28 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
220 | } | 230 | } |
221 | } | 231 | } |
222 | 232 | ||
223 | close(*sd); | 233 | close(*socketDescriptor); |
224 | r = r->ai_next; | 234 | addressPointer = addressPointer->ai_next; |
225 | } | 235 | } |
236 | |||
226 | freeaddrinfo(res); | 237 | freeaddrinfo(res); |
227 | } | 238 | |
228 | /* else the hostname is interpreted as a path to a unix socket */ | 239 | } else { |
229 | else { | 240 | /* else the hostname is interpreted as a path to a unix socket */ |
230 | if (strlen(host_name) >= UNIX_PATH_MAX) { | 241 | if (strlen(host_name) >= UNIX_PATH_MAX) { |
231 | die(STATE_UNKNOWN, _("Supplied path too long unix domain socket")); | 242 | die(STATE_UNKNOWN, _("Supplied path too long unix domain socket")); |
232 | } | 243 | } |
233 | memset(&su, 0, sizeof(su)); | 244 | |
245 | struct sockaddr_un su = {}; | ||
234 | su.sun_family = AF_UNIX; | 246 | su.sun_family = AF_UNIX; |
235 | strncpy(su.sun_path, host_name, UNIX_PATH_MAX); | 247 | strncpy(su.sun_path, host_name, UNIX_PATH_MAX); |
236 | *sd = socket(PF_UNIX, SOCK_STREAM, 0); | 248 | *socketDescriptor = socket(PF_UNIX, SOCK_STREAM, 0); |
237 | if (*sd < 0) { | 249 | |
250 | if (*socketDescriptor < 0) { | ||
238 | die(STATE_UNKNOWN, _("Socket creation failed")); | 251 | die(STATE_UNKNOWN, _("Socket creation failed")); |
239 | } | 252 | } |
240 | result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); | 253 | |
254 | result = connect(*socketDescriptor, (struct sockaddr *)&su, sizeof(su)); | ||
241 | if (result < 0 && errno == ECONNREFUSED) { | 255 | if (result < 0 && errno == ECONNREFUSED) { |
242 | was_refused = true; | 256 | was_refused = true; |
243 | } | 257 | } |
@@ -245,7 +259,9 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
245 | 259 | ||
246 | if (result == 0) { | 260 | if (result == 0) { |
247 | return STATE_OK; | 261 | return STATE_OK; |
248 | } else if (was_refused) { | 262 | } |
263 | |||
264 | if (was_refused) { | ||
249 | switch (econn_refuse_state) { /* a user-defined expected outcome */ | 265 | switch (econn_refuse_state) { /* a user-defined expected outcome */ |
250 | case STATE_OK: | 266 | case STATE_OK: |
251 | case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ | 267 | case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ |
@@ -253,7 +269,8 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
253 | if (is_socket) { | 269 | if (is_socket) { |
254 | // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); | 270 | // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); |
255 | } else { | 271 | } else { |
256 | // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); | 272 | // printf("connect to address %s and port %d: %s\n", host_name, port, |
273 | // strerror(errno)); | ||
257 | } | 274 | } |
258 | return STATE_CRITICAL; | 275 | return STATE_CRITICAL; |
259 | break; | 276 | break; |
@@ -271,14 +288,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
271 | } | 288 | } |
272 | } | 289 | } |
273 | 290 | ||
274 | int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size) { | 291 | mp_state_enum send_request(const int socket, const int proto, const char *send_buffer, |
275 | int result = STATE_OK; | 292 | char *recv_buffer, const int recv_size) { |
276 | int send_result; | 293 | mp_state_enum result = STATE_OK; |
277 | int recv_result; | ||
278 | struct timeval tv; | ||
279 | fd_set readfds; | ||
280 | 294 | ||
281 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); | 295 | ssize_t send_result = send(socket, send_buffer, strlen(send_buffer), 0); |
282 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { | 296 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { |
283 | // printf("%s\n", _("Send failed")); | 297 | // printf("%s\n", _("Send failed")); |
284 | result = STATE_WARNING; | 298 | result = STATE_WARNING; |
@@ -286,21 +300,22 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
286 | 300 | ||
287 | /* wait up to the number of seconds for socket timeout minus one | 301 | /* wait up to the number of seconds for socket timeout minus one |
288 | for data from the host */ | 302 | for data from the host */ |
289 | tv.tv_sec = socket_timeout - 1; | 303 | struct timeval timestamp = { |
290 | tv.tv_usec = 0; | 304 | .tv_sec = socket_timeout - 1, |
305 | .tv_usec = 0, | ||
306 | }; | ||
307 | fd_set readfds; | ||
291 | FD_ZERO(&readfds); | 308 | FD_ZERO(&readfds); |
292 | FD_SET(sd, &readfds); | 309 | FD_SET(socket, &readfds); |
293 | select(sd + 1, &readfds, NULL, NULL, &tv); | 310 | select(socket + 1, &readfds, NULL, NULL, ×tamp); |
294 | 311 | ||
295 | /* make sure some data has arrived */ | 312 | /* make sure some data has arrived */ |
296 | if (!FD_ISSET(sd, &readfds)) { | 313 | if (!FD_ISSET(socket, &readfds)) { |
297 | strcpy(recv_buffer, ""); | 314 | strcpy(recv_buffer, ""); |
298 | // printf("%s\n", _("No data was received from host!")); | 315 | // printf("%s\n", _("No data was received from host!")); |
299 | result = STATE_WARNING; | 316 | result = STATE_WARNING; |
300 | } | 317 | } else { |
301 | 318 | ssize_t recv_result = recv(socket, recv_buffer, (size_t)(recv_size - 1), 0); | |
302 | else { | ||
303 | recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); | ||
304 | if (recv_result == -1) { | 319 | if (recv_result == -1) { |
305 | strcpy(recv_buffer, ""); | 320 | strcpy(recv_buffer, ""); |
306 | if (proto != IPPROTO_TCP) { | 321 | if (proto != IPPROTO_TCP) { |
@@ -314,6 +329,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
314 | /* die returned string */ | 329 | /* die returned string */ |
315 | recv_buffer[recv_size - 1] = 0; | 330 | recv_buffer[recv_size - 1] = 0; |
316 | } | 331 | } |
332 | |||
317 | return result; | 333 | return result; |
318 | } | 334 | } |
319 | 335 | ||
@@ -335,27 +351,27 @@ bool is_addr(const char *address) { | |||
335 | #ifdef USE_IPV6 | 351 | #ifdef USE_IPV6 |
336 | if (address_family == AF_INET && is_inet_addr(address)) { | 352 | if (address_family == AF_INET && is_inet_addr(address)) { |
337 | return true; | 353 | return true; |
338 | } else if (address_family == AF_INET6 && is_inet6_addr(address)) { | 354 | } |
355 | |||
356 | if (address_family == AF_INET6 && is_inet6_addr(address)) { | ||
339 | return true; | 357 | return true; |
340 | } | 358 | } |
341 | #else | 359 | #else |
342 | if (is_inet_addr(address)) { | 360 | if (is_inet_addr(address)) { |
343 | return (true); | 361 | return true; |
344 | } | 362 | } |
345 | #endif | 363 | #endif |
346 | 364 | ||
347 | return (false); | 365 | return false; |
348 | } | 366 | } |
349 | 367 | ||
350 | int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { | 368 | bool dns_lookup(const char *node_string, struct sockaddr_storage *ss, const int family) { |
351 | struct addrinfo hints; | 369 | struct addrinfo hints; |
352 | struct addrinfo *res; | ||
353 | int retval; | ||
354 | |||
355 | memset(&hints, 0, sizeof(struct addrinfo)); | 370 | memset(&hints, 0, sizeof(struct addrinfo)); |
356 | hints.ai_family = family; | 371 | hints.ai_family = family; |
357 | 372 | ||
358 | retval = getaddrinfo(in, NULL, &hints, &res); | 373 | struct addrinfo *res; |
374 | int retval = getaddrinfo(node_string, NULL, &hints, &res); | ||
359 | if (retval != 0) { | 375 | if (retval != 0) { |
360 | return false; | 376 | return false; |
361 | } | 377 | } |
@@ -363,6 +379,8 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { | |||
363 | if (ss != NULL) { | 379 | if (ss != NULL) { |
364 | memcpy(ss, res->ai_addr, res->ai_addrlen); | 380 | memcpy(ss, res->ai_addr, res->ai_addrlen); |
365 | } | 381 | } |
382 | |||
366 | freeaddrinfo(res); | 383 | freeaddrinfo(res); |
384 | |||
367 | return true; | 385 | return true; |
368 | } | 386 | } |
diff --git a/plugins/netutils.h b/plugins/netutils.h index a95057e0..c4461113 100644 --- a/plugins/netutils.h +++ b/plugins/netutils.h | |||
@@ -1,120 +1,120 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring Plugins net utilities include file | 3 | * Monitoring Plugins net utilities include file |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) | 6 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) |
7 | * Copyright (c) 2003-2007 Monitoring Plugins Development Team | 7 | * Copyright (c) 2003-2007 Monitoring Plugins Development Team |
8 | * | 8 | * |
9 | * Description: | 9 | * Description: |
10 | * | 10 | * |
11 | * This file contains common include files and function definitions | 11 | * This file contains common include files and function definitions |
12 | * used in many of the plugins. | 12 | * used in many of the plugins. |
13 | * | 13 | * |
14 | * | 14 | * |
15 | * This program is free software: you can redistribute it and/or modify | 15 | * This program is free software: you can redistribute it and/or modify |
16 | * it under the terms of the GNU General Public License as published by | 16 | * it under the terms of the GNU General Public License as published by |
17 | * the Free Software Foundation, either version 3 of the License, or | 17 | * the Free Software Foundation, either version 3 of the License, or |
18 | * (at your option) any later version. | 18 | * (at your option) any later version. |
19 | * | 19 | * |
20 | * This program is distributed in the hope that it will be useful, | 20 | * This program is distributed in the hope that it will be useful, |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | * GNU General Public License for more details. | 23 | * GNU General Public License for more details. |
24 | * | 24 | * |
25 | * You should have received a copy of the GNU General Public License | 25 | * You should have received a copy of the GNU General Public License |
26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | * | 27 | * |
28 | * | 28 | * |
29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
30 | 30 | ||
31 | #ifndef _NETUTILS_H_ | 31 | #ifndef _NETUTILS_H_ |
32 | #define _NETUTILS_H_ | 32 | #define _NETUTILS_H_ |
33 | 33 | ||
34 | #include "common.h" | 34 | #include "output.h" |
35 | #include "states.h" | ||
35 | #include "utils.h" | 36 | #include "utils.h" |
36 | #include <netinet/in.h> | 37 | #include <netinet/in.h> |
37 | #include <arpa/inet.h> | 38 | #include <arpa/inet.h> |
38 | #include <netdb.h> | 39 | #include <netdb.h> |
39 | 40 | ||
40 | #ifdef HAVE_SYS_UN_H | 41 | #ifdef HAVE_SYS_UN_H |
41 | # include <sys/un.h> | 42 | # include <sys/un.h> |
42 | # ifndef UNIX_PATH_MAX | 43 | # ifndef UNIX_PATH_MAX |
43 | /* linux uses this, on sun it's hard-coded at 108 without a define, on BSD at 104 */ | 44 | /* linux uses this, on sun it's hard-coded at 108 without a define, on BSD at 104 */ |
44 | # define UNIX_PATH_MAX 104 | 45 | # define UNIX_PATH_MAX 104 |
45 | # endif /* UNIX_PATH_MAX */ | 46 | # endif /* UNIX_PATH_MAX */ |
46 | #endif /* HAVE_SYS_UN_H */ | 47 | #endif /* HAVE_SYS_UN_H */ |
47 | 48 | ||
48 | #ifndef HOST_MAX_BYTES | 49 | #ifndef HOST_MAX_BYTES |
49 | # define HOST_MAX_BYTES 255 | 50 | # define HOST_MAX_BYTES 255 |
50 | #endif | 51 | #endif |
51 | 52 | ||
52 | /* process_request and wrapper macros */ | 53 | /* process_request and wrapper macros */ |
53 | #define process_tcp_request(addr, port, sbuf, rbuf, rsize) \ | 54 | #define process_tcp_request(addr, port, sbuf, rbuf, rsize) \ |
54 | process_request(addr, port, IPPROTO_TCP, sbuf, rbuf, rsize) | 55 | process_request(addr, port, IPPROTO_TCP, sbuf, rbuf, rsize) |
55 | #define process_udp_request(addr, port, sbuf, rbuf, rsize) \ | 56 | #define process_udp_request(addr, port, sbuf, rbuf, rsize) \ |
56 | process_request(addr, port, IPPROTO_UDP, sbuf, rbuf, rsize) | 57 | process_request(addr, port, IPPROTO_UDP, sbuf, rbuf, rsize) |
57 | int process_tcp_request2 (const char *address, int port, | 58 | mp_state_enum process_tcp_request2(const char *server_address, int server_port, |
58 | const char *sbuffer, char *rbuffer, int rsize); | 59 | const char *send_buffer, char *recv_buffer, int recv_size); |
59 | int process_request (const char *address, int port, int proto, | 60 | mp_state_enum process_request(const char *server_address, int server_port, int proto, |
60 | const char *sbuffer, char *rbuffer, int rsize); | 61 | const char *send_buffer, char *recv_buffer, int recv_size); |
61 | 62 | ||
62 | /* my_connect and wrapper macros */ | 63 | /* my_connect and wrapper macros */ |
63 | #define my_tcp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_TCP) | 64 | #define my_tcp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_TCP) |
64 | #define my_udp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_UDP) | 65 | #define my_udp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_UDP) |
65 | int np_net_connect(const char *address, int port, int *sd, int proto); | 66 | mp_state_enum np_net_connect(const char *host_name, int port, int *socketDescriptor, int proto); |
66 | 67 | ||
67 | /* send_request and wrapper macros */ | 68 | /* send_request and wrapper macros */ |
68 | #define send_tcp_request(s, sbuf, rbuf, rsize) \ | 69 | #define send_tcp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_TCP, sbuf, rbuf, rsize) |
69 | send_request(s, IPPROTO_TCP, sbuf, rbuf, rsize) | 70 | #define send_udp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_UDP, sbuf, rbuf, rsize) |
70 | #define send_udp_request(s, sbuf, rbuf, rsize) \ | 71 | mp_state_enum send_request(int socket, int proto, const char *send_buffer, char *recv_buffer, |
71 | send_request(s, IPPROTO_UDP, sbuf, rbuf, rsize) | 72 | int recv_size); |
72 | int send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size); | ||
73 | |||
74 | 73 | ||
75 | /* "is_*" wrapper macros and functions */ | 74 | /* "is_*" wrapper macros and functions */ |
76 | bool is_host (const char *); | 75 | bool is_host(const char *); |
77 | bool is_addr (const char *); | 76 | bool is_addr(const char *); |
78 | int dns_lookup (const char *, struct sockaddr_storage *, int); | 77 | bool dns_lookup(const char *, struct sockaddr_storage *, int); |
79 | void host_or_die(const char *str); | 78 | void host_or_die(const char *str); |
80 | #define resolve_host_or_addr(addr, family) dns_lookup(addr, NULL, family) | 79 | #define resolve_host_or_addr(addr, family) dns_lookup(addr, NULL, family) |
81 | #define is_inet_addr(addr) resolve_host_or_addr(addr, AF_INET) | 80 | #define is_inet_addr(addr) resolve_host_or_addr(addr, AF_INET) |
82 | #ifdef USE_IPV6 | 81 | #ifdef USE_IPV6 |
83 | # define is_inet6_addr(addr) resolve_host_or_addr(addr, AF_INET6) | 82 | # define is_inet6_addr(addr) resolve_host_or_addr(addr, AF_INET6) |
84 | # define is_hostname(addr) resolve_host_or_addr(addr, address_family) | 83 | # define is_hostname(addr) resolve_host_or_addr(addr, address_family) |
85 | #else | 84 | #else |
86 | # define is_hostname(addr) resolve_host_or_addr(addr, AF_INET) | 85 | # define is_hostname(addr) resolve_host_or_addr(addr, AF_INET) |
87 | #endif | 86 | #endif |
88 | 87 | ||
89 | extern unsigned int socket_timeout; | 88 | extern unsigned int socket_timeout; |
90 | extern unsigned int socket_timeout_state; | 89 | extern mp_state_enum socket_timeout_state; |
91 | extern int econn_refuse_state; | 90 | extern mp_state_enum econn_refuse_state; |
92 | extern bool was_refused; | 91 | extern bool was_refused; |
93 | extern int address_family; | 92 | extern int address_family; |
94 | 93 | ||
95 | void socket_timeout_alarm_handler (int) __attribute__((noreturn)); | 94 | void socket_timeout_alarm_handler(int) __attribute__((noreturn)); |
96 | 95 | ||
97 | /* SSL-Related functionality */ | 96 | /* SSL-Related functionality */ |
98 | #ifdef HAVE_SSL | 97 | #ifdef HAVE_SSL |
99 | # define MP_SSLv2 1 | 98 | # define MP_SSLv2 1 |
100 | # define MP_SSLv3 2 | 99 | # define MP_SSLv3 2 |
101 | # define MP_TLSv1 3 | 100 | # define MP_TLSv1 3 |
102 | # define MP_TLSv1_1 4 | 101 | # define MP_TLSv1_1 4 |
103 | # define MP_TLSv1_2 5 | 102 | # define MP_TLSv1_2 5 |
104 | # define MP_SSLv2_OR_NEWER 6 | 103 | # define MP_SSLv2_OR_NEWER 6 |
105 | # define MP_SSLv3_OR_NEWER 7 | 104 | # define MP_SSLv3_OR_NEWER 7 |
106 | # define MP_TLSv1_OR_NEWER 8 | 105 | # define MP_TLSv1_OR_NEWER 8 |
107 | # define MP_TLSv1_1_OR_NEWER 9 | 106 | # define MP_TLSv1_1_OR_NEWER 9 |
108 | # define MP_TLSv1_2_OR_NEWER 10 | 107 | # define MP_TLSv1_2_OR_NEWER 10 |
109 | /* maybe this could be merged with the above np_net_connect, via some flags */ | 108 | /* maybe this could be merged with the above np_net_connect, via some flags */ |
110 | int np_net_ssl_init(int sd); | 109 | int np_net_ssl_init(int socket); |
111 | int np_net_ssl_init_with_hostname(int sd, char *host_name); | 110 | int np_net_ssl_init_with_hostname(int socket, char *host_name); |
112 | int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version); | 111 | int np_net_ssl_init_with_hostname_and_version(int socket, char *host_name, int version); |
113 | int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, char *privkey); | 112 | int np_net_ssl_init_with_hostname_version_and_cert(int socket, char *host_name, int version, |
114 | void np_net_ssl_cleanup(); | 113 | char *cert, char *privkey); |
114 | void np_net_ssl_cleanup(void); | ||
115 | int np_net_ssl_write(const void *buf, int num); | 115 | int np_net_ssl_write(const void *buf, int num); |
116 | int np_net_ssl_read(void *buf, int num); | 116 | int np_net_ssl_read(void *buf, int num); |
117 | int np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit); | 117 | mp_state_enum np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit); |
118 | mp_subcheck mp_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit); | ||
118 | #endif /* HAVE_SSL */ | 119 | #endif /* HAVE_SSL */ |
119 | |||
120 | #endif /* _NETUTILS_H_ */ | 120 | #endif /* _NETUTILS_H_ */ |
diff --git a/plugins/picohttpparser/picohttpparser.c b/plugins/picohttpparser/picohttpparser.c index 2ae92d66..e87388b0 100644 --- a/plugins/picohttpparser/picohttpparser.c +++ b/plugins/picohttpparser/picohttpparser.c | |||
@@ -50,59 +50,61 @@ | |||
50 | # define ALIGNED(n) __attribute__((aligned(n))) | 50 | # define ALIGNED(n) __attribute__((aligned(n))) |
51 | #endif | 51 | #endif |
52 | 52 | ||
53 | #define IS_PRINTABLE_ASCII(c) ((unsigned char)(c)-040u < 0137u) | 53 | #define IS_PRINTABLE_ASCII(c) ((unsigned char)(c) - 040u < 0137u) |
54 | 54 | ||
55 | #define CHECK_EOF() \ | 55 | #define CHECK_EOF() \ |
56 | if (buf == buf_end) { \ | 56 | if (buf == buf_end) { \ |
57 | *ret = -2; \ | 57 | *ret = -2; \ |
58 | return NULL; \ | 58 | return NULL; \ |
59 | } | 59 | } |
60 | 60 | ||
61 | #define EXPECT_CHAR_NO_CHECK(ch) \ | 61 | #define EXPECT_CHAR_NO_CHECK(ch) \ |
62 | if (*buf++ != ch) { \ | 62 | if (*buf++ != ch) { \ |
63 | *ret = -1; \ | 63 | *ret = -1; \ |
64 | return NULL; \ | 64 | return NULL; \ |
65 | } | 65 | } |
66 | 66 | ||
67 | #define EXPECT_CHAR(ch) \ | 67 | #define EXPECT_CHAR(ch) \ |
68 | CHECK_EOF(); \ | 68 | CHECK_EOF(); \ |
69 | EXPECT_CHAR_NO_CHECK(ch); | 69 | EXPECT_CHAR_NO_CHECK(ch); |
70 | 70 | ||
71 | #define ADVANCE_TOKEN(tok, toklen) \ | 71 | #define ADVANCE_TOKEN(tok, toklen) \ |
72 | do { \ | 72 | do { \ |
73 | const char *tok_start = buf; \ | 73 | const char *tok_start = buf; \ |
74 | static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \ | 74 | static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \ |
75 | int found2; \ | 75 | int found2; \ |
76 | buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \ | 76 | buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \ |
77 | if (!found2) { \ | 77 | if (!found2) { \ |
78 | CHECK_EOF(); \ | 78 | CHECK_EOF(); \ |
79 | } \ | 79 | } \ |
80 | while (1) { \ | 80 | while (1) { \ |
81 | if (*buf == ' ') { \ | 81 | if (*buf == ' ') { \ |
82 | break; \ | 82 | break; \ |
83 | } else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \ | 83 | } else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \ |
84 | if ((unsigned char)*buf < '\040' || *buf == '\177') { \ | 84 | if ((unsigned char)*buf < '\040' || *buf == '\177') { \ |
85 | *ret = -1; \ | 85 | *ret = -1; \ |
86 | return NULL; \ | 86 | return NULL; \ |
87 | } \ | 87 | } \ |
88 | } \ | 88 | } \ |
89 | ++buf; \ | 89 | ++buf; \ |
90 | CHECK_EOF(); \ | 90 | CHECK_EOF(); \ |
91 | } \ | 91 | } \ |
92 | tok = tok_start; \ | 92 | tok = tok_start; \ |
93 | toklen = buf - tok_start; \ | 93 | toklen = buf - tok_start; \ |
94 | } while (0) | 94 | } while (0) |
95 | 95 | ||
96 | static const char *token_char_map = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" | 96 | static const char *token_char_map = |
97 | "\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0" | 97 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
98 | "\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1" | 98 | "\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0" |
99 | "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0" | 99 | "\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1" |
100 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" | 100 | "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0" |
101 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" | 101 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
102 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" | 102 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
103 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; | 103 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
104 | 104 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; | |
105 | static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found) { | 105 | |
106 | static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, | ||
107 | size_t ranges_size, int *found) { | ||
106 | *found = 0; | 108 | *found = 0; |
107 | #if __SSE4_2__ | 109 | #if __SSE4_2__ |
108 | if (likely(buf_end - buf >= 16)) { | 110 | if (likely(buf_end - buf >= 16)) { |
@@ -111,7 +113,8 @@ static const char *findchar_fast(const char *buf, const char *buf_end, const cha | |||
111 | size_t left = (buf_end - buf) & ~15; | 113 | size_t left = (buf_end - buf) & ~15; |
112 | do { | 114 | do { |
113 | __m128i b16 = _mm_loadu_si128((const __m128i *)buf); | 115 | __m128i b16 = _mm_loadu_si128((const __m128i *)buf); |
114 | int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS); | 116 | int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, |
117 | _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS); | ||
115 | if (unlikely(r != 16)) { | 118 | if (unlikely(r != 16)) { |
116 | buf += r; | 119 | buf += r; |
117 | *found = 1; | 120 | *found = 1; |
@@ -130,25 +133,29 @@ static const char *findchar_fast(const char *buf, const char *buf_end, const cha | |||
130 | return buf; | 133 | return buf; |
131 | } | 134 | } |
132 | 135 | ||
133 | static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, size_t *token_len, int *ret) { | 136 | static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, |
137 | size_t *token_len, int *ret) { | ||
134 | const char *token_start = buf; | 138 | const char *token_start = buf; |
135 | 139 | ||
136 | #ifdef __SSE4_2__ | 140 | #ifdef __SSE4_2__ |
137 | static const char ALIGNED(16) ranges1[16] = "\0\010" /* allow HT */ | 141 | static const char ALIGNED(16) ranges1[16] = |
138 | "\012\037" /* allow SP and up to but not including DEL */ | 142 | "\0\010" /* allow HT */ |
139 | "\177\177"; /* allow chars w. MSB set */ | 143 | "\012\037" /* allow SP and up to but not including DEL */ |
144 | "\177\177"; /* allow chars w. MSB set */ | ||
140 | int found; | 145 | int found; |
141 | buf = findchar_fast(buf, buf_end, ranges1, 6, &found); | 146 | buf = findchar_fast(buf, buf_end, ranges1, 6, &found); |
142 | if (found) | 147 | if (found) { |
143 | goto FOUND_CTL; | 148 | goto FOUND_CTL; |
149 | } | ||
144 | #else | 150 | #else |
145 | /* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */ | 151 | /* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined |
152 | */ | ||
146 | while (likely(buf_end - buf >= 8)) { | 153 | while (likely(buf_end - buf >= 8)) { |
147 | # define DOIT() \ | 154 | # define DOIT() \ |
148 | do { \ | 155 | do { \ |
149 | if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \ | 156 | if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \ |
150 | goto NonPrintable; \ | 157 | goto NonPrintable; \ |
151 | ++buf; \ | 158 | ++buf; \ |
152 | } while (0) | 159 | } while (0) |
153 | DOIT(); | 160 | DOIT(); |
154 | DOIT(); | 161 | DOIT(); |
@@ -161,7 +168,8 @@ static const char *get_token_to_eol(const char *buf, const char *buf_end, const | |||
161 | # undef DOIT | 168 | # undef DOIT |
162 | continue; | 169 | continue; |
163 | NonPrintable: | 170 | NonPrintable: |
164 | if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { | 171 | if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || |
172 | unlikely(*buf == '\177')) { | ||
165 | goto FOUND_CTL; | 173 | goto FOUND_CTL; |
166 | } | 174 | } |
167 | ++buf; | 175 | ++buf; |
@@ -170,7 +178,8 @@ static const char *get_token_to_eol(const char *buf, const char *buf_end, const | |||
170 | for (;; ++buf) { | 178 | for (;; ++buf) { |
171 | CHECK_EOF(); | 179 | CHECK_EOF(); |
172 | if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { | 180 | if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { |
173 | if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { | 181 | if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || |
182 | unlikely(*buf == '\177')) { | ||
174 | goto FOUND_CTL; | 183 | goto FOUND_CTL; |
175 | } | 184 | } |
176 | } | 185 | } |
@@ -219,27 +228,28 @@ static const char *is_complete(const char *buf, const char *buf_end, size_t last | |||
219 | return NULL; | 228 | return NULL; |
220 | } | 229 | } |
221 | 230 | ||
222 | #define PARSE_INT(valp_, mul_) \ | 231 | #define PARSE_INT(valp_, mul_) \ |
223 | if (*buf < '0' || '9' < *buf) { \ | 232 | if (*buf < '0' || '9' < *buf) { \ |
224 | buf++; \ | 233 | buf++; \ |
225 | *ret = -1; \ | 234 | *ret = -1; \ |
226 | return NULL; \ | 235 | return NULL; \ |
227 | } \ | 236 | } \ |
228 | *(valp_) = (mul_) * (*buf++ - '0'); | 237 | *(valp_) = (mul_) * (*buf++ - '0'); |
229 | 238 | ||
230 | #define PARSE_INT_3(valp_) \ | 239 | #define PARSE_INT_3(valp_) \ |
231 | do { \ | 240 | do { \ |
232 | int res_ = 0; \ | 241 | int res_ = 0; \ |
233 | PARSE_INT(&res_, 100) \ | 242 | PARSE_INT(&res_, 100) \ |
234 | *valp_ = res_; \ | 243 | *valp_ = res_; \ |
235 | PARSE_INT(&res_, 10) \ | 244 | PARSE_INT(&res_, 10) \ |
236 | *valp_ += res_; \ | 245 | *valp_ += res_; \ |
237 | PARSE_INT(&res_, 1) \ | 246 | PARSE_INT(&res_, 1) \ |
238 | *valp_ += res_; \ | 247 | *valp_ += res_; \ |
239 | } while (0) | 248 | } while (0) |
240 | 249 | ||
241 | /* returned pointer is always within [buf, buf_end), or null */ | 250 | /* returned pointer is always within [buf, buf_end), or null */ |
242 | static const char *parse_http_version(const char *buf, const char *buf_end, int *major_version, int *minor_version, int *ret) { | 251 | static const char *parse_http_version(const char *buf, const char *buf_end, int *major_version, |
252 | int *minor_version, int *ret) { | ||
243 | /* we want at least [HTTP/1.<two chars>] to try to parse */ | 253 | /* we want at least [HTTP/1.<two chars>] to try to parse */ |
244 | if (buf_end - buf < 9) { | 254 | if (buf_end - buf < 9) { |
245 | *ret = -2; | 255 | *ret = -2; |
@@ -260,8 +270,8 @@ static const char *parse_http_version(const char *buf, const char *buf_end, int | |||
260 | return buf; | 270 | return buf; |
261 | } | 271 | } |
262 | 272 | ||
263 | static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers, size_t max_headers, | 273 | static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, |
264 | int *ret) { | 274 | size_t *num_headers, size_t max_headers, int *ret) { |
265 | for (;; ++*num_headers) { | 275 | for (;; ++*num_headers) { |
266 | CHECK_EOF(); | 276 | CHECK_EOF(); |
267 | if (*buf == '\015') { | 277 | if (*buf == '\015') { |
@@ -337,9 +347,10 @@ static const char *parse_headers(const char *buf, const char *buf_end, struct ph | |||
337 | return buf; | 347 | return buf; |
338 | } | 348 | } |
339 | 349 | ||
340 | static const char *parse_request(const char *buf, const char *buf_end, const char **method, size_t *method_len, const char **path, | 350 | static const char *parse_request(const char *buf, const char *buf_end, const char **method, |
341 | size_t *path_len, int *major_version, int *minor_version, struct phr_header *headers, size_t *num_headers, | 351 | size_t *method_len, const char **path, size_t *path_len, |
342 | size_t max_headers, int *ret) { | 352 | int *major_version, int *minor_version, struct phr_header *headers, |
353 | size_t *num_headers, size_t max_headers, int *ret) { | ||
343 | /* skip first empty line (some clients add CRLF after POST content) */ | 354 | /* skip first empty line (some clients add CRLF after POST content) */ |
344 | CHECK_EOF(); | 355 | CHECK_EOF(); |
345 | if (*buf == '\015') { | 356 | if (*buf == '\015') { |
@@ -378,8 +389,9 @@ static const char *parse_request(const char *buf, const char *buf_end, const cha | |||
378 | return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); | 389 | return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); |
379 | } | 390 | } |
380 | 391 | ||
381 | int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len, | 392 | int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, |
382 | int *major_version, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len) { | 393 | const char **path, size_t *path_len, int *major_version, int *minor_version, |
394 | struct phr_header *headers, size_t *num_headers, size_t last_len) { | ||
383 | const char *buf = buf_start, *buf_end = buf_start + len; | 395 | const char *buf = buf_start, *buf_end = buf_start + len; |
384 | size_t max_headers = *num_headers; | 396 | size_t max_headers = *num_headers; |
385 | int r; | 397 | int r; |
@@ -398,17 +410,18 @@ int phr_parse_request(const char *buf_start, size_t len, const char **method, si | |||
398 | return r; | 410 | return r; |
399 | } | 411 | } |
400 | 412 | ||
401 | if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, major_version, minor_version, headers, num_headers, | 413 | if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, major_version, |
402 | max_headers, &r)) == NULL) { | 414 | minor_version, headers, num_headers, max_headers, &r)) == NULL) { |
403 | return r; | 415 | return r; |
404 | } | 416 | } |
405 | 417 | ||
406 | return (int)(buf - buf_start); | 418 | return (int)(buf - buf_start); |
407 | } | 419 | } |
408 | 420 | ||
409 | static const char *parse_response(const char *buf, const char *buf_end, int *major_version, int *minor_version, int *status, | 421 | static const char *parse_response(const char *buf, const char *buf_end, int *major_version, |
410 | const char **msg, size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, | 422 | int *minor_version, int *status, const char **msg, |
411 | int *ret) { | 423 | size_t *msg_len, struct phr_header *headers, size_t *num_headers, |
424 | size_t max_headers, int *ret) { | ||
412 | /* parse "HTTP/1.x" */ | 425 | /* parse "HTTP/1.x" */ |
413 | if ((buf = parse_http_version(buf, buf_end, major_version, minor_version, ret)) == NULL) { | 426 | if ((buf = parse_http_version(buf, buf_end, major_version, minor_version, ret)) == NULL) { |
414 | return NULL; | 427 | return NULL; |
@@ -421,7 +434,8 @@ static const char *parse_response(const char *buf, const char *buf_end, int *maj | |||
421 | do { | 434 | do { |
422 | ++buf; | 435 | ++buf; |
423 | } while (*buf == ' '); | 436 | } while (*buf == ' '); |
424 | /* parse status code, we want at least [:digit:][:digit:][:digit:]<other char> to try to parse */ | 437 | /* parse status code, we want at least [:digit:][:digit:][:digit:]<other char> to try to parse |
438 | */ | ||
425 | if (buf_end - buf < 4) { | 439 | if (buf_end - buf < 4) { |
426 | *ret = -2; | 440 | *ret = -2; |
427 | return NULL; | 441 | return NULL; |
@@ -449,8 +463,9 @@ static const char *parse_response(const char *buf, const char *buf_end, int *maj | |||
449 | return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); | 463 | return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); |
450 | } | 464 | } |
451 | 465 | ||
452 | int phr_parse_response(const char *buf_start, size_t len, int *major_version, int *minor_version, int *status, const char **msg, | 466 | int phr_parse_response(const char *buf_start, size_t len, int *major_version, int *minor_version, |
453 | size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t last_len) { | 467 | int *status, const char **msg, size_t *msg_len, struct phr_header *headers, |
468 | size_t *num_headers, size_t last_len) { | ||
454 | const char *buf = buf_start, *buf_end = buf + len; | 469 | const char *buf = buf_start, *buf_end = buf + len; |
455 | size_t max_headers = *num_headers; | 470 | size_t max_headers = *num_headers; |
456 | int r; | 471 | int r; |
@@ -468,15 +483,16 @@ int phr_parse_response(const char *buf_start, size_t len, int *major_version, in | |||
468 | return r; | 483 | return r; |
469 | } | 484 | } |
470 | 485 | ||
471 | if ((buf = parse_response(buf, buf_end, major_version, minor_version, status, msg, msg_len, headers, num_headers, max_headers, &r)) == | 486 | if ((buf = parse_response(buf, buf_end, major_version, minor_version, status, msg, msg_len, |
472 | NULL) { | 487 | headers, num_headers, max_headers, &r)) == NULL) { |
473 | return r; | 488 | return r; |
474 | } | 489 | } |
475 | 490 | ||
476 | return (int)(buf - buf_start); | 491 | return (int)(buf - buf_start); |
477 | } | 492 | } |
478 | 493 | ||
479 | int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len) { | 494 | int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, |
495 | size_t *num_headers, size_t last_len) { | ||
480 | const char *buf = buf_start, *buf_end = buf + len; | 496 | const char *buf = buf_start, *buf_end = buf + len; |
481 | size_t max_headers = *num_headers; | 497 | size_t max_headers = *num_headers; |
482 | int r; | 498 | int r; |
@@ -526,8 +542,9 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ | |||
526 | case CHUNKED_IN_CHUNK_SIZE: | 542 | case CHUNKED_IN_CHUNK_SIZE: |
527 | for (;; ++src) { | 543 | for (;; ++src) { |
528 | int v; | 544 | int v; |
529 | if (src == bufsz) | 545 | if (src == bufsz) { |
530 | goto Exit; | 546 | goto Exit; |
547 | } | ||
531 | if ((v = decode_hex(buf[src])) == -1) { | 548 | if ((v = decode_hex(buf[src])) == -1) { |
532 | if (decoder->_hex_count == 0) { | 549 | if (decoder->_hex_count == 0) { |
533 | ret = -1; | 550 | ret = -1; |
@@ -548,10 +565,12 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ | |||
548 | case CHUNKED_IN_CHUNK_EXT: | 565 | case CHUNKED_IN_CHUNK_EXT: |
549 | /* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */ | 566 | /* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */ |
550 | for (;; ++src) { | 567 | for (;; ++src) { |
551 | if (src == bufsz) | 568 | if (src == bufsz) { |
552 | goto Exit; | 569 | goto Exit; |
553 | if (buf[src] == '\012') | 570 | } |
571 | if (buf[src] == '\012') { | ||
554 | break; | 572 | break; |
573 | } | ||
555 | } | 574 | } |
556 | ++src; | 575 | ++src; |
557 | if (decoder->bytes_left_in_chunk == 0) { | 576 | if (decoder->bytes_left_in_chunk == 0) { |
@@ -567,15 +586,17 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ | |||
567 | case CHUNKED_IN_CHUNK_DATA: { | 586 | case CHUNKED_IN_CHUNK_DATA: { |
568 | size_t avail = bufsz - src; | 587 | size_t avail = bufsz - src; |
569 | if (avail < decoder->bytes_left_in_chunk) { | 588 | if (avail < decoder->bytes_left_in_chunk) { |
570 | if (dst != src) | 589 | if (dst != src) { |
571 | memmove(buf + dst, buf + src, avail); | 590 | memmove(buf + dst, buf + src, avail); |
591 | } | ||
572 | src += avail; | 592 | src += avail; |
573 | dst += avail; | 593 | dst += avail; |
574 | decoder->bytes_left_in_chunk -= avail; | 594 | decoder->bytes_left_in_chunk -= avail; |
575 | goto Exit; | 595 | goto Exit; |
576 | } | 596 | } |
577 | if (dst != src) | 597 | if (dst != src) { |
578 | memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk); | 598 | memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk); |
599 | } | ||
579 | src += decoder->bytes_left_in_chunk; | 600 | src += decoder->bytes_left_in_chunk; |
580 | dst += decoder->bytes_left_in_chunk; | 601 | dst += decoder->bytes_left_in_chunk; |
581 | decoder->bytes_left_in_chunk = 0; | 602 | decoder->bytes_left_in_chunk = 0; |
@@ -584,10 +605,12 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ | |||
584 | /* fallthru */ | 605 | /* fallthru */ |
585 | case CHUNKED_IN_CHUNK_CRLF: | 606 | case CHUNKED_IN_CHUNK_CRLF: |
586 | for (;; ++src) { | 607 | for (;; ++src) { |
587 | if (src == bufsz) | 608 | if (src == bufsz) { |
588 | goto Exit; | 609 | goto Exit; |
589 | if (buf[src] != '\015') | 610 | } |
611 | if (buf[src] != '\015') { | ||
590 | break; | 612 | break; |
613 | } | ||
591 | } | 614 | } |
592 | if (buf[src] != '\012') { | 615 | if (buf[src] != '\012') { |
593 | ret = -1; | 616 | ret = -1; |
@@ -598,21 +621,26 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ | |||
598 | break; | 621 | break; |
599 | case CHUNKED_IN_TRAILERS_LINE_HEAD: | 622 | case CHUNKED_IN_TRAILERS_LINE_HEAD: |
600 | for (;; ++src) { | 623 | for (;; ++src) { |
601 | if (src == bufsz) | 624 | if (src == bufsz) { |
602 | goto Exit; | 625 | goto Exit; |
603 | if (buf[src] != '\015') | 626 | } |
627 | if (buf[src] != '\015') { | ||
604 | break; | 628 | break; |
629 | } | ||
605 | } | 630 | } |
606 | if (buf[src++] == '\012') | 631 | if (buf[src++] == '\012') { |
607 | goto Complete; | 632 | goto Complete; |
633 | } | ||
608 | decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE; | 634 | decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE; |
609 | /* fallthru */ | 635 | /* fallthru */ |
610 | case CHUNKED_IN_TRAILERS_LINE_MIDDLE: | 636 | case CHUNKED_IN_TRAILERS_LINE_MIDDLE: |
611 | for (;; ++src) { | 637 | for (;; ++src) { |
612 | if (src == bufsz) | 638 | if (src == bufsz) { |
613 | goto Exit; | 639 | goto Exit; |
614 | if (buf[src] == '\012') | 640 | } |
641 | if (buf[src] == '\012') { | ||
615 | break; | 642 | break; |
643 | } | ||
616 | } | 644 | } |
617 | ++src; | 645 | ++src; |
618 | decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD; | 646 | decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD; |
@@ -625,13 +653,16 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ | |||
625 | Complete: | 653 | Complete: |
626 | ret = bufsz - src; | 654 | ret = bufsz - src; |
627 | Exit: | 655 | Exit: |
628 | if (dst != src) | 656 | if (dst != src) { |
629 | memmove(buf + dst, buf + src, bufsz - src); | 657 | memmove(buf + dst, buf + src, bufsz - src); |
658 | } | ||
630 | *_bufsz = dst; | 659 | *_bufsz = dst; |
631 | return ret; | 660 | return ret; |
632 | } | 661 | } |
633 | 662 | ||
634 | int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder) { return decoder->_state == CHUNKED_IN_CHUNK_DATA; } | 663 | int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder) { |
664 | return decoder->_state == CHUNKED_IN_CHUNK_DATA; | ||
665 | } | ||
635 | 666 | ||
636 | #undef CHECK_EOF | 667 | #undef CHECK_EOF |
637 | #undef EXPECT_CHAR | 668 | #undef EXPECT_CHAR |
diff --git a/plugins/picohttpparser/picohttpparser.h b/plugins/picohttpparser/picohttpparser.h index 8f13b36f..054c2812 100644 --- a/plugins/picohttpparser/picohttpparser.h +++ b/plugins/picohttpparser/picohttpparser.h | |||
@@ -30,7 +30,7 @@ | |||
30 | #include <sys/types.h> | 30 | #include <sys/types.h> |
31 | 31 | ||
32 | #ifdef _MSC_VER | 32 | #ifdef _MSC_VER |
33 | #define ssize_t intptr_t | 33 | # define ssize_t intptr_t |
34 | #endif | 34 | #endif |
35 | 35 | ||
36 | #ifdef __cplusplus | 36 | #ifdef __cplusplus |
@@ -40,30 +40,33 @@ extern "C" { | |||
40 | /* contains name and value of a header (name == NULL if is a continuing line | 40 | /* contains name and value of a header (name == NULL if is a continuing line |
41 | * of a multiline header */ | 41 | * of a multiline header */ |
42 | struct phr_header { | 42 | struct phr_header { |
43 | const char *name; | 43 | const char *name; |
44 | size_t name_len; | 44 | size_t name_len; |
45 | const char *value; | 45 | const char *value; |
46 | size_t value_len; | 46 | size_t value_len; |
47 | }; | 47 | }; |
48 | 48 | ||
49 | /* returns number of bytes consumed if successful, -2 if request is partial, | 49 | /* returns number of bytes consumed if successful, -2 if request is partial, |
50 | * -1 if failed */ | 50 | * -1 if failed */ |
51 | int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len, | 51 | int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, |
52 | int *major_version, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len); | 52 | const char **path, size_t *path_len, int *major_version, int *minor_version, |
53 | struct phr_header *headers, size_t *num_headers, size_t last_len); | ||
53 | 54 | ||
54 | /* ditto */ | 55 | /* ditto */ |
55 | int phr_parse_response(const char *_buf, size_t len, int *major_version, int *minor_version, int *status, const char **msg, size_t *msg_len, | 56 | int phr_parse_response(const char *_buf, size_t len, int *major_version, int *minor_version, |
56 | struct phr_header *headers, size_t *num_headers, size_t last_len); | 57 | int *status, const char **msg, size_t *msg_len, struct phr_header *headers, |
58 | size_t *num_headers, size_t last_len); | ||
57 | 59 | ||
58 | /* ditto */ | 60 | /* ditto */ |
59 | int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len); | 61 | int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, |
62 | size_t last_len); | ||
60 | 63 | ||
61 | /* should be zero-filled before start */ | 64 | /* should be zero-filled before start */ |
62 | struct phr_chunked_decoder { | 65 | struct phr_chunked_decoder { |
63 | size_t bytes_left_in_chunk; /* number of bytes left in current chunk */ | 66 | size_t bytes_left_in_chunk; /* number of bytes left in current chunk */ |
64 | char consume_trailer; /* if trailing headers should be consumed */ | 67 | char consume_trailer; /* if trailing headers should be consumed */ |
65 | char _hex_count; | 68 | char _hex_count; |
66 | char _state; | 69 | char _state; |
67 | }; | 70 | }; |
68 | 71 | ||
69 | /* the function rewrites the buffer given as (buf, bufsz) removing the chunked- | 72 | /* the function rewrites the buffer given as (buf, bufsz) removing the chunked- |
diff --git a/plugins/popen.c b/plugins/popen.c index cfe930b6..c596d1e0 100644 --- a/plugins/popen.c +++ b/plugins/popen.c | |||
@@ -68,7 +68,7 @@ void popen_timeout_alarm_handler(int /*signo*/); | |||
68 | #endif | 68 | #endif |
69 | 69 | ||
70 | #ifndef WIFEXITED | 70 | #ifndef WIFEXITED |
71 | # define WIFEXITED(stat_val) (((stat_val)&255) == 0) | 71 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) |
72 | #endif | 72 | #endif |
73 | 73 | ||
74 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ | 74 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ |
@@ -96,24 +96,28 @@ FILE *spopen(const char *cmdstring) { | |||
96 | env[1] = NULL; | 96 | env[1] = NULL; |
97 | 97 | ||
98 | /* if no command was passed, return with no error */ | 98 | /* if no command was passed, return with no error */ |
99 | if (cmdstring == NULL) | 99 | if (cmdstring == NULL) { |
100 | return (NULL); | 100 | return (NULL); |
101 | } | ||
101 | 102 | ||
102 | char *cmd = NULL; | 103 | char *cmd = NULL; |
103 | /* make copy of command string so strtok() doesn't silently modify it */ | 104 | /* make copy of command string so strtok() doesn't silently modify it */ |
104 | /* (the calling program may want to access it later) */ | 105 | /* (the calling program may want to access it later) */ |
105 | cmd = malloc(strlen(cmdstring) + 1); | 106 | cmd = malloc(strlen(cmdstring) + 1); |
106 | if (cmd == NULL) | 107 | if (cmd == NULL) { |
107 | return NULL; | 108 | return NULL; |
109 | } | ||
108 | strcpy(cmd, cmdstring); | 110 | strcpy(cmd, cmdstring); |
109 | 111 | ||
110 | /* This is not a shell, so we don't handle "???" */ | 112 | /* This is not a shell, so we don't handle "???" */ |
111 | if (strstr(cmdstring, "\"")) | 113 | if (strstr(cmdstring, "\"")) { |
112 | return NULL; | 114 | return NULL; |
115 | } | ||
113 | 116 | ||
114 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ | 117 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ |
115 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) | 118 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) { |
116 | return NULL; | 119 | return NULL; |
120 | } | ||
117 | 121 | ||
118 | int argc; | 122 | int argc; |
119 | char **argv = NULL; | 123 | char **argv = NULL; |
@@ -140,15 +144,17 @@ FILE *spopen(const char *cmdstring) { | |||
140 | 144 | ||
141 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ | 145 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ |
142 | str++; | 146 | str++; |
143 | if (!strstr(str, "'")) | 147 | if (!strstr(str, "'")) { |
144 | return NULL; /* balanced? */ | 148 | return NULL; /* balanced? */ |
149 | } | ||
145 | cmd = 1 + strstr(str, "'"); | 150 | cmd = 1 + strstr(str, "'"); |
146 | str[strcspn(str, "'")] = 0; | 151 | str[strcspn(str, "'")] = 0; |
147 | } else if (strcspn(str, "'") < strcspn(str, " \t\r\n")) { | 152 | } else if (strcspn(str, "'") < strcspn(str, " \t\r\n")) { |
148 | /* handle --option='foo bar' strings */ | 153 | /* handle --option='foo bar' strings */ |
149 | char *tmp = str + strcspn(str, "'") + 1; | 154 | char *tmp = str + strcspn(str, "'") + 1; |
150 | if (!strstr(tmp, "'")) | 155 | if (!strstr(tmp, "'")) { |
151 | return NULL; /* balanced? */ | 156 | return NULL; /* balanced? */ |
157 | } | ||
152 | tmp += strcspn(tmp, "'") + 1; | 158 | tmp += strcspn(tmp, "'") + 1; |
153 | *tmp = 0; | 159 | *tmp = 0; |
154 | cmd = tmp + 1; | 160 | cmd = tmp + 1; |
@@ -161,8 +167,9 @@ FILE *spopen(const char *cmdstring) { | |||
161 | } | 167 | } |
162 | } | 168 | } |
163 | 169 | ||
164 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) | 170 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) { |
165 | cmd = NULL; | 171 | cmd = NULL; |
172 | } | ||
166 | 173 | ||
167 | argv[i++] = str; | 174 | argv[i++] = str; |
168 | } | 175 | } |
@@ -171,22 +178,26 @@ FILE *spopen(const char *cmdstring) { | |||
171 | long maxfd = mp_open_max(); | 178 | long maxfd = mp_open_max(); |
172 | 179 | ||
173 | if (childpid == NULL) { /* first time through */ | 180 | if (childpid == NULL) { /* first time through */ |
174 | if ((childpid = calloc((size_t)maxfd, sizeof(pid_t))) == NULL) | 181 | if ((childpid = calloc((size_t)maxfd, sizeof(pid_t))) == NULL) { |
175 | return (NULL); | 182 | return (NULL); |
183 | } | ||
176 | } | 184 | } |
177 | 185 | ||
178 | if (child_stderr_array == NULL) { /* first time through */ | 186 | if (child_stderr_array == NULL) { /* first time through */ |
179 | if ((child_stderr_array = calloc((size_t)maxfd, sizeof(int))) == NULL) | 187 | if ((child_stderr_array = calloc((size_t)maxfd, sizeof(int))) == NULL) { |
180 | return (NULL); | 188 | return (NULL); |
189 | } | ||
181 | } | 190 | } |
182 | 191 | ||
183 | int pfd[2]; | 192 | int pfd[2]; |
184 | if (pipe(pfd) < 0) | 193 | if (pipe(pfd) < 0) { |
185 | return (NULL); /* errno set by pipe() */ | 194 | return (NULL); /* errno set by pipe() */ |
195 | } | ||
186 | 196 | ||
187 | int pfderr[2]; | 197 | int pfderr[2]; |
188 | if (pipe(pfderr) < 0) | 198 | if (pipe(pfderr) < 0) { |
189 | return (NULL); /* errno set by pipe() */ | 199 | return (NULL); /* errno set by pipe() */ |
200 | } | ||
190 | 201 | ||
191 | #ifdef REDHAT_SPOPEN_ERROR | 202 | #ifdef REDHAT_SPOPEN_ERROR |
192 | if (signal(SIGCHLD, popen_sigchld_handler) == SIG_ERR) { | 203 | if (signal(SIGCHLD, popen_sigchld_handler) == SIG_ERR) { |
@@ -195,8 +206,9 @@ FILE *spopen(const char *cmdstring) { | |||
195 | #endif | 206 | #endif |
196 | 207 | ||
197 | pid_t pid; | 208 | pid_t pid; |
198 | if ((pid = fork()) < 0) | 209 | if ((pid = fork()) < 0) { |
199 | return (NULL); /* errno set by fork() */ | 210 | return (NULL); /* errno set by fork() */ |
211 | } | ||
200 | 212 | ||
201 | if (pid == 0) { /* child */ | 213 | if (pid == 0) { /* child */ |
202 | close(pfd[0]); | 214 | close(pfd[0]); |
@@ -210,17 +222,20 @@ FILE *spopen(const char *cmdstring) { | |||
210 | close(pfderr[1]); | 222 | close(pfderr[1]); |
211 | } | 223 | } |
212 | /* close all descriptors in childpid[] */ | 224 | /* close all descriptors in childpid[] */ |
213 | for (i = 0; i < maxfd; i++) | 225 | for (i = 0; i < maxfd; i++) { |
214 | if (childpid[i] > 0) | 226 | if (childpid[i] > 0) { |
215 | close(i); | 227 | close(i); |
228 | } | ||
229 | } | ||
216 | 230 | ||
217 | execve(argv[0], argv, env); | 231 | execve(argv[0], argv, env); |
218 | _exit(0); | 232 | _exit(0); |
219 | } | 233 | } |
220 | 234 | ||
221 | close(pfd[1]); /* parent */ | 235 | close(pfd[1]); /* parent */ |
222 | if ((child_process = fdopen(pfd[0], "r")) == NULL) | 236 | if ((child_process = fdopen(pfd[0], "r")) == NULL) { |
223 | return (NULL); | 237 | return (NULL); |
238 | } | ||
224 | close(pfderr[1]); | 239 | close(pfderr[1]); |
225 | 240 | ||
226 | childpid[fileno(child_process)] = pid; /* remember child pid for this fd */ | 241 | childpid[fileno(child_process)] = pid; /* remember child pid for this fd */ |
@@ -229,17 +244,20 @@ FILE *spopen(const char *cmdstring) { | |||
229 | } | 244 | } |
230 | 245 | ||
231 | int spclose(FILE *fp) { | 246 | int spclose(FILE *fp) { |
232 | if (childpid == NULL) | 247 | if (childpid == NULL) { |
233 | return (1); /* popen() has never been called */ | 248 | return (1); /* popen() has never been called */ |
249 | } | ||
234 | 250 | ||
235 | pid_t pid; | 251 | pid_t pid; |
236 | int fd = fileno(fp); | 252 | int fd = fileno(fp); |
237 | if ((pid = childpid[fd]) == 0) | 253 | if ((pid = childpid[fd]) == 0) { |
238 | return (1); /* fp wasn't opened by popen() */ | 254 | return (1); /* fp wasn't opened by popen() */ |
255 | } | ||
239 | 256 | ||
240 | childpid[fd] = 0; | 257 | childpid[fd] = 0; |
241 | if (fclose(fp) == EOF) | 258 | if (fclose(fp) == EOF) { |
242 | return (1); | 259 | return (1); |
260 | } | ||
243 | 261 | ||
244 | #ifdef REDHAT_SPOPEN_ERROR | 262 | #ifdef REDHAT_SPOPEN_ERROR |
245 | while (!childtermd) | 263 | while (!childtermd) |
@@ -247,20 +265,24 @@ int spclose(FILE *fp) { | |||
247 | #endif | 265 | #endif |
248 | 266 | ||
249 | int status; | 267 | int status; |
250 | while (waitpid(pid, &status, 0) < 0) | 268 | while (waitpid(pid, &status, 0) < 0) { |
251 | if (errno != EINTR) | 269 | if (errno != EINTR) { |
252 | return (1); /* error other than EINTR from waitpid() */ | 270 | return (1); /* error other than EINTR from waitpid() */ |
271 | } | ||
272 | } | ||
253 | 273 | ||
254 | if (WIFEXITED(status)) | 274 | if (WIFEXITED(status)) { |
255 | return (WEXITSTATUS(status)); /* return child's termination status */ | 275 | return (WEXITSTATUS(status)); /* return child's termination status */ |
276 | } | ||
256 | 277 | ||
257 | return (1); | 278 | return (1); |
258 | } | 279 | } |
259 | 280 | ||
260 | #ifdef REDHAT_SPOPEN_ERROR | 281 | #ifdef REDHAT_SPOPEN_ERROR |
261 | void popen_sigchld_handler(int signo) { | 282 | void popen_sigchld_handler(int signo) { |
262 | if (signo == SIGCHLD) | 283 | if (signo == SIGCHLD) { |
263 | childtermd = 1; | 284 | childtermd = 1; |
285 | } | ||
264 | } | 286 | } |
265 | #endif | 287 | #endif |
266 | 288 | ||
diff --git a/plugins/popen.h b/plugins/popen.h index 1ea69632..e318ce25 100644 --- a/plugins/popen.h +++ b/plugins/popen.h | |||
@@ -1,13 +1,13 @@ | |||
1 | /****************************************************************************** | 1 | /****************************************************************************** |
2 | * | 2 | * |
3 | * | 3 | * |
4 | *****************************************************************************/ | 4 | *****************************************************************************/ |
5 | 5 | ||
6 | FILE *spopen (const char *); | 6 | FILE *spopen(const char *); |
7 | int spclose (FILE *); | 7 | int spclose(FILE *); |
8 | void popen_timeout_alarm_handler (int); | 8 | void popen_timeout_alarm_handler(int); |
9 | 9 | ||
10 | pid_t *childpid=NULL; | 10 | pid_t *childpid = NULL; |
11 | int *child_stderr_array=NULL; | 11 | int *child_stderr_array = NULL; |
12 | FILE *child_process=NULL; | 12 | FILE *child_process = NULL; |
13 | FILE *child_stderr=NULL; | 13 | FILE *child_stderr = NULL; |
diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 4429ceb0..7c583b85 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c | |||
@@ -53,7 +53,7 @@ | |||
53 | #endif | 53 | #endif |
54 | 54 | ||
55 | #ifndef WIFEXITED | 55 | #ifndef WIFEXITED |
56 | # define WIFEXITED(stat_val) (((stat_val)&255) == 0) | 56 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) |
57 | #endif | 57 | #endif |
58 | 58 | ||
59 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ | 59 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ |
@@ -87,8 +87,9 @@ extern void die(int, const char *, ...) __attribute__((__noreturn__, __format__( | |||
87 | * through this api and thus achieve async-safeness throughout the api */ | 87 | * through this api and thus achieve async-safeness throughout the api */ |
88 | void np_runcmd_init(void) { | 88 | void np_runcmd_init(void) { |
89 | long maxfd = mp_open_max(); | 89 | long maxfd = mp_open_max(); |
90 | if (!np_pids) | 90 | if (!np_pids) { |
91 | np_pids = calloc(maxfd, sizeof(pid_t)); | 91 | np_pids = calloc(maxfd, sizeof(pid_t)); |
92 | } | ||
92 | } | 93 | } |
93 | 94 | ||
94 | /* Start running a command */ | 95 | /* Start running a command */ |
@@ -106,8 +107,9 @@ static int np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) { | |||
106 | 107 | ||
107 | int i = 0; | 108 | int i = 0; |
108 | 109 | ||
109 | if (!np_pids) | 110 | if (!np_pids) { |
110 | NP_RUNCMD_INIT; | 111 | NP_RUNCMD_INIT; |
112 | } | ||
111 | 113 | ||
112 | env[0] = strdup("LC_ALL=C"); | 114 | env[0] = strdup("LC_ALL=C"); |
113 | env[1] = NULL; | 115 | env[1] = NULL; |
@@ -115,18 +117,21 @@ static int np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) { | |||
115 | /* make copy of command string so strtok() doesn't silently modify it */ | 117 | /* make copy of command string so strtok() doesn't silently modify it */ |
116 | /* (the calling program may want to access it later) */ | 118 | /* (the calling program may want to access it later) */ |
117 | cmdlen = strlen(cmdstring); | 119 | cmdlen = strlen(cmdstring); |
118 | if ((cmd = malloc(cmdlen + 1)) == NULL) | 120 | if ((cmd = malloc(cmdlen + 1)) == NULL) { |
119 | return -1; | 121 | return -1; |
122 | } | ||
120 | memcpy(cmd, cmdstring, cmdlen); | 123 | memcpy(cmd, cmdstring, cmdlen); |
121 | cmd[cmdlen] = '\0'; | 124 | cmd[cmdlen] = '\0'; |
122 | 125 | ||
123 | /* This is not a shell, so we don't handle "???" */ | 126 | /* This is not a shell, so we don't handle "???" */ |
124 | if (strstr(cmdstring, "\"")) | 127 | if (strstr(cmdstring, "\"")) { |
125 | return -1; | 128 | return -1; |
129 | } | ||
126 | 130 | ||
127 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ | 131 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ |
128 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) | 132 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) { |
129 | return -1; | 133 | return -1; |
134 | } | ||
130 | 135 | ||
131 | /* each arg must be whitespace-separated, so args can be a maximum | 136 | /* each arg must be whitespace-separated, so args can be a maximum |
132 | * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ | 137 | * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ |
@@ -145,8 +150,9 @@ static int np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) { | |||
145 | 150 | ||
146 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ | 151 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ |
147 | str++; | 152 | str++; |
148 | if (!strstr(str, "'")) | 153 | if (!strstr(str, "'")) { |
149 | return -1; /* balanced? */ | 154 | return -1; /* balanced? */ |
155 | } | ||
150 | cmd = 1 + strstr(str, "'"); | 156 | cmd = 1 + strstr(str, "'"); |
151 | str[strcspn(str, "'")] = 0; | 157 | str[strcspn(str, "'")] = 0; |
152 | } else { | 158 | } else { |
@@ -158,14 +164,16 @@ static int np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) { | |||
158 | } | 164 | } |
159 | } | 165 | } |
160 | 166 | ||
161 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) | 167 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) { |
162 | cmd = NULL; | 168 | cmd = NULL; |
169 | } | ||
163 | 170 | ||
164 | argv[i++] = str; | 171 | argv[i++] = str; |
165 | } | 172 | } |
166 | 173 | ||
167 | if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) | 174 | if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) { |
168 | return -1; /* errno set by the failing function */ | 175 | return -1; /* errno set by the failing function */ |
176 | } | ||
169 | 177 | ||
170 | /* child runs exceve() and _exit. */ | 178 | /* child runs exceve() and _exit. */ |
171 | if (pid == 0) { | 179 | if (pid == 0) { |
@@ -190,9 +198,11 @@ static int np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) { | |||
190 | * This is executed in a separate address space (pure child), | 198 | * This is executed in a separate address space (pure child), |
191 | * so we don't have to worry about async safety */ | 199 | * so we don't have to worry about async safety */ |
192 | long maxfd = mp_open_max(); | 200 | long maxfd = mp_open_max(); |
193 | for (i = 0; i < maxfd; i++) | 201 | for (i = 0; i < maxfd; i++) { |
194 | if (np_pids[i] > 0) | 202 | if (np_pids[i] > 0) { |
195 | close(i); | 203 | close(i); |
204 | } | ||
205 | } | ||
196 | 206 | ||
197 | execve(argv[0], argv, env); | 207 | execve(argv[0], argv, env); |
198 | _exit(STATE_UNKNOWN); | 208 | _exit(STATE_UNKNOWN); |
@@ -215,17 +225,21 @@ static int np_runcmd_close(int fd) { | |||
215 | 225 | ||
216 | /* make sure this fd was opened by popen() */ | 226 | /* make sure this fd was opened by popen() */ |
217 | long maxfd = mp_open_max(); | 227 | long maxfd = mp_open_max(); |
218 | if (fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) | 228 | if (fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) { |
219 | return -1; | 229 | return -1; |
230 | } | ||
220 | 231 | ||
221 | np_pids[fd] = 0; | 232 | np_pids[fd] = 0; |
222 | if (close(fd) == -1) | 233 | if (close(fd) == -1) { |
223 | return -1; | 234 | return -1; |
235 | } | ||
224 | 236 | ||
225 | /* EINTR is ok (sort of), everything else is bad */ | 237 | /* EINTR is ok (sort of), everything else is bad */ |
226 | while (waitpid(pid, &status, 0) < 0) | 238 | while (waitpid(pid, &status, 0) < 0) { |
227 | if (errno != EINTR) | 239 | if (errno != EINTR) { |
228 | return -1; | 240 | return -1; |
241 | } | ||
242 | } | ||
229 | 243 | ||
230 | /* return child's termination status */ | 244 | /* return child's termination status */ |
231 | return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; | 245 | return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; |
@@ -233,15 +247,18 @@ static int np_runcmd_close(int fd) { | |||
233 | 247 | ||
234 | void runcmd_timeout_alarm_handler(int signo) { | 248 | void runcmd_timeout_alarm_handler(int signo) { |
235 | 249 | ||
236 | if (signo == SIGALRM) | 250 | if (signo == SIGALRM) { |
237 | puts(_("CRITICAL - Plugin timed out while executing system call")); | 251 | puts(_("CRITICAL - Plugin timed out while executing system call")); |
252 | } | ||
238 | 253 | ||
239 | long maxfd = mp_open_max(); | 254 | long maxfd = mp_open_max(); |
240 | if (np_pids) | 255 | if (np_pids) { |
241 | for (long int i = 0; i < maxfd; i++) { | 256 | for (long int i = 0; i < maxfd; i++) { |
242 | if (np_pids[i] != 0) | 257 | if (np_pids[i] != 0) { |
243 | kill(np_pids[i], SIGKILL); | 258 | kill(np_pids[i], SIGKILL); |
259 | } | ||
244 | } | 260 | } |
261 | } | ||
245 | 262 | ||
246 | exit(STATE_CRITICAL); | 263 | exit(STATE_CRITICAL); |
247 | } | 264 | } |
@@ -270,15 +287,17 @@ static int np_fetch_output(int fd, output *op, int flags) { | |||
270 | 287 | ||
271 | /* some plugins may want to keep output unbroken, and some commands | 288 | /* some plugins may want to keep output unbroken, and some commands |
272 | * will yield no output, so return here for those */ | 289 | * will yield no output, so return here for those */ |
273 | if (flags & RUNCMD_NO_ARRAYS || !op->buf || !op->buflen) | 290 | if (flags & RUNCMD_NO_ARRAYS || !op->buf || !op->buflen) { |
274 | return op->buflen; | 291 | return op->buflen; |
292 | } | ||
275 | 293 | ||
276 | /* and some may want both */ | 294 | /* and some may want both */ |
277 | if (flags & RUNCMD_NO_ASSOC) { | 295 | if (flags & RUNCMD_NO_ASSOC) { |
278 | buf = malloc(op->buflen); | 296 | buf = malloc(op->buflen); |
279 | memcpy(buf, op->buf, op->buflen); | 297 | memcpy(buf, op->buf, op->buflen); |
280 | } else | 298 | } else { |
281 | buf = op->buf; | 299 | buf = op->buf; |
300 | } | ||
282 | 301 | ||
283 | op->line = NULL; | 302 | op->line = NULL; |
284 | op->lens = NULL; | 303 | op->lens = NULL; |
@@ -299,8 +318,9 @@ static int np_fetch_output(int fd, output *op, int flags) { | |||
299 | op->line[lineno] = &buf[i]; | 318 | op->line[lineno] = &buf[i]; |
300 | 319 | ||
301 | /* hop to next newline or end of buffer */ | 320 | /* hop to next newline or end of buffer */ |
302 | while (buf[i] != '\n' && i < op->buflen) | 321 | while (buf[i] != '\n' && i < op->buflen) { |
303 | i++; | 322 | i++; |
323 | } | ||
304 | buf[i] = '\0'; | 324 | buf[i] = '\0'; |
305 | 325 | ||
306 | /* calculate the string length using pointer difference */ | 326 | /* calculate the string length using pointer difference */ |
@@ -317,18 +337,23 @@ int np_runcmd(const char *cmd, output *out, output *err, int flags) { | |||
317 | int fd, pfd_out[2], pfd_err[2]; | 337 | int fd, pfd_out[2], pfd_err[2]; |
318 | 338 | ||
319 | /* initialize the structs */ | 339 | /* initialize the structs */ |
320 | if (out) | 340 | if (out) { |
321 | memset(out, 0, sizeof(output)); | 341 | memset(out, 0, sizeof(output)); |
322 | if (err) | 342 | } |
343 | if (err) { | ||
323 | memset(err, 0, sizeof(output)); | 344 | memset(err, 0, sizeof(output)); |
345 | } | ||
324 | 346 | ||
325 | if ((fd = np_runcmd_open(cmd, pfd_out, pfd_err)) == -1) | 347 | if ((fd = np_runcmd_open(cmd, pfd_out, pfd_err)) == -1) { |
326 | die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); | 348 | die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); |
349 | } | ||
327 | 350 | ||
328 | if (out) | 351 | if (out) { |
329 | out->lines = np_fetch_output(pfd_out[0], out, flags); | 352 | out->lines = np_fetch_output(pfd_out[0], out, flags); |
330 | if (err) | 353 | } |
354 | if (err) { | ||
331 | err->lines = np_fetch_output(pfd_err[0], err, flags); | 355 | err->lines = np_fetch_output(pfd_err[0], err, flags); |
356 | } | ||
332 | 357 | ||
333 | return np_runcmd_close(fd); | 358 | return np_runcmd_close(fd); |
334 | } | 359 | } |
diff --git a/plugins/runcmd.h b/plugins/runcmd.h index 2dcdadf0..63ce7b12 100644 --- a/plugins/runcmd.h +++ b/plugins/runcmd.h | |||
@@ -1,25 +1,25 @@ | |||
1 | /**************************************************************************** | 1 | /**************************************************************************** |
2 | * | 2 | * |
3 | * License: GPL | 3 | * License: GPL |
4 | * Copyright (c) 2005 Monitoring Plugins Development Team | 4 | * Copyright (c) 2005 Monitoring Plugins Development Team |
5 | * Author: Andreas Ericsson <ae@op5.se> | 5 | * Author: Andreas Ericsson <ae@op5.se> |
6 | * | 6 | * |
7 | * | 7 | * |
8 | * This program is free software: you can redistribute it and/or modify | 8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | 9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or | 10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. | 11 | * (at your option) any later version. |
12 | * | 12 | * |
13 | * This program is distributed in the hope that it will be useful, | 13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. | 16 | * GNU General Public License for more details. |
17 | * | 17 | * |
18 | * You should have received a copy of the GNU General Public License | 18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * | 20 | * |
21 | * | 21 | * |
22 | *****************************************************************************/ | 22 | *****************************************************************************/ |
23 | 23 | ||
24 | #ifndef NAGIOSPLUG_RUNCMD_H | 24 | #ifndef NAGIOSPLUG_RUNCMD_H |
25 | #define NAGIOSPLUG_RUNCMD_H | 25 | #define NAGIOSPLUG_RUNCMD_H |
@@ -29,8 +29,7 @@ | |||
29 | 29 | ||
30 | /** prototypes **/ | 30 | /** prototypes **/ |
31 | int np_runcmd(const char *, output *, output *, int); | 31 | int np_runcmd(const char *, output *, output *, int); |
32 | void runcmd_timeout_alarm_handler(int) | 32 | void runcmd_timeout_alarm_handler(int) __attribute__((__noreturn__)); |
33 | __attribute__((__noreturn__)); | ||
34 | 33 | ||
35 | /* only multi-threaded plugins need to bother with this */ | 34 | /* only multi-threaded plugins need to bother with this */ |
36 | void np_runcmd_init(void); | 35 | void np_runcmd_init(void); |
@@ -38,6 +37,6 @@ void np_runcmd_init(void); | |||
38 | 37 | ||
39 | /* possible flags for np_runcmd()'s fourth argument */ | 38 | /* possible flags for np_runcmd()'s fourth argument */ |
40 | #define RUNCMD_NO_ARRAYS 0x01 /* don't populate arrays at all */ | 39 | #define RUNCMD_NO_ARRAYS 0x01 /* don't populate arrays at all */ |
41 | #define RUNCMD_NO_ASSOC 0x02 /* output.line won't point to buf */ | 40 | #define RUNCMD_NO_ASSOC 0x02 /* output.line won't point to buf */ |
42 | 41 | ||
43 | #endif /* NAGIOSPLUG_RUNCMD_H */ | 42 | #endif /* NAGIOSPLUG_RUNCMD_H */ |
diff --git a/plugins/sslutils.c b/plugins/sslutils.c index 719de575..0e6d7525 100644 --- a/plugins/sslutils.c +++ b/plugins/sslutils.c | |||
@@ -26,10 +26,12 @@ | |||
26 | * | 26 | * |
27 | *****************************************************************************/ | 27 | *****************************************************************************/ |
28 | 28 | ||
29 | #include "output.h" | ||
29 | #define MAX_CN_LENGTH 256 | 30 | #define MAX_CN_LENGTH 256 |
30 | #include "common.h" | 31 | #include "common.h" |
31 | #include "netutils.h" | 32 | #include "netutils.h" |
32 | #include "../lib/monitoringplug.h" | 33 | #include "../lib/monitoringplug.h" |
34 | #include "states.h" | ||
33 | 35 | ||
34 | #ifdef HAVE_SSL | 36 | #ifdef HAVE_SSL |
35 | static SSL_CTX *ctx = NULL; | 37 | static SSL_CTX *ctx = NULL; |
@@ -37,13 +39,16 @@ static SSL *s = NULL; | |||
37 | 39 | ||
38 | int np_net_ssl_init(int sd) { return np_net_ssl_init_with_hostname(sd, NULL); } | 40 | int np_net_ssl_init(int sd) { return np_net_ssl_init_with_hostname(sd, NULL); } |
39 | 41 | ||
40 | int np_net_ssl_init_with_hostname(int sd, char *host_name) { return np_net_ssl_init_with_hostname_and_version(sd, host_name, 0); } | 42 | int np_net_ssl_init_with_hostname(int sd, char *host_name) { |
43 | return np_net_ssl_init_with_hostname_and_version(sd, host_name, 0); | ||
44 | } | ||
41 | 45 | ||
42 | int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version) { | 46 | int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version) { |
43 | return np_net_ssl_init_with_hostname_version_and_cert(sd, host_name, version, NULL, NULL); | 47 | return np_net_ssl_init_with_hostname_version_and_cert(sd, host_name, version, NULL, NULL); |
44 | } | 48 | } |
45 | 49 | ||
46 | int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, char *privkey) { | 50 | int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, |
51 | char *privkey) { | ||
47 | long options = 0; | 52 | long options = 0; |
48 | 53 | ||
49 | if ((ctx = SSL_CTX_new(TLS_client_method())) == NULL) { | 54 | if ((ctx = SSL_CTX_new(TLS_client_method())) == NULL) { |
@@ -75,7 +80,8 @@ int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int | |||
75 | # endif | 80 | # endif |
76 | case MP_TLSv1_1: /* TLSv1.1 protocol */ | 81 | case MP_TLSv1_1: /* TLSv1.1 protocol */ |
77 | # if !defined(SSL_OP_NO_TLSv1_1) | 82 | # if !defined(SSL_OP_NO_TLSv1_1) |
78 | printf("%s\n", _("UNKNOWN - TLS protocol version 1.1 is not supported by your SSL library.")); | 83 | printf("%s\n", |
84 | _("UNKNOWN - TLS protocol version 1.1 is not supported by your SSL library.")); | ||
79 | return STATE_UNKNOWN; | 85 | return STATE_UNKNOWN; |
80 | # else | 86 | # else |
81 | SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); | 87 | SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
@@ -84,7 +90,8 @@ int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int | |||
84 | # endif | 90 | # endif |
85 | case MP_TLSv1_2: /* TLSv1.2 protocol */ | 91 | case MP_TLSv1_2: /* TLSv1.2 protocol */ |
86 | # if !defined(SSL_OP_NO_TLSv1_2) | 92 | # if !defined(SSL_OP_NO_TLSv1_2) |
87 | printf("%s\n", _("UNKNOWN - TLS protocol version 1.2 is not supported by your SSL library.")); | 93 | printf("%s\n", |
94 | _("UNKNOWN - TLS protocol version 1.2 is not supported by your SSL library.")); | ||
88 | return STATE_UNKNOWN; | 95 | return STATE_UNKNOWN; |
89 | # else | 96 | # else |
90 | SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); | 97 | SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
@@ -145,8 +152,9 @@ int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int | |||
145 | SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY); | 152 | SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY); |
146 | if ((s = SSL_new(ctx)) != NULL) { | 153 | if ((s = SSL_new(ctx)) != NULL) { |
147 | # ifdef SSL_set_tlsext_host_name | 154 | # ifdef SSL_set_tlsext_host_name |
148 | if (host_name != NULL) | 155 | if (host_name != NULL) { |
149 | SSL_set_tlsext_host_name(s, host_name); | 156 | SSL_set_tlsext_host_name(s, host_name); |
157 | } | ||
150 | # endif | 158 | # endif |
151 | SSL_set_fd(s, sd); | 159 | SSL_set_fd(s, sd); |
152 | if (SSL_connect(s) == 1) { | 160 | if (SSL_connect(s) == 1) { |
@@ -182,63 +190,54 @@ int np_net_ssl_write(const void *buf, int num) { return SSL_write(s, buf, num); | |||
182 | 190 | ||
183 | int np_net_ssl_read(void *buf, int num) { return SSL_read(s, buf, num); } | 191 | int np_net_ssl_read(void *buf, int num) { return SSL_read(s, buf, num); } |
184 | 192 | ||
185 | int np_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, int days_till_exp_crit) { | 193 | mp_state_enum np_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, |
194 | int days_till_exp_crit) { | ||
186 | # ifdef USE_OPENSSL | 195 | # ifdef USE_OPENSSL |
187 | X509_NAME *subj = NULL; | ||
188 | char timestamp[50] = ""; | ||
189 | char cn[MAX_CN_LENGTH] = ""; | ||
190 | char *tz; | ||
191 | |||
192 | int cnlen = -1; | ||
193 | int status = STATE_UNKNOWN; | ||
194 | |||
195 | ASN1_STRING *tm; | ||
196 | int offset; | ||
197 | struct tm stamp; | ||
198 | float time_left; | ||
199 | int days_left; | ||
200 | int time_remaining; | ||
201 | time_t tm_t; | ||
202 | |||
203 | if (!certificate) { | 196 | if (!certificate) { |
204 | printf("%s\n", _("CRITICAL - Cannot retrieve server certificate.")); | 197 | printf("%s\n", _("CRITICAL - No server certificate present to inspect.")); |
205 | return STATE_CRITICAL; | 198 | return STATE_CRITICAL; |
206 | } | 199 | } |
207 | 200 | ||
208 | /* Extract CN from certificate subject */ | 201 | /* Extract CN from certificate subject */ |
209 | subj = X509_get_subject_name(certificate); | 202 | X509_NAME *subj = X509_get_subject_name(certificate); |
210 | 203 | ||
211 | if (!subj) { | 204 | if (!subj) { |
212 | printf("%s\n", _("CRITICAL - Cannot retrieve certificate subject.")); | 205 | printf("%s\n", _("CRITICAL - Cannot retrieve certificate subject.")); |
213 | return STATE_CRITICAL; | 206 | return STATE_CRITICAL; |
214 | } | 207 | } |
215 | cnlen = X509_NAME_get_text_by_NID(subj, NID_commonName, cn, sizeof(cn)); | 208 | |
216 | if (cnlen == -1) | 209 | char cn[MAX_CN_LENGTH] = ""; |
210 | int cnlen = X509_NAME_get_text_by_NID(subj, NID_commonName, cn, sizeof(cn)); | ||
211 | if (cnlen == -1) { | ||
217 | strcpy(cn, _("Unknown CN")); | 212 | strcpy(cn, _("Unknown CN")); |
213 | } | ||
218 | 214 | ||
219 | /* Retrieve timestamp of certificate */ | 215 | /* Retrieve timestamp of certificate */ |
220 | tm = X509_get_notAfter(certificate); | 216 | ASN1_STRING *tm = X509_get_notAfter(certificate); |
221 | 217 | ||
218 | int offset = 0; | ||
219 | struct tm stamp = {}; | ||
222 | /* Generate tm structure to process timestamp */ | 220 | /* Generate tm structure to process timestamp */ |
223 | if (tm->type == V_ASN1_UTCTIME) { | 221 | if (tm->type == V_ASN1_UTCTIME) { |
224 | if (tm->length < 10) { | 222 | if (tm->length < 10) { |
225 | printf("%s\n", _("CRITICAL - Wrong time format in certificate.")); | 223 | printf("%s\n", _("CRITICAL - Wrong time format in certificate.")); |
226 | return STATE_CRITICAL; | 224 | return STATE_CRITICAL; |
227 | } else { | ||
228 | stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0'); | ||
229 | if (stamp.tm_year < 50) | ||
230 | stamp.tm_year += 100; | ||
231 | offset = 0; | ||
232 | } | 225 | } |
226 | stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0'); | ||
227 | if (stamp.tm_year < 50) { | ||
228 | stamp.tm_year += 100; | ||
229 | } | ||
230 | offset = 0; | ||
231 | |||
233 | } else { | 232 | } else { |
234 | if (tm->length < 12) { | 233 | if (tm->length < 12) { |
235 | printf("%s\n", _("CRITICAL - Wrong time format in certificate.")); | 234 | printf("%s\n", _("CRITICAL - Wrong time format in certificate.")); |
236 | return STATE_CRITICAL; | 235 | return STATE_CRITICAL; |
237 | } else { | ||
238 | stamp.tm_year = (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 + (tm->data[2] - '0') * 10 + (tm->data[3] - '0'); | ||
239 | stamp.tm_year -= 1900; | ||
240 | offset = 2; | ||
241 | } | 236 | } |
237 | stamp.tm_year = (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 + | ||
238 | (tm->data[2] - '0') * 10 + (tm->data[3] - '0'); | ||
239 | stamp.tm_year -= 1900; | ||
240 | offset = 2; | ||
242 | } | 241 | } |
243 | stamp.tm_mon = (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1; | 242 | stamp.tm_mon = (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1; |
244 | stamp.tm_mday = (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0'); | 243 | stamp.tm_mday = (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0'); |
@@ -247,48 +246,60 @@ int np_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, int | |||
247 | stamp.tm_sec = (tm->data[10 + offset] - '0') * 10 + (tm->data[11 + offset] - '0'); | 246 | stamp.tm_sec = (tm->data[10 + offset] - '0') * 10 + (tm->data[11 + offset] - '0'); |
248 | stamp.tm_isdst = -1; | 247 | stamp.tm_isdst = -1; |
249 | 248 | ||
250 | tm_t = timegm(&stamp); | 249 | time_t tm_t = timegm(&stamp); |
251 | time_left = difftime(tm_t, time(NULL)); | 250 | float time_left = difftime(tm_t, time(NULL)); |
252 | days_left = time_left / 86400; | 251 | int days_left = time_left / 86400; |
253 | tz = getenv("TZ"); | 252 | char *tz = getenv("TZ"); |
254 | setenv("TZ", "GMT", 1); | 253 | setenv("TZ", "GMT", 1); |
255 | tzset(); | 254 | tzset(); |
255 | |||
256 | char timestamp[50] = ""; | ||
256 | strftime(timestamp, 50, "%c %z", localtime(&tm_t)); | 257 | strftime(timestamp, 50, "%c %z", localtime(&tm_t)); |
257 | if (tz) | 258 | if (tz) { |
258 | setenv("TZ", tz, 1); | 259 | setenv("TZ", tz, 1); |
259 | else | 260 | } else { |
260 | unsetenv("TZ"); | 261 | unsetenv("TZ"); |
262 | } | ||
263 | |||
261 | tzset(); | 264 | tzset(); |
262 | 265 | ||
266 | int time_remaining; | ||
267 | mp_state_enum status = STATE_UNKNOWN; | ||
263 | if (days_left > 0 && days_left <= days_till_exp_warn) { | 268 | if (days_left > 0 && days_left <= days_till_exp_warn) { |
264 | printf(_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", cn, | 269 | printf(_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), |
265 | days_left, timestamp); | 270 | (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", cn, days_left, timestamp); |
266 | if (days_left > days_till_exp_crit) | 271 | if (days_left > days_till_exp_crit) { |
267 | status = STATE_WARNING; | 272 | status = STATE_WARNING; |
268 | else | 273 | } else { |
269 | status = STATE_CRITICAL; | 274 | status = STATE_CRITICAL; |
275 | } | ||
270 | } else if (days_left == 0 && time_left > 0) { | 276 | } else if (days_left == 0 && time_left > 0) { |
271 | if (time_left >= 3600) | 277 | if (time_left >= 3600) { |
272 | time_remaining = (int)time_left / 3600; | 278 | time_remaining = (int)time_left / 3600; |
273 | else | 279 | } else { |
274 | time_remaining = (int)time_left / 60; | 280 | time_remaining = (int)time_left / 60; |
281 | } | ||
275 | 282 | ||
276 | printf(_("%s - Certificate '%s' expires in %u %s (%s)\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", cn, | 283 | printf(_("%s - Certificate '%s' expires in %u %s (%s)\n"), |
277 | time_remaining, time_left >= 3600 ? "hours" : "minutes", timestamp); | 284 | (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", cn, time_remaining, |
285 | time_left >= 3600 ? "hours" : "minutes", timestamp); | ||
278 | 286 | ||
279 | if (days_left > days_till_exp_crit) | 287 | if (days_left > days_till_exp_crit) { |
280 | status = STATE_WARNING; | 288 | status = STATE_WARNING; |
281 | else | 289 | } else { |
282 | status = STATE_CRITICAL; | 290 | status = STATE_CRITICAL; |
291 | } | ||
283 | } else if (time_left < 0) { | 292 | } else if (time_left < 0) { |
284 | printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), cn, timestamp); | 293 | printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), cn, timestamp); |
285 | status = STATE_CRITICAL; | 294 | status = STATE_CRITICAL; |
286 | } else if (days_left == 0) { | 295 | } else if (days_left == 0) { |
287 | printf(_("%s - Certificate '%s' just expired (%s).\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", cn, timestamp); | 296 | printf(_("%s - Certificate '%s' just expired (%s).\n"), |
288 | if (days_left > days_till_exp_crit) | 297 | (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", cn, timestamp); |
298 | if (days_left > days_till_exp_crit) { | ||
289 | status = STATE_WARNING; | 299 | status = STATE_WARNING; |
290 | else | 300 | } else { |
291 | status = STATE_CRITICAL; | 301 | status = STATE_CRITICAL; |
302 | } | ||
292 | } else { | 303 | } else { |
293 | printf(_("OK - Certificate '%s' will expire on %s.\n"), cn, timestamp); | 304 | printf(_("OK - Certificate '%s' will expire on %s.\n"), cn, timestamp); |
294 | status = STATE_OK; | 305 | status = STATE_OK; |
@@ -301,7 +312,7 @@ int np_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, int | |||
301 | # endif /* USE_OPENSSL */ | 312 | # endif /* USE_OPENSSL */ |
302 | } | 313 | } |
303 | 314 | ||
304 | int np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit) { | 315 | mp_state_enum np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit) { |
305 | # ifdef USE_OPENSSL | 316 | # ifdef USE_OPENSSL |
306 | X509 *certificate = NULL; | 317 | X509 *certificate = NULL; |
307 | certificate = SSL_get_peer_certificate(s); | 318 | certificate = SSL_get_peer_certificate(s); |
@@ -312,4 +323,136 @@ int np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit) { | |||
312 | # endif /* USE_OPENSSL */ | 323 | # endif /* USE_OPENSSL */ |
313 | } | 324 | } |
314 | 325 | ||
326 | mp_subcheck mp_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, | ||
327 | int days_till_exp_crit) { | ||
328 | mp_subcheck sc_cert = mp_subcheck_init(); | ||
329 | # ifdef USE_OPENSSL | ||
330 | if (!certificate) { | ||
331 | xasprintf(&sc_cert.output, _("No server certificate present to inspect")); | ||
332 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_CRITICAL); | ||
333 | return sc_cert; | ||
334 | } | ||
335 | |||
336 | /* Extract CN from certificate subject */ | ||
337 | X509_NAME *subj = X509_get_subject_name(certificate); | ||
338 | |||
339 | if (!subj) { | ||
340 | xasprintf(&sc_cert.output, _("Cannot retrieve certificate subject")); | ||
341 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_CRITICAL); | ||
342 | return sc_cert; | ||
343 | } | ||
344 | |||
345 | char commonName[MAX_CN_LENGTH] = ""; | ||
346 | int cnlen = X509_NAME_get_text_by_NID(subj, NID_commonName, commonName, sizeof(commonName)); | ||
347 | if (cnlen == -1) { | ||
348 | strcpy(commonName, _("Unknown CN")); | ||
349 | } | ||
350 | |||
351 | /* Retrieve timestamp of certificate */ | ||
352 | ASN1_STRING *expiry_timestamp = X509_get_notAfter(certificate); | ||
353 | |||
354 | int offset = 0; | ||
355 | struct tm stamp = {}; | ||
356 | /* Generate tm structure to process timestamp */ | ||
357 | if (expiry_timestamp->type == V_ASN1_UTCTIME) { | ||
358 | if (expiry_timestamp->length < 10) { | ||
359 | xasprintf(&sc_cert.output, _("Wrong time format in certificate")); | ||
360 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_CRITICAL); | ||
361 | return sc_cert; | ||
362 | } | ||
363 | |||
364 | stamp.tm_year = (expiry_timestamp->data[0] - '0') * 10 + (expiry_timestamp->data[1] - '0'); | ||
365 | if (stamp.tm_year < 50) { | ||
366 | stamp.tm_year += 100; | ||
367 | } | ||
368 | |||
369 | offset = 0; | ||
370 | } else { | ||
371 | if (expiry_timestamp->length < 12) { | ||
372 | xasprintf(&sc_cert.output, _("Wrong time format in certificate")); | ||
373 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_CRITICAL); | ||
374 | return sc_cert; | ||
375 | } | ||
376 | stamp.tm_year = (expiry_timestamp->data[0] - '0') * 1000 + | ||
377 | (expiry_timestamp->data[1] - '0') * 100 + | ||
378 | (expiry_timestamp->data[2] - '0') * 10 + (expiry_timestamp->data[3] - '0'); | ||
379 | stamp.tm_year -= 1900; | ||
380 | offset = 2; | ||
381 | } | ||
382 | |||
383 | stamp.tm_mon = (expiry_timestamp->data[2 + offset] - '0') * 10 + | ||
384 | (expiry_timestamp->data[3 + offset] - '0') - 1; | ||
385 | stamp.tm_mday = (expiry_timestamp->data[4 + offset] - '0') * 10 + | ||
386 | (expiry_timestamp->data[5 + offset] - '0'); | ||
387 | stamp.tm_hour = (expiry_timestamp->data[6 + offset] - '0') * 10 + | ||
388 | (expiry_timestamp->data[7 + offset] - '0'); | ||
389 | stamp.tm_min = (expiry_timestamp->data[8 + offset] - '0') * 10 + | ||
390 | (expiry_timestamp->data[9 + offset] - '0'); | ||
391 | stamp.tm_sec = (expiry_timestamp->data[10 + offset] - '0') * 10 + | ||
392 | (expiry_timestamp->data[11 + offset] - '0'); | ||
393 | stamp.tm_isdst = -1; | ||
394 | |||
395 | time_t tm_t = timegm(&stamp); | ||
396 | double time_left = difftime(tm_t, time(NULL)); | ||
397 | int days_left = (int)(time_left / 86400); | ||
398 | char *timeZone = getenv("TZ"); | ||
399 | setenv("TZ", "GMT", 1); | ||
400 | tzset(); | ||
401 | |||
402 | char timestamp[50] = ""; | ||
403 | strftime(timestamp, 50, "%c %z", localtime(&tm_t)); | ||
404 | if (timeZone) { | ||
405 | setenv("TZ", timeZone, 1); | ||
406 | } else { | ||
407 | unsetenv("TZ"); | ||
408 | } | ||
409 | |||
410 | tzset(); | ||
411 | |||
412 | int time_remaining; | ||
413 | if (days_left > 0 && days_left <= days_till_exp_warn) { | ||
414 | xasprintf(&sc_cert.output, _("Certificate '%s' expires in %d day(s) (%s)"), commonName, | ||
415 | days_left, timestamp); | ||
416 | if (days_left > days_till_exp_crit) { | ||
417 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_WARNING); | ||
418 | } else { | ||
419 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_CRITICAL); | ||
420 | } | ||
421 | } else if (days_left == 0 && time_left > 0) { | ||
422 | if (time_left >= 3600) { | ||
423 | time_remaining = (int)time_left / 3600; | ||
424 | } else { | ||
425 | time_remaining = (int)time_left / 60; | ||
426 | } | ||
427 | |||
428 | xasprintf(&sc_cert.output, _("Certificate '%s' expires in %u %s (%s)"), commonName, | ||
429 | time_remaining, time_left >= 3600 ? "hours" : "minutes", timestamp); | ||
430 | |||
431 | if (days_left > days_till_exp_crit) { | ||
432 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_WARNING); | ||
433 | } else { | ||
434 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_CRITICAL); | ||
435 | } | ||
436 | } else if (time_left < 0) { | ||
437 | xasprintf(&sc_cert.output, _("Certificate '%s' expired on %s"), commonName, timestamp); | ||
438 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_CRITICAL); | ||
439 | } else if (days_left == 0) { | ||
440 | xasprintf(&sc_cert.output, _("Certificate '%s' just expired (%s)"), commonName, timestamp); | ||
441 | if (days_left > days_till_exp_crit) { | ||
442 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_WARNING); | ||
443 | } else { | ||
444 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_CRITICAL); | ||
445 | } | ||
446 | } else { | ||
447 | xasprintf(&sc_cert.output, _("Certificate '%s' will expire on %s"), commonName, timestamp); | ||
448 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_OK); | ||
449 | } | ||
450 | X509_free(certificate); | ||
451 | return sc_cert; | ||
452 | # else /* ifndef USE_OPENSSL */ | ||
453 | xasprintf(&sc_cert.output, _("Plugin does not support checking certificates")); | ||
454 | sc_cert = mp_set_subcheck_state(sc_cert, STATE_WARNING); | ||
455 | return sc_cert; | ||
456 | # endif /* USE_OPENSSL */ | ||
457 | } | ||
315 | #endif /* HAVE_SSL */ | 458 | #endif /* HAVE_SSL */ |
diff --git a/plugins/t/check_curl.t b/plugins/t/check_curl.t index 7a930a4e..2c2fafde 100644 --- a/plugins/t/check_curl.t +++ b/plugins/t/check_curl.t | |||
@@ -13,12 +13,12 @@ use vars qw($tests $has_ipv6); | |||
13 | BEGIN { | 13 | BEGIN { |
14 | use NPTest; | 14 | use NPTest; |
15 | $has_ipv6 = NPTest::has_ipv6(); | 15 | $has_ipv6 = NPTest::has_ipv6(); |
16 | $tests = $has_ipv6 ? 59 : 57; | 16 | $tests = $has_ipv6 ? 55 : 53; |
17 | plan tests => $tests; | 17 | plan tests => $tests; |
18 | } | 18 | } |
19 | 19 | ||
20 | 20 | ||
21 | my $successOutput = '/OK.*HTTP.*second/'; | 21 | my $successOutput = '/.*HTTP.*second/'; |
22 | 22 | ||
23 | my $res; | 23 | my $res; |
24 | my $plugin = 'check_http'; | 24 | my $plugin = 'check_http'; |
@@ -63,7 +63,7 @@ $res = NPTest->testCmd( | |||
63 | ); | 63 | ); |
64 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); | 64 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); |
65 | # was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) | 65 | # was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) |
66 | like( $res->output, "/HTTP CRITICAL - Invalid HTTP response received from host on port 80: cURL returned 28 - Connection timed out after/", "Output OK"); | 66 | like( $res->output, "/cURL returned 28 - Connection timed out after/", "Output OK"); |
67 | 67 | ||
68 | $res = NPTest->testCmd( | 68 | $res = NPTest->testCmd( |
69 | "./$plugin $hostname_invalid -wt 1 -ct 2" | 69 | "./$plugin $hostname_invalid -wt 1 -ct 2" |
@@ -124,14 +124,14 @@ SKIP: { | |||
124 | 124 | ||
125 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing'" ); | 125 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing'" ); |
126 | cmp_ok( $res->return_code, "==", 2, "Not got 'mONiTORing'"); | 126 | cmp_ok( $res->return_code, "==", 2, "Not got 'mONiTORing'"); |
127 | like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'"); | 127 | like ( $res->output, "/matched not/", "Error message says 'matched not'"); |
128 | 128 | ||
129 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -R 'mONiTORing'" ); | 129 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -R 'mONiTORing'" ); |
130 | cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'mONiTORing'"); | 130 | cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'mONiTORing'"); |
131 | 131 | ||
132 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring' --invert-regex" ); | 132 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring' --invert-regex" ); |
133 | cmp_ok( $res->return_code, "==", 2, "Invert results work when found"); | 133 | cmp_ok( $res->return_code, "==", 2, "Invert results work when found"); |
134 | like ( $res->output, "/pattern found/", "Error message says 'pattern found'"); | 134 | like ( $res->output, "/matched/", "Error message says 'matched'"); |
135 | 135 | ||
136 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing' --invert-regex" ); | 136 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing' --invert-regex" ); |
137 | cmp_ok( $res->return_code, "==", 0, "And also when not found"); | 137 | cmp_ok( $res->return_code, "==", 0, "And also when not found"); |
@@ -151,63 +151,74 @@ SKIP: { | |||
151 | 151 | ||
152 | $res = NPTest->testCmd( "./$plugin -C 8000,1 --ssl $host_tls_http" ); | 152 | $res = NPTest->testCmd( "./$plugin -C 8000,1 --ssl $host_tls_http" ); |
153 | cmp_ok( $res->return_code, '==', 1, "Checking certificate for $host_tls_http"); | 153 | cmp_ok( $res->return_code, '==', 1, "Checking certificate for $host_tls_http"); |
154 | like ( $res->output, qr/WARNING - Certificate '$host_tls_cert' expires in \d+ day/, "Output Warning" ); | 154 | like ( $res->output, qr/Certificate '$host_tls_cert' expires in \d+ day/, "Output Warning" ); |
155 | 155 | ||
156 | $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); | 156 | $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); |
157 | is( $res->return_code, 0, "Old syntax for cert checking okay" ); | 157 | is( $res->return_code, 0, "Old syntax for cert checking okay" ); |
158 | is( $res->output, $saved_cert_output, "Same output as new syntax" ); | 158 | # deactivated since different timings will change the output |
159 | # TODO compare without perfdata | ||
160 | # is( $res->output, $saved_cert_output, "Same output as new syntax" ); | ||
159 | 161 | ||
160 | $res = NPTest->testCmd( "./$plugin -H $host_tls_http -C 1" ); | 162 | $res = NPTest->testCmd( "./$plugin -H $host_tls_http -C 1" ); |
161 | is( $res->return_code, 0, "Updated syntax for cert checking okay" ); | 163 | is( $res->return_code, 0, "Updated syntax for cert checking okay" ); |
162 | is( $res->output, $saved_cert_output, "Same output as new syntax" ); | 164 | # deactivated since different timings will change the output |
165 | # TODO compare without perfdata | ||
166 | # is( $res->output, $saved_cert_output, "Same output as new syntax" ); | ||
163 | 167 | ||
164 | $res = NPTest->testCmd( "./$plugin -C 1 $host_tls_http" ); | 168 | $res = NPTest->testCmd( "./$plugin -C 1 $host_tls_http" ); |
165 | cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added"); | 169 | # deactivated since different timings will change the output |
170 | # TODO compare without perfdata | ||
171 | # cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added"); | ||
166 | 172 | ||
167 | $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); | 173 | $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); |
168 | cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works"); | 174 | # deactivated since different timings will change the output |
175 | # TODO compare without perfdata | ||
176 | # cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works"); | ||
169 | 177 | ||
170 | # run some certificate checks with faketime | 178 | # run some certificate checks with faketime |
171 | SKIP: { | 179 | SKIP: { |
172 | skip "No faketime binary found", 12 if !$faketime; | 180 | skip "No faketime binary found", 12 if !$faketime; |
173 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./$plugin -C 1 $host_tls_http"); | 181 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./$plugin -C 1 $host_tls_http"); |
174 | like($res->output, qr/OK - Certificate '$host_tls_cert' will expire on/, "Catch cert output"); | 182 | like($res->output, qr/Certificate '$host_tls_cert' will expire on/, "Catch cert output"); |
175 | is( $res->return_code, 0, "Catch cert output exit code" ); | 183 | is( $res->return_code, 0, "Catch cert output exit code" ); |
184 | |||
176 | my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/); | 185 | my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/); |
177 | if(!defined $year) { | 186 | if(!defined $year) { |
178 | die("parsing date failed from: ".$res->output); | 187 | die("parsing date failed from: ".$res->output); |
179 | } | 188 | } |
189 | |||
180 | my $months = {'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11}; | 190 | my $months = {'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11}; |
181 | my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900); | 191 | my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900); |
182 | my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts)); | 192 | my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts)); |
193 | |||
183 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./$plugin -C 1 $host_tls_http"); | 194 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./$plugin -C 1 $host_tls_http"); |
184 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' just expired/, "Output on expire date"); | 195 | like($res->output, qr/Certificate '$host_tls_cert' just expired/, "Output on expire date"); |
185 | is( $res->return_code, 2, "Output on expire date" ); | 196 | is( $res->return_code, 2, "Output on expire date" ); |
186 | 197 | ||
187 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-1))."' ./$plugin -C 1 $host_tls_http"); | 198 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-1))."' ./$plugin -C 1 $host_tls_http"); |
188 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 0 minutes/, "cert expires in 1 second output"); | 199 | like($res->output, qr/Certificate '$host_tls_cert' expires in 0 minutes/, "cert expires in 1 second output"); |
189 | is( $res->return_code, 2, "cert expires in 1 second exit code" ); | 200 | is( $res->return_code, 2, "cert expires in 1 second exit code" ); |
190 | 201 | ||
191 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-120))."' ./$plugin -C 1 $host_tls_http"); | 202 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-120))."' ./$plugin -C 1 $host_tls_http"); |
192 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 minutes/, "cert expires in 2 minutes output"); | 203 | like($res->output, qr/Certificate '$host_tls_cert' expires in 2 minutes/, "cert expires in 2 minutes output"); |
193 | is( $res->return_code, 2, "cert expires in 2 minutes exit code" ); | 204 | is( $res->return_code, 2, "cert expires in 2 minutes exit code" ); |
194 | 205 | ||
195 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-7200))."' ./$plugin -C 1 $host_tls_http"); | 206 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-7200))."' ./$plugin -C 1 $host_tls_http"); |
196 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 hours/, "cert expires in 2 hours output"); | 207 | like($res->output, qr/Certificate '$host_tls_cert' expires in 2 hours/, "cert expires in 2 hours output"); |
197 | is( $res->return_code, 2, "cert expires in 2 hours exit code" ); | 208 | is( $res->return_code, 2, "cert expires in 2 hours exit code" ); |
198 | 209 | ||
199 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./$plugin -C 1 $host_tls_http"); | 210 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./$plugin -C 1 $host_tls_http"); |
200 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expired on/, "Certificate expired output"); | 211 | like($res->output, qr/Certificate '$host_tls_cert' expired on/, "Certificate expired output"); |
201 | is( $res->return_code, 2, "Certificate expired exit code" ); | 212 | is( $res->return_code, 2, "Certificate expired exit code" ); |
202 | }; | 213 | }; |
203 | 214 | ||
204 | $res = NPTest->testCmd( "./$plugin --ssl $host_tls_http -E" ); | 215 | $res = NPTest->testCmd( "./$plugin --ssl $host_tls_http -E" ); |
205 | like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); | 216 | like ( $res->output, '/\'time_connect\'=[\d\.]+/', 'Extended Performance Data Output OK' ); |
206 | like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' ); | 217 | like ( $res->output, '/\'time_tls\'=[\d\.]+/', 'Extended Performance Data SSL Output OK' ); |
207 | 218 | ||
208 | $res = NPTest->testCmd( "./$plugin -H monitoring-plugins.org -u /download.html -f follow" ); | 219 | $res = NPTest->testCmd( "./$plugin -H monitoring-plugins.org -u /download.html -f follow" ); |
209 | is( $res->return_code, 0, "Redirection based on location is okay"); | 220 | is( $res->return_code, 0, "Redirection based on location is okay"); |
210 | 221 | ||
211 | $res = NPTest->testCmd( "./$plugin -H monitoring-plugins.org --extended-perfdata" ); | 222 | $res = NPTest->testCmd( "./$plugin -H monitoring-plugins.org --extended-perfdata" ); |
212 | like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); | 223 | like ( $res->output, '/\'time_connect\'=[\d\.]+/', 'Extended Performance Data Output OK' ); |
213 | } | 224 | } |
diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index 9eb77ce4..0f62fb2b 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t | |||
@@ -10,6 +10,7 @@ use strict; | |||
10 | use Test::More; | 10 | use Test::More; |
11 | use NPTest; | 11 | use NPTest; |
12 | use POSIX qw(ceil floor); | 12 | use POSIX qw(ceil floor); |
13 | use Data::Dumper; | ||
13 | 14 | ||
14 | my $successOutput = '/^DISK OK/'; | 15 | my $successOutput = '/^DISK OK/'; |
15 | my $failureOutput = '/^DISK CRITICAL/'; | 16 | my $failureOutput = '/^DISK CRITICAL/'; |
@@ -20,173 +21,216 @@ my $result; | |||
20 | my $mountpoint_valid = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint", "/"); | 21 | my $mountpoint_valid = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint", "/"); |
21 | my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var"); | 22 | my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var"); |
22 | 23 | ||
24 | my $output_format = "--output-format mp-test-json"; | ||
25 | |||
23 | if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") { | 26 | if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") { |
24 | plan skip_all => "Need 2 mountpoints to test"; | 27 | plan skip_all => "Need 2 mountpoints to test"; |
25 | } else { | 28 | } else { |
26 | plan tests => 94; | 29 | plan tests => 97; |
27 | } | 30 | } |
28 | 31 | ||
29 | $result = NPTest->testCmd( | 32 | $result = NPTest->testCmd( |
30 | "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid" | 33 | "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -P -p $mountpoint2_valid $output_format" |
31 | ); | 34 | ); |
32 | cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free in space and inodes)"); | 35 | cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free in space and inodes)"); |
33 | my $c = 0; | ||
34 | $_ = $result->output; | ||
35 | $c++ while /\(/g; # counts number of "(" - should be two | ||
36 | cmp_ok( $c, '==', 2, "Got two mountpoints in output"); | ||
37 | 36 | ||
37 | like($result->{'mp_test_result'}->{'state'}, "/OK/", "Main result is OK"); | ||
38 | like($result->{'mp_test_result'}->{'checks'}->[0]->{'state'}, "/OK/", "First sub result is OK"); | ||
39 | like($result->{'mp_test_result'}->{'checks'}->[1]->{'state'}, "/OK/", "Second sub result is OK"); | ||
40 | |||
41 | my $absolut_space_mp1 = $result->{'mp_test_result'}->{'checks'}->[1]->{'checks'}->[0]->{'perfdata'}->[0]->{'max'}->{'value'}; | ||
42 | # print("absolute space on mp1: ". $absolut_space_mp1 . "\n"); | ||
43 | |||
44 | my $free_percent_on_mp1 = ($result->{'mp_test_result'}->{'checks'}->[1]->{'checks'}->[0]->{'perfdata'}->[0]->{'value'}->{'value'} / ($absolut_space_mp1/100)); | ||
45 | print("free percent on mp1: ". $free_percent_on_mp1 . "\n"); | ||
46 | |||
47 | my $absolut_space_mp2 = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}->[0]->{'perfdata'}->[0]->{'max'}->{'value'}; | ||
48 | # print("absolute space on mp2: ". $absolut_space_mp2 . "\n"); | ||
49 | |||
50 | my $free_percent_on_mp2 = ($result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}->[0]->{'perfdata'}->[0]->{'value'}->{'value'}/ ($absolut_space_mp2/100)); | ||
51 | print("free percent on mp2: ". $free_percent_on_mp2 . "\n"); | ||
38 | 52 | ||
39 | # Get perf data | 53 | my @perfdata; |
40 | # Should use Monitoring::Plugin | 54 | @perfdata[0] = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}->[0]->{'perfdata'}->[0]; |
41 | my @perf_data = sort(split(/ /, $result->perf_output)); | 55 | @perfdata[1] = $result->{'mp_test_result'}->{'checks'}->[1]->{'checks'}->[0]->{'perfdata'}->[0]; |
42 | 56 | ||
57 | # Decrease precision of numbers since the the fs might be modified between the two runs | ||
58 | $perfdata[0]->{'value'}->{'value'} = int($perfdata[0]->{'value'}->{'value'} / 1000000); | ||
59 | $perfdata[1]->{'value'}->{'value'} = int($perfdata[1]->{'value'}->{'value'} / 1000000); | ||
43 | 60 | ||
44 | # Calculate avg_free free on mountpoint1 and mountpoint2 | 61 | # Calculate avg_free free on mountpoint1 and mountpoint2 |
45 | # because if you check in the middle, you should get different errors | 62 | # because if you check in the middle, you should get different errors |
46 | $_ = $result->output; | 63 | my $avg_free_percent = ceil(($free_percent_on_mp1+$free_percent_on_mp2)/2); |
47 | my ($free_on_mp1, $free_on_mp2) = (m/\((\d+\.\d+)%.*\((\d+\.\d+)%/); | 64 | # print("avg_free: " . $avg_free_percent . "\n"); |
48 | die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2); | ||
49 | my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2); | ||
50 | my ($more_free, $less_free); | 65 | my ($more_free, $less_free); |
51 | if ($free_on_mp1 > $free_on_mp2) { | 66 | if ($free_percent_on_mp1 > $free_percent_on_mp2) { |
52 | $more_free = $mountpoint_valid; | 67 | $more_free = $mountpoint_valid; |
53 | $less_free = $mountpoint2_valid; | 68 | $less_free = $mountpoint2_valid; |
54 | } elsif ($free_on_mp1 < $free_on_mp2) { | 69 | } elsif ($free_percent_on_mp1 < $free_percent_on_mp2) { |
55 | $more_free = $mountpoint2_valid; | 70 | $more_free = $mountpoint2_valid; |
56 | $less_free = $mountpoint_valid; | 71 | $less_free = $mountpoint_valid; |
57 | } else { | 72 | } else { |
58 | die "Two mountpoints are the same - cannot do rest of test"; | 73 | die "Two mountpoints are the same - cannot do rest of test"; |
59 | } | 74 | } |
60 | if($free_on_mp1 == $avg_free || $free_on_mp2 == $avg_free) { | 75 | |
76 | print("less free: " . $less_free . "\n"); | ||
77 | print("more free: " . $more_free . "\n"); | ||
78 | |||
79 | if($free_percent_on_mp1 == $avg_free_percent || $free_percent_on_mp2 == $avg_free_percent) { | ||
61 | die "One mountpoints has average space free - cannot do rest of test"; | 80 | die "One mountpoints has average space free - cannot do rest of test"; |
62 | } | 81 | } |
63 | 82 | ||
83 | my $free_inodes_on_mp1 = $result->{'mp_test_result'}->{'checks'}->[1]->{'checks'}[2]->{'perfdata'}->[0]->{'value'}->{'value'}; | ||
84 | my $total_inodes_on_mp1 = $result->{'mp_test_result'}->{'checks'}->[1]->{'checks'}[2]->{'perfdata'}->[0]->{'max'}->{'value'}; | ||
85 | my $free_inode_percentage_on_mp1 = $free_inodes_on_mp1 / ($total_inodes_on_mp1 / 100); | ||
64 | 86 | ||
65 | # Do same for inodes | 87 | my $free_inodes_on_mp2 = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}[2]->{'perfdata'}->[0]->{'value'}->{'value'}; |
66 | $_ = $result->output; | 88 | my $total_inodes_on_mp2 = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}[2]->{'perfdata'}->[0]->{'max'}->{'value'}; |
67 | my ($free_inode_on_mp1, $free_inode_on_mp2) = (m/inode=(\d+)%.*inode=(\d+)%/); | 89 | my $free_inode_percentage_on_mp2 = $free_inodes_on_mp2 / ($total_inodes_on_mp2 / 100); |
68 | die "Cannot parse free inodes: $_" unless ($free_inode_on_mp1 && $free_inode_on_mp2); | 90 | |
69 | my $avg_inode_free = ceil(($free_inode_on_mp1 + $free_inode_on_mp2)/2); | 91 | my $avg_inode_free_percentage = ceil(($free_inode_percentage_on_mp1 + $free_inode_percentage_on_mp2)/2); |
70 | my ($more_inode_free, $less_inode_free); | 92 | my ($more_inode_free, $less_inode_free); |
71 | if ($free_inode_on_mp1 > $free_inode_on_mp2) { | 93 | if ($free_inode_percentage_on_mp1 > $free_inode_percentage_on_mp2) { |
72 | $more_inode_free = $mountpoint_valid; | 94 | $more_inode_free = $mountpoint_valid; |
73 | $less_inode_free = $mountpoint2_valid; | 95 | $less_inode_free = $mountpoint2_valid; |
74 | } elsif ($free_inode_on_mp1 < $free_inode_on_mp2) { | 96 | } elsif ($free_inode_percentage_on_mp1 < $free_inode_percentage_on_mp2) { |
75 | $more_inode_free = $mountpoint2_valid; | 97 | $more_inode_free = $mountpoint2_valid; |
76 | $less_inode_free = $mountpoint_valid; | 98 | $less_inode_free = $mountpoint_valid; |
77 | } else { | 99 | } else { |
78 | die "Two mountpoints with same inodes free - cannot do rest of test"; | 100 | die "Two mountpoints with same inodes free - cannot do rest of test"; |
79 | } | 101 | } |
80 | if($free_inode_on_mp1 == $avg_inode_free || $free_inode_on_mp2 == $avg_inode_free) { | 102 | if($free_inode_percentage_on_mp1 == $avg_inode_free_percentage || $free_inode_percentage_on_mp2 == $avg_inode_free_percentage) { |
81 | die "One mountpoints has average inodes free - cannot do rest of test"; | 103 | die "One mountpoints has average inodes free - cannot do rest of test"; |
82 | } | 104 | } |
83 | 105 | ||
84 | # Verify performance data | 106 | # Verify performance data |
85 | # First check absolute thresholds... | 107 | # First check absolute thresholds... |
86 | $result = NPTest->testCmd( | 108 | $result = NPTest->testCmd( |
87 | "./check_disk -w 20 -c 10 -p $mountpoint_valid" | 109 | "./check_disk -w 20 -c 10 -p $mountpoint_valid $output_format" |
88 | ); | 110 | ); |
89 | $_ = $result->perf_output; | 111 | |
90 | my ($warn_absth_data, $crit_absth_data, $total_absth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/); | 112 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); |
91 | # default unit is MiB, but perfdata is always bytes | 113 | |
92 | is ($warn_absth_data, $total_absth_data - (20 * (2 ** 20)), "Wrong warning in perf data using absolute thresholds"); | 114 | my $warn_absth_data = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}[0]->{'perfdata'}->[0]->{'warn'}->{'end'}->{'value'}; |
93 | is ($crit_absth_data, $total_absth_data - (10 * (2 ** 20)), "Wrong critical in perf data using absolute thresholds"); | 115 | my $crit_absth_data = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}[0]->{'perfdata'}->[0]->{'crit'}->{'end'}->{'value'}; |
116 | my $total_absth_data= $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}[0]->{'perfdata'}->[0]->{'max'}->{'value'}; | ||
117 | |||
118 | # print("warn: " .$warn_absth_data . "\n"); | ||
119 | # print("crit: " .$crit_absth_data . "\n"); | ||
120 | # print("total: " .$total_absth_data . "\n"); | ||
121 | |||
122 | is ($warn_absth_data <=> (20 * (2 ** 20)), 0, "Wrong warning in perf data using absolute thresholds"); | ||
123 | is ($crit_absth_data <=> (10 * (2 ** 20)), 0, "Wrong critical in perf data using absolute thresholds"); | ||
94 | 124 | ||
95 | # Then check percent thresholds. | 125 | # Then check percent thresholds. |
96 | $result = NPTest->testCmd( | 126 | $result = NPTest->testCmd( |
97 | "./check_disk -w 20% -c 10% -p $mountpoint_valid" | 127 | "./check_disk -w 20% -c 10% -p $mountpoint_valid $output_format" |
98 | ); | 128 | ); |
99 | $_ = $result->perf_output; | 129 | |
100 | my ($warn_percth_data, $crit_percth_data, $total_percth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/); | 130 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); |
101 | is ($warn_percth_data, int((1-20/100)*$total_percth_data), "Wrong warning in perf data using percent thresholds"); | 131 | |
102 | is ($crit_percth_data, int((1-10/100)*$total_percth_data), "Wrong critical in perf data using percent thresholds"); | 132 | my $warn_percth_data = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}[0]->{'perfdata'}->[0]->{'warn'}->{'end'}->{'value'}; |
133 | my $crit_percth_data = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}[0]->{'perfdata'}->[0]->{'crit'}->{'end'}->{'value'}; | ||
134 | my $total_percth_data = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}[0]->{'perfdata'}->[0]->{'max'}->{'value'}; | ||
135 | |||
136 | print("warn_percth_data: " . $warn_percth_data . "\n"); | ||
137 | print("crit_percth_data: " . $crit_percth_data . "\n"); | ||
138 | |||
139 | is (int($warn_percth_data), int((20/100)*$total_percth_data), "Wrong warning in perf data using percent thresholds. Got " . $warn_percth_data . " with total " . $total_percth_data); | ||
140 | is (int($crit_percth_data), int((10/100)*$total_percth_data), "Wrong critical in perf data using percent thresholds. Got " . $crit_percth_data . " with total " . $total_percth_data); | ||
103 | 141 | ||
104 | 142 | ||
105 | # Check when order of mount points are reversed, that perf data remains same | 143 | # Check when order of mount points are reversed, that perf data remains same |
106 | $result = NPTest->testCmd( | 144 | $result = NPTest->testCmd( |
107 | "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid" | 145 | "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid $output_format" |
108 | ); | 146 | ); |
109 | @_ = sort(split(/ /, $result->perf_output)); | 147 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); |
110 | is_deeply( \@perf_data, \@_, "perf data for both filesystems same when reversed"); | ||
111 | 148 | ||
149 | # write comparison set for perfdata here, but in reversed order, maybe there is a smarter way | ||
150 | my @perfdata2; | ||
151 | @perfdata2[0] = $result->{'mp_test_result'}->{'checks'}->[1]->{'checks'}->[0]->{'perfdata'}->[0]; | ||
152 | @perfdata2[1] = $result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}->[0]->{'perfdata'}->[0]; | ||
153 | # Decrease precision of numbers since the the fs might be modified between the two runs | ||
154 | $perfdata2[0]->{'value'}->{'value'} = int($perfdata2[0]->{'value'}->{'value'} / 1000000); | ||
155 | $perfdata2[1]->{'value'}->{'value'} = int($perfdata2[1]->{'value'}->{'value'} / 1000000); | ||
156 | is_deeply(\@perfdata, \@perfdata2, "perf data for both filesystems same when reversed"); | ||
112 | 157 | ||
113 | # Basic filesystem checks for sizes | 158 | # Basic filesystem checks for sizes |
114 | $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" ); | 159 | $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free $output_format"); |
115 | cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free"); | 160 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); |
116 | like ( $result->output, $successOutput, "OK output" ); | 161 | like($result->{'mp_test_result'}->{'state'}, "/OK/", "At least 1 MB available on $more_free"); |
117 | like ( $result->only_output, qr/free space/, "Have free space text"); | ||
118 | like ( $result->only_output, qr/$more_free/, "Have disk name in text"); | ||
119 | 162 | ||
120 | $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" ); | 163 | $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free $output_format" ); |
121 | cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free"); | 164 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); |
165 | like($result->{'mp_test_result'}->{'state'}, "/OK/", "At least 1 MB available on $more_free and $less_free"); | ||
122 | 166 | ||
123 | $_ = $result->output; | 167 | my $free_mb_on_mp1 =$result->{'mp_test_result'}->{'checks'}->[0]->{'checks'}->[0]->{'perfdata'}->[0]->{'value'}->{'value'} / (1024 * 1024); |
124 | 168 | my $free_mb_on_mp2 = $result->{'mp_test_result'}->{'checks'}->[1]->{'checks'}->[0]->{'perfdata'}->[0]->{'value'}->{'value'}/ (1024 * 1024); | |
125 | my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+)MiB .* (\d+)MiB /g); | ||
126 | die "Cannot parse output: $_" unless ($free_mb_on_mp1 && $free_mb_on_mp2); | 169 | die "Cannot parse output: $_" unless ($free_mb_on_mp1 && $free_mb_on_mp2); |
127 | 170 | ||
128 | my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2; | 171 | my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2; |
129 | 172 | ||
130 | 173 | ||
174 | $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free $output_format" ); | ||
175 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); | ||
131 | 176 | ||
132 | $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" ); | 177 | $result = NPTest->testCmd( "./check_disk 101 101 $more_free" ); |
133 | is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs"); | 178 | like($result->output, "/OK/", "OK in Output"); |
134 | 179 | cmp_ok( $result->return_code, '==', 0, "Old syntax okay, output was: ". $result->output . "\n" ); | |
135 | $result = NPTest->testCmd( "./check_disk 100 100 $more_free" ); | ||
136 | cmp_ok( $result->return_code, '==', 0, "Old syntax okay" ); | ||
137 | 180 | ||
138 | $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" ); | 181 | $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" ); |
139 | cmp_ok( $result->return_code, "==", 0, "At least 1% free" ); | 182 | cmp_ok( $result->return_code, "==", 0, "At least 1% free" ); |
140 | 183 | ||
141 | $result = NPTest->testCmd( | 184 | $result = NPTest->testCmd( |
142 | "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" | 185 | "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free $output_format" |
143 | ); | 186 | ); |
144 | cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" ); | 187 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); |
145 | like( $result->output, $failureOutput, "Right output" ); | 188 | like($result->{'mp_test_result'}->{'state'}, "/CRITICAL/", "Get critical on less_free mountpoint $less_free"); |
146 | 189 | ||
147 | 190 | ||
148 | $result = NPTest->testCmd( | 191 | $result = NPTest->testCmd( |
149 | "./check_disk -w $avg_free% -c 0% -p $less_free" | 192 | "./check_disk -w $avg_free_percent% -c 0% -p $less_free $output_format" |
150 | ); | 193 | ); |
151 | cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free"); | 194 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); |
195 | like($result->{'mp_test_result'}->{'state'}, "/WARNING/", "Get warning on less_free mountpoint, when checking avg_free"); | ||
152 | 196 | ||
153 | $result = NPTest->testCmd( | 197 | $result = NPTest->testCmd( |
154 | "./check_disk -w $avg_free% -c $avg_free% -p $more_free" | 198 | "./check_disk -w $avg_free_percent% -c $avg_free_percent% -p $more_free" |
155 | ); | 199 | ); |
156 | cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free"); | 200 | cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free"); |
157 | 201 | ||
158 | $result = NPTest->testCmd( | 202 | $result = NPTest->testCmd( |
159 | "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" | 203 | "./check_disk -w $avg_free_percent% -c 0% -p $less_free -w $avg_free_percent% -c $avg_free_percent% -p $more_free" |
160 | ); | 204 | ); |
161 | cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning"); | 205 | cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning"); |
162 | my $all_disks = $result->output; | 206 | my $all_disks = $result->output; |
163 | 207 | ||
164 | $result = NPTest->testCmd( | 208 | $result = NPTest->testCmd( |
165 | "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" | 209 | "./check_disk -e -w $avg_free_percent% -c 0% -p $less_free -w $avg_free_percent% -c $avg_free_percent% -p $more_free" |
166 | ); | 210 | ); |
167 | isnt( $result->output, $all_disks, "-e gives different output"); | 211 | isnt( $result->output, $all_disks, "-e gives different output"); |
168 | 212 | ||
169 | # Need spaces around filesystem name in case less_free and more_free are nested | 213 | # Need spaces around filesystem name in case less_free and more_free are nested |
170 | like( $result->output, qr/ $less_free /, "Found problem $less_free"); | 214 | like( $result->output, qr/ $less_free /, "Found problem $less_free"); |
171 | unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem"); | 215 | unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem"); |
172 | like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data"); | 216 | like( $result->perf_output, qr/'$more_free'=/, "But $more_free is still in perf data"); |
173 | 217 | ||
174 | $result = NPTest->testCmd( | 218 | $result = NPTest->testCmd( |
175 | "./check_disk -w $avg_free% -c 0% -p $more_free" | 219 | "./check_disk -w $avg_free_percent% -c 0% -p $more_free" |
176 | ); | 220 | ); |
177 | cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free"); | 221 | cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free"); |
178 | 222 | ||
179 | $result = NPTest->testCmd( | 223 | $result = NPTest->testCmd( |
180 | "./check_disk -w $avg_free% -c $avg_free% -p $less_free" | 224 | "./check_disk -w $avg_free_percent% -c $avg_free_percent% -p $less_free" |
181 | ); | 225 | ); |
182 | cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free"); | 226 | cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free"); |
183 | $result = NPTest->testCmd( | 227 | $result = NPTest->testCmd( |
184 | "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free" | 228 | "./check_disk -w $avg_free_percent% -c 0% -p $more_free -w $avg_free_percent% -c $avg_free_percent% -p $less_free" |
185 | ); | 229 | ); |
186 | cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical"); | 230 | cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical"); |
187 | 231 | ||
188 | $result = NPTest->testCmd( | 232 | $result = NPTest->testCmd( |
189 | "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free" | 233 | "./check_disk -w $avg_free_percent% -c $avg_free_percent% -p $less_free -w $avg_free_percent% -c 0% -p $more_free" |
190 | ); | 234 | ); |
191 | cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference"); | 235 | cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference"); |
192 | 236 | ||
@@ -203,32 +247,32 @@ is( $result->return_code, 2, "Critical requesting 100% free inodes for both moun | |||
203 | $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" ); | 247 | $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" ); |
204 | is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free"); | 248 | is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free"); |
205 | 249 | ||
206 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" ); | 250 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free_percentage% -K 0% -p $less_inode_free" ); |
207 | is( $result->return_code, 1, "Get warning on less_inode_free, when checking average"); | 251 | is( $result->return_code, 1, "Get warning on less_inode_free, when checking average"); |
208 | 252 | ||
209 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free "); | 253 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free_percentage% -K $avg_inode_free_percentage% -p $more_inode_free "); |
210 | is( $result->return_code, 0, "Get ok on more_inode_free when checking average"); | 254 | is( $result->return_code, 0, "Get ok on more_inode_free when checking average"); |
211 | 255 | ||
212 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free" ); | 256 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free_percentage% -K 0% -p $less_inode_free -W $avg_inode_free_percentage% -K $avg_inode_free_percentage% -p $more_inode_free" ); |
213 | is ($result->return_code, 1, "Combine above two tests, get warning"); | 257 | is ($result->return_code, 1, "Combine above two tests, get warning"); |
214 | $all_disks = $result->output; | 258 | $all_disks = $result->output; |
215 | 259 | ||
216 | $result = NPTest->testCmd( "./check_disk -e -W $avg_inode_free% -K 0% -p $less_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free" ); | 260 | $result = NPTest->testCmd( "./check_disk -e -W $avg_inode_free_percentage% -K 0% -p $less_inode_free -W $avg_inode_free_percentage% -K $avg_inode_free_percentage% -p $more_inode_free" ); |
217 | isnt( $result->output, $all_disks, "-e gives different output"); | 261 | isnt( $result->output, $all_disks, "-e gives different output"); |
218 | like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free"); | 262 | like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free"); |
219 | unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem"); | 263 | unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem"); |
220 | like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data"); | 264 | like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data"); |
221 | 265 | ||
222 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" ); | 266 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free_percentage% -K 0% -p $more_inode_free" ); |
223 | is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average"); | 267 | is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average"); |
224 | 268 | ||
225 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" ); | 269 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free_percentage% -K $avg_inode_free_percentage% -p $less_inode_free" ); |
226 | is( $result->return_code, 2, "Get critical on less_inode_free, checking average"); | 270 | is( $result->return_code, 2, "Get critical on less_inode_free, checking average"); |
227 | 271 | ||
228 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" ); | 272 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free_percentage% -K 0% -p $more_inode_free -W $avg_inode_free_percentage% -K $avg_inode_free_percentage% -p $less_inode_free" ); |
229 | is( $result->return_code, 2, "Combining above two tests, get critical"); | 273 | is( $result->return_code, 2, "Combining above two tests, get critical"); |
230 | 274 | ||
231 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free -W $avg_inode_free% -K 0% -p $more_inode_free" ); | 275 | $result = NPTest->testCmd( "./check_disk -W $avg_inode_free_percentage% -K $avg_inode_free_percentage% -p $less_inode_free -W $avg_inode_free_percentage% -K 0% -p $more_inode_free" ); |
232 | cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference"); | 276 | cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference"); |
233 | 277 | ||
234 | 278 | ||
@@ -249,9 +293,9 @@ $result = NPTest->testCmd( | |||
249 | ); | 293 | ); |
250 | cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" ); | 294 | cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" ); |
251 | 295 | ||
252 | $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty | 296 | $result = NPTest->testCmd( "./check_disk -w 100% -c 100% $output_format ".${mountpoint_valid} ); # 100% empty |
253 | cmp_ok( $result->return_code, "==", 2, "100% empty" ); | 297 | cmp_ok( $result->return_code, "==", 0, "100% empty" ); |
254 | like( $result->output, $failureOutput, "Right output" ); | 298 | like($result->{'mp_test_result'}->{'state'}, "/CRITICAL/", "100% empty"); |
255 | 299 | ||
256 | $result = NPTest->testCmd( "./check_disk -w 100000000 -c 100000000 $mountpoint_valid" ); | 300 | $result = NPTest->testCmd( "./check_disk -w 100000000 -c 100000000 $mountpoint_valid" ); |
257 | cmp_ok( $result->return_code, '==', 2, "Check for 100TB free" ); | 301 | cmp_ok( $result->return_code, '==', 2, "Check for 100TB free" ); |
@@ -263,7 +307,8 @@ cmp_ok( $result->return_code, "==", 2, "100 TB empty" ); | |||
263 | # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds | 307 | # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds |
264 | $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} ); | 308 | $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} ); |
265 | cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used"); | 309 | cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used"); |
266 | like ( $result->only_output, qr(^[^;]*;[^;]*$), "Select only one path with positional arguments"); | 310 | # like ( $result->only_output, qr(^[^;]*;[^;]*$), "Select only one path with positional arguments"); |
311 | # TODO not sure what the above should test, taking it out | ||
267 | 312 | ||
268 | $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" ); | 313 | $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" ); |
269 | cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" ); | 314 | cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" ); |
@@ -311,8 +356,9 @@ $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" ); | |||
311 | unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice"); | 356 | unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice"); |
312 | 357 | ||
313 | # are partitions added if -C is given without path selection -p ? | 358 | # are partitions added if -C is given without path selection -p ? |
314 | $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid" ); | 359 | $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid $output_format" ); |
315 | like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given"); | 360 | cmp_ok( $result->return_code, "==", 0, "with JSON test format result should always be OK"); |
361 | cmp_ok(scalar $result->{'mp_test_result'}->{'checks'}, '>', 1, "-C invokes matchall logic again"); | ||
316 | 362 | ||
317 | # grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit | 363 | # grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit |
318 | $result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all + 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" ); | 364 | $result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all + 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" ); |
@@ -359,39 +405,37 @@ like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mo | |||
359 | # ignore-missing: exit okay, when fs is not accessible | 405 | # ignore-missing: exit okay, when fs is not accessible |
360 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p /bob"); | 406 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p /bob"); |
361 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for not existing filesystem /bob"); | 407 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for not existing filesystem /bob"); |
362 | like( $result->output, '/^DISK OK - No disks were found for provided parameters - ignored paths: /bob;.*$/', 'Output OK'); | 408 | like( $result->output, '/No filesystems were found for the provided parameters.*$/', 'Output OK'); |
363 | 409 | ||
364 | # ignore-missing: exit okay, when regex does not match | 410 | # ignore-missing: exit okay, when regex does not match |
365 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r /bob"); | 411 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r /bob"); |
366 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); | 412 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); |
367 | like( $result->output, '/^DISK OK - No disks were found for provided parameters.*$/', 'Output OK'); | 413 | like( $result->output, '/No filesystems were found for the provided parameters.*$/', 'Output OK'); |
368 | 414 | ||
369 | # ignore-missing: exit okay, when fs with exact match (-E) is not found | 415 | # ignore-missing: exit okay, when fs with exact match (-E) is not found |
370 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -E -p /etc"); | 416 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -E -p /etc"); |
371 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay when exact match does not find fs"); | 417 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay when exact match does not find fs"); |
372 | like( $result->output, '/^DISK OK - No disks were found for provided parameters - ignored paths: /etc;.*$/', 'Output OK'); | 418 | like( $result->output, '/No filesystems were found for the provided parameters.*$/', 'Output OK'); |
373 | 419 | ||
374 | # ignore-missing: exit okay, when checking one existing fs and one non-existing fs (regex) | 420 | # ignore-missing: exit okay, when checking one existing fs and one non-existing fs (regex) |
375 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r '/bob' -r '^/\$'"); | 421 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r '/bob' -r '^/\$'"); |
376 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); | 422 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); |
377 | like( $result->output, '/^DISK OK - free space: \/ .*$/', 'Output OK'); | ||
378 | 423 | ||
379 | # ignore-missing: exit okay, when checking one existing fs and one non-existing fs (path) | 424 | # ignore-missing: exit okay, when checking one existing fs and one non-existing fs (path) |
380 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p '/bob' -p '/'"); | 425 | $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p '/bob' -p '/'"); |
381 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); | 426 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); |
382 | like( $result->output, '/^DISK OK - free space: / .*; - ignored paths: /bob;.*$/', 'Output OK'); | 427 | # like( $result->output, '/^DISK OK - free space: / .*; - ignored paths: /bob;.*$/', 'Output OK'); |
383 | 428 | ||
384 | # ignore-missing: exit okay, when checking one non-existing fs (path) and one ignored | 429 | # ignore-missing: exit okay, when checking one non-existing fs (path) and one ignored |
385 | $result = NPTest->testCmd( "./check_disk -n -w 0% -c 0% -r /dummy -i /dummy2"); | 430 | $result = NPTest->testCmd( "./check_disk -n -w 0% -c 0% -r /dummy -i /dummy2"); |
386 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); | 431 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); |
387 | like( $result->output, '/^DISK OK - No disks were found for provided parameters\|$/', 'Output OK'); | 432 | like( $result->output, '/No filesystems were found for the provided parameters.*$/', 'Output OK'); |
388 | 433 | ||
389 | # ignore-missing: exit okay, when regex match does not find anything | 434 | # ignore-missing: exit okay, when regex match does not find anything |
390 | $result = NPTest->testCmd( "./check_disk -n -e -l -w 10% -c 5% -W 10% -K 5% -r /dummy"); | 435 | $result = NPTest->testCmd( "./check_disk -n -e -l -w 10% -c 5% -W 10% -K 5% -r /dummy"); |
391 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); | 436 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); |
392 | like( $result->output, '/^DISK OK\|$/', 'Output OK'); | ||
393 | 437 | ||
394 | # ignore-missing: exit okay, when regex match does not find anything | 438 | # ignore-missing: exit okay, when regex match does not find anything |
395 | $result = NPTest->testCmd( "./check_disk -n -l -w 10% -c 5% -W 10% -K 5% -r /dummy"); | 439 | $result = NPTest->testCmd( "./check_disk -n -l -w 10% -c 5% -W 10% -K 5% -r /dummy"); |
396 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); | 440 | cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); |
397 | like( $result->output, '/^DISK OK - No disks were found for provided parameters\|$/', 'Output OK'); | 441 | like( $result->output, '/No filesystems were found for the provided parameters.*$/', 'Output OK'); |
diff --git a/plugins/t/check_load.t b/plugins/t/check_load.t index bba8947c..fc26bb35 100644 --- a/plugins/t/check_load.t +++ b/plugins/t/check_load.t | |||
@@ -16,28 +16,28 @@ my $successScaledOutput = "/^LOAD OK - scaled load average: $loadValue, $loadVal | |||
16 | my $failureOutput = "/^LOAD CRITICAL - total load average: $loadValue, $loadValue, $loadValue/"; | 16 | my $failureOutput = "/^LOAD CRITICAL - total load average: $loadValue, $loadValue, $loadValue/"; |
17 | my $failurScaledOutput = "/^LOAD CRITICAL - scaled load average: $loadValue, $loadValue, $loadValue - total load average: $loadValue, $loadValue, $loadValue/"; | 17 | my $failurScaledOutput = "/^LOAD CRITICAL - scaled load average: $loadValue, $loadValue, $loadValue - total load average: $loadValue, $loadValue, $loadValue/"; |
18 | 18 | ||
19 | plan tests => 13; | 19 | plan tests => 8; |
20 | 20 | ||
21 | $res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100" ); | 21 | $res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100" ); |
22 | cmp_ok( $res->return_code, 'eq', 0, "load not over 100"); | 22 | cmp_ok( $res->return_code, 'eq', 0, "load not over 100"); |
23 | like( $res->output, $successOutput, "Output OK"); | 23 | # like( $res->output, $successOutput, "Output OK"); |
24 | 24 | ||
25 | $res = NPTest->testCmd( "./check_load -w 0,0,0 -c 0,0,0" ); | 25 | $res = NPTest->testCmd( "./check_load -w 0,0,0 -c 0,0,0" ); |
26 | cmp_ok( $res->return_code, 'eq', 2, "Load over 0"); | 26 | cmp_ok( $res->return_code, 'eq', 2, "Load over 0"); |
27 | like( $res->output, $failureOutput, "Output OK"); | 27 | # like( $res->output, $failureOutput, "Output OK"); |
28 | 28 | ||
29 | $res = NPTest->testCmd( "./check_load -r -w 0,0,0 -c 0,0,0" ); | 29 | $res = NPTest->testCmd( "./check_load -r -w 0,0,0 -c 0,0,0" ); |
30 | cmp_ok( $res->return_code, 'eq', 2, "Load over 0 with per cpu division"); | 30 | cmp_ok( $res->return_code, 'eq', 2, "Load over 0 with per cpu division"); |
31 | like( $res->output, $failurScaledOutput, "Output OK"); | 31 | # like( $res->output, $failurScaledOutput, "Output OK"); |
32 | 32 | ||
33 | $res = NPTest->testCmd( "./check_load -w 100 -c 100,110" ); | 33 | $res = NPTest->testCmd( "./check_load -w 100 -c 100,110" ); |
34 | cmp_ok( $res->return_code, 'eq', 0, "Plugin can handle non-triplet-arguments"); | 34 | cmp_ok( $res->return_code, 'eq', 0, "Plugin can handle non-triplet-arguments"); |
35 | like( $res->output, $successOutput, "Output OK"); | 35 | # like( $res->output, $successOutput, "Output OK"); |
36 | like( $res->perf_output, "/load1=$loadValue;100.000;100.000/", "Test handling of non triplet thresholds (load1)"); | 36 | like( $res->perf_output, "/'load1'=$loadValue;~:100.0+;~:100.0+/", "Test handling of non triplet thresholds (load1)"); |
37 | like( $res->perf_output, "/load5=$loadValue;100.000;110.000/", "Test handling of non triplet thresholds (load5)"); | 37 | like( $res->perf_output, "/'load5'=$loadValue;~:100.0+;~:110.0+/", "Test handling of non triplet thresholds (load5)"); |
38 | like( $res->perf_output, "/load15=$loadValue;100.000;110.000/", "Test handling of non triplet thresholds (load15)"); | 38 | like( $res->perf_output, "/'load15'=$loadValue;~:100.0+;~:110.0+/", "Test handling of non triplet thresholds (load15)"); |
39 | 39 | ||
40 | 40 | ||
41 | $res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100 -r" ); | 41 | $res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100 -r" ); |
42 | cmp_ok( $res->return_code, 'eq', 0, "load not over 100"); | 42 | cmp_ok( $res->return_code, 'eq', 0, "load not over 100"); |
43 | like( $res->output, $successScaledOutput, "Output OK"); | 43 | # like( $res->output, $successScaledOutput, "Output OK"); |
diff --git a/plugins/t/check_snmp.t b/plugins/t/check_snmp.t index 576cc506..8d435df3 100644 --- a/plugins/t/check_snmp.t +++ b/plugins/t/check_snmp.t | |||
@@ -10,7 +10,7 @@ use NPTest; | |||
10 | 10 | ||
11 | BEGIN { | 11 | BEGIN { |
12 | plan skip_all => 'check_snmp is not compiled' unless -x "./check_snmp"; | 12 | plan skip_all => 'check_snmp is not compiled' unless -x "./check_snmp"; |
13 | plan tests => 63; | 13 | plan tests => 62; |
14 | } | 14 | } |
15 | 15 | ||
16 | my $res; | 16 | my $res; |
@@ -24,7 +24,7 @@ my $user_snmp = getTestParameter("NP_SNMP_USER", "An SNMP user", "auth_ | |||
24 | 24 | ||
25 | $res = NPTest->testCmd( "./check_snmp -t 1" ); | 25 | $res = NPTest->testCmd( "./check_snmp -t 1" ); |
26 | is( $res->return_code, 3, "No host name" ); | 26 | is( $res->return_code, 3, "No host name" ); |
27 | is( $res->output, "No host specified" ); | 27 | is( $res->output, "No OIDs specified" ); |
28 | 28 | ||
29 | $res = NPTest->testCmd( "./check_snmp -H fakehostname --ignore-mib-parsing-errors" ); | 29 | $res = NPTest->testCmd( "./check_snmp -H fakehostname --ignore-mib-parsing-errors" ); |
30 | is( $res->return_code, 3, "No OIDs specified" ); | 30 | is( $res->return_code, 3, "No OIDs specified" ); |
@@ -32,145 +32,124 @@ is( $res->output, "No OIDs specified" ); | |||
32 | 32 | ||
33 | $res = NPTest->testCmd( "./check_snmp -H fakehost --ignore-mib-parsing-errors -o oids -P 3 -U not_a_user --seclevel=rubbish" ); | 33 | $res = NPTest->testCmd( "./check_snmp -H fakehost --ignore-mib-parsing-errors -o oids -P 3 -U not_a_user --seclevel=rubbish" ); |
34 | is( $res->return_code, 3, "Invalid seclevel" ); | 34 | is( $res->return_code, 3, "Invalid seclevel" ); |
35 | like( $res->output, "/check_snmp: Invalid seclevel - rubbish/" ); | 35 | like( $res->output, "/invalid security level: rubbish/" ); |
36 | 36 | ||
37 | $res = NPTest->testCmd( "./check_snmp -H fakehost --ignore-mib-parsing-errors -o oids -P 3c" ); | 37 | $res = NPTest->testCmd( "./check_snmp -H fakehost --ignore-mib-parsing-errors -o oids -P 3c" ); |
38 | is( $res->return_code, 3, "Invalid protocol" ); | 38 | is( $res->return_code, 3, "Invalid protocol" ); |
39 | like( $res->output, "/check_snmp: Invalid SNMP version - 3c/" ); | 39 | like( $res->output, "/invalid SNMP version/protocol: 3c/" ); |
40 | 40 | ||
41 | SKIP: { | 41 | SKIP: { |
42 | skip "no snmp host defined", 50 if ( ! $host_snmp ); | 42 | skip "no snmp host defined", 50 if ( ! $host_snmp ); |
43 | 43 | ||
44 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); | 44 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -P 2c -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); |
45 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying uptime" ); | 45 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying uptime" ); |
46 | like($res->output, '/^SNMP OK - (\d+)/', "String contains SNMP OK"); | 46 | $res->output =~ /\|.*=(\d+);/; |
47 | $res->output =~ /^SNMP OK - (\d+)/; | ||
48 | my $value = $1; | 47 | my $value = $1; |
49 | cmp_ok( $value, ">", 0, "Got a time value" ); | 48 | cmp_ok( $value, ">", 0, "Got a time value" ); |
50 | like($res->perf_output, "/sysUpTime.*$1/", "Got perfdata with value '$1' in it"); | 49 | like($res->perf_output, "/sysUpTime.*$1/", "Got perfdata with value '$1' in it"); |
51 | 50 | ||
52 | 51 | ||
53 | # some more threshold tests | 52 | # some more threshold tests |
54 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1"); | 53 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1 -P 2c"); |
55 | cmp_ok( $res->return_code, '==', 2, "Threshold test -c 1" ); | 54 | cmp_ok( $res->return_code, '==', 2, "Threshold test -c 1" ); |
56 | 55 | ||
57 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1:"); | 56 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1: -P 2c"); |
58 | cmp_ok( $res->return_code, '==', 0, "Threshold test -c 1:" ); | 57 | cmp_ok( $res->return_code, '==', 0, "Threshold test -c 1:" ); |
59 | 58 | ||
60 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c ~:1"); | 59 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c ~:1 -P 2c"); |
61 | cmp_ok( $res->return_code, '==', 2, "Threshold test -c ~:1" ); | 60 | cmp_ok( $res->return_code, '==', 2, "Threshold test -c ~:1" ); |
62 | 61 | ||
63 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1:10"); | 62 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1:10 -P 2c"); |
64 | cmp_ok( $res->return_code, '==', 2, "Threshold test -c 1:10" ); | 63 | cmp_ok( $res->return_code, '==', 2, "Threshold test -c 1:10" ); |
65 | 64 | ||
66 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c \@1:10"); | 65 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c \@1:10 -P 2c"); |
67 | cmp_ok( $res->return_code, '==', 0, "Threshold test -c \@1:10" ); | 66 | cmp_ok( $res->return_code, '==', 0, "Threshold test -c \@1:10" ); |
68 | 67 | ||
69 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 10:1"); | ||
70 | cmp_ok( $res->return_code, '==', 0, "Threshold test -c 10:1" ); | ||
71 | |||
72 | 68 | ||
73 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o .1.3.6.1.2.1.1.3.0 -w 1: -c 1:"); | 69 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o .1.3.6.1.2.1.1.3.0 -w 1: -c 1: -P 2c"); |
74 | cmp_ok( $res->return_code, '==', 0, "Test with numeric OID (no mibs loaded)" ); | 70 | cmp_ok( $res->return_code, '==', 0, "Test with numeric OID (no mibs loaded)" ); |
75 | like($res->output, '/^SNMP OK - \d+/', "String contains SNMP OK"); | ||
76 | 71 | ||
77 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0"); | 72 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0 -P 2c"); |
78 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying sysDescr" ); | 73 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying sysDescr" ); |
79 | unlike($res->perf_output, '/sysDescr/', "Perfdata doesn't contain string values"); | 74 | unlike($res->perf_output, '/sysDescr/', "Perfdata doesn't contain string values"); |
80 | 75 | ||
81 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0,system.sysDescr.0"); | 76 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0,system.sysDescr.0 -P 2c"); |
82 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying two string OIDs, comma-separated" ); | 77 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying two string OIDs, comma-separated" ); |
83 | like($res->output, '/^SNMP OK - /', "String contains SNMP OK"); | ||
84 | 78 | ||
85 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0 -o system.sysDescr.0"); | 79 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0 -o system.sysDescr.0 -P 2c"); |
86 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying two string OIDs, repeated option" ); | 80 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying two string OIDs, repeated option" ); |
87 | like($res->output, '/^SNMP OK - /', "String contains SNMP OK"); | ||
88 | 81 | ||
89 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 1:1 -c 1:1"); | 82 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 1:1 -c 1:1 -P 2c"); |
90 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrSWRunIndex.1" ); | 83 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrSWRunIndex.1" ); |
91 | like($res->output, '/^SNMP OK - 1\s.*$/', "String fits SNMP OK and output format"); | ||
92 | 84 | ||
93 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 0 -c 1:"); | 85 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 0 -c 1: -P 2c"); |
94 | cmp_ok( $res->return_code, '==', 1, "Exit WARNING when querying hrSWRunIndex.1 and warn-th doesn't apply " ); | 86 | cmp_ok( $res->return_code, '==', 1, "Exit WARNING when querying hrSWRunIndex.1 and warn-th doesn't apply " ); |
95 | like($res->output, '/^SNMP WARNING - \*1\*\s.*$/', "String matches SNMP WARNING and output format"); | ||
96 | 87 | ||
97 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0"); | 88 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0 -P 2c"); |
98 | cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL when querying hrSWRunIndex.1 and crit-th doesn't apply" ); | 89 | cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL when querying hrSWRunIndex.1 and crit-th doesn't apply" ); |
99 | like($res->output, '/^SNMP CRITICAL - \*1\*\s.*$/', "String matches SNMP CRITICAL and output format"); | ||
100 | 90 | ||
101 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2 -c 1:2"); | 91 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2 -c 1:2 -P 2c"); |
102 | cmp_ok( $res->return_code, '==', 0, "Checking two OIDs at once" ); | 92 | cmp_ok( $res->return_code, '==', 0, "Checking two OIDs at once" ); |
103 | like($res->output, "/^SNMP OK - 2 1/", "Got two values back" ); | 93 | like( $res->perf_output, "/ifIndex.2'?=2/", "Got 1st perf data" ); |
104 | like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" ); | 94 | like( $res->perf_output, "/ifIndex.1'?=1/", "Got 2nd perf data" ); |
105 | like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" ); | ||
106 | 95 | ||
107 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2,1:2 -c 2:2,2:2"); | 96 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2,1:2 -c 2:2,2:2 -P 2c"); |
108 | cmp_ok( $res->return_code, '==', 2, "Checking critical threshold is passed if any one value crosses" ); | 97 | cmp_ok( $res->return_code, '==', 2, "Checking critical threshold is passed if any one value crosses" ); |
109 | like($res->output, "/^SNMP CRITICAL - 2 *1*/", "Got two values back" ); | 98 | like( $res->perf_output, "/ifIndex.2'?=2/", "Got 1st perf data" ); |
110 | like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" ); | 99 | like( $res->perf_output, "/ifIndex.1'?=1/", "Got 2nd perf data" ); |
111 | like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" ); | ||
112 | 100 | ||
113 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w 1:,1: -c 1:,1:"); | 101 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w 1:,1: -c 1:,1: -P 2c"); |
114 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrMemorySize and hrSystemProcesses"); | 102 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrMemorySize and hrSystemProcesses"); |
115 | like($res->output, '/^SNMP OK - \d+ \d+/', "String contains hrMemorySize and hrSystemProcesses"); | ||
116 | 103 | ||
117 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w \@:0 -c \@0"); | 104 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w \@:0 -c \@0 -P 2c"); |
118 | cmp_ok( $res->return_code, '==', 0, "Exit OK with inside-range thresholds"); | 105 | cmp_ok( $res->return_code, '==', 0, "Exit OK with inside-range thresholds"); |
119 | like($res->output, '/^SNMP OK - 1\s.*$/', "String matches SNMP OK and output format"); | ||
120 | 106 | ||
121 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoad.3"); | 107 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoadInt.3 -P 2c"); |
122 | $res->output =~ m/^SNMP OK - (\d+\.\d{2})\s.*$/; | 108 | $res->output =~ m/^.*Value: (\d+).*$/; |
123 | my $lower = $1 - 0.05; | 109 | my $lower = $1 - 0.05; |
124 | my $higher = $1 + 0.05; | 110 | my $higher = $1 + 0.05; |
125 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoad.3 -w $lower -c $higher"); | 111 | # $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoadInt.3 -w $lower -c $higher -P 2c"); |
126 | cmp_ok( $res->return_code, '==', 1, "Exit WARNING with fractionnal arguments"); | 112 | # cmp_ok( $res->return_code, '==', 1, "Exit WARNING with fractional arguments"); |
127 | 113 | ||
128 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0,host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w ,:0 -c ,:2"); | 114 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0,host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w ,:0 -c ,:2 -P 2c"); |
129 | cmp_ok( $res->return_code, '==', 1, "Exit WARNING on 2nd threshold"); | 115 | cmp_ok( $res->return_code, '==', 1, "Exit WARNING on 2nd threshold"); |
130 | like($res->output, '/^SNMP WARNING - Timeticks:\s\(\d+\)\s+(?:\d+ days?,\s+)?\d+:\d+:\d+\.\d+\s+\*1\*\s.*$/', "First OID returned as string, 2nd checked for thresholds"); | ||
131 | 116 | ||
132 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w '' -c ''"); | 117 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w '' -c '' -P 2c"); |
133 | cmp_ok( $res->return_code, '==', 0, "Empty thresholds doesn't crash"); | 118 | cmp_ok( $res->return_code, '==', 0, "Empty thresholds doesn't crash"); |
134 | 119 | ||
135 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,,1 -c ,,2"); | 120 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,,1 -c ,,2 -P 2c"); |
136 | cmp_ok( $res->return_code, '==', 0, "Skipping first two thresholds on 2 OID check"); | 121 | cmp_ok( $res->return_code, '==', 0, "Skipping first two thresholds on 2 OID check"); |
137 | like($res->output, '/^SNMP OK - \d+ \w+ \d+\s.*$/', "Skipping first two thresholds, result printed rather than parsed"); | ||
138 | 122 | ||
139 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,, -c ,,"); | 123 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,, -c ,, -P 2c"); |
140 | cmp_ok( $res->return_code, '==', 0, "Skipping all thresholds"); | 124 | cmp_ok( $res->return_code, '==', 0, "Skipping all thresholds"); |
141 | like($res->output, '/^SNMP OK - \d+ \w+ \d+\s.*$/', "Skipping all thresholds, result printed rather than parsed"); | ||
142 | 125 | ||
143 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1000000000000: -u '1/100 sec'"); | 126 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1000000000000: -u '1/100 sec' -P 2c"); |
144 | cmp_ok( $res->return_code, '==', 2, "Timetick used as a threshold"); | 127 | cmp_ok( $res->return_code, '==', 2, "Timetick used as a threshold"); |
145 | like($res->output, '/^SNMP CRITICAL - \*\d+\* 1\/100 sec.*$/', "Timetick used as a threshold, parsed as numeric"); | ||
146 | 128 | ||
147 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0"); | 129 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -P 2c"); |
148 | cmp_ok( $res->return_code, '==', 0, "Timetick used as a string"); | 130 | cmp_ok( $res->return_code, '==', 0, "Timetick used as a string"); |
149 | like($res->output, '/^SNMP OK - Timeticks:\s\(\d+\)\s+(?:\d+ days?,\s+)?\d+:\d+:\d+\.\d+\s.*$/', "Timetick used as a string, result printed rather than parsed"); | ||
150 | 131 | ||
151 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o HOST-RESOURCES-MIB::hrSWRunName.1"); | 132 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o HOST-RESOURCES-MIB::hrSWRunName.1 -P 2c"); |
152 | cmp_ok( $res->return_code, '==', 0, "snmp response without datatype"); | 133 | cmp_ok( $res->return_code, '==', 0, "snmp response without datatype"); |
153 | like( $res->output, '/^SNMP OK - "(systemd|init)" \| $/', "snmp response without datatype" ); | ||
154 | } | 134 | } |
155 | 135 | ||
156 | SKIP: { | 136 | SKIP: { |
157 | skip "no SNMP user defined", 1 if ( ! $user_snmp ); | 137 | skip "no SNMP user defined", 1 if ( ! $user_snmp ); |
158 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -o HOST-RESOURCES-MIB::hrSystemUptime.0 -P 3 -U $user_snmp -L noAuthNoPriv"); | 138 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -o HOST-RESOURCES-MIB::hrSystemUptime.0 -P 3 -U $user_snmp -L noAuthNoPriv"); |
159 | like( $res->output, '/^SNMP OK - Timeticks:\s\(\d+\)\s+(?:\d+ days?,\s+)?\d+:\d+:\d+\.\d+\s.*$/', "noAuthNoPriv security level works properly" ); | ||
160 | } | 139 | } |
161 | 140 | ||
162 | # These checks need a complete command line. An invalid community is used so | 141 | # These checks need a complete command line. An invalid community is used so |
163 | # the tests can run on hosts w/o snmp host/community in NPTest.cache. Execution will fail anyway | 142 | # the tests can run on hosts w/o snmp host/community in NPTest.cache. Execution will fail anyway |
164 | SKIP: { | 143 | SKIP: { |
165 | skip "no non responsive host defined", 2 if ( ! $host_nonresponsive ); | 144 | skip "no non responsive host defined", 2 if ( ! $host_nonresponsive ); |
166 | $res = NPTest->testCmd( "./check_snmp -H $host_nonresponsive --ignore-mib-parsing-errors -C np_foobar -o system.sysUpTime.0 -w 1: -c 1:"); | 145 | $res = NPTest->testCmd( "./check_snmp -H $host_nonresponsive --ignore-mib-parsing-errors -C np_foobar -o system.sysUpTime.0 -w 1: -c 1: -P 2c"); |
167 | cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL with non responsive host" ); | 146 | cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL with non responsive host" ); |
168 | like($res->output, '/Plugin timed out while executing system call/', "String matches timeout problem"); | 147 | # like($res->output, '/Plugin timed out while executing system call/', "String matches timeout problem"); |
169 | } | 148 | } |
170 | 149 | ||
171 | SKIP: { | 150 | SKIP: { |
172 | skip "no non invalid host defined", 2 if ( ! $hostname_invalid ); | 151 | skip "no non invalid host defined", 2 if ( ! $hostname_invalid ); |
173 | $res = NPTest->testCmd( "./check_snmp -H $hostname_invalid --ignore-mib-parsing-errors -C np_foobar -o system.sysUpTime.0 -w 1: -c 1:"); | 152 | $res = NPTest->testCmd( "./check_snmp -H $hostname_invalid --ignore-mib-parsing-errors -C np_foobar -o system.sysUpTime.0 -w 1: -c 1: -P 2c"); |
174 | cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" ); | 153 | cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" ); |
175 | like($res->output, '/External command error: .*(nosuchhost|Name or service not known|Unknown host).*/s', "String matches invalid host"); | 154 | like($res->output, '/.*Unknown host.*/s', "String matches invalid host"); |
176 | } | 155 | } |
diff --git a/plugins/t/check_users.t b/plugins/t/check_users.t index 21c3e53d..446e0476 100644 --- a/plugins/t/check_users.t +++ b/plugins/t/check_users.t | |||
@@ -15,8 +15,8 @@ use NPTest; | |||
15 | use vars qw($tests); | 15 | use vars qw($tests); |
16 | BEGIN {$tests = 12; plan tests => $tests} | 16 | BEGIN {$tests = 12; plan tests => $tests} |
17 | 17 | ||
18 | my $successOutput = '/^USERS OK - [0-9]+ users currently logged in/'; | 18 | my $successOutput = '/[0-9]+ users currently logged in/'; |
19 | my $failureOutput = '/^USERS CRITICAL - [0-9]+ users currently logged in/'; | 19 | my $failureOutput = '/[0-9]+ users currently logged in/'; |
20 | my $wrongOptionOutput = '/Usage:/'; | 20 | my $wrongOptionOutput = '/Usage:/'; |
21 | 21 | ||
22 | my $t; | 22 | my $t; |
diff --git a/plugins/tests/check_curl.t b/plugins/tests/check_curl.t index eaa9f518..52c5ad1c 100755 --- a/plugins/tests/check_curl.t +++ b/plugins/tests/check_curl.t | |||
@@ -15,6 +15,7 @@ | |||
15 | # Email Address []:devel@monitoring-plugins.org | 15 | # Email Address []:devel@monitoring-plugins.org |
16 | 16 | ||
17 | use strict; | 17 | use strict; |
18 | use warnings; | ||
18 | use Test::More; | 19 | use Test::More; |
19 | use NPTest; | 20 | use NPTest; |
20 | use FindBin qw($Bin); | 21 | use FindBin qw($Bin); |
@@ -245,21 +246,21 @@ SKIP: { | |||
245 | 246 | ||
246 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14" ); | 247 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14" ); |
247 | is( $result->return_code, 0, "$command -p $port_https -S -C 14" ); | 248 | is( $result->return_code, 0, "$command -p $port_https -S -C 14" ); |
248 | is( $result->output, "OK - Certificate 'Monitoring Plugins' will expire on $expiry.", "output ok" ); | 249 | like( $result->output, '/.*Certificate \'Monitoring Plugins\' will expire on ' . quotemeta($expiry) . '.*/', "output ok" ); |
249 | 250 | ||
250 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" ); | 251 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" ); |
251 | is( $result->return_code, 1, "$command -p $port_https -S -C 14000" ); | 252 | is( $result->return_code, 1, "$command -p $port_https -S -C 14000" ); |
252 | like( $result->output, '/WARNING - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(' . quotemeta($expiry) . '\)./', "output ok" ); | 253 | like( $result->output, '/.*Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(' . quotemeta($expiry) . '\).*/', "output ok" ); |
253 | 254 | ||
254 | # Expired cert tests | 255 | # Expired cert tests |
255 | $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" ); | 256 | $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" ); |
256 | is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" ); | 257 | is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" ); |
257 | like( $result->output, '/CRITICAL - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(' . quotemeta($expiry) . '\)./', "output ok" ); | 258 | like( $result->output, '/.*Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(' . quotemeta($expiry) . '\).*/', "output ok" ); |
258 | 259 | ||
259 | $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" ); | 260 | $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" ); |
260 | is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" ); | 261 | is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" ); |
261 | is( $result->output, | 262 | like( $result->output, |
262 | 'CRITICAL - Certificate \'Monitoring Plugins\' expired on Wed Jan 2 12:00:00 2008 +0000.', | 263 | '/.*Certificate \'Monitoring Plugins\' expired on Wed Jan\s+2 12:00:00 2008 \+0000.*/', |
263 | "output ok" ); | 264 | "output ok" ); |
264 | 265 | ||
265 | } | 266 | } |
@@ -274,19 +275,19 @@ SKIP: { | |||
274 | $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$port_http\$"; | 275 | $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$port_http\$"; |
275 | $result = NPTest->testCmd( $cmd ); | 276 | $result = NPTest->testCmd( $cmd ); |
276 | is( $result->return_code, 0, $cmd); | 277 | is( $result->return_code, 0, $cmd); |
277 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 278 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
278 | 279 | ||
279 | # http with virtual port (!= 80) | 280 | # http with virtual port (!= 80) |
280 | $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$virtual_port\$"; | 281 | $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$virtual_port\$"; |
281 | $result = NPTest->testCmd( $cmd ); | 282 | $result = NPTest->testCmd( $cmd ); |
282 | is( $result->return_code, 0, $cmd); | 283 | is( $result->return_code, 0, $cmd); |
283 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 284 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
284 | 285 | ||
285 | # http with virtual port (80) | 286 | # http with virtual port (80) |
286 | $cmd = "./$plugin -H $virtual_host:80 -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host\$"; | 287 | $cmd = "./$plugin -H $virtual_host:80 -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host\$"; |
287 | $result = NPTest->testCmd( $cmd ); | 288 | $result = NPTest->testCmd( $cmd ); |
288 | is( $result->return_code, 0, $cmd); | 289 | is( $result->return_code, 0, $cmd); |
289 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 290 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
290 | } | 291 | } |
291 | 292 | ||
292 | # and the same for SSL | 293 | # and the same for SSL |
@@ -296,19 +297,19 @@ SKIP: { | |||
296 | $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$port_https\$"; | 297 | $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$port_https\$"; |
297 | $result = NPTest->testCmd( $cmd ); | 298 | $result = NPTest->testCmd( $cmd ); |
298 | is( $result->return_code, 0, $cmd); | 299 | is( $result->return_code, 0, $cmd); |
299 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 300 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
300 | 301 | ||
301 | # https with virtual port (!= 443) | 302 | # https with virtual port (!= 443) |
302 | $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$virtual_port\$"; | 303 | $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$virtual_port\$"; |
303 | $result = NPTest->testCmd( $cmd ); | 304 | $result = NPTest->testCmd( $cmd ); |
304 | is( $result->return_code, 0, $cmd); | 305 | is( $result->return_code, 0, $cmd); |
305 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 306 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
306 | 307 | ||
307 | # https with virtual port (443) | 308 | # https with virtual port (443) |
308 | $cmd = "./$plugin -H $virtual_host:443 -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host\$"; | 309 | $cmd = "./$plugin -H $virtual_host:443 -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host\$"; |
309 | $result = NPTest->testCmd( $cmd ); | 310 | $result = NPTest->testCmd( $cmd ); |
310 | is( $result->return_code, 0, $cmd); | 311 | is( $result->return_code, 0, $cmd); |
311 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 312 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
312 | } | 313 | } |
313 | 314 | ||
314 | 315 | ||
@@ -321,165 +322,165 @@ sub run_common_tests { | |||
321 | 322 | ||
322 | $result = NPTest->testCmd( "$command -u /file/root" ); | 323 | $result = NPTest->testCmd( "$command -u /file/root" ); |
323 | is( $result->return_code, 0, "/file/root"); | 324 | is( $result->return_code, 0, "/file/root"); |
324 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" ); | 325 | like( $result->output, '/.*HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second.*/', "Output correct" ); |
325 | 326 | ||
326 | $result = NPTest->testCmd( "$command -u /file/root -s Root" ); | 327 | $result = NPTest->testCmd( "$command -u /file/root -s Root" ); |
327 | is( $result->return_code, 0, "/file/root search for string"); | 328 | is( $result->return_code, 0, "/file/root search for string"); |
328 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" ); | 329 | like( $result->output, '/.*HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second.*/', "Output correct" ); |
329 | 330 | ||
330 | $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" ); | 331 | $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" ); |
331 | is( $result->return_code, 2, "Missing string check"); | 332 | is( $result->return_code, 2, "Missing string check"); |
332 | like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRoot' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location"); | 333 | like( $result->output, qr%string 'NonRoot' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location"); |
333 | 334 | ||
334 | $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" ); | 335 | $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" ); |
335 | is( $result->return_code, 2, "Missing string check"); | 336 | is( $result->return_code, 2, "Missing string check"); |
336 | like( $result->output, qr%HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRootWithOver30charsAndM...' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location"); | 337 | like( $result->output, qr%string 'NonRootWithOver30charsAndM...' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location"); |
337 | 338 | ||
338 | $result = NPTest->testCmd( "$command -u /header_check -d foo" ); | 339 | $result = NPTest->testCmd( "$command -u /header_check -d foo" ); |
339 | is( $result->return_code, 0, "header_check search for string"); | 340 | is( $result->return_code, 0, "header_check search for string"); |
340 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 96 bytes in [\d\.]+ second/', "Output correct" ); | 341 | like( $result->output, '/.*HTTP/1.1 200 OK - 96 bytes in [\d\.]+ second.*/', "Output correct" ); |
341 | 342 | ||
342 | $result = NPTest->testCmd( "$command -u /header_check -d bar" ); | 343 | $result = NPTest->testCmd( "$command -u /header_check -d bar" ); |
343 | is( $result->return_code, 2, "Missing header string check"); | 344 | is( $result->return_code, 2, "Missing header string check"); |
344 | like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - header 'bar' not found on 'https?://127\.0\.0\.1:\d+/header_check'%, "Shows search string and location"); | 345 | like( $result->output, qr%header 'bar' not found on 'https?://127\.0\.0\.1:\d+/header_check'%, "Shows search string and location"); |
345 | 346 | ||
346 | $result = NPTest->testCmd( "$command -u /header_broken_check" ); | 347 | $result = NPTest->testCmd( "$command -u /header_broken_check" ); |
347 | is( $result->return_code, 0, "header_check search for string"); | 348 | is( $result->return_code, 0, "header_check search for string"); |
348 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 138 bytes in [\d\.]+ second/', "Output correct" ); | 349 | like( $result->output, '/.*HTTP/1.1 200 OK - 138 bytes in [\d\.]+ second.*/', "Output correct" ); |
349 | 350 | ||
350 | my $cmd; | 351 | my $cmd; |
351 | $cmd = "$command -u /slow"; | 352 | $cmd = "$command -u /slow"; |
352 | $result = NPTest->testCmd( $cmd ); | 353 | $result = NPTest->testCmd( $cmd ); |
353 | is( $result->return_code, 0, "$cmd"); | 354 | is( $result->return_code, 0, "$cmd"); |
354 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 355 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
355 | $result->output =~ /in ([\d\.]+) second/; | 356 | $result->output =~ /in ([\d\.]+) second/; |
356 | cmp_ok( $1, ">", 1, "Time is > 1 second" ); | 357 | cmp_ok( $1, ">", 1, "Time is > 1 second" ); |
357 | 358 | ||
358 | $cmd = "$command -u /statuscode/200"; | 359 | $cmd = "$command -u /statuscode/200"; |
359 | $result = NPTest->testCmd( $cmd ); | 360 | $result = NPTest->testCmd( $cmd ); |
360 | is( $result->return_code, 0, $cmd); | 361 | is( $result->return_code, 0, $cmd); |
361 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 362 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
362 | 363 | ||
363 | $cmd = "$command -u /statuscode/200 -e 200"; | 364 | $cmd = "$command -u /statuscode/200 -e 200"; |
364 | $result = NPTest->testCmd( $cmd ); | 365 | $result = NPTest->testCmd( $cmd ); |
365 | is( $result->return_code, 0, $cmd); | 366 | is( $result->return_code, 0, $cmd); |
366 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 367 | like( $result->output, '/.*Status line output matched "200".*/', "Output correct: ".$result->output ); |
367 | 368 | ||
368 | $cmd = "$command -u /statuscode/201"; | 369 | $cmd = "$command -u /statuscode/201"; |
369 | $result = NPTest->testCmd( $cmd ); | 370 | $result = NPTest->testCmd( $cmd ); |
370 | is( $result->return_code, 0, $cmd); | 371 | is( $result->return_code, 0, $cmd); |
371 | like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output ); | 372 | like( $result->output, '/.*HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
372 | 373 | ||
373 | $cmd = "$command -u /statuscode/201 -e 201"; | 374 | $cmd = "$command -u /statuscode/201 -e 201"; |
374 | $result = NPTest->testCmd( $cmd ); | 375 | $result = NPTest->testCmd( $cmd ); |
375 | is( $result->return_code, 0, $cmd); | 376 | is( $result->return_code, 0, $cmd); |
376 | like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output ); | 377 | like( $result->output, '/.*Status line output matched "201".*/', "Output correct: ".$result->output ); |
377 | 378 | ||
378 | $cmd = "$command -u /statuscode/201 -e 200"; | 379 | $cmd = "$command -u /statuscode/201 -e 200"; |
379 | $result = NPTest->testCmd( $cmd ); | 380 | $result = NPTest->testCmd( $cmd ); |
380 | is( $result->return_code, 2, $cmd); | 381 | is( $result->return_code, 2, $cmd); |
381 | like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output ); | 382 | like( $result->output, '/.*Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created.*/', "Output correct: ".$result->output ); |
382 | 383 | ||
383 | $cmd = "$command -u /statuscode/200 -e 200,201,202"; | 384 | $cmd = "$command -u /statuscode/200 -e 200,201,202"; |
384 | $result = NPTest->testCmd( $cmd ); | 385 | $result = NPTest->testCmd( $cmd ); |
385 | is( $result->return_code, 0, $cmd); | 386 | is( $result->return_code, 0, $cmd); |
386 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 387 | like( $result->output, '/.*Status line output matched "200,201,202".*/', "Output correct: ".$result->output ); |
387 | 388 | ||
388 | $cmd = "$command -u /statuscode/201 -e 200,201,202"; | 389 | $cmd = "$command -u /statuscode/201 -e 200,201,202"; |
389 | $result = NPTest->testCmd( $cmd ); | 390 | $result = NPTest->testCmd( $cmd ); |
390 | is( $result->return_code, 0, $cmd); | 391 | is( $result->return_code, 0, $cmd); |
391 | like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 392 | like( $result->output, '/.*Status line output matched "200,201,202".*/', "Output correct: ".$result->output ); |
392 | 393 | ||
393 | $cmd = "$command -u /statuscode/203 -e 200,201,202"; | 394 | $cmd = "$command -u /statuscode/203 -e 200,201,202"; |
394 | $result = NPTest->testCmd( $cmd ); | 395 | $result = NPTest->testCmd( $cmd ); |
395 | is( $result->return_code, 2, $cmd); | 396 | is( $result->return_code, 2, $cmd); |
396 | like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port (\d+): HTTP/1.1 203 Non-Authoritative Information/', "Output correct: ".$result->output ); | 397 | like( $result->output, '/.*Invalid HTTP response received from host on port (\d+): HTTP/1.1 203 Non-Authoritative Information.*/', "Output correct: ".$result->output ); |
397 | 398 | ||
398 | $cmd = "$command -j HEAD -u /method"; | 399 | $cmd = "$command -j HEAD -u /method"; |
399 | $result = NPTest->testCmd( $cmd ); | 400 | $result = NPTest->testCmd( $cmd ); |
400 | is( $result->return_code, 0, $cmd); | 401 | is( $result->return_code, 0, $cmd); |
401 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 402 | like( $result->output, '/.*HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
402 | 403 | ||
403 | $cmd = "$command -j POST -u /method"; | 404 | $cmd = "$command -j POST -u /method"; |
404 | $result = NPTest->testCmd( $cmd ); | 405 | $result = NPTest->testCmd( $cmd ); |
405 | is( $result->return_code, 0, $cmd); | 406 | is( $result->return_code, 0, $cmd); |
406 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 407 | like( $result->output, '/.*HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
407 | 408 | ||
408 | $cmd = "$command -j GET -u /method"; | 409 | $cmd = "$command -j GET -u /method"; |
409 | $result = NPTest->testCmd( $cmd ); | 410 | $result = NPTest->testCmd( $cmd ); |
410 | is( $result->return_code, 0, $cmd); | 411 | is( $result->return_code, 0, $cmd); |
411 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 412 | like( $result->output, '/.*HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
412 | 413 | ||
413 | $cmd = "$command -u /method"; | 414 | $cmd = "$command -u /method"; |
414 | $result = NPTest->testCmd( $cmd ); | 415 | $result = NPTest->testCmd( $cmd ); |
415 | is( $result->return_code, 0, $cmd); | 416 | is( $result->return_code, 0, $cmd); |
416 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 417 | like( $result->output, '/.*HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
417 | 418 | ||
418 | $cmd = "$command -P foo -u /method"; | 419 | $cmd = "$command -P foo -u /method"; |
419 | $result = NPTest->testCmd( $cmd ); | 420 | $result = NPTest->testCmd( $cmd ); |
420 | is( $result->return_code, 0, $cmd); | 421 | is( $result->return_code, 0, $cmd); |
421 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 422 | like( $result->output, '/.*HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
422 | 423 | ||
423 | $cmd = "$command -j DELETE -u /method"; | 424 | $cmd = "$command -j DELETE -u /method"; |
424 | $result = NPTest->testCmd( $cmd ); | 425 | $result = NPTest->testCmd( $cmd ); |
425 | is( $result->return_code, 1, $cmd); | 426 | is( $result->return_code, 1, $cmd); |
426 | like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output ); | 427 | like( $result->output, '/.*HTTP/1.1 405 Method Not Allowed.*/', "Output correct: ".$result->output ); |
427 | 428 | ||
428 | $cmd = "$command -j foo -u /method"; | 429 | $cmd = "$command -j foo -u /method"; |
429 | $result = NPTest->testCmd( $cmd ); | 430 | $result = NPTest->testCmd( $cmd ); |
430 | is( $result->return_code, 2, $cmd); | 431 | is( $result->return_code, 2, $cmd); |
431 | like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output ); | 432 | like( $result->output, '/.*HTTP/1.1 501 Not Implemented.*/', "Output correct: ".$result->output ); |
432 | 433 | ||
433 | $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude"; | 434 | $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude"; |
434 | $result = NPTest->testCmd( $cmd ); | 435 | $result = NPTest->testCmd( $cmd ); |
435 | is( $result->return_code, 0, $cmd); | 436 | is( $result->return_code, 0, $cmd); |
436 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 437 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
437 | 438 | ||
438 | $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude"; | 439 | $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude"; |
439 | $result = NPTest->testCmd( $cmd ); | 440 | $result = NPTest->testCmd( $cmd ); |
440 | is( $result->return_code, 0, $cmd); | 441 | is( $result->return_code, 0, $cmd); |
441 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 442 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
442 | 443 | ||
443 | # To confirm that the free doesn't segfault | 444 | # To confirm that the free doesn't segfault |
444 | $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude"; | 445 | $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude"; |
445 | $result = NPTest->testCmd( $cmd ); | 446 | $result = NPTest->testCmd( $cmd ); |
446 | is( $result->return_code, 0, $cmd); | 447 | is( $result->return_code, 0, $cmd); |
447 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 448 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
448 | 449 | ||
449 | $cmd = "$command -u /redirect"; | 450 | $cmd = "$command -u /redirect"; |
450 | $result = NPTest->testCmd( $cmd ); | 451 | $result = NPTest->testCmd( $cmd ); |
451 | is( $result->return_code, 0, $cmd); | 452 | is( $result->return_code, 0, $cmd); |
452 | like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 453 | like( $result->output, '/.*HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
453 | 454 | ||
454 | $cmd = "$command -f follow -u /redirect"; | 455 | $cmd = "$command -f follow -u /redirect"; |
455 | $result = NPTest->testCmd( $cmd ); | 456 | $result = NPTest->testCmd( $cmd ); |
456 | is( $result->return_code, 0, $cmd); | 457 | is( $result->return_code, 0, $cmd); |
457 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 458 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
458 | 459 | ||
459 | $cmd = "$command -u /redirect -k 'follow: me'"; | 460 | $cmd = "$command -u /redirect -k 'follow: me'"; |
460 | $result = NPTest->testCmd( $cmd ); | 461 | $result = NPTest->testCmd( $cmd ); |
461 | is( $result->return_code, 0, $cmd); | 462 | is( $result->return_code, 0, $cmd); |
462 | like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 463 | like( $result->output, '/.*HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
463 | 464 | ||
464 | $cmd = "$command -f follow -u /redirect -k 'follow: me'"; | 465 | $cmd = "$command -f follow -u /redirect -k 'follow: me'"; |
465 | $result = NPTest->testCmd( $cmd ); | 466 | $result = NPTest->testCmd( $cmd ); |
466 | is( $result->return_code, 0, $cmd); | 467 | is( $result->return_code, 0, $cmd); |
467 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 468 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
468 | 469 | ||
469 | $cmd = "$command -f sticky -u /redirect -k 'follow: me'"; | 470 | $cmd = "$command -f sticky -u /redirect -k 'follow: me'"; |
470 | $result = NPTest->testCmd( $cmd ); | 471 | $result = NPTest->testCmd( $cmd ); |
471 | is( $result->return_code, 0, $cmd); | 472 | is( $result->return_code, 0, $cmd); |
472 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 473 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
473 | 474 | ||
474 | $cmd = "$command -f stickyport -u /redirect -k 'follow: me'"; | 475 | $cmd = "$command -f stickyport -u /redirect -k 'follow: me'"; |
475 | $result = NPTest->testCmd( $cmd ); | 476 | $result = NPTest->testCmd( $cmd ); |
476 | is( $result->return_code, 0, $cmd); | 477 | is( $result->return_code, 0, $cmd); |
477 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 478 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
478 | 479 | ||
479 | $cmd = "$command -f follow -u /redirect_rel -s redirected"; | 480 | $cmd = "$command -f follow -u /redirect_rel -s redirected"; |
480 | $result = NPTest->testCmd( $cmd ); | 481 | $result = NPTest->testCmd( $cmd ); |
481 | is( $result->return_code, 0, $cmd); | 482 | is( $result->return_code, 0, $cmd); |
482 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | 483 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct: ".$result->output ); |
483 | 484 | ||
484 | # These tests may block | 485 | # These tests may block |
485 | # stickyport - on full urlS port is set back to 80 otherwise | 486 | # stickyport - on full urlS port is set back to 80 otherwise |
diff --git a/plugins/tests/check_snmp.t b/plugins/tests/check_snmp.t index bfe42e16..26d67898 100755 --- a/plugins/tests/check_snmp.t +++ b/plugins/tests/check_snmp.t | |||
@@ -4,12 +4,13 @@ | |||
4 | # | 4 | # |
5 | 5 | ||
6 | use strict; | 6 | use strict; |
7 | use warnings; | ||
7 | use Test::More; | 8 | use Test::More; |
8 | use NPTest; | 9 | use NPTest; |
9 | use FindBin qw($Bin); | 10 | use FindBin qw($Bin); |
10 | use POSIX qw/strftime/; | 11 | use POSIX qw/strftime/; |
11 | 12 | ||
12 | my $tests = 81; | 13 | my $tests = 75; |
13 | # Check that all dependent modules are available | 14 | # Check that all dependent modules are available |
14 | eval { | 15 | eval { |
15 | require NetSNMP::OID; | 16 | require NetSNMP::OID; |
@@ -76,49 +77,36 @@ my $res; | |||
76 | 77 | ||
77 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.0"); | 78 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.0"); |
78 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying a multi-line string" ); | 79 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying a multi-line string" ); |
79 | like($res->output, '/^SNMP OK - /', "String contains SNMP OK"); | 80 | like($res->output, '/.*Cisco Internetwork Operating System Software.*/m', "String contains all lines"); |
80 | like($res->output, '/'.quotemeta('SNMP OK - Cisco Internetwork Operating System Software | | ||
81 | .1.3.6.1.4.1.8072.3.2.67.0: | ||
82 | "Cisco Internetwork Operating System Software | ||
83 | IOS (tm) Catalyst 4000 \"L3\" Switch Software (cat4000-I9K91S-M), Version | ||
84 | 12.2(20)EWA, RELEASE SOFTWARE (fc1) | ||
85 | Technical Support: http://www.cisco.com/techsupport | ||
86 | Copyright (c) 1986-2004 by cisco Systems, Inc. | ||
87 | "').'/m', "String contains all lines"); | ||
88 | 81 | ||
89 | # sysContact.0 is "Alice" (from our snmpd.conf) | 82 | # sysContact.0 is "Alice" (from our snmpd.conf) |
90 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.0 -o sysContact.0 -o .1.3.6.1.4.1.8072.3.2.67.1"); | 83 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.0 -o sysContact.0 -o .1.3.6.1.4.1.8072.3.2.67.1"); |
91 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying multi-line OIDs" ); | 84 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying multi-line OIDs" ); |
92 | like($res->output, '/^SNMP OK - /', "String contains SNMP OK"); | 85 | # like($res->output, '/^SNMP OK - /', "String contains SNMP OK"); |
93 | like($res->output, '/'.quotemeta('SNMP OK - Cisco Internetwork Operating System Software ').'"?Alice"?'.quotemeta(' Kisco Outernetwork Oserating Gystem Totware | | 86 | like($res->output, '/.*Cisco Internetwork Operating System Software.*/m', "String contains all lines with multiple OIDs"); |
94 | .1.3.6.1.4.1.8072.3.2.67.0: | 87 | like($res->output, '/.*Alice.*/m', "String contains all lines with multiple OIDs"); |
95 | "Cisco Internetwork Operating System Software | 88 | like($res->output, '/.*Kisco Outernetwork Oserating Gystem Totware.*/m', "String contains all lines with multiple OIDs"); |
96 | IOS (tm) Catalyst 4000 \"L3\" Switch Software (cat4000-I9K91S-M), Version | ||
97 | 12.2(20)EWA, RELEASE SOFTWARE (fc1) | ||
98 | Technical Support: http://www.cisco.com/techsupport | ||
99 | Copyright (c) 1986-2004 by cisco Systems, Inc. | ||
100 | " | ||
101 | .1.3.6.1.4.1.8072.3.2.67.1: | ||
102 | "Kisco Outernetwork Oserating Gystem Totware | ||
103 | Copyleft (c) 2400-2689 by kisco Systrems, Inc."').'/m', "String contains all lines with multiple OIDs"); | ||
104 | 89 | ||
105 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.2"); | 90 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.2"); |
106 | like($res->output, '/'.quotemeta('SNMP OK - This should not confuse check_snmp \"parser\" | | 91 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying multi-line OIDs" ); |
107 | .1.3.6.1.4.1.8072.3.2.67.2: | 92 | # like($res->output, '/'.quotemeta('This should not confuse check_snmp \"parser\" | |
108 | "This should not confuse check_snmp \"parser\" | 93 | # .1.3.6.1.4.1.8072.3.2.67.2: |
109 | into thinking there is no 2nd line"').'/m', "Attempt to confuse parser No.1"); | 94 | # "This should not confuse check_snmp \"parser\" |
95 | # into thinking there is no 2nd line"').'/m', "Attempt to confuse parser No.1"); | ||
110 | 96 | ||
111 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.3"); | 97 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.3"); |
112 | like($res->output, '/'.quotemeta('SNMP OK - It\'s getting even harder if the line | | 98 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying multi-line OIDs" ); |
113 | .1.3.6.1.4.1.8072.3.2.67.3: | 99 | # like($res->output, '/'.quotemeta('It\'s getting even harder if the line | |
114 | "It\'s getting even harder if the line | 100 | # .1.3.6.1.4.1.8072.3.2.67.3: |
115 | ends with with this: C:\\\\"').'/m', "Attempt to confuse parser No.2"); | 101 | # "It\'s getting even harder if the line |
102 | # ends with with this: C:\\\\"').'/m', "Attempt to confuse parser No.2"); | ||
116 | 103 | ||
117 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.4"); | 104 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.4"); |
118 | like($res->output, '/'.quotemeta('SNMP OK - And now have fun with with this: \"C:\\\\\" | | 105 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying multi-line OIDs" ); |
119 | .1.3.6.1.4.1.8072.3.2.67.4: | 106 | # like($res->output, '/'.quotemeta('And now have fun with with this: \"C:\\\\\" | |
120 | "And now have fun with with this: \"C:\\\\\" | 107 | # .1.3.6.1.4.1.8072.3.2.67.4: |
121 | because we\'re not done yet!"').'/m', "Attempt to confuse parser No.3"); | 108 | # "And now have fun with with this: \"C:\\\\\" |
109 | # because we\'re not done yet!"').'/m', "Attempt to confuse parser No.3"); | ||
122 | 110 | ||
123 | system("rm -f ".$ENV{'MP_STATE_PATH'}."/*/check_snmp/*"); | 111 | system("rm -f ".$ENV{'MP_STATE_PATH'}."/*/check_snmp/*"); |
124 | 112 | ||
@@ -131,156 +119,159 @@ SKIP: { | |||
131 | my $ts = time(); | 119 | my $ts = time(); |
132 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" ); | 120 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" ); |
133 | is($res->return_code, 0, "Returns OK"); | 121 | is($res->return_code, 0, "Returns OK"); |
134 | is($res->output, "No previous data to calculate rate - assume okay"); | 122 | like($res->output, "/.*No previous data to calculate rate - assume okay.*/"); |
135 | 123 | ||
136 | # test rate 1 second later | 124 | # test rate 1 second later |
137 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" ); | 125 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" ); |
138 | is($res->return_code, 1, "WARNING - due to going above rate calculation" ); | 126 | is($res->return_code, 1, "WARNING - due to going above rate calculation" ); |
139 | is($res->output, "SNMP RATE WARNING - *666* | iso.3.6.1.4.1.8072.3.2.67.10=666;600 "); | 127 | like($res->output, "/.*=666.*/"); |
140 | 128 | ||
141 | # test rate with same time | 129 | # test rate with same time |
142 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" ); | 130 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" ); |
143 | is($res->return_code, 3, "UNKNOWN - basically the divide by zero error" ); | 131 | is($res->return_code, 3, "UNKNOWN - basically the divide by zero error" ); |
144 | is($res->output, "Time duration between plugin calls is invalid"); | 132 | like($res->output, "/.*Time duration between plugin calls is invalid.*/"); |
145 | 133 | ||
146 | 134 | ||
147 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" ); | 135 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" ); |
148 | is($res->return_code, 0, "OK for first call" ); | 136 | is($res->return_code, 0, "OK for first call" ); |
149 | is($res->output, "No previous data to calculate rate - assume okay" ); | 137 | like($res->output, "/.*No previous data to calculate rate - assume okay.*/" ); |
150 | 138 | ||
151 | # test rate 1 second later | 139 | # test rate 1 second later |
152 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" ); | 140 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" ); |
153 | is($res->return_code, 0, "OK as no thresholds" ); | 141 | is($res->return_code, 0, "OK as no thresholds" ); |
154 | is($res->output, "SNMP RATE OK - inoctets 666 | inoctets=666 ", "Check label"); | 142 | like($res->output, "/.*inoctets.*=666.*/m", "Check label"); |
155 | 143 | ||
156 | # test rate 3 seconds later | 144 | # test rate 3 seconds later |
157 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+3))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" ); | 145 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+3))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" ); |
158 | is($res->return_code, 0, "OK as no thresholds" ); | 146 | is($res->return_code, 0, "OK as no thresholds" ); |
159 | is($res->output, "SNMP RATE OK - inoctets 333 | inoctets=333 ", "Check rate decreases due to longer interval"); | 147 | like($res->output, "/.*inoctets.*333.*/", "Check rate decreases due to longer interval"); |
160 | 148 | ||
161 | 149 | ||
162 | # label performance data check | 150 | # label performance data check |
163 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l test" ); | 151 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l test" ); |
164 | is($res->return_code, 0, "OK as no thresholds" ); | 152 | is($res->return_code, 0, "OK as no thresholds" ); |
165 | is($res->output, "SNMP OK - test 67996 | test=67996c ", "Check label"); | 153 | like($res->output, "/.*test.?=67996c/", "Check label"); |
166 | 154 | ||
167 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l \"test'test\"" ); | 155 | # following test is deactivated since it was not valid due to the guidelines (no single quote in label allowed) |
168 | is($res->return_code, 0, "OK as no thresholds" ); | 156 | # $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l \"test'test\"" ); |
169 | is($res->output, "SNMP OK - test'test 68662 | \"test'test\"=68662c ", "Check label"); | 157 | # is($res->return_code, 0, "OK as no thresholds" ); |
158 | # is($res->output, "test'test 68662 | \"test'test\"=68662c ", "Check label"); | ||
170 | 159 | ||
171 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l 'test\"test'" ); | 160 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l 'test\"test'" ); |
172 | is($res->return_code, 0, "OK as no thresholds" ); | 161 | is($res->return_code, 0, "OK as no thresholds" ); |
173 | is($res->output, "SNMP OK - test\"test 69328 | 'test\"test'=69328c ", "Check label"); | 162 | like($res->output, "/.*'test\"test'=68662c.*/", "Check label"); |
174 | 163 | ||
175 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l test -O" ); | 164 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l test -O" ); |
176 | is($res->return_code, 0, "OK as no thresholds" ); | 165 | is($res->return_code, 0, "OK as no thresholds" ); |
177 | is($res->output, "SNMP OK - test 69994 | iso.3.6.1.4.1.8072.3.2.67.10=69994c ", "Check label"); | 166 | like($res->output, "/.*.67.10.?=69328c.*/", "Check label"); |
178 | 167 | ||
179 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10" ); | 168 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10" ); |
180 | is($res->return_code, 0, "OK as no thresholds" ); | 169 | is($res->return_code, 0, "OK as no thresholds" ); |
181 | is($res->output, "SNMP OK - 70660 | iso.3.6.1.4.1.8072.3.2.67.10=70660c ", "Check label"); | 170 | like($res->output, "/.*8072.3.2.67.10.?=69994c.*/", "Check label"); |
182 | 171 | ||
183 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l 'test test'" ); | 172 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l 'test test'" ); |
184 | is($res->return_code, 0, "OK as no thresholds" ); | 173 | is($res->return_code, 0, "OK as no thresholds" ); |
185 | is($res->output, "SNMP OK - test test 71326 | 'test test'=71326c ", "Check label"); | 174 | like($res->output, "/.*'test test'=70660c/", "Check label"); |
186 | 175 | ||
187 | 176 | ||
188 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets_per_minute --rate-multiplier=60" ); | 177 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets_per_minute --rate-multiplier=60" ); |
189 | is($res->return_code, 0, "OK for first call" ); | 178 | is($res->return_code, 0, "OK for first call" ); |
190 | is($res->output, "No previous data to calculate rate - assume okay" ); | 179 | like($res->output, "/.*No previous data to calculate rate - assume okay.*/" ); |
191 | 180 | ||
192 | # test 1 second later | 181 | # test 1 second later |
193 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets_per_minute --rate-multiplier=60" ); | 182 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets_per_minute --rate-multiplier=60" ); |
194 | is($res->return_code, 0, "OK as no thresholds" ); | 183 | is($res->return_code, 0, "OK as no thresholds" ); |
195 | is($res->output, "SNMP RATE OK - inoctets_per_minute 39960 | inoctets_per_minute=39960 ", "Checking multiplier"); | 184 | like($res->output, "/.*inoctets_per_minute.*=39960/", "Checking multiplier"); |
196 | }; | 185 | }; |
197 | 186 | ||
198 | 187 | ||
199 | 188 | ||
200 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 -s '\"stringtests\"'" ); | 189 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 -s 'stringtests'" ); |
201 | is($res->return_code, 0, "OK as string matches" ); | 190 | is($res->return_code, 0, "OK as string matches" ); |
202 | is($res->output, 'SNMP OK - "stringtests" | ', "Good string match" ); | 191 | like($res->output, '/.*stringtests.*/', "Good string match" ); |
203 | 192 | ||
204 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 -s ring" ); | 193 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 -s ring" ); |
205 | is($res->return_code, 2, "CRITICAL as string doesn't match (though is a substring)" ); | 194 | is($res->return_code, 2, "CRITICAL as string doesn't match (though is a substring)" ); |
206 | is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Failed string match" ); | 195 | like($res->output, '/.*stringtests.*/', "Failed string match" ); |
207 | 196 | ||
208 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 --invert-search -s '\"stringtests\"'" ); | 197 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 --invert-search -s 'stringtests'" ); |
209 | is($res->return_code, 2, "CRITICAL as string matches but inverted" ); | 198 | is($res->return_code, 2, "CRITICAL as string matches but inverted" ); |
210 | is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Inverted string match" ); | 199 | like($res->output, '/.*"stringtests".*/', "Inverted string match" ); |
211 | 200 | ||
212 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 --invert-search -s ring" ); | 201 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 --invert-search -s ring" ); |
213 | is($res->return_code, 0, "OK as string doesn't match but inverted" ); | 202 | is($res->return_code, 0, "OK as string doesn't match but inverted" ); |
214 | is($res->output, 'SNMP OK - "stringtests" | ', "OK as inverted string no match" ); | 203 | like($res->output, '/.*"stringtests".*/', "OK as inverted string no match" ); |
215 | 204 | ||
216 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.12 -w 4:5" ); | 205 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.12 -w 4:5" ); |
217 | is($res->return_code, 1, "Numeric in string test" ); | 206 | # a string is a string and not a number |
218 | is($res->output, 'SNMP WARNING - *3.5* | iso.3.6.1.4.1.8072.3.2.67.12=3.5;4:5 ', "WARNING threshold checks for string masquerading as number" ); | 207 | is($res->return_code, 0, "Numeric in string test" ); |
208 | like($res->output, '/.*3.5.*| iso.3.6.1.4.1.8072.3.2.67.12=3.5;4:5/', "WARNING threshold checks for string masquerading as number" ); | ||
219 | 209 | ||
220 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.13" ); | 210 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.13" ); |
221 | is($res->return_code, 0, "Not really numeric test" ); | 211 | is($res->return_code, 0, "Not really numeric test" ); |
222 | is($res->output, 'SNMP OK - "87.4startswithnumberbutshouldbestring" | ', "Check string with numeric start is still string" ); | 212 | like($res->output, '/.*"87.4startswithnumberbutshouldbestring".*/', "Check string with numeric start is still string" ); |
223 | 213 | ||
224 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.14" ); | 214 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.14" ); |
225 | is($res->return_code, 0, "Not really numeric test (trying best to fool it)" ); | 215 | is($res->return_code, 0, "Not really numeric test (trying best to fool it)" ); |
226 | is($res->output, 'SNMP OK - "555\"I said\"" | ', "Check string with a double quote following is still a string (looks like the perl routine will always escape though)" ); | 216 | like($res->output, '/.*\'555"I said"\'.*/', "Check string with a double quote following is still a string (looks like the perl routine will always escape though)" ); |
227 | 217 | ||
228 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.15 -r 'CUSTOM CHECK OK'" ); | 218 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.15 -r 'CUSTOM CHECK OK'" ); |
229 | is($res->return_code, 0, "String check should check whole string, not a parsed number" ); | 219 | is($res->return_code, 0, "String check should check whole string, not a parsed number" ); |
230 | is($res->output, 'SNMP OK - "CUSTOM CHECK OK: foo is 12345" | ', "String check with numbers returns whole string"); | 220 | like($res->output, '/.*CUSTOM CHECK OK: foo is 12345.*/', "String check with numbers returns whole string"); |
231 | 221 | ||
232 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.16 -w -2: -c -3:" ); | 222 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.16 -w -2: -c -3:" ); |
233 | is($res->return_code, 0, "Negative integer check OK" ); | 223 | is($res->return_code, 0, "Negative integer check OK" ); |
234 | is($res->output, 'SNMP OK - -2 | iso.3.6.1.4.1.8072.3.2.67.16=-2;-2:;-3: ', "Negative integer check OK output" ); | 224 | like($res->output, '/.*-2.*| iso.3.6.1.4.1.8072.3.2.67.16=-2;-2:;-3:/', "Negative integer check OK output" ); |
235 | 225 | ||
236 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.16 -w -2: -c -3:" ); | 226 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.16 -w -2: -c -3:" ); |
237 | is($res->return_code, 1, "Negative integer check WARNING" ); | 227 | is($res->return_code, 1, "Negative integer check WARNING" ); |
238 | is($res->output, 'SNMP WARNING - *-3* | iso.3.6.1.4.1.8072.3.2.67.16=-3;-2:;-3: ', "Negative integer check WARNING output" ); | 228 | like($res->output, '/.*-3.*| iso.3.6.1.4.1.8072.3.2.67.16=-3;-2:;-3:/', "Negative integer check WARNING output" ); |
239 | 229 | ||
240 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.16 -w -2: -c -3:" ); | 230 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.16 -w -2: -c -3:" ); |
241 | is($res->return_code, 2, "Negative integer check CRITICAL" ); | 231 | is($res->return_code, 2, "Negative integer check CRITICAL" ); |
242 | is($res->output, 'SNMP CRITICAL - *-4* | iso.3.6.1.4.1.8072.3.2.67.16=-4;-2:;-3: ', "Negative integer check CRITICAL output" ); | 232 | like($res->output, '/.*-4.*| iso.3.6.1.4.1.8072.3.2.67.16=-4;-2:;-3:/', "Negative integer check CRITICAL output" ); |
243 | 233 | ||
244 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.17 -w -3: -c -6:" ); | 234 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.17 -w -3: -c -6:" ); |
245 | is($res->return_code, 1, "Negative integer as string, WARNING" ); | 235 | is($res->return_code, 1, "Negative integer as string, WARNING" ); |
246 | is($res->output, 'SNMP WARNING - *-4* | iso.3.6.1.4.1.8072.3.2.67.17=-4;-3:;-6: ', "Negative integer as string, WARNING output" ); | 236 | like($res->output, '/.*-4.*| iso.3.6.1.4.1.8072.3.2.67.17=-4;-3:;-6:/', "Negative integer as string, WARNING output" ); |
247 | 237 | ||
248 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.17 -w -2: -c -3:" ); | 238 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.17 -w -2: -c -3:" ); |
249 | is($res->return_code, 2, "Negative integer as string, CRITICAL" ); | 239 | is($res->return_code, 2, "Negative integer as string, CRITICAL" ); |
250 | is($res->output, 'SNMP CRITICAL - *-4* | iso.3.6.1.4.1.8072.3.2.67.17=-4;-2:;-3: ', "Negative integer as string, CRITICAL output" ); | 240 | like($res->output, '/.*-4.*| iso.3.6.1.4.1.8072.3.2.67.17=-4;-2:;-3:/', "Negative integer as string, CRITICAL output" ); |
251 | 241 | ||
252 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.18 -c '~:-6.5'" ); | 242 | # deactivated since the perl agent api of snmpd really does not allow floats |
253 | is($res->return_code, 0, "Negative float OK" ); | 243 | # $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.18 -c '~:-6.5'" ); |
254 | is($res->output, 'SNMP OK - -6.6 | iso.3.6.1.4.1.8072.3.2.67.18=-6.6;;@-6.5:~ ', "Negative float OK output" ); | 244 | # is($res->return_code, 0, "Negative float OK" ); |
245 | # is($res->output, '-6.6 | iso.3.6.1.4.1.8072.3.2.67.18=-6.6;;@-6.5:~ ', "Negative float OK output" ); | ||
255 | 246 | ||
256 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.18 -w '~:-6.65' -c '~:-6.55'" ); | 247 | # $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.18 -w '~:-6.65' -c '~:-6.55'" ); |
257 | is($res->return_code, 1, "Negative float WARNING" ); | 248 | # is($res->return_code, 1, "Negative float WARNING" ); |
258 | is($res->output, 'SNMP WARNING - *-6.6* | iso.3.6.1.4.1.8072.3.2.67.18=-6.6;@-6.65:~;@-6.55:~ ', "Negative float WARNING output" ); | 249 | # like($res->output, '/-6.6.*| .*67.18=-6.6;@-6.65:~;@-6.55:~/', "Negative float WARNING output" ); |
259 | 250 | ||
260 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10,.1.3.6.1.4.1.8072.3.2.67.17 -w '1:100000,-10:20' -c '2:200000,-20:30'" ); | 251 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10,.1.3.6.1.4.1.8072.3.2.67.17 -w '1:100000,-10:20' -c '2:200000,-20:30'" ); |
261 | is($res->return_code, 0, "Multiple OIDs with thresholds" ); | 252 | is($res->return_code, 0, "Multiple OIDs with thresholds" ); |
262 | like($res->output, '/SNMP OK - \d+ -4 | iso.3.6.1.4.1.8072.3.2.67.10=\d+c;1:100000;2:200000 iso.3.6.1.4.1.8072.3.2.67.17=-4;-10:20;-20:30/', "Multiple OIDs with thresholds output" ); | 253 | like($res->output, '/-4.*| .*67.10=\d+c;1:100000;2:200000 .*67.17=-4;-10:20;-20:30/', "Multiple OIDs with thresholds output" ); |
263 | 254 | ||
264 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10,.1.3.6.1.4.1.8072.3.2.67.17 -w '1:100000,-1:2' -c '2:200000,-20:30'" ); | 255 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10,.1.3.6.1.4.1.8072.3.2.67.17 -w '1:100000,-1:2' -c '2:200000,-20:30'" ); |
265 | is($res->return_code, 1, "Multiple OIDs with thresholds" ); | 256 | is($res->return_code, 1, "Multiple OIDs with thresholds" ); |
266 | like($res->output, '/SNMP WARNING - \d+ \*-4\* | iso.3.6.1.4.1.8072.3.2.67.10=\d+c;1:100000;2:200000 iso.3.6.1.4.1.8072.3.2.67.17=-4;-10:20;-20:30/', "Multiple OIDs with thresholds output" ); | 257 | like($res->output, '/-4.*| iso.3.6.1.4.1.8072.3.2.67.10=\d+c;1:100000;2:200000 iso.3.6.1.4.1.8072.3.2.67.17=-4;-10:20;-20:30/', "Multiple OIDs with thresholds output" ); |
267 | 258 | ||
268 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10,.1.3.6.1.4.1.8072.3.2.67.17 -w 1,2 -c 1" ); | 259 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10,.1.3.6.1.4.1.8072.3.2.67.17 -w 1,2 -c 1 -O" ); |
269 | is($res->return_code, 2, "Multiple OIDs with some thresholds" ); | 260 | is($res->return_code, 2, "Multiple OIDs with some thresholds" ); |
270 | like($res->output, '/SNMP CRITICAL - \*\d+\* \*-4\* | iso.3.6.1.4.1.8072.3.2.67.10=\d+c;1;2 iso.3.6.1.4.1.8072.3.2.67.17=-4;;/', "Multiple OIDs with thresholds output" ); | 261 | like($res->output, '/.*-4.*| iso.3.6.1.4.1.8072.3.2.67.10=\d+c;1;2 iso.3.6.1.4.1.8072.3.2.67.17=-4;;/', "Multiple OIDs with thresholds output" ); |
271 | 262 | ||
272 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19"); | 263 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19"); |
273 | is($res->return_code, 0, "Test plain .1.3.6.1.4.1.8072.3.2.67.6 RC" ); | 264 | is($res->return_code, 0, "Test plain .1.3.6.1.4.1.8072.3.2.67.6 RC" ); |
274 | is($res->output,'SNMP OK - 42 | iso.3.6.1.4.1.8072.3.2.67.19=42 ', "Test plain value of .1.3.6.1.4.1.8072.3.2.67.1" ); | 265 | like($res->output,'/.*42.*| iso.3.6.1.4.1.8072.3.2.67.19=42/', "Test plain value of .1.3.6.1.4.1.8072.3.2.67.1" ); |
275 | 266 | ||
276 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 -M .1"); | 267 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 -M .1"); |
277 | is($res->return_code, 0, "Test multiply RC" ); | 268 | is($res->return_code, 0, "Test multiply RC" ); |
278 | is($res->output,'SNMP OK - 4.200000 | iso.3.6.1.4.1.8072.3.2.67.19=4.200000 ' , "Test multiply .1 output" ); | 269 | like($res->output,'/.*4.200000.*| iso.3.6.1.4.1.8072.3.2.67.19=4.200000/' , "Test multiply .1 output" ); |
279 | 270 | ||
280 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 --multiplier=.1 -f '%.2f' "); | 271 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 --multiplier=.1"); |
281 | is($res->return_code, 0, "Test multiply RC + format" ); | 272 | is($res->return_code, 0, "Test multiply RC" ); |
282 | is($res->output, 'SNMP OK - 4.20 | iso.3.6.1.4.1.8072.3.2.67.19=4.20 ', "Test multiply .1 output + format" ); | 273 | like($res->output, '/.*4.20.*| iso.3.6.1.4.1.8072.3.2.67.19=4.20/', "Test multiply .1 output" ); |
283 | 274 | ||
284 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 --multiplier=.1 -f '%.2f' -w 1"); | 275 | $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 --multiplier=.1 -w 1"); |
285 | is($res->return_code, 1, "Test multiply RC + format + thresholds" ); | 276 | is($res->return_code, 1, "Test multiply RC + thresholds" ); |
286 | is($res->output, 'SNMP WARNING - *4.20* | iso.3.6.1.4.1.8072.3.2.67.19=4.20;1 ', "Test multiply .1 output + format + thresholds" ); | 277 | like($res->output, '/.*4.20.* | iso.3.6.1.4.1.8072.3.2.67.19=4.20+;1/', "Test multiply .1 output + thresholds" ); |
diff --git a/plugins/tests/check_snmp_agent.pl b/plugins/tests/check_snmp_agent.pl index 38912e98..608b6f92 100644 --- a/plugins/tests/check_snmp_agent.pl +++ b/plugins/tests/check_snmp_agent.pl | |||
@@ -4,9 +4,10 @@ | |||
4 | # | 4 | # |
5 | 5 | ||
6 | #use strict; # Doesn't work | 6 | #use strict; # Doesn't work |
7 | use warnings; | ||
7 | use NetSNMP::OID qw(:all); | 8 | use NetSNMP::OID qw(:all); |
8 | use NetSNMP::agent; | 9 | use NetSNMP::agent; |
9 | use NetSNMP::ASN qw(ASN_OCTET_STR ASN_COUNTER ASN_COUNTER64 ASN_INTEGER ASN_INTEGER64 ASN_UNSIGNED ASN_UNSIGNED64); | 10 | use NetSNMP::ASN qw(ASN_OCTET_STR ASN_COUNTER ASN_COUNTER64 ASN_INTEGER ASN_INTEGER64 ASN_UNSIGNED ASN_UNSIGNED64 ASN_FLOAT); |
10 | #use Math::Int64 qw(uint64); # Skip that module while we don't need it | 11 | #use Math::Int64 qw(uint64); # Skip that module while we don't need it |
11 | sub uint64 { return $_ } | 12 | sub uint64 { return $_ } |
12 | 13 | ||
@@ -22,21 +23,82 @@ IOS (tm) Catalyst 4000 "L3" Switch Software (cat4000-I9K91S-M), Version | |||
22 | Technical Support: http://www.cisco.com/techsupport | 23 | Technical Support: http://www.cisco.com/techsupport |
23 | Copyright (c) 1986-2004 by cisco Systems, Inc. | 24 | Copyright (c) 1986-2004 by cisco Systems, Inc. |
24 | '; | 25 | '; |
25 | my $multilin2 = "Kisco Outernetwork Oserating Gystem Totware | 26 | my $multiline2 = "Kisco Outernetwork Oserating Gystem Totware |
26 | Copyleft (c) 2400-2689 by kisco Systrems, Inc."; | 27 | Copyleft (c) 2400-2689 by kisco Systrems, Inc."; |
27 | my $multilin3 = 'This should not confuse check_snmp "parser" | 28 | my $multiline3 = 'This should not confuse check_snmp "parser" |
28 | into thinking there is no 2nd line'; | 29 | into thinking there is no 2nd line'; |
29 | my $multilin4 = 'It\'s getting even harder if the line | 30 | my $multiline4 = 'It\'s getting even harder if the line |
30 | ends with with this: C:\\'; | 31 | ends with with this: C:\\'; |
31 | my $multilin5 = 'And now have fun with with this: "C:\\" | 32 | my $multiline5 = 'And now have fun with with this: "C:\\" |
32 | because we\'re not done yet!'; | 33 | because we\'re not done yet!'; |
33 | 34 | ||
34 | # Next are arrays of indexes (Type, initial value and increments) | 35 | # Next are arrays of indexes (Type, initial value and increments) |
35 | # 0..19 <---- please update comment when adding/removing fields | 36 | # 0..19 <---- please update comment when adding/removing fields |
36 | my @fields = (ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED, ASN_COUNTER, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_INTEGER, ASN_OCTET_STR, ASN_OCTET_STR, ASN_INTEGER ); | 37 | my @fields = (ASN_OCTET_STR, # 0 |
37 | my @values = ($multiline, $multilin2, $multilin3, $multilin4, $multilin5, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)), 64000, "stringtests", "3.5", "87.4startswithnumberbutshouldbestring", '555"I said"', 'CUSTOM CHECK OK: foo is 12345', -2, '-4', '-6.6', 42 ); | 38 | ASN_OCTET_STR, # 1 |
39 | ASN_OCTET_STR, # 2 | ||
40 | ASN_OCTET_STR, # 3 | ||
41 | ASN_OCTET_STR, # 4 | ||
42 | ASN_UNSIGNED, # 5 | ||
43 | ASN_UNSIGNED, # 6 | ||
44 | ASN_COUNTER, # 7 | ||
45 | ASN_COUNTER64, # 8 | ||
46 | ASN_UNSIGNED, # 9 | ||
47 | ASN_COUNTER, # 10 | ||
48 | ASN_OCTET_STR, # 11 | ||
49 | ASN_OCTET_STR, # 12 | ||
50 | ASN_OCTET_STR, # 13 | ||
51 | ASN_OCTET_STR, # 14 | ||
52 | ASN_OCTET_STR, # 15 | ||
53 | ASN_INTEGER, # 16 | ||
54 | ASN_INTEGER, # 17 | ||
55 | ASN_FLOAT, # 18 | ||
56 | ASN_INTEGER # 19 | ||
57 | ); | ||
58 | my @values = ($multiline, # 0 | ||
59 | $multiline2, # 1 | ||
60 | $multiline3, # 2 | ||
61 | $multiline4, # 3 | ||
62 | $multiline5, # 4 | ||
63 | 4294965296, # 5 | ||
64 | 1000, # 6 | ||
65 | 4294965296, # 7 | ||
66 | uint64("18446744073709351616"), # 8 | ||
67 | int(rand(2**32)), # 9 | ||
68 | 64000, # 10 | ||
69 | "stringtests", # 11 | ||
70 | "3.5", # 12 | ||
71 | "87.4startswithnumberbutshouldbestring", # 13 | ||
72 | '555"I said"', # 14 | ||
73 | 'CUSTOM CHECK OK: foo is 12345', # 15 | ||
74 | '-2', # 16 | ||
75 | '-4', # 17 | ||
76 | '-6.6', # 18 | ||
77 | 42 # 19 | ||
78 | ); | ||
38 | # undef increments are randomized | 79 | # undef increments are randomized |
39 | my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef, 666, undef, undef, undef, undef, undef, -1, undef, undef, 0 ); | 80 | my @incrts = ( |
81 | undef, # 0 | ||
82 | undef, # 1 | ||
83 | undef, # 2 | ||
84 | undef, # 3 | ||
85 | undef, # 4 | ||
86 | 1000, # 5 | ||
87 | -500, # 6 | ||
88 | 1000, # 7 | ||
89 | 100000, # 8 | ||
90 | undef, # 9 | ||
91 | 666, # 10 | ||
92 | undef, # 11 | ||
93 | undef, # 12 | ||
94 | undef, # 13 | ||
95 | undef, # 14 | ||
96 | undef, # 15 | ||
97 | -1, # 16 | ||
98 | 0, # 17 | ||
99 | undef, # 18 | ||
100 | 0 # 19 | ||
101 | ); | ||
40 | 102 | ||
41 | # Number of elements in our OID | 103 | # Number of elements in our OID |
42 | my $oidelts; | 104 | my $oidelts; |
diff --git a/plugins/tests/conf/snmpd.conf b/plugins/tests/conf/snmpd.conf index eff5b0b3..1724c027 100644 --- a/plugins/tests/conf/snmpd.conf +++ b/plugins/tests/conf/snmpd.conf | |||
@@ -19,5 +19,5 @@ syscontact Alice | |||
19 | # Embedded Subagents | 19 | # Embedded Subagents |
20 | ############################################################################### | 20 | ############################################################################### |
21 | 21 | ||
22 | perl do "tests/check_snmp_agent.pl"; | 22 | perl do "./tests/check_snmp_agent.pl"; |
23 | 23 | ||
diff --git a/plugins/tests/test_check_disk.c b/plugins/tests/test_check_disk.c new file mode 100644 index 00000000..32b46c2d --- /dev/null +++ b/plugins/tests/test_check_disk.c | |||
@@ -0,0 +1,213 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 3 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | * | ||
16 | * | ||
17 | *****************************************************************************/ | ||
18 | |||
19 | #include "common.h" | ||
20 | #include "../check_disk.d/utils_disk.h" | ||
21 | #include "../../tap/tap.h" | ||
22 | #include "regex.h" | ||
23 | |||
24 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, | ||
25 | int expect, char *desc); | ||
26 | |||
27 | int main(int argc, char **argv) { | ||
28 | plan_tests(35); | ||
29 | |||
30 | struct name_list *exclude_filesystem = NULL; | ||
31 | ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); | ||
32 | np_add_name(&exclude_filesystem, "/var/log"); | ||
33 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); | ||
34 | ok(np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); | ||
35 | np_add_name(&exclude_filesystem, "/home"); | ||
36 | ok(np_find_name(exclude_filesystem, "/home") == true, "is in list now"); | ||
37 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); | ||
38 | |||
39 | struct name_list *exclude_fstype = NULL; | ||
40 | ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); | ||
41 | np_add_name(&exclude_fstype, "iso9660"); | ||
42 | ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); | ||
43 | |||
44 | ok(np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); | ||
45 | |||
46 | /* | ||
47 | for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { | ||
48 | printf("Name: %s\n", temp_name->name); | ||
49 | } | ||
50 | */ | ||
51 | |||
52 | struct mount_entry *dummy_mount_list; | ||
53 | struct mount_entry **mtail = &dummy_mount_list; | ||
54 | struct mount_entry *me = (struct mount_entry *)malloc(sizeof *me); | ||
55 | me->me_devname = strdup("/dev/c0t0d0s0"); | ||
56 | me->me_mountdir = strdup("/"); | ||
57 | *mtail = me; | ||
58 | mtail = &me->me_next; | ||
59 | |||
60 | me = (struct mount_entry *)malloc(sizeof *me); | ||
61 | me->me_devname = strdup("/dev/c1t0d1s0"); | ||
62 | me->me_mountdir = strdup("/var"); | ||
63 | *mtail = me; | ||
64 | mtail = &me->me_next; | ||
65 | |||
66 | me = (struct mount_entry *)malloc(sizeof *me); | ||
67 | me->me_devname = strdup("/dev/c2t0d0s0"); | ||
68 | me->me_mountdir = strdup("/home"); | ||
69 | *mtail = me; | ||
70 | mtail = &me->me_next; | ||
71 | |||
72 | int cflags = REG_NOSUB | REG_EXTENDED; | ||
73 | np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); | ||
74 | np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), cflags, 3, | ||
75 | strdup("regex on dev names:")); | ||
76 | np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, | ||
77 | strdup("regex on non existent dev/path:")); | ||
78 | np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), cflags | REG_ICASE, 0, | ||
79 | strdup("regi on non existent dev/path:")); | ||
80 | np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), cflags, 3, | ||
81 | strdup("partial devname regex match:")); | ||
82 | np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"), cflags, 1, | ||
83 | strdup("partial devname regex match:")); | ||
84 | np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"), cflags | REG_ICASE, 1, | ||
85 | strdup("partial devname regi match:")); | ||
86 | np_test_mount_entry_regex(dummy_mount_list, strdup("home"), cflags, 1, | ||
87 | strdup("partial pathname regex match:")); | ||
88 | np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"), cflags | REG_ICASE, 1, | ||
89 | strdup("partial pathname regi match:")); | ||
90 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, | ||
91 | strdup("grouped regex pathname match:")); | ||
92 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, | ||
93 | strdup("grouped regi pathname match:")); | ||
94 | |||
95 | filesystem_list test_paths = filesystem_list_init(); | ||
96 | mp_int_fs_list_append(&test_paths, "/home/groups"); | ||
97 | mp_int_fs_list_append(&test_paths, "/var"); | ||
98 | mp_int_fs_list_append(&test_paths, "/tmp"); | ||
99 | mp_int_fs_list_append(&test_paths, "/home/tonvoon"); | ||
100 | mp_int_fs_list_append(&test_paths, "/dev/c2t0d0s0"); | ||
101 | ok(test_paths.length == 5, "List counter works correctly with appends"); | ||
102 | |||
103 | mp_int_fs_list_set_best_match(test_paths, dummy_mount_list, false); | ||
104 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
105 | struct mount_entry *temp_me; | ||
106 | temp_me = p->best_match; | ||
107 | if (!strcmp(p->name, "/home/groups")) { | ||
108 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), | ||
109 | "/home/groups got right best match: /home"); | ||
110 | } else if (!strcmp(p->name, "/var")) { | ||
111 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); | ||
112 | } else if (!strcmp(p->name, "/tmp")) { | ||
113 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); | ||
114 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
115 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), | ||
116 | "/home/tonvoon got right best match: /home"); | ||
117 | } else if (!strcmp(p->name, "/dev/c2t0d0s0")) { | ||
118 | ok(temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), | ||
119 | "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
124 | mp_int_fs_list_del(&test_paths, p); | ||
125 | } | ||
126 | ok(test_paths.length == 0, "List delete sets counter properly"); | ||
127 | |||
128 | mp_int_fs_list_append(&test_paths, "/home/groups"); | ||
129 | mp_int_fs_list_append(&test_paths, "/var"); | ||
130 | mp_int_fs_list_append(&test_paths, "/tmp"); | ||
131 | mp_int_fs_list_append(&test_paths, "/home/tonvoon"); | ||
132 | mp_int_fs_list_append(&test_paths, "/home"); | ||
133 | |||
134 | mp_int_fs_list_set_best_match(test_paths, dummy_mount_list, true); | ||
135 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
136 | if (!strcmp(p->name, "/home/groups")) { | ||
137 | ok(!p->best_match, "/home/groups correctly not found"); | ||
138 | } else if (!strcmp(p->name, "/var")) { | ||
139 | ok(p->best_match, "/var found"); | ||
140 | } else if (!strcmp(p->name, "/tmp")) { | ||
141 | ok(!p->best_match, "/tmp correctly not found"); | ||
142 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
143 | ok(!p->best_match, "/home/tonvoon not found"); | ||
144 | } else if (!strcmp(p->name, "/home")) { | ||
145 | ok(p->best_match, "/home found"); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | bool found = false; | ||
150 | /* test deleting first element in paths */ | ||
151 | mp_int_fs_list_del(&test_paths, NULL); | ||
152 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
153 | if (!strcmp(p->name, "/home/groups")) { | ||
154 | found = true; | ||
155 | } | ||
156 | } | ||
157 | ok(!found, "first element successfully deleted"); | ||
158 | found = false; | ||
159 | |||
160 | parameter_list_elem *prev = NULL; | ||
161 | parameter_list_elem *p = NULL; | ||
162 | for (parameter_list_elem *path = test_paths.first; path; path = mp_int_fs_list_get_next(path)) { | ||
163 | if (!strcmp(path->name, "/tmp")) { | ||
164 | mp_int_fs_list_del(&test_paths, path); | ||
165 | } | ||
166 | p = path; | ||
167 | } | ||
168 | |||
169 | parameter_list_elem *last = NULL; | ||
170 | for (parameter_list_elem *path = test_paths.first; path; path = mp_int_fs_list_get_next(path)) { | ||
171 | if (!strcmp(path->name, "/tmp")) { | ||
172 | found = true; | ||
173 | } | ||
174 | if (path->next) { | ||
175 | prev = path; | ||
176 | } else { | ||
177 | last = path; | ||
178 | } | ||
179 | } | ||
180 | ok(!found, "/tmp element successfully deleted"); | ||
181 | |||
182 | int count = 0; | ||
183 | mp_int_fs_list_del(&test_paths, p); | ||
184 | for (p = test_paths.first; p; p = p->next) { | ||
185 | if (!strcmp(p->name, "/home")) { | ||
186 | found = true; | ||
187 | } | ||
188 | last = p; | ||
189 | count++; | ||
190 | } | ||
191 | ok(!found, "last (/home) element successfully deleted"); | ||
192 | ok(count == 2, "two elements remaining"); | ||
193 | |||
194 | return exit_status(); | ||
195 | } | ||
196 | |||
197 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, | ||
198 | int expect, char *desc) { | ||
199 | regex_t regex; | ||
200 | if (regcomp(®ex, regstr, cflags) == 0) { | ||
201 | int matches = 0; | ||
202 | for (struct mount_entry *me = dummy_mount_list; me; me = me->me_next) { | ||
203 | if (np_regex_match_mount_entry(me, ®ex)) { | ||
204 | matches++; | ||
205 | } | ||
206 | } | ||
207 | ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, | ||
208 | matches); | ||
209 | |||
210 | } else { | ||
211 | ok(false, "regex '%s' not compilable", regstr); | ||
212 | } | ||
213 | } | ||
diff --git a/plugins/tests/test_check_disk.t b/plugins/tests/test_check_disk.t new file mode 100755 index 00000000..56354650 --- /dev/null +++ b/plugins/tests/test_check_disk.t | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_check_disk") { | ||
4 | plan skip_all => "./test_check_disk not compiled - please enable libtap library to test"; | ||
5 | } | ||
6 | exec "./test_check_disk"; | ||
diff --git a/plugins/tests/test_check_snmp.c b/plugins/tests/test_check_snmp.c new file mode 100644 index 00000000..d71706d0 --- /dev/null +++ b/plugins/tests/test_check_snmp.c | |||
@@ -0,0 +1,175 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 3 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | * | ||
16 | * | ||
17 | *****************************************************************************/ | ||
18 | |||
19 | #include "tap.h" | ||
20 | #include "../../config.h" | ||
21 | |||
22 | #include <unistd.h> | ||
23 | #include <sys/types.h> | ||
24 | #include <sys/stat.h> | ||
25 | |||
26 | #include "utils_base.c" | ||
27 | #include "../check_snmp.d/check_snmp_helpers.h" | ||
28 | |||
29 | char *_np_state_generate_key(int argc, char **argv); | ||
30 | char *_np_state_calculate_location_prefix(void); | ||
31 | |||
32 | int main(int argc, char **argv) { | ||
33 | char *temp_string = (char *)_np_state_generate_key(argc, argv); | ||
34 | ok(!strcmp(temp_string, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), | ||
35 | "Got hash with exe and no parameters") || | ||
36 | diag("You are probably running in wrong directory. Must run as ./test_utils"); | ||
37 | |||
38 | int fake_argc = 4; | ||
39 | char *fake_argv[] = { | ||
40 | "./test_utils", | ||
41 | "here", | ||
42 | "--and", | ||
43 | "now", | ||
44 | }; | ||
45 | temp_string = (char *)_np_state_generate_key(fake_argc, fake_argv); | ||
46 | ok(!strcmp(temp_string, "bd72da9f78ff1419fad921ea5e43ce56508aef6c"), | ||
47 | "Got based on expected argv"); | ||
48 | |||
49 | unsetenv("MP_STATE_PATH"); | ||
50 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
51 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory"); | ||
52 | |||
53 | setenv("MP_STATE_PATH", "", 1); | ||
54 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
55 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string"); | ||
56 | |||
57 | setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1); | ||
58 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
59 | ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory"); | ||
60 | |||
61 | fake_argc = 1; | ||
62 | fake_argv[0] = "./test_utils"; | ||
63 | state_key temp_state_key1 = np_enable_state(NULL, 51, "check_test", fake_argc, fake_argv); | ||
64 | ok(!strcmp(temp_state_key1.plugin_name, "check_test"), "Got plugin name"); | ||
65 | ok(!strcmp(temp_state_key1.name, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), | ||
66 | "Got generated filename"); | ||
67 | |||
68 | state_key temp_state_key2 = | ||
69 | np_enable_state("allowedchars_in_keyname", 77, "check_snmp", fake_argc, fake_argv); | ||
70 | |||
71 | char state_path[1024]; | ||
72 | sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", | ||
73 | (unsigned long)geteuid()); | ||
74 | ok(!strcmp(temp_state_key2.plugin_name, "check_test"), "Got plugin name"); | ||
75 | ok(!strcmp(temp_state_key2.name, "allowedchars_in_keyname"), "Got key name with valid chars"); | ||
76 | ok(!strcmp(temp_state_key2._filename, state_path), "Got internal filename"); | ||
77 | |||
78 | /* Don't do this test just yet. Will die */ | ||
79 | /* | ||
80 | np_enable_state("bad^chars$in@here", 77); | ||
81 | temp_state_key = this_monitoring_plugin->state; | ||
82 | ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" | ||
83 | ); | ||
84 | */ | ||
85 | |||
86 | state_key temp_state_key3 = | ||
87 | np_enable_state("funnykeyname", 54, "check_snmp", fake_argc, fake_argv); | ||
88 | sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", | ||
89 | (unsigned long)geteuid()); | ||
90 | ok(!strcmp(temp_state_key3.plugin_name, "check_test"), "Got plugin name"); | ||
91 | ok(!strcmp(temp_state_key3.name, "funnykeyname"), "Got key name"); | ||
92 | |||
93 | ok(!strcmp(temp_state_key3._filename, state_path), "Got internal filename"); | ||
94 | ok(temp_state_key3.data_version == 54, "Version set"); | ||
95 | |||
96 | state_data *temp_state_data = np_state_read(temp_state_key3); | ||
97 | ok(temp_state_data == NULL, "Got no state data as file does not exist"); | ||
98 | |||
99 | /* | ||
100 | temp_fp = fopen("var/statefile", "r"); | ||
101 | if (temp_fp==NULL) | ||
102 | printf("Error opening. errno=%d\n", errno); | ||
103 | printf("temp_fp=%s\n", temp_fp); | ||
104 | ok( _np_state_read_file(temp_fp) == true, "Can read state file" ); | ||
105 | fclose(temp_fp); | ||
106 | */ | ||
107 | |||
108 | temp_state_key3._filename = "var/statefile"; | ||
109 | temp_state_data = np_state_read(temp_state_key3); | ||
110 | ok(temp_state_data != NULL, "Got state data now") || | ||
111 | diag("Are you running in right directory? Will get coredump next if not"); | ||
112 | ok(temp_state_data->time == 1234567890, "Got time"); | ||
113 | ok(!strcmp((char *)temp_state_data->data, "String to read"), "Data as expected"); | ||
114 | |||
115 | temp_state_key3.data_version = 53; | ||
116 | temp_state_data = np_state_read(temp_state_key3); | ||
117 | ok(temp_state_data == NULL, "Older data version gives NULL"); | ||
118 | temp_state_key3.data_version = 54; | ||
119 | |||
120 | temp_state_key3._filename = "var/nonexistent"; | ||
121 | temp_state_data = np_state_read(temp_state_key3); | ||
122 | ok(temp_state_data == NULL, "Missing file gives NULL"); | ||
123 | |||
124 | temp_state_key3._filename = "var/oldformat"; | ||
125 | temp_state_data = np_state_read(temp_state_key3); | ||
126 | ok(temp_state_data == NULL, "Old file format gives NULL"); | ||
127 | |||
128 | temp_state_key3._filename = "var/baddate"; | ||
129 | temp_state_data = np_state_read(temp_state_key3); | ||
130 | ok(temp_state_data == NULL, "Bad date gives NULL"); | ||
131 | |||
132 | temp_state_key3._filename = "var/missingdataline"; | ||
133 | temp_state_data = np_state_read(temp_state_key3); | ||
134 | ok(temp_state_data == NULL, "Missing data line gives NULL"); | ||
135 | |||
136 | unlink("var/generated"); | ||
137 | temp_state_key3._filename = "var/generated"; | ||
138 | |||
139 | time_t current_time = 1234567890; | ||
140 | np_state_write_string(temp_state_key3, current_time, "String to read"); | ||
141 | ok(system("cmp var/generated var/statefile") == 0, "Generated file same as expected"); | ||
142 | |||
143 | unlink("var/generated_directory/statefile"); | ||
144 | unlink("var/generated_directory"); | ||
145 | temp_state_key3._filename = "var/generated_directory/statefile"; | ||
146 | current_time = 1234567890; | ||
147 | np_state_write_string(temp_state_key3, current_time, "String to read"); | ||
148 | ok(system("cmp var/generated_directory/statefile var/statefile") == 0, | ||
149 | "Have created directory"); | ||
150 | |||
151 | /* This test to check cannot write to dir - can't automate yet */ | ||
152 | /* | ||
153 | unlink("var/generated_bad_dir"); | ||
154 | mkdir("var/generated_bad_dir", S_IRUSR); | ||
155 | np_state_write_string(current_time, "String to read"); | ||
156 | */ | ||
157 | |||
158 | temp_state_key3._filename = "var/generated"; | ||
159 | time(¤t_time); | ||
160 | np_state_write_string(temp_state_key3, 0, "String to read"); | ||
161 | temp_state_data = np_state_read(temp_state_key3); | ||
162 | /* Check time is set to current_time */ | ||
163 | ok(system("cmp var/generated var/statefile > /dev/null") != 0, | ||
164 | "Generated file should be different this time"); | ||
165 | ok(temp_state_data->time - current_time <= 1, "Has time generated from current time"); | ||
166 | |||
167 | /* Don't know how to automatically test this. Need to be able to redefine die and catch the | ||
168 | * error */ | ||
169 | /* | ||
170 | temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write"; | ||
171 | np_state_write_string(0, "Bad file"); | ||
172 | */ | ||
173 | |||
174 | np_cleanup(); | ||
175 | } | ||
diff --git a/plugins/tests/test_check_snmp.t b/plugins/tests/test_check_snmp.t new file mode 100755 index 00000000..967633e9 --- /dev/null +++ b/plugins/tests/test_check_snmp.t | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_check_snmp") { | ||
4 | plan skip_all => "./test_check_snmp not compiled - please enable libtap library to test"; | ||
5 | } | ||
6 | exec "./test_check_snmp"; | ||
diff --git a/plugins/tests/test_check_swap.c b/plugins/tests/test_check_swap.c index b85fb4ad..94d56ce7 100644 --- a/plugins/tests/test_check_swap.c +++ b/plugins/tests/test_check_swap.c | |||
@@ -5,9 +5,7 @@ | |||
5 | int verbose = 0; | 5 | int verbose = 0; |
6 | 6 | ||
7 | void print_usage(void) {} | 7 | void print_usage(void) {} |
8 | void print_help(swap_config config) { | 8 | void print_help(swap_config config) { (void)config; } |
9 | (void) config; | ||
10 | } | ||
11 | 9 | ||
12 | const char *progname = "test_check_swap"; | 10 | const char *progname = "test_check_swap"; |
13 | 11 | ||
diff --git a/plugins/urlize.c b/plugins/urlize.c index 1aa4e425..a8590fae 100644 --- a/plugins/urlize.c +++ b/plugins/urlize.c | |||
@@ -53,8 +53,10 @@ int main(int argc, char **argv) { | |||
53 | 53 | ||
54 | int c; | 54 | int c; |
55 | int option = 0; | 55 | int option = 0; |
56 | static struct option longopts[] = { | 56 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, |
57 | {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"url", required_argument, 0, 'u'}, {0, 0, 0, 0}}; | 57 | {"version", no_argument, 0, 'V'}, |
58 | {"url", required_argument, 0, 'u'}, | ||
59 | {0, 0, 0, 0}}; | ||
58 | 60 | ||
59 | setlocale(LC_ALL, ""); | 61 | setlocale(LC_ALL, ""); |
60 | bindtextdomain(PACKAGE, LOCALEDIR); | 62 | bindtextdomain(PACKAGE, LOCALEDIR); |
@@ -69,8 +71,9 @@ int main(int argc, char **argv) { | |||
69 | while (1) { | 71 | while (1) { |
70 | c = getopt_long(argc, argv, "+hVu:", longopts, &option); | 72 | c = getopt_long(argc, argv, "+hVu:", longopts, &option); |
71 | 73 | ||
72 | if (c == -1 || c == EOF) | 74 | if (c == -1 || c == EOF) { |
73 | break; | 75 | break; |
76 | } | ||
74 | 77 | ||
75 | switch (c) { | 78 | switch (c) { |
76 | case 'h': /* help */ | 79 | case 'h': /* help */ |
@@ -90,8 +93,9 @@ int main(int argc, char **argv) { | |||
90 | } | 93 | } |
91 | } | 94 | } |
92 | 95 | ||
93 | if (url == NULL) | 96 | if (url == NULL) { |
94 | url = strdup(argv[optind++]); | 97 | url = strdup(argv[optind++]); |
98 | } | ||
95 | 99 | ||
96 | cmd = strdup(argv[optind++]); | 100 | cmd = strdup(argv[optind++]); |
97 | for (c = optind; c < argc; c++) { | 101 | for (c = optind; c < argc; c++) { |
@@ -118,27 +122,32 @@ int main(int argc, char **argv) { | |||
118 | strcat(tstr, buf); | 122 | strcat(tstr, buf); |
119 | } | 123 | } |
120 | 124 | ||
121 | if (!found) | 125 | if (!found) { |
122 | die(STATE_UNKNOWN, _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"), argv[0], cmd); | 126 | die(STATE_UNKNOWN, _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"), argv[0], |
127 | cmd); | ||
128 | } | ||
123 | 129 | ||
124 | /* chop the newline character */ | 130 | /* chop the newline character */ |
125 | if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL) | 131 | if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL) { |
126 | *nstr = '\0'; | 132 | *nstr = '\0'; |
133 | } | ||
127 | 134 | ||
128 | /* tokenize the string for Perfdata if there is some */ | 135 | /* tokenize the string for Perfdata if there is some */ |
129 | nstr = strtok(tstr, PERF_CHARACTER); | 136 | nstr = strtok(tstr, PERF_CHARACTER); |
130 | printf("%s", nstr); | 137 | printf("%s", nstr); |
131 | printf("</A>"); | 138 | printf("</A>"); |
132 | nstr = strtok(NULL, PERF_CHARACTER); | 139 | nstr = strtok(NULL, PERF_CHARACTER); |
133 | if (nstr != NULL) | 140 | if (nstr != NULL) { |
134 | printf(" | %s", nstr); | 141 | printf(" | %s", nstr); |
142 | } | ||
135 | 143 | ||
136 | /* close the pipe */ | 144 | /* close the pipe */ |
137 | result = spclose(child_process); | 145 | result = spclose(child_process); |
138 | 146 | ||
139 | /* WARNING if output found on stderr */ | 147 | /* WARNING if output found on stderr */ |
140 | if (fgets(buf, MAX_INPUT_BUFFER - 1, child_stderr)) | 148 | if (fgets(buf, MAX_INPUT_BUFFER - 1, child_stderr)) { |
141 | result = max_state(result, STATE_WARNING); | 149 | result = max_state(result, STATE_WARNING); |
150 | } | ||
142 | 151 | ||
143 | /* close stderr */ | 152 | /* close stderr */ |
144 | (void)fclose(child_stderr); | 153 | (void)fclose(child_stderr); |
@@ -153,8 +162,10 @@ void print_help(void) { | |||
153 | printf(COPYRIGHT, copyright, email); | 162 | printf(COPYRIGHT, copyright, email); |
154 | 163 | ||
155 | printf("%s\n", _("This plugin wraps the text output of another command (plugin) in HTML <A>")); | 164 | printf("%s\n", _("This plugin wraps the text output of another command (plugin) in HTML <A>")); |
156 | printf("%s\n", _("tags, thus displaying the child plugin's output as a clickable link in compatible")); | 165 | printf("%s\n", |
157 | printf("%s\n", _("monitoring status screen. This plugin returns the status of the invoked plugin.")); | 166 | _("tags, thus displaying the child plugin's output as a clickable link in compatible")); |
167 | printf("%s\n", | ||
168 | _("monitoring status screen. This plugin returns the status of the invoked plugin.")); | ||
158 | 169 | ||
159 | printf("\n\n"); | 170 | printf("\n\n"); |
160 | 171 | ||
@@ -164,7 +175,8 @@ void print_help(void) { | |||
164 | 175 | ||
165 | printf("\n"); | 176 | printf("\n"); |
166 | printf("%s\n", _("Examples:")); | 177 | printf("%s\n", _("Examples:")); |
167 | printf("%s\n", _("Pay close attention to quoting to ensure that the shell passes the expected")); | 178 | printf("%s\n", |
179 | _("Pay close attention to quoting to ensure that the shell passes the expected")); | ||
168 | printf("%s\n\n", _("data to the plugin. For example, in:")); | 180 | printf("%s\n\n", _("data to the plugin. For example, in:")); |
169 | printf(" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'")); | 181 | printf(" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'")); |
170 | printf(" %s\n", _("the shell will remove the single quotes and urlize will see:")); | 182 | printf(" %s\n", _("the shell will remove the single quotes and urlize will see:")); |
diff --git a/plugins/utils.c b/plugins/utils.c index 34335c89..41fe5fcf 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -285,7 +285,8 @@ double delta_time(struct timeval tv) { | |||
285 | struct timeval now; | 285 | struct timeval now; |
286 | 286 | ||
287 | gettimeofday(&now, NULL); | 287 | gettimeofday(&now, NULL); |
288 | return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000); | 288 | return ((double)(now.tv_sec - tv.tv_sec) + |
289 | (double)(now.tv_usec - tv.tv_usec) / (double)1000000); | ||
289 | } | 290 | } |
290 | 291 | ||
291 | long deltime(struct timeval tv) { | 292 | long deltime(struct timeval tv) { |
@@ -507,8 +508,8 @@ int xasprintf(char **strp, const char *fmt, ...) { | |||
507 | * | 508 | * |
508 | ******************************************************************************/ | 509 | ******************************************************************************/ |
509 | 510 | ||
510 | char *perfdata(const char *label, long int val, const char *uom, bool warnp, long int warn, bool critp, long int crit, bool minp, | 511 | char *perfdata(const char *label, long int val, const char *uom, bool warnp, long int warn, |
511 | long int minv, bool maxp, long int maxv) { | 512 | bool critp, long int crit, bool minp, long int minv, bool maxp, long int maxv) { |
512 | char *data = NULL; | 513 | char *data = NULL; |
513 | 514 | ||
514 | if (strpbrk(label, "'= ")) { | 515 | if (strpbrk(label, "'= ")) { |
@@ -542,10 +543,11 @@ char *perfdata(const char *label, long int val, const char *uom, bool warnp, lon | |||
542 | return data; | 543 | return data; |
543 | } | 544 | } |
544 | 545 | ||
545 | char *perfdata_uint64(const char *label, uint64_t val, const char *uom, bool warnp, /* Warning present */ | 546 | char *perfdata_uint64(const char *label, uint64_t val, const char *uom, |
546 | uint64_t warn, bool critp, /* Critical present */ | 547 | bool warnp, /* Warning present */ |
547 | uint64_t crit, bool minp, /* Minimum present */ | 548 | uint64_t warn, bool critp, /* Critical present */ |
548 | uint64_t minv, bool maxp, /* Maximum present */ | 549 | uint64_t crit, bool minp, /* Minimum present */ |
550 | uint64_t minv, bool maxp, /* Maximum present */ | ||
549 | uint64_t maxv) { | 551 | uint64_t maxv) { |
550 | char *data = NULL; | 552 | char *data = NULL; |
551 | 553 | ||
@@ -580,10 +582,11 @@ char *perfdata_uint64(const char *label, uint64_t val, const char *uom, bool war | |||
580 | return data; | 582 | return data; |
581 | } | 583 | } |
582 | 584 | ||
583 | char *perfdata_int64(const char *label, int64_t val, const char *uom, bool warnp, /* Warning present */ | 585 | char *perfdata_int64(const char *label, int64_t val, const char *uom, |
584 | int64_t warn, bool critp, /* Critical present */ | 586 | bool warnp, /* Warning present */ |
585 | int64_t crit, bool minp, /* Minimum present */ | 587 | int64_t warn, bool critp, /* Critical present */ |
586 | int64_t minv, bool maxp, /* Maximum present */ | 588 | int64_t crit, bool minp, /* Minimum present */ |
589 | int64_t minv, bool maxp, /* Maximum present */ | ||
587 | int64_t maxv) { | 590 | int64_t maxv) { |
588 | char *data = NULL; | 591 | char *data = NULL; |
589 | 592 | ||
@@ -618,8 +621,8 @@ char *perfdata_int64(const char *label, int64_t val, const char *uom, bool warnp | |||
618 | return data; | 621 | return data; |
619 | } | 622 | } |
620 | 623 | ||
621 | char *fperfdata(const char *label, double val, const char *uom, bool warnp, double warn, bool critp, double crit, bool minp, double minv, | 624 | char *fperfdata(const char *label, double val, const char *uom, bool warnp, double warn, bool critp, |
622 | bool maxp, double maxv) { | 625 | double crit, bool minp, double minv, bool maxp, double maxv) { |
623 | char *data = NULL; | 626 | char *data = NULL; |
624 | 627 | ||
625 | if (strpbrk(label, "'= ")) { | 628 | if (strpbrk(label, "'= ")) { |
@@ -655,7 +658,8 @@ char *fperfdata(const char *label, double val, const char *uom, bool warnp, doub | |||
655 | return data; | 658 | return data; |
656 | } | 659 | } |
657 | 660 | ||
658 | char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, bool minp, double minv, bool maxp, double maxv) { | 661 | char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, bool minp, |
662 | double minv, bool maxp, double maxv) { | ||
659 | char *data = NULL; | 663 | char *data = NULL; |
660 | if (strpbrk(label, "'= ")) { | 664 | if (strpbrk(label, "'= ")) { |
661 | xasprintf(&data, "'%s'=", label); | 665 | xasprintf(&data, "'%s'=", label); |
@@ -690,7 +694,8 @@ char *sperfdata(const char *label, double val, const char *uom, char *warn, char | |||
690 | return data; | 694 | return data; |
691 | } | 695 | } |
692 | 696 | ||
693 | char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, bool minp, int minv, bool maxp, int maxv) { | 697 | char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, bool minp, |
698 | int minv, bool maxp, int maxv) { | ||
694 | char *data = NULL; | 699 | char *data = NULL; |
695 | if (strpbrk(label, "'= ")) { | 700 | if (strpbrk(label, "'= ")) { |
696 | xasprintf(&data, "'%s'=", label); | 701 | xasprintf(&data, "'%s'=", label); |
diff --git a/plugins/utils.h b/plugins/utils.h index 92a6c115..1f0e021b 100644 --- a/plugins/utils.h +++ b/plugins/utils.h | |||
@@ -76,7 +76,7 @@ char *strnl(char *); | |||
76 | char *strpcpy(char *, const char *, const char *); | 76 | char *strpcpy(char *, const char *, const char *); |
77 | char *strpcat(char *, const char *, const char *); | 77 | char *strpcat(char *, const char *, const char *); |
78 | int xvasprintf(char **strp, const char *fmt, va_list ap); | 78 | int xvasprintf(char **strp, const char *fmt, va_list ap); |
79 | int xasprintf(char **strp, const char *fmt, ...); | 79 | int xasprintf(char **strp, const char *fmt, ...) __attribute__((format(printf, 2, 3))); |
80 | 80 | ||
81 | void usage(const char *) __attribute__((noreturn)); | 81 | void usage(const char *) __attribute__((noreturn)); |
82 | void usage2(const char *, const char *) __attribute__((noreturn)); | 82 | void usage2(const char *, const char *) __attribute__((noreturn)); |
@@ -88,13 +88,17 @@ void usage_va(const char *fmt, ...) __attribute__((noreturn)); | |||
88 | #define max(a, b) (((a) > (b)) ? (a) : (b)) | 88 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
89 | #define min(a, b) (((a) < (b)) ? (a) : (b)) | 89 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
90 | 90 | ||
91 | char *perfdata(const char *, long int, const char *, bool, long int, bool, long int, bool, long int, bool, long int); | 91 | char *perfdata(const char *, long int, const char *, bool, long int, bool, long int, bool, long int, |
92 | bool, long int); | ||
92 | 93 | ||
93 | char *perfdata_uint64(const char *, uint64_t, const char *, bool, uint64_t, bool, uint64_t, bool, uint64_t, bool, uint64_t); | 94 | char *perfdata_uint64(const char *, uint64_t, const char *, bool, uint64_t, bool, uint64_t, bool, |
95 | uint64_t, bool, uint64_t); | ||
94 | 96 | ||
95 | char *perfdata_int64(const char *, int64_t, const char *, bool, int64_t, bool, int64_t, bool, int64_t, bool, int64_t); | 97 | char *perfdata_int64(const char *, int64_t, const char *, bool, int64_t, bool, int64_t, bool, |
98 | int64_t, bool, int64_t); | ||
96 | 99 | ||
97 | char *fperfdata(const char *, double, const char *, bool, double, bool, double, bool, double, bool, double); | 100 | char *fperfdata(const char *, double, const char *, bool, double, bool, double, bool, double, bool, |
101 | double); | ||
98 | 102 | ||
99 | char *sperfdata(const char *, double, const char *, char *, char *, bool, double, bool, double); | 103 | char *sperfdata(const char *, double, const char *, char *, char *, bool, double, bool, double); |
100 | 104 | ||
@@ -104,21 +108,22 @@ char *sperfdata_int(const char *, int, const char *, char *, char *, bool, int, | |||
104 | most will or should. Therefore, for consistency, these very common | 108 | most will or should. Therefore, for consistency, these very common |
105 | options should have only these meanings throughout the overall suite */ | 109 | options should have only these meanings throughout the overall suite */ |
106 | 110 | ||
107 | #define STD_LONG_OPTS \ | 111 | #define STD_LONG_OPTS \ |
108 | {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, \ | 112 | {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, \ |
109 | {"timeout", required_argument, 0, 't'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, \ | 113 | {"help", no_argument, 0, 'h'}, {"timeout", required_argument, 0, 't'}, \ |
114 | {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, \ | ||
110 | {"hostname", required_argument, 0, 'H'} | 115 | {"hostname", required_argument, 0, 'H'} |
111 | 116 | ||
112 | #define COPYRIGHT \ | 117 | #define COPYRIGHT \ |
113 | "Copyright (c) %s Monitoring Plugins Development Team\n\ | 118 | "Copyright (c) %s Monitoring Plugins Development Team\n\ |
114 | \t<%s>\n\n" | 119 | \t<%s>\n\n" |
115 | 120 | ||
116 | #define UT_HLP_VRS \ | 121 | #define UT_HLP_VRS \ |
117 | _("\ | 122 | _("\ |
118 | %s (-h | --help) for detailed help\n\ | 123 | %s (-h | --help) for detailed help\n\ |
119 | %s (-V | --version) for version information\n") | 124 | %s (-V | --version) for version information\n") |
120 | 125 | ||
121 | #define UT_HELP_VRSN \ | 126 | #define UT_HELP_VRSN \ |
122 | _("\ | 127 | _("\ |
123 | \nOptions:\n\ | 128 | \nOptions:\n\ |
124 | -h, --help\n\ | 129 | -h, --help\n\ |
@@ -126,52 +131,52 @@ char *sperfdata_int(const char *, int, const char *, char *, char *, bool, int, | |||
126 | -V, --version\n\ | 131 | -V, --version\n\ |
127 | Print version information\n") | 132 | Print version information\n") |
128 | 133 | ||
129 | #define UT_HOST_PORT \ | 134 | #define UT_HOST_PORT \ |
130 | _("\ | 135 | _("\ |
131 | -H, --hostname=ADDRESS\n\ | 136 | -H, --hostname=ADDRESS\n\ |
132 | Host name, IP Address, or unix socket (must be an absolute path)\n\ | 137 | Host name, IP Address, or unix socket (must be an absolute path)\n\ |
133 | -%c, --port=INTEGER\n\ | 138 | -%c, --port=INTEGER\n\ |
134 | Port number (default: %s)\n") | 139 | Port number (default: %s)\n") |
135 | 140 | ||
136 | #define UT_IPv46 \ | 141 | #define UT_IPv46 \ |
137 | _("\ | 142 | _("\ |
138 | -4, --use-ipv4\n\ | 143 | -4, --use-ipv4\n\ |
139 | Use IPv4 connection\n\ | 144 | Use IPv4 connection\n\ |
140 | -6, --use-ipv6\n\ | 145 | -6, --use-ipv6\n\ |
141 | Use IPv6 connection\n") | 146 | Use IPv6 connection\n") |
142 | 147 | ||
143 | #define UT_VERBOSE \ | 148 | #define UT_VERBOSE \ |
144 | _("\ | 149 | _("\ |
145 | -v, --verbose\n\ | 150 | -v, --verbose\n\ |
146 | Show details for command-line debugging (output may be truncated by\n\ | 151 | Show details for command-line debugging (output may be truncated by\n\ |
147 | the monitoring system)\n") | 152 | the monitoring system)\n") |
148 | 153 | ||
149 | #define UT_WARN_CRIT \ | 154 | #define UT_WARN_CRIT \ |
150 | _("\ | 155 | _("\ |
151 | -w, --warning=DOUBLE\n\ | 156 | -w, --warning=DOUBLE\n\ |
152 | Response time to result in warning status (seconds)\n\ | 157 | Response time to result in warning status (seconds)\n\ |
153 | -c, --critical=DOUBLE\n\ | 158 | -c, --critical=DOUBLE\n\ |
154 | Response time to result in critical status (seconds)\n") | 159 | Response time to result in critical status (seconds)\n") |
155 | 160 | ||
156 | #define UT_WARN_CRIT_RANGE \ | 161 | #define UT_WARN_CRIT_RANGE \ |
157 | _("\ | 162 | _("\ |
158 | -w, --warning=RANGE\n\ | 163 | -w, --warning=RANGE\n\ |
159 | Warning range (format: start:end). Alert if outside this range\n\ | 164 | Warning range (format: start:end). Alert if outside this range\n\ |
160 | -c, --critical=RANGE\n\ | 165 | -c, --critical=RANGE\n\ |
161 | Critical range\n") | 166 | Critical range\n") |
162 | 167 | ||
163 | #define UT_CONN_TIMEOUT \ | 168 | #define UT_CONN_TIMEOUT \ |
164 | _("\ | 169 | _("\ |
165 | -t, --timeout=INTEGER\n\ | 170 | -t, --timeout=INTEGER\n\ |
166 | Seconds before connection times out (default: %d)\n") | 171 | Seconds before connection times out (default: %d)\n") |
167 | 172 | ||
168 | #define UT_PLUG_TIMEOUT \ | 173 | #define UT_PLUG_TIMEOUT \ |
169 | _("\ | 174 | _("\ |
170 | -t, --timeout=INTEGER\n\ | 175 | -t, --timeout=INTEGER\n\ |
171 | Seconds before plugin times out (default: %d)\n") | 176 | Seconds before plugin times out (default: %d)\n") |
172 | 177 | ||
173 | #ifdef NP_EXTRA_OPTS | 178 | #ifdef NP_EXTRA_OPTS |
174 | # define UT_EXTRA_OPTS \ | 179 | # define UT_EXTRA_OPTS \ |
175 | _("\ | 180 | _("\ |
176 | --extra-opts=[section][@file]\n\ | 181 | --extra-opts=[section][@file]\n\ |
177 | Read options from an ini file. See\n\ | 182 | Read options from an ini file. See\n\ |
@@ -181,25 +186,25 @@ char *sperfdata_int(const char *, int, const char *, char *, char *, bool, int, | |||
181 | # define UT_EXTRA_OPTS " \b" | 186 | # define UT_EXTRA_OPTS " \b" |
182 | #endif | 187 | #endif |
183 | 188 | ||
184 | #define UT_THRESHOLDS_NOTES \ | 189 | #define UT_THRESHOLDS_NOTES \ |
185 | _("\ | 190 | _("\ |
186 | See:\n\ | 191 | See:\n\ |
187 | https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n\ | 192 | https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n\ |
188 | for THRESHOLD format and examples.\n") | 193 | for THRESHOLD format and examples.\n") |
189 | 194 | ||
190 | #define UT_SUPPORT \ | 195 | #define UT_SUPPORT \ |
191 | _("\n\ | 196 | _("\n\ |
192 | Send email to help@monitoring-plugins.org if you have questions regarding\n\ | 197 | Send email to help@monitoring-plugins.org if you have questions regarding\n\ |
193 | use of this software. To submit patches or suggest improvements, send email\n\ | 198 | use of this software. To submit patches or suggest improvements, send email\n\ |
194 | to devel@monitoring-plugins.org\n\n") | 199 | to devel@monitoring-plugins.org\n\n") |
195 | 200 | ||
196 | #define UT_NOWARRANTY \ | 201 | #define UT_NOWARRANTY \ |
197 | _("\n\ | 202 | _("\n\ |
198 | The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ | 203 | The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ |
199 | copies of the plugins under the terms of the GNU General Public License.\n\ | 204 | copies of the plugins under the terms of the GNU General Public License.\n\ |
200 | For more information about these matters, see the file named COPYING.\n") | 205 | For more information about these matters, see the file named COPYING.\n") |
201 | 206 | ||
202 | #define UT_OUTPUT_FORMAT \ | 207 | #define UT_OUTPUT_FORMAT \ |
203 | _("\ | 208 | _("\ |
204 | --output-format=OUTPUT_FORMAT\n\ | 209 | --output-format=OUTPUT_FORMAT\n\ |
205 | Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") | 210 | Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") |