/* running in print live mode */ void running_print_live() {//实时隔一会打印一行数据。 int print_num = 1, re_p_hdr = 0; collect_record(); /* print header */ print_header(); /* set struct module fields */ init_module_fields(); /* skip first record */ if (collect_record_stat() == 0) { do_debug(LOG_INFO, "collect_record_stat warn\n"); } sleep(conf.print_interval); /* print live record */ while (1) { collect_record();//调用各个模块的回调函数,获取一次当前的数据,字符串形式保存在mod->record中。 if (!((print_num) % DEFAULT_PRINT_NUM) || re_p_hdr) { /* get the header will print every DEFAULT_PRINT_NUM */ print_header(); re_p_hdr = 0; print_num = 1; } if (!collect_record_stat()) {//解析mod->record里面的数据,放入st_array中,以备打印 re_p_hdr = 1; continue; } /* print current time */ print_current_time(); print_record();//打印st_array里面的数据 print_num++; /* sleep every interval */ sleep(conf.print_interval);//睡会。 } }
/* running in print live mode */ void running_print_live() { int print_num = 1, re_p_hdr = 0; collect_record(); /* print header */ print_header(); /* set struct module fields */ init_module_fields(); /* skip first record */ if (collect_record_stat() == 0) { do_debug(LOG_INFO, "collect_record_stat warn\n"); } sleep(conf.print_interval); /* print live record */ while (1) { collect_record(); if (!((print_num) % DEFAULT_PRINT_NUM) || re_p_hdr) { /* get the header will print every DEFAULT_PRINT_NUM */ print_header(); re_p_hdr = 0; print_num = 1; } if (!collect_record_stat()) { re_p_hdr = 1; continue; } /* print current time */ print_current_time(); print_record(); print_num++; /* sleep every interval */ sleep(conf.print_interval); } }
void running_cron(void) { int have_collect = 0; if (strstr(conf.output_interface, "file")) { /* 一次收集数据,db和nagios都可以使用 */ collect_record(); output_file(); have_collect = 1; } if (strstr(conf.output_interface, "db")) output_db(); if (strstr(conf.output_interface, "nagios")) output_nagios(); }
void running_cron() { int have_collect = 0; /* output interface */ if (strstr(conf.output_interface, "file")) { /* output data */ collect_record(); output_file(); have_collect = 1; } if (strstr(conf.output_interface, "db")) { output_db(have_collect); } if (strstr(conf.output_interface, "nagios")) { output_nagios(); } }
/* get data from tsar.data */ int get_st_array_from_file(int have_collect) { int i, ret = 0; char pre_line[LEN_10240] = {0}; char line[LEN_10240] = {0}; char detail[LEN_1024] = {0}; char pre_time[32] = {0}; char *s_token; FILE *fp; struct module *mod; if (!have_collect) { collect_record(0); } /* update module parameter */ conf.print_merge = MERGE_ITEM; sprintf(line, "%ld", statis.cur_time); for (i = 0; i < statis.total_mod_num; i++) { mod = &mods[i]; if (mod->enable && strlen(mod->record)) { memset(&detail, 0, sizeof(detail)); /* save collect data to output_file */ sprintf(detail, "%s%s%s%s", SECTION_SPLIT, mod->opt_line, STRING_SPLIT, mod->record); strcat(line, detail); } } if (strlen(line)) { strcat(line, "\n"); } /* if fopen PRE_RECORD_FILE sucess then store data to pre_record */ if ((fp = fopen(PRE_RECORD_FILE, "r"))) { if (!fgets(pre_line, LEN_10240, fp)) { if (fclose(fp) < 0) { do_debug(LOG_FATAL, "fclose error:%s", strerror(errno)); } ret = -1; goto out; } } else { ret = -1; goto out; } /* set print_interval */ s_token = strstr(pre_line, SECTION_SPLIT); if (!s_token) { ret = -1; goto out; } memcpy(pre_time, pre_line, s_token - pre_line); if (!(conf.print_interval = statis.cur_time - atol(pre_time))) { goto out; } /* read pre_line to mod->record and store to pre_array */ read_line_to_module_record(pre_line); init_module_fields(); collect_record_stat(); /* read cur_line and stats operation */ read_line_to_module_record(line); collect_record_stat(); ret = 0; out: /* store current record to PRE_RECORD_FILE */ if ((fp = fopen(PRE_RECORD_FILE, "w"))) { strcat(line, "\n"); if (fputs(line, fp) < 0) { do_debug(LOG_ERR, "fputs error:%s", strerror(errno)); } if (fclose(fp) < 0) { do_debug(LOG_FATAL, "fclose error:%s", strerror(errno)); } chmod(PRE_RECORD_FILE, 0666); } return ret; }