📄 stat.c
字号:
/* * File: cpu_stat.c * * Author: zhubo@legendsec.com * * Date: 2009-08-25 * * Brief: calculate the cpu usage rate */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/vfs.h> #include <sys/statvfs.h> #include "stat.h"/* * Function: int cpu_stat(void) * * Purpose: Statitic the cpu usage rate * * Argument: None * * Return: Success:0 Failure:-1 */int cpu_stat(void){ FILE *fp = NULL; unsigned int num = 0; unsigned long long idle_pre = 0, idle_next = 0; unsigned long long total_pre = 0, total_next = 0; char buf[BUFSIZE - 1] = {0}; cpu_t cpu_t_next, cpu_t_pre; fp = fopen("/proc/stat", "rb"); if (!fp) { fprintf(stderr, "Unable to open /proc/stat"); return -1; } if (!fgets(buf, BUFSIZE-1, fp)) { fclose(fp); return -1; } memset(&cpu_t_pre, 0, sizeof(cpu_t)); num = sscanf(buf, "cpu %lld %lld %lld %lld %lld %lld %lld %lld", &cpu_t_pre.user, &cpu_t_pre.nice, &cpu_t_pre.system, &cpu_t_pre.idle, &cpu_t_pre.iowait, &cpu_t_pre.irq, &cpu_t_pre.softirq, &cpu_t_pre.z); if (num >= 4) { idle_pre = cpu_t_pre.idle; total_pre = cpu_t_pre.user + cpu_t_pre.nice + cpu_t_pre.system + cpu_t_pre.idle + cpu_t_pre.iowait + cpu_t_pre.irq + cpu_t_pre.softirq + cpu_t_pre.z; } fclose(fp); sleep(1); fp = fopen("/proc/stat", "rb"); if (!fp) { fprintf(stderr, "Unable to open /proc/stat"); return -1; } memset(buf, 0, BUFSIZE-1); if (!fgets(buf, BUFSIZE-1, fp)) { fclose(fp); return -1; } memset(&cpu_t_next, 0, sizeof(cpu_t)); num = sscanf(buf, "cpu %lld %lld %lld %lld %lld %lld %lld %lld", &cpu_t_next.user, &cpu_t_next.nice, &cpu_t_next.system, &cpu_t_next.idle, &cpu_t_next.iowait, &cpu_t_next.irq, &cpu_t_next.softirq, &cpu_t_next.z); if (num >= 4) { idle_next = cpu_t_next.idle; total_next = cpu_t_next.user + cpu_t_next.nice + cpu_t_next.system + cpu_t_next.idle + cpu_t_next.iowait + cpu_t_next.irq + cpu_t_next.softirq + cpu_t_next.z; } fclose(fp); /* calculate cpu_usage_rate which is a global varible*/ if (total_next != total_pre) { cpu_usage_rate = 1-((float)idle_next - (float)idle_pre)/((float)total_next - (float)total_pre); fprintf(stderr, "The CPU usage rate: %.0f%%\n", cpu_usage_rate*100); return 0; } else return -1;}/* * Function: int mem_stat(void) * * Purpose: Statitic the memory usage rate * * Argument: None * * Return: Success:0 Failure:-1 */int mem_stat(void){ FILE *fp = NULL; unsigned int num = 0; unsigned int read_byte = 0; unsigned long long mem_total = 0; unsigned long long mem_free = 0; char *position = NULL; char buf[BUFSIZE - 1] = {0}; fp = fopen("/proc/meminfo", "rb"); if (!fp) { fprintf(stderr, "Unable to open /proc/stat"); return -1; } read_byte = fread(buf, 1, sizeof(buf), fp); fclose(fp); if (read_byte == 0) return -1; if (buf != NULL && strlen(buf) > 0) position = strstr(buf, "MemTotal:"); if (position == NULL) return -1; num = sscanf(position, "MemTotal: %lld", &mem_total); if (num != 1) return -1; if (position != NULL && strlen(position) > 0) position = strstr(position, "MemFree:"); if (position == NULL) return -1; num = sscanf(position, "MemFree: %lld", &mem_free); if (num != 1) return -1; /* calculate mem_usage_rate which is a global varible*/ if (mem_total) { mem_usage_rate = ((float)mem_total - (float)mem_free)/(float)mem_total; fprintf(stderr, "The memory usage rate: %.0f%%\n", mem_usage_rate*100); return 0; } else return -1;}/* * Function: int fs_stat(void) * * Purpose: Statitic the log filesystem usage rate * * Argument: None * * Return: Success:0 Failure:-1 */int fs_stat(void) { struct statvfs stat; char *path = "/var/log"; memset(&stat, 0, sizeof(struct statvfs)); /* get the information about the partition which the path is in */ if(statvfs(path, &stat) == -1) return -1; /* calculate fs_usage_rate which is a global varible*/ if (stat.f_blocks) { fs_usage_rate = ((float)stat.f_blocks-(float)stat.f_bfree)/(float)stat.f_blocks; fprintf(stderr, "The log filesystem: Total=%ldK Free=%ldK Usage rate=%.0f%%\n", stat.f_bsize*stat.f_blocks/1024, stat.f_bsize*stat.f_bfree/1024, 100*fs_usage_rate); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -