forked from tiwilliam/istatd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isr.cpp
292 lines (226 loc) · 8.15 KB
/
isr.cpp
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
/*
* Copyright 2010 William Tisäter. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The name of the copyright holder may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL WILLIAM TISÄTER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <vector>
#include <sstream>
#include <string.h>
#include <iostream>
#include "isr.h"
#include "stats.h"
#include "utility.h"
using namespace std;
string isr_create_header()
{
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
}
string isr_create_session(int _rid, int _ds, int _ts, int _fs)
{
stringstream temp;
temp << "<isr ds=\"" << _ds << "\" ts=\"" << _ts << "\" fs=\"" << _fs << "\" rid=\"" << _rid << "\">";
return temp.str();
}
string isr_accept_code()
{
stringstream temp;
temp << isr_create_header() << "<isr ready=\"1\"></isr>";
return temp.str();
}
string isr_reject_code()
{
stringstream temp;
temp << isr_create_header() << "<isr athrej=\"1\"></isr>";
return temp.str();
}
string isr_conntest()
{
stringstream temp;
temp << isr_create_header() << "<isr></isr>";
return temp.str();
}
string isr_accept_connection(int _ath, int _ss, int _c, int _n)
{
stringstream temp;
temp << isr_create_header() << "<isr pl=\"2\" ath=\"" << _ath << "\" ss=\"" << _ss << "\" c=\"" << _c << "\" n=\"" << _n << "\"></isr>";
return temp.str();
}
string isr_cpu_data(vector<sys_info> * _history, int _init)
{
int uptime;
sys_info prev;
stringstream temp;
int diff_u, diff_s, diff_n, diff_i;
int load_t, load_u, load_s, load_n;
if(0 == _history->size()) return temp.str();
temp << "<CPU>";
for (vector<sys_info>::iterator cur = _history->begin() + 1; cur != _history->end(); ++cur)
{
uptime = cur->upt;
if (_init == -1)
{
cur = _history->end() - 1;
uptime = -1;
}
cur--; prev = (*cur); cur++;
diff_u = cur->cpu.u - prev.cpu.u;
diff_s = cur->cpu.s - prev.cpu.s;
diff_n = cur->cpu.n - prev.cpu.n;
diff_i = cur->cpu.i - prev.cpu.i;
load_t = (int) (diff_u + diff_s + diff_n + diff_i);
load_u = (int) (((float) diff_u / load_t) * 100);
load_s = (int) (((float) diff_s / load_t) * 100);
load_n = (int) (((float) diff_n / load_t) * 100);
temp << "<c id=\"" << uptime << "\" u=\"" << load_u << "\" s=\"" << load_s << "\" n=\"" << load_n << "\"></c>";
}
temp << "</CPU>";
return temp.str();
}
string isr_network_data(vector<net_info> * _history, int _init)
{
stringstream temp;
if(0 == _history->size()) return temp.str();
for (vector<net_info>::iterator cur = _history->begin(); cur != _history->end(); ++cur)
{
if ((*cur).active == false) continue;
temp << "<NET if=\"" << cur->id << "\">";
for (vector<net_data>::iterator curhis = cur->history.begin(); curhis != cur->history.end(); ++curhis)
{
if (_init == -1)
{
curhis = cur->history.end() - 1;
curhis->upt = -1;
}
temp << "<n id=\"" << curhis->upt << "\" d=\"" << curhis->r << "\" u=\"" << curhis->s << "\" t=\"" << curhis->uxt << "\"></n>";
}
temp << "</NET>";
}
return temp.str();
}
string isr_disk_data(vector<disk_info> * _disks, int _init, const string cf_disk_mount_path_label, const string cf_disk_filesystem_label, vector<string> cf_disk_rename_label)
{
stringstream temp;
vector<string> conf_label;
string disk_label, disk_label_custom, disk_uuid;
int disk_size, disk_percent, disk_usage;
if(0 == _disks->size()) return temp.str();
temp << "<DISKS>";
for (vector<disk_info>::iterator cur = _disks->begin(); cur != _disks->end(); ++cur)
{
if ((*cur).active == false) continue;
disk_uuid = "";
disk_label = "";
disk_label_custom = "";
// Read custom disk label from config
for (vector<string>::iterator cur_label = cf_disk_rename_label.begin(); cur_label != cf_disk_rename_label.end(); ++cur_label)
{
conf_label = explode(*cur_label);
if (conf_label[1].length())
{
if (conf_label[0] == (*cur).device || conf_label[0] == (*cur).name)
{
disk_label_custom = trim(conf_label[1], "\"");
break;
}
}
}
if (to_int(cf_disk_mount_path_label))
disk_label = (*cur).name;
else
disk_label = (*cur).device;
// Set label and uuid if libfslabel was able to retrive data and is configured to do it
if (strlen((*cur).label) && to_int(cf_disk_filesystem_label) == 1)
disk_label = (*cur).label;
// Set custom disk label if configured. Will override everything.
if (disk_label_custom.length())
disk_label = disk_label_custom;
// Use real uuid if we found one with libfslabel
if (strlen((*cur).uuid))
disk_uuid = (*cur).uuid;
else
disk_uuid = (*cur).device;
disk_size = (*cur).history.front().f / 1000;
disk_percent = (*cur).history.front().p;
disk_usage = disk_size * disk_percent / 100;
temp << "<d n=\"" << disk_label << "\" uuid=\"" << disk_uuid << "\" f=\"" << disk_size << "\" p=\"" << disk_percent << "\" u=\"" << disk_usage << "\"></d>";
}
temp << "</DISKS>";
// cout << temp.str() << endl;
return temp.str();
}
string isr_uptime_data(vector<sys_info> * _history)
{
stringstream temp;
if(0 == _history->size()) return temp.str();
temp << "<UPT u=\"" << _history->back().upt << "\"></UPT>";
return temp.str();
}
string isr_loadavg_data(vector<sys_info> * _history)
{
stringstream temp;
if(0 == _history->size()) return temp.str();
temp << "<LOAD one=\"" << _history->back().cpu.one << "\" fv=\"" << _history->back().cpu.two << "\" ff=\"" << _history->back().cpu.three << "\"></LOAD>";
return temp.str();
}
string isr_memory_data(vector<sys_info> * _history)
{
stringstream temp;
if(0 == _history->size()) return temp.str();
temp << "<MEM w=\"" << _history->back().mem.c / 1000 << "\" a=\"" << _history->back().mem.a / 1000 << "\" i=\"" << _history->back().mem.i / 1000 << "\" f=\"" << _history->back().mem.f / 1000 << "\" t=\"" << _history->back().mem.t / 1000 << "\" su=\"" << _history->back().mem.swu / 1000 << "\" st=\"" << _history->back().mem.swt / 1000 << "\" pi=\"" << _history->back().mem.swi << "\" po=\"" << _history->back().mem.swo << "\"></MEM>";
return temp.str();
}
string isr_fan_data(vector<sensor_info> * _data, int _init)
{
stringstream temp;
if(0 == _data->size()) return temp.str();
temp << "<FANS>";
for (vector<sensor_info>::iterator cur = _data->begin(); cur != _data->end(); ++cur)
{
if (cur->data.kind == SENSOR_FAN)
temp << "<f n=\"" << cur->data.label << "\" i=\"" << cur->data.id << "\" s=\"" << cur->data.data << "\"></f>";
}
temp << "</FANS>";
return temp.str();
}
string isr_temp_data(vector<sensor_info> * _data, int _init)
{
stringstream temp;
if(0 == _data->size()) return temp.str();
temp << "<TEMPS>";
for (vector<sensor_info>::iterator cur = _data->begin(); cur != _data->end(); ++cur)
{
if (cur->data.kind == SENSOR_TEMP)
{
temp << "<t ";
if (_init)
temp << "n=\"" << cur->data.label << "\" ";
temp << "i=\"" << cur->data.id << "\" t=\"" << cur->data.data << "\"></t>";
}
}
temp << "</TEMPS>";
return temp.str();
}