void diag_queues() { char * message = NULL; char * version = NULL; int rc, x; mod_gm_server_status_t *stats; // print some debug info stats = malloc(sizeof(mod_gm_server_status_t)); stats->function_num = 0; stats->worker_num = 0; rc = get_gearman_server_data(stats, &message, &version, "127.0.0.1", GEARMAND_TEST_PORT); diag("get_gearman_server_data: rc: %d\n", rc); diag("get_gearman_server_data: msg: %s\n", message); diag("get_gearman_server_data: ver: %s\n", version); if( rc == STATE_OK ) { diag("%-35s %-9s %-9s\n", "queue", "waiting", "running"); for(x=0; x<stats->function_num;x++) { diag("%-35s %-9d %-9d\n", stats->function[x]->queue, stats->function[x]->waiting, stats->function[x]->running); } } free(message); free(version); free_mod_gm_status_server(stats); return; }
void wait_for_empty_queue(char *queue, int timeout) { char * message = NULL; char * version = NULL; int rc, x; mod_gm_server_status_t *stats; int tries = 0; int found = 0; while(tries <= timeout && found == 0) { tries++; stats = malloc(sizeof(mod_gm_server_status_t)); stats->function_num = 0; stats->worker_num = 0; rc = get_gearman_server_data(stats, &message, &version, "localhost", GEARMAND_TEST_PORT); if( rc == STATE_OK ) { for(x=0; x<stats->function_num;x++) { //diag("%s %d %d\n", stats->function[x]->queue, stats->function[x]->waiting, stats->function[x]->running); if(stats->function[x]->waiting == 0 && stats->function[x]->running == 0 && !strcmp( stats->function[x]->queue, queue ) ) { found = 1; } } } free(message); free(version); free_mod_gm_status_server(stats); sleep(1); } ok(tries < timeout, "queue %s empty after %d seconds", queue, tries); return; }
/* check gearman server */ int check_server(char * server, in_port_t port) { mod_gm_server_status_t *stats; int x; char * message = NULL; char * version = NULL; char * buf = NULL; int total_running = 0; int total_waiting = 0; int checked = 0; int rc; stats = gm_malloc(sizeof(mod_gm_server_status_t)); stats->function_num = 0; stats->worker_num = 0; rc = get_gearman_server_data(stats, &message, &version, server, port); if( rc == STATE_OK ) { for(x=0; x<stats->function_num;x++) { if(opt_queue != NULL && strcmp(opt_queue, stats->function[x]->queue)) continue; checked++; total_running += stats->function[x]->running; total_waiting += stats->function[x]->waiting; if(stats->function[x]->waiting > 0 && stats->function[x]->worker == 0) { rc = STATE_CRITICAL; buf = (char*)gm_malloc(GM_BUFFERSIZE); snprintf(buf, GM_BUFFERSIZE, "Queue %s has %i job%s without any worker. ", stats->function[x]->queue, stats->function[x]->waiting, stats->function[x]->waiting > 1 ? "s":"" ); strncat(message, buf, GM_BUFFERSIZE); } else if(opt_job_critical > 0 && stats->function[x]->waiting >= opt_job_critical) { rc = STATE_CRITICAL; buf = (char*)gm_malloc(GM_BUFFERSIZE); snprintf(buf, GM_BUFFERSIZE, "Queue %s has %i waiting job%s. ", stats->function[x]->queue, stats->function[x]->waiting, stats->function[x]->waiting > 1 ? "s":"" ); strncat(message, buf, GM_BUFFERSIZE); } else if(opt_worker_critical > 0 && stats->function[x]->worker >= opt_worker_critical) { rc = STATE_CRITICAL; buf = (char*)gm_malloc(GM_BUFFERSIZE); snprintf(buf, GM_BUFFERSIZE, "Queue %s has %i worker. ", stats->function[x]->queue, stats->function[x]->worker ); strncat(message, buf, GM_BUFFERSIZE); } else if(opt_job_warning > 0 && stats->function[x]->waiting >= opt_job_warning) { rc = STATE_WARNING; buf = (char*)gm_malloc(GM_BUFFERSIZE); snprintf(buf, GM_BUFFERSIZE, "Queue %s has %i waiting job%s. ", stats->function[x]->queue, stats->function[x]->waiting, stats->function[x]->waiting > 1 ? "s":"" ); strncat(message, buf, GM_BUFFERSIZE); } else if(opt_worker_warning > 0 && stats->function[x]->worker >= opt_worker_warning) { rc = STATE_WARNING; buf = (char*)gm_malloc(GM_BUFFERSIZE); snprintf(buf, GM_BUFFERSIZE, "Queue %s has %i worker. ", stats->function[x]->queue, stats->function[x]->worker ); strncat(message, buf, GM_BUFFERSIZE); } if(buf != NULL) free(buf); buf = NULL; } } if(opt_queue != NULL && checked == 0) { buf = (char*)gm_malloc(GM_BUFFERSIZE); snprintf(buf, GM_BUFFERSIZE, "Queue %s not found", opt_queue ); strncat(message, buf, GM_BUFFERSIZE); rc = STATE_WARNING; } /* print plugin name and state */ printf("%s ", PLUGIN_NAME); if(rc == STATE_OK) printf("OK - %i job%s running and %i job%s waiting. Version: %s", total_running, total_running==1?"":"s", total_waiting, total_waiting==1?"":"s", version); if(rc == STATE_WARNING) printf("WARNING - "); if(rc == STATE_CRITICAL) printf("CRITICAL - "); if(rc == STATE_UNKNOWN) printf("UNKNOWN - "); printf("%s", message); /* print performance data */ if(stats->function_num > 0) { printf("|"); for(x=0; x<stats->function_num;x++) { if(opt_queue != NULL && strcmp(opt_queue, stats->function[x]->queue)) continue; printf( "'%s_waiting'=%i;%i;%i;0 '%s_running'=%i '%s_worker'=%i;%i;%i;0 ", stats->function[x]->queue, stats->function[x]->waiting, opt_job_warning, opt_job_critical, stats->function[x]->queue, stats->function[x]->running, stats->function[x]->queue, stats->function[x]->worker, opt_worker_warning, opt_worker_critical ); } } printf("\n"); free(message); free(version); free_mod_gm_status_server(stats); if(buf != NULL) free(buf); return(rc); }
/* print stats */ void print_stats(char * hostnam) { char * hst = strdup(hostnam); char * hst_c = hst; char * server = NULL; char * port_c = NULL; char * message = NULL; char * version = NULL; char format1[GM_BUFFERSIZE]; char format2[GM_BUFFERSIZE]; char cur_time[GM_BUFFERSIZE]; mod_gm_server_status_t *stats; int port = GM_SERVER_DEFAULT_PORT; int rc; int x; int max_length = 12; int found = 0; struct tm now; time_t t; gm_log( GM_LOG_DEBUG, "print_stats()\n"); server = strsep(&hst, ":"); port_c = strsep(&hst, "\x0"); if(port_c != NULL) port = atoi(port_c); /* get stats */ stats = malloc(sizeof(mod_gm_server_status_t)); stats->function_num = 0; stats->worker_num = 0; rc = get_gearman_server_data(stats, &message, &version, server, port); t = time(NULL); now = *(localtime(&t)); strftime(cur_time, sizeof(cur_time), "%Y-%m-%d %H:%M:%S", &now ); my_printf("%s - %s:%i", cur_time, server, port ); if(version != NULL && strcmp(version, "") != 0) { if(version_saved != NULL) free(version_saved); version_saved = strdup(version); } if(version_saved != NULL && strcmp(version_saved, "") != 0) my_printf(" - v%s", version_saved ); my_printf("\n\n"); if( rc == STATE_OK ) { for(x=0; x<stats->function_num;x++) { if(opt_quiet == GM_ENABLED && stats->function[x]->worker == 0 && stats->function[x]->total == 0) continue; if((int)strlen(stats->function[x]->queue) > max_length) { max_length = (int)strlen(stats->function[x]->queue); } } snprintf(format1, sizeof(format1), " %%-%is | %%16s | %%12s | %%12s\n", max_length); snprintf(format2, sizeof(format2), " %%-%is |%%16i |%%12i |%%12i \n", max_length); my_printf(format1, "Queue Name", "Worker Available", "Jobs Waiting", "Jobs Running"); for(x=0; x < max_length + 51; x++) my_printf("-"); my_printf("\n"); for(x=0; x<stats->function_num;x++) { if(opt_quiet == GM_ENABLED && stats->function[x]->worker == 0 && stats->function[x]->total == 0) continue; my_printf(format2, stats->function[x]->queue, stats->function[x]->worker, stats->function[x]->waiting, stats->function[x]->running); found++; } if(found == 0) { for(x=0; x < max_length + 25; x++) { my_printf(" "); } my_printf("no queues found\n"); } for(x=0; x < max_length + 51; x++) my_printf("-"); my_printf("\n"); } else { my_printf(" %s\n", message); } refresh(); free(hst_c); free(message); free(version); free_mod_gm_status_server(stats); return; }