void send_big_jobs(int transportmode) { struct timeval start_time; gettimeofday(&start_time,NULL); char temp_buffer[GM_BUFFERSIZE]; temp_buffer[0]='\x0'; snprintf( temp_buffer,sizeof( temp_buffer )-1,"type=service\nresult_queue=%s\nhost_name=%s\nservice_description=%s\nstart_time=%i.%i\ntimeout=%d\ncheck_options=%i\nscheduled_check=%i\nreschedule_check=%i\nlatency=%f\ncommand_line=%s\n\n\n", GM_DEFAULT_RESULT_QUEUE, "host1", "service1", ( int )start_time.tv_sec, ( int )start_time.tv_usec, 60, 0, 1, 1, 0.0, "/bin/hostname" ); temp_buffer[sizeof( temp_buffer )-1]='\x0'; char * uniq = "something at least bigger than the (GEARMAN_MAX_UNIQUE_SIZE) 64 chars allowed by libgearman!"; int rt = add_job_to_queue( &client, mod_gm_opt->server_list, "service", uniq, temp_buffer, GM_JOB_PRIO_NORMAL, 1, transportmode, TRUE ); ok(rt == GM_OK, "big uniq id sent successfully in mode %s", transportmode == GM_ENCODE_ONLY ? "base64" : "aes256"); char * queue = "something at least bigger than the (GEARMAN_FUNCTION_MAX_SIZE) 512 chars allowed by libgearman! aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; rt = add_job_to_queue( &client, mod_gm_opt->server_list, queue, uniq, temp_buffer, GM_JOB_PRIO_NORMAL, 1, transportmode, TRUE ); ok(rt == GM_ERROR, "big queue sent unsuccessfully in mode %s", transportmode == GM_ENCODE_ONLY ? "base64" : "aes256"); return; }
void test_eventhandler(int transportmode) { char * testdata = strdup("type=eventhandler\ncommand_line=/bin/hostname\n\n\n"); int rt = add_job_to_queue( &client, mod_gm_opt->server_list, "eventhandler", NULL, testdata, GM_JOB_PRIO_NORMAL, 1, transportmode, TRUE ); ok(rt == GM_OK, "eventhandler sent successfully in mode %s", transportmode == GM_ENCODE_ONLY ? "base64" : "aes256"); free(testdata); return; }
void test_servicecheck(int transportmode, char*cmd) { struct timeval start_time; gettimeofday(&start_time,NULL); char temp_buffer[GM_BUFFERSIZE]; temp_buffer[0]='\x0'; snprintf( temp_buffer,sizeof( temp_buffer )-1,"type=service\nresult_queue=%s\nhost_name=%s\nservice_description=%s\nstart_time=%i.%i\ntimeout=%d\ncheck_options=%i\nscheduled_check=%i\nreschedule_check=%i\nlatency=%f\ncommand_line=%s\n\n\n", GM_DEFAULT_RESULT_QUEUE, "host1", "service1", ( int )start_time.tv_sec, ( int )start_time.tv_usec, 60, 0, 1, 1, 0.0, cmd==NULL ? "/bin/hostname" : cmd ); temp_buffer[sizeof( temp_buffer )-1]='\x0'; int rt = add_job_to_queue( &client, mod_gm_opt->server_list, "service", NULL, temp_buffer, GM_JOB_PRIO_NORMAL, 1, transportmode, TRUE ); ok(rt == GM_OK, "servicecheck sent successfully in mode %s", transportmode == GM_ENCODE_ONLY ? "base64" : "aes256"); return; }
/* 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); }
/* create a task and send it */ int add_job_to_queue( gearman_client_st *client, char ** server_list, char * queue, char * uniq, char * data, int priority, int retries, int transport_mode, int send_now ) { gearman_task_st *task = NULL; gearman_return_t ret1, ret2; char * crypted_data; int size, free_uniq; struct timeval now; /* check too long queue names */ if(strlen(queue) > 63) { gm_log( GM_LOG_ERROR, "queue name too long: '%s'\n", queue ); return GM_ERROR; } /* cut off to long uniq ids */ free_uniq = 0; if(uniq != NULL && strlen(uniq) > 63) { uniq = strndup(uniq, 63); free_uniq = 1; } signal(SIGPIPE, SIG_IGN); gm_log( GM_LOG_TRACE, "add_job_to_queue(%s, %s, %d, %d, %d, %d)\n", queue, uniq, priority, retries, transport_mode, send_now ); gm_log( GM_LOG_TRACE, "%d --->%s<---\n", strlen(data), data ); crypted_data = malloc(GM_BUFFERSIZE); size = mod_gm_encrypt(&crypted_data, data, transport_mode); gm_log( GM_LOG_TRACE, "%d +++>\n%s\n<+++\n", size, crypted_data ); #ifdef GM_DEBUG /* verify decrypted string is equal to the original */ char * test; test = malloc(GM_BUFFERSIZE); mod_gm_decrypt(&test, crypted_data, transport_mode); gm_log( GM_LOG_TRACE, "%d ===>\n%s\n<===\n", size, test ); if(strcmp(test, data)) { gm_log( GM_LOG_ERROR, "%d --->%s<---\n", strlen(data), data ); gm_log( GM_LOG_ERROR, "%d ===>\n%s\n<===\n", size, test ); fprintf(stderr, "encrypted string does not match\n"); exit(EXIT_FAILURE); } #endif if( priority == GM_JOB_PRIO_LOW ) { task = gearman_client_add_task_low_background( client, NULL, NULL, queue, uniq, ( void * )crypted_data, ( size_t )size, &ret1 ); gearman_task_give_workload(task,crypted_data,size); } else if( priority == GM_JOB_PRIO_NORMAL ) { task = gearman_client_add_task_background( client, NULL, NULL, queue, uniq, ( void * )crypted_data, ( size_t )size, &ret1 ); gearman_task_give_workload(task,crypted_data,size); } else if( priority == GM_JOB_PRIO_HIGH ) { task = gearman_client_add_task_high_background( client, NULL, NULL, queue, uniq, ( void * )crypted_data, ( size_t )size, &ret1 ); gearman_task_give_workload(task,crypted_data,size); } else { gm_log( GM_LOG_ERROR, "add_job_to_queue() wrong priority: %d\n", priority ); } if(send_now != TRUE) return GM_OK; ret2 = gearman_client_run_tasks( client ); gearman_client_task_free_all( client ); if( ret1 != GEARMAN_SUCCESS || ret2 != GEARMAN_SUCCESS || task == NULL || ( gearman_client_error(client) != NULL && atof(gearman_version()) == 0.14 ) ) { /* log the error */ if(retries == 0) { gettimeofday(&now,NULL); /* only log the first error, otherwise we would fill the log very quickly */ if( mod_gm_con_errors == 0 ) { gettimeofday(&mod_gm_error_time,NULL); gm_log( GM_LOG_ERROR, "sending job to gearmand failed: %s\n", gearman_client_error(client) ); } /* or every minute to give an update */ else if( now.tv_sec >= mod_gm_error_time.tv_sec + 60) { gettimeofday(&mod_gm_error_time,NULL); gm_log( GM_LOG_ERROR, "sending job to gearmand failed: %s (%i lost jobs so far)\n", gearman_client_error(client), mod_gm_con_errors ); } mod_gm_con_errors++; } /* recreate client, otherwise gearman sigsegvs */ gearman_client_free( client ); create_client( server_list, client ); /* retry as long as we have retries */ if(retries > 0) { retries--; gm_log( GM_LOG_TRACE, "add_job_to_queue() retrying... %d\n", retries ); return(add_job_to_queue( client, server_list, queue, uniq, data, priority, retries, transport_mode, send_now )); } /* no more retries... */ else { gm_log( GM_LOG_TRACE, "add_job_to_queue() finished with errors: %d %d\n", ret1, ret2 ); return GM_ERROR; } } /* reset error counter */ mod_gm_con_errors = 0; if(free_uniq) free(uniq); gm_log( GM_LOG_TRACE, "add_job_to_queue() finished sucessfully: %d %d\n", ret1, ret2 ); return GM_OK; }
/* handle performance data */ int handle_perfdata(int event_type, void *data) { nebstruct_host_check_data *hostchkdata = NULL; nebstruct_service_check_data *srvchkdata = NULL; host *hst = NULL; service *svc = NULL; int has_perfdata = FALSE; gm_log( GM_LOG_TRACE, "handle_perfdata(%d)\n", event_type ); if(process_performance_data == 0) { gm_log( GM_LOG_TRACE, "handle_perfdata() process_performance_data disabled globally\n" ); return 0; } /* what type of event/data do we have? */ switch (event_type) { case NEBCALLBACK_HOST_CHECK_DATA: /* an aggregated status data dump just started or ended */ if ((hostchkdata = (nebstruct_host_check_data *) data)) { if (hostchkdata->type != NEBTYPE_HOSTCHECK_PROCESSED || hostchkdata->perf_data == NULL ) { break; } hst = find_host(hostchkdata->host_name); if(hst->process_performance_data == 0) { gm_log( GM_LOG_TRACE, "handle_perfdata() process_performance_data disabled for: %s\n", hst->name ); break; } uniq[0]='\x0'; snprintf( uniq,GM_BUFFERSIZE-1,"%s", hostchkdata->host_name); temp_buffer[0]='\x0'; snprintf( temp_buffer,GM_BUFFERSIZE-1, "DATATYPE::HOSTPERFDATA\t" "TIMET::%d\t" "HOSTNAME::%s\t" "HOSTPERFDATA::%s\t" "HOSTCHECKCOMMAND::%s!%s\t" "HOSTSTATE::%d\t" "HOSTSTATETYPE::%d\n", (int)hostchkdata->timestamp.tv_sec, hostchkdata->host_name, hostchkdata->perf_data, hostchkdata->command_name, hostchkdata->command_args, hostchkdata->state, hostchkdata->state_type); has_perfdata = TRUE; } break; case NEBCALLBACK_SERVICE_CHECK_DATA: /* an aggregated status data dump just started or ended */ if ((srvchkdata = (nebstruct_service_check_data *) data)) { if(srvchkdata->type != NEBTYPE_SERVICECHECK_PROCESSED || srvchkdata->perf_data == NULL) { break; } /* find the naemon service object for this service */ svc = find_service(srvchkdata->host_name, srvchkdata->service_description); if(svc->process_performance_data == 0) { gm_log( GM_LOG_TRACE, "handle_perfdata() process_performance_data disabled for: %s - %s\n", svc->host_name, svc->description ); break; } uniq[0]='\x0'; snprintf( uniq,GM_BUFFERSIZE-1,"%s-%s", srvchkdata->host_name, srvchkdata->service_description); temp_buffer[0]='\x0'; snprintf( temp_buffer,GM_BUFFERSIZE-1, "DATATYPE::SERVICEPERFDATA\t" "TIMET::%d\t" "HOSTNAME::%s\t" "SERVICEDESC::%s\t" "SERVICEPERFDATA::%s\t" "SERVICECHECKCOMMAND::%s\t" "SERVICESTATE::%d\t" "SERVICESTATETYPE::%d\n\n\n", (int)srvchkdata->timestamp.tv_sec, srvchkdata->host_name, srvchkdata->service_description, srvchkdata->perf_data, svc->check_command, srvchkdata->state, srvchkdata->state_type); temp_buffer[GM_BUFFERSIZE-1]='\x0'; has_perfdata = TRUE; } break; default: break; } if(has_perfdata == TRUE) { /* add our job onto the queue */ if(add_job_to_queue( &client, mod_gm_opt->server_list, GM_PERFDATA_QUEUE, (mod_gm_opt->perfdata_mode == GM_PERFDATA_OVERWRITE ? uniq : NULL), temp_buffer, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { gm_log( GM_LOG_TRACE, "handle_perfdata() finished successfully\n" ); } else { gm_log( GM_LOG_TRACE, "handle_perfdata() finished unsuccessfully\n" ); } } return 0; }
/* handle host check events */ static int handle_host_check( int event_type, void *data ) { nebstruct_host_check_data * hostdata; char *raw_command=NULL; char *processed_command=NULL; host * hst; check_result * chk_result; int check_options; struct timeval core_time; struct tm next_check; char buffer1[GM_BUFFERSIZE]; gettimeofday(&core_time,NULL); gm_log( GM_LOG_TRACE, "handle_host_check(%i)\n", event_type ); if ( mod_gm_opt->do_hostchecks != GM_ENABLED ) return NEB_OK; hostdata = ( nebstruct_host_check_data * )data; gm_log( GM_LOG_TRACE, "---------------\nhost Job -> %i, %i\n", event_type, hostdata->type ); if ( event_type != NEBCALLBACK_HOST_CHECK_DATA ) return NEB_OK; /* ignore non-initiate host checks */ if ( hostdata->type != NEBTYPE_HOSTCHECK_ASYNC_PRECHECK && hostdata->type != NEBTYPE_HOSTCHECK_SYNC_PRECHECK) return NEB_OK; /* get objects and set target function */ if((hst=hostdata->object_ptr)==NULL) { gm_log( GM_LOG_ERROR, "Host handler received NULL host object pointer.\n" ); return NEBERROR_CALLBACKCANCEL; } set_target_queue( hst, NULL ); /* local check? */ if(!strcmp( target_queue, "" )) { gm_log( GM_LOG_DEBUG, "passing by local hostcheck: %s\n", hostdata->host_name ); return NEB_OK; } gm_log( GM_LOG_DEBUG, "received job for queue %s: %s\n", target_queue, hostdata->host_name ); /* as we have to intercept host checks so early * (we cannot cancel checks otherwise) * we have to do some host check logic here * taken from checks.c: */ /* clear check options - we don't want old check options retained */ check_options = hst->check_options; hst->check_options = CHECK_OPTION_NONE; /* unset the freshening flag, otherwise only the first freshness check would be run */ hst->is_being_freshened=FALSE; /* adjust host check attempt */ adjust_host_check_attempt(hst,TRUE); temp_buffer[0]='\x0'; /* grab the host macro variables */ clear_volatile_macros(); grab_host_macros(hst); /* get the raw command line */ get_raw_command_line(hst->check_command_ptr,hst->check_command,&raw_command,0); if(raw_command==NULL){ gm_log( GM_LOG_ERROR, "Raw check command for host '%s' was NULL - aborting.\n",hst->name ); return NEBERROR_CALLBACKCANCEL; } /* process any macros contained in the argument */ process_macros(raw_command,&processed_command,0); if(processed_command==NULL){ gm_log( GM_LOG_ERROR, "Processed check command for host '%s' was NULL - aborting.\n",hst->name); return NEBERROR_CALLBACKCANCEL; } /* log latency */ if(mod_gm_opt->debug_level >= GM_LOG_DEBUG) { localtime_r(&hst->next_check, &next_check); strftime(buffer1, sizeof(buffer1), "%Y-%m-%d %H:%M:%S", &next_check ); gm_log( GM_LOG_DEBUG, "host: '%s', next_check is at %s, latency so far: %i\n", hst->name, buffer1, ((int)core_time.tv_sec - (int)hst->next_check)); } /* increment number of host checks that are currently running */ currently_running_host_checks++; /* set the execution flag */ hst->is_executing=TRUE; gm_log( GM_LOG_TRACE, "cmd_line: %s\n", processed_command ); snprintf( temp_buffer,GM_BUFFERSIZE-1,"type=host\nresult_queue=%s\nhost_name=%s\nstart_time=%i.0\nnext_check=%i.0\ntimeout=%d\ncore_time=%i.%i\ncommand_line=%s\n\n\n", mod_gm_opt->result_queue, hst->name, (int)hst->next_check, (int)hst->next_check, host_check_timeout, (int)core_time.tv_sec, (int)core_time.tv_usec, processed_command ); if(add_job_to_queue( &client, mod_gm_opt->server_list, target_queue, (mod_gm_opt->use_uniq_jobs == GM_ENABLED ? hst->name : NULL), temp_buffer, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { } else { my_free(raw_command); my_free(processed_command); /* unset the execution flag */ hst->is_executing=FALSE; /* decrement number of host checks that are currently running */ currently_running_host_checks--; gm_log( GM_LOG_TRACE, "handle_host_check() finished unsuccessfully -> %d\n", NEBERROR_CALLBACKCANCEL ); return NEBERROR_CALLBACKCANCEL; } /* clean up */ my_free(raw_command); my_free(processed_command); /* orphaned check - submit fake result to mark host as orphaned */ if(mod_gm_opt->orphan_host_checks == GM_ENABLED && check_options & CHECK_OPTION_ORPHAN_CHECK) { gm_log( GM_LOG_DEBUG, "host check for %s orphaned\n", hst->name ); if ( ( chk_result = ( check_result * )malloc( sizeof *chk_result ) ) == 0 ) return NEBERROR_CALLBACKCANCEL; snprintf( temp_buffer,GM_BUFFERSIZE-1,"(host check orphaned, is the mod-gearman worker on queue '%s' running?)\n", target_queue); init_check_result(chk_result); chk_result->host_name = strdup( hst->name ); chk_result->scheduled_check = TRUE; chk_result->reschedule_check = TRUE; chk_result->output_file = 0; chk_result->output_file_fp = NULL; chk_result->output = strdup(temp_buffer); chk_result->return_code = mod_gm_opt->orphan_return; chk_result->check_options = CHECK_OPTION_NONE; chk_result->object_check_type = HOST_CHECK; chk_result->check_type = HOST_CHECK_ACTIVE; chk_result->start_time.tv_sec = (unsigned long)time(NULL); chk_result->finish_time.tv_sec = (unsigned long)time(NULL); chk_result->latency = 0; mod_gm_add_result_to_list( chk_result ); chk_result = NULL; } /* tell naemon to not execute */ gm_log( GM_LOG_TRACE, "handle_host_check() finished successfully -> %d\n", NEBERROR_CALLBACKOVERRIDE ); return NEBERROR_CALLBACKOVERRIDE; }
/* handle service check events */ static int handle_svc_check( int event_type, void *data ) { host * hst = NULL; service * svc = NULL; char *raw_command=NULL; char *processed_command=NULL; nebstruct_service_check_data * svcdata; int prio = GM_JOB_PRIO_LOW; check_result * chk_result; struct timeval core_time; struct tm next_check; char buffer1[GM_BUFFERSIZE]; gettimeofday(&core_time,NULL); gm_log( GM_LOG_TRACE, "handle_svc_check(%i, data)\n", event_type ); svcdata = ( nebstruct_service_check_data * )data; if ( event_type != NEBCALLBACK_SERVICE_CHECK_DATA ) return NEB_OK; /* ignore non-initiate service checks */ if ( svcdata->type != NEBTYPE_SERVICECHECK_ASYNC_PRECHECK ) return NEB_OK; /* get objects and set target function */ if((svc=svcdata->object_ptr)==NULL) { gm_log( GM_LOG_ERROR, "Service handler received NULL service object pointer.\n" ); return NEBERROR_CALLBACKCANCEL; } /* find the host associated with this service */ if((hst=svc->host_ptr)==NULL) { gm_log( GM_LOG_ERROR, "Service handler received NULL host object pointer.\n" ); return NEBERROR_CALLBACKCANCEL; } set_target_queue( hst, svc ); /* local check? */ if(!strcmp( target_queue, "" )) { gm_log( GM_LOG_DEBUG, "passing by local servicecheck: %s - %s\n", svcdata->host_name, svcdata->service_description); return NEB_OK; } gm_log( GM_LOG_DEBUG, "received job for queue %s: %s - %s\n", target_queue, svcdata->host_name, svcdata->service_description ); temp_buffer[0]='\x0'; /* as we have to intercept service checks so early * (we cannot cancel checks otherwise) * we have to do some service check logic here * taken from checks.c: */ /* clear check options - we don't want old check options retained */ svc->check_options=CHECK_OPTION_NONE; /* unset the freshening flag, otherwise only the first freshness check would be run */ svc->is_being_freshened=FALSE; /* grab the host and service macro variables */ clear_volatile_macros(); grab_host_macros(hst); grab_service_macros(svc); /* get the raw command line */ get_raw_command_line(svc->check_command_ptr,svc->check_command,&raw_command,0); if(raw_command==NULL){ gm_log( GM_LOG_ERROR, "Raw check command for service '%s' on host '%s' was NULL - aborting.\n", svc->description, svc->host_name ); return NEBERROR_CALLBACKCANCEL; } /* process any macros contained in the argument */ process_macros(raw_command, &processed_command, 0); if(processed_command==NULL) { gm_log( GM_LOG_ERROR, "Processed check command for service '%s' on host '%s' was NULL - aborting.\n", svc->description, svc->host_name); my_free(raw_command); return NEBERROR_CALLBACKCANCEL; } /* log latency */ if(mod_gm_opt->debug_level >= GM_LOG_DEBUG) { localtime_r(&svc->next_check, &next_check); strftime(buffer1, sizeof(buffer1), "%Y-%m-%d %H:%M:%S", &next_check ); gm_log( GM_LOG_DEBUG, "service: '%s' - '%s', next_check is at %s, latency so far: %i\n", svcdata->host_name, svcdata->service_description, buffer1, ((int)core_time.tv_sec - (int)svc->next_check)); } /* increment number of service checks that are currently running... */ currently_running_service_checks++; /* set the execution flag */ svc->is_executing=TRUE; gm_log( GM_LOG_TRACE, "cmd_line: %s\n", processed_command ); snprintf( temp_buffer,GM_BUFFERSIZE-1,"type=service\nresult_queue=%s\nhost_name=%s\nservice_description=%s\nstart_time=%i.0\nnext_check=%i.0\ncore_time=%i.%i\ntimeout=%d\ncommand_line=%s\n\n\n", mod_gm_opt->result_queue, svcdata->host_name, svcdata->service_description, (int)svc->next_check, (int)svc->next_check, (int)core_time.tv_sec, (int)core_time.tv_usec, service_check_timeout, processed_command ); uniq[0]='\x0'; snprintf( uniq,GM_BUFFERSIZE-1,"%s-%s", svcdata->host_name, svcdata->service_description); /* execute forced checks with high prio as they are propably user requested */ //if(check_result_info.check_options & CHECK_OPTION_FORCE_EXECUTION) // prio = GM_JOB_PRIO_HIGH; if(add_job_to_queue( &client, mod_gm_opt->server_list, target_queue, (mod_gm_opt->use_uniq_jobs == GM_ENABLED ? uniq : NULL), temp_buffer, prio, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, TRUE ) == GM_OK) { gm_log( GM_LOG_TRACE, "handle_svc_check() finished successfully\n" ); } else { my_free(raw_command); my_free(processed_command); /* unset the execution flag */ svc->is_executing=FALSE; /* decrement number of host checks that are currently running */ currently_running_service_checks--; gm_log( GM_LOG_TRACE, "handle_svc_check() finished unsuccessfully\n" ); return NEBERROR_CALLBACKCANCEL; } /* clean up */ my_free(raw_command); my_free(processed_command); /* orphaned check - submit fake result to mark service as orphaned */ if(mod_gm_opt->orphan_service_checks == GM_ENABLED && svc->check_options & CHECK_OPTION_ORPHAN_CHECK) { gm_log( GM_LOG_DEBUG, "service check for %s - %s orphaned\n", svc->host_name, svc->description ); if ( ( chk_result = ( check_result * )malloc( sizeof *chk_result ) ) == 0 ) return NEBERROR_CALLBACKCANCEL; snprintf( temp_buffer,GM_BUFFERSIZE-1,"(service check orphaned, is the mod-gearman worker on queue '%s' running?)\n", target_queue); init_check_result(chk_result); chk_result->host_name = strdup( svc->host_name ); chk_result->service_description = strdup( svc->description ); chk_result->scheduled_check = TRUE; chk_result->reschedule_check = TRUE; chk_result->output_file = 0; chk_result->output_file_fp = NULL; chk_result->output = strdup(temp_buffer); chk_result->return_code = mod_gm_opt->orphan_return; chk_result->check_options = CHECK_OPTION_NONE; chk_result->object_check_type = SERVICE_CHECK; chk_result->check_type = SERVICE_CHECK_ACTIVE; chk_result->start_time.tv_sec = (unsigned long)time(NULL); chk_result->finish_time.tv_sec = (unsigned long)time(NULL); chk_result->latency = 0; mod_gm_add_result_to_list( chk_result ); chk_result = NULL; } /* tell naemon to not execute */ gm_log( GM_LOG_TRACE, "handle_svc_check() finished successfully -> %d\n", NEBERROR_CALLBACKOVERRIDE ); return NEBERROR_CALLBACKOVERRIDE; }
/* handle eventhandler events */ static int handle_eventhandler( int event_type, void *data ) { nebstruct_event_handler_data * ds; host * hst = NULL; service * svc = NULL; struct timeval core_time; gettimeofday(&core_time,NULL); gm_log( GM_LOG_TRACE, "handle_eventhandler(%i, data)\n", event_type ); if ( event_type != NEBCALLBACK_EVENT_HANDLER_DATA ) return NEB_OK; ds = ( nebstruct_event_handler_data * )data; if ( ds->type != NEBTYPE_EVENTHANDLER_START ) { gm_log( GM_LOG_TRACE, "skiped type %i, expecting: %i\n", ds->type, NEBTYPE_EVENTHANDLER_START ); return NEB_OK; } gm_log( GM_LOG_DEBUG, "got eventhandler event\n" ); gm_log( GM_LOG_TRACE, "got eventhandler event: %s\n", ds->command_line ); /* service event handler? */ if(ds->service_description != NULL) { if((svc=ds->object_ptr)==NULL) { gm_log( GM_LOG_ERROR, "Eventhandler handler received NULL service object pointer.\n" ); return NEB_OK; } if((hst=svc->host_ptr)==NULL) { gm_log( GM_LOG_ERROR, "Service handler received NULL host object pointer.\n" ); return NEB_OK; } } else { if((hst=ds->object_ptr)==NULL) { gm_log( GM_LOG_ERROR, "Host handler received NULL host object pointer.\n" ); return NEB_OK; } } /* local eventhandler? */ set_target_queue( hst, svc ); if(!strcmp( target_queue, "" )) { if(svc != NULL) { gm_log( GM_LOG_DEBUG, "passing by local service eventhandler: %s - %s\n", svc->host_name, svc->description ); } else { gm_log( GM_LOG_DEBUG, "passing by local host eventhandler: %s\n", hst->name ); } return NEB_OK; } if(mod_gm_opt->route_eventhandler_like_checks != GM_ENABLED || !strcmp( target_queue, "host" ) || !strcmp( target_queue, "service" )) { target_queue[0] = '\x0'; snprintf( target_queue, GM_BUFFERSIZE-1, "eventhandler" ); } gm_log( GM_LOG_DEBUG, "eventhandler for queue %s\n", target_queue ); temp_buffer[0]='\x0'; snprintf( temp_buffer,GM_BUFFERSIZE-1, "type=eventhandler\nstart_time=%i.0\ncore_time=%i.%i\ncommand_line=%s\n\n\n", (int)core_time.tv_sec, (int)core_time.tv_sec, (int)core_time.tv_usec, ds->command_line ); if(add_job_to_queue( &client, mod_gm_opt->server_list, target_queue, NULL, temp_buffer, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, FALSE ) == GM_OK) { gm_log( GM_LOG_TRACE, "handle_eventhandler() finished successfully\n" ); } else { gm_log( GM_LOG_TRACE, "handle_eventhandler() finished unsuccessfully\n" ); } /* tell naemon to not execute */ return NEBERROR_CALLBACKOVERRIDE; }
/* handle generic exports */ int handle_export(int callback_type, void *data) { int i, debug_level_orig, return_code; char * buffer; char * type; char * event_type; nebstruct_log_data * nld; nebstruct_process_data * npd; nebstruct_timed_event_data * nted; temp_buffer[0] = '\x0'; mod_gm_opt->debug_level = -1; debug_level_orig = mod_gm_opt->debug_level; return_code = 0; /* what type of event/data do we have? */ switch (callback_type) { case NEBCALLBACK_PROCESS_DATA: /* 7 */ npd = (nebstruct_process_data *)data; type = nebtype2str(npd->type); snprintf( temp_buffer,GM_BUFFERSIZE-1, "{\"callback_type\":\"%s\",\"type\":\"%s\",\"flags\":%d,\"attr\":%d,\"timestamp\":%d.%d}", "NEBCALLBACK_PROCESS_DATA", type, npd->flags, npd->attr, (int)npd->timestamp.tv_sec, (int)npd->timestamp.tv_usec ); free(type); break; case NEBCALLBACK_TIMED_EVENT_DATA: /* 8 */ nted = (nebstruct_timed_event_data *)data; event_type = eventtype2str(nted->event_type); type = nebtype2str(nted->type); snprintf( temp_buffer,GM_BUFFERSIZE-1, "{\"callback_type\":\"%s\",\"event_type\":\"%s\",\"type\":\"%s\",\"flags\":%d,\"attr\":%d,\"timestamp\":%d.%d,\"recurring\":%d,\"run_time\":%d}", "NEBCALLBACK_TIMED_EVENT_DATA", event_type, type, nted->flags, nted->attr, (int)nted->timestamp.tv_sec, (int)nted->timestamp.tv_usec, nted->recurring, (int)nted->run_time ); free(event_type); free(type); break; case NEBCALLBACK_LOG_DATA: /* 9 */ nld = (nebstruct_log_data *)data; buffer = escapestring(nld->data); type = nebtype2str(nld->type); snprintf( temp_buffer,GM_BUFFERSIZE-1, "{\"callback_type\":\"%s\",\"type\":\"%s\",\"flags\":%d,\"attr\":%d,\"timestamp\":%d.%d,\"entry_time\":%d,\"data_type\":%d,\"data\":\"%s\"}", "NEBCALLBACK_LOG_DATA", type, nld->flags, nld->attr, (int)nld->timestamp.tv_sec, (int)nld->timestamp.tv_usec, (int)nld->entry_time, nld->data_type, buffer); free(type); free(buffer); break; case NEBCALLBACK_SYSTEM_COMMAND_DATA: /* 10 */ break; case NEBCALLBACK_EVENT_HANDLER_DATA: /* 11 */ break; case NEBCALLBACK_NOTIFICATION_DATA: /* 12 */ break; case NEBCALLBACK_SERVICE_CHECK_DATA: /* 13 */ break; case NEBCALLBACK_HOST_CHECK_DATA: /* 14 */ break; case NEBCALLBACK_COMMENT_DATA: /* 15 */ break; case NEBCALLBACK_DOWNTIME_DATA: /* 16 */ break; case NEBCALLBACK_FLAPPING_DATA: /* 17 */ break; case NEBCALLBACK_PROGRAM_STATUS_DATA: /* 18 */ break; case NEBCALLBACK_HOST_STATUS_DATA: /* 19 */ break; case NEBCALLBACK_SERVICE_STATUS_DATA: /* 20 */ break; case NEBCALLBACK_ADAPTIVE_PROGRAM_DATA: /* 21 */ break; case NEBCALLBACK_ADAPTIVE_HOST_DATA: /* 22 */ break; case NEBCALLBACK_ADAPTIVE_SERVICE_DATA: /* 23 */ break; case NEBCALLBACK_EXTERNAL_COMMAND_DATA: /* 24 */ break; case NEBCALLBACK_AGGREGATED_STATUS_DATA: /* 25 */ break; case NEBCALLBACK_RETENTION_DATA: /* 26 */ break; case NEBCALLBACK_CONTACT_NOTIFICATION_DATA: /* 27 */ break; case NEBCALLBACK_CONTACT_NOTIFICATION_METHOD_DATA: /* 28 */ break; case NEBCALLBACK_ACKNOWLEDGEMENT_DATA: /* 29 */ break; case NEBCALLBACK_STATE_CHANGE_DATA: /* 30 */ break; case NEBCALLBACK_CONTACT_STATUS_DATA: /* 31 */ break; case NEBCALLBACK_ADAPTIVE_CONTACT_DATA: /* 32 */ break; default: gm_log( GM_LOG_ERROR, "handle_export() unknown export type: %d\n", callback_type ); mod_gm_opt->debug_level = debug_level_orig; return 0; } if(temp_buffer[0] != '\x0') { for(i=0;i<mod_gm_opt->exports[callback_type]->elem_number;i++) { return_code = mod_gm_opt->exports[callback_type]->return_code[i]; add_job_to_queue( &client, mod_gm_opt->server_list, mod_gm_opt->exports[callback_type]->name[i], /* queue name */ NULL, temp_buffer, GM_JOB_PRIO_NORMAL, GM_DEFAULT_JOB_RETRIES, mod_gm_opt->transportmode, send_now ); } } mod_gm_opt->debug_level = debug_level_orig; return return_code; }
/* 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 ); }
void push_to_queue(struct work_queue* wq, int i) { int *data = (int*)malloc(sizeof(int)); *data = i; add_job_to_queue((struct work_queue*)wq, (void*)data); }