Exemple #1
0
void
output_multi_tcp(int have_collect)
{
    int        fd, flags, res;
    fd_set     fdr, fdw;
    struct     timeval timeout;
    struct     sockaddr_in db_addr;
    int           out_pipe[2];
    int           len;
    static char   data[LEN_10M] = {0};
    int         i;
    /* get st_array */
    if (get_st_array_from_file(have_collect)) {
        return;
    }
    /* only output from output_db_mod */
    reload_modules(conf.output_tcp_mod);

    if (!strcasecmp(conf.output_tcp_merge, "on") || !strcasecmp(conf.output_tcp_merge, "enable")) {
        conf.print_merge = MERGE_ITEM;
    } else {
        conf.print_merge = MERGE_NOT;
    }

    if (pipe(out_pipe) != 0) {
        return;
    }

    dup2(out_pipe[1], STDOUT_FILENO);
    close(out_pipe[1]);

    running_check(RUN_CHECK_NEW);

    fflush(stdout);
    len = read(out_pipe[0], data, LEN_10M);
    close(out_pipe[0]);
    /*now ,the data to send is gotten*/
    for(i = 0; i < conf.output_tcp_addr_num; i++){
        send_data_tcp(conf.output_tcp_addr[i], data, len);
    }
}
Exemple #2
0
/*
 * send check to remote
 */
void
send_tcp(int fd, int have_collect)
{
    char   data[LEN_40960] = {0};
    int    out_pipe[2];
    int    len;

    /* get st_array */
    if (get_st_array_from_file(have_collect)) {
        return;
    }

    /* only output from output_db_mod */
    reload_modules(conf.output_tcp_mod);

    if (!strcasecmp(conf.output_tcp_merge, "on") || !strcasecmp(conf.output_tcp_merge, "enable")) {
        conf.print_merge = MERGE_ITEM;
    } else {
        conf.print_merge = MERGE_NOT;
    }

    if (pipe(out_pipe) != 0) {
        return;
    }

    dup2(out_pipe[1], STDOUT_FILENO);
    close(out_pipe[1]);

    running_check(RUN_CHECK_NEW);

    fflush(stdout);
    len = read(out_pipe[0], data, LEN_40960);
    close(out_pipe[0]);

    if (len > 0 && write(fd, data, len) != len) {
        do_debug(LOG_ERR, "output_db write error:%s", strerror(errno));
    }
}
Exemple #3
0
/*
 * send sql to remote db
 */
void
send_sql_txt(int fd, int have_collect)
{
    int    i = 0, j, len;
    char   sqls[LEN_10240] = {0};
    char   s_time[LEN_64] = {0};
    char   host_name[LEN_64] = {0};
    struct module *mod;

    /* get hostname */
    if (0 != gethostname(host_name, sizeof(host_name))) {
        do_debug(LOG_FATAL, "send_sql_txt: gethostname err, errno=%d", errno);
    }
    while (host_name[i]) {
        if (!isprint(host_name[i++])) {
            host_name[i-1] = '\0';
            break;
        }
    }

    /* get st_array */
    if (get_st_array_from_file(have_collect)) {
        return;
    }

    /* only output from output_db_mod */
    reload_modules(conf.output_db_mod);

    sprintf(s_time, "%ld", time(NULL));

    /* print summary data */
    for (i = 0; i < statis.total_mod_num; i++) {
        mod = &mods[i];
        if (!mod->enable) {
            continue;

        } else {
            if (!mod->st_flag) {
                char    sql_hdr[LEN_256] = {0};
                /* set sql header */
                memset(sql_hdr, '\0', sizeof(sql_hdr));
                sprintf(sql_hdr, "insert into `%s` (host_name, time) VALUES ('%s', '%s');",
                        mod->opt_line+2, host_name, s_time);
                strcat(sqls, sql_hdr);

            } else {
                char    str[LEN_32] = {0};
                char    sql_hdr[LEN_256] = {0};
                struct  mod_info *info = mod->info;

                /* set sql header */
                memset(sql_hdr, '\0', sizeof(sql_hdr));
                sprintf(sql_hdr, "insert into `%s` (host_name, time", mod->opt_line+2);

                /* get value */
                for (j = 0; j < mod->n_col; j++) {
                    strcat(sql_hdr, ", `");
                    char *p = info[j].hdr;
                    while (*p == ' ') {
                        p++;
                    }
                    strcat(sql_hdr, p);
                    strcat(sql_hdr, "`");
                }
                strcat(sql_hdr, ") VALUES ('");
                strcat(sql_hdr, host_name);
                strcat(sql_hdr, "', '");
                strcat(sql_hdr, s_time);
                strcat(sql_hdr, "'");
                strcat(sqls, sql_hdr);

                /* get value */
                for (j = 0; j < mod->n_col; j++) {
                    memset(str, 0, sizeof(str));
                    sprintf(str, ", '%.1f'", mod->st_array[j]);
                    strcat(sqls, str);
                }
                strcat(sqls, ");");
            }
        }
        len = strlen(sqls);
        if (write(fd, sqls, len) != len) {
            do_debug(LOG_ERR, "output_db write error:%s", strerror(errno));
        }
    }
}
Exemple #4
0
void output_nagios(){
	struct	module *mod;
	int	result = 0;
	char	output[LEN_4096] = {0};
	char	output_err[LEN_4096] = {0};
	char	s_time[LEN_64] = {0};
	char	host_name[LEN_64] = {0};
	int	i = 0, j = 0, k = 0, l = 0;
	/* if cycle time ok*/
	int	now_time;
	now_time = statis.cur_time - statis.cur_time%60;
	if ((*conf.cycle_time) == 0 || now_time%*(conf.cycle_time) != 0)
		return;

	/* get hostname */
	if (0 != gethostname(host_name, sizeof(host_name))) {
		do_debug(LOG_FATAL, "send to nagios: gethostname err, errno=%d \n", errno);
	}
	while (host_name[i]) {
		if (!isprint(host_name[i++])) {
			host_name[i-1] = '\0';
			break;
		}
	}

	/* update module parameter */
	conf.print_merge = MERGE_NOT;

	/* get st_array */
	if (get_st_array_from_file(0))
		return;

	/* only output from output_nagios_mod */
	reload_modules(conf.output_nagios_mod);

	sprintf(s_time, "%ld", time(NULL));

	/* print summary data */
	for (i = 0; i < statis.total_mod_num; i++) {
		mod = &mods[i];
		if (!mod->enable)
			continue;
		else if (!mod->st_flag) {
			printf("name %s\n",mod->name);
			printf("do nothing\n");
		}else {
			char opt[LEN_32];
			char check[LEN_64];
			char *n_record = strdup(mod->record);
			char *token = strtok(n_record, ITEM_SPLIT);
			char *s_token;
			double  *st_array;
			struct mod_info *info = mod->info;
			j = 0;
			//get mod_name.(item_name).col_name value
			while (token) {
				memset(check, 0, sizeof(check));
				strcat(check,mod->name+4);
				strcat(check,".");
				s_token = strstr(token, ITEM_SPSTART);
				//multi item
				if(s_token){
					memset(opt, 0, sizeof(opt));
					strncat(opt, token, s_token - token);
					strcat(check,opt);
					strcat(check,".");
				}
				//get value
				st_array = &mod->st_array[j * mod->n_col];
				token = strtok(NULL, ITEM_SPLIT);
				j++;
				for (k = 0; k < mod->n_col; k++) {
					char check_item[LEN_64];
					char *p;
					memset(check_item,0,LEN_64);
					memcpy(check_item,check,LEN_64);
					p = info[k].hdr;
					while(*p == ' ')
						p++;
					strcat(check_item,p);
					for(l = 0; l < conf.mod_num; l++){
						/* cmp tsar item with naigos item*/
						if(!strcmp(conf.check_name[l],check_item))
						{
							char value[LEN_32];
							memset(value,0,sizeof(value));
							sprintf(value,"%0.2f",st_array[k]);
							strcat(output,check_item);
							strcat(output,"=");
							strcat(output,value);
							strcat(output," ");
							if( conf.cmin[l] != 0 && st_array[k] >= conf.cmin[l] ){
								if( conf.cmax[l] == 0 || (conf.cmax[l] != 0 && st_array[k] <= conf.cmax[l]) ){
									result = 2;
									strcat(output_err,check_item);
									strcat(output_err,"=");
									strcat(output_err,value);
									strcat(output_err," ");
									continue;
								}
							}
							if( conf.wmin[l] != 0 && st_array[k] >= conf.wmin[l] ){
								if( conf.wmax[l] == 0 || (conf.wmax[l] != 0 && st_array[k] <= conf.wmax[l]) ){
									if( result != 2)
										result = 1;
									strcat(output_err,check_item);
									strcat(output_err,"=");
									strcat(output_err,value);
									strcat(output_err," ");
								}
							}
						}
					}
				}
			}
		}
	}
	if(!strcmp(output_err,""))
		strcat(output_err,"OK");
	/* send to nagios server*/
	char nagios_cmd[LEN_1024];
	sprintf(nagios_cmd,"echo \"%s;tsar;%d;%s|%s\"|%s -H %s -p %d -to 10 -d \";\" -c %s",host_name,result,output_err,output,conf.send_nsca_cmd,conf.server_addr,*(conf.server_port),conf.send_nsca_conf);
	do_debug(LOG_DEBUG,"send to naigos:%s\n",nagios_cmd);
	if(system(nagios_cmd) != 0)
		do_debug(LOG_WARN,"nsca run error:%s\n",nagios_cmd);;
	printf("%s\n",nagios_cmd);
}
Exemple #5
0
/* mod->record输出到nagios */
void output_nagios(void) {
    int now_time;
    char s_time[LEN_64] = {0};
    struct module *mod;
    int i, j, k, l, result = 0;
    char output[LEN_4096] = {0};
    char output_err[LEN_4096] = {0};

    /* 当前整分时间 */
    now_time = statis.cur_time - statis.cur_time%60;
    /* 周期为整分 */
    if ((*conf.cycle_time == 0) || (now_time % *conf.cycle_time != 0))
        return;

    /* 非合并模式 */
    conf.print_merge = MERGE_NOT;

    /* 从文件里读记录并保存至st_array */
    if (get_st_array_from_file())
        return;

    /* 重新加载输出到nagios里的模块 */
    reload_modules(conf.output_nagios_mod);

    /* 当前时间 */
    sprintf(s_time, "%ld", time(NULL));

    for (i = 0; i < statis.total_mod_num; i++) {
        mod = &mods[i];
        if (!mod->enable) 
            continue;
        else {
            /* st_flag在set_record里设置, 表明st_array里有数据 */
            if (!mod->st_flag)
                printf("name %s do nothing!\n", mod->name);
            else {
                char *s_token;
                double *st_array;

                char check[LEN_64] = {0};
                char opt[LEN_32] = {0};
                char *n_record = strdup(mod->record);
                char *token = strtok(n_record, ITEM_SPLIT);
                struct mod_info *info = mod->info;
                j = 0;

                while (token) {
                    memset(check, 0, sizeof(check));
                    /* 组合模块与各项的名字 */
                    strcat(check, mod->name + 4);
                    strcat(check, ".");
                    s_token = strstr(token, ITEM_SPSTART);
                    /* multi item */
                    if (s_token){
                        memset(opt, 0, sizeof(opt));
                        strncat(opt, token, s_token - token);
                        strcat(check, opt);
                        strcat(check, ".");
                    }

                    /* get value */
                    st_array = &mod->st_array[j * mod->n_col];
                    token = strtok(NULL, ITEM_SPLIT);
                    j++;

                    for (k = 0; k < mod->n_col; k++) {
                        char check_item[LEN_64] = {0};
                        char *p;

                        memcpy(check_item, check, LEN_64);
                        p = info[k].hdr;
                        while (*p == ' ') p++;
                        /* 组合module与各列的名字 */
                        strcat(check_item, p);

                        for (l = 0; l < conf.mod_num; l++) {
                            /* threshold的check_name与check_item进行比较 
                             * 把代表util的st_array保存到output里
                             */
                            if (!strcmp(conf.check_name[l], check_item)) {
                                char value[LEN_32] = {0};

                                /* 模块多项目组合时, threshold要分项目写 */
                                sprintf(value, "%0.02f", st_array[k]);
                                strcat(output, check_item);
                                strcat(output, "=");
                                strcat(output, value);
                                strcat(output, " ");

                                /* 阀值计算 */
                                    /* Critical min与max之间
                                     * result为2, 状态危险
                                     * result为0, 状态正常
                                     */
                                if (conf.cmin[l] != 0 && st_array[k] >= conf.cmin[l]) {
                                    if (conf.cmax[l] == 0 || (conf.cmax[l] != 0 && st_array[k] <= conf.cmax[l])) {
                                        result = 2;
                                        strcat(output_err, check_item);
                                        strcat(output_err, "=");
                                        strcat(output_err, value);
                                        strcat(output_err, " ");
                                        continue;
                                    }
                                }
                                    /* Warning min与max之间
                                     * result为1, 状态警告
                                     * result为0, 状态正常
                                     */
                                if (conf.wmin[l] != 0 && st_array[k] >= conf.wmin[l]) {
                                    if (conf.wmax[l] == 0 || (conf.wmax[l] != 0 && st_array[k] <= conf.wmax[l])) {
                                        if (result != 2)
                                            result = 1;
                                        strcat(output_err, check_item);
                                        strcat(output_err, "=");
                                        strcat(output_err, value);
                                        strcat(output_err, " ");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    /* 组合nagios命令串,把数据写到nagios服务器上 */
    if (!strcmp(output_err, ""))
        strcat(output_err, "OK");

    char nagios_cmd[LEN_1024] = {0};
    sprintf(nagios_cmd, "echo \"%s;tsar;%d;%s|%s\"|%s -H %s -p %d -to 10 -d \";\" -c %s", conf.host_name, result, output_err, output, conf.send_nsca_cmd, conf.server_addr, *(conf.server_port), conf.send_nsca_conf);

    do_debug(LOG_DEBUG, "send to nagios:%s\n", nagios_cmd);

    // if (system(nagios_cmd) != 0)
    //     do_debug(LOG_WARN, "nsca run error:%s\n", nagios_cmd);

    printf("%s\n", nagios_cmd);
}
Exemple #6
0
void send_sql_txt(int fd)
{
	struct	module *mod;
	char	sqls[LEN_4096] = {0};
	char	s_time[LEN_64] = {0};
	char	host_name[LEN_64] = {0};
	int	i = 0, j;

	/* get hostname */	
	if (0 != gethostname(host_name, sizeof(host_name))) {
		do_debug(LOG_FATAL, "send_sql_txt: gethostname err, errno=%d\n", errno);
	}
	while (host_name[i]) {
                if (!isprint(host_name[i++])) {
                        host_name[i-1] = '\0';
                        break;
                }
        }
	
	/* update module parameter */
	conf.print_merge = MERGE_ITEM;

	/* get st_array */
	if (get_st_array_from_file())
		return;
	
	/* only output from output_db_mod */
	reload_modules(conf.output_db_mod);

	sprintf(s_time, "%ld", time(NULL));

	/* print summary data */
	for (i = 0; i < statis.total_mod_num; i++) {
		mod = &mods[i];
		if (!mod->enable)
			continue;
		else if (!mod->st_flag) {
			char	sql_hdr[LEN_256] = {0};
			/* set sql header */
			memset(sql_hdr, '\0', sizeof(sql_hdr));
			sprintf(sql_hdr, "insert into `%s` (host_name, time) VALUES ('%s', '%s');", 
					mod->opt_line+2, host_name, s_time);
			strcat(sqls, sql_hdr);
		} else {
			char	str[LEN_32] = {0};
			char	sql_hdr[LEN_256] = {0};
			struct  mod_info *info = mod->info;

			/* set sql header */
			memset(sql_hdr, '\0', sizeof(sql_hdr));
			sprintf(sql_hdr, "insert into `%s` (host_name, time", mod->opt_line+2);
	
			/* get value */
			for (j = 0; j < mod->n_col; j++) {
				strcat(sql_hdr, ", ");
				strcat(sql_hdr, info[j].hdr);
			}
			strcat(sql_hdr, ") VALUES ('");
			strcat(sql_hdr, host_name);
			strcat(sql_hdr, "', '");
			strcat(sql_hdr, s_time);
			strcat(sql_hdr, "'");
			strcat(sqls, sql_hdr);

			/* get value */
			for (j = 0; j < mod->n_col; j++) {
				memset(str, 0, sizeof(str));
				sprintf(str, ", '%.1f'", mod->st_array[j]);
				strcat(sqls, str);	
			}
			strcat(sqls, ");");
		}
	}
	do_debug(LOG_DEBUG,"send to db sql:%s\n",sqls);
	if(write(fd, sqls, strlen(sqls)) < strlen(sqls)){
		do_debug(LOG_WARN, "send to db sql:'%s' error\n", sqls);
	}
}