-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
114 lines (104 loc) · 2.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/statvfs.h>
#define CPU_STAT_FILE "/proc/stat"
#define MEM_STAT_FILE "/proc/meminfo"
typedef struct {
unsigned long long user;
unsigned long long nice;
unsigned long long system;
unsigned long long idle;
} cpu_stat_t;
static cpu_stat_t previous_stat, current_stat;
static void read_cpu_stat(cpu_stat_t *stat) {
FILE *pf = fopen(CPU_STAT_FILE, "r");
if(!pf) {
perror("fopen");
exit(1);
}
char line[128];
if(fgets(line, sizeof(line), pf)) {
sscanf(line, "cpu %llu %llu %llu %llu",
&stat->user,
&stat->nice,
&stat->system,
&stat->idle);
}
fclose(pf);
}
float get_ram_usage() {
FILE *pf = fopen(MEM_STAT_FILE, "r");
if(!pf) {
perror("fopen");
return -1.0;
}
char line[1024];
long total_ram = 0;
long free_ram = 0;
while(fgets(line, sizeof(line), pf)){
if(strncmp(line, "MemTotal:", 9) == 0){
total_ram = atol(line + 9);
} else if(strncmp(line, "MemFree:", 8) == 0){
free_ram = atol(line + 8);
}
if(total_ram > 0 && free_ram > 0) {
break;
}
}
fclose(pf);
if(total_ram > 0 && free_ram > 0){
long used_ram = total_ram - free_ram;
return (float)used_ram / total_ram * 100;
} else {
return -1.0;
}
}
float get_disk_usage(const char *mount_point){
struct statvfs fs;
int result = statvfs(mount_point, &fs);
if(result != 0){
perror("statvfs");
return -1.0;
}
unsigned long total_blocks = fs.f_blocks;
unsigned long free_blocks = fs.f_bfree;
float usage_percentage =
(1 - (float)free_blocks / total_blocks) * 100;
return usage_percentage;
}
static float calculate_cpu_usage(cpu_stat_t *previous,
cpu_stat_t *current) {
unsigned long long total_previous =
previous->user +
previous->nice +
previous->system +
previous->idle;
unsigned long long total_current =
current->user +
current->nice +
current->system +
current->idle;
unsigned long long idle_difference = current->idle - previous->idle;
unsigned long long total_difference = total_current - total_previous;
if(total_difference == 0) {
return 0.0;
}
return (1.0 - (idle_difference / (float)total_difference)) * 100.0;
}
int main() {
while (1) {
read_cpu_stat(&previous_stat);
sleep(1);
read_cpu_stat(¤t_stat);
float cpu_usage =
calculate_cpu_usage(&previous_stat, ¤t_stat);
float ram_usage = get_ram_usage();
float disk_usage = get_disk_usage("/");
printf("\rCpu %.2f%% | Ram %.2f%% | Disk %.2f%%",
cpu_usage, ram_usage, disk_usage);
fflush(stdout);
}
return 0;
}