diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-09 02:07:32 +0200 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-09 02:07:32 +0200 |
| commit | fbd60d6a9d9ba5b45879280dcfda92379dec65da (patch) | |
| tree | a7c0e428be10bb27b6344c6531b804451c4d40cc /lib/utils_base.c | |
| parent | b54869391faab7ef91586c81de21f18a61bac5aa (diff) | |
| parent | 615fec347cd575c0ee4343aa17ee99962f375f64 (diff) | |
| download | monitoring-plugins-fbd60d6a9d9ba5b45879280dcfda92379dec65da.tar.gz | |
Merge branch 'master' into refactor/check_curl
Diffstat (limited to 'lib/utils_base.c')
| -rw-r--r-- | lib/utils_base.c | 428 |
1 files changed, 40 insertions, 388 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c index 60103614..e95eeaf0 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
| @@ -34,12 +34,12 @@ | |||
| 34 | #include <unistd.h> | 34 | #include <unistd.h> |
| 35 | #include <sys/types.h> | 35 | #include <sys/types.h> |
| 36 | 36 | ||
| 37 | #define np_free(ptr) \ | 37 | #define np_free(ptr) \ |
| 38 | { \ | 38 | { \ |
| 39 | if (ptr) { \ | 39 | if (ptr) { \ |
| 40 | free(ptr); \ | 40 | free(ptr); \ |
| 41 | ptr = NULL; \ | 41 | ptr = NULL; \ |
| 42 | } \ | 42 | } \ |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | monitoring_plugin *this_monitoring_plugin = NULL; | 45 | monitoring_plugin *this_monitoring_plugin = NULL; |
| @@ -47,7 +47,7 @@ monitoring_plugin *this_monitoring_plugin = NULL; | |||
| 47 | int timeout_state = STATE_CRITICAL; | 47 | int timeout_state = STATE_CRITICAL; |
| 48 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; | 48 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; |
| 49 | 49 | ||
| 50 | bool _np_state_read_file(FILE *); | 50 | bool _np_state_read_file(FILE *state_file); |
| 51 | 51 | ||
| 52 | void np_init(char *plugin_name, int argc, char **argv) { | 52 | void np_init(char *plugin_name, int argc, char **argv) { |
| 53 | if (this_monitoring_plugin == NULL) { | 53 | if (this_monitoring_plugin == NULL) { |
| @@ -75,14 +75,6 @@ void np_set_args(int argc, char **argv) { | |||
| 75 | 75 | ||
| 76 | void np_cleanup(void) { | 76 | void np_cleanup(void) { |
| 77 | if (this_monitoring_plugin != NULL) { | 77 | if (this_monitoring_plugin != NULL) { |
| 78 | if (this_monitoring_plugin->state != NULL) { | ||
| 79 | if (this_monitoring_plugin->state->state_data) { | ||
| 80 | np_free(this_monitoring_plugin->state->state_data->data); | ||
| 81 | np_free(this_monitoring_plugin->state->state_data); | ||
| 82 | } | ||
| 83 | np_free(this_monitoring_plugin->state->name); | ||
| 84 | np_free(this_monitoring_plugin->state); | ||
| 85 | } | ||
| 86 | np_free(this_monitoring_plugin->plugin_name); | 78 | np_free(this_monitoring_plugin->plugin_name); |
| 87 | np_free(this_monitoring_plugin); | 79 | np_free(this_monitoring_plugin); |
| 88 | } | 80 | } |
| @@ -154,7 +146,8 @@ range *parse_range_string(char *str) { | |||
| 154 | set_range_end(temp_range, end); | 146 | set_range_end(temp_range, end); |
| 155 | } | 147 | } |
| 156 | 148 | ||
| 157 | if (temp_range->start_infinity == true || temp_range->end_infinity == true || temp_range->start <= temp_range->end) { | 149 | if (temp_range->start_infinity || temp_range->end_infinity || |
| 150 | temp_range->start <= temp_range->end) { | ||
| 158 | return temp_range; | 151 | return temp_range; |
| 159 | } | 152 | } |
| 160 | free(temp_range); | 153 | free(temp_range); |
| @@ -206,12 +199,14 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { | |||
| 206 | printf("Threshold not set"); | 199 | printf("Threshold not set"); |
| 207 | } else { | 200 | } else { |
| 208 | if (my_threshold->warning) { | 201 | if (my_threshold->warning) { |
| 209 | printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end); | 202 | printf("Warning: start=%g end=%g; ", my_threshold->warning->start, |
| 203 | my_threshold->warning->end); | ||
| 210 | } else { | 204 | } else { |
| 211 | printf("Warning not set; "); | 205 | printf("Warning not set; "); |
| 212 | } | 206 | } |
| 213 | if (my_threshold->critical) { | 207 | if (my_threshold->critical) { |
| 214 | printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end); | 208 | printf("Critical: start=%g end=%g", my_threshold->critical->start, |
| 209 | my_threshold->critical->end); | ||
| 215 | } else { | 210 | } else { |
| 216 | printf("Critical not set"); | 211 | printf("Critical not set"); |
| 217 | } | 212 | } |
| @@ -223,15 +218,16 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { | |||
| 223 | bool mp_check_range(const mp_perfdata_value value, const mp_range my_range) { | 218 | bool mp_check_range(const mp_perfdata_value value, const mp_range my_range) { |
| 224 | bool is_inside = false; | 219 | bool is_inside = false; |
| 225 | 220 | ||
| 226 | if (my_range.end_infinity == false && my_range.start_infinity == false) { | 221 | if (!my_range.end_infinity && !my_range.start_infinity) { |
| 227 | // range: .........|---inside---|........... | 222 | // range: .........|---inside---|........... |
| 228 | // value | 223 | // value |
| 229 | is_inside = ((cmp_perfdata_value(my_range.start, value) < 1) && (cmp_perfdata_value(value, my_range.end) <= 0)); | 224 | is_inside = ((cmp_perfdata_value(value, my_range.start) >= 0) && |
| 230 | } else if (my_range.start_infinity == false && my_range.end_infinity == true) { | 225 | (cmp_perfdata_value(value, my_range.end) <= 0)); |
| 226 | } else if (!my_range.start_infinity && my_range.end_infinity) { | ||
| 231 | // range: .........|---inside--------- | 227 | // range: .........|---inside--------- |
| 232 | // value | 228 | // value |
| 233 | is_inside = (cmp_perfdata_value(my_range.start, value) < 0); | 229 | is_inside = (cmp_perfdata_value(value, my_range.start) >= 0); |
| 234 | } else if (my_range.start_infinity == true && my_range.end_infinity == false) { | 230 | } else if (my_range.start_infinity && !my_range.end_infinity) { |
| 235 | // range: -inside--------|.................... | 231 | // range: -inside--------|.................... |
| 236 | // value | 232 | // value |
| 237 | is_inside = (cmp_perfdata_value(value, my_range.end) == -1); | 233 | is_inside = (cmp_perfdata_value(value, my_range.end) == -1); |
| @@ -240,7 +236,8 @@ bool mp_check_range(const mp_perfdata_value value, const mp_range my_range) { | |||
| 240 | is_inside = true; | 236 | is_inside = true; |
| 241 | } | 237 | } |
| 242 | 238 | ||
| 243 | if ((is_inside && my_range.alert_on_inside_range == INSIDE) || (!is_inside && my_range.alert_on_inside_range == OUTSIDE)) { | 239 | if ((is_inside && my_range.alert_on_inside_range == INSIDE) || |
| 240 | (!is_inside && my_range.alert_on_inside_range == OUTSIDE)) { | ||
| 244 | return true; | 241 | return true; |
| 245 | } | 242 | } |
| 246 | 243 | ||
| @@ -297,32 +294,31 @@ mp_state_enum get_status(double value, thresholds *my_thresholds) { | |||
| 297 | 294 | ||
| 298 | char *np_escaped_string(const char *string) { | 295 | char *np_escaped_string(const char *string) { |
| 299 | char *data; | 296 | char *data; |
| 300 | int i; | 297 | int write_index = 0; |
| 301 | int j = 0; | ||
| 302 | data = strdup(string); | 298 | data = strdup(string); |
| 303 | for (i = 0; data[i]; i++) { | 299 | for (int i = 0; data[i]; i++) { |
| 304 | if (data[i] == '\\') { | 300 | if (data[i] == '\\') { |
| 305 | switch (data[++i]) { | 301 | switch (data[++i]) { |
| 306 | case 'n': | 302 | case 'n': |
| 307 | data[j++] = '\n'; | 303 | data[write_index++] = '\n'; |
| 308 | break; | 304 | break; |
| 309 | case 'r': | 305 | case 'r': |
| 310 | data[j++] = '\r'; | 306 | data[write_index++] = '\r'; |
| 311 | break; | 307 | break; |
| 312 | case 't': | 308 | case 't': |
| 313 | data[j++] = '\t'; | 309 | data[write_index++] = '\t'; |
| 314 | break; | 310 | break; |
| 315 | case '\\': | 311 | case '\\': |
| 316 | data[j++] = '\\'; | 312 | data[write_index++] = '\\'; |
| 317 | break; | 313 | break; |
| 318 | default: | 314 | default: |
| 319 | data[j++] = data[i]; | 315 | data[write_index++] = data[i]; |
| 320 | } | 316 | } |
| 321 | } else { | 317 | } else { |
| 322 | data[j++] = data[i]; | 318 | data[write_index++] = data[i]; |
| 323 | } | 319 | } |
| 324 | } | 320 | } |
| 325 | data[j] = '\0'; | 321 | data[write_index] = '\0'; |
| 326 | return data; | 322 | return data; |
| 327 | } | 323 | } |
| 328 | 324 | ||
| @@ -337,33 +333,35 @@ int np_check_if_root(void) { return (geteuid() == 0); } | |||
| 337 | char *np_extract_value(const char *varlist, const char *name, char sep) { | 333 | char *np_extract_value(const char *varlist, const char *name, char sep) { |
| 338 | char *tmp = NULL; | 334 | char *tmp = NULL; |
| 339 | char *value = NULL; | 335 | char *value = NULL; |
| 340 | int i; | ||
| 341 | 336 | ||
| 342 | while (1) { | 337 | while (true) { |
| 343 | /* Strip any leading space */ | 338 | /* Strip any leading space */ |
| 344 | for (; isspace(varlist[0]); varlist++) | 339 | for (; isspace(varlist[0]); varlist++) { |
| 345 | ; | 340 | ; |
| 341 | } | ||
| 346 | 342 | ||
| 347 | if (strncmp(name, varlist, strlen(name)) == 0) { | 343 | if (strncmp(name, varlist, strlen(name)) == 0) { |
| 348 | varlist += strlen(name); | 344 | varlist += strlen(name); |
| 349 | /* strip trailing spaces */ | 345 | /* strip trailing spaces */ |
| 350 | for (; isspace(varlist[0]); varlist++) | 346 | for (; isspace(varlist[0]); varlist++) { |
| 351 | ; | 347 | ; |
| 348 | } | ||
| 352 | 349 | ||
| 353 | if (varlist[0] == '=') { | 350 | if (varlist[0] == '=') { |
| 354 | /* We matched the key, go past the = sign */ | 351 | /* We matched the key, go past the = sign */ |
| 355 | varlist++; | 352 | varlist++; |
| 356 | /* strip leading spaces */ | 353 | /* strip leading spaces */ |
| 357 | for (; isspace(varlist[0]); varlist++) | 354 | for (; isspace(varlist[0]); varlist++) { |
| 358 | ; | 355 | ; |
| 356 | } | ||
| 359 | 357 | ||
| 360 | if ((tmp = index(varlist, sep))) { | 358 | if ((tmp = index(varlist, sep))) { |
| 361 | /* Value is delimited by a comma */ | 359 | /* Value is delimited by a comma */ |
| 362 | if (tmp - varlist == 0) { | 360 | if (tmp - varlist == 0) { |
| 363 | continue; | 361 | continue; |
| 364 | } | 362 | } |
| 365 | value = (char *)calloc(1, tmp - varlist + 1); | 363 | value = (char *)calloc(1, (unsigned long)(tmp - varlist + 1)); |
| 366 | strncpy(value, varlist, tmp - varlist); | 364 | strncpy(value, varlist, (unsigned long)(tmp - varlist)); |
| 367 | value[tmp - varlist] = '\0'; | 365 | value[tmp - varlist] = '\0'; |
| 368 | } else { | 366 | } else { |
| 369 | /* Value is delimited by a \0 */ | 367 | /* Value is delimited by a \0 */ |
| @@ -388,7 +386,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
| 388 | 386 | ||
| 389 | /* Clean-up trailing spaces/newlines */ | 387 | /* Clean-up trailing spaces/newlines */ |
| 390 | if (value) { | 388 | if (value) { |
| 391 | for (i = strlen(value) - 1; isspace(value[i]); i--) { | 389 | for (unsigned long i = strlen(value) - 1; isspace(value[i]); i--) { |
| 392 | value[i] = '\0'; | 390 | value[i] = '\0'; |
| 393 | } | 391 | } |
| 394 | } | 392 | } |
| @@ -430,349 +428,3 @@ int mp_translate_state(char *state_text) { | |||
| 430 | } | 428 | } |
| 431 | return ERROR; | 429 | return ERROR; |
| 432 | } | 430 | } |
| 433 | |||
| 434 | /* | ||
| 435 | * Returns a string to use as a keyname, based on an md5 hash of argv, thus | ||
| 436 | * hopefully a unique key per service/plugin invocation. Use the extra-opts | ||
| 437 | * parse of argv, so that uniqueness in parameters are reflected there. | ||
| 438 | */ | ||
| 439 | char *_np_state_generate_key(void) { | ||
| 440 | int i; | ||
| 441 | char **argv = this_monitoring_plugin->argv; | ||
| 442 | char keyname[41]; | ||
| 443 | char *p = NULL; | ||
| 444 | |||
| 445 | unsigned char result[256]; | ||
| 446 | |||
| 447 | #ifdef USE_OPENSSL | ||
| 448 | /* | ||
| 449 | * This code path is chosen if openssl is available (which should be the most common | ||
| 450 | * scenario). Alternatively, the gnulib implementation/ | ||
| 451 | * | ||
| 452 | */ | ||
| 453 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | ||
| 454 | |||
| 455 | EVP_DigestInit(ctx, EVP_sha256()); | ||
| 456 | |||
| 457 | for (i = 0; i < this_monitoring_plugin->argc; i++) { | ||
| 458 | EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); | ||
| 459 | } | ||
| 460 | |||
| 461 | EVP_DigestFinal(ctx, result, NULL); | ||
| 462 | #else | ||
| 463 | |||
| 464 | struct sha256_ctx ctx; | ||
| 465 | |||
| 466 | for (i = 0; i < this_monitoring_plugin->argc; i++) { | ||
| 467 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); | ||
| 468 | } | ||
| 469 | |||
| 470 | sha256_finish_ctx(&ctx, result); | ||
| 471 | #endif // FOUNDOPENSSL | ||
| 472 | |||
| 473 | for (i = 0; i < 20; ++i) { | ||
| 474 | sprintf(&keyname[2 * i], "%02x", result[i]); | ||
| 475 | } | ||
| 476 | |||
| 477 | keyname[40] = '\0'; | ||
| 478 | |||
| 479 | p = strdup(keyname); | ||
| 480 | if (p == NULL) { | ||
| 481 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
| 482 | } | ||
| 483 | return p; | ||
| 484 | } | ||
| 485 | |||
| 486 | void _cleanup_state_data(void) { | ||
| 487 | if (this_monitoring_plugin->state->state_data != NULL) { | ||
| 488 | np_free(this_monitoring_plugin->state->state_data->data); | ||
| 489 | np_free(this_monitoring_plugin->state->state_data); | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | /* | ||
| 494 | * Internal function. Returns either: | ||
| 495 | * envvar NAGIOS_PLUGIN_STATE_DIRECTORY | ||
| 496 | * statically compiled shared state directory | ||
| 497 | */ | ||
| 498 | char *_np_state_calculate_location_prefix(void) { | ||
| 499 | char *env_dir; | ||
| 500 | |||
| 501 | /* Do not allow passing MP_STATE_PATH in setuid plugins | ||
| 502 | * for security reasons */ | ||
| 503 | if (!mp_suid()) { | ||
| 504 | env_dir = getenv("MP_STATE_PATH"); | ||
| 505 | if (env_dir && env_dir[0] != '\0') { | ||
| 506 | return env_dir; | ||
| 507 | } | ||
| 508 | /* This is the former ENV, for backward-compatibility */ | ||
| 509 | env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); | ||
| 510 | if (env_dir && env_dir[0] != '\0') { | ||
| 511 | return env_dir; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | |||
| 515 | return NP_STATE_DIR_PREFIX; | ||
| 516 | } | ||
| 517 | |||
| 518 | /* | ||
| 519 | * Initiatializer for state routines. | ||
| 520 | * Sets variables. Generates filename. Returns np_state_key. die with | ||
| 521 | * UNKNOWN if exception | ||
| 522 | */ | ||
| 523 | void np_enable_state(char *keyname, int expected_data_version) { | ||
| 524 | state_key *this_state = NULL; | ||
| 525 | char *temp_filename = NULL; | ||
| 526 | char *temp_keyname = NULL; | ||
| 527 | char *p = NULL; | ||
| 528 | int ret; | ||
| 529 | |||
| 530 | if (this_monitoring_plugin == NULL) { | ||
| 531 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | ||
| 532 | } | ||
| 533 | |||
| 534 | this_state = (state_key *)calloc(1, sizeof(state_key)); | ||
| 535 | if (this_state == NULL) { | ||
| 536 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
| 537 | } | ||
| 538 | |||
| 539 | if (keyname == NULL) { | ||
| 540 | temp_keyname = _np_state_generate_key(); | ||
| 541 | } else { | ||
| 542 | temp_keyname = strdup(keyname); | ||
| 543 | if (temp_keyname == NULL) { | ||
| 544 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
| 545 | } | ||
| 546 | } | ||
| 547 | /* Die if invalid characters used for keyname */ | ||
| 548 | p = temp_keyname; | ||
| 549 | while (*p != '\0') { | ||
| 550 | if (!(isalnum(*p) || *p == '_')) { | ||
| 551 | die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'")); | ||
| 552 | } | ||
| 553 | p++; | ||
| 554 | } | ||
| 555 | this_state->name = temp_keyname; | ||
| 556 | this_state->plugin_name = this_monitoring_plugin->plugin_name; | ||
| 557 | this_state->data_version = expected_data_version; | ||
| 558 | this_state->state_data = NULL; | ||
| 559 | |||
| 560 | /* Calculate filename */ | ||
| 561 | ret = asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), (unsigned long)geteuid(), | ||
| 562 | this_monitoring_plugin->plugin_name, this_state->name); | ||
| 563 | if (ret < 0) { | ||
| 564 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
| 565 | } | ||
| 566 | |||
| 567 | this_state->_filename = temp_filename; | ||
| 568 | |||
| 569 | this_monitoring_plugin->state = this_state; | ||
| 570 | } | ||
| 571 | |||
| 572 | /* | ||
| 573 | * Will return NULL if no data is available (first run). If key currently | ||
| 574 | * exists, read data. If state file format version is not expected, return | ||
| 575 | * as if no data. Get state data version number and compares to expected. | ||
| 576 | * If numerically lower, then return as no previous state. die with UNKNOWN | ||
| 577 | * if exceptional error. | ||
| 578 | */ | ||
| 579 | state_data *np_state_read(void) { | ||
| 580 | state_data *this_state_data = NULL; | ||
| 581 | FILE *statefile; | ||
| 582 | bool rc = false; | ||
| 583 | |||
| 584 | if (this_monitoring_plugin == NULL) { | ||
| 585 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | ||
| 586 | } | ||
| 587 | |||
| 588 | /* Open file. If this fails, no previous state found */ | ||
| 589 | statefile = fopen(this_monitoring_plugin->state->_filename, "r"); | ||
| 590 | if (statefile != NULL) { | ||
| 591 | |||
| 592 | this_state_data = (state_data *)calloc(1, sizeof(state_data)); | ||
| 593 | if (this_state_data == NULL) { | ||
| 594 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
| 595 | } | ||
| 596 | |||
| 597 | this_state_data->data = NULL; | ||
| 598 | this_monitoring_plugin->state->state_data = this_state_data; | ||
| 599 | |||
| 600 | rc = _np_state_read_file(statefile); | ||
| 601 | |||
| 602 | fclose(statefile); | ||
| 603 | } | ||
| 604 | |||
| 605 | if (!rc) { | ||
| 606 | _cleanup_state_data(); | ||
| 607 | } | ||
| 608 | |||
| 609 | return this_monitoring_plugin->state->state_data; | ||
| 610 | } | ||
| 611 | |||
| 612 | /* | ||
| 613 | * Read the state file | ||
| 614 | */ | ||
| 615 | bool _np_state_read_file(FILE *f) { | ||
| 616 | bool status = false; | ||
| 617 | size_t pos; | ||
| 618 | char *line; | ||
| 619 | int i; | ||
| 620 | int failure = 0; | ||
| 621 | time_t current_time, data_time; | ||
| 622 | enum { | ||
| 623 | STATE_FILE_VERSION, | ||
| 624 | STATE_DATA_VERSION, | ||
| 625 | STATE_DATA_TIME, | ||
| 626 | STATE_DATA_TEXT, | ||
| 627 | STATE_DATA_END | ||
| 628 | } expected = STATE_FILE_VERSION; | ||
| 629 | |||
| 630 | time(¤t_time); | ||
| 631 | |||
| 632 | /* Note: This introduces a limit of 1024 bytes in the string data */ | ||
| 633 | line = (char *)calloc(1, 1024); | ||
| 634 | if (line == NULL) { | ||
| 635 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
| 636 | } | ||
| 637 | |||
| 638 | while (!failure && (fgets(line, 1024, f)) != NULL) { | ||
| 639 | pos = strlen(line); | ||
| 640 | if (line[pos - 1] == '\n') { | ||
| 641 | line[pos - 1] = '\0'; | ||
| 642 | } | ||
| 643 | |||
| 644 | if (line[0] == '#') { | ||
| 645 | continue; | ||
| 646 | } | ||
| 647 | |||
| 648 | switch (expected) { | ||
| 649 | case STATE_FILE_VERSION: | ||
| 650 | i = atoi(line); | ||
| 651 | if (i != NP_STATE_FORMAT_VERSION) { | ||
| 652 | failure++; | ||
| 653 | } else { | ||
| 654 | expected = STATE_DATA_VERSION; | ||
| 655 | } | ||
| 656 | break; | ||
| 657 | case STATE_DATA_VERSION: | ||
| 658 | i = atoi(line); | ||
| 659 | if (i != this_monitoring_plugin->state->data_version) { | ||
| 660 | failure++; | ||
| 661 | } else { | ||
| 662 | expected = STATE_DATA_TIME; | ||
| 663 | } | ||
| 664 | break; | ||
| 665 | case STATE_DATA_TIME: | ||
| 666 | /* If time > now, error */ | ||
| 667 | data_time = strtoul(line, NULL, 10); | ||
| 668 | if (data_time > current_time) { | ||
| 669 | failure++; | ||
| 670 | } else { | ||
| 671 | this_monitoring_plugin->state->state_data->time = data_time; | ||
| 672 | expected = STATE_DATA_TEXT; | ||
| 673 | } | ||
| 674 | break; | ||
| 675 | case STATE_DATA_TEXT: | ||
| 676 | this_monitoring_plugin->state->state_data->data = strdup(line); | ||
| 677 | if (this_monitoring_plugin->state->state_data->data == NULL) { | ||
| 678 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
| 679 | } | ||
| 680 | expected = STATE_DATA_END; | ||
| 681 | status = true; | ||
| 682 | break; | ||
| 683 | case STATE_DATA_END:; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | |||
| 687 | np_free(line); | ||
| 688 | return status; | ||
| 689 | } | ||
| 690 | |||
| 691 | /* | ||
| 692 | * If time=NULL, use current time. Create state file, with state format | ||
| 693 | * version, default text. Writes version, time, and data. Avoid locking | ||
| 694 | * problems - use mv to write and then swap. Possible loss of state data if | ||
| 695 | * two things writing to same key at same time. | ||
| 696 | * Will die with UNKNOWN if errors | ||
| 697 | */ | ||
| 698 | void np_state_write_string(time_t data_time, char *data_string) { | ||
| 699 | FILE *fp; | ||
| 700 | char *temp_file = NULL; | ||
| 701 | int fd = 0, result = 0; | ||
| 702 | time_t current_time; | ||
| 703 | char *directories = NULL; | ||
| 704 | char *p = NULL; | ||
| 705 | |||
| 706 | if (data_time == 0) { | ||
| 707 | time(¤t_time); | ||
| 708 | } else { | ||
| 709 | current_time = data_time; | ||
| 710 | } | ||
| 711 | |||
| 712 | /* If file doesn't currently exist, create directories */ | ||
| 713 | if (access(this_monitoring_plugin->state->_filename, F_OK) != 0) { | ||
| 714 | result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); | ||
| 715 | if (result < 0) { | ||
| 716 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
| 717 | } | ||
| 718 | |||
| 719 | for (p = directories + 1; *p; p++) { | ||
| 720 | if (*p == '/') { | ||
| 721 | *p = '\0'; | ||
| 722 | if ((access(directories, F_OK) != 0) && (mkdir(directories, S_IRWXU) != 0)) { | ||
| 723 | /* Can't free this! Otherwise error message is wrong! */ | ||
| 724 | /* np_free(directories); */ | ||
| 725 | die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); | ||
| 726 | } | ||
| 727 | *p = '/'; | ||
| 728 | } | ||
| 729 | } | ||
| 730 | np_free(directories); | ||
| 731 | } | ||
| 732 | |||
| 733 | result = asprintf(&temp_file, "%s.XXXXXX", this_monitoring_plugin->state->_filename); | ||
| 734 | if (result < 0) { | ||
| 735 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
| 736 | } | ||
| 737 | |||
| 738 | if ((fd = mkstemp(temp_file)) == -1) { | ||
| 739 | np_free(temp_file); | ||
| 740 | die(STATE_UNKNOWN, _("Cannot create temporary filename")); | ||
| 741 | } | ||
| 742 | |||
| 743 | fp = (FILE *)fdopen(fd, "w"); | ||
| 744 | if (fp == NULL) { | ||
| 745 | close(fd); | ||
| 746 | unlink(temp_file); | ||
| 747 | np_free(temp_file); | ||
| 748 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); | ||
| 749 | } | ||
| 750 | |||
| 751 | fprintf(fp, "# NP State file\n"); | ||
| 752 | fprintf(fp, "%d\n", NP_STATE_FORMAT_VERSION); | ||
| 753 | fprintf(fp, "%d\n", this_monitoring_plugin->state->data_version); | ||
| 754 | fprintf(fp, "%lu\n", current_time); | ||
| 755 | fprintf(fp, "%s\n", data_string); | ||
| 756 | |||
| 757 | fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP); | ||
| 758 | |||
| 759 | fflush(fp); | ||
| 760 | |||
| 761 | result = fclose(fp); | ||
| 762 | |||
| 763 | fsync(fd); | ||
| 764 | |||
| 765 | if (result != 0) { | ||
| 766 | unlink(temp_file); | ||
| 767 | np_free(temp_file); | ||
| 768 | die(STATE_UNKNOWN, _("Error writing temp file")); | ||
| 769 | } | ||
| 770 | |||
| 771 | if (rename(temp_file, this_monitoring_plugin->state->_filename) != 0) { | ||
| 772 | unlink(temp_file); | ||
| 773 | np_free(temp_file); | ||
| 774 | die(STATE_UNKNOWN, _("Cannot rename state temp file")); | ||
| 775 | } | ||
| 776 | |||
| 777 | np_free(temp_file); | ||
| 778 | } | ||
