From 833fd7ca04ae1e746db1feafae3bae1bbf4a10c3 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Mon, 21 Jun 2010 15:19:50 +0100 Subject: Added sha1 from gnulib. Also filled out _np_state_generate_key() --- lib/tests/test_utils.c | 19 +++++++++++++++---- lib/utils_base.c | 25 +++++++++++++++++++++++-- lib/utils_base.h | 6 ++++-- 3 files changed, 42 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index ffab7486..ccc4a52c 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c @@ -39,12 +39,13 @@ main (int argc, char **argv) nagios_plugin *temp_nagios_plugin; FILE *temp_fp; - plan_tests(134); + plan_tests(138); _get_nagios_plugin( &temp_nagios_plugin ); ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised"); - np_init( "check_test" ); + np_init( "check_test", argc, argv ); + _get_nagios_plugin( &temp_nagios_plugin ); ok( temp_nagios_plugin!=NULL, "nagios_plugin now initialised"); ok( !strcmp(temp_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" ); @@ -271,10 +272,18 @@ main (int argc, char **argv) ok(!test, "Empty string return NULL"); + /* This is the result of running ./test_utils */ temp_string = (char *) _np_state_generate_key(); - ok(!strcmp(temp_string, "Ahash"), "Got hash" ); + ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" ); + temp_nagios_plugin->argc=4; + temp_nagios_plugin->argv[0] = "./test_utils"; + temp_nagios_plugin->argv[1] = "here"; + temp_nagios_plugin->argv[2] = "--and"; + temp_nagios_plugin->argv[3] = "now"; + temp_string = (char *) _np_state_generate_key(); + ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" ); unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); temp_string = (char *) _np_state_calculate_location_prefix(); @@ -292,10 +301,12 @@ main (int argc, char **argv) ok(temp_state_key==NULL, "temp_state_key initially empty"); + temp_nagios_plugin->argc=1; + temp_nagios_plugin->argv[0] = "./test_utils"; np_enable_state(NULL, 51); temp_state_key = temp_nagios_plugin->state; ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); - ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); + ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" ); np_enable_state("bad^chars$in@here", 77); diff --git a/lib/utils_base.c b/lib/utils_base.c index b86ba737..cfff7cb9 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -33,7 +33,7 @@ nagios_plugin *this_nagios_plugin=NULL; -void np_init( char *plugin_name ) { +void np_init( char *plugin_name, int argc, char **argv ) { if (this_nagios_plugin==NULL) { this_nagios_plugin = malloc(sizeof(nagios_plugin)); if (this_nagios_plugin==NULL) { @@ -41,6 +41,8 @@ void np_init( char *plugin_name ) { strerror(errno)); } this_nagios_plugin->plugin_name = strdup(plugin_name); + this_nagios_plugin->argc = argc; + this_nagios_plugin->argv = argv; } } @@ -354,7 +356,26 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { * parse of argv, so that uniqueness in parameters are reflected there. */ char *_np_state_generate_key() { - return strdup("Ahash"); + struct sha1_ctx ctx; + int i; + char **argv = this_nagios_plugin->argv; + unsigned char result[20]; + char keyname[41]; + + sha1_init_ctx(&ctx); + + for(i=0; iargc; i++) { + sha1_process_bytes(argv[i], strlen(argv[i]), &ctx); + } + + sha1_finish_ctx(&ctx, &result); + + for (i=0; i<20; ++i) { + sprintf(&keyname[2*i], "%02x", result[i]); + } + keyname[40]='\0'; + + return strdup(keyname); } void _cleanup_state_data() { diff --git a/lib/utils_base.h b/lib/utils_base.h index 8f2f6649..0037b7b5 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -2,6 +2,7 @@ #define _UTILS_BASE_ /* Header file for nagios plugins utils_base.c */ +#include "sha1.h" /* This file holds header information for thresholds - use this in preference to individual plugin logic */ @@ -50,7 +51,8 @@ typedef struct state_key_struct { typedef struct np_struct { char *plugin_name; state_key *state; - char **expanded_argv; + int argc; + char **argv; } nagios_plugin; range *parse_range_string (char *); @@ -97,7 +99,7 @@ void np_enable_state(char *, int); state_data *np_state_read(); void np_state_write_string(time_t, char *); -void np_init(char *); +void np_init(char *, int argc, char **argv); void np_cleanup(); #endif /* _UTILS_BASE_ */ -- cgit v1.2.3-74-g34f1