예제 #1
0
파일: mod_rpi.c 프로젝트: cloudcache/tsar
static void read_rpi_stats(struct module *mod, char *parameter)
{
	FILE *fp;
	char buf[64];
	memset(buf, 0, sizeof(buf));
	struct stats_rpi st_rpi;
	memset(&st_rpi, 0, sizeof(struct stats_rpi));

	if ((fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r")) == NULL) {
		return;
	}

	int cpu_temp;

	fscanf(fp, "%d", &cpu_temp);
    
    if (cpu_temp == 85*1000 || cpu_temp < 1) {
        return;
    }

	st_rpi.cpu_temp = cpu_temp;

	int pos = sprintf(buf, "%u",
			/* the store order is not same as read procedure */
			st_rpi.cpu_temp);
	buf[pos] = '\0';
	set_mod_record(mod, buf);
	fclose(fp);
	return;
}
예제 #2
0
파일: mod_tcprt.c 프로젝트: chobits/tsar
static void
read_tcprt_stats(struct module *mod)
{
    char    buf[LEN_4096];
    struct  stats_tcprt st_tcprt;

    memset(buf, 0, LEN_4096);
    memset(&st_tcprt, 0, sizeof(struct stats_tcprt));

    st_tcprt.avg_bytes = get_value(file_avg_bytes);
    st_tcprt.avg_bytes81 = get_value(file_avg_bytes81);
    st_tcprt.avg_rt = get_value(file_avg_rt);
    st_tcprt.avg_rt81 = get_value(file_avg_rt81);
    st_tcprt.avg_drop = get_value(file_avg_drop);
    st_tcprt.avg_drop81 = get_value(file_avg_drop81);
    st_tcprt.avg_server_time = get_value(file_avg_server_time);
    st_tcprt.avg_server_time81 = get_value(file_avg_server_time81);
    st_tcprt.avg_fail = get_value(file_avg_fail);

    int pos = sprintf(buf, "%u,%u,%u,%u,%u,%u,%u,%u,%u",
            st_tcprt.avg_bytes,
            st_tcprt.avg_bytes81,
            st_tcprt.avg_drop,
            st_tcprt.avg_drop81,
            st_tcprt.avg_rt,
            st_tcprt.avg_rt81,
            st_tcprt.avg_server_time,
            st_tcprt.avg_server_time81,
            st_tcprt.avg_fail);

    buf[pos] = '\0';
    set_mod_record(mod, buf);
}
예제 #3
0
void
read_swift_stats(struct module *mod, char *parameter)
{
    int    retry = 0, pos = 0;
    char   buf[LEN_1024];

    memset(&stats, 0, sizeof(stats));
    mgrport = atoi(parameter);
    if (!mgrport) {
        mgrport = 81;
    }
    while (read_swift_stat("info") < 0 && retry < RETRY_NUM) {
        retry++;
    }
    retry = 0;
    while (read_swift_stat("counters") < 0 && retry < RETRY_NUM) {
        retry++;
    }
    pos = sprintf(buf, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld",
            stats.requests,
            stats.total_svc_time,
            stats.hits,
            stats.b_hit,
            stats.objs,
            stats.bytes_in,
            stats.bytes_out,
            stats.t_cpu,
            stats.s_cpu
             );
    buf[pos] = '\0';
    // fprintf(stderr, "buf: %s\n", buf);
    set_mod_record(mod, buf);
}
예제 #4
0
/*
 * collect traffic infomation
 */
static void
read_traffic_stats(struct module *mod)
{
    int                   len = 0, num = 0;
    FILE                 *fp;
    char                 *p = NULL;
    char                  line[LEN_4096] = {0};
    char                  buf[LEN_4096] = {0};
    struct stats_traffic  total_st, cur_st;

    memset(buf, 0, LEN_4096);
    memset(&total_st, 0, sizeof(struct stats_traffic));
    memset(&cur_st, 0, sizeof(struct stats_traffic));

    if ((fp = fopen(NET_DEV, "r")) == NULL) {
        return;
    }

    memset(&total_st, 0, sizeof(cur_st));

    while (fgets(line, LEN_4096, fp) != NULL) {
        if (strstr(line, "eth") || strstr(line, "em") || strstr(line, "venet")) {
            memset(&cur_st, 0, sizeof(cur_st));
            p = strchr(line, ':');
            sscanf(p + 1, "%llu %llu %llu %llu %*u %*u %*u %*u "
                    "%llu %llu %llu %llu %*u %*u %*u %*u",
                    &cur_st.bytein,
                    &cur_st.pktin,
		    &cur_st.pkterrin,
		    &cur_st.pktdrpin,
                    &cur_st.byteout,
		    &cur_st.pktout,
		    &cur_st.pkterrout,
                    &cur_st.pktdrpout);

            num++;
            total_st.bytein    += cur_st.bytein;
            total_st.byteout   += cur_st.byteout;
            total_st.pktin     += cur_st.pktin;
            total_st.pktout    += cur_st.pktout;
            total_st.pkterrin  += cur_st.pkterrin;
            total_st.pktdrpin  += cur_st.pktdrpin;
            total_st.pkterrout += cur_st.pkterrout;
            total_st.pktdrpout += cur_st.pktdrpout;
        }
    }

    len = sprintf(buf, "%lld,%lld,%lld,%lld,%lld,%lld",
            total_st.bytein,
            total_st.byteout,
            total_st.pktin,
            total_st.pktout,
            total_st.pkterrin + total_st.pkterrout,
            total_st.pktdrpin + total_st.pktdrpout);
    buf[len] = '\0';
    if(num > 0) {
        set_mod_record(mod, buf);
    }
    fclose(fp);
}
예제 #5
0
void
read_swift_tcmalloc_stats(struct module *mod, char *parameter)
{
    int    retry = 0 , pos = 0;
    char   buf[LEN_10240];
    memset(&stats, 0, sizeof(stats));
    mgrport = atoi(parameter);
    if (!mgrport) {
        mgrport = 82;
    }
    while (read_swift_tcmalloc_stat() < 0 && retry < RETRY_NUM) {
        retry++;
    }

    pos = sprintf(buf, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld",
            stats.uba,
            stats.phf,
            stats.ccf,
            stats.trcf,
            stats.thcf,
            stats.mm,
            stats.amu,
            stats.brto,
            stats.vasu,
            stats.siu,
            stats.thiu,
            stats.tps
             );
    buf[pos] = '\0';
    set_mod_record(mod, buf);
}
예제 #6
0
static void
read_swift_stats(struct module *mod, char *parameter)
{
    int    retry = 0, pos = 0;
    char   buf[LEN_4096];

    memset(&stats, 0, sizeof(stats));
    mgrport = atoi(parameter);
    if (!mgrport) {
        mgrport = 82;
    }
    retry = 0;
    while (read_swift_stat("counters") < 0 && retry < RETRY_NUM) {
        retry++;
    }
    pos = sprintf(buf, "%lld,%lld,%lld,%lld,%lld",
             stats.c_act,
             stats.c_conn,
             stats.s_act,
             stats.s_conn,
             stats.s_wait
               );

    buf[pos] = '\0';
    set_mod_record(mod, buf);
}
예제 #7
0
void
print_cgblkio_stats(struct module *mod)
{
    int    pos = 0, i = 0;
    char   buf[LEN_4096];
    /*set n group's data to buf*/
    for(i = 0; i < n_group; i++){
        pos += snprintf(buf + pos, LEN_4096, "%s=%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu",
                blkio_groups[i].group_name,
                blkio_groups[i].rd_merges,
                blkio_groups[i].wr_merges,
                blkio_groups[i].rd_ios,
                blkio_groups[i].wr_ios,
                blkio_groups[i].rd_secs,
                blkio_groups[i].wr_secs,
                blkio_groups[i].qusize,
                blkio_groups[i].wait,
                blkio_groups[i].svctm);
        if(pos >= LEN_4096)
            break;
        pos += snprintf(buf + pos, LEN_4096, ITEM_SPLIT);
        if(pos >= LEN_4096)
            break;
    }
    /*notice tsar to store my mult item data*/
    set_mod_record(mod, buf);
}
예제 #8
0
void
read_swift_store_stats(struct module *mod, char *parameter)
{
    int    retry = 0, pos = 0;
    char   buf[LEN_1024];
    memset(&stats, 0, sizeof(stats));
    mgrport = atoi(parameter);
    if (!mgrport) {
        mgrport = 81;
    }
    while (read_swift_store_stat() < 0 && retry < RETRY_NUM) {
        retry++;
    }
    pos = sprintf(buf, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld",
            stats.objs,
            stats.mobj,
            stats.dobj,
            stats.size,
            stats.ram,
            stats.disk,
            stats.m_hit,
            stats.coss,
            stats.tcoss
             );
    buf[pos] = '\0';
    set_mod_record(mod, buf);
}
예제 #9
0
void
read_swift_code_stats(struct module *mod, char *parameter)
{
    int    retry = 0, pos = 0;
    char   buf[LEN_1024];
    memset(&stats, 0, sizeof(stats));
    mgrport = atoi(parameter);
    if(!mgrport){
        mgrport = 81;
    }
    while (read_swift_code_stat() < 0 && retry < RETRY_NUM) {
        retry++;
    }
    pos = sprintf(buf, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld",
            stats.code200,
            stats.code206,
            stats.code301,
            stats.code302,
            stats.code304,
            stats.code400,
            stats.code403,
            stats.code404,
            stats.code500,
            stats.code502,
            stats.code503,
            stats.code504,
            stats.codeother
             );
    buf[pos] = '\0';
    set_mod_record(mod, buf);
}
예제 #10
0
파일: mod_haproxy.c 프로젝트: haiger/tsar
/*
 *******************************************************
 * Read swapping statistics from haproxy.stat & 80 port
 *******************************************************
 */
static void
read_haproxy(struct module *mod)
{
    int i, pos=0;
    char buf[512];

    memset(&st_haproxy, 0, sizeof(struct stats_haproxy));
    for (i = 0;i < RETRY;i++) {
        if (get_http_status() == 0 && access(HAPROXY, 0) == 0) {
            st_haproxy.stat = 1;
            break;
        }
    }
    if (st_haproxy.stat == 1 && get_haproxy_detail() == 0) {
        if (DEBUG) {
            printf("get right.\n");
        }

    } else {
        if (DEBUG) {
            printf("get wrong.\n");
        }
    }
    if (st_haproxy.stat == 1) {
        pos = sprintf(buf, HAPROXY_STORE_FMT(DATA_SPLIT), st_haproxy.stat, st_haproxy.uptime,
                      st_haproxy.conns, st_haproxy.qps, st_haproxy.hit, st_haproxy.rt);
    }
    buf[pos] = '\0';
    set_mod_record(mod, buf);
    return;
}
예제 #11
0
파일: mod_io.c 프로젝트: nksosoon/tsar
void
print_partition_stats(struct module *mod)
{
    int pos = 0;
    char buf[LEN_4096];
    memset(buf, 0, LEN_4096);
    unsigned int p;

    for (p = 0; p < n_partitions; p++) {
        pos += sprintf(buf + pos, "%s=%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%d",
                partition[p].name,
                new_blkio[p].rd_ios,
                new_blkio[p].rd_merges,
                new_blkio[p].rd_sectors,
                new_blkio[p].rd_ticks,
                new_blkio[p].wr_ios,
                new_blkio[p].wr_merges,
                new_blkio[p].wr_sectors,
                new_blkio[p].wr_ticks,
                new_blkio[p].ticks,
                new_blkio[p].aveq,
                pos);
        pos += sprintf(buf + pos, ITEM_SPLIT);
    }
    if (pos) {
        buf[pos] = '\0';
        set_mod_record(mod, buf);
    }
    rewind(iofp);
    if (NULL != iofp) {
        fclose(iofp);
        iofp =NULL;
    }
    return;
}
예제 #12
0
static void read_swift_domain_stats(struct module *mod, char *parameter)
{
    int    retry = 0, pos = 0;
    char   buf[LEN_1024];
    int    i;

    memset(&swift_domain_stats, 0, sizeof(swift_domain_stats));

    swift_domain_init(parameter);

    while (read_swift_code_stat() < 0 && retry < RETRY_NUM) {
        retry++;
    }

    for (i = 0; i < stats_count; i ++) {
        pos += sprintf(buf + pos, "%s=%lld,%lld,%lld",
                swift_domain[i],
                swift_domain_stats[i][0],
                swift_domain_stats[i][1],
                swift_domain_stats[i][2]);
        pos += sprintf(buf + pos, ITEM_SPLIT);
    }
    buf[pos] = '\0';
    set_mod_record(mod, buf);

    swift_domian_free();
}
예제 #13
0
파일: mod_swap.c 프로젝트: 2005wind/tsar
/*
 *********************************************
 * Read swapping statistics from /proc/vmstat.
 *********************************************
 */
static void read_vmstat_swap(struct module *mod)
{
	FILE *fp;
	char line[4096], buf[LEN_4096];
	memset(buf, 0, LEN_4096);
	struct stats_swap st_swap;
	memset(&st_swap, 0, sizeof(struct stats_swap));
	if ((fp = fopen(VMSTAT, "r")) == NULL) {
		return ;
	}

	while (fgets(line, LEN_4096, fp) != NULL) {

		if (!strncmp(line, "pswpin ", 7)) {
			/* Read number of swap pages brought in */
			sscanf(line + 7, "%lu", &st_swap.pswpin);
		}
		else if (!strncmp(line, "pswpout ", 8)) {
			/* Read number of swap pages brought out */
			sscanf(line + 8, "%lu", &st_swap.pswpout);
		}
	}
	fclose(fp);
	int pos = sprintf(buf, "%ld,%ld", st_swap.pswpin, st_swap.pswpout);
	buf[pos] = '\0';
	set_mod_record(mod, buf);
	return;
}
예제 #14
0
파일: mod_cgmem.c 프로젝트: atixing/tsar
void
print_cgmem_stats(struct module *mod)
{
    int    pos = 0, i = 0;
    char   buf[LEN_4096];

    /*set n group's data to buf*/
    for (i = 0; i < n_group; i++) {
        pos += snprintf(buf + pos, LEN_4096, "%s=%lu,%lu,%lu,%lu,%lu,%lu",
                cgmem_groups[i].group_name,
                cgmem_groups[i].cache + cgmem_groups[i].rss,
                cgmem_groups[i].swap,
                cgmem_groups[i].inanon,
                cgmem_groups[i].acanon,
                cgmem_groups[i].infile,
                cgmem_groups[i].acfile);
        if (pos >= LEN_4096) {
            break;
        }
        pos += snprintf(buf + pos, LEN_4096, ITEM_SPLIT);
        if (pos >= LEN_4096) {
            break;
        }
    }
    /*notice tsar to store my mult item data*/
    set_mod_record(mod, buf);
}
예제 #15
0
파일: mod_ncpu.c 프로젝트: atixing/tsar
static void
read_cpu_stats(struct module *mod)
{
    int               pos = 0;
    char              line[LEN_4096];
    char              buf[LEN_4096];
    char              cpuname[16];
    FILE             *fp;
    struct stats_cpu  st_cpu;

    memset(buf, 0, LEN_4096);
    memset(&st_cpu, 0, sizeof(struct stats_cpu));
    if ((fp = fopen(STAT, "r")) == NULL) {
        return;
    }
    while (fgets(line, LEN_4096, fp) != NULL) {
        if (!strncmp(line, "cpu", 3)) {
            /*
             * Read the number of jiffies spent in the different modes
             * (user, nice, etc.) among all proc. CPU usage is not reduced
             * to one processor to avoid rounding problems.
             */
            sscanf(line,"%4s", cpuname);
            if(strcmp(cpuname,"cpu") == 0)
                continue;
            sscanf(line+5, "%llu %llu %llu %llu %llu %llu %llu %llu %llu",
                    &st_cpu.cpu_user,
                    &st_cpu.cpu_nice,
                    &st_cpu.cpu_sys,
                    &st_cpu.cpu_idle,
                    &st_cpu.cpu_iowait,
                    &st_cpu.cpu_hardirq,
                    &st_cpu.cpu_softirq,
                    &st_cpu.cpu_steal,
                    &st_cpu.cpu_guest);

            pos += sprintf(buf + pos, "%s=%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu",
                    /* the store order is not same as read procedure */
                    cpuname,
                    st_cpu.cpu_user,
                    st_cpu.cpu_sys,
                    st_cpu.cpu_iowait,
                    st_cpu.cpu_hardirq,
                    st_cpu.cpu_softirq,
                    st_cpu.cpu_idle,
                    st_cpu.cpu_nice,
                    st_cpu.cpu_steal,
                    st_cpu.cpu_guest);
            pos += sprintf(buf + pos, ITEM_SPLIT);
        }
    }
    if (pos) {
        buf[pos] = '\0';
        set_mod_record(mod, buf);
    }
    fclose(fp);
    return;
}
예제 #16
0
파일: mod_lvs.c 프로젝트: 0xmalloc/tsar
/*
 *******************************************************
 * Read swapping statistics from ip_vs_stat
 *******************************************************
 */
static void
read_lvs(struct module *mod)
{
    st_lvs.stat = 0;
    st_lvs.conns = 0;
    st_lvs.pktin = 0;
    st_lvs.pktout = 0;
    st_lvs.bytin = 0;
    st_lvs.bytout = 0;

    int    i = 0, pos=0;
    char   buf[512];
    char   tmp[5][16];
    FILE  *fp;
    char   line[MAX_LINE_LEN];

    if ((fp = fopen(LVS_STATS, "r")) != NULL) {
        st_lvs.stat = 1;
        while (fgets(line, MAX_LINE_LEN, fp) != NULL) {
            i++;
            if (i < 3) {
                continue;
            }
            if (!strncmp(line, "CPU", 3)) {
                /* CPU 0:       5462458943          44712664864          54084995692        8542115117674       41738811918899 */
                int    k = 0;
                k = strcspn(line, ":");
                sscanf(line + k + 1, "%s %s %s %s %s", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
                st_lvs.conns += strtoll(tmp[0], NULL, 10);
                st_lvs.pktin += strtoll(tmp[1], NULL, 10);
                st_lvs.pktout += strtoll(tmp[2], NULL, 10);
                st_lvs.bytin += strtoll(tmp[3], NULL, 10);
                st_lvs.bytout += strtoll(tmp[4], NULL, 10);
            } else {
                /* 218EEA1A 1B3BA96D        0    163142140FA1F                0 */
                sscanf(line, "%s %s %s %s %s", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
                st_lvs.conns += strtoll(tmp[0], NULL ,16);
                st_lvs.pktin += strtoll(tmp[1], NULL, 16);
                st_lvs.pktout += strtoll(tmp[2], NULL, 16);
                st_lvs.bytin += strtoll(tmp[3], NULL, 16);
                st_lvs.bytout += strtoll(tmp[4], NULL, 16);
                break;
            }
        }
        if (fclose(fp) < 0) {
            return;
        }
    }
    if (st_lvs.stat == 1) {
        pos = sprintf(buf, LVS_STORE_FMT(DATA_SPLIT), st_lvs.stat, st_lvs.conns, st_lvs.pktin, st_lvs.pktout, st_lvs.bytin, st_lvs.bytout);
    } else {
        return;
    }
    buf[pos] = '\0';
    set_mod_record(mod, buf);
    return;
}
예제 #17
0
/*
 * collect pernic infomation
 */
static void
read_pernic_stats(struct module *mod)
{
    int                   pos = 0, nics = 0;
    FILE                 *fp;
    char                  line[LEN_10240] = {0};
    char                  buf[LEN_10240] = {0};
    struct stats_pernic   st_pernic;

    memset(buf, 0, LEN_10240);
    memset(&st_pernic, 0, sizeof(struct stats_pernic));

    if ((fp = fopen(NET_DEV, "r")) == NULL) {
        return;
    }

    while (fgets(line, LEN_10240, fp) != NULL) {
        memset(&st_pernic, 0, sizeof(st_pernic));
        if (!strstr(line, ":")) {
            continue;
        }
        sscanf(line,  "%*[^a-z]%[^:]:%llu %llu %*u %*u %*u %*u %*u %*u "
                "%llu %llu %*u %*u %*u %*u %*u %*u",
                st_pernic.name,
                &st_pernic.bytein,
                &st_pernic.pktin,
                &st_pernic.byteout,
                &st_pernic.pktout);
        /* if nic not used, skip it */
        if (st_pernic.bytein == 0) {
            continue;
        }

        pos += snprintf(buf + pos, LEN_10240 - pos, "%s=%lld,%lld,%lld,%lld" ITEM_SPLIT,
                st_pernic.name,
                st_pernic.bytein,
                st_pernic.byteout,
                st_pernic.pktin,
		st_pernic.pktout);
        if (strlen(buf) == LEN_10240 - 1) {
            fclose(fp);
            return;
        }

	nics++;
        if (nics > MAX_NICS) {
            break;
        }
    }

    set_mod_record(mod, buf);
    fclose(fp);
    return;
}
예제 #18
0
void
read_squid_stat(struct module *mod, char *parameter)
{
    int i, pos = 0;
    char buf[LEN_4096] = {0};
    char itemname[LEN_4096] = {0};
    live_squid_nr = 0;

    count_squid_nr();
    if (squid_nr == 0) {
        if (atoi(parameter) != 0) {
            port_list[0] = atoi(parameter);  
            squid_nr = 1;
        } else {
            port_list[0] = 3128;
            squid_nr = 1;
        }
    }

    memset(s_st_squid, 0, STATS_SQUID_SIZE * MAXSQUID);
    /*get the live squid number*/
    for (i = 0; i < squid_nr; i++) {
        int retry = 0;
        /* read on each port and retry to get squidclient for 3 times*/
        while (__read_squid_stat(port_list[i], i) < 0 && retry < RETRY_NUM) {
            retry++;
        }
        if (retry == RETRY_NUM) {
            continue;
        }
        s_st_squid[i].usable = TRUE;
        live_squid_nr++;
    }

    /* traverse the port list */
    for (i = 0; i < squid_nr; i++) {
        if (!s_st_squid[i].usable) {
            continue;
        }
        /* generate the item name */
        int n = sprintf(itemname, "port%d", port_list[i]);
        itemname[n] = '\0';

        /* print log to buffer */
        pos += store_single_port(buf + pos, itemname, i);
        /* print a seperate char */
        pos += sprintf(buf + pos, ITEM_SPLIT);
    }
    if (pos && squid_nr == live_squid_nr) {
        buf[pos] = '\0';
        set_mod_record(mod, buf);
    }
}
예제 #19
0
파일: mod_tcp.c 프로젝트: 4Second2None/tsar
void
read_tcp_stats(struct module *mod)
{
    int               sw = FALSE;
    FILE             *fp;
    char              line[LEN_1024];
    char              buf[LEN_1024];
    struct stats_tcp  st_tcp;

    memset(buf, 0, LEN_1024);
    memset(&st_tcp, 0, sizeof(struct stats_tcp));
    if ((fp = fopen(NET_SNMP, "r")) == NULL) {
        return;
    }

    while (fgets(line, LEN_1024, fp) != NULL) {
        if (!strncmp(line, "Tcp:", 4)) {
            if (sw) {
                sscanf(line + 4, "%*u %*u %*u %*d %llu %llu "
                        "%llu %llu %*u %llu %llu %llu %llu %llu",
                        &st_tcp.ActiveOpens,
                        &st_tcp.PassiveOpens,
                        &st_tcp.AttemptFails,
                        &st_tcp.EstabResets,
                        &st_tcp.InSegs,
                        &st_tcp.OutSegs,
                        &st_tcp.RetransSegs,
                        &st_tcp.InErrs,
                        &st_tcp.OutRsts);
                break;

            } else {
                sw = TRUE;
            }
        }
    }

    fclose(fp);

    int pos = sprintf(buf, "%lld,%lld,%lld,%lld,%lld",
            st_tcp.ActiveOpens,
            st_tcp.PassiveOpens,
            st_tcp.InSegs,
            st_tcp.OutSegs,
            st_tcp.RetransSegs);
    buf[pos] = '\0';
    set_mod_record(mod, buf);
}
예제 #20
0
파일: mod_load.c 프로젝트: 0xmalloc/tsar
void
read_stat_load(struct module *mod)
{
    int     load_tmp[3];
    FILE   *fp;
    char    buf[LEN_4096];
    struct  stats_load st_load;
    memset(buf, 0, LEN_4096);
    memset(&st_load, 0, sizeof(struct stats_load));

    if ((fp = fopen(LOADAVG, "r")) == NULL) {
        return;
    }

    /* Read load averages and queue length */
    if (fscanf(fp, "%d.%d %d.%d %d.%d %ld/%d %*d\n",
            &load_tmp[0], &st_load.load_avg_1,
            &load_tmp[1], &st_load.load_avg_5,
            &load_tmp[2], &st_load.load_avg_15,
            &st_load.nr_running,
            &st_load.nr_threads) != 8)
    {
        fclose(fp);
        return;
    }
    st_load.load_avg_1  += load_tmp[0] * 100;
    st_load.load_avg_5  += load_tmp[1] * 100;
    st_load.load_avg_15 += load_tmp[2] * 100;

    if (st_load.nr_running) {
        /* Do not take current process into account */
        st_load.nr_running--;
    }

    int pos = sprintf(buf , "%u,%u,%u,%lu,%u",
            st_load.load_avg_1,
            st_load.load_avg_5,
            st_load.load_avg_15,
            st_load.nr_running,
            st_load.nr_threads);
    buf[pos] = '\0';
    set_mod_record(mod, buf);
    fclose(fp);
}
예제 #21
0
파일: mod_udp.c 프로젝트: abgood/taobao
void
read_udp_stats(struct module *mod)
{
    int               sw = FALSE;
    FILE             *fp;
    char              line[LEN_1024];
    char              buf[LEN_1024];
    struct stats_udp  st_udp;

    memset(buf, 0, LEN_1024);
    memset(&st_udp, 0, sizeof(struct stats_udp));
    if ((fp = fopen(NET_SNMP, "r")) == NULL) {
        return;
    }

    while (fgets(line, LEN_1024, fp) != NULL) {

        if (!strncmp(line, "Udp:", 4)) {
            if (sw) {
                sscanf(line + 4, "%llu %llu %llu %llu",
                        &st_udp.InDatagrams,
                        &st_udp.NoPorts,
                        &st_udp.InErrors,
                        &st_udp.OutDatagrams);
                break;

            } else {
                sw = TRUE;
            }
        }
    }

    fclose(fp);

    int pos = sprintf(buf, "%lld,%lld,%lld,%lld",
            st_udp.InDatagrams,
            st_udp.NoPorts,
            st_udp.InErrors,
            st_udp.OutDatagrams);
    buf[pos] = '\0';
    set_mod_record(mod, buf);
}
예제 #22
0
void
read_partition_stat(struct module *mod)
{
    int                     part_nr, pos = 0;
    char                    buf[LEN_4096];
    FILE                   *mntfile;
    struct mntent          *mnt = NULL;
    struct stats_partition  temp;

    memset(buf, 0, LEN_4096);
    memset(&temp, 0, sizeof(temp));

    /* part_nr = count_partition_nr(NULL); */

    mntfile = setmntent("/etc/mtab", "r");

    /* init part_nr */
    part_nr = 0;
    /* traverse the mount table */
    while ((mnt = getmntent(mntfile)) != NULL) {
        /* only recore block filesystems */
        if (! strncmp(mnt->mnt_fsname, "/", 1)) {
            /* we only read MAXPART partition */
            if (part_nr >= MAXPART) break;
            /* read each partition infomation */
            __read_partition_stat(mnt->mnt_dir, &temp);

            /* print log to the buffer */
            pos += store_single_partition(buf + pos, mnt->mnt_dir, &temp);
            pos += sprintf(buf + pos, ITEM_SPLIT);
            /* successful read */
            part_nr++;
            /* move the pointer to the next structure */
        }
    }
    endmntent(mntfile);
    buf[pos] = '\0';
    set_mod_record(mod, buf);
    return;
}
예제 #23
0
void
read_swift_fwd_stats(struct module *mod, char *parameter)
{
    int    retry = 0 ,pos = 0;
    char   buf[LEN_1024];
    memset(&stats, 0, sizeof(stats));
    mgrport = atoi(parameter);
    if (!mgrport) {
        mgrport = 81;
    }
    while (read_swift_fwd_stat() < 0 && retry < RETRY_NUM) {
        retry++;
    }
    pos = sprintf(buf, "%lld,%lld,%lld,%lld",
            stats.requests,
            stats.bytes_in,
            stats.errors,
            stats.svc_time
             );
    buf[pos] = '\0';
    set_mod_record(mod, buf);
}
예제 #24
0
void
print_partition_stats(struct module *mod)
{
    int pos = 0;
    char buf[LEN_10240];
    memset(buf, 0, LEN_10240);
    unsigned int p;

    for (p = 0; p < n_partitions; p++) {
        pos += snprintf(buf + pos, LEN_10240 - pos, "%s=%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%d" ITEM_SPLIT,
                partition[p].name,
                new_blkio[p].rd_ios,
                new_blkio[p].rd_merges,
                new_blkio[p].rd_sectors,
                new_blkio[p].rd_ticks,
                new_blkio[p].wr_ios,
                new_blkio[p].wr_merges,
                new_blkio[p].wr_sectors,
                new_blkio[p].wr_ticks,
                new_blkio[p].ticks,
                new_blkio[p].aveq,
                pos);
        if (strlen(buf) == LEN_10240 - 1) {
            fclose(iofp);
            return;
        }
    }
    set_mod_record(mod, buf);
    rewind(iofp);
    if (NULL != iofp) {
        if (fclose(iofp) < 0) {
            return;
        }
        iofp =NULL;
    }
    return;
}
예제 #25
0
파일: mod_test.c 프로젝트: nksosoon/tsar
static void
read_test_stats(struct module *mod, char *parameter)
{
    char               buf[256];
    struct stats_test  st_test;

    memset(buf, 0, sizeof(buf));
    memset(&st_test, 0, sizeof(struct stats_test));

    st_test.value_1 = 1;
    st_test.value_2 = 1;
    st_test.value_3 = 1;

    int pos = sprintf(buf, "%llu,%llu,%llu",
            /* the store order is not same as read procedure */
            st_test.value_1,
            st_test.value_2,
            st_test.value_3);

    buf[pos] = '\0';
    /* send data to tsar you can get it by pre_array&cur_array at set_test_record */
    set_mod_record(mod, buf);
    return;
}
예제 #26
0
파일: mod_shell.c 프로젝트: libingli/tsar
static void
read_shell_stats(struct module *mod, const char *parameter)
{
    /* parameter actually equals to mod->parameter */
    int     i = 0, pos = 0;
    FILE   *fp;
    char    line[LEN_4096];
    char    buf[LEN_4096];
    memset(buf, 0, sizeof(buf));
    if ((fp = fopen(parameter, "r")) == NULL) {
        return;
    }
    while (fgets(line, LEN_4096, fp) != NULL) {
        unsigned long long value;
        if (i == 1) {
            char *p;
            char *delim = " \r\n";
            p = strtok(line, delim);
            value = strtoll(p, NULL, 10);
            pos = sprintf(buf, "%llu,", value);
            while ((p = strtok(NULL, delim))) {
                value = strtol(p, NULL, 10);
                pos += sprintf(buf + pos, "%llu,", value);
            }
        }
        if (i++ > 0) {
            break;
        }
    }
    fclose(fp);
    buf[pos-1] = '\0';
    buf[pos] = '\0';
    /* send data to tsar you can get it by pre_array&cur_array at set_shell_record */
    set_mod_record(mod, buf);
    return;
}
예제 #27
0
static void
read_stat_tcpx(struct module *mod)
{
    int                sw;
    FILE              *fp_tcp;
    FILE              *fp_snmp;
    FILE              *fp_netstat;
    char               buf[LEN_4096], line[LEN_4096];
    struct stats_tcpx  st_tcpx;

    memset(buf, 0, LEN_4096);
    memset(&st_tcpx, 0, sizeof(struct stats_tcpx));

    fp_tcp = fopen(TCP, "r");
    if (fp_tcp == NULL) {
        return;
    }

    fp_snmp = fopen(NET_SNMP, "r");
    if (fp_snmp == NULL) {
        fclose(fp_tcp);
        return;
    }
    fp_netstat = fopen(NETSTAT, "r");
    if (fp_netstat == NULL) {
        fclose(fp_snmp);
        fclose(fp_tcp);
        return;
    }
    st_tcpx.tcplistenq = 0;
    st_tcpx.tcplistenincq = 0;

    sw = 0;
    while (fgets(line, LEN_4096, fp_netstat) !=NULL) {
        if (!strncmp(line, "TcpExt:", 7)) {
            if (!sw) {sw = 1; continue;}
            sscanf(line + 7,
                    "%*u %*u %*u %*u %*u %*u %*u %*u %*u "
                    "%*u %*u %*u %*u %*u %*u %*u %*u %*u "
                    "%*u %llu %*u %*u %*u %*u %*u %*u %*u "
                    "%*u %*u %*u %*u %*u %*u %*u %*u %*u "
                    "%*u %*u %*u %*u %*u %*u %*u %*u %*u "
                    "%*u %*u %*u %*u %*u %*u %*u %*u %*u "
                    "%*u %*u %*u %llu %*u %*u %*u %llu",
                    &st_tcpx.tcplistenover,
                    &st_tcpx.tcpembdrop,
                    &st_tcpx.tcprexmitdrop);
            break;
        }
    }
    sw = 0;
    unsigned long activeopen, passiveopen;
    activeopen = 0;
    passiveopen = 0;
    while (fgets(line, LEN_4096, fp_snmp) !=NULL) {
        if (!strncmp(line, "Tcp:", 4)) {
            if (!sw) {sw = 1; continue;}
            sscanf(line + 4, "%*u %*u %*u %*d %lu %lu",
                    &activeopen, &passiveopen);
            break;
        }
    }

    st_tcpx.tcpnconnest = activeopen + passiveopen;
    st_tcpx.tcpnconndrop = 0;
    st_tcpx.tcppersistdrop = 0;
    st_tcpx.tcpkadrop = 0;

    int pos = sprintf(buf,
            "%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,"
            "%llu,%llu,%llu,%llu,%llu,%llu,%llu",
            st_tcpx.tcprecvq,
            st_tcpx.tcpsendq,
            st_tcpx.tcpest,
            st_tcpx.tcptimewait,
            st_tcpx.tcpfinwait1,
            st_tcpx.tcpfinwait2,
            st_tcpx.tcplistenq,
            st_tcpx.tcplistenincq,
            st_tcpx.tcplistenover,
            st_tcpx.tcpnconnest,
            st_tcpx.tcpnconndrop,
            st_tcpx.tcpembdrop,
            st_tcpx.tcprexmitdrop,
            st_tcpx.tcppersistdrop,
            st_tcpx.tcpkadrop);
    buf[pos] = '\0';
    set_mod_record(mod, buf);
    fclose(fp_tcp);
    fclose(fp_snmp);
    fclose(fp_netstat);
}
예제 #28
0
파일: mod_pharos.c 프로젝트: ccfeeling/tsar
void
read_pharos_stats(struct module *mod, char *parameter)
{
    int                 write_flag = 0, addr_len, domain;
    int                 m, sockfd, send, pos;
    void               *addr;
    char                buf[LEN_4096], request[LEN_4096], line[LEN_4096];
    FILE               *stream = NULL;

    struct sockaddr_in  servaddr;
    struct sockaddr_un  servaddr_un;
    struct hostinfo     hinfo;

    init_pharos_host_info(&hinfo);
    if (atoi(parameter) != 0) {
       hinfo.port = atoi(parameter);
    }
    struct stats_pharos st_pharos;
    memset(&st_pharos, 0, sizeof(struct stats_pharos));

    if (*hinfo.host == '/') {
        addr = &servaddr_un;
        addr_len = sizeof(servaddr_un);
        bzero(addr, addr_len);
        domain = AF_LOCAL;
        servaddr_un.sun_family = AF_LOCAL;
        strncpy(servaddr_un.sun_path, hinfo.host, sizeof(servaddr_un.sun_path) - 1);

    } else {
        addr = &servaddr;
        addr_len = sizeof(servaddr);
        bzero(addr, addr_len);
        domain = AF_INET;
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(hinfo.port);
        inet_pton(AF_INET, hinfo.host, &servaddr.sin_addr);
    }


    if ((sockfd = socket(domain, SOCK_STREAM, 0)) == -1) {
        goto writebuf;
    }

    sprintf(request,
            "GET %s HTTP/1.0\r\n"
            "User-Agent: taobot\r\n"
            "Host: %s\r\n"
            "Accept:*/*\r\n"
            "Connection: Close\r\n\r\n",
            hinfo.uri, hinfo.server_name);

    if ((m = connect(sockfd, (struct sockaddr *) addr, addr_len)) == -1 ) {
        goto writebuf;
    }

    if ((send = write(sockfd, request, strlen(request))) == -1) {
        goto writebuf;
    }

    if ((stream = fdopen(sockfd, "r")) == NULL) {
        goto writebuf;
    }

    while (fgets(line, LEN_4096, stream) != NULL) {
        if (!strncmp(line, "request_status:", sizeof("request_status:") - 1)) {
            sscanf(line, "request_status:requests=%llu,tcp_reqs=%llu,udp_reqs=%llu,tcp_accepts=%llu,rt=%llu",
                    &st_pharos.requests, &st_pharos.tcp_reqs, &st_pharos.udp_reqs,
                    &st_pharos.tcp_accepts, &st_pharos.rt);
            write_flag = 1;
        }

        if (!strncmp(line, "reload_status:", sizeof("reload_status:") - 1)) {
            sscanf(line, "reload_status:total_reload_num=%llu,success_reload_num=%llu",
                    &st_pharos.total_reloads, &st_pharos.success_reloads);
            write_flag = 1;
        }

        if (!strncmp(line, "mysql_module_status:", sizeof("mysql_module_status:") - 1)) {
            sscanf(line, "mysql_module_status:sn=%llu,latest_reload_time=%s %8s,frs_region_ip=%llu,frs_wrp=%llu,frs_wideip_pool=%llu,frs_pool=%llu,introduce_domain=%llu,soa=%llu,sn=%llu,wideip=%llu,region=%llu,pool=%llu",
                    &st_pharos.sn,
                    st_pharos.ignore1,
                    st_pharos.ignore2,
                    &st_pharos.rip,
                    &st_pharos.wrp,
                    &st_pharos.wideip_pool,
                    &st_pharos.fpool,
                    &st_pharos.introduce_domain,
                    &st_pharos.soa,
                    &st_pharos.ns,
                    &st_pharos.wideip,
                    &st_pharos.region,
                    &st_pharos.pool);
            write_flag = 1;
        }
    }

writebuf:
    if (stream) {
        fclose(stream);
    }

    if (sockfd != -1) {
        close(sockfd);
    }

    if (write_flag) {
        pos = sprintf(buf, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld",
                st_pharos.requests,
                st_pharos.tcp_reqs,
                st_pharos.udp_reqs,
                st_pharos.total_reloads,
                st_pharos.success_reloads,
                st_pharos.sn,
                st_pharos.rip,
                st_pharos.wrp,
                st_pharos.wideip_pool,
                st_pharos.fpool,
                st_pharos.introduce_domain,
                st_pharos.soa,
                st_pharos.ns,
                st_pharos.wideip,
                st_pharos.region,
                st_pharos.pool);

        buf[pos] = '\0';
        set_mod_record(mod, buf);
    }
}
예제 #29
0
void
read_nginx_domain_stats(struct module *mod, char *parameter)
{
    int                 i, addr_len, domain, m, sockfd, send, pos = 0;
    char                buf[LEN_10240], request[LEN_4096], line[LEN_4096];
    char               *p;
    void               *addr;
    FILE               *stream = NULL;
    struct sockaddr_in  servaddr;
    struct sockaddr_un  servaddr_un;
    struct hostinfo     hinfo;
    struct stats_nginx_domain  stat;
    struct stats_nginx_domain *pair;

    /* get peer info */
    init_nginx_host_info(&hinfo);

    nginx_domain_init(parameter);

    hinfo.port = nginx_port;

    if (*hinfo.host == '/') {
        addr = &servaddr_un;
        addr_len = sizeof(servaddr_un);
        bzero(addr, addr_len);
        domain = AF_LOCAL;
        servaddr_un.sun_family = AF_LOCAL;
        strncpy(servaddr_un.sun_path, hinfo.host, sizeof(servaddr_un.sun_path) - 1);

    } else {
        addr = &servaddr;
        addr_len = sizeof(servaddr);
        bzero(addr, addr_len);
        domain = AF_INET;
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(hinfo.port);
        inet_pton(AF_INET, hinfo.host, &servaddr.sin_addr);
    }

    /* send request */
    if ((sockfd = socket(domain, SOCK_STREAM, 0)) == -1) {
        return;
    }

    sprintf(request,
            "GET %s HTTP/1.0\r\n"
            "User-Agent: taobot\r\n"
            "Host: %s\r\n"
            "Accept:*/*\r\n"
            "Connection: Close\r\n\r\n",
            hinfo.uri, hinfo.server_name);

    if ((m = connect(sockfd, (struct sockaddr *) addr, addr_len)) == -1 ) {
        return;
    }

    if ((send = write(sockfd, request, strlen(request))) == -1) {
        return;
    }

    /* read & parse request */
    if ((stream = fdopen(sockfd, "r")) == NULL) {
        close(sockfd);
        return;
    }

    while (fgets(line, LEN_4096, stream) != NULL) {
        if ((p = strchr(line, ',')) == NULL) {
            continue;
        }
        *p++ = '\0';    /* stat.domain terminating null */

        memset(&stat, 0, sizeof(struct stats_nginx_domain));
        if (sscanf(p, "%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,"
                   "%*u,%*u,%*u,%*u,%*u,%*u,%*u,%*u,%*u,%*u,%*u,%*u,%*u,%*u",
                   &stat.nbytesin, &stat.nbytesout, &stat.nconn, &stat.nreq, &stat.n2XX, &stat.n3XX, &stat.n4XX, &stat.n5XX, &stat.nother, &stat.rt, &stat.upreq, &stat.uprt, &stat.upactreq) != 13) {
            continue;
        }
        strcpy(stat.domain, line);

        if (all_domain == 0) {
            pair = bsearch(&stat, nginx_domain_stats, domain_num, sizeof(nginx_domain_stats[0]), nginxcmp);
            if (pair == NULL) {
                continue;
	    } else {
	        memcpy(pair, &stat, sizeof(struct stats_nginx_domain));
	    }
	} else {
	    memcpy(&nginx_domain_stats[domain_num], &stat, sizeof(struct stats_nginx_domain));
            domain_num++;
        }
    }
    if (top_domain == 0 || top_domain > domain_num) {
        top_domain = domain_num;
    }

    qsort(nginx_domain_stats, domain_num, sizeof(nginx_domain_stats[0]), nginxcmp2);

    for (i=0; i< top_domain; i++) {
        pos += snprintf(buf + pos, LEN_10240 - pos, "%s=%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld" ITEM_SPLIT,
                       nginx_domain_stats[i].domain, nginx_domain_stats[i].nconn, nginx_domain_stats[i].nreq, nginx_domain_stats[i].n2XX, nginx_domain_stats[i].n3XX, nginx_domain_stats[i].n4XX, nginx_domain_stats[i].n5XX, nginx_domain_stats[i].rt, nginx_domain_stats[i].uprt, nginx_domain_stats[i].upreq, nginx_domain_stats[i].upactreq);
        if (strlen(buf) == LEN_10240 - 1) {
            fclose(stream);
            close(sockfd);
            return;
        }
    }

    set_mod_record(mod, buf);
    fclose(stream);
    close(sockfd);
}
예제 #30
0
void read_ts_storage_stats(struct module *mod)
{
  int fd = -1;
  struct sockaddr_un un;
  struct stats_ts_storage st_ts;
  int pos;
  char buf[LINE_4096];
  if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    goto done;
  }
  bzero(&st_ts, sizeof(st_ts));
  bzero(&un, sizeof(un));
  un.sun_family = AF_UNIX;
  strcpy(un.sun_path, conf.ts_mng_socket);
  if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
    goto done;
  }

  int record_len = sizeof(RECORDS_NAME)/sizeof(RECORDS_NAME[0]);
  int i;
  const char *info;
  for ( i = 0; i < record_len; ++i) {
    info = RECORDS_NAME[i];
    long int info_len = strlen(info);
    short int command = TS_RECORD_GET;
    char write_buf[LINE_1024];
    *((short int *)&write_buf[0]) = command;
    *((long int *)&write_buf[2]) = info_len;
    strcpy(write_buf+6, info);
    write(fd, write_buf, 2+4+strlen(info));

    short int ret_status;
    long int ret_info_len;
    short int ret_type;
    long ret_val;
    int read_len = read(fd, buf, LINE_1024);
    if (read_len != -1) {
      ret_status = *((short int *)&buf[0]);
      ret_info_len = *((long int *)&buf[2]);
      ret_type = *((short int *)&buf[6]);
    }
    if (0 == ret_status) {
      if (ret_type < 2) {
	     ret_val= *((long int *)&buf[8]);
      } 
      else if (2 == ret_type) {
	    float ret_val_float = *((float *)&buf[8]);
        ret_val_float *= 100;
        ret_val = (unsigned long long)ret_val_float;
      } 
      else {
        goto done;
      }
    }
    ((unsigned long long *)&st_ts)[i] = ret_val;
  }
done:
  if (-1 != fd)
    close(fd);
  pos = sprintf(buf, "%lld,%lld,%lld,%lld",
                st_ts.ram_used_space,
                st_ts.disk_used_space,
                st_ts.dirs_used,
                st_ts.avg_obj_size
                );
  buf[pos] = '\0';
  set_mod_record(mod, buf);
}