-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmonitor.c
More file actions
196 lines (147 loc) · 5.85 KB
/
monitor.c
File metadata and controls
196 lines (147 loc) · 5.85 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// ----------------------------------------------------------------------
// Função para obter uso de CPU
// ----------------------------------------------------------------------
float get_cpu_usage() {
static long prev_total = 0, prev_idle = 0;
long user, nice, system, idle, iowait, irq, softirq, steal;
long total, total_diff, idle_diff;
FILE *f = fopen("/proc/stat", "r");
if (!f) return -1;
fscanf(f, "cpu %ld %ld %ld %ld %ld %ld %ld %ld",
&user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal);
fclose(f);
total = user + nice + system + idle + iowait + irq + softirq + steal;
total_diff = total - prev_total;
idle_diff = idle - prev_idle;
prev_total = total;
prev_idle = idle;
if (total_diff == 0) return 0;
return (100.0 * (total_diff - idle_diff) / total_diff);
}
// ----------------------------------------------------------------------
// Função para obter RAM total e livre
// ----------------------------------------------------------------------
void get_memory_usage(long *total, long *free) {
char label[32], unit[8];
long value;
*total = *free = 0;
FILE *f = fopen("/proc/meminfo", "r");
if (!f) return;
while (fscanf(f, "%31s %ld %7s", label, &value, unit) != EOF) {
if (strcmp(label, "MemTotal:") == 0) *total = value;
if (strcmp(label, "MemAvailable:") == 0) *free = value;
if (*total > 0 && *free > 0) break;
}
fclose(f);
}
// ----------------------------------------------------------------------
// Função para obter I/O de disco
// ----------------------------------------------------------------------
void get_disk_io(long *read, long *write) {
char device[32];
long reads, read_sectors, writes, write_sectors;
*read = *write = 0;
FILE *f = fopen("/proc/diskstats", "r");
if (!f) return;
while (fscanf(f, "%*d %*d %31s %ld %*d %ld %*d %ld %*d %ld",
device, &reads, &read_sectors, &writes, &write_sectors) != EOF) {
if (strcmp(device, "sda") == 0 || strcmp(device, "nvme0n1") == 0) {
*read = read_sectors * 512;
*write = write_sectors * 512;
break;
}
}
fclose(f);
}
// ----------------------------------------------------------------------
// Número de núcleos
// ----------------------------------------------------------------------
int get_cpu_cores() {
FILE *f = fopen("/proc/cpuinfo", "r");
if (!f) return -1;
char line[256];
int cores = 0;
while (fgets(line, sizeof(line), f)) {
if (strncmp(line, "processor", 9) == 0) cores++;
}
fclose(f);
return cores;
}
// ----------------------------------------------------------------------
// Uso da GPU NVIDIA (%)
// ----------------------------------------------------------------------
int get_gpu_usage() {
FILE *fp = popen("nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits", "r");
if (!fp) return -1;
int usage = 0;
fscanf(fp, "%d", &usage);
pclose(fp);
return usage;
}
// ----------------------------------------------------------------------
// Temperatura da GPU NVIDIA (°C)
// ----------------------------------------------------------------------
int get_gpu_temp() {
FILE *fp = popen("nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits", "r");
if (!fp) return -1;
int temp = 0;
fscanf(fp, "%d", &temp);
pclose(fp);
return temp;
}
// ----------------------------------------------------------------------
// Potência (Watts) da GPU NVIDIA
// ----------------------------------------------------------------------
float get_gpu_power() {
FILE *fp = popen("nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits", "r");
if (!fp) return -1;
float watts = 0.0f;
fscanf(fp, "%f", &watts);
pclose(fp);
return watts;
}
// ----------------------------------------------------------------------
// PROGRAMA PRINCIPAL
// ----------------------------------------------------------------------
int main() {
printf("Monitor completo do sistema (CPU, RAM, Disco, Cores, GPU)\n");
long mem_total, mem_free;
long disk_read_prev = 0, disk_write_prev = 0;
int cores = get_cpu_cores();
while (1) {
float cpu = get_cpu_usage();
get_memory_usage(&mem_total, &mem_free);
long disk_read, disk_write;
get_disk_io(&disk_read, &disk_write);
long read_diff = disk_read - disk_read_prev;
long write_diff = disk_write - disk_write_prev;
disk_read_prev = disk_read;
disk_write_prev = disk_write;
// Comentar essas linhas se não tiver GPU NVIDIA ou nvidia-smi instalado
int gpu = get_gpu_usage();
int gpu_temp = get_gpu_temp();
float gpu_power = get_gpu_power();
system("clear");
printf("=== MONITOR DE SISTEMA ===\n\n");
printf("CPU: %.2f%%\n", cpu);
printf("Núcleos: %d\n\n", cores);
printf("RAM Total: %ld MB\n", mem_total / 1024);
printf("RAM Livre: %ld MB\n", mem_free / 1024);
printf("Uso da RAM: %.2f%%\n\n",
100.0 * (mem_total - mem_free) / mem_total);
printf("Disco (sda/nvme0n1):\n");
printf("Leitura: %ld KB/s\n", read_diff / 1024);
printf("Escrita: %ld KB/s\n\n", write_diff / 1024);
// Comentar esse bloco se não tiver GPU NVIDIA ou nvidia-smi instalado
printf("GPU (RTX):\n");
printf("Uso: %d%%\n", gpu);
printf("Temperatura: %d°C\n", gpu_temp);
printf("Consumo: %.2f Watts\n", gpu_power);
sleep(1);
}
return 0;
}