/* extract check result */ char *extract_check_result(FILE *fp, int trimmed) { int size; char buffer[GM_BUFFERSIZE] = ""; char output[GM_BUFFERSIZE] = ""; /* get all lines of plugin output - escape newlines */ strcpy(buffer,""); strcpy(output,""); size = GM_MAX_OUTPUT; while(size > 0 && fgets(buffer,sizeof(buffer)-1,fp)){ strncat(output, buffer, size); size -= strlen(buffer); } return(gm_escape_newlines(output, trimmed)); }
/* send message to job server */ int send_result() { char * buf; char temp_buffer1[GM_BUFFERSIZE]; char temp_buffer2[GM_BUFFERSIZE]; gm_log( GM_LOG_TRACE, "send_result()\n" ); if(mod_gm_opt->result_queue == NULL) { printf( "got no result queue, please use --result_queue=...\n" ); return(GM_ERROR); } /* escape newline */ buf = gm_escape_newlines(mod_gm_opt->message, GM_DISABLED); free(mod_gm_opt->message); mod_gm_opt->message = gm_malloc(GM_BUFFERSIZE); snprintf(mod_gm_opt->message, GM_BUFFERSIZE, "%s", buf); free(buf); gm_log( GM_LOG_TRACE, "queue: %s\n", mod_gm_opt->result_queue ); temp_buffer1[0]='\x0'; snprintf( temp_buffer1, sizeof( temp_buffer1 )-1, "type=%s\nhost_name=%s\nstart_time=%i.%i\nfinish_time=%i.%i\nreturn_code=%i\nsource=send_multi\n", mod_gm_opt->active == GM_ENABLED ? "active" : "passive", mod_gm_opt->host, (int)mod_gm_opt->starttime.tv_sec, (int)mod_gm_opt->starttime.tv_usec, (int)mod_gm_opt->finishtime.tv_sec, (int)mod_gm_opt->finishtime.tv_usec, mod_gm_opt->return_code ); if(mod_gm_opt->service != NULL) { temp_buffer2[0]='\x0'; strncat(temp_buffer2, "service_description=", (sizeof(temp_buffer2)-1)); strncat(temp_buffer2, mod_gm_opt->service, (sizeof(temp_buffer2)-1)); strncat(temp_buffer2, "\n", (sizeof(temp_buffer2)-1)); strncat(temp_buffer1, temp_buffer2, (sizeof(temp_buffer1)-1)); } if(mod_gm_opt->message != NULL) { temp_buffer2[0]='\x0'; strncat(temp_buffer2, "output=", (sizeof(temp_buffer2)-1)); strncat(temp_buffer2, mod_gm_opt->message, (sizeof(temp_buffer2)-1)); strncat(temp_buffer2, "\n", (sizeof(temp_buffer2)-1)); strncat(temp_buffer1, temp_buffer2, (sizeof(temp_buffer1)-1)); } strncat(temp_buffer1, "\n", (sizeof(temp_buffer1)-2)); gm_log( GM_LOG_TRACE, "data:\n%s\n", temp_buffer1); if(add_job_to_queue( &client, mod_gm_opt->server_list, mod_gm_opt->result_queue, NULL, temp_buffer1, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { gm_log( GM_LOG_TRACE, "send_result_back() finished successfully\n" ); if( mod_gm_opt->dupserver_num ) { if(add_job_to_queue( &client, mod_gm_opt->dupserver_list, mod_gm_opt->result_queue, NULL, temp_buffer1, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { gm_log( GM_LOG_TRACE, "send_result_back() finished successfully for duplicate server.\n" ); } else { gm_log( GM_LOG_TRACE, "send_result_back() finished unsuccessfully for duplicate server\n" ); } } } else { gm_log( GM_LOG_TRACE, "send_result_back() finished unsuccessfully\n" ); return(GM_ERROR); } return(GM_OK); }
int main(void) { plan(60); /* lowercase */ char test[100]; ok(lc(NULL) == NULL, "lc(NULL)"); strcpy(test, "Yes"); like(lc(test), "yes", "lc(yes)"); strcpy(test, "YES"); like(lc(test), "yes", "lc(YES)"); strcpy(test, "yeS"); like(lc(test), "yes", "lc(yeS)"); /* trim */ strcpy(test, " text "); like(ltrim(test), "text ", "ltrim()"); strcpy(test, " text "); like(rtrim(test), " text", "rtrim()"); strcpy(test, " text "); like(trim(test), "text", "trim()"); char *test2; test2 = strdup(" text "); like(trim(test2), "text", "trim()"); free(test2); /* parse_yes_or_no */ ok(parse_yes_or_no(NULL, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 1"); ok(parse_yes_or_no(NULL, GM_DISABLED) == GM_DISABLED, "parse_yes_or_no 2"); strcpy(test, ""); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 3"); strcpy(test, ""); ok(parse_yes_or_no(test, GM_DISABLED) == GM_DISABLED, "parse_yes_or_no 4"); strcpy(test, "yes"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 5"); strcpy(test, "true"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 6"); strcpy(test, "Yes"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 7"); strcpy(test, "1"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 8"); strcpy(test, "On"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_ENABLED, "parse_yes_or_no 9"); strcpy(test, "Off"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_DISABLED, "parse_yes_or_no 10"); strcpy(test, "false"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_DISABLED, "parse_yes_or_no 11"); strcpy(test, "no"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_DISABLED, "parse_yes_or_no 12"); strcpy(test, "0"); ok(parse_yes_or_no(test, GM_ENABLED) == GM_DISABLED, "parse_yes_or_no 13"); /* trim */ ok(trim(NULL) == NULL, "trim(NULL)"); strcpy(test, " test "); like(trim(test), "^test$", "trim(' test ')"); strcpy(test, "\ntest\n"); like(trim(test), "^test$", "trim('\\ntest\\n')"); /* reading keys */ mod_gm_opt_t *mod_gm_opt; mod_gm_opt = malloc(sizeof(mod_gm_opt_t)); int rc = set_default_options(mod_gm_opt); ok(rc == 0, "setting default options"); mod_gm_opt->keyfile = strdup("t/data/test1.key"); read_keyfile(mod_gm_opt); //printf_hex(mod_gm_opt->crypt_key, 32); test[0]='\x0'; int i = 0; char hex[4]; for(i=0; i<32; i++) { hex[0] = '\x0'; snprintf(hex, 4, "%02x", mod_gm_opt->crypt_key[i]); strncat(test, hex, 4); } like(test, "3131313131313131313131313131313131313131313131313131313131310000", "read keyfile t/data/test1.key"); free(mod_gm_opt->keyfile); mod_gm_opt->keyfile = strdup("t/data/test2.key"); read_keyfile(mod_gm_opt); like(mod_gm_opt->crypt_key, "abcdef", "reading keyfile t/data/test2.key"); free(mod_gm_opt->keyfile); mod_gm_opt->keyfile = strdup("t/data/test3.key"); read_keyfile(mod_gm_opt); //printf_hex(mod_gm_opt->crypt_key, 32); like(mod_gm_opt->crypt_key, "11111111111111111111111111111111", "reading keyfile t/data/test3.key"); ok(strlen(mod_gm_opt->crypt_key) == 32, "key size for t/data/test3.key"); /* encrypt */ char * key = "test1234"; char * encrypted = malloc(GM_BUFFERSIZE); char * text = "test message"; char * base = "a7HqhQEE8TQBde9uknpPYQ=="; mod_gm_crypt_init(key); int len; len = mod_gm_encrypt(&encrypted, text, GM_ENCODE_AND_ENCRYPT); ok(len == 24, "length of encrypted only"); like(encrypted, base, "encrypted string"); /* decrypt */ char * decrypted = malloc(GM_BUFFERSIZE); mod_gm_decrypt(&decrypted, encrypted, GM_ENCODE_AND_ENCRYPT); like(decrypted, text, "decrypted text"); free(decrypted); free(encrypted); /* base 64 */ char * base64 = malloc(GM_BUFFERSIZE); len = mod_gm_encrypt(&base64, text, GM_ENCODE_ONLY); ok(len == 16, "length of encode only"); like(base64, "dGVzdCBtZXNzYWdl", "base64 only string"); /* debase 64 */ char * debase64 = malloc(GM_BUFFERSIZE); mod_gm_decrypt(&debase64, base64, GM_ENCODE_ONLY); like(debase64, text, "debase64 text"); free(debase64); free(base64); /* file_exists */ ok(file_exists("01_utils") == 1, "file_exists('01_utils')"); ok(file_exists("non-exist") == 0, "file_exists('non-exist')"); /* nr2signal */ char * signame1 = nr2signal(9); like(signame1, "SIGKILL", "get SIGKILL for 9"); free(signame1); char * signame2 = nr2signal(15); like(signame2, "SIGTERM", "get SIGTERM for 15"); free(signame2); /* string2timeval */ struct timeval t; string2timeval("100.50", &t); ok(t.tv_sec == 100, "string2timeval 1"); ok(t.tv_usec == 50, "string2timeval 2"); string2timeval("100", &t); ok(t.tv_sec == 100, "string2timeval 3"); ok(t.tv_usec == 0, "string2timeval 4"); string2timeval("", &t); ok(t.tv_sec == 0, "string2timeval 5"); ok(t.tv_usec == 0, "string2timeval 6"); string2timeval(NULL, &t); ok(t.tv_sec == 0, "string2timeval 7"); ok(t.tv_usec == 0, "string2timeval 8"); /* command line parsing */ mod_gm_free_opt(mod_gm_opt); mod_gm_opt = renew_opts(); strcpy(test, "server=host:4730"); parse_args_line(mod_gm_opt, test, 0); like(mod_gm_opt->server_list[0], "host:4730", "server=host:4730"); ok(mod_gm_opt->server_num == 1, "server_number = %d", mod_gm_opt->server_num); mod_gm_free_opt(mod_gm_opt); mod_gm_opt = renew_opts(); strcpy(test, "server=:4730"); parse_args_line(mod_gm_opt, test, 0); like(mod_gm_opt->server_list[0], "localhost:4730", "server=:4730"); ok(mod_gm_opt->server_num == 1, "server_number = %d", mod_gm_opt->server_num); mod_gm_free_opt(mod_gm_opt); mod_gm_opt = renew_opts(); strcpy(test, "server=localhost:4730"); parse_args_line(mod_gm_opt, test, 0); strcpy(test, "server=localhost:4730"); parse_args_line(mod_gm_opt, test, 0); like(mod_gm_opt->server_list[0], "localhost:4730", "duplicate server"); ok(mod_gm_opt->server_num == 1, "server_number = %d", mod_gm_opt->server_num); mod_gm_free_opt(mod_gm_opt); mod_gm_opt = renew_opts(); strcpy(test, "server=localhost:4730,localhost:4730,:4730,host:4730,"); parse_args_line(mod_gm_opt, test, 0); like(mod_gm_opt->server_list[0], "localhost:4730", "duplicate server"); like(mod_gm_opt->server_list[1], "host:4730", "duplicate server"); ok(mod_gm_opt->server_num == 2, "server_number = %d", mod_gm_opt->server_num); /* escape newlines */ char * escaped = gm_escape_newlines(" test\n", GM_DISABLED); is(escaped, " test\\n", "untrimmed escape string"); free(escaped); escaped = gm_escape_newlines(" test\n", GM_ENABLED); is(escaped, "test", "trimmed escape string"); free(escaped); /* md5 sum */ char * sum = NULL; strcpy(test, ""); sum = md5sum(test); like(sum, "d41d8cd98f00b204e9800998ecf8427e", "md5sum()"); free(sum); strcpy(test, "The quick brown fox jumps over the lazy dog."); sum = md5sum(test); like(sum, "e4d909c290d0fb1ca068ffaddf22cbd0", "md5sum()"); free(sum); mod_gm_free_opt(mod_gm_opt); return exit_status(); }
/* submit result */ int submit_result() { char * buf; char * temp_buffer; char * result; struct timeval now; struct timeval starttime; struct timeval finishtime; int resultsize; gettimeofday(&now, NULL); if(mod_gm_opt->has_starttime == FALSE) { starttime = now; } else { starttime = mod_gm_opt->starttime; } if(mod_gm_opt->has_finishtime == FALSE) { finishtime = now; } else { finishtime = mod_gm_opt->finishtime; } if(mod_gm_opt->has_latency == FALSE) { mod_gm_opt->latency.tv_sec = 0; mod_gm_opt->latency.tv_usec = 0; } /* escape newline */ buf = gm_escape_newlines(mod_gm_opt->message, GM_DISABLED); free(mod_gm_opt->message); mod_gm_opt->message = gm_strdup(buf); free(buf); gm_log( GM_LOG_TRACE, "queue: %s\n", mod_gm_opt->result_queue ); resultsize = sizeof(char) * strlen(mod_gm_opt->message) + GM_BUFFERSIZE; result = gm_malloc(resultsize); snprintf( result, resultsize-1, "type=%s\nhost_name=%s\nstart_time=%Lf\nfinish_time=%Lf\nlatency=%Lf\nreturn_code=%i\nsource=send_gearman\n", mod_gm_opt->active == GM_ENABLED ? "active" : "passive", mod_gm_opt->host, timeval2double(&starttime), timeval2double(&finishtime), timeval2double(&mod_gm_opt->latency), mod_gm_opt->return_code ); temp_buffer = gm_malloc(resultsize); if(mod_gm_opt->service != NULL) { temp_buffer[0]='\x0'; strcat(temp_buffer, "service_description="); strcat(temp_buffer, mod_gm_opt->service); strcat(temp_buffer, "\n"); strcat(result, temp_buffer); } if(mod_gm_opt->message != NULL) { temp_buffer[0]='\x0'; strcat(temp_buffer, "output="); strcat(temp_buffer, mod_gm_opt->message); strcat(temp_buffer, "\n"); strcat(result, temp_buffer); } strcat(result, "\n"); gm_log( GM_LOG_TRACE, "data:\n%s\n", result); if(add_job_to_queue( &client, mod_gm_opt->server_list, mod_gm_opt->result_queue, NULL, result, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { gm_log( GM_LOG_TRACE, "send_result_back() finished successfully\n" ); if( mod_gm_opt->dupserver_num ) { if(add_job_to_queue( &client_dup, mod_gm_opt->dupserver_list, mod_gm_opt->result_queue, NULL, result, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { gm_log( GM_LOG_TRACE, "send_result_back() finished successfully for duplicate server.\n" ); } else { gm_log( GM_LOG_TRACE, "send_result_back() finished unsuccessfully for duplicate server\n" ); } } } else { gm_log( GM_LOG_TRACE, "send_result_back() finished unsuccessfully\n" ); free(result); free(temp_buffer); return( STATE_UNKNOWN ); } free(result); free(temp_buffer); return( STATE_OK ); }
/* submit result */ int submit_result() { char * buf; char temp_buffer1[GM_BUFFERSIZE]; char temp_buffer2[GM_BUFFERSIZE]; struct timeval now; struct timeval starttime; struct timeval finishtime; gettimeofday(&now, NULL); if(mod_gm_opt->has_starttime == FALSE) { starttime = now; } else { starttime = mod_gm_opt->starttime; } if(mod_gm_opt->has_finishtime == FALSE) { finishtime = now; } else { finishtime = mod_gm_opt->finishtime; } if(mod_gm_opt->has_latency == FALSE) { mod_gm_opt->latency.tv_sec = 0; mod_gm_opt->latency.tv_usec = 0; } /* escape newline */ buf = gm_escape_newlines(mod_gm_opt->message, GM_DISABLED); free(mod_gm_opt->message); mod_gm_opt->message = strdup(buf); free(buf); gm_log( GM_LOG_TRACE, "queue: %s\n", mod_gm_opt->result_queue ); temp_buffer1[0]='\x0'; snprintf( temp_buffer1, sizeof( temp_buffer1 )-1, "type=%s\nhost_name=%s\nstart_time=%i.%i\nfinish_time=%i.%i\nlatency=%i.%i\nreturn_code=%i\n", mod_gm_opt->active == GM_ENABLED ? "active" : "passive", mod_gm_opt->host, (int)starttime.tv_sec, (int)starttime.tv_usec, (int)finishtime.tv_sec, (int)finishtime.tv_usec, ( int )mod_gm_opt->latency.tv_sec, ( int )mod_gm_opt->latency.tv_usec, mod_gm_opt->return_code ); if(mod_gm_opt->service != NULL) { temp_buffer2[0]='\x0'; strncat(temp_buffer2, "service_description=", (sizeof(temp_buffer2)-1)); strncat(temp_buffer2, mod_gm_opt->service, (sizeof(temp_buffer2)-1)); strncat(temp_buffer2, "\n", (sizeof(temp_buffer2)-1)); strncat(temp_buffer1, temp_buffer2, (sizeof(temp_buffer1)-1)); } if(mod_gm_opt->message != NULL) { temp_buffer2[0]='\x0'; strncat(temp_buffer2, "output=", (sizeof(temp_buffer2)-1)); strncat(temp_buffer2, mod_gm_opt->message, (sizeof(temp_buffer2)-1)); strncat(temp_buffer2, "\n", (sizeof(temp_buffer2)-1)); strncat(temp_buffer1, temp_buffer2, (sizeof(temp_buffer1)-1)); } strncat(temp_buffer1, "\n", (sizeof(temp_buffer1)-2)); gm_log( GM_LOG_TRACE, "data:\n%s\n", temp_buffer1); if(add_job_to_queue( &client, mod_gm_opt->server_list, mod_gm_opt->result_queue, NULL, temp_buffer1, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { gm_log( GM_LOG_TRACE, "send_result_back() finished successfully\n" ); if( mod_gm_opt->dupserver_num ) { if(add_job_to_queue( &client_dup, mod_gm_opt->dupserver_list, mod_gm_opt->result_queue, NULL, temp_buffer1, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { gm_log( GM_LOG_TRACE, "send_result_back() finished successfully for duplicate server.\n" ); } else { gm_log( GM_LOG_TRACE, "send_result_back() finished unsuccessfully for duplicate server\n" ); } } } else { gm_log( GM_LOG_TRACE, "send_result_back() finished unsuccessfully\n" ); return( STATE_UNKNOWN ); } return( STATE_OK ); }