added to git

This commit is contained in:
2026-08-10 22:27:53 -04:00
commit 815a663a2c
14 changed files with 802 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
#include "readUtil.h"
#include "myMonitoringTool.h"
#include <assert.h>
// Rip an integer out of a string.
int extract_int(char *str) {
///_|> descry: takes an integer out of a string
///_|> str: string to search
///_|> returning: void
char num[LINE_BUF];
int num_idx = 0;
int str_idx = 0;
while (str[str_idx] != '\0') {
if (isdigit(str[str_idx])) {
num[num_idx] = str[str_idx];
num_idx++;
}
str_idx++;
}
num[num_idx] = '\0';
return atoi(num);
}
int get_file_line_value(char *filename, int linenum) {
///_|> descry: Get the integer at a specific line in a value. First line in
/// file is line num
///_|> filename: file to search
///_|> linenum: line to read
///_|> returning: integer thats found
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("could not open %s", filename);
exit(1);
}
char line[LINE_BUF];
for (int i = 0; i <= linenum; i++) {
fgets(line, LINE_BUF, file);
}
fclose(file);
char label[LINE_BUF];
char sep[LINE_BUF];
int value;
char tail[LINE_BUF];
sscanf(line, "%s %s %d %s", label, sep, &value, tail);
return extract_int(line);
}
long read_cpu_util() {
///_|> decry: Read total cpu time usage from /proc/stat
///_|> returning: cpu usage since boot
char dummy[LINE_BUF];
long user;
long nice;
long system;
long idle; // not counted
long io;
long intr;
long sintr;
long steal;
long guest;
long guest_nice;
FILE *file = fopen("/proc/stat", "r");
if (file == NULL) {
printf("could not open /proc/stat");
exit(1);
}
fscanf(file, "%s %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld", dummy, &user,
&nice, &system, &idle, &io, &intr, &sintr, &steal, &guest,
&guest_nice);
fclose(file);
// return value is in 1/100 s
return user + nice + system + io + intr + sintr;
}