summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am6
-rw-r--r--lib/extra_opts.c4
-rw-r--r--lib/extra_opts.h2
-rw-r--r--lib/parse_ini.c19
-rw-r--r--lib/parse_ini.h2
-rw-r--r--lib/tests/Makefile.am2
-rwxr-xr-xlib/tests/test_base64.t2
-rw-r--r--lib/tests/test_cmd.t2
-rwxr-xr-xlib/tests/test_disk.t2
-rwxr-xr-xlib/tests/test_ini1.t2
-rwxr-xr-xlib/tests/test_ini3.t2
-rwxr-xr-xlib/tests/test_opts1.t2
-rwxr-xr-xlib/tests/test_opts2.t2
-rwxr-xr-xlib/tests/test_opts3.t2
-rwxr-xr-xlib/tests/test_tcp.t2
-rw-r--r--lib/tests/test_utils.c120
-rwxr-xr-xlib/tests/test_utils.t2
-rw-r--r--lib/utils_base.c134
-rw-r--r--lib/utils_base.h15
-rw-r--r--lib/utils_cmd.c4
-rw-r--r--lib/utils_cmd.h2
-rw-r--r--lib/utils_disk.c2
-rw-r--r--lib/utils_tcp.c2
23 files changed, 216 insertions, 118 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 99fa591..1515312 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -2,15 +2,15 @@
2 2
3SUBDIRS = . tests 3SUBDIRS = . tests
4 4
5noinst_LIBRARIES = libnagiosplug.a 5noinst_LIBRARIES = libmonitoringplug.a
6 6
7AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" 7AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\"
8 8
9libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c 9libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c
10EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h 10EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h
11 11
12if USE_PARSE_INI 12if USE_PARSE_INI
13libnagiosplug_a_SOURCES += parse_ini.c extra_opts.c 13libmonitoringplug_a_SOURCES += parse_ini.c extra_opts.c
14endif USE_PARSE_INI 14endif USE_PARSE_INI
15 15
16INCLUDES = -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins 16INCLUDES = -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
diff --git a/lib/extra_opts.c b/lib/extra_opts.c
index 2939c7a..f4d5e66 100644
--- a/lib/extra_opts.c
+++ b/lib/extra_opts.c
@@ -1,9 +1,9 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Nagios-plugins extra_opts library 3* Monitoring Plugins extra_opts library
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2007 Nagios Plugins Development Team 6* Copyright (c) 2007 Monitoring Plugins Development Team
7* 7*
8* This program is free software: you can redistribute it and/or modify 8* This program is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by 9* it under the terms of the GNU General Public License as published by
diff --git a/lib/extra_opts.h b/lib/extra_opts.h
index 4bb7b73..8ff14a1 100644
--- a/lib/extra_opts.h
+++ b/lib/extra_opts.h
@@ -2,7 +2,7 @@
2#define _EXTRA_OPTS_H_ 2#define _EXTRA_OPTS_H_
3 3
4/* 4/*
5 * extra_opts.h: routines for loading nagios-plugin defaults from ini 5 * extra_opts.h: routines for loading monitoring-plugin defaults from ini
6 * configuration files. 6 * configuration files.
7 */ 7 */
8 8
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index 004396f..76953e9 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -1,9 +1,9 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Nagios-plugins parse_ini library 3* Monitoring Plugins parse_ini library
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2007 Nagios Plugins Development Team 6* Copyright (c) 2007 Monitoring Plugins Development Team
7* 7*
8* This program is free software: you can redistribute it and/or modify 8* This program is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by 9* it under the terms of the GNU General Public License as published by
@@ -49,7 +49,7 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
49static int add_option(FILE *f, np_arg_list **optlst); 49static int add_option(FILE *f, np_arg_list **optlst);
50/* internal function to find default file */ 50/* internal function to find default file */
51static char* default_file(void); 51static char* default_file(void);
52/* internal function to stat() files */ 52/* internal function to test files access */
53static int test_file(const char* env, int len, const char* file, char* temp_file); 53static int test_file(const char* env, int len, const char* file, char* temp_file);
54 54
55/* parse_locator decomposes a string of the form 55/* parse_locator decomposes a string of the form
@@ -101,7 +101,7 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
101 } else { 101 } else {
102 inifile=fopen(i.file, "r"); 102 inifile=fopen(i.file, "r");
103 } 103 }
104 if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file")); 104 if(inifile==NULL) die(STATE_UNKNOWN, "%s\n", _("Can't read config file"));
105 if(read_defaults(inifile, i.stanza, &defaults)==FALSE) 105 if(read_defaults(inifile, i.stanza, &defaults)==FALSE)
106 die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file); 106 die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file);
107 107
@@ -163,7 +163,7 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
163 * we're dealing with a config error 163 * we're dealing with a config error
164 */ 164 */
165 case NOSTANZA: 165 case NOSTANZA:
166 die(STATE_UNKNOWN, _("Config file error")); 166 die(STATE_UNKNOWN, "%s\n", _("Config file error"));
167 break; 167 break;
168 /* we're in a stanza, but for a different plugin */ 168 /* we're in a stanza, but for a different plugin */
169 case WRONGSTANZA: 169 case WRONGSTANZA:
@@ -173,7 +173,7 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
173 case RIGHTSTANZA: 173 case RIGHTSTANZA:
174 ungetc(c, f); 174 ungetc(c, f);
175 if(add_option(f, opts)){ 175 if(add_option(f, opts)){
176 die(STATE_UNKNOWN, _("Config file error")); 176 die(STATE_UNKNOWN, "%s\n", _("Config file error"));
177 } 177 }
178 status=TRUE; 178 status=TRUE;
179 break; 179 break;
@@ -229,7 +229,7 @@ static int add_option(FILE *f, np_arg_list **optlst){
229 if(optend==NULL) optend=eqptr; 229 if(optend==NULL) optend=eqptr;
230 --optend; 230 --optend;
231 /* ^[[:space:]]*=foo is a syntax error */ 231 /* ^[[:space:]]*=foo is a syntax error */
232 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n")); 232 if(optptr==eqptr) die(STATE_UNKNOWN, "%s\n", _("Config file error"));
233 /* continue from '=' to start of value or EOL */ 233 /* continue from '=' to start of value or EOL */
234 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++); 234 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
235 /* continue to the end of value */ 235 /* continue to the end of value */
@@ -256,7 +256,7 @@ static int add_option(FILE *f, np_arg_list **optlst){
256 cfg_len+=1; 256 cfg_len+=1;
257 } 257 }
258 /* A line with no equal sign isn't valid */ 258 /* A line with no equal sign isn't valid */
259 if(equals==0) die(STATE_UNKNOWN, _("Config file error\n")); 259 if(equals==0) die(STATE_UNKNOWN, "%s\n", _("Config file error"));
260 260
261 /* okay, now we have all the info we need, so we create a new np_arg_list 261 /* okay, now we have all the info we need, so we create a new np_arg_list
262 * element and set the argument... 262 * element and set the argument...
@@ -350,7 +350,6 @@ static char* default_file(void){
350 * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed. 350 * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
351 */ 351 */
352static int test_file(const char* env, int len, const char* file, char* temp_file){ 352static int test_file(const char* env, int len, const char* file, char* temp_file){
353 struct stat sb;
354 353
355 /* test if len + filelen + '/' + '\0' fits in temp_file */ 354 /* test if len + filelen + '/' + '\0' fits in temp_file */
356 if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1; 355 if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
@@ -360,7 +359,7 @@ static int test_file(const char* env, int len, const char* file, char* temp_file
360 strncat(temp_file,"/",len+1); 359 strncat(temp_file,"/",len+1);
361 strncat(temp_file,file,len+strlen(file)+1); 360 strncat(temp_file,file,len+strlen(file)+1);
362 361
363 if(stat(temp_file, &sb) != -1) return 1; 362 if(access(temp_file, F_OK) == 0) return 1;
364 return 0; 363 return 0;
365} 364}
366 365
diff --git a/lib/parse_ini.h b/lib/parse_ini.h
index e0ba816..a3a494e 100644
--- a/lib/parse_ini.h
+++ b/lib/parse_ini.h
@@ -2,7 +2,7 @@
2#define _PARSE_INI_H_ 2#define _PARSE_INI_H_
3 3
4/* 4/*
5 * parse_ini.h: routines for loading nagios-plugin defaults from ini 5 * parse_ini.h: routines for loading monitoring-plugin defaults from ini
6 * configuration files. 6 * configuration files.
7 */ 7 */
8 8
diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am
index 1af5891..c0411ac 100644
--- a/lib/tests/Makefile.am
+++ b/lib/tests/Makefile.am
@@ -28,7 +28,7 @@ endif
28 28
29AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags) 29AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags)
30AM_LDFLAGS = $(tap_ldflags) -ltap 30AM_LDFLAGS = $(tap_ldflags) -ltap
31LDADD = $(top_srcdir)/lib/libnagiosplug.a $(top_srcdir)/gl/libgnu.a 31LDADD = $(top_srcdir)/lib/libmonitoringplug.a $(top_srcdir)/gl/libgnu.a
32 32
33SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c 33SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c
34 34
diff --git a/lib/tests/test_base64.t b/lib/tests/test_base64.t
index 4b9236f..4ad6473 100755
--- a/lib/tests/test_base64.t
+++ b/lib/tests/test_base64.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_base64") { 3if (! -e "./test_base64") {
4 plan skip_all => "./test_base64 not compiled - please install tap library to test"; 4 plan skip_all => "./test_base64 not compiled - please enable libtap library to test";
5} 5}
6exec "./test_base64"; 6exec "./test_base64";
diff --git a/lib/tests/test_cmd.t b/lib/tests/test_cmd.t
index 4dd54ef..c50dded 100644
--- a/lib/tests/test_cmd.t
+++ b/lib/tests/test_cmd.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_cmd") { 3if (! -e "./test_cmd") {
4 plan skip_all => "./test_cmd not compiled - please install tap library to test"; 4 plan skip_all => "./test_cmd not compiled - please enable libtap library to test";
5} 5}
6exec "./test_cmd"; 6exec "./test_cmd";
diff --git a/lib/tests/test_disk.t b/lib/tests/test_disk.t
index d32567a..da84dfd 100755
--- a/lib/tests/test_disk.t
+++ b/lib/tests/test_disk.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_disk") { 3if (! -e "./test_disk") {
4 plan skip_all => "./test_disk not compiled - please install tap library to test"; 4 plan skip_all => "./test_disk not compiled - please enable libtap library to test";
5} 5}
6exec "./test_disk"; 6exec "./test_disk";
diff --git a/lib/tests/test_ini1.t b/lib/tests/test_ini1.t
index d481011..25baed9 100755
--- a/lib/tests/test_ini1.t
+++ b/lib/tests/test_ini1.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_ini1") { 3if (! -e "./test_ini1") {
4 plan skip_all => "./test_ini not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_ini not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6exec "./test_ini1"; 6exec "./test_ini1";
diff --git a/lib/tests/test_ini3.t b/lib/tests/test_ini3.t
index 4b0be4c..a2ca94a 100755
--- a/lib/tests/test_ini3.t
+++ b/lib/tests/test_ini3.t
@@ -4,7 +4,7 @@ use strict;
4use warnings; 4use warnings;
5 5
6if (! -e "./test_ini3") { 6if (! -e "./test_ini3") {
7 plan skip_all => "./test_ini not compiled - please install tap library and/or enable parse-ini to test"; 7 plan skip_all => "./test_ini not compiled - please enable libtap library and/or extra-opts to test";
8} 8}
9 9
10# array of argument arrays 10# array of argument arrays
diff --git a/lib/tests/test_opts1.t b/lib/tests/test_opts1.t
index 52eed94..1934f12 100755
--- a/lib/tests/test_opts1.t
+++ b/lib/tests/test_opts1.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_opts1") { 3if (! -e "./test_opts1") {
4 plan skip_all => "./test_opts1 not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_opts1 not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6exec "./test_opts1"; 6exec "./test_opts1";
diff --git a/lib/tests/test_opts2.t b/lib/tests/test_opts2.t
index f7444f6..59b9fdb 100755
--- a/lib/tests/test_opts2.t
+++ b/lib/tests/test_opts2.t
@@ -1,7 +1,7 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_opts2") { 3if (! -e "./test_opts2") {
4 plan skip_all => "./test_opts2 not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_opts2 not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6$ENV{"NAGIOS_CONFIG_PATH"} = "."; 6$ENV{"NAGIOS_CONFIG_PATH"} = ".";
7exec "./test_opts2"; 7exec "./test_opts2";
diff --git a/lib/tests/test_opts3.t b/lib/tests/test_opts3.t
index fa7876d..8d974ca 100755
--- a/lib/tests/test_opts3.t
+++ b/lib/tests/test_opts3.t
@@ -4,7 +4,7 @@ use strict;
4use warnings; 4use warnings;
5 5
6if (! -e "./test_opts3") { 6if (! -e "./test_opts3") {
7 plan skip_all => "./test_opts3 not compiled - please install tap library and/or enable parse-ini to test"; 7 plan skip_all => "./test_opts3 not compiled - please enable libtap library and/or extra-opts to test";
8} 8}
9 9
10# array of argument arrays 10# array of argument arrays
diff --git a/lib/tests/test_tcp.t b/lib/tests/test_tcp.t
index 8f34b67..e5e12fc 100755
--- a/lib/tests/test_tcp.t
+++ b/lib/tests/test_tcp.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_tcp") { 3if (! -e "./test_tcp") {
4 plan skip_all => "./test_tcp not compiled - please install tap library to test"; 4 plan skip_all => "./test_tcp not compiled - please enable libtap library to test";
5} 5}
6exec "./test_tcp"; 6exec "./test_tcp";
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 79e1110..356887d 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -32,28 +32,28 @@ main (int argc, char **argv)
32 range *range; 32 range *range;
33 double temp; 33 double temp;
34 thresholds *thresholds = NULL; 34 thresholds *thresholds = NULL;
35 int rc; 35 int i, rc;
36 char *temp_string; 36 char *temp_string;
37 state_key *temp_state_key = NULL; 37 state_key *temp_state_key = NULL;
38 state_data *temp_state_data; 38 state_data *temp_state_data;
39 time_t current_time; 39 time_t current_time;
40 40
41 plan_tests(150); 41 plan_tests(185);
42 42
43 ok( this_nagios_plugin==NULL, "nagios_plugin not initialised"); 43 ok( this_monitoring_plugin==NULL, "monitoring_plugin not initialised");
44 44
45 np_init( "check_test", argc, argv ); 45 np_init( "check_test", argc, argv );
46 46
47 ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised"); 47 ok( this_monitoring_plugin!=NULL, "monitoring_plugin now initialised");
48 ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" ); 48 ok( !strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised" );
49 49
50 ok( this_nagios_plugin->argc==argc, "Argc set" ); 50 ok( this_monitoring_plugin->argc==argc, "Argc set" );
51 ok( this_nagios_plugin->argv==argv, "Argv set" ); 51 ok( this_monitoring_plugin->argv==argv, "Argv set" );
52 52
53 np_set_args(0,0); 53 np_set_args(0,0);
54 54
55 ok( this_nagios_plugin->argc==0, "argc changed" ); 55 ok( this_monitoring_plugin->argc==0, "argc changed" );
56 ok( this_nagios_plugin->argv==0, "argv changed" ); 56 ok( this_monitoring_plugin->argv==0, "argv changed" );
57 57
58 np_set_args(argc, argv); 58 np_set_args(argc, argv);
59 59
@@ -182,6 +182,21 @@ main (int argc, char **argv)
182 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); 182 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
183 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); 183 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
184 184
185 rc = _set_thresholds(&thresholds, "-10:-2", "-30:20");
186 ok( rc == 0, "Thresholds ('-30:20', '-10:-2') set");
187 ok( thresholds->warning->start == -10, "Warning start set correctly");
188 ok( thresholds->warning->end == -2, "Warning end set correctly");
189 ok( thresholds->critical->start == -30, "Critical start set correctly");
190 ok( thresholds->critical->end == 20, "Critical end set correctly");
191 ok( get_status(-31, thresholds) == STATE_CRITICAL, "-31 - critical");
192 ok( get_status(-29, thresholds) == STATE_WARNING, "-29 - warning");
193 ok( get_status(-11, thresholds) == STATE_WARNING, "-11 - warning");
194 ok( get_status(-10, thresholds) == STATE_OK, "-10 - ok");
195 ok( get_status(-2, thresholds) == STATE_OK, "-2 - ok");
196 ok( get_status(-1, thresholds) == STATE_WARNING, "-1 - warning");
197 ok( get_status(19, thresholds) == STATE_WARNING, "19 - warning");
198 ok( get_status(21, thresholds) == STATE_CRITICAL, "21 - critical");
199
185 char *test; 200 char *test;
186 test = np_escaped_string("bob\\n"); 201 test = np_escaped_string("bob\\n");
187 ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); 202 ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
@@ -296,23 +311,23 @@ main (int argc, char **argv)
296 diag( "You are probably running in wrong directory. Must run as ./test_utils" ); 311 diag( "You are probably running in wrong directory. Must run as ./test_utils" );
297 312
298 313
299 this_nagios_plugin->argc=4; 314 this_monitoring_plugin->argc=4;
300 this_nagios_plugin->argv[0] = "./test_utils"; 315 this_monitoring_plugin->argv[0] = "./test_utils";
301 this_nagios_plugin->argv[1] = "here"; 316 this_monitoring_plugin->argv[1] = "here";
302 this_nagios_plugin->argv[2] = "--and"; 317 this_monitoring_plugin->argv[2] = "--and";
303 this_nagios_plugin->argv[3] = "now"; 318 this_monitoring_plugin->argv[3] = "now";
304 temp_string = (char *) _np_state_generate_key(); 319 temp_string = (char *) _np_state_generate_key();
305 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" ); 320 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
306 321
307 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 322 unsetenv("MP_STATE_PATH");
308 temp_string = (char *) _np_state_calculate_location_prefix(); 323 temp_string = (char *) _np_state_calculate_location_prefix();
309 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" ); 324 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
310 325
311 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1); 326 setenv("MP_STATE_PATH", "", 1);
312 temp_string = (char *) _np_state_calculate_location_prefix(); 327 temp_string = (char *) _np_state_calculate_location_prefix();
313 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" ); 328 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
314 329
315 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1); 330 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1);
316 temp_string = (char *) _np_state_calculate_location_prefix(); 331 temp_string = (char *) _np_state_calculate_location_prefix();
317 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" ); 332 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
318 333
@@ -320,16 +335,16 @@ main (int argc, char **argv)
320 335
321 ok(temp_state_key==NULL, "temp_state_key initially empty"); 336 ok(temp_state_key==NULL, "temp_state_key initially empty");
322 337
323 this_nagios_plugin->argc=1; 338 this_monitoring_plugin->argc=1;
324 this_nagios_plugin->argv[0] = "./test_utils"; 339 this_monitoring_plugin->argv[0] = "./test_utils";
325 np_enable_state(NULL, 51); 340 np_enable_state(NULL, 51);
326 temp_state_key = this_nagios_plugin->state; 341 temp_state_key = this_monitoring_plugin->state;
327 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 342 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
328 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" ); 343 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
329 344
330 345
331 np_enable_state("allowedchars_in_keyname", 77); 346 np_enable_state("allowedchars_in_keyname", 77);
332 temp_state_key = this_nagios_plugin->state; 347 temp_state_key = this_monitoring_plugin->state;
333 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 348 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
334 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" ); 349 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
335 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" ); 350 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" );
@@ -338,12 +353,12 @@ main (int argc, char **argv)
338 /* Don't do this test just yet. Will die */ 353 /* Don't do this test just yet. Will die */
339 /* 354 /*
340 np_enable_state("bad^chars$in@here", 77); 355 np_enable_state("bad^chars$in@here", 77);
341 temp_state_key = this_nagios_plugin->state; 356 temp_state_key = this_monitoring_plugin->state;
342 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); 357 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
343 */ 358 */
344 359
345 np_enable_state("funnykeyname", 54); 360 np_enable_state("funnykeyname", 54);
346 temp_state_key = this_nagios_plugin->state; 361 temp_state_key = this_monitoring_plugin->state;
347 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 362 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
348 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 363 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
349 364
@@ -367,9 +382,9 @@ main (int argc, char **argv)
367 382
368 temp_state_key->_filename="var/statefile"; 383 temp_state_key->_filename="var/statefile";
369 temp_state_data = np_state_read(); 384 temp_state_data = np_state_read();
370 ok( this_nagios_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not"); 385 ok( this_monitoring_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not");
371 ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" ); 386 ok( this_monitoring_plugin->state->state_data->time==1234567890, "Got time" );
372 ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); 387 ok( !strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected" );
373 388
374 temp_state_key->data_version=53; 389 temp_state_key->data_version=53;
375 temp_state_data = np_state_read(); 390 temp_state_data = np_state_read();
@@ -379,7 +394,7 @@ main (int argc, char **argv)
379 temp_state_key->_filename="var/nonexistant"; 394 temp_state_key->_filename="var/nonexistant";
380 temp_state_data = np_state_read(); 395 temp_state_data = np_state_read();
381 ok( temp_state_data==NULL, "Missing file gives NULL" ); 396 ok( temp_state_data==NULL, "Missing file gives NULL" );
382 ok( this_nagios_plugin->state->state_data==NULL, "No state information" ); 397 ok( this_monitoring_plugin->state->state_data==NULL, "No state information" );
383 398
384 temp_state_key->_filename="var/oldformat"; 399 temp_state_key->_filename="var/oldformat";
385 temp_state_data = np_state_read(); 400 temp_state_data = np_state_read();
@@ -426,7 +441,7 @@ main (int argc, char **argv)
426 temp_state_data = np_state_read(); 441 temp_state_data = np_state_read();
427 /* Check time is set to current_time */ 442 /* Check time is set to current_time */
428 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); 443 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
429 ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); 444 ok(this_monitoring_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
430 445
431 446
432 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ 447 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
@@ -438,7 +453,54 @@ main (int argc, char **argv)
438 453
439 np_cleanup(); 454 np_cleanup();
440 455
441 ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" ); 456 ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin");
457
458 ok(mp_suid() == FALSE, "Test aren't suid");
459
460 /* base states with random case */
461 char *states[] = {
462 "Ok",
463 "wArnINg",
464 "cRiTIcaL",
465 "UnKNoWN",
466 NULL
467 };
468
469 for (i=0; states[i]!=NULL; i++) {
470 /* out of the random case states, create the lower and upper versions + numeric string one */
471 char *statelower = strdup(states[i]);
472 char *stateupper = strdup(states[i]);
473 char statenum[2];
474 char *temp_ptr;
475 for (temp_ptr = statelower; *temp_ptr; temp_ptr++) {
476 *temp_ptr = tolower(*temp_ptr);
477 }
478 for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) {
479 *temp_ptr = toupper(*temp_ptr);
480 }
481 snprintf(statenum, 2, "%i", i);
482
483 /* Base test names, we'll append the state string */
484 char testname[64] = "Translate state string: ";
485 int tlen = strlen(testname);
486
487 strcpy(testname+tlen, states[i]);
488 ok(i==mp_translate_state(states[i]), testname);
489
490 strcpy(testname+tlen, statelower);
491 ok(i==mp_translate_state(statelower), testname);
492
493 strcpy(testname+tlen, stateupper);
494 ok(i==mp_translate_state(stateupper), testname);
495
496 strcpy(testname+tlen, statenum);
497 ok(i==mp_translate_state(statenum), testname);
498 }
499 ok(ERROR==mp_translate_state("warningfewgw"), "Translate state string with garbage");
500 ok(ERROR==mp_translate_state("00"), "Translate state string: bad numeric string 1");
501 ok(ERROR==mp_translate_state("01"), "Translate state string: bad numeric string 2");
502 ok(ERROR==mp_translate_state("10"), "Translate state string: bad numeric string 3");
503 ok(ERROR==mp_translate_state(""), "Translate state string: empty string");
442 504
443 return exit_status(); 505 return exit_status();
444} 506}
diff --git a/lib/tests/test_utils.t b/lib/tests/test_utils.t
index 152eb71..e2a9311 100755
--- a/lib/tests/test_utils.t
+++ b/lib/tests/test_utils.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_utils") { 3if (! -e "./test_utils") {
4 plan skip_all => "./test_utils not compiled - please install tap library to test"; 4 plan skip_all => "./test_utils not compiled - please enable libtap library to test";
5} 5}
6exec "./test_utils"; 6exec "./test_utils";
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 54463e9..04c4b4f 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*
@@ -30,56 +30,58 @@
30#include <ctype.h> 30#include <ctype.h>
31#include <fcntl.h> 31#include <fcntl.h>
32#include <sys/stat.h> 32#include <sys/stat.h>
33#include <unistd.h>
34#include <sys/types.h>
33 35
34#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } 36#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
35 37
36nagios_plugin *this_nagios_plugin=NULL; 38monitoring_plugin *this_monitoring_plugin=NULL;
37 39
38int _np_state_read_file(FILE *); 40int _np_state_read_file(FILE *);
39 41
40void np_init( char *plugin_name, int argc, char **argv ) { 42void np_init( char *plugin_name, int argc, char **argv ) {
41 if (this_nagios_plugin==NULL) { 43 if (this_monitoring_plugin==NULL) {
42 this_nagios_plugin = calloc(1, sizeof(nagios_plugin)); 44 this_monitoring_plugin = calloc(1, sizeof(monitoring_plugin));
43 if (this_nagios_plugin==NULL) { 45 if (this_monitoring_plugin==NULL) {
44 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 46 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
45 strerror(errno)); 47 strerror(errno));
46 } 48 }
47 this_nagios_plugin->plugin_name = strdup(plugin_name); 49 this_monitoring_plugin->plugin_name = strdup(plugin_name);
48 if (this_nagios_plugin->plugin_name==NULL) 50 if (this_monitoring_plugin->plugin_name==NULL)
49 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); 51 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
50 this_nagios_plugin->argc = argc; 52 this_monitoring_plugin->argc = argc;
51 this_nagios_plugin->argv = argv; 53 this_monitoring_plugin->argv = argv;
52 } 54 }
53} 55}
54 56
55void np_set_args( int argc, char **argv ) { 57void np_set_args( int argc, char **argv ) {
56 if (this_nagios_plugin==NULL) 58 if (this_monitoring_plugin==NULL)
57 die(STATE_UNKNOWN, _("This requires np_init to be called")); 59 die(STATE_UNKNOWN, _("This requires np_init to be called"));
58 60
59 this_nagios_plugin->argc = argc; 61 this_monitoring_plugin->argc = argc;
60 this_nagios_plugin->argv = argv; 62 this_monitoring_plugin->argv = argv;
61} 63}
62 64
63 65
64void np_cleanup() { 66void np_cleanup() {
65 if (this_nagios_plugin!=NULL) { 67 if (this_monitoring_plugin!=NULL) {
66 if(this_nagios_plugin->state!=NULL) { 68 if(this_monitoring_plugin->state!=NULL) {
67 if(this_nagios_plugin->state->state_data) { 69 if(this_monitoring_plugin->state->state_data) {
68 np_free(this_nagios_plugin->state->state_data->data); 70 np_free(this_monitoring_plugin->state->state_data->data);
69 np_free(this_nagios_plugin->state->state_data); 71 np_free(this_monitoring_plugin->state->state_data);
70 } 72 }
71 np_free(this_nagios_plugin->state->name); 73 np_free(this_monitoring_plugin->state->name);
72 np_free(this_nagios_plugin->state); 74 np_free(this_monitoring_plugin->state);
73 } 75 }
74 np_free(this_nagios_plugin->plugin_name); 76 np_free(this_monitoring_plugin->plugin_name);
75 np_free(this_nagios_plugin); 77 np_free(this_monitoring_plugin);
76 } 78 }
77 this_nagios_plugin=NULL; 79 this_monitoring_plugin=NULL;
78} 80}
79 81
80/* Hidden function to get a pointer to this_nagios_plugin for testing */ 82/* Hidden function to get a pointer to this_monitoring_plugin for testing */
81void _get_nagios_plugin( nagios_plugin **pointer ){ 83void _get_monitoring_plugin( monitoring_plugin **pointer ){
82 *pointer = this_nagios_plugin; 84 *pointer = this_monitoring_plugin;
83} 85}
84 86
85void 87void
@@ -89,7 +91,7 @@ die (int result, const char *fmt, ...)
89 va_start (ap, fmt); 91 va_start (ap, fmt);
90 vprintf (fmt, ap); 92 vprintf (fmt, ap);
91 va_end (ap); 93 va_end (ap);
92 if(this_nagios_plugin!=NULL) { 94 if(this_monitoring_plugin!=NULL) {
93 np_cleanup(); 95 np_cleanup();
94 } 96 }
95 exit (result); 97 exit (result);
@@ -367,6 +369,23 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
367 return value; 369 return value;
368} 370}
369 371
372
373/*
374 * Read a string representing a state (ok, warning... or numeric: 0, 1) and
375 * return the corresponding STATE_ value or ERROR)
376 */
377int mp_translate_state (char *state_text) {
378 if (!strcasecmp(state_text,"OK") || !strcmp(state_text,"0"))
379 return STATE_OK;
380 if (!strcasecmp(state_text,"WARNING") || !strcmp(state_text,"1"))
381 return STATE_WARNING;
382 if (!strcasecmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
383 return STATE_CRITICAL;
384 if (!strcasecmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
385 return STATE_UNKNOWN;
386 return ERROR;
387}
388
370/* 389/*
371 * Returns a string to use as a keyname, based on an md5 hash of argv, thus 390 * Returns a string to use as a keyname, based on an md5 hash of argv, thus
372 * hopefully a unique key per service/plugin invocation. Use the extra-opts 391 * hopefully a unique key per service/plugin invocation. Use the extra-opts
@@ -375,14 +394,14 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
375char *_np_state_generate_key() { 394char *_np_state_generate_key() {
376 struct sha1_ctx ctx; 395 struct sha1_ctx ctx;
377 int i; 396 int i;
378 char **argv = this_nagios_plugin->argv; 397 char **argv = this_monitoring_plugin->argv;
379 unsigned char result[20]; 398 unsigned char result[20];
380 char keyname[41]; 399 char keyname[41];
381 char *p=NULL; 400 char *p=NULL;
382 401
383 sha1_init_ctx(&ctx); 402 sha1_init_ctx(&ctx);
384 403
385 for(i=0; i<this_nagios_plugin->argc; i++) { 404 for(i=0; i<this_monitoring_plugin->argc; i++) {
386 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx); 405 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx);
387 } 406 }
388 407
@@ -401,9 +420,9 @@ char *_np_state_generate_key() {
401} 420}
402 421
403void _cleanup_state_data() { 422void _cleanup_state_data() {
404 if (this_nagios_plugin->state->state_data!=NULL) { 423 if (this_monitoring_plugin->state->state_data!=NULL) {
405 np_free(this_nagios_plugin->state->state_data->data); 424 np_free(this_monitoring_plugin->state->state_data->data);
406 np_free(this_nagios_plugin->state->state_data); 425 np_free(this_monitoring_plugin->state->state_data);
407 } 426 }
408} 427}
409 428
@@ -415,9 +434,18 @@ void _cleanup_state_data() {
415char* _np_state_calculate_location_prefix(){ 434char* _np_state_calculate_location_prefix(){
416 char *env_dir; 435 char *env_dir;
417 436
418 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 437 /* Do not allow passing MP_STATE_PATH in setuid plugins
419 if(env_dir && env_dir[0] != '\0') 438 * for security reasons */
420 return env_dir; 439 if (mp_suid() == FALSE) {
440 env_dir = getenv("MP_STATE_PATH");
441 if(env_dir && env_dir[0] != '\0')
442 return env_dir;
443 /* This is the former ENV, for backward-compatibility */
444 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
445 if(env_dir && env_dir[0] != '\0')
446 return env_dir;
447 }
448
421 return NP_STATE_DIR_PREFIX; 449 return NP_STATE_DIR_PREFIX;
422} 450}
423 451
@@ -432,7 +460,7 @@ void np_enable_state(char *keyname, int expected_data_version) {
432 char *temp_keyname = NULL; 460 char *temp_keyname = NULL;
433 char *p=NULL; 461 char *p=NULL;
434 462
435 if(this_nagios_plugin==NULL) 463 if(this_monitoring_plugin==NULL)
436 die(STATE_UNKNOWN, _("This requires np_init to be called")); 464 die(STATE_UNKNOWN, _("This requires np_init to be called"));
437 465
438 this_state = (state_key *) calloc(1, sizeof(state_key)); 466 this_state = (state_key *) calloc(1, sizeof(state_key));
@@ -456,15 +484,15 @@ void np_enable_state(char *keyname, int expected_data_version) {
456 p++; 484 p++;
457 } 485 }
458 this_state->name=temp_keyname; 486 this_state->name=temp_keyname;
459 this_state->plugin_name=this_nagios_plugin->plugin_name; 487 this_state->plugin_name=this_monitoring_plugin->plugin_name;
460 this_state->data_version=expected_data_version; 488 this_state->data_version=expected_data_version;
461 this_state->state_data=NULL; 489 this_state->state_data=NULL;
462 490
463 /* Calculate filename */ 491 /* Calculate filename */
464 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name); 492 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; 493 this_state->_filename=temp_filename;
466 494
467 this_nagios_plugin->state = this_state; 495 this_monitoring_plugin->state = this_state;
468} 496}
469 497
470/* 498/*
@@ -479,11 +507,11 @@ state_data *np_state_read() {
479 FILE *statefile; 507 FILE *statefile;
480 int rc = FALSE; 508 int rc = FALSE;
481 509
482 if(this_nagios_plugin==NULL) 510 if(this_monitoring_plugin==NULL)
483 die(STATE_UNKNOWN, _("This requires np_init to be called")); 511 die(STATE_UNKNOWN, _("This requires np_init to be called"));
484 512
485 /* Open file. If this fails, no previous state found */ 513 /* Open file. If this fails, no previous state found */
486 statefile = fopen( this_nagios_plugin->state->_filename, "r" ); 514 statefile = fopen( this_monitoring_plugin->state->_filename, "r" );
487 if(statefile!=NULL) { 515 if(statefile!=NULL) {
488 516
489 this_state_data = (state_data *) calloc(1, sizeof(state_data)); 517 this_state_data = (state_data *) calloc(1, sizeof(state_data));
@@ -492,7 +520,7 @@ state_data *np_state_read() {
492 strerror(errno)); 520 strerror(errno));
493 521
494 this_state_data->data=NULL; 522 this_state_data->data=NULL;
495 this_nagios_plugin->state->state_data = this_state_data; 523 this_monitoring_plugin->state->state_data = this_state_data;
496 524
497 rc = _np_state_read_file(statefile); 525 rc = _np_state_read_file(statefile);
498 526
@@ -503,10 +531,10 @@ state_data *np_state_read() {
503 _cleanup_state_data(); 531 _cleanup_state_data();
504 } 532 }
505 533
506 return this_nagios_plugin->state->state_data; 534 return this_monitoring_plugin->state->state_data;
507} 535}
508 536
509/* 537/*
510 * Read the state file 538 * Read the state file
511 */ 539 */
512int _np_state_read_file(FILE *f) { 540int _np_state_read_file(FILE *f) {
@@ -544,7 +572,7 @@ int _np_state_read_file(FILE *f) {
544 break; 572 break;
545 case STATE_DATA_VERSION: 573 case STATE_DATA_VERSION:
546 i=atoi(line); 574 i=atoi(line);
547 if(i != this_nagios_plugin->state->data_version) 575 if(i != this_monitoring_plugin->state->data_version)
548 failure++; 576 failure++;
549 else 577 else
550 expected=STATE_DATA_TIME; 578 expected=STATE_DATA_TIME;
@@ -555,13 +583,13 @@ int _np_state_read_file(FILE *f) {
555 if(data_time > current_time) 583 if(data_time > current_time)
556 failure++; 584 failure++;
557 else { 585 else {
558 this_nagios_plugin->state->state_data->time = data_time; 586 this_monitoring_plugin->state->state_data->time = data_time;
559 expected=STATE_DATA_TEXT; 587 expected=STATE_DATA_TEXT;
560 } 588 }
561 break; 589 break;
562 case STATE_DATA_TEXT: 590 case STATE_DATA_TEXT:
563 this_nagios_plugin->state->state_data->data = strdup(line); 591 this_monitoring_plugin->state->state_data->data = strdup(line);
564 if(this_nagios_plugin->state->state_data->data==NULL) 592 if(this_monitoring_plugin->state->state_data->data==NULL)
565 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); 593 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
566 expected=STATE_DATA_END; 594 expected=STATE_DATA_END;
567 status=TRUE; 595 status=TRUE;
@@ -596,8 +624,8 @@ void np_state_write_string(time_t data_time, char *data_string) {
596 current_time=data_time; 624 current_time=data_time;
597 625
598 /* If file doesn't currently exist, create directories */ 626 /* If file doesn't currently exist, create directories */
599 if(access(this_nagios_plugin->state->_filename,F_OK)!=0) { 627 if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) {
600 asprintf(&directories, "%s", this_nagios_plugin->state->_filename); 628 asprintf(&directories, "%s", this_monitoring_plugin->state->_filename);
601 if(directories==NULL) 629 if(directories==NULL)
602 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 630 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
603 strerror(errno)); 631 strerror(errno));
@@ -607,7 +635,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
607 *p='\0'; 635 *p='\0';
608 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) { 636 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
609 /* Can't free this! Otherwise error message is wrong! */ 637 /* Can't free this! Otherwise error message is wrong! */
610 /* np_free(directories); */ 638 /* np_free(directories); */
611 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); 639 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories);
612 } 640 }
613 *p='/'; 641 *p='/';
@@ -616,7 +644,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
616 np_free(directories); 644 np_free(directories);
617 } 645 }
618 646
619 asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename); 647 asprintf(&temp_file,"%s.XXXXXX",this_monitoring_plugin->state->_filename);
620 if(temp_file==NULL) 648 if(temp_file==NULL)
621 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 649 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
622 strerror(errno)); 650 strerror(errno));
@@ -636,7 +664,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
636 664
637 fprintf(fp,"# NP State file\n"); 665 fprintf(fp,"# NP State file\n");
638 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); 666 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION);
639 fprintf(fp,"%d\n",this_nagios_plugin->state->data_version); 667 fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version);
640 fprintf(fp,"%lu\n",current_time); 668 fprintf(fp,"%lu\n",current_time);
641 fprintf(fp,"%s\n",data_string); 669 fprintf(fp,"%s\n",data_string);
642 670
@@ -654,7 +682,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
654 die(STATE_UNKNOWN, _("Error writing temp file")); 682 die(STATE_UNKNOWN, _("Error writing temp file"));
655 } 683 }
656 684
657 if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) { 685 if(rename(temp_file, this_monitoring_plugin->state->_filename)!=0) {
658 unlink(temp_file); 686 unlink(temp_file);
659 np_free(temp_file); 687 np_free(temp_file);
660 die(STATE_UNKNOWN, _("Cannot rename state temp file")); 688 die(STATE_UNKNOWN, _("Cannot rename state temp file"));
diff --git a/lib/utils_base.h b/lib/utils_base.h
index 0cb371f..d69b0da 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -1,6 +1,6 @@
1#ifndef _UTILS_BASE_ 1#ifndef _UTILS_BASE_
2#define _UTILS_BASE_ 2#define _UTILS_BASE_
3/* Header file for nagios plugins utils_base.c */ 3/* Header file for Monitoring Plugins utils_base.c */
4 4
5#include "sha1.h" 5#include "sha1.h"
6 6
@@ -52,7 +52,7 @@ typedef struct np_struct {
52 state_key *state; 52 state_key *state;
53 int argc; 53 int argc;
54 char **argv; 54 char **argv;
55 } nagios_plugin; 55 } monitoring_plugin;
56 56
57range *parse_range_string (char *); 57range *parse_range_string (char *);
58int _set_thresholds(thresholds **, char *, char *); 58int _set_thresholds(thresholds **, char *, char *);
@@ -62,7 +62,7 @@ int check_range(double, range *);
62int get_status(double, thresholds *); 62int get_status(double, thresholds *);
63 63
64/* All possible characters in a threshold range */ 64/* All possible characters in a threshold range */
65#define NP_THRESHOLDS_CHARS "0123456789.:@~" 65#define NP_THRESHOLDS_CHARS "-0123456789.:@~"
66 66
67char *np_escaped_string (const char *); 67char *np_escaped_string (const char *);
68 68
@@ -79,6 +79,10 @@ int np_check_if_root(void);
79 * code from the above function, in case it's helpful for testing */ 79 * code from the above function, in case it's helpful for testing */
80int np_warn_if_not_root(void); 80int np_warn_if_not_root(void);
81 81
82/* mp_suid() returns true if the real and effective uids differs, such as when
83 * running a suid plugin */
84#define mp_suid() (getuid() != geteuid())
85
82/* 86/*
83 * Extract the value from key/value pairs, or return NULL. The value returned 87 * Extract the value from key/value pairs, or return NULL. The value returned
84 * can be free()ed. 88 * can be free()ed.
@@ -93,6 +97,11 @@ char *np_extract_value(const char*, const char*, char);
93 */ 97 */
94#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',') 98#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
95 99
100/*
101 * Read a string representing a state (ok, warning... or numeric: 0, 1) and
102 * return the corresponding NP_STATE or ERROR)
103 */
104int mp_translate_state (char *);
96 105
97void np_enable_state(char *, int); 106void np_enable_state(char *, int);
98state_data *np_state_read(); 107state_data *np_state_read();
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c
index 0c853dc..4c6d0be 100644
--- a/lib/utils_cmd.c
+++ b/lib/utils_cmd.c
@@ -1,9 +1,9 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Nagios run command utilities 3* Monitoring run command utilities
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2005-2006 Nagios Plugins Development Team 6* Copyright (c) 2005-2006 Monitoring Plugins Development Team
7* 7*
8* Description : 8* Description :
9* 9*
diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h
index 8ebb589..ebaf15b 100644
--- a/lib/utils_cmd.h
+++ b/lib/utils_cmd.h
@@ -2,7 +2,7 @@
2#define _UTILS_CMD_ 2#define _UTILS_CMD_
3 3
4/* 4/*
5 * Header file for nagios plugins utils_cmd.c 5 * Header file for Monitoring Plugins utils_cmd.c
6 * 6 *
7 * 7 *
8 */ 8 */
diff --git a/lib/utils_disk.c b/lib/utils_disk.c
index 5be2b2c..efe35fc 100644
--- a/lib/utils_disk.c
+++ b/lib/utils_disk.c
@@ -3,7 +3,7 @@
3* Library for check_disk 3* Library for check_disk
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 1999-2007 Nagios Plugins Development Team 6* Copyright (c) 1999-2007 Monitoring Plugins Development Team
7* 7*
8* Description: 8* Description:
9* 9*
diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c
index 46ad7f7..b37c446 100644
--- a/lib/utils_tcp.c
+++ b/lib/utils_tcp.c
@@ -3,7 +3,7 @@
3* Library for check_tcp 3* Library for check_tcp
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 1999-2013 Nagios Plugins Development Team 6* Copyright (c) 1999-2013 Monitoring Plugins Development Team
7* 7*
8* Description: 8* Description:
9* 9*