-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_monitory_tool_shaaf.c
429 lines (374 loc) · 12.6 KB
/
system_monitory_tool_shaaf.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#include <poll.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <unistd.h>
int memory(int sam, long delay, int row_main, int col_main, bool run) {
FILE *memfile;
memfile = fopen("/proc/meminfo", "r");
if (memfile == NULL) {
fprintf(stderr, "File Empty");
return 1;
}
long infomem [5];
long memtotal, memfree, memava, membuf, memcach;
fscanf(memfile, "MemTotal: %ld kB", &memtotal);
fgetc(memfile);
fscanf(memfile, "MemFree: %ld kB", &memfree);
fgetc(memfile);
fscanf(memfile, "MemAvailable: %ld kB", &memava);
fgetc(memfile);
fscanf(memfile, "Buffers: %ld kB", &membuf);
fgetc(memfile);
fscanf(memfile, "Cached: %ld kB", &memcach);
double mem_util_total = memtotal /1024.0 /1024.0;
double mem_util_available = memava /1024.0 /1024.0;
double mem_util_used = mem_util_total - mem_util_available;
fclose(memfile);
double display_ram = ((((int)mem_util_total/10)*10)+10);
int row = (mem_util_used/ display_ram) *10;
// This sections runs until the program ends to update the graph with new values
if(run == true) {
int location = 11;
printf("\x1B[%dG", 0);
printf("> Memory usage: %.2f GB\n", mem_util_used);
location--;
printf("\x1B[%dB", 10);
location -= 10;
if(row_main != 0){
printf("\x1B[%dA", row_main);
location += row_main;
}
if (location == 0) {
printf("\x1B[%dG", col_main - 1);
printf("_");
} else {
printf("\x1B[%dG", col_main);
}
if(row_main == row){
printf("#");
fflush(stdout);
} else if (row_main < row){
printf("\x1B[%dA", row - row_main);
printf("#");
fflush(stdout);
} else if (row_main > row){
printf("\x1B[%dB", row_main - row);
printf("#");
fflush(stdout);
}
}
// This Runs only once (to draw graph and set initial values)
if (run == false) {
printf("\x1B[%dG", 0);
printf("> Memory usage: %.2f GB\n", mem_util_used);
printf("\n");
printf("%.0f GB |\n", display_ram);
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" 0 GB |");
int samplespast = 0;
while(samplespast < sam) {
printf("_");
samplespast += 1;
}
if(row != 0) {printf("\x1B[%dA", row);}
printf("\x1B[%dG", col_main);
printf("#");
fflush(stdout);
}
return row;
}
int usecpu(int sam, long delay, int* row_main, int col_main, bool run, long* prev_total, long* prev_idle) {
long user, nice2, sys, idle, iowait, irq, softirq;
FILE *cpufile;
cpufile = fopen("/proc/stat", "r");
if (cpufile == NULL) {
fprintf(stderr, "Cpu Cores File Empty");
return 1;
}
fscanf(cpufile, "cpu %ld %ld %ld %ld %ld %ld %ld", &user, &nice2, &sys, &idle, &iowait, &irq, &softirq);
long total_now = user + nice2 + sys + idle + iowait + irq + softirq;
long change_total = total_now - *prev_total;
long change_idle = idle - *prev_idle;
double cpu_util = 100 * (1.0 - ((double)change_idle / (double)change_total));
int graph_cpu = cpu_util / 10;
int row = graph_cpu;
int i = 0;
// This sections runs until the program ends to update the graph with new values
if(run == true) {
int c_row = abs(row - *row_main);
int location = 12;
printf("\x1B[%dG", 0);
printf("> CPU utilization: %.2f %%\n", cpu_util);
location--;
printf("\x1B[%dB", 11);
location -= 11;
printf("\x1B[%dA", *row_main);
location += *row_main;
if(location == 0) {
printf("\x1B[%dG", col_main - 1);
printf("_");
} else {
printf("\x1B[%dG", col_main);
}
if(*row_main == row){
printf(":");
fflush(stdout);
} else if (*row_main < row){
if(location + c_row >= 10) {
printf(":");
fflush(stdout);
} else {
printf("\x1B[%dA", c_row);
printf(":");
fflush(stdout);
}
} else if (*row_main > row){
if(location - c_row < 1) {
printf(":");
fflush(stdout);
} else {
printf("\x1B[%dB", c_row);
printf(":");
fflush(stdout);
}
}
}
// This Runs only once (to draw graph and set initial values)
if (run == false) {
printf("\x1B[%dG", 0);
printf("> CPU utilization: %.2f %%\n", cpu_util);
printf("\n");
printf("100 %% |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" 0 %% |");
int samplespast = 0;
while(samplespast < sam) {
printf("_");
samplespast += 1;
}
if(row != 0){printf("\x1B[%dA", row);}
if((row - *row_main) == 10){printf("\x1B[%dB", 1);}
printf("\x1B[%dG", col_main);
printf(":");
fflush(stdout);
}
fclose(cpufile);
*prev_total = total_now;
*prev_idle = idle;
*row_main = row;
return 0;
}
int manycores(int last_sample, int current_sample) {
if ((last_sample + 1) == current_sample){
long maxfreqcpu;
FILE *cpufreq;
cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq","r");
if (cpufreq == NULL) {
fprintf(stderr, "Cpu Max Frequency File Empty");
}
fscanf(cpufreq, "%ld", &maxfreqcpu);
FILE *coresfile;
coresfile = fopen("/proc/cpuinfo", "r");
if (coresfile == NULL) {
fprintf(stderr, "File Empty");
return 1;
}
char buffer[40];
int num_cores = 0;
double clock_rate = maxfreqcpu / 1000000.0;
while (fgets(buffer, sizeof(buffer), coresfile) != NULL) {
if (strncmp(buffer, "processor", 9) == 0) {
char *colon = strchr(buffer, ':');
if (colon != NULL) {
int procNum = atoi(colon + 1);
num_cores = procNum;
}
}
}
num_cores += 1;
printf("\x1B[%dG", 0);
printf("> Number of Cores: %d @ %.2f GHz\n", num_cores, clock_rate);
int printed = 0;
int lines = 0;
int remaining = 0;
while(printed < num_cores) {
remaining = num_cores - printed;
if(remaining >=4) {
printf("+----+ +----+ +----+ +----+\n");
printf("| | | | | | | |\n");
printf("+----+ +----+ +----+ +----+\n");
printf("\n");
printed += 4;
} else if(remaining == 3){
printf("+----+ +----+ +----+\n");
printf("| | | | | |\n");
printf("+----+ +----+ +----+\n");
printed += 3;
} else if(remaining == 2){
printf("+----+ +----+\n");
printf("| | | |\n");
printf("+----+ +----+\n");
printed += 2;
} else if(remaining == 1){
printf("+----+\n");
printf("| |\n");
printf("+----+\n");
printed += 1;
}
lines++;
}
fclose(coresfile);
fclose(cpufreq);
return lines;
}
}
int main(int argc, char **argv) {
int i = 1;
int samples = 20; // Samples default value = 20
long delay = 500000; // tdelay default value = 0.5 seconds
bool cpu = false;
bool mem = false;
bool cores = false;
bool pos_arg = false;
printf("\x1B[2J");
// The following code checks for the positional samples and tdelay
// arguments. If found, the code will ignore --samples=N and --tdelay=T.
if (1 < argc && isdigit(argv[1][0])) {
samples = (int)strtol(argv[1], NULL, 10);
pos_arg = true;
if (2 < argc && isdigit(argv[2][0])) {
delay = (int)strtol(argv[2], NULL, 10);
}
}
// The following code compares the command line arguments with any known
// arguments. Any extra/unknown commands by the user are ignored for
// ease of user experience. (it gets annoying when you mistype something)
while(i < argc && argc > 0) {
if(strncmp("--nsamples=", argv[i], 10) == 0) {
if(pos_arg == false) {
strtok(argv[i], "=");
char *substring = strtok(NULL, "=");
if(substring != NULL) {
samples = strtol(substring, NULL, 10);
}
}
}
if(strncmp("--ninterval=", argv[i], 9) == 0) {
if(pos_arg == false) {
strtok(argv[i], "=");
char *substring = strtok(NULL, "=");
if(substring != NULL) {
delay = strtol(substring, NULL, 10);
}
}
}
if(strcmp("--umem", argv[i]) == 0) {
mem = true;
}
if(strcmp("--ucpu", argv[i]) == 0) {
cpu = true;
}
if(strcmp("--ucores", argv[i]) == 0) {
cores = true;
}
i++;
}
// We open cpu file to sample the first cpu readings
FILE *cpufile;
cpufile = fopen("/proc/stat", "r");
if (cpufile == NULL) {
fprintf(stderr, "File Empty");
return 1;
}
long user, nice2, sys, idle = 0, iowait, irq, softirq;
long total = 0;
long idle_passed;
bool did_we_run = false;
long milidelay = delay/1000;
int col_main = 10;
int time = 0;
int row_mem = 0;
int row_cpu = 0;
int lines = 0;
printf("\x1B[H");
printf("Nbr of samples: %d -- every %ld microSecs(%.1f secs)", samples, delay, (double)delay/1000000.0);
printf("\x1B[2B");
printf("\x1B[s");
// if use selects all functions to be displayed, we default them
// to all false, because of the way I coded
if(mem == true && cpu == true && cores == true) {
mem = false;
cpu = false;
cores = false;
}
// The following code decides what components are to be displayed
// The following code is also responsible for updating the values on a program
while(time < samples) {
// Now we check to run all diagnostics
if(mem == false && cpu == false && cores == false) {
//run all commands if values untouched
printf("\x1B[u");
row_mem = memory(samples, delay, row_mem, col_main + time, did_we_run);
printf("\x1B[u");
printf("\x1B[13B");
usecpu(samples, delay, &row_cpu, col_main + time, did_we_run, &total, &idle_passed);
printf("\x1B[u");
printf("\x1B[13B");
printf("\x1B[13B");
lines = manycores(time, samples);
}
if (mem == true) {
printf("\x1B[u");
row_mem = memory(samples, delay, row_mem, col_main + time, did_we_run);
} if (cpu == true) {
printf("\x1B[u");
if(mem == true) {printf("\x1B[13B");}
usecpu(samples, delay, &row_cpu, col_main + time, did_we_run, &total, &idle_passed);
} if (cores == true) {
printf("\x1B[u");
if(mem == true || cpu == true) {printf("\x1B[13B");}
else {time = 19;}
lines = manycores(time, samples);
}
time += 1;
did_we_run = true;
col_main += 0.9;
poll(NULL, 0, milidelay);
}
fclose(cpufile);
// The following code decides the final cursor position after
// all functions have been run.
printf("\x1b[u");
printf("\x1B[%dG", 0);
lines = lines*4;
if(mem == false && cpu == false && cores == false) {
printf("\x1b[%dB", 26 + lines + 1);
}
if (mem == true) {
printf("\x1b[13B");
} if (cpu == true) {
printf("\x1b[13B");
} if (cores == true) {
printf("\x1b[%dB", lines + 1);
}
return 0;
}