-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.cpp
More file actions
267 lines (237 loc) · 7.96 KB
/
Copy pathmonitor.cpp
File metadata and controls
267 lines (237 loc) · 7.96 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
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
#include <cstdio>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <ncurses.h>
#include <sstream>
#include <string>
#include <unistd.h>
#include <vector>
#include <algorithm>
using namespace std;
struct tot_use_ava { //struct to storage memory
double total;
double used;
double available;
};
unsigned long int convert_str_int(const string text) {
unsigned long int final = 0;
for (unsigned short i = 0; i < text.length(); i++) {
final = final * 10 + (text[i] - 48);
}
return final;
}
string search_int_in_str(const string text) {
unsigned short start = 0, end = text.length();
for (unsigned long int i = 0; i < text.length(); i++) {
if (text[i] < 58 and text[i] > 47 and start == 0) {
start = i;
} else if ((text[i] > 58 or text[i] < 47) and start != 0) {
end = i;
break;
}
}
return text.substr(start, end - start);
}
void global_memory(tot_use_ava &storage) {
ifstream in("/proc/meminfo");
string line;
getline(in, line);
getline(in, line);
getline(in, line);
storage.available =
convert_str_int(search_int_in_str(line)) / 1048576.0;
storage.used = storage.total - storage.available;
}
struct cpu {
unsigned long int total;
unsigned long int work;
};
cpu cpu_usage() {
ifstream in("/proc/stat");
string line;
getline(in, line);
unsigned short count = 5;
unsigned long int total = 0;
unsigned int work = 0;
unsigned char count_som = 0;
while (count < line.length()) {
unsigned short end = line.length() - 1;
if (line.find(' ', count) != string::npos) {
end = line.find(' ', count);
}
unsigned long int novo =
convert_str_int(line.substr(count, end - count));
total += novo;
if (count_som < 3) {
work += novo;
count_som++;
}
count = end + 1;
}
return {total, work};
}
void browser_search(const vector<string> &names, vector<double> &storage,
vector<unsigned long int> &uptime,
vector<unsigned long int> &stime) {
DIR *dir = opendir("/proc");
struct dirent *entrada;
while ((entrada = readdir(dir)) != NULL) {
if (entrada->d_name[0] >= '0' and entrada->d_name[0] <= '9') {
string entrada_str = entrada->d_name;
entrada_str = "/proc/" + entrada_str + '/';
ifstream in(entrada_str + "cmdline");
string line;
getline(in, line);
for (unsigned short i = 0; i < names.size(); i++) {
if (line.find(names[i]) != string::npos) {
ifstream in2(entrada_str + "smaps_rollup");
getline(in2, line);
getline(in2, line);
getline(in2, line);
storage[i] += convert_str_int(search_int_in_str(line));
ifstream in3(entrada_str + "stat");
getline(in3, line);
line = line.substr(line.find(')') + 4);
istringstream iss(line);
string totem;
for (unsigned short i2 = 0; i2 < 11; i2++) {
iss >> totem;
}
uptime[i] += convert_str_int(totem);
iss >> totem;
stime[i] += convert_str_int(totem);
// while (getline(in2, line)){
// if (line.find("Pss:") != string::npos) {
// total.ram[i] +=
// convert_str_int(search_int_in_str(line)); break;}
// }
break;
}
}
}
}
closedir(dir);
}
int main() {
tot_use_ava storage;
ifstream in("/proc/meminfo");
string line;
getline(in, line);
storage.total = convert_str_int(search_int_in_str(line)) / 1048576.0;
in.close();
line.clear();
printf("\n=====================<Monitor>====================\n\n");
//Choose whish browsers you want to analyze
const string names[] = {"firefox", "chrome", "chromium", "brave"};
vector<unsigned short> names_to_use;
printf("Browsers disponiveis:\n");
for (unsigned short i = 0; i < 4; i++) {
printf(" %d. %s\n", i, names[i].c_str());
}
printf("Press the numbers of the browsers you want to add:\n\n");
vector<string> nomes_a_procurar;
while (true) {
char x;
printf("Number(press . to exit):");
cin >> x;
cin.ignore();
if (x == '.') {
break;
}
if (x < '0' or x > '3') {
printf("ERROR! INVALID VALUE\n");
} else {
if(find(names_to_use.begin(), names_to_use.end(), x - 48) != names_to_use.end()){printf("ERROR! ALREADY ADDED! TRY AGAIN\n");}
else {nomes_a_procurar.push_back(names[x - 48]);names_to_use.push_back(x-48);}
}
}
//add browsers whish are not in the list
printf("\nDo you want to add a browser that is not in the list? (Y/N)");
char x;
cin >>x;
cin.ignore();
while (x != 'N' and x != 'n'){
if (x == 'Y' or x == 'y'){
string new_browser;
printf("Name of the new browser:");
cin >> new_browser;
cin.ignore();
nomes_a_procurar.push_back(new_browser);
}
else{printf("VALUE ERROR\nTry again");}
printf("Do you want to add another? (Y/N)");
cin >>x;
cin.ignore();
}
vector<string> nomes_a_procurar_titulo;
for (const string &str: nomes_a_procurar){
string new_str = "";
for (unsigned long int i = 0; i < str.length(); i++){
if (i == 0){new_str.push_back(str[i] - 'a' + 'A');}
else{new_str.push_back(str[i]);}
}
nomes_a_procurar_titulo.push_back(new_str);
}
initscr();
nodelay(stdscr, TRUE);
box(stdscr, 0, 0);
vector<double> browsers_ram(nomes_a_procurar.size());
vector<unsigned long int> new_uptime(nomes_a_procurar.size());
vector<unsigned long int> last_uptime(nomes_a_procurar.size());
vector<unsigned long int> new_stime(nomes_a_procurar.size());
vector<unsigned long int> last_stime(nomes_a_procurar.size());
cpu last_cpu = cpu_usage();
while (true) {
clear();
unsigned short contage_linhas = 1;
//print memory
global_memory(storage);
mvprintw(contage_linhas++, 2, "Memory");
mvprintw(contage_linhas++, 4, "Total : %8.2f GB", storage.total);
mvprintw(contage_linhas++, 4, "Available : %8.2f GB Press 'q' to leave", storage.available);
mvprintw(contage_linhas, 4, "Used : %8.2f GB", storage.used);
contage_linhas += 2;
mvprintw(contage_linhas++, 4, "RAM Usage:%6.2f%%",storage.used / storage.total * 100.0);
mvaddch(contage_linhas, 4, '[');
int preenchimento = storage.used / storage.total * 50.0;
for (int i = 0; i < 50; i++) {mvaddch(contage_linhas, 5 + i, i < preenchimento ? '#' : ' ');}
mvaddch(contage_linhas, 5 + 50, ']');
contage_linhas += 2;
//print CPU
mvprintw(contage_linhas++, 2, "CPU");
cpu current_cpu = cpu_usage();
double cpu_percent = (current_cpu.work - last_cpu.work) * 100.0 /
(current_cpu.total - last_cpu.total);
mvprintw(contage_linhas++, 4, "CPU Usage: %6.2f%%", (cpu_percent));
mvaddch(contage_linhas, 4, '[');
preenchimento = cpu_percent * 0.5;
for (int i = 0; i < 50; i++) {mvaddch(contage_linhas, 5 + i, i < preenchimento ? '#' : ' ');}
mvaddch(contage_linhas, 5 + 50, ']');
contage_linhas+=2;
//print Browsers
mvprintw(contage_linhas++, 2, "Browsers");
browsers_ram.resize(nomes_a_procurar.size());
browser_search(nomes_a_procurar, browsers_ram, new_uptime,new_stime);
for (unsigned short i = 0; i < nomes_a_procurar.size(); i++) {
mvprintw(contage_linhas++, 4, "%-15s RAM: %7.2f Gb CPU: %5.1f%%",
nomes_a_procurar_titulo[i].c_str(), browsers_ram[i] / 1048576,
((new_uptime[i] - last_uptime[i]) + (new_stime[i] - last_stime[i])) *
100.0 / (current_cpu.total - last_cpu.total));
browsers_ram[i] = 0;
last_uptime[i] = new_uptime[i];
last_stime[i] = new_stime[i];
new_uptime[i] = 0;
new_stime[i] = 0;
}
//search if the user wants to leave
refresh();
int ch = getch();
if (ch == 'q' || ch == 'Q') {
endwin();
exit(0);
}
sleep(1);
last_cpu = current_cpu;
}
}