hlit-mon  0.5.2
 All Classes Functions Groups Pages
HlitNodeSensor.cpp
1 #include "HlitNodeSensor.h"
2 #include <sstream>
3 using namespace std;
4 HlitNodeSensor::HlitNodeSensor(char* net_list)
5  : fCPU(0)
6  , fMem(0)
7  , fNetList(0)
8  , fNetArray(0)
9 {
10  string to_split = net_list;
11 
12  size_t pos = 0, found;
13  while ((found = to_split.find_first_of(',', pos)) != string::npos) {
14  NetVector.push_back(to_split.substr(pos, found - pos));
15  pos = found + 1;
16  }
17  NetVector.push_back(to_split.substr(pos));
18  vector<string>::iterator it = NetVector.begin();
19  if (strcmp((*it).data(), "all") == 0) {
20  NetVector.clear();
21  }
22 }
23 HlitNodeSensor::~HlitNodeSensor()
24 {
25  delete fCPU;
26  delete fMem;
27 
28  if (fNetArray) {
29  glibtop_netload* net = (glibtop_netload*)zlistx_first(fNetArray);
30  for (unsigned int i = 0; i < fNetList->number; i++) {
31  delete net;
32  net = (glibtop_netload*)zlistx_next(fNetArray);
33  }
34  zlistx_destroy(&fNetArray);
35  }
36  delete fNetList;
37 }
38 
39 void HlitNodeSensor::Init()
40 {
41  fCPU = new glibtop_cpu;
42  fMem = new glibtop_mem;
43  fNetList = new glibtop_netlist;
44  fNetArray = zlistx_new();
45 
46  char** devices = glibtop_get_netlist(fNetList);
47  glibtop_netload* net;
48  for (unsigned int i = 0; i < fNetList->number; i++) {
49  net = new glibtop_netload;
50  zlistx_add_end(fNetArray, net);
51  free(devices[i]);
52  }
53  free(devices);
54 }
55 
56 glibtop_cpu* HlitNodeSensor::GetCpu()
57 {
58  return fCPU;
59 }
60 
61 glibtop_mem* HlitNodeSensor::GetMem()
62 {
63  return fMem;
64 }
65 
66 glibtop_netlist* HlitNodeSensor::GetNetList()
67 {
68  return fNetList;
69 }
70 
71 zlistx_t* HlitNodeSensor::GetNetArray()
72 {
73  return fNetArray;
74 }
75 
76 int HlitNodeSensor::Update()
77 {
78  if (!fCPU)
79  Init();
80 
81  glibtop_get_cpu(fCPU);
82  glibtop_get_mem(fMem);
83 
84  char** devices = glibtop_get_netlist(fNetList);
85  glibtop_netload* net;
86 
87  net = (glibtop_netload*)zlistx_first(fNetArray);
88  for (unsigned int i = 0; i < fNetList->number; i++) {
89  // printf("dev=%s\n", devices[i]);
90  glibtop_get_netload(net, devices[i]);
91  net = (glibtop_netload*)zlistx_next(fNetArray);
92  free(devices[i]);
93  }
94  free(devices);
95 
96  return 0;
97 }
98 
99 void HlitNodeSensor::GetSpeed(HlitNodeSensor* s1, HlitNodeSensor* s2, int timeout)
100 {
101 
102  if (!fCPU)
103  Init();
104 
105  glibtop_cpu* cpu1 = s1->GetCpu();
106  glibtop_cpu* cpu2 = s2->GetCpu();
107 
108  fCPU->total = (cpu2->total - cpu1->total);
109  fCPU->sys = (cpu2->sys - cpu1->sys);
110  fCPU->user = (cpu2->user - cpu1->user);
111  fCPU->nice = (cpu2->nice - cpu1->nice);
112  fCPU->idle = (cpu2->idle - cpu1->idle);
113  fCPU->iowait = (cpu2->iowait - cpu1->iowait);
114  fCPU->irq = (cpu2->irq - cpu1->irq);
115  fCPU->softirq = (cpu2->softirq - cpu1->softirq);
116  fCPU->frequency = (cpu2->frequency - cpu1->frequency);
117 
118  glibtop_mem* mem1 = s1->GetMem();
119  glibtop_mem* mem2 = s2->GetMem();
120 
121  fMem->total = (mem2->total - mem1->total) * 1000 / timeout;
122  fMem->used = (mem2->used - mem1->used) * 1000 / timeout;
123  fMem->free = (mem2->free - mem1->free) * 1000 / timeout;
124  fMem->shared = (mem2->shared - mem1->shared) * 1000 / timeout;
125  fMem->buffer = (mem2->buffer - mem1->buffer) * 1000 / timeout;
126  fMem->cached = (mem2->cached - mem1->cached) * 1000 / timeout;
127  fMem->locked = (mem2->locked - mem1->locked) * 1000 / timeout;
128 
129  // char** devices = glibtop_get_netlist(fNetList);
130  glibtop_netload* net = (glibtop_netload*)zlistx_first(fNetArray);
131  glibtop_netload* net1 = (glibtop_netload*)zlistx_first(s1->GetNetArray());
132  glibtop_netload* net2 = (glibtop_netload*)zlistx_first(s2->GetNetArray());
133  for (unsigned int i = 0; i < fNetList->number; i++) {
134 
135  if (net) {
136  net->address = (net2->address - net1->address) * 1000 / timeout;
137  net->bytes_in = (net2->bytes_in - net1->bytes_in) * 1000 / timeout;
138  net->bytes_out = (net2->bytes_out - net1->bytes_out) * 1000 / timeout;
139  net->errors_in = (net2->errors_in - net1->errors_in) * 1000 / timeout;
140  net->errors_out = (net2->errors_out - net1->errors_out) * 1000 / timeout;
141  net->packets_in = (net2->packets_in - net1->packets_in) * 1000 / timeout;
142  net->packets_out = (net2->packets_out - net1->packets_out) * 1000 / timeout;
143  }
144 
145  net = (glibtop_netload*)zlistx_next(fNetArray);
146  net1 = (glibtop_netload*)zlistx_next(s1->GetNetArray());
147  net2 = (glibtop_netload*)zlistx_next(s2->GetNetArray());
148  }
149 }
150 
151 bool HlitNodeSensor::FindAdapter(vector<string> vctr, string adapter) const
152 {
153  for (vector<string>::iterator it = vctr.begin(); it != vctr.end(); ++it) {
154  if (strcmp((*it).data(), adapter.data()) == 0)
155  return true;
156  }
157  return false;
158 }
159 
160 /* returning JSON data in char* type string */
161 char* HlitNodeSensor::GetJson(const char* name, bool debug) const
162 {
163 
164  string json;
165  double precent = (double)fCPU->total;
166 
167  // start ROOT
168  json += "\"";
169  json += name;
170  json += "\" : {";
171 
172  char* cpu_str = zsys_sprintf("\"cpu\" : { "
173  "\"total\" : %.3f, "
174  "\"sys\" : %.3f, "
175  "\"user\" : %.3f, "
176  "\"nice\" : %.3f, "
177  "\"idle\" : %.3f, "
178  "\"iowait\" : %.3f, "
179  "\"irq\" : %.3f, "
180  "\"softirq\" : %.3f, "
181  "\"frequency\" : %.3f "
182  "}",
183  ((double)fCPU->total / precent) * 100.0, ((double)fCPU->sys / precent) * 100.0, ((double)fCPU->user / precent) * 100.0, ((double)fCPU->nice / precent) * 100.0, ((double)fCPU->idle / precent) * 100.0,
184  ((double)fCPU->iowait / precent) * 100.0, ((double)fCPU->irq / precent) * 100.0, ((double)fCPU->softirq / precent) * 100.0, (double)fCPU->frequency);
185  json += cpu_str;
186  free(cpu_str);
187  // return json.data();
188 
189  // mem
190  json += ", ";
191  char* mem_str = zsys_sprintf("\"mem\" : { "
192  "\"total\" : %lld, "
193  "\"used\" : %lld, "
194  "\"free\" : %lld, "
195  "\"shared\" : %lld, "
196  "\"buffer\" : %lld, "
197  "\"cached\" : %lld, "
198  "\"locked\" : %lld "
199  "}",
200  fMem->total, fMem->used, fMem->free, fMem->shared, fMem->buffer,
201  fMem->cached, fMem->locked);
202  json += mem_str;
203  free(mem_str);
204 
205  // net
206  json += ", ";
207  json += "\"net\" : [";
208  char** devices = glibtop_get_netlist(fNetList);
209  glibtop_netload* net = (glibtop_netload*)zlistx_first(fNetArray);
210  int count = 0;
211  for (unsigned int i = 0; i < fNetList->number; i++) {
212  if (net && (NetVector.size() == 0 || FindAdapter(NetVector, devices[i]) == true)) {
213  if (count != 0)
214  json += ",";
215  char* net_str = zsys_sprintf("{\"adapter\" : \"%s\", "
216  "\"address\" : %lld, "
217  "\"bytes_in\" : %lld, "
218  "\"bytes_out\" : %lld, "
219  "\"errors_in\" : %lld, "
220  "\"errors_out\" : %lld, "
221  "\"packets_in\" : %lld, "
222  "\"packets_out\" : %lld ",
223  devices[i], net->address, net->bytes_in, net->bytes_out,
224  net->errors_in, net->errors_out, net->packets_in, net->packets_out);
225  json += net_str;
226  json += "}";
227  count++;
228  free(net_str);
229  }
230  free(devices[i]);
231  net = (glibtop_netload*)zlistx_next(fNetArray);
232  }
233  free(devices);
234 
235  json += "]";
236 
237  // end ROOT
238  json += "}";
239 
240  if (debug)
241  printf("\n %s", json.data());
242  return zsys_sprintf("%s", json.data());
243 }
244 
245 void HlitNodeSensor::Print() const
246 {
247 
248  printf("Cpu Data : \n");
249  printf(" total : %ld \n", fCPU->total);
250  printf(" user : %ld \n", fCPU->user);
251  printf(" nice : %ld \n", fCPU->nice);
252  printf(" sys: %ld \n", fCPU->sys);
253  printf(" idle : %ld \n", fCPU->idle);
254  printf(" iowait : %ld \n", fCPU->iowait);
255  printf(" irq : %ld \n", fCPU->irq);
256  printf(" softirq : %ld \n", fCPU->softirq);
257  printf(" frequency : %ld \n", fCPU->frequency);
258 
259  printf("Memory Data : \n");
260  printf(" total : %ld \n", fMem->total);
261  printf(" used : %ld \n", fMem->used);
262  printf(" free : %ld \n", fMem->free);
263  printf(" shared : %ld \n", fMem->shared);
264  printf(" buffer : %ld \n", fMem->buffer);
265  printf(" cached : %ld \n", fMem->cached);
266  printf(" user : %ld \n", fMem->user);
267  printf(" locked : %ld \n", fMem->locked);
268 
269  printf("Net Data : %d devices\n", fNetList->number);
270  char** devices = glibtop_get_netlist(fNetList);
271  glibtop_netload* net = (glibtop_netload*)zlistx_first(fNetArray);
272  for (unsigned int i = 0; i < fNetList->number; i++) {
273  if (net && (NetVector.size() == 0 || FindAdapter(NetVector, devices[i]))) {
274  printf(" %s\n", devices[i]);
275  printf(" address : %d\n", net->address);
276  printf(" bytes_in : %ld\n", net->bytes_in);
277  printf(" bytes_out : %ld\n", net->bytes_out);
278  printf(" errors_in : %ld\n", net->errors_in);
279  printf(" errors_out : %ld\n", net->errors_out);
280  printf(" packets_in : %ld\n", net->packets_in);
281  printf(" packets_out : %ld\n", net->packets_out);
282  }
283  free(devices[i]);
284  net = (glibtop_netload*)zlistx_next(fNetArray);
285  }
286  free(devices);
287 }