summaryrefslogtreecommitdiffstats
path: root/lib/utils_base.c
diff options
context:
space:
mode:
authorTon Voon <ton.voon@opsera.com>2010-06-17 09:16:43 (GMT)
committertonvoon <ton.voon@opsera.com>2010-06-23 13:30:34 (GMT)
commit18f6835edaf7d640a2c9e476cb1babdbdadbfd9b (patch)
treeae11f40e48dc34658445c99012726f32bfb45c56 /lib/utils_base.c
parentf61412478ceb7c821793c8356b936f64066508bf (diff)
downloadmonitoring-plugins-18f6835edaf7d640a2c9e476cb1babdbdadbfd9b.tar.gz
Added state retention APIs. Implemented for check_snmp with --rate option.
See http://nagiosplugin.org/c-api-private for more details on the API. Also updated check_snmp -l option to change the perfdata label.
Diffstat (limited to 'lib/utils_base.c')
-rw-r--r--lib/utils_base.c354
1 files changed, 353 insertions, 1 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 4303e15..6de92cb 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -27,6 +27,56 @@
27#include "common.h" 27#include "common.h"
28#include <stdarg.h> 28#include <stdarg.h>
29#include "utils_base.h" 29#include "utils_base.h"
30#include <fcntl.h>
31
32#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
33
34nagios_plugin *this_nagios_plugin=NULL;
35
36void np_init( char *plugin_name, int argc, char **argv ) {
37 if (this_nagios_plugin==NULL) {
38 this_nagios_plugin = malloc(sizeof(nagios_plugin));
39 if (this_nagios_plugin==NULL) {
40 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
41 strerror(errno));
42 }
43 this_nagios_plugin->plugin_name = strdup(plugin_name);
44 if (this_nagios_plugin->plugin_name==NULL)
45 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
46 this_nagios_plugin->argc = argc;
47 this_nagios_plugin->argv = argv;
48 }
49}
50
51void np_set_args( int argc, char **argv ) {
52 if (this_nagios_plugin==NULL)
53 die(STATE_UNKNOWN, _("This requires np_init to be called"));
54
55 this_nagios_plugin->argc = argc;
56 this_nagios_plugin->argv = argv;
57}
58
59
60void np_cleanup() {
61 if (this_nagios_plugin!=NULL) {
62 if(this_nagios_plugin->state!=NULL) {
63 if(this_nagios_plugin->state->state_data) {
64 np_free(this_nagios_plugin->state->state_data->data);
65 np_free(this_nagios_plugin->state->state_data);
66 }
67 np_free(this_nagios_plugin->state->name);
68 np_free(this_nagios_plugin->state);
69 }
70 np_free(this_nagios_plugin->plugin_name);
71 np_free(this_nagios_plugin);
72 }
73 this_nagios_plugin=NULL;
74}
75
76/* Hidden function to get a pointer to this_nagios_plugin for testing */
77void _get_nagios_plugin( nagios_plugin **pointer ){
78 *pointer = this_nagios_plugin;
79}
30 80
31void 81void
32die (int result, const char *fmt, ...) 82die (int result, const char *fmt, ...)
@@ -35,6 +85,9 @@ die (int result, const char *fmt, ...)
35 va_start (ap, fmt); 85 va_start (ap, fmt);
36 vprintf (fmt, ap); 86 vprintf (fmt, ap);
37 va_end (ap); 87 va_end (ap);
88 if(this_nagios_plugin!=NULL) {
89 np_cleanup();
90 }
38 exit (result); 91 exit (result);
39} 92}
40 93
@@ -102,7 +155,7 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
102 thresholds *temp_thresholds = NULL; 155 thresholds *temp_thresholds = NULL;
103 156
104 if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL) 157 if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL)
105 die(STATE_UNKNOWN, _("Cannot allocate memory: %s\n"), 158 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
106 strerror(errno)); 159 strerror(errno));
107 160
108 temp_thresholds->warning = NULL; 161 temp_thresholds->warning = NULL;
@@ -310,3 +363,302 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
310 return value; 363 return value;
311} 364}
312 365
366/*
367 * Returns a string to use as a keyname, based on an md5 hash of argv, thus
368 * hopefully a unique key per service/plugin invocation. Use the extra-opts
369 * parse of argv, so that uniqueness in parameters are reflected there.
370 */
371char *_np_state_generate_key() {
372 struct sha1_ctx ctx;
373 int i;
374 char **argv = this_nagios_plugin->argv;
375 unsigned char result[20];
376 char keyname[41];
377 char *p=NULL;
378
379 sha1_init_ctx(&ctx);
380
381 for(i=0; i<this_nagios_plugin->argc; i++) {
382 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx);
383 }
384
385 sha1_finish_ctx(&ctx, &result);
386
387 for (i=0; i<20; ++i) {
388 sprintf(&keyname[2*i], "%02x", result[i]);
389 }
390 keyname[40]='\0';
391
392 p = strdup(keyname);
393 if(p==NULL) {
394 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
395 }
396 return p;
397}
398
399void _cleanup_state_data() {
400 if (this_nagios_plugin->state->state_data!=NULL) {
401 np_free(this_nagios_plugin->state->state_data->data);
402 np_free(this_nagios_plugin->state->state_data);
403 }
404}
405
406/*
407 * Internal function. Returns either:
408 * envvar NAGIOS_PLUGIN_STATE_DIRECTORY
409 * statically compiled shared state directory
410 */
411char* _np_state_calculate_location_prefix(){
412 char *env_dir;
413
414 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
415 if(env_dir && env_dir[0] != '\0')
416 return env_dir;
417 return NP_STATE_DIR_PREFIX;
418}
419
420/*
421 * Initiatializer for state routines.
422 * Sets variables. Generates filename. Returns np_state_key. die with
423 * UNKNOWN if exception
424 */
425void np_enable_state(char *keyname, int expected_data_version) {
426 state_key *this_state = NULL;
427 char *temp_filename = NULL;
428 char *temp_keyname = NULL;
429 char *p=NULL;
430
431 if(this_nagios_plugin==NULL)
432 die(STATE_UNKNOWN, _("This requires np_init to be called"));
433
434 this_state = (state_key *) malloc(sizeof(state_key));
435 if(this_state==NULL)
436 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
437 strerror(errno));
438
439 if(keyname==NULL) {
440 temp_keyname = _np_state_generate_key();
441 } else {
442 temp_keyname = strdup(keyname);
443 if(temp_keyname==NULL)
444 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
445 }
446 /* Die if invalid characters used for keyname */
447 p = temp_keyname;
448 while(*p!='\0') {
449 if(! (isalnum(*p) || *p == '_')) {
450 die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'"));
451 }
452 p++;
453 }
454 this_state->name=temp_keyname;
455 this_state->plugin_name=this_nagios_plugin->plugin_name;
456 this_state->data_version=expected_data_version;
457 this_state->state_data=NULL;
458
459 /* Calculate filename */
460 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name);
461 this_state->_filename=temp_filename;
462
463 this_nagios_plugin->state = this_state;
464}
465
466/*
467 * Will return NULL if no data is available (first run). If key currently
468 * exists, read data. If state file format version is not expected, return
469 * as if no data. Get state data version number and compares to expected.
470 * If numerically lower, then return as no previous state. die with UNKNOWN
471 * if exceptional error.
472 */
473state_data *np_state_read() {
474 state_key *my_state_key;
475 state_data *this_state_data=NULL;
476 FILE *statefile;
477 int c;
478 int rc = FALSE;
479
480 if(this_nagios_plugin==NULL)
481 die(STATE_UNKNOWN, _("This requires np_init to be called"));
482
483 /* Open file. If this fails, no previous state found */
484 statefile = fopen( this_nagios_plugin->state->_filename, "r" );
485 if(statefile!=NULL) {
486
487 this_state_data = (state_data *) malloc(sizeof(state_data));
488 if(this_state_data==NULL)
489 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
490 strerror(errno));
491
492 this_state_data->data=NULL;
493 this_nagios_plugin->state->state_data = this_state_data;
494
495 rc = _np_state_read_file(statefile);
496
497 fclose(statefile);
498 }
499
500 if(rc==FALSE) {
501 _cleanup_state_data();
502 }
503
504 return this_nagios_plugin->state->state_data;
505}
506
507/*
508 * Read the state file
509 */
510int _np_state_read_file(FILE *f) {
511 int c, status=FALSE;
512 size_t pos;
513 char *line;
514 int i;
515 int failure=0;
516 time_t current_time, data_time;
517 enum { STATE_FILE_VERSION, STATE_DATA_VERSION, STATE_DATA_TIME, STATE_DATA_TEXT, STATE_DATA_END } expected=STATE_FILE_VERSION;
518
519 time(&current_time);
520
521 /* Note: This introduces a limit of 1024 bytes in the string data */
522 line = (char *) malloc(1024);
523 if(line==NULL)
524 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
525 strerror(errno));
526
527 while(!failure && (fgets(line,1024,f))!=NULL){
528 pos=strlen(line);
529 if(line[pos-1]=='\n') {
530 line[pos-1]='\0';
531 }
532
533 if(line[0] == '#') continue;
534
535 switch(expected) {
536 case STATE_FILE_VERSION:
537 i=atoi(line);
538 if(i!=NP_STATE_FORMAT_VERSION)
539 failure++;
540 else
541 expected=STATE_DATA_VERSION;
542 break;
543 case STATE_DATA_VERSION:
544 i=atoi(line);
545 if(i != this_nagios_plugin->state->data_version)
546 failure++;
547 else
548 expected=STATE_DATA_TIME;
549 break;
550 case STATE_DATA_TIME:
551 /* If time > now, error */
552 data_time=strtoul(line,NULL,10);
553 if(data_time > current_time)
554 failure++;
555 else {
556 this_nagios_plugin->state->state_data->time = data_time;
557 expected=STATE_DATA_TEXT;
558 }
559 break;
560 case STATE_DATA_TEXT:
561 this_nagios_plugin->state->state_data->data = strdup(line);
562 if(this_nagios_plugin->state->state_data->data==NULL)
563 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
564 expected=STATE_DATA_END;
565 status=TRUE;
566 break;
567 case STATE_DATA_END:
568 ;
569 }
570 }
571
572 np_free(line);
573 return status;
574}
575
576/*
577 * If time=NULL, use current time. Create state file, with state format
578 * version, default text. Writes version, time, and data. Avoid locking
579 * problems - use mv to write and then swap. Possible loss of state data if
580 * two things writing to same key at same time.
581 * Will die with UNKNOWN if errors
582 */
583void np_state_write_string(time_t data_time, char *data_string) {
584 FILE *fp;
585 char *temp_file=NULL;
586 int fd=0, result=0;
587 time_t current_time;
588 size_t len;
589 char *directories=NULL;
590 char *p=NULL;
591
592 if(data_time==0)
593 time(&current_time);
594 else
595 current_time=data_time;
596
597 /* If file doesn't currently exist, create directories */
598 if(access(this_nagios_plugin->state->_filename,F_OK)!=0) {
599 asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
600 if(directories==NULL)
601 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
602 strerror(errno));
603
604 for(p=directories+1; *p; p++) {
605 if(*p=='/') {
606 *p='\0';
607 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
608 /* Can't free this! Otherwise error message is wrong! */
609 /* np_free(directories); */
610 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories);
611 }
612 *p='/';
613 }
614 }
615 np_free(directories);
616 }
617
618 asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
619 if(temp_file==NULL)
620 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
621 strerror(errno));
622
623 if((fd=mkstemp(temp_file))==-1) {
624 np_free(temp_file);
625 die(STATE_UNKNOWN, _("Cannot create temporary filename"));
626 }
627
628 fp=(FILE *)fdopen(fd,"w");
629 if(fp==NULL) {
630 close(fd);
631 unlink(temp_file);
632 np_free(temp_file);
633 die(STATE_UNKNOWN, _("Unable to open temporary state file"));
634 }
635
636 fprintf(fp,"# NP State file\n");
637 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION);
638 fprintf(fp,"%d\n",this_nagios_plugin->state->data_version);
639 fprintf(fp,"%lu\n",current_time);
640 fprintf(fp,"%s\n",data_string);
641
642 fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP);
643
644 fflush(fp);
645
646 result=fclose(fp);
647
648 fsync(fd);
649
650 if(result!=0) {
651 unlink(temp_file);
652 np_free(temp_file);
653 die(STATE_UNKNOWN, _("Error writing temp file"));
654 }
655
656 if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) {
657 unlink(temp_file);
658 np_free(temp_file);
659 die(STATE_UNKNOWN, _("Cannot rename state temp file"));
660 }
661
662 np_free(temp_file);
663}
664