diff --git a/source/tools/monitor/mservice/master/include/output_print.h b/source/tools/monitor/mservice/master/include/output_print.h index 0c8e9d985ebdc1d73d89eef23633bf28d6a0b7c2..36e578a7d1b4bfaf01e2ec3a172cbc6f53bcf614 100644 --- a/source/tools/monitor/mservice/master/include/output_print.h +++ b/source/tools/monitor/mservice/master/include/output_print.h @@ -30,6 +30,6 @@ void running_current(); void running_check(int check_type); #endif void running_print_live(); - +char * trim(char* src, int max_len); #endif diff --git a/source/tools/monitor/mservice/master/include/tsar.h b/source/tools/monitor/mservice/master/include/tsar.h index 52d419b595048644a6c37d992278dd1317a08440..444debd268fc5168dd7a40ad695b82e1ebb84cd7 100644 --- a/source/tools/monitor/mservice/master/include/tsar.h +++ b/source/tools/monitor/mservice/master/include/tsar.h @@ -56,6 +56,6 @@ struct statistic { extern struct configure conf; extern struct module *mods[MAX_MOD_NUM]; extern struct statistic statis; - +extern pthread_mutex_t module_record_mutex; #endif diff --git a/source/tools/monitor/mservice/master/src/httpserver.c b/source/tools/monitor/mservice/master/src/httpserver.c index c288d2ebcae39eb0ea7f36a6bd6d45cd97def4bb..33175d6896e20014c65c9722fd35bf94de8868e3 100644 --- a/source/tools/monitor/mservice/master/src/httpserver.c +++ b/source/tools/monitor/mservice/master/src/httpserver.c @@ -19,30 +19,52 @@ static int get_request(const char *buf) return REQUEST_MAX; } - void output_http(int sk) { - int i, n = 0; + int i, j, k, n = 0; char detail[LEN_1M] = {0}; struct module *mod; static char line[LEN_10M] = {0}; - char http_header[64]; + char http_header[LEN_64]; + char opt_line[LEN_64]; + char *precord, *psub; + double *st_array; line[0] = 0; for (i = 0; i < statis.total_mod_num; i++) { mod = mods[i]; - if (mod->enable && strlen(mod->record)) { - n = snprintf(detail, LEN_1M, "%s %s\n", mod->opt_line, mod->record); - if (n >= LEN_1M - 1) { - do_debug(LOG_FATAL, "mod %s lenth is overflow %d\n", mod->name, n); - } - /* one for \n one for \0 */ - if (strlen(line) + strlen(detail) >= LEN_10M - 2) { - do_debug(LOG_FATAL, "tsar.data line lenth is overflow line %d detail %d\n", strlen(line), strlen(detail)); - } - strcat(line, detail); - } + if (mod->enable && strlen(mod->record)) { + precord = mod->record; + j = 0; + for (j = 0; j < mod->n_item; j++) { + if (mod->n_item > 1) { + psub = strstr(precord, "="); + if (!psub) + break; + *psub = 0; + snprintf(opt_line, LEN_64, "%s{%s,", mod->opt_line+2, precord); + precord = strstr(psub + 1, ";"); + if (precord) + precord = precord + 1; + } else { + snprintf(opt_line, LEN_64, "%s{", mod->opt_line+2); + } + + st_array = &mod->st_array[j * mod->n_col]; + for (k = 0; k < mod->n_col; k++) { + n = snprintf(detail, LEN_1M, "%s%s} %6.2f\n", opt_line, trim(mod->info[k].hdr, LEN_128), st_array[k]); + if (n >= LEN_1M - 1) { + do_debug(LOG_FATAL, "mod %s lenth is overflow %d\n", mod->name, n); + } + /* one for \n one for \0 */ + if (strlen(line) + strlen(detail) >= LEN_10M - 2) { + do_debug(LOG_FATAL, "tsar.data line lenth is overflow line %d detail %d\n", strlen(line), strlen(detail)); + } + strcat(line, detail); + } + } + } } strcat(line, "\n"); @@ -56,8 +78,16 @@ void output_http(int sk) static void handle_metric(int sk) { - collect_record(); - output_http(sk); + pthread_mutex_lock(&module_record_mutex); + init_module_fields(); + /*read twice for metrics which need compute diff*/ + collect_record(); + collect_record_stat(); + usleep(50000); + collect_record(); + collect_record_stat(); + output_http(sk); + pthread_mutex_unlock(&module_record_mutex); } int http_server(void) diff --git a/source/tools/monitor/mservice/master/src/tsar.c b/source/tools/monitor/mservice/master/src/tsar.c index e861d9d5dc7b7d71fba64f226570791be00346a3..54070f8e7a210874718873f9a3adb4aaf92bbd7a 100644 --- a/source/tools/monitor/mservice/master/src/tsar.c +++ b/source/tools/monitor/mservice/master/src/tsar.c @@ -24,6 +24,8 @@ struct statistic statis; struct configure conf; struct module *mods[MAX_MOD_NUM]; +pthread_mutex_t module_record_mutex = PTHREAD_MUTEX_INITIALIZER; + void usage() { @@ -258,7 +260,9 @@ void *cron_thread(void *arg) { while (1) { statis.cur_time = time(NULL); + pthread_mutex_lock(&module_record_mutex); running_cron(); + pthread_mutex_unlock(&module_record_mutex); sleep(conf.cron_period); } }