summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/tests/test_utils.c19
-rw-r--r--lib/utils_base.c25
-rw-r--r--lib/utils_base.h6
3 files changed, 42 insertions, 8 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index ffab748..ccc4a52 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -39,12 +39,13 @@ main (int argc, char **argv)
39 nagios_plugin *temp_nagios_plugin; 39 nagios_plugin *temp_nagios_plugin;
40 FILE *temp_fp; 40 FILE *temp_fp;
41 41
42 plan_tests(134); 42 plan_tests(138);
43 43
44 _get_nagios_plugin( &temp_nagios_plugin ); 44 _get_nagios_plugin( &temp_nagios_plugin );
45 ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised"); 45 ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised");
46 46
47 np_init( "check_test" ); 47 np_init( "check_test", argc, argv );
48
48 _get_nagios_plugin( &temp_nagios_plugin ); 49 _get_nagios_plugin( &temp_nagios_plugin );
49 ok( temp_nagios_plugin!=NULL, "nagios_plugin now initialised"); 50 ok( temp_nagios_plugin!=NULL, "nagios_plugin now initialised");
50 ok( !strcmp(temp_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" ); 51 ok( !strcmp(temp_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
@@ -271,10 +272,18 @@ main (int argc, char **argv)
271 ok(!test, "Empty string return NULL"); 272 ok(!test, "Empty string return NULL");
272 273
273 274
275 /* This is the result of running ./test_utils */
274 temp_string = (char *) _np_state_generate_key(); 276 temp_string = (char *) _np_state_generate_key();
275 ok(!strcmp(temp_string, "Ahash"), "Got hash" ); 277 ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" );
276 278
277 279
280 temp_nagios_plugin->argc=4;
281 temp_nagios_plugin->argv[0] = "./test_utils";
282 temp_nagios_plugin->argv[1] = "here";
283 temp_nagios_plugin->argv[2] = "--and";
284 temp_nagios_plugin->argv[3] = "now";
285 temp_string = (char *) _np_state_generate_key();
286 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
278 287
279 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 288 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
280 temp_string = (char *) _np_state_calculate_location_prefix(); 289 temp_string = (char *) _np_state_calculate_location_prefix();
@@ -292,10 +301,12 @@ main (int argc, char **argv)
292 301
293 ok(temp_state_key==NULL, "temp_state_key initially empty"); 302 ok(temp_state_key==NULL, "temp_state_key initially empty");
294 303
304 temp_nagios_plugin->argc=1;
305 temp_nagios_plugin->argv[0] = "./test_utils";
295 np_enable_state(NULL, 51); 306 np_enable_state(NULL, 51);
296 temp_state_key = temp_nagios_plugin->state; 307 temp_state_key = temp_nagios_plugin->state;
297 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 308 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
298 ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); 309 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
299 310
300 311
301 np_enable_state("bad^chars$in@here", 77); 312 np_enable_state("bad^chars$in@here", 77);
diff --git a/lib/utils_base.c b/lib/utils_base.c
index b86ba73..cfff7cb 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -33,7 +33,7 @@
33 33
34nagios_plugin *this_nagios_plugin=NULL; 34nagios_plugin *this_nagios_plugin=NULL;
35 35
36void np_init( char *plugin_name ) { 36void np_init( char *plugin_name, int argc, char **argv ) {
37 if (this_nagios_plugin==NULL) { 37 if (this_nagios_plugin==NULL) {
38 this_nagios_plugin = malloc(sizeof(nagios_plugin)); 38 this_nagios_plugin = malloc(sizeof(nagios_plugin));
39 if (this_nagios_plugin==NULL) { 39 if (this_nagios_plugin==NULL) {
@@ -41,6 +41,8 @@ void np_init( char *plugin_name ) {
41 strerror(errno)); 41 strerror(errno));
42 } 42 }
43 this_nagios_plugin->plugin_name = strdup(plugin_name); 43 this_nagios_plugin->plugin_name = strdup(plugin_name);
44 this_nagios_plugin->argc = argc;
45 this_nagios_plugin->argv = argv;
44 } 46 }
45} 47}
46 48
@@ -354,7 +356,26 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
354 * parse of argv, so that uniqueness in parameters are reflected there. 356 * parse of argv, so that uniqueness in parameters are reflected there.
355 */ 357 */
356char *_np_state_generate_key() { 358char *_np_state_generate_key() {
357 return strdup("Ahash"); 359 struct sha1_ctx ctx;
360 int i;
361 char **argv = this_nagios_plugin->argv;
362 unsigned char result[20];
363 char keyname[41];
364
365 sha1_init_ctx(&ctx);
366
367 for(i=0; i<this_nagios_plugin->argc; i++) {
368 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx);
369 }
370
371 sha1_finish_ctx(&ctx, &result);
372
373 for (i=0; i<20; ++i) {
374 sprintf(&keyname[2*i], "%02x", result[i]);
375 }
376 keyname[40]='\0';
377
378 return strdup(keyname);
358} 379}
359 380
360void _cleanup_state_data() { 381void _cleanup_state_data() {
diff --git a/lib/utils_base.h b/lib/utils_base.h
index 8f2f664..0037b7b 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -2,6 +2,7 @@
2#define _UTILS_BASE_ 2#define _UTILS_BASE_
3/* Header file for nagios plugins utils_base.c */ 3/* Header file for nagios plugins utils_base.c */
4 4
5#include "sha1.h"
5 6
6/* This file holds header information for thresholds - use this in preference to 7/* This file holds header information for thresholds - use this in preference to
7 individual plugin logic */ 8 individual plugin logic */
@@ -50,7 +51,8 @@ typedef struct state_key_struct {
50typedef struct np_struct { 51typedef struct np_struct {
51 char *plugin_name; 52 char *plugin_name;
52 state_key *state; 53 state_key *state;
53 char **expanded_argv; 54 int argc;
55 char **argv;
54 } nagios_plugin; 56 } nagios_plugin;
55 57
56range *parse_range_string (char *); 58range *parse_range_string (char *);
@@ -97,7 +99,7 @@ void np_enable_state(char *, int);
97state_data *np_state_read(); 99state_data *np_state_read();
98void np_state_write_string(time_t, char *); 100void np_state_write_string(time_t, char *);
99 101
100void np_init(char *); 102void np_init(char *, int argc, char **argv);
101void np_cleanup(); 103void np_cleanup();
102 104
103#endif /* _UTILS_BASE_ */ 105#endif /* _UTILS_BASE_ */