示例#1
0
文件: cpuinfo.c 项目: gc87/ctools
void
test_cpu_info(void) {
	//int status, i;
    sigar_t *sigar;
    //sigar_cpu_list_t cpulist;

    sigar_open(&sigar);

    sigar_cpu_info_list_t cpu_info_list;
    sigar_cpu_info_list_get(sigar, &cpu_info_list);
    //sigar_cpu_info_t cpu_info = cup_info_list;

    printf("vendor......%s\n", cpu_info_list.data->vendor);
    printf("model......%s\n", cpu_info_list.data->model);
    printf("mhz......%d\n", cpu_info_list.data->mhz);
    printf("total cores......%d\n", cpu_info_list.data->total_cores);
    //printf("Vendor......%s\n", cpu_info->vendor);

    sigar_cpu_info_list_destroy(sigar, &cpu_info_list);

    sigar_cpu_t cpu_old;
    sigar_cpu_t cpu_cur;
    sigar_cpu_get(sigar, &cpu_old);
    sigar_cpu_perc_t perc;
    sleep(1);
    sigar_cpu_get(sigar, &cpu_cur);

    sigar_cpu_perc_calculate(&cpu_old, &cpu_cur, &perc);
    printf("user......%3.2f%%\n", perc.user * 100);
    printf("sys......%3.2f%%\n", perc.sys * 100);
    printf("wait......%3.2f%%\n", perc.wait * 100);
    printf("nice......%3.2f%%\n", perc.nice * 100);
    printf("irq time......%f\n", perc.irq);
    printf("total used rate......%3.2f%%\n", perc.combined * 100);
    printf("total idle rate......%3.2f%%\n", perc.idle * 100);

    cpu_old = cpu_cur;

    /*
    status = sigar_cpu_list_get(sigar, &cpulist);

    if (status != SIGAR_OK) {
        printf("cpu_list error: %d (%s)\n",
               status, sigar_strerror(sigar, status));
        exit(1);
    }

    for (i=0; i<cpulist.number; i++) {
        sigar_cpu_t cpu = cpulist.data[i];

        printf("cpu user: %ld, sys: %ld, nice: %ld, idle: %ld, wait: %ld, irq: %ld, soft_irq: %ld, stolen: %ld, total: %ld.\n", 
            cpu.user, cpu.sys, cpu.nice, cpu.idle, cpu.wait, cpu.irq, cpu.soft_irq, cpu.stolen, cpu.total);
    }

    sigar_cpu_list_destroy(sigar, &cpulist);
    */

    sigar_close(sigar);
}
示例#2
0
//-------------------------------------------------------------------------------------
float SystemInfo::getCPUPer()
{
	sigar_cpu_t current;
	 
	if(_g_sigar_cpu == NULL)
		sigar_open(&_g_sigar_cpu);

	// sigar_cpu_get(_g_sigar_cpu, &_g_old_cpu);
	 
	sigar_cpu_perc_t perc;

	// while(1)
	{
		sigar_cpu_get(_g_sigar_cpu, &current);
		sigar_cpu_perc_calculate(&_g_old_cpu, &current, &perc);
	 
		// std::cout << "CPU " << perc.combined * 100 << "%\n";
		_g_old_cpu = current;
	//	sleep(1000);
	}
	 
	 
	float ret = float(perc.combined) * 100.f;
	//sigar_close(sigar_cpu);
	// DEBUG_MSG(fmt::format("SystemInfo::getCPUPer(): {}\n", ret));
	return ret;
}
示例#3
0
文件: core.cpp 项目: fredix/geekast
CpuStat::CpuStat()
{
    int status, i;
    sigar_t *sigar;
    sigar_cpu_t _prev_cpu, cpu;
    sigar_cpu_perc_t cpu_perc;

    sigar_open(&sigar);
    status = sigar_cpu_get(sigar, &_prev_cpu);

    if (status != SIGAR_OK) {
        printf("cpu stat error: %d (%s)\n",
               status, sigar_strerror(sigar, status));
        exit(1);
    }

    QTest::qSleep(1000);

    status = sigar_cpu_get(sigar, &cpu);

    if (status != SIGAR_OK) {
        printf("cpu stat error: %d (%s)\n",
               status, sigar_strerror(sigar, status));
        exit(1);
    }

    sigar_cpu_perc_calculate(&_prev_cpu, &cpu, &cpu_perc);



    user.setNum(cpu_perc.user * 100.);
    sys.setNum(cpu_perc.sys * 100.);
    nice.setNum(cpu_perc.nice * 100.);
    idle.setNum(cpu_perc.idle * 100.);
    wait.setNum(cpu_perc.wait * 100.);
    irq.setNum(cpu_perc.irq * 100.);
    soft_irq.setNum(cpu_perc.soft_irq * 100.);
    stolen.setNum(cpu_perc.stolen * 100.);
    combined.setNum(cpu_perc.combined * 100.);

    total.setNum(cpu.total);

    sigar_close(sigar);
}
示例#4
0
文件: cpuinfo.c 项目: gc87/ctools
cJSON* cpu_info_get(CPUInfo* info) {
    sigar_cpu_t cpu_old;
    sigar_cpu_t cpu_cur;
    sigar_cpu_get(info->sigar, &cpu_old);
    sigar_cpu_perc_t perc;
    sleep(1);
    sigar_cpu_get(info->sigar, &cpu_cur);
    sigar_cpu_perc_calculate(&cpu_old, &cpu_cur, &perc);

    //填充json数据格式
    cJSON* body = cJSON_GetObjectItem(info->json, "body");
    cJSON_GetObjectItem(body, "user")->valuedouble = perc.user * 100;
    cJSON_GetObjectItem(body, "sys")->valuedouble = perc.sys * 100;
    cJSON_GetObjectItem(body, "wait")->valuedouble = perc.wait * 100;
    cJSON_GetObjectItem(body, "nice")->valuedouble = perc.nice * 100;
    cJSON_GetObjectItem(body, "irq")->valuedouble = perc.irq * 100;
    cJSON_GetObjectItem(body, "combined")->valuedouble = perc.combined * 100;
    cJSON_GetObjectItem(body, "idle")->valuedouble = perc.idle * 100;

    //char* ret = cJSON_Print(info->json);
    cpu_old = cpu_cur;
    return info->json;
}
示例#5
0
static void send_machine_metrics(SOCKET sock)
{
	sigar_mem_t mem;
	sigar_swap_t swap;
	sigar_cpu_t cpu;
	sigar_loadavg_t loadavg;
	sigar_disk_usage_t tdisk;
	sigar_net_interface_stat_t tnet;
	static int first = 1;
	static sigar_cpu_t pcpu = { 0 };
	static sigar_swap_t pswap = { 0 };
	gp_smon_to_mmon_packet_t pkt;
	struct timeval currenttime = { 0 };
	double seconds_duration = 0.0;
	sigar_file_system_usage_t fsusage;
	const char** fsdir;
	const char** netname;
	sigar_net_interface_stat_t netstat;
	int cpu_total_diff;

	/* NIC metrics */
	apr_uint64_t rx_packets = 0;
	apr_uint64_t tx_packets = 0;
	apr_uint64_t rx_bytes = 0;
	apr_uint64_t tx_bytes = 0;

	/* Disk metrics */
	apr_uint64_t reads = 0;
	apr_uint64_t writes = 0;
	apr_uint64_t read_bytes = 0;
	apr_uint64_t write_bytes = 0;

	memset(&mem, 0, sizeof(mem));
	sigar_mem_get(gx.sigar, &mem);
	TR2(("mem ram: %" FMT64 " total: %" FMT64 " used: %" FMT64 " free: %" FMT64 "\n",
		 mem.ram, mem.total, mem.used, mem.free));

	memset(&swap, 0, sizeof(swap));
	sigar_swap_get(gx.sigar, &swap);
	TR2(("swap total: %" FMT64 " used: %" FMT64 "page_in: %" FMT64 " page_out: %" FMT64 "\n",
		 swap.total, swap.used, swap.page_in, swap.page_out));

	memset(&cpu, 0, sizeof(cpu));
	sigar_cpu_get(gx.sigar, &cpu);
	TR2(("cpu user: %" FMT64 " sys: %" FMT64 " idle: %" FMT64 " wait: %" FMT64 " nice: %" FMT64 " total: %" FMT64 "\n",
			cpu.user, cpu.sys, cpu.idle, cpu.wait, cpu.nice, cpu.total));

	memset(&loadavg, 0, sizeof(loadavg));
	sigar_loadavg_get(gx.sigar, &loadavg);
	TR2(("load_avg: %e %e %e\n", loadavg.loadavg[0], loadavg.loadavg[1], loadavg.loadavg[2]));
	memset(&tdisk, 0, sizeof(tdisk));
	memset(&tnet, 0, sizeof(tnet));

	for (fsdir = gx.fslist; *fsdir; fsdir++)
	{
		int e = sigar_file_system_usage_get(gx.sigar, *fsdir, &fsusage);

		if (0 == e)
		{
			disk_device_t* disk = (disk_device_t*)apr_hash_get(disk_devices, *fsdir, APR_HASH_KEY_STRING);
			/* Check if this is a new device */
			if (!disk)
			{
				disk = (disk_device_t*)apr_palloc(gx.pool, sizeof(disk_device_t));
				disk->name = apr_pstrdup(gx.pool, *fsdir);
				disk->read_bytes = disk->write_bytes = disk->reads = disk->writes = 0;
				apr_hash_set(disk_devices, disk->name, APR_HASH_KEY_STRING, disk);
			}
			reads = disk->reads;
			writes = disk->writes;
			read_bytes = disk->read_bytes;
			write_bytes = disk->write_bytes;

			// DISK READS
			reads = metric_diff_calc(fsusage.disk.reads, disk->reads, disk->name, "disk reads");
			disk->reads = fsusage.disk.reads; // old = new

			// DISK WRITES
			writes = metric_diff_calc(fsusage.disk.writes, disk->writes, disk->name, "disk writes");
			disk->writes = fsusage.disk.writes; // old = new

			// WRITE BYTES
			write_bytes = metric_diff_calc(fsusage.disk.write_bytes, disk->write_bytes, disk->name, "disk write bytes");
			disk->write_bytes = fsusage.disk.write_bytes; // old = new

			// READ BYTES
			read_bytes = metric_diff_calc(fsusage.disk.read_bytes, disk->read_bytes, disk->name, "disk read bytes");
			disk->read_bytes = fsusage.disk.read_bytes; // old = new

			tdisk.reads += reads;
			tdisk.writes += writes;
			tdisk.write_bytes += write_bytes;
			tdisk.read_bytes += read_bytes;
		}
	}
	TR2(("disk reads: %" APR_UINT64_T_FMT " writes: %" APR_UINT64_T_FMT
		 " rbytes: %" APR_UINT64_T_FMT " wbytes: %" APR_UINT64_T_FMT "\n",
		 tdisk.reads, tdisk.writes, tdisk.read_bytes, tdisk.write_bytes));

	for (netname = gx.netlist; *netname; netname++)
	{
		int e = sigar_net_interface_stat_get(gx.sigar, *netname, &netstat);

		if (0 == e)
		{
			net_device_t* nic = (net_device_t*)apr_hash_get(net_devices, *netname, APR_HASH_KEY_STRING);

			/* Check if this is a new device */
			if (!nic)
			{
				nic = (net_device_t*)apr_palloc(gx.pool, sizeof(net_device_t));
				nic->name = apr_pstrdup(gx.pool, *netname);
				nic->tx_bytes = nic->rx_bytes = nic->tx_packets = nic->rx_packets = 0;
				apr_hash_set(net_devices, nic->name, APR_HASH_KEY_STRING, nic);
			}

			//////// RECEIVE PACKEtS
			rx_packets = metric_diff_calc(netstat.rx_packets, nic->rx_packets, nic->name, "rx packets");
			nic->rx_packets = netstat.rx_packets; // old = new

			//////// RECEIVE BYTES
			rx_bytes = metric_diff_calc(netstat.rx_bytes, nic->rx_bytes, nic->name, "rx bytes");
			nic->rx_bytes = netstat.rx_bytes; // old = new

			//////// SEND PACKETS
			tx_packets = metric_diff_calc(netstat.tx_packets, nic->tx_packets, nic->name, "tx packets");
			nic->tx_packets = netstat.tx_packets; // old = new

			//////// SEND BYTES
			tx_bytes = metric_diff_calc(netstat.tx_bytes, nic->tx_bytes, nic->name, "tx bytes");
			nic->tx_bytes = netstat.tx_bytes; // old = new

			tnet.rx_packets += rx_packets;
			tnet.rx_bytes += rx_bytes;
			tnet.tx_packets += tx_packets;
			tnet.tx_bytes += tx_bytes;
		}
	}

	TR2(("rx: %" APR_UINT64_T_FMT " rx_bytes: %" APR_UINT64_T_FMT "\n",
					tnet.rx_packets, tnet.rx_bytes));
	TR2(("tx: %" APR_UINT64_T_FMT " tx_bytes: %" APR_UINT64_T_FMT "\n",
					tnet.tx_packets, tnet.tx_bytes));

	if (first)
	{
		pswap = swap, pcpu = cpu;

		/* We want 0s for these metrics on first pass rather
		 * than some possibly huge number that will throw off
		 * the UI graphs.
		 */
		memset(&tdisk, 0, sizeof(tdisk));
		memset(&tnet, 0, sizeof(tnet));
	}
	first = 0;

	gp_smon_to_mmon_set_header(&pkt,GPMON_PKTTYPE_METRICS);

	pkt.u.metrics.mem.total = mem.total;
	pkt.u.metrics.mem.used = mem.used;
	pkt.u.metrics.mem.actual_used = mem.actual_used;
	pkt.u.metrics.mem.actual_free = mem.actual_free;
	pkt.u.metrics.swap.total = swap.total;
	pkt.u.metrics.swap.used = swap.used;
	pkt.u.metrics.swap.page_in = swap.page_in - pswap.page_in;
	pkt.u.metrics.swap.page_out = swap.page_out - pswap.page_out;
	cpu_total_diff = cpu.total - pcpu.total;
	if (cpu_total_diff)
	{
		float cpu_user = calc_diff_percentage(cpu.user, pcpu.user, cpu_total_diff, "cpu.user") + calc_diff_percentage(cpu.nice, pcpu.nice, cpu_total_diff, "cpu.nice");
		float cpu_sys  = calc_diff_percentage(cpu.sys,  pcpu.sys,  cpu_total_diff, "cpu.sys")  + calc_diff_percentage(cpu.wait, pcpu.wait, cpu_total_diff, "cpu.wait");
		float cpu_idle = calc_diff_percentage(cpu.idle, pcpu.idle, cpu_total_diff, "cpu.idle");


		pkt.u.metrics.cpu.user_pct = cpu_user;
		pkt.u.metrics.cpu.sys_pct = cpu_sys;
		pkt.u.metrics.cpu.idle_pct = cpu_idle;
	}
	else
	{
		pkt.u.metrics.cpu.user_pct = 0;
		pkt.u.metrics.cpu.sys_pct = 0;
		pkt.u.metrics.cpu.idle_pct = 0;
	}
	pkt.u.metrics.load_avg.value[0] = (float) loadavg.loadavg[0];
	pkt.u.metrics.load_avg.value[1] = (float) loadavg.loadavg[1];
	pkt.u.metrics.load_avg.value[2] = (float) loadavg.loadavg[2];

	gettimeofday(&currenttime, NULL);
	seconds_duration = subtractTimeOfDay(&g_time_last_reading, &currenttime);

	pkt.u.metrics.disk.ro_rate = (apr_uint64_t)ceil(tdisk.reads/seconds_duration);
	pkt.u.metrics.disk.wo_rate = (apr_uint64_t)ceil(tdisk.writes/seconds_duration);
	pkt.u.metrics.disk.rb_rate = (apr_uint64_t)ceil(tdisk.read_bytes/seconds_duration);
	pkt.u.metrics.disk.wb_rate = (apr_uint64_t)ceil(tdisk.write_bytes/seconds_duration);
	pkt.u.metrics.net.rp_rate = (apr_uint64_t)ceil(tnet.rx_packets/seconds_duration);
	pkt.u.metrics.net.wp_rate = (apr_uint64_t)ceil(tnet.tx_packets/seconds_duration);
	pkt.u.metrics.net.rb_rate = (apr_uint64_t)ceil(tnet.rx_bytes/seconds_duration);
	pkt.u.metrics.net.wb_rate = (apr_uint64_t)ceil(tnet.tx_bytes/seconds_duration);

	g_time_last_reading = currenttime;

	strncpy(pkt.u.metrics.hname, gx.hostname, sizeof(pkt.u.metrics.hname) - 1);
	pkt.u.metrics.hname[sizeof(pkt.u.metrics.hname) - 1] = 0;
	send_smon_to_mon_pkt(sock, &pkt);

	/* save for next time around */
	pswap = swap, pcpu = cpu;
}