diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-09 01:57:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-09-09 01:57:25 +0200 |
commit | 615fec347cd575c0ee4343aa17ee99962f375f64 (patch) | |
tree | a3603d9006ecd9ed9dadc33aa3e9eeb25dbb5113 /lib | |
parent | 5c81b8e2ab3df0b8c56bf5ab5b30b15a816a1112 (diff) | |
parent | c43f845c22a9e879546472aa9051d7ca0803ef2a (diff) | |
download | monitoring-plugins-615fec347cd575c0ee4343aa17ee99962f375f64.tar.gz |
Merge pull request #2144 from RincewindsHat/refactor/check_snmp
Refactor check snmp:
- Switch from executing `snmpget`/`snmpgetnext` to linking directly agains net-snmp
- Refactor to use test abstraction -> allows for JSON output
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/perfdata.c | 72 | ||||
-rw-r--r-- | lib/perfdata.h | 2 | ||||
-rw-r--r-- | lib/tests/test_utils.c | 190 | ||||
-rw-r--r-- | lib/utils_base.c | 438 | ||||
-rw-r--r-- | lib/utils_base.h | 22 |
6 files changed, 128 insertions, 598 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index a9f3ff40..27a08278 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -4,7 +4,7 @@ SUBDIRS = . tests | |||
4 | 4 | ||
5 | noinst_LIBRARIES = libmonitoringplug.a | 5 | noinst_LIBRARIES = libmonitoringplug.a |
6 | 6 | ||
7 | AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ | 7 | AM_CPPFLAGS = \ |
8 | -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins | 8 | -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins |
9 | 9 | ||
10 | libmonitoringplug_a_SOURCES = utils_base.c utils_tcp.c utils_cmd.c maxfd.c output.c perfdata.c output.c thresholds.c vendor/cJSON/cJSON.c | 10 | libmonitoringplug_a_SOURCES = utils_base.c utils_tcp.c utils_cmd.c maxfd.c output.c perfdata.c output.c thresholds.c vendor/cJSON/cJSON.c |
diff --git a/lib/perfdata.c b/lib/perfdata.c index b87de7e0..2930a8bc 100644 --- a/lib/perfdata.c +++ b/lib/perfdata.c | |||
@@ -33,7 +33,18 @@ char *pd_value_to_string(const mp_perfdata_value pd) { | |||
33 | char *pd_to_string(mp_perfdata pd) { | 33 | char *pd_to_string(mp_perfdata pd) { |
34 | assert(pd.label != NULL); | 34 | assert(pd.label != NULL); |
35 | char *result = NULL; | 35 | char *result = NULL; |
36 | asprintf(&result, "'%s'=", pd.label); | 36 | |
37 | if (strchr(pd.label, '\'') == NULL) { | ||
38 | asprintf(&result, "'%s'=", pd.label); | ||
39 | } else { | ||
40 | // we have a illegal single quote in the string | ||
41 | // replace it silently instead of complaining | ||
42 | for (char *ptr = pd.label; *ptr == '\0'; ptr++) { | ||
43 | if (*ptr == '\'') { | ||
44 | *ptr = '_'; | ||
45 | } | ||
46 | } | ||
47 | } | ||
37 | 48 | ||
38 | asprintf(&result, "%s%s", result, pd_value_to_string(pd.value)); | 49 | asprintf(&result, "%s%s", result, pd_value_to_string(pd.value)); |
39 | 50 | ||
@@ -249,7 +260,9 @@ char *mp_range_to_string(const mp_range input) { | |||
249 | return result; | 260 | return result; |
250 | } | 261 | } |
251 | 262 | ||
252 | mp_perfdata mp_set_pd_value_float(mp_perfdata pd, float value) { return mp_set_pd_value_double(pd, value); } | 263 | mp_perfdata mp_set_pd_value_float(mp_perfdata pd, float value) { |
264 | return mp_set_pd_value_double(pd, value); | ||
265 | } | ||
253 | 266 | ||
254 | mp_perfdata mp_set_pd_value_double(mp_perfdata pd, double value) { | 267 | mp_perfdata mp_set_pd_value_double(mp_perfdata pd, double value) { |
255 | pd.value.pd_double = value; | 268 | pd.value.pd_double = value; |
@@ -257,15 +270,25 @@ mp_perfdata mp_set_pd_value_double(mp_perfdata pd, double value) { | |||
257 | return pd; | 270 | return pd; |
258 | } | 271 | } |
259 | 272 | ||
260 | mp_perfdata mp_set_pd_value_char(mp_perfdata pd, char value) { return mp_set_pd_value_long_long(pd, (long long)value); } | 273 | mp_perfdata mp_set_pd_value_char(mp_perfdata pd, char value) { |
274 | return mp_set_pd_value_long_long(pd, (long long)value); | ||
275 | } | ||
261 | 276 | ||
262 | mp_perfdata mp_set_pd_value_u_char(mp_perfdata pd, unsigned char value) { return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); } | 277 | mp_perfdata mp_set_pd_value_u_char(mp_perfdata pd, unsigned char value) { |
278 | return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); | ||
279 | } | ||
263 | 280 | ||
264 | mp_perfdata mp_set_pd_value_int(mp_perfdata pd, int value) { return mp_set_pd_value_long_long(pd, (long long)value); } | 281 | mp_perfdata mp_set_pd_value_int(mp_perfdata pd, int value) { |
282 | return mp_set_pd_value_long_long(pd, (long long)value); | ||
283 | } | ||
265 | 284 | ||
266 | mp_perfdata mp_set_pd_value_u_int(mp_perfdata pd, unsigned int value) { return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); } | 285 | mp_perfdata mp_set_pd_value_u_int(mp_perfdata pd, unsigned int value) { |
286 | return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); | ||
287 | } | ||
267 | 288 | ||
268 | mp_perfdata mp_set_pd_value_long(mp_perfdata pd, long value) { return mp_set_pd_value_long_long(pd, (long long)value); } | 289 | mp_perfdata mp_set_pd_value_long(mp_perfdata pd, long value) { |
290 | return mp_set_pd_value_long_long(pd, (long long)value); | ||
291 | } | ||
269 | 292 | ||
270 | mp_perfdata mp_set_pd_value_u_long(mp_perfdata pd, unsigned long value) { | 293 | mp_perfdata mp_set_pd_value_u_long(mp_perfdata pd, unsigned long value) { |
271 | return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); | 294 | return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); |
@@ -290,19 +313,33 @@ mp_perfdata_value mp_create_pd_value_double(double value) { | |||
290 | return res; | 313 | return res; |
291 | } | 314 | } |
292 | 315 | ||
293 | mp_perfdata_value mp_create_pd_value_float(float value) { return mp_create_pd_value_double((double)value); } | 316 | mp_perfdata_value mp_create_pd_value_float(float value) { |
317 | return mp_create_pd_value_double((double)value); | ||
318 | } | ||
294 | 319 | ||
295 | mp_perfdata_value mp_create_pd_value_char(char value) { return mp_create_pd_value_long_long((long long)value); } | 320 | mp_perfdata_value mp_create_pd_value_char(char value) { |
321 | return mp_create_pd_value_long_long((long long)value); | ||
322 | } | ||
296 | 323 | ||
297 | mp_perfdata_value mp_create_pd_value_u_char(unsigned char value) { return mp_create_pd_value_u_long_long((unsigned long long)value); } | 324 | mp_perfdata_value mp_create_pd_value_u_char(unsigned char value) { |
325 | return mp_create_pd_value_u_long_long((unsigned long long)value); | ||
326 | } | ||
298 | 327 | ||
299 | mp_perfdata_value mp_create_pd_value_int(int value) { return mp_create_pd_value_long_long((long long)value); } | 328 | mp_perfdata_value mp_create_pd_value_int(int value) { |
329 | return mp_create_pd_value_long_long((long long)value); | ||
330 | } | ||
300 | 331 | ||
301 | mp_perfdata_value mp_create_pd_value_u_int(unsigned int value) { return mp_create_pd_value_u_long_long((unsigned long long)value); } | 332 | mp_perfdata_value mp_create_pd_value_u_int(unsigned int value) { |
333 | return mp_create_pd_value_u_long_long((unsigned long long)value); | ||
334 | } | ||
302 | 335 | ||
303 | mp_perfdata_value mp_create_pd_value_long(long value) { return mp_create_pd_value_long_long((long long)value); } | 336 | mp_perfdata_value mp_create_pd_value_long(long value) { |
337 | return mp_create_pd_value_long_long((long long)value); | ||
338 | } | ||
304 | 339 | ||
305 | mp_perfdata_value mp_create_pd_value_u_long(unsigned long value) { return mp_create_pd_value_u_long_long((unsigned long long)value); } | 340 | mp_perfdata_value mp_create_pd_value_u_long(unsigned long value) { |
341 | return mp_create_pd_value_u_long_long((unsigned long long)value); | ||
342 | } | ||
306 | 343 | ||
307 | mp_perfdata_value mp_create_pd_value_long_long(long long value) { | 344 | mp_perfdata_value mp_create_pd_value_long_long(long long value) { |
308 | mp_perfdata_value res = {0}; | 345 | mp_perfdata_value res = {0}; |
@@ -368,6 +405,13 @@ mp_range_parsed mp_parse_range_string(const char *input) { | |||
368 | } | 405 | } |
369 | 406 | ||
370 | char *working_copy = strdup(input); | 407 | char *working_copy = strdup(input); |
408 | if (working_copy == NULL) { | ||
409 | // strdup error, probably | ||
410 | mp_range_parsed result = { | ||
411 | .error = MP_RANGE_PARSING_FAILURE, | ||
412 | }; | ||
413 | return result; | ||
414 | } | ||
371 | input = working_copy; | 415 | input = working_copy; |
372 | 416 | ||
373 | char *separator = index(working_copy, ':'); | 417 | char *separator = index(working_copy, ':'); |
diff --git a/lib/perfdata.h b/lib/perfdata.h index 7fd908a9..c5d4a61d 100644 --- a/lib/perfdata.h +++ b/lib/perfdata.h | |||
@@ -45,7 +45,7 @@ typedef struct range_struct { | |||
45 | double start; | 45 | double start; |
46 | bool start_infinity; | 46 | bool start_infinity; |
47 | double end; | 47 | double end; |
48 | int end_infinity; | 48 | bool end_infinity; |
49 | int alert_on; /* OUTSIDE (default) or INSIDE */ | 49 | int alert_on; /* OUTSIDE (default) or INSIDE */ |
50 | char *text; /* original unparsed text input */ | 50 | char *text; /* original unparsed text input */ |
51 | } range; | 51 | } range; |
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index c3150f00..8040dec8 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c | |||
@@ -28,17 +28,7 @@ | |||
28 | #include "utils_base.c" | 28 | #include "utils_base.c" |
29 | 29 | ||
30 | int main(int argc, char **argv) { | 30 | int main(int argc, char **argv) { |
31 | char state_path[1024]; | 31 | plan_tests(155); |
32 | range *range; | ||
33 | double temp; | ||
34 | thresholds *thresholds = NULL; | ||
35 | int i, rc; | ||
36 | char *temp_string; | ||
37 | state_key *temp_state_key = NULL; | ||
38 | state_data *temp_state_data; | ||
39 | time_t current_time; | ||
40 | |||
41 | plan_tests(185); | ||
42 | 32 | ||
43 | ok(this_monitoring_plugin == NULL, "monitoring_plugin not initialised"); | 33 | ok(this_monitoring_plugin == NULL, "monitoring_plugin not initialised"); |
44 | 34 | ||
@@ -57,7 +47,7 @@ int main(int argc, char **argv) { | |||
57 | 47 | ||
58 | np_set_args(argc, argv); | 48 | np_set_args(argc, argv); |
59 | 49 | ||
60 | range = parse_range_string("6"); | 50 | range *range = parse_range_string("6"); |
61 | ok(range != NULL, "'6' is valid range"); | 51 | ok(range != NULL, "'6' is valid range"); |
62 | ok(range->start == 0, "Start correct"); | 52 | ok(range->start == 0, "Start correct"); |
63 | ok(range->start_infinity == false, "Not using negative infinity"); | 53 | ok(range->start_infinity == false, "Not using negative infinity"); |
@@ -97,7 +87,7 @@ int main(int argc, char **argv) { | |||
97 | free(range); | 87 | free(range); |
98 | 88 | ||
99 | range = parse_range_string("12345678901234567890:"); | 89 | range = parse_range_string("12345678901234567890:"); |
100 | temp = atof("12345678901234567890"); /* Can't just use this because number too large */ | 90 | double temp = atof("12345678901234567890"); /* Can't just use this because number too large */ |
101 | ok(range != NULL, "'12345678901234567890:' is valid range"); | 91 | ok(range != NULL, "'12345678901234567890:' is valid range"); |
102 | ok(range->start == temp, "Start correct"); | 92 | ok(range->start == temp, "Start correct"); |
103 | ok(range->start_infinity == false, "Not using negative infinity"); | 93 | ok(range->start_infinity == false, "Not using negative infinity"); |
@@ -158,32 +148,34 @@ int main(int argc, char **argv) { | |||
158 | range = parse_range_string("2:1"); | 148 | range = parse_range_string("2:1"); |
159 | ok(range == NULL, "'2:1' rejected"); | 149 | ok(range == NULL, "'2:1' rejected"); |
160 | 150 | ||
161 | rc = _set_thresholds(&thresholds, NULL, NULL); | 151 | thresholds *thresholds = NULL; |
162 | ok(rc == 0, "Thresholds (NULL, NULL) set"); | 152 | int returnCode; |
153 | returnCode = _set_thresholds(&thresholds, NULL, NULL); | ||
154 | ok(returnCode == 0, "Thresholds (NULL, NULL) set"); | ||
163 | ok(thresholds->warning == NULL, "Warning not set"); | 155 | ok(thresholds->warning == NULL, "Warning not set"); |
164 | ok(thresholds->critical == NULL, "Critical not set"); | 156 | ok(thresholds->critical == NULL, "Critical not set"); |
165 | 157 | ||
166 | rc = _set_thresholds(&thresholds, NULL, "80"); | 158 | returnCode = _set_thresholds(&thresholds, NULL, "80"); |
167 | ok(rc == 0, "Thresholds (NULL, '80') set"); | 159 | ok(returnCode == 0, "Thresholds (NULL, '80') set"); |
168 | ok(thresholds->warning == NULL, "Warning not set"); | 160 | ok(thresholds->warning == NULL, "Warning not set"); |
169 | ok(thresholds->critical->end == 80, "Critical set correctly"); | 161 | ok(thresholds->critical->end == 80, "Critical set correctly"); |
170 | 162 | ||
171 | rc = _set_thresholds(&thresholds, "5:33", NULL); | 163 | returnCode = _set_thresholds(&thresholds, "5:33", NULL); |
172 | ok(rc == 0, "Thresholds ('5:33', NULL) set"); | 164 | ok(returnCode == 0, "Thresholds ('5:33', NULL) set"); |
173 | ok(thresholds->warning->start == 5, "Warning start set"); | 165 | ok(thresholds->warning->start == 5, "Warning start set"); |
174 | ok(thresholds->warning->end == 33, "Warning end set"); | 166 | ok(thresholds->warning->end == 33, "Warning end set"); |
175 | ok(thresholds->critical == NULL, "Critical not set"); | 167 | ok(thresholds->critical == NULL, "Critical not set"); |
176 | 168 | ||
177 | rc = _set_thresholds(&thresholds, "30", "60"); | 169 | returnCode = _set_thresholds(&thresholds, "30", "60"); |
178 | ok(rc == 0, "Thresholds ('30', '60') set"); | 170 | ok(returnCode == 0, "Thresholds ('30', '60') set"); |
179 | ok(thresholds->warning->end == 30, "Warning set correctly"); | 171 | ok(thresholds->warning->end == 30, "Warning set correctly"); |
180 | ok(thresholds->critical->end == 60, "Critical set correctly"); | 172 | ok(thresholds->critical->end == 60, "Critical set correctly"); |
181 | ok(get_status(15.3, thresholds) == STATE_OK, "15.3 - ok"); | 173 | ok(get_status(15.3, thresholds) == STATE_OK, "15.3 - ok"); |
182 | ok(get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); | 174 | ok(get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); |
183 | ok(get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); | 175 | ok(get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); |
184 | 176 | ||
185 | rc = _set_thresholds(&thresholds, "-10:-2", "-30:20"); | 177 | returnCode = _set_thresholds(&thresholds, "-10:-2", "-30:20"); |
186 | ok(rc == 0, "Thresholds ('-30:20', '-10:-2') set"); | 178 | ok(returnCode == 0, "Thresholds ('-30:20', '-10:-2') set"); |
187 | ok(thresholds->warning->start == -10, "Warning start set correctly"); | 179 | ok(thresholds->warning->start == -10, "Warning start set correctly"); |
188 | ok(thresholds->warning->end == -2, "Warning end set correctly"); | 180 | ok(thresholds->warning->end == -2, "Warning end set correctly"); |
189 | ok(thresholds->critical->start == -30, "Critical start set correctly"); | 181 | ok(thresholds->critical->start == -30, "Critical start set correctly"); |
@@ -304,164 +296,28 @@ int main(int argc, char **argv) { | |||
304 | test = np_extract_ntpvar("", "foo"); | 296 | test = np_extract_ntpvar("", "foo"); |
305 | ok(!test, "Empty string return NULL"); | 297 | ok(!test, "Empty string return NULL"); |
306 | 298 | ||
307 | /* This is the result of running ./test_utils */ | ||
308 | temp_string = (char *)_np_state_generate_key(); | ||
309 | ok(!strcmp(temp_string, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got hash with exe and no parameters") || | ||
310 | diag("You are probably running in wrong directory. Must run as ./test_utils"); | ||
311 | |||
312 | this_monitoring_plugin->argc = 4; | ||
313 | this_monitoring_plugin->argv[0] = "./test_utils"; | ||
314 | this_monitoring_plugin->argv[1] = "here"; | ||
315 | this_monitoring_plugin->argv[2] = "--and"; | ||
316 | this_monitoring_plugin->argv[3] = "now"; | ||
317 | temp_string = (char *)_np_state_generate_key(); | ||
318 | ok(!strcmp(temp_string, "bd72da9f78ff1419fad921ea5e43ce56508aef6c"), "Got based on expected argv"); | ||
319 | |||
320 | unsetenv("MP_STATE_PATH"); | ||
321 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
322 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory"); | ||
323 | |||
324 | setenv("MP_STATE_PATH", "", 1); | ||
325 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
326 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string"); | ||
327 | |||
328 | setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1); | ||
329 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
330 | ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory"); | ||
331 | |||
332 | ok(temp_state_key == NULL, "temp_state_key initially empty"); | ||
333 | |||
334 | this_monitoring_plugin->argc = 1; | ||
335 | this_monitoring_plugin->argv[0] = "./test_utils"; | ||
336 | np_enable_state(NULL, 51); | ||
337 | temp_state_key = this_monitoring_plugin->state; | ||
338 | ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name"); | ||
339 | ok(!strcmp(temp_state_key->name, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got generated filename"); | ||
340 | |||
341 | np_enable_state("allowedchars_in_keyname", 77); | ||
342 | temp_state_key = this_monitoring_plugin->state; | ||
343 | sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", (unsigned long)geteuid()); | ||
344 | ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name"); | ||
345 | ok(!strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars"); | ||
346 | ok(!strcmp(temp_state_key->_filename, state_path), "Got internal filename"); | ||
347 | |||
348 | /* Don't do this test just yet. Will die */ | ||
349 | /* | ||
350 | np_enable_state("bad^chars$in@here", 77); | ||
351 | temp_state_key = this_monitoring_plugin->state; | ||
352 | ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); | ||
353 | */ | ||
354 | |||
355 | np_enable_state("funnykeyname", 54); | ||
356 | temp_state_key = this_monitoring_plugin->state; | ||
357 | sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid()); | ||
358 | ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name"); | ||
359 | ok(!strcmp(temp_state_key->name, "funnykeyname"), "Got key name"); | ||
360 | |||
361 | ok(!strcmp(temp_state_key->_filename, state_path), "Got internal filename"); | ||
362 | ok(temp_state_key->data_version == 54, "Version set"); | ||
363 | |||
364 | temp_state_data = np_state_read(); | ||
365 | ok(temp_state_data == NULL, "Got no state data as file does not exist"); | ||
366 | |||
367 | /* | ||
368 | temp_fp = fopen("var/statefile", "r"); | ||
369 | if (temp_fp==NULL) | ||
370 | printf("Error opening. errno=%d\n", errno); | ||
371 | printf("temp_fp=%s\n", temp_fp); | ||
372 | ok( _np_state_read_file(temp_fp) == true, "Can read state file" ); | ||
373 | fclose(temp_fp); | ||
374 | */ | ||
375 | |||
376 | temp_state_key->_filename = "var/statefile"; | ||
377 | temp_state_data = np_state_read(); | ||
378 | ok(this_monitoring_plugin->state->state_data != NULL, "Got state data now") || | ||
379 | diag("Are you running in right directory? Will get coredump next if not"); | ||
380 | ok(this_monitoring_plugin->state->state_data->time == 1234567890, "Got time"); | ||
381 | ok(!strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected"); | ||
382 | |||
383 | temp_state_key->data_version = 53; | ||
384 | temp_state_data = np_state_read(); | ||
385 | ok(temp_state_data == NULL, "Older data version gives NULL"); | ||
386 | temp_state_key->data_version = 54; | ||
387 | |||
388 | temp_state_key->_filename = "var/nonexistent"; | ||
389 | temp_state_data = np_state_read(); | ||
390 | ok(temp_state_data == NULL, "Missing file gives NULL"); | ||
391 | ok(this_monitoring_plugin->state->state_data == NULL, "No state information"); | ||
392 | |||
393 | temp_state_key->_filename = "var/oldformat"; | ||
394 | temp_state_data = np_state_read(); | ||
395 | ok(temp_state_data == NULL, "Old file format gives NULL"); | ||
396 | |||
397 | temp_state_key->_filename = "var/baddate"; | ||
398 | temp_state_data = np_state_read(); | ||
399 | ok(temp_state_data == NULL, "Bad date gives NULL"); | ||
400 | |||
401 | temp_state_key->_filename = "var/missingdataline"; | ||
402 | temp_state_data = np_state_read(); | ||
403 | ok(temp_state_data == NULL, "Missing data line gives NULL"); | ||
404 | |||
405 | unlink("var/generated"); | ||
406 | temp_state_key->_filename = "var/generated"; | ||
407 | current_time = 1234567890; | ||
408 | np_state_write_string(current_time, "String to read"); | ||
409 | ok(system("cmp var/generated var/statefile") == 0, "Generated file same as expected"); | ||
410 | |||
411 | unlink("var/generated_directory/statefile"); | ||
412 | unlink("var/generated_directory"); | ||
413 | temp_state_key->_filename = "var/generated_directory/statefile"; | ||
414 | current_time = 1234567890; | ||
415 | np_state_write_string(current_time, "String to read"); | ||
416 | ok(system("cmp var/generated_directory/statefile var/statefile") == 0, "Have created directory"); | ||
417 | |||
418 | /* This test to check cannot write to dir - can't automate yet */ | ||
419 | /* | ||
420 | unlink("var/generated_bad_dir"); | ||
421 | mkdir("var/generated_bad_dir", S_IRUSR); | ||
422 | np_state_write_string(current_time, "String to read"); | ||
423 | */ | ||
424 | |||
425 | temp_state_key->_filename = "var/generated"; | ||
426 | time(¤t_time); | ||
427 | np_state_write_string(0, "String to read"); | ||
428 | temp_state_data = np_state_read(); | ||
429 | /* Check time is set to current_time */ | ||
430 | ok(system("cmp var/generated var/statefile > /dev/null") != 0, "Generated file should be different this time"); | ||
431 | ok(this_monitoring_plugin->state->state_data->time - current_time <= 1, "Has time generated from current time"); | ||
432 | |||
433 | /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ | ||
434 | /* | ||
435 | temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write"; | ||
436 | np_state_write_string(0, "Bad file"); | ||
437 | */ | ||
438 | |||
439 | np_cleanup(); | ||
440 | |||
441 | ok(this_monitoring_plugin == NULL, "Free'd this_monitoring_plugin"); | ||
442 | |||
443 | ok(mp_suid() == false, "Test aren't suid"); | 299 | ok(mp_suid() == false, "Test aren't suid"); |
444 | 300 | ||
445 | /* base states with random case */ | 301 | /* base states with random case */ |
446 | char *states[] = {"Ok", "wArnINg", "cRiTIcaL", "UnKNoWN", NULL}; | 302 | char *states[] = {"Ok", "wArnINg", "cRiTIcaL", "UnKNoWN", NULL}; |
447 | 303 | ||
448 | for (i = 0; states[i] != NULL; i++) { | 304 | for (int i = 0; states[i] != NULL; i++) { |
449 | /* out of the random case states, create the lower and upper versions + numeric string one */ | 305 | /* out of the random case states, create the lower and upper versions + numeric string one |
306 | */ | ||
450 | char *statelower = strdup(states[i]); | 307 | char *statelower = strdup(states[i]); |
451 | char *stateupper = strdup(states[i]); | 308 | char *stateupper = strdup(states[i]); |
452 | char statenum[2]; | 309 | char statenum[2]; |
453 | char *temp_ptr; | 310 | for (char *temp_ptr = statelower; *temp_ptr; temp_ptr++) { |
454 | for (temp_ptr = statelower; *temp_ptr; temp_ptr++) { | 311 | *temp_ptr = (char)tolower(*temp_ptr); |
455 | *temp_ptr = tolower(*temp_ptr); | ||
456 | } | 312 | } |
457 | for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) { | 313 | for (char *temp_ptr = stateupper; *temp_ptr; temp_ptr++) { |
458 | *temp_ptr = toupper(*temp_ptr); | 314 | *temp_ptr = (char)toupper(*temp_ptr); |
459 | } | 315 | } |
460 | snprintf(statenum, 2, "%i", i); | 316 | snprintf(statenum, 2, "%i", i); |
461 | 317 | ||
462 | /* Base test names, we'll append the state string */ | 318 | /* Base test names, we'll append the state string */ |
463 | char testname[64] = "Translate state string: "; | 319 | char testname[64] = "Translate state string: "; |
464 | int tlen = strlen(testname); | 320 | size_t tlen = strlen(testname); |
465 | 321 | ||
466 | strcpy(testname + tlen, states[i]); | 322 | strcpy(testname + tlen, states[i]); |
467 | ok(i == mp_translate_state(states[i]), testname); | 323 | ok(i == mp_translate_state(states[i]), testname); |
diff --git a/lib/utils_base.c b/lib/utils_base.c index c49a473f..29b393d0 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
@@ -33,12 +33,12 @@ | |||
33 | #include <unistd.h> | 33 | #include <unistd.h> |
34 | #include <sys/types.h> | 34 | #include <sys/types.h> |
35 | 35 | ||
36 | #define np_free(ptr) \ | 36 | #define np_free(ptr) \ |
37 | { \ | 37 | { \ |
38 | if (ptr) { \ | 38 | if (ptr) { \ |
39 | free(ptr); \ | 39 | free(ptr); \ |
40 | ptr = NULL; \ | 40 | ptr = NULL; \ |
41 | } \ | 41 | } \ |
42 | } | 42 | } |
43 | 43 | ||
44 | monitoring_plugin *this_monitoring_plugin = NULL; | 44 | monitoring_plugin *this_monitoring_plugin = NULL; |
@@ -46,7 +46,7 @@ monitoring_plugin *this_monitoring_plugin = NULL; | |||
46 | int timeout_state = STATE_CRITICAL; | 46 | int timeout_state = STATE_CRITICAL; |
47 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; | 47 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; |
48 | 48 | ||
49 | bool _np_state_read_file(FILE *); | 49 | bool _np_state_read_file(FILE *state_file); |
50 | 50 | ||
51 | void np_init(char *plugin_name, int argc, char **argv) { | 51 | void np_init(char *plugin_name, int argc, char **argv) { |
52 | if (this_monitoring_plugin == NULL) { | 52 | if (this_monitoring_plugin == NULL) { |
@@ -74,14 +74,6 @@ void np_set_args(int argc, char **argv) { | |||
74 | 74 | ||
75 | void np_cleanup(void) { | 75 | void np_cleanup(void) { |
76 | if (this_monitoring_plugin != NULL) { | 76 | if (this_monitoring_plugin != NULL) { |
77 | if (this_monitoring_plugin->state != NULL) { | ||
78 | if (this_monitoring_plugin->state->state_data) { | ||
79 | np_free(this_monitoring_plugin->state->state_data->data); | ||
80 | np_free(this_monitoring_plugin->state->state_data); | ||
81 | } | ||
82 | np_free(this_monitoring_plugin->state->name); | ||
83 | np_free(this_monitoring_plugin->state); | ||
84 | } | ||
85 | np_free(this_monitoring_plugin->plugin_name); | 77 | np_free(this_monitoring_plugin->plugin_name); |
86 | np_free(this_monitoring_plugin); | 78 | np_free(this_monitoring_plugin); |
87 | } | 79 | } |
@@ -153,7 +145,8 @@ range *parse_range_string(char *str) { | |||
153 | set_range_end(temp_range, end); | 145 | set_range_end(temp_range, end); |
154 | } | 146 | } |
155 | 147 | ||
156 | if (temp_range->start_infinity == true || temp_range->end_infinity == true || temp_range->start <= temp_range->end) { | 148 | if (temp_range->start_infinity || temp_range->end_infinity || |
149 | temp_range->start <= temp_range->end) { | ||
157 | return temp_range; | 150 | return temp_range; |
158 | } | 151 | } |
159 | free(temp_range); | 152 | free(temp_range); |
@@ -205,12 +198,14 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { | |||
205 | printf("Threshold not set"); | 198 | printf("Threshold not set"); |
206 | } else { | 199 | } else { |
207 | if (my_threshold->warning) { | 200 | if (my_threshold->warning) { |
208 | printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end); | 201 | printf("Warning: start=%g end=%g; ", my_threshold->warning->start, |
202 | my_threshold->warning->end); | ||
209 | } else { | 203 | } else { |
210 | printf("Warning not set; "); | 204 | printf("Warning not set; "); |
211 | } | 205 | } |
212 | if (my_threshold->critical) { | 206 | if (my_threshold->critical) { |
213 | printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end); | 207 | printf("Critical: start=%g end=%g", my_threshold->critical->start, |
208 | my_threshold->critical->end); | ||
214 | } else { | 209 | } else { |
215 | printf("Critical not set"); | 210 | printf("Critical not set"); |
216 | } | 211 | } |
@@ -222,15 +217,16 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { | |||
222 | bool mp_check_range(const mp_perfdata_value value, const mp_range my_range) { | 217 | bool mp_check_range(const mp_perfdata_value value, const mp_range my_range) { |
223 | bool is_inside = false; | 218 | bool is_inside = false; |
224 | 219 | ||
225 | if (my_range.end_infinity == false && my_range.start_infinity == false) { | 220 | if (!my_range.end_infinity && !my_range.start_infinity) { |
226 | // range: .........|---inside---|........... | 221 | // range: .........|---inside---|........... |
227 | // value | 222 | // value |
228 | is_inside = ((cmp_perfdata_value(my_range.start, value) < 1) && (cmp_perfdata_value(value, my_range.end) <= 0)); | 223 | is_inside = ((cmp_perfdata_value(value, my_range.start) >= 0) && |
229 | } else if (my_range.start_infinity == false && my_range.end_infinity == true) { | 224 | (cmp_perfdata_value(value, my_range.end) <= 0)); |
225 | } else if (!my_range.start_infinity && my_range.end_infinity) { | ||
230 | // range: .........|---inside--------- | 226 | // range: .........|---inside--------- |
231 | // value | 227 | // value |
232 | is_inside = (cmp_perfdata_value(my_range.start, value) < 0); | 228 | is_inside = (cmp_perfdata_value(value, my_range.start) >= 0); |
233 | } else if (my_range.start_infinity == true && my_range.end_infinity == false) { | 229 | } else if (my_range.start_infinity && !my_range.end_infinity) { |
234 | // range: -inside--------|.................... | 230 | // range: -inside--------|.................... |
235 | // value | 231 | // value |
236 | is_inside = (cmp_perfdata_value(value, my_range.end) == -1); | 232 | is_inside = (cmp_perfdata_value(value, my_range.end) == -1); |
@@ -239,7 +235,8 @@ bool mp_check_range(const mp_perfdata_value value, const mp_range my_range) { | |||
239 | is_inside = true; | 235 | is_inside = true; |
240 | } | 236 | } |
241 | 237 | ||
242 | if ((is_inside && my_range.alert_on_inside_range == INSIDE) || (!is_inside && my_range.alert_on_inside_range == OUTSIDE)) { | 238 | if ((is_inside && my_range.alert_on_inside_range == INSIDE) || |
239 | (!is_inside && my_range.alert_on_inside_range == OUTSIDE)) { | ||
243 | return true; | 240 | return true; |
244 | } | 241 | } |
245 | 242 | ||
@@ -256,21 +253,21 @@ bool check_range(double value, range *my_range) { | |||
256 | yes = false; | 253 | yes = false; |
257 | } | 254 | } |
258 | 255 | ||
259 | if (my_range->end_infinity == false && my_range->start_infinity == false) { | 256 | if (!my_range->end_infinity && !my_range->start_infinity) { |
260 | if ((my_range->start <= value) && (value <= my_range->end)) { | 257 | if ((my_range->start <= value) && (value <= my_range->end)) { |
261 | return no; | 258 | return no; |
262 | } | 259 | } |
263 | return yes; | 260 | return yes; |
264 | } | 261 | } |
265 | 262 | ||
266 | if (my_range->start_infinity == false && my_range->end_infinity == true) { | 263 | if (!my_range->start_infinity && my_range->end_infinity) { |
267 | if (my_range->start <= value) { | 264 | if (my_range->start <= value) { |
268 | return no; | 265 | return no; |
269 | } | 266 | } |
270 | return yes; | 267 | return yes; |
271 | } | 268 | } |
272 | 269 | ||
273 | if (my_range->start_infinity == true && my_range->end_infinity == false) { | 270 | if (my_range->start_infinity && !my_range->end_infinity) { |
274 | if (value <= my_range->end) { | 271 | if (value <= my_range->end) { |
275 | return no; | 272 | return no; |
276 | } | 273 | } |
@@ -282,12 +279,12 @@ bool check_range(double value, range *my_range) { | |||
282 | /* Returns status */ | 279 | /* Returns status */ |
283 | int get_status(double value, thresholds *my_thresholds) { | 280 | int get_status(double value, thresholds *my_thresholds) { |
284 | if (my_thresholds->critical != NULL) { | 281 | if (my_thresholds->critical != NULL) { |
285 | if (check_range(value, my_thresholds->critical) == true) { | 282 | if (check_range(value, my_thresholds->critical)) { |
286 | return STATE_CRITICAL; | 283 | return STATE_CRITICAL; |
287 | } | 284 | } |
288 | } | 285 | } |
289 | if (my_thresholds->warning != NULL) { | 286 | if (my_thresholds->warning != NULL) { |
290 | if (check_range(value, my_thresholds->warning) == true) { | 287 | if (check_range(value, my_thresholds->warning)) { |
291 | return STATE_WARNING; | 288 | return STATE_WARNING; |
292 | } | 289 | } |
293 | } | 290 | } |
@@ -296,32 +293,31 @@ int get_status(double value, thresholds *my_thresholds) { | |||
296 | 293 | ||
297 | char *np_escaped_string(const char *string) { | 294 | char *np_escaped_string(const char *string) { |
298 | char *data; | 295 | char *data; |
299 | int i; | 296 | int write_index = 0; |
300 | int j = 0; | ||
301 | data = strdup(string); | 297 | data = strdup(string); |
302 | for (i = 0; data[i]; i++) { | 298 | for (int i = 0; data[i]; i++) { |
303 | if (data[i] == '\\') { | 299 | if (data[i] == '\\') { |
304 | switch (data[++i]) { | 300 | switch (data[++i]) { |
305 | case 'n': | 301 | case 'n': |
306 | data[j++] = '\n'; | 302 | data[write_index++] = '\n'; |
307 | break; | 303 | break; |
308 | case 'r': | 304 | case 'r': |
309 | data[j++] = '\r'; | 305 | data[write_index++] = '\r'; |
310 | break; | 306 | break; |
311 | case 't': | 307 | case 't': |
312 | data[j++] = '\t'; | 308 | data[write_index++] = '\t'; |
313 | break; | 309 | break; |
314 | case '\\': | 310 | case '\\': |
315 | data[j++] = '\\'; | 311 | data[write_index++] = '\\'; |
316 | break; | 312 | break; |
317 | default: | 313 | default: |
318 | data[j++] = data[i]; | 314 | data[write_index++] = data[i]; |
319 | } | 315 | } |
320 | } else { | 316 | } else { |
321 | data[j++] = data[i]; | 317 | data[write_index++] = data[i]; |
322 | } | 318 | } |
323 | } | 319 | } |
324 | data[j] = '\0'; | 320 | data[write_index] = '\0'; |
325 | return data; | 321 | return data; |
326 | } | 322 | } |
327 | 323 | ||
@@ -336,33 +332,35 @@ int np_check_if_root(void) { return (geteuid() == 0); } | |||
336 | char *np_extract_value(const char *varlist, const char *name, char sep) { | 332 | char *np_extract_value(const char *varlist, const char *name, char sep) { |
337 | char *tmp = NULL; | 333 | char *tmp = NULL; |
338 | char *value = NULL; | 334 | char *value = NULL; |
339 | int i; | ||
340 | 335 | ||
341 | while (1) { | 336 | while (true) { |
342 | /* Strip any leading space */ | 337 | /* Strip any leading space */ |
343 | for (; isspace(varlist[0]); varlist++) | 338 | for (; isspace(varlist[0]); varlist++) { |
344 | ; | 339 | ; |
340 | } | ||
345 | 341 | ||
346 | if (strncmp(name, varlist, strlen(name)) == 0) { | 342 | if (strncmp(name, varlist, strlen(name)) == 0) { |
347 | varlist += strlen(name); | 343 | varlist += strlen(name); |
348 | /* strip trailing spaces */ | 344 | /* strip trailing spaces */ |
349 | for (; isspace(varlist[0]); varlist++) | 345 | for (; isspace(varlist[0]); varlist++) { |
350 | ; | 346 | ; |
347 | } | ||
351 | 348 | ||
352 | if (varlist[0] == '=') { | 349 | if (varlist[0] == '=') { |
353 | /* We matched the key, go past the = sign */ | 350 | /* We matched the key, go past the = sign */ |
354 | varlist++; | 351 | varlist++; |
355 | /* strip leading spaces */ | 352 | /* strip leading spaces */ |
356 | for (; isspace(varlist[0]); varlist++) | 353 | for (; isspace(varlist[0]); varlist++) { |
357 | ; | 354 | ; |
355 | } | ||
358 | 356 | ||
359 | if ((tmp = index(varlist, sep))) { | 357 | if ((tmp = index(varlist, sep))) { |
360 | /* Value is delimited by a comma */ | 358 | /* Value is delimited by a comma */ |
361 | if (tmp - varlist == 0) { | 359 | if (tmp - varlist == 0) { |
362 | continue; | 360 | continue; |
363 | } | 361 | } |
364 | value = (char *)calloc(1, tmp - varlist + 1); | 362 | value = (char *)calloc(1, (unsigned long)(tmp - varlist + 1)); |
365 | strncpy(value, varlist, tmp - varlist); | 363 | strncpy(value, varlist, (unsigned long)(tmp - varlist)); |
366 | value[tmp - varlist] = '\0'; | 364 | value[tmp - varlist] = '\0'; |
367 | } else { | 365 | } else { |
368 | /* Value is delimited by a \0 */ | 366 | /* Value is delimited by a \0 */ |
@@ -387,7 +385,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
387 | 385 | ||
388 | /* Clean-up trailing spaces/newlines */ | 386 | /* Clean-up trailing spaces/newlines */ |
389 | if (value) { | 387 | if (value) { |
390 | for (i = strlen(value) - 1; isspace(value[i]); i--) { | 388 | for (unsigned long i = strlen(value) - 1; isspace(value[i]); i--) { |
391 | value[i] = '\0'; | 389 | value[i] = '\0'; |
392 | } | 390 | } |
393 | } | 391 | } |
@@ -429,349 +427,3 @@ int mp_translate_state(char *state_text) { | |||
429 | } | 427 | } |
430 | return ERROR; | 428 | return ERROR; |
431 | } | 429 | } |
432 | |||
433 | /* | ||
434 | * Returns a string to use as a keyname, based on an md5 hash of argv, thus | ||
435 | * hopefully a unique key per service/plugin invocation. Use the extra-opts | ||
436 | * parse of argv, so that uniqueness in parameters are reflected there. | ||
437 | */ | ||
438 | char *_np_state_generate_key(void) { | ||
439 | int i; | ||
440 | char **argv = this_monitoring_plugin->argv; | ||
441 | char keyname[41]; | ||
442 | char *p = NULL; | ||
443 | |||
444 | unsigned char result[256]; | ||
445 | |||
446 | #ifdef USE_OPENSSL | ||
447 | /* | ||
448 | * This code path is chosen if openssl is available (which should be the most common | ||
449 | * scenario). Alternatively, the gnulib implementation/ | ||
450 | * | ||
451 | */ | ||
452 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | ||
453 | |||
454 | EVP_DigestInit(ctx, EVP_sha256()); | ||
455 | |||
456 | for (i = 0; i < this_monitoring_plugin->argc; i++) { | ||
457 | EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); | ||
458 | } | ||
459 | |||
460 | EVP_DigestFinal(ctx, result, NULL); | ||
461 | #else | ||
462 | |||
463 | struct sha256_ctx ctx; | ||
464 | |||
465 | for (i = 0; i < this_monitoring_plugin->argc; i++) { | ||
466 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); | ||
467 | } | ||
468 | |||
469 | sha256_finish_ctx(&ctx, result); | ||
470 | #endif // FOUNDOPENSSL | ||
471 | |||
472 | for (i = 0; i < 20; ++i) { | ||
473 | sprintf(&keyname[2 * i], "%02x", result[i]); | ||
474 | } | ||
475 | |||
476 | keyname[40] = '\0'; | ||
477 | |||
478 | p = strdup(keyname); | ||
479 | if (p == NULL) { | ||
480 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
481 | } | ||
482 | return p; | ||
483 | } | ||
484 | |||
485 | void _cleanup_state_data(void) { | ||
486 | if (this_monitoring_plugin->state->state_data != NULL) { | ||
487 | np_free(this_monitoring_plugin->state->state_data->data); | ||
488 | np_free(this_monitoring_plugin->state->state_data); | ||
489 | } | ||
490 | } | ||
491 | |||
492 | /* | ||
493 | * Internal function. Returns either: | ||
494 | * envvar NAGIOS_PLUGIN_STATE_DIRECTORY | ||
495 | * statically compiled shared state directory | ||
496 | */ | ||
497 | char *_np_state_calculate_location_prefix(void) { | ||
498 | char *env_dir; | ||
499 | |||
500 | /* Do not allow passing MP_STATE_PATH in setuid plugins | ||
501 | * for security reasons */ | ||
502 | if (!mp_suid()) { | ||
503 | env_dir = getenv("MP_STATE_PATH"); | ||
504 | if (env_dir && env_dir[0] != '\0') { | ||
505 | return env_dir; | ||
506 | } | ||
507 | /* This is the former ENV, for backward-compatibility */ | ||
508 | env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); | ||
509 | if (env_dir && env_dir[0] != '\0') { | ||
510 | return env_dir; | ||
511 | } | ||
512 | } | ||
513 | |||
514 | return NP_STATE_DIR_PREFIX; | ||
515 | } | ||
516 | |||
517 | /* | ||
518 | * Initiatializer for state routines. | ||
519 | * Sets variables. Generates filename. Returns np_state_key. die with | ||
520 | * UNKNOWN if exception | ||
521 | */ | ||
522 | void np_enable_state(char *keyname, int expected_data_version) { | ||
523 | state_key *this_state = NULL; | ||
524 | char *temp_filename = NULL; | ||
525 | char *temp_keyname = NULL; | ||
526 | char *p = NULL; | ||
527 | int ret; | ||
528 | |||
529 | if (this_monitoring_plugin == NULL) { | ||
530 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | ||
531 | } | ||
532 | |||
533 | this_state = (state_key *)calloc(1, sizeof(state_key)); | ||
534 | if (this_state == NULL) { | ||
535 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
536 | } | ||
537 | |||
538 | if (keyname == NULL) { | ||
539 | temp_keyname = _np_state_generate_key(); | ||
540 | } else { | ||
541 | temp_keyname = strdup(keyname); | ||
542 | if (temp_keyname == NULL) { | ||
543 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
544 | } | ||
545 | } | ||
546 | /* Die if invalid characters used for keyname */ | ||
547 | p = temp_keyname; | ||
548 | while (*p != '\0') { | ||
549 | if (!(isalnum(*p) || *p == '_')) { | ||
550 | die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'")); | ||
551 | } | ||
552 | p++; | ||
553 | } | ||
554 | this_state->name = temp_keyname; | ||
555 | this_state->plugin_name = this_monitoring_plugin->plugin_name; | ||
556 | this_state->data_version = expected_data_version; | ||
557 | this_state->state_data = NULL; | ||
558 | |||
559 | /* Calculate filename */ | ||
560 | ret = asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), (unsigned long)geteuid(), | ||
561 | this_monitoring_plugin->plugin_name, this_state->name); | ||
562 | if (ret < 0) { | ||
563 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
564 | } | ||
565 | |||
566 | this_state->_filename = temp_filename; | ||
567 | |||
568 | this_monitoring_plugin->state = this_state; | ||
569 | } | ||
570 | |||
571 | /* | ||
572 | * Will return NULL if no data is available (first run). If key currently | ||
573 | * exists, read data. If state file format version is not expected, return | ||
574 | * as if no data. Get state data version number and compares to expected. | ||
575 | * If numerically lower, then return as no previous state. die with UNKNOWN | ||
576 | * if exceptional error. | ||
577 | */ | ||
578 | state_data *np_state_read(void) { | ||
579 | state_data *this_state_data = NULL; | ||
580 | FILE *statefile; | ||
581 | bool rc = false; | ||
582 | |||
583 | if (this_monitoring_plugin == NULL) { | ||
584 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | ||
585 | } | ||
586 | |||
587 | /* Open file. If this fails, no previous state found */ | ||
588 | statefile = fopen(this_monitoring_plugin->state->_filename, "r"); | ||
589 | if (statefile != NULL) { | ||
590 | |||
591 | this_state_data = (state_data *)calloc(1, sizeof(state_data)); | ||
592 | if (this_state_data == NULL) { | ||
593 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
594 | } | ||
595 | |||
596 | this_state_data->data = NULL; | ||
597 | this_monitoring_plugin->state->state_data = this_state_data; | ||
598 | |||
599 | rc = _np_state_read_file(statefile); | ||
600 | |||
601 | fclose(statefile); | ||
602 | } | ||
603 | |||
604 | if (!rc) { | ||
605 | _cleanup_state_data(); | ||
606 | } | ||
607 | |||
608 | return this_monitoring_plugin->state->state_data; | ||
609 | } | ||
610 | |||
611 | /* | ||
612 | * Read the state file | ||
613 | */ | ||
614 | bool _np_state_read_file(FILE *f) { | ||
615 | bool status = false; | ||
616 | size_t pos; | ||
617 | char *line; | ||
618 | int i; | ||
619 | int failure = 0; | ||
620 | time_t current_time, data_time; | ||
621 | enum { | ||
622 | STATE_FILE_VERSION, | ||
623 | STATE_DATA_VERSION, | ||
624 | STATE_DATA_TIME, | ||
625 | STATE_DATA_TEXT, | ||
626 | STATE_DATA_END | ||
627 | } expected = STATE_FILE_VERSION; | ||
628 | |||
629 | time(¤t_time); | ||
630 | |||
631 | /* Note: This introduces a limit of 1024 bytes in the string data */ | ||
632 | line = (char *)calloc(1, 1024); | ||
633 | if (line == NULL) { | ||
634 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
635 | } | ||
636 | |||
637 | while (!failure && (fgets(line, 1024, f)) != NULL) { | ||
638 | pos = strlen(line); | ||
639 | if (line[pos - 1] == '\n') { | ||
640 | line[pos - 1] = '\0'; | ||
641 | } | ||
642 | |||
643 | if (line[0] == '#') { | ||
644 | continue; | ||
645 | } | ||
646 | |||
647 | switch (expected) { | ||
648 | case STATE_FILE_VERSION: | ||
649 | i = atoi(line); | ||
650 | if (i != NP_STATE_FORMAT_VERSION) { | ||
651 | failure++; | ||
652 | } else { | ||
653 | expected = STATE_DATA_VERSION; | ||
654 | } | ||
655 | break; | ||
656 | case STATE_DATA_VERSION: | ||
657 | i = atoi(line); | ||
658 | if (i != this_monitoring_plugin->state->data_version) { | ||
659 | failure++; | ||
660 | } else { | ||
661 | expected = STATE_DATA_TIME; | ||
662 | } | ||
663 | break; | ||
664 | case STATE_DATA_TIME: | ||
665 | /* If time > now, error */ | ||
666 | data_time = strtoul(line, NULL, 10); | ||
667 | if (data_time > current_time) { | ||
668 | failure++; | ||
669 | } else { | ||
670 | this_monitoring_plugin->state->state_data->time = data_time; | ||
671 | expected = STATE_DATA_TEXT; | ||
672 | } | ||
673 | break; | ||
674 | case STATE_DATA_TEXT: | ||
675 | this_monitoring_plugin->state->state_data->data = strdup(line); | ||
676 | if (this_monitoring_plugin->state->state_data->data == NULL) { | ||
677 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
678 | } | ||
679 | expected = STATE_DATA_END; | ||
680 | status = true; | ||
681 | break; | ||
682 | case STATE_DATA_END:; | ||
683 | } | ||
684 | } | ||
685 | |||
686 | np_free(line); | ||
687 | return status; | ||
688 | } | ||
689 | |||
690 | /* | ||
691 | * If time=NULL, use current time. Create state file, with state format | ||
692 | * version, default text. Writes version, time, and data. Avoid locking | ||
693 | * problems - use mv to write and then swap. Possible loss of state data if | ||
694 | * two things writing to same key at same time. | ||
695 | * Will die with UNKNOWN if errors | ||
696 | */ | ||
697 | void np_state_write_string(time_t data_time, char *data_string) { | ||
698 | FILE *fp; | ||
699 | char *temp_file = NULL; | ||
700 | int fd = 0, result = 0; | ||
701 | time_t current_time; | ||
702 | char *directories = NULL; | ||
703 | char *p = NULL; | ||
704 | |||
705 | if (data_time == 0) { | ||
706 | time(¤t_time); | ||
707 | } else { | ||
708 | current_time = data_time; | ||
709 | } | ||
710 | |||
711 | /* If file doesn't currently exist, create directories */ | ||
712 | if (access(this_monitoring_plugin->state->_filename, F_OK) != 0) { | ||
713 | result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); | ||
714 | if (result < 0) { | ||
715 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
716 | } | ||
717 | |||
718 | for (p = directories + 1; *p; p++) { | ||
719 | if (*p == '/') { | ||
720 | *p = '\0'; | ||
721 | if ((access(directories, F_OK) != 0) && (mkdir(directories, S_IRWXU) != 0)) { | ||
722 | /* Can't free this! Otherwise error message is wrong! */ | ||
723 | /* np_free(directories); */ | ||
724 | die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); | ||
725 | } | ||
726 | *p = '/'; | ||
727 | } | ||
728 | } | ||
729 | np_free(directories); | ||
730 | } | ||
731 | |||
732 | result = asprintf(&temp_file, "%s.XXXXXX", this_monitoring_plugin->state->_filename); | ||
733 | if (result < 0) { | ||
734 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
735 | } | ||
736 | |||
737 | if ((fd = mkstemp(temp_file)) == -1) { | ||
738 | np_free(temp_file); | ||
739 | die(STATE_UNKNOWN, _("Cannot create temporary filename")); | ||
740 | } | ||
741 | |||
742 | fp = (FILE *)fdopen(fd, "w"); | ||
743 | if (fp == NULL) { | ||
744 | close(fd); | ||
745 | unlink(temp_file); | ||
746 | np_free(temp_file); | ||
747 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); | ||
748 | } | ||
749 | |||
750 | fprintf(fp, "# NP State file\n"); | ||
751 | fprintf(fp, "%d\n", NP_STATE_FORMAT_VERSION); | ||
752 | fprintf(fp, "%d\n", this_monitoring_plugin->state->data_version); | ||
753 | fprintf(fp, "%lu\n", current_time); | ||
754 | fprintf(fp, "%s\n", data_string); | ||
755 | |||
756 | fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP); | ||
757 | |||
758 | fflush(fp); | ||
759 | |||
760 | result = fclose(fp); | ||
761 | |||
762 | fsync(fd); | ||
763 | |||
764 | if (result != 0) { | ||
765 | unlink(temp_file); | ||
766 | np_free(temp_file); | ||
767 | die(STATE_UNKNOWN, _("Error writing temp file")); | ||
768 | } | ||
769 | |||
770 | if (rename(temp_file, this_monitoring_plugin->state->_filename) != 0) { | ||
771 | unlink(temp_file); | ||
772 | np_free(temp_file); | ||
773 | die(STATE_UNKNOWN, _("Cannot rename state temp file")); | ||
774 | } | ||
775 | |||
776 | np_free(temp_file); | ||
777 | } | ||
diff --git a/lib/utils_base.h b/lib/utils_base.h index 123066f8..f1c99a54 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h | |||
@@ -8,7 +8,6 @@ | |||
8 | #include "./perfdata.h" | 8 | #include "./perfdata.h" |
9 | #include "./thresholds.h" | 9 | #include "./thresholds.h" |
10 | 10 | ||
11 | |||
12 | #ifndef USE_OPENSSL | 11 | #ifndef USE_OPENSSL |
13 | # include "sha256.h" | 12 | # include "sha256.h" |
14 | #endif | 13 | #endif |
@@ -26,25 +25,8 @@ | |||
26 | #define OUTSIDE 0 | 25 | #define OUTSIDE 0 |
27 | #define INSIDE 1 | 26 | #define INSIDE 1 |
28 | 27 | ||
29 | #define NP_STATE_FORMAT_VERSION 1 | ||
30 | |||
31 | typedef struct state_data_struct { | ||
32 | time_t time; | ||
33 | void *data; | ||
34 | int length; /* Of binary data */ | ||
35 | } state_data; | ||
36 | |||
37 | typedef struct state_key_struct { | ||
38 | char *name; | ||
39 | char *plugin_name; | ||
40 | int data_version; | ||
41 | char *_filename; | ||
42 | state_data *state_data; | ||
43 | } state_key; | ||
44 | |||
45 | typedef struct np_struct { | 28 | typedef struct np_struct { |
46 | char *plugin_name; | 29 | char *plugin_name; |
47 | state_key *state; | ||
48 | int argc; | 30 | int argc; |
49 | char **argv; | 31 | char **argv; |
50 | } monitoring_plugin; | 32 | } monitoring_plugin; |
@@ -100,10 +82,6 @@ char *np_extract_value(const char *, const char *, char); | |||
100 | */ | 82 | */ |
101 | int mp_translate_state(char *); | 83 | int mp_translate_state(char *); |
102 | 84 | ||
103 | void np_enable_state(char *, int); | ||
104 | state_data *np_state_read(void); | ||
105 | void np_state_write_string(time_t, char *); | ||
106 | |||
107 | void np_init(char *, int argc, char **argv); | 85 | void np_init(char *, int argc, char **argv); |
108 | void np_set_args(int argc, char **argv); | 86 | void np_set_args(int argc, char **argv); |
109 | void np_cleanup(void); | 87 | void np_cleanup(void); |