-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteSysMonitor.cpp
More file actions
262 lines (241 loc) · 8.82 KB
/
RemoteSysMonitor.cpp
File metadata and controls
262 lines (241 loc) · 8.82 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
/*
* Example for using LetMeSeeSee
*
* Remote system monitor
*
* Crated at 2019-02-22 by zxcpyp
*/
// Add the LetMeSeeSee header
#include <LetMeSeeSee/storager.h>
#include "lib/sys/cmdparser.h"
#include "lib/sys/time.h"
#include "lib/io/file.h"
std::string datadir; // Savedata directory
pylib::Clock timer; // Global clock
// ----- Function Prototypes for use -----
void print_usage();
std::string get_process_info();
std::string get_cpu_info();
std::string get_memory_info();
// ---------------------------------------
// ----- Here add the store function -----
Store(Process_Info, process_info.csv) {
std::string SaveFile = datadir + "/process_info.csv";
pylib::WriteFile(SaveFile, get_process_info());
return SaveFile;
}
Store(CPU_Info, cpu_info.txt) {
std::string SaveFile = datadir + "/cpu_info.txt";
pylib::WriteFile(SaveFile, get_cpu_info());
return SaveFile;
}
Store(Memory_Info, memory_info.txt) {
std::string SaveFile = datadir + "/memory_info.txt";
pylib::WriteFile(SaveFile, get_memory_info());
return SaveFile;
}
// ---------------------------------------
// -------------- Your Code --------------
int main(int argc, char **argv) {
// Use the storager
lmss::Storager storager;
// Handle arguments
std::string srcfile, logfile, addr, port, password;
int local = 0;
int freq = 1;
pylib::CmdParser parser(argc, argv);
while (parser.NowPos() < parser.OperateNum()) {
std::queue<std::string> arg_list;
std::string op = parser.Next(arg_list);
if (op == "source" || op == "s")
srcfile = arg_list.front();
else if (op == "datadir" || op == "d")
datadir = arg_list.front();
else if (op == "addr" || op == "a")
addr = arg_list.front();
else if (op == "port" || op == "p")
port = arg_list.front();
else if (op == "logfile" || op == "l")
logfile = arg_list.front();
else if (op == "password" || op == "pd")
password = arg_list.front();
else if (op == "local") {
local = 1;
freq = std::stoi(arg_list.front());
}
else {
print_usage();
exit(0);
}
}
// Required data
if (srcfile.empty() || datadir.empty() || port.empty()) {
print_usage();
exit(0);
}
if (addr.empty()) {
addr = "0.0.0.0";
}
if (!logfile.empty()) {
storager.SetLogFile(logfile);
}
if (!password.empty()) {
storager.SetPassword(password);
}
// Specify the source directory
storager.ScanSourceCode(srcfile);
// Start the server
storager.ListenAndServe(addr, std::stoi(port));
pylib::Clock timer;
while (true) {
if (local == 1) {
pylib::WriteFile(datadir + "/process_info.csv", get_process_info());
pylib::WriteFile(datadir + "/cpu_info.txt", get_cpu_info());
pylib::WriteFile(datadir + "/memory_info.txt", get_memory_info());
}
sleep(freq);
}
}
void print_usage() {
pylib::println("Usage: ./RemoteSM [Options]");
pylib::println("Options:");
pylib::println(" --source -s <dir> : Specify source directory [Required]");
pylib::println(" --datadir -d <dir> : Specify the savedata directory [Required]");
pylib::println(" --addr -a <addr> : Specify the listen address (The default is 0.0.0.0)");
pylib::println(" --port -p <port> : Specify the savedata directory [Required]");
pylib::println(" --logfile -l <file> : Specify the log output directory");
pylib::println(" --password -pd <file> : Specify the log output directory");
pylib::println(" --local <freq> : Store data by frequency");
}
std::string get_process_info() {
std::string process_data = "PID,Name,PPID,State,Priority,Memory Use\n";
pylib::OpenDir("/proc").Traverse([&process_data](pylib::FileInfo &&info) {
// Judge process file
if (info.Name()[0] >= '0' && info.Name()[0] <= '9') {
auto stat = pylib::split(pylib::OpenFile("/proc/" + info.Name() + "/stat", false, O_RDONLY).Read(1024), " ");
if (!stat[0].empty()) {
std::string line = stat[0] + ","
+ stat[1].substr(1, stat[1].size() - 2) + ","
+ stat[3] + ","
+ stat[2] + ","
+ stat[17] + ",";
auto buf = new char[32];
if (std::stoi(stat[23]) > 1024) {
double MemUse = std::stoi(stat[23]) / 1024.0;
sprintf(buf, "%.1f MB", MemUse);
}
else if (std::stoi(stat[23]) > 0) {
sprintf(buf, "%d.0 KB", std::stoi(stat[23]));
}
else {
sprintf(buf, "0");
}
line += std::string(buf) + "\n";
delete[] buf;
process_data += line;
}
}
});
return process_data;
}
std::string get_cpu_info() {
std::string cpu_data = "";
static int flag = 0;
static double cpu_ratio = 0;
int user, nice, system, idle, iowait, irq, softirq;
double total, idle_diff, total_diff;
static double old_idle, old_total;
auto buf = new char[64];
timer.Refresh();
sprintf(buf, "Time: %d-%d-%d %02d:%02d:%02d\n",
timer.getyear(), timer.getmonth(), timer.getday(), timer.gethour(), timer.getminute(), timer.getsecond());
cpu_data += std::string(buf);
// Get CPU information
auto cpu = pylib::split(pylib::split(pylib::OpenFile("/proc/cpuinfo", false, O_RDONLY).Read(4096), "\n\n")[0], "\n");
for (int i = 0; i < cpu.size(); i++) {
if (cpu[i].find("model name") != std::string::npos)
cpu_data += " CPU Name: " + cpu[i].substr(cpu[i].rfind(":") + 2) + "\n";
else if (cpu[i].find("cpu MHz") != std::string::npos)
cpu_data += " CPU MHz: " + cpu[i].substr(cpu[i].rfind(":") + 2) + "\n";
else if (cpu[i].find("cache size") != std::string::npos)
cpu_data += " Cache size: " + cpu[i].substr(cpu[i].rfind(":") + 2) + "\n";
else if (cpu[i].find("cpu cores") != std::string::npos)
cpu_data += " CPU Cores: " + cpu[i].substr(cpu[i].rfind(":") + 2) + "\n";
else if (cpu[i].find("address sizes") != std::string::npos)
cpu_data += "Addressing digit: " + cpu[i].substr(cpu[i].rfind(":") + 2) + "\n";
}
// Get CPU use ratio
auto cpuUse = pylib::split(pylib::OpenFile("/proc/stat", false, O_RDONLY).Read(4096), "\n");
auto cpu_use_data = pylib::split(cpuUse[0], " ");
user = std::stoi(cpu_use_data[2]);
nice = std::stoi(cpu_use_data[3]);
system = std::stoi(cpu_use_data[4]);
idle = std::stoi(cpu_use_data[5]);
iowait = std::stoi(cpu_use_data[6]);
irq = std::stoi(cpu_use_data[7]);
softirq = std::stoi(cpu_use_data[8]);
// First time
if (flag == 0) {
flag = 1;
old_idle = idle / cpuUse.size();
old_total = (user + nice + system + idle + iowait + irq + softirq) / cpuUse.size();
cpu_ratio = 0;
}
else {
total = (user + nice + system + idle + iowait + irq + softirq) / cpuUse.size();
total_diff = total - old_total;
idle_diff = idle / cpuUse.size() - old_idle;
cpu_ratio = 100 * (total_diff - idle_diff) / total_diff;
total = old_total;
idle = old_idle;
}
sprintf(buf, " CPU Use: %.2f%%\n", cpu_ratio);
cpu_data += std::string(buf) + "\n";
delete[] buf;
return cpu_data;
}
std::string get_memory_info() {
std::string mem_data = "";
int mem_total, mem_free, mem_use;
int swap_total, swap_free, swap_use;
auto buf = new char[64];
timer.Refresh();
sprintf(buf, "Time: %d-%d-%d %02d:%02d:%02d\n",
timer.getyear(), timer.getmonth(), timer.getday(), timer.gethour(), timer.getminute(), timer.getsecond());
mem_data += std::string(buf);
auto MemUse = pylib::split(pylib::OpenFile("/proc/meminfo", false, O_RDONLY).Read(4096), "\n");
for (int i = 0; i < MemUse.size(); i++) {
if (MemUse[i].find("MemTotal") != std::string::npos) {
auto tmp = pylib::split(MemUse[i], " ");
mem_total = std::stoi(tmp[tmp.size() - 2]);
}
if (MemUse[i].find("MemAvailable") != std::string::npos) {
auto tmp = pylib::split(MemUse[i], " ");
mem_free = std::stoi(tmp[tmp.size() - 2]);
}
if (MemUse[i].find("SwapTotal") != std::string::npos) {
auto tmp = pylib::split(MemUse[i], " ");
swap_total = std::stoi(tmp[tmp.size() - 2]);
}
if (MemUse[i].find("SwapFree") != std::string::npos) {
auto tmp = pylib::split(MemUse[i], " ");
swap_free = std::stoi(tmp[tmp.size() - 2]);
}
}
mem_use = mem_total - mem_free;
swap_use = swap_total - swap_free;
sprintf(buf, " Memory Use: %.1f MB\n", mem_use / 1024.0);
mem_data += std::string(buf);
sprintf(buf, "Memory Total: %.1f MB\n", mem_total / 1024.0);
mem_data += std::string(buf);
sprintf(buf, " Memory Use: %.2f%%\n\n", 100 * (double)mem_use / mem_total);
mem_data += std::string(buf);
sprintf(buf, " Swap Use: %.1f MB\n", swap_use / 1024.0);
mem_data += std::string(buf);
sprintf(buf, " Swap Total: %.1f MB\n", swap_total / 1024.0);
mem_data += std::string(buf);
sprintf(buf, " Swap Use: %.2f%%\n", 100 * (double)swap_use / swap_total);
mem_data += std::string(buf);
delete[] buf;
return mem_data;
}