summaryrefslogtreecommitdiffstats
path: root/lib/utils_base.c
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@aei.ca>2014-01-21 12:59:07 (GMT)
committerThomas Guyot-Sionnest <dermoth@aei.ca>2014-01-21 12:59:07 (GMT)
commit11cf54ca78185d820085a476cdbf7d8f9149c292 (patch)
treef91c77af08f3bbe60be2a1a699033ad7eda27ba2 /lib/utils_base.c
parent66544851f7a4f5eeaf86d1cee8b1f54c76f98433 (diff)
parent6141cfebf6c42873e0181105d2051143d8c11eb7 (diff)
downloadmonitoring-plugins-11cf54ca78185d820085a476cdbf7d8f9149c292.tar.gz
Merge branch 'rename'
Diffstat (limited to 'lib/utils_base.c')
-rw-r--r--lib/utils_base.c100
1 files changed, 50 insertions, 50 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 54463e9..57d01e2 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -3,7 +3,7 @@
3* utils_base.c 3* utils_base.c
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2006 Nagios Plugins Development Team 6* Copyright (c) 2006 Monitoring Plugins Development Team
7* 7*
8* Library of useful functions for plugins 8* Library of useful functions for plugins
9* 9*
@@ -33,53 +33,53 @@
33 33
34#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } 34#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
35 35
36nagios_plugin *this_nagios_plugin=NULL; 36monitoring_plugin *this_monitoring_plugin=NULL;
37 37
38int _np_state_read_file(FILE *); 38int _np_state_read_file(FILE *);
39 39
40void np_init( char *plugin_name, int argc, char **argv ) { 40void np_init( char *plugin_name, int argc, char **argv ) {
41 if (this_nagios_plugin==NULL) { 41 if (this_monitoring_plugin==NULL) {
42 this_nagios_plugin = calloc(1, sizeof(nagios_plugin)); 42 this_monitoring_plugin = calloc(1, sizeof(monitoring_plugin));
43 if (this_nagios_plugin==NULL) { 43 if (this_monitoring_plugin==NULL) {
44 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 44 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
45 strerror(errno)); 45 strerror(errno));
46 } 46 }
47 this_nagios_plugin->plugin_name = strdup(plugin_name); 47 this_monitoring_plugin->plugin_name = strdup(plugin_name);
48 if (this_nagios_plugin->plugin_name==NULL) 48 if (this_monitoring_plugin->plugin_name==NULL)
49 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); 49 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
50 this_nagios_plugin->argc = argc; 50 this_monitoring_plugin->argc = argc;
51 this_nagios_plugin->argv = argv; 51 this_monitoring_plugin->argv = argv;
52 } 52 }
53} 53}
54 54
55void np_set_args( int argc, char **argv ) { 55void np_set_args( int argc, char **argv ) {
56 if (this_nagios_plugin==NULL) 56 if (this_monitoring_plugin==NULL)
57 die(STATE_UNKNOWN, _("This requires np_init to be called")); 57 die(STATE_UNKNOWN, _("This requires np_init to be called"));
58 58
59 this_nagios_plugin->argc = argc; 59 this_monitoring_plugin->argc = argc;
60 this_nagios_plugin->argv = argv; 60 this_monitoring_plugin->argv = argv;
61} 61}
62 62
63 63
64void np_cleanup() { 64void np_cleanup() {
65 if (this_nagios_plugin!=NULL) { 65 if (this_monitoring_plugin!=NULL) {
66 if(this_nagios_plugin->state!=NULL) { 66 if(this_monitoring_plugin->state!=NULL) {
67 if(this_nagios_plugin->state->state_data) { 67 if(this_monitoring_plugin->state->state_data) {
68 np_free(this_nagios_plugin->state->state_data->data); 68 np_free(this_monitoring_plugin->state->state_data->data);
69 np_free(this_nagios_plugin->state->state_data); 69 np_free(this_monitoring_plugin->state->state_data);
70 } 70 }
71 np_free(this_nagios_plugin->state->name); 71 np_free(this_monitoring_plugin->state->name);
72 np_free(this_nagios_plugin->state); 72 np_free(this_monitoring_plugin->state);
73 } 73 }
74 np_free(this_nagios_plugin->plugin_name); 74 np_free(this_monitoring_plugin->plugin_name);
75 np_free(this_nagios_plugin); 75 np_free(this_monitoring_plugin);
76 } 76 }
77 this_nagios_plugin=NULL; 77 this_monitoring_plugin=NULL;
78} 78}
79 79
80/* Hidden function to get a pointer to this_nagios_plugin for testing */ 80/* Hidden function to get a pointer to this_monitoring_plugin for testing */
81void _get_nagios_plugin( nagios_plugin **pointer ){ 81void _get_monitoring_plugin( monitoring_plugin **pointer ){
82 *pointer = this_nagios_plugin; 82 *pointer = this_monitoring_plugin;
83} 83}
84 84
85void 85void
@@ -89,7 +89,7 @@ die (int result, const char *fmt, ...)
89 va_start (ap, fmt); 89 va_start (ap, fmt);
90 vprintf (fmt, ap); 90 vprintf (fmt, ap);
91 va_end (ap); 91 va_end (ap);
92 if(this_nagios_plugin!=NULL) { 92 if(this_monitoring_plugin!=NULL) {
93 np_cleanup(); 93 np_cleanup();
94 } 94 }
95 exit (result); 95 exit (result);
@@ -375,14 +375,14 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
375char *_np_state_generate_key() { 375char *_np_state_generate_key() {
376 struct sha1_ctx ctx; 376 struct sha1_ctx ctx;
377 int i; 377 int i;
378 char **argv = this_nagios_plugin->argv; 378 char **argv = this_monitoring_plugin->argv;
379 unsigned char result[20]; 379 unsigned char result[20];
380 char keyname[41]; 380 char keyname[41];
381 char *p=NULL; 381 char *p=NULL;
382 382
383 sha1_init_ctx(&ctx); 383 sha1_init_ctx(&ctx);
384 384
385 for(i=0; i<this_nagios_plugin->argc; i++) { 385 for(i=0; i<this_monitoring_plugin->argc; i++) {
386 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx); 386 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx);
387 } 387 }
388 388
@@ -401,9 +401,9 @@ char *_np_state_generate_key() {
401} 401}
402 402
403void _cleanup_state_data() { 403void _cleanup_state_data() {
404 if (this_nagios_plugin->state->state_data!=NULL) { 404 if (this_monitoring_plugin->state->state_data!=NULL) {
405 np_free(this_nagios_plugin->state->state_data->data); 405 np_free(this_monitoring_plugin->state->state_data->data);
406 np_free(this_nagios_plugin->state->state_data); 406 np_free(this_monitoring_plugin->state->state_data);
407 } 407 }
408} 408}
409 409
@@ -432,7 +432,7 @@ void np_enable_state(char *keyname, int expected_data_version) {
432 char *temp_keyname = NULL; 432 char *temp_keyname = NULL;
433 char *p=NULL; 433 char *p=NULL;
434 434
435 if(this_nagios_plugin==NULL) 435 if(this_monitoring_plugin==NULL)
436 die(STATE_UNKNOWN, _("This requires np_init to be called")); 436 die(STATE_UNKNOWN, _("This requires np_init to be called"));
437 437
438 this_state = (state_key *) calloc(1, sizeof(state_key)); 438 this_state = (state_key *) calloc(1, sizeof(state_key));
@@ -456,15 +456,15 @@ void np_enable_state(char *keyname, int expected_data_version) {
456 p++; 456 p++;
457 } 457 }
458 this_state->name=temp_keyname; 458 this_state->name=temp_keyname;
459 this_state->plugin_name=this_nagios_plugin->plugin_name; 459 this_state->plugin_name=this_monitoring_plugin->plugin_name;
460 this_state->data_version=expected_data_version; 460 this_state->data_version=expected_data_version;
461 this_state->state_data=NULL; 461 this_state->state_data=NULL;
462 462
463 /* Calculate filename */ 463 /* Calculate filename */
464 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name); 464 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_monitoring_plugin->plugin_name, this_state->name);
465 this_state->_filename=temp_filename; 465 this_state->_filename=temp_filename;
466 466
467 this_nagios_plugin->state = this_state; 467 this_monitoring_plugin->state = this_state;
468} 468}
469 469
470/* 470/*
@@ -479,11 +479,11 @@ state_data *np_state_read() {
479 FILE *statefile; 479 FILE *statefile;
480 int rc = FALSE; 480 int rc = FALSE;
481 481
482 if(this_nagios_plugin==NULL) 482 if(this_monitoring_plugin==NULL)
483 die(STATE_UNKNOWN, _("This requires np_init to be called")); 483 die(STATE_UNKNOWN, _("This requires np_init to be called"));
484 484
485 /* Open file. If this fails, no previous state found */ 485 /* Open file. If this fails, no previous state found */
486 statefile = fopen( this_nagios_plugin->state->_filename, "r" ); 486 statefile = fopen( this_monitoring_plugin->state->_filename, "r" );
487 if(statefile!=NULL) { 487 if(statefile!=NULL) {
488 488
489 this_state_data = (state_data *) calloc(1, sizeof(state_data)); 489 this_state_data = (state_data *) calloc(1, sizeof(state_data));
@@ -492,7 +492,7 @@ state_data *np_state_read() {
492 strerror(errno)); 492 strerror(errno));
493 493
494 this_state_data->data=NULL; 494 this_state_data->data=NULL;
495 this_nagios_plugin->state->state_data = this_state_data; 495 this_monitoring_plugin->state->state_data = this_state_data;
496 496
497 rc = _np_state_read_file(statefile); 497 rc = _np_state_read_file(statefile);
498 498
@@ -503,10 +503,10 @@ state_data *np_state_read() {
503 _cleanup_state_data(); 503 _cleanup_state_data();
504 } 504 }
505 505
506 return this_nagios_plugin->state->state_data; 506 return this_monitoring_plugin->state->state_data;
507} 507}
508 508
509/* 509/*
510 * Read the state file 510 * Read the state file
511 */ 511 */
512int _np_state_read_file(FILE *f) { 512int _np_state_read_file(FILE *f) {
@@ -544,7 +544,7 @@ int _np_state_read_file(FILE *f) {
544 break; 544 break;
545 case STATE_DATA_VERSION: 545 case STATE_DATA_VERSION:
546 i=atoi(line); 546 i=atoi(line);
547 if(i != this_nagios_plugin->state->data_version) 547 if(i != this_monitoring_plugin->state->data_version)
548 failure++; 548 failure++;
549 else 549 else
550 expected=STATE_DATA_TIME; 550 expected=STATE_DATA_TIME;
@@ -555,13 +555,13 @@ int _np_state_read_file(FILE *f) {
555 if(data_time > current_time) 555 if(data_time > current_time)
556 failure++; 556 failure++;
557 else { 557 else {
558 this_nagios_plugin->state->state_data->time = data_time; 558 this_monitoring_plugin->state->state_data->time = data_time;
559 expected=STATE_DATA_TEXT; 559 expected=STATE_DATA_TEXT;
560 } 560 }
561 break; 561 break;
562 case STATE_DATA_TEXT: 562 case STATE_DATA_TEXT:
563 this_nagios_plugin->state->state_data->data = strdup(line); 563 this_monitoring_plugin->state->state_data->data = strdup(line);
564 if(this_nagios_plugin->state->state_data->data==NULL) 564 if(this_monitoring_plugin->state->state_data->data==NULL)
565 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); 565 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
566 expected=STATE_DATA_END; 566 expected=STATE_DATA_END;
567 status=TRUE; 567 status=TRUE;
@@ -596,8 +596,8 @@ void np_state_write_string(time_t data_time, char *data_string) {
596 current_time=data_time; 596 current_time=data_time;
597 597
598 /* If file doesn't currently exist, create directories */ 598 /* If file doesn't currently exist, create directories */
599 if(access(this_nagios_plugin->state->_filename,F_OK)!=0) { 599 if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) {
600 asprintf(&directories, "%s", this_nagios_plugin->state->_filename); 600 asprintf(&directories, "%s", this_monitoring_plugin->state->_filename);
601 if(directories==NULL) 601 if(directories==NULL)
602 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 602 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
603 strerror(errno)); 603 strerror(errno));
@@ -607,7 +607,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
607 *p='\0'; 607 *p='\0';
608 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) { 608 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
609 /* Can't free this! Otherwise error message is wrong! */ 609 /* Can't free this! Otherwise error message is wrong! */
610 /* np_free(directories); */ 610 /* np_free(directories); */
611 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); 611 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories);
612 } 612 }
613 *p='/'; 613 *p='/';
@@ -616,7 +616,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
616 np_free(directories); 616 np_free(directories);
617 } 617 }
618 618
619 asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename); 619 asprintf(&temp_file,"%s.XXXXXX",this_monitoring_plugin->state->_filename);
620 if(temp_file==NULL) 620 if(temp_file==NULL)
621 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 621 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
622 strerror(errno)); 622 strerror(errno));
@@ -636,7 +636,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
636 636
637 fprintf(fp,"# NP State file\n"); 637 fprintf(fp,"# NP State file\n");
638 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); 638 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION);
639 fprintf(fp,"%d\n",this_nagios_plugin->state->data_version); 639 fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version);
640 fprintf(fp,"%lu\n",current_time); 640 fprintf(fp,"%lu\n",current_time);
641 fprintf(fp,"%s\n",data_string); 641 fprintf(fp,"%s\n",data_string);
642 642
@@ -654,7 +654,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
654 die(STATE_UNKNOWN, _("Error writing temp file")); 654 die(STATE_UNKNOWN, _("Error writing temp file"));
655 } 655 }
656 656
657 if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) { 657 if(rename(temp_file, this_monitoring_plugin->state->_filename)!=0) {
658 unlink(temp_file); 658 unlink(temp_file);
659 np_free(temp_file); 659 np_free(temp_file);
660 die(STATE_UNKNOWN, _("Cannot rename state temp file")); 660 die(STATE_UNKNOWN, _("Cannot rename state temp file"));