obmon  0.0.0
 All Classes Functions Variables Enumerations Groups Pages
ObSensorSystem.cpp
1 #include "ObSensorSystem.h"
2 
3 ObSensorSystem::ObSensorSystem(std::string name) : ObSensor(name) {
7 
8  char **devices = glibtop_get_netlist(&_netlist);
9  glibtop_netload *net;
10  if (_netloads.empty()) {
11  for (unsigned int i = 0; i < _netlist.number; i++) {
12  net = new glibtop_netload;
13  _netloads.push_back(net);
14  _netnames.push_back(devices[i]);
15  free(devices[i]);
16  }
17  free(devices);
18  }
19 }
24 
25  for (auto net : _netloads)
26  delete net;
27 }
28 
33 
34  _first = new ObSensorSystem("value");
35  _second = new ObSensorSystem("value");
36  _change = new ObSensorSystem("change");
37 }
38 
43 
44  _logger->trace("ObSensorSystem::process cpu ...");
45  glibtop_get_cpu(&_cpu);
46  _logger->trace("ObSensorSystem::process mem ...");
47  glibtop_get_mem(&_mem);
48 
49  std::string net_name;
50  int i = 0;
51  for (auto net : _netloads) {
52  net_name = _netnames.at(i++);
53  _logger->trace("ObSensorSystem::process net {}", net_name.data());
54  glibtop_get_netload(net, net_name.data());
55  }
56 }
57 
58 void ObSensorSystem::speed(ObSensor *s1, ObSensor *s2, int timeout) {
62 
63  ObSensorSystem *ss1 = static_cast<ObSensorSystem *>(s1);
64  ObSensorSystem *ss2 = static_cast<ObSensorSystem *>(s2);
65  glibtop_cpu cpu1 = ss1->cpu();
66  glibtop_cpu cpu2 = ss2->cpu();
67 
68  _cpu.total = (cpu2.total - cpu1.total);
69  _cpu.sys = (cpu2.sys - cpu1.sys);
70  _cpu.user = (cpu2.user - cpu1.user);
71  _cpu.nice = (cpu2.nice - cpu1.nice);
72  _cpu.idle = (cpu2.idle - cpu1.idle);
73  _cpu.iowait = (cpu2.iowait - cpu1.iowait);
74  _cpu.irq = (cpu2.irq - cpu1.irq);
75  _cpu.softirq = (cpu2.softirq - cpu1.softirq);
76  _cpu.frequency = (cpu2.frequency - cpu1.frequency);
77 
78  glibtop_mem mem1 = ss1->mem();
79  glibtop_mem mem2 = ss2->mem();
80 
81  _mem.total = (mem2.total - mem1.total) * 1000 / timeout;
82  _mem.used = (mem2.used - mem1.used) * 1000 / timeout;
83  _mem.free = (mem2.free - mem1.free) * 1000 / timeout;
84  _mem.shared = (mem2.shared - mem1.shared) * 1000 / timeout;
85  _mem.buffer = (mem2.buffer - mem1.buffer) * 1000 / timeout;
86  _mem.cached = (mem2.cached - mem1.cached) * 1000 / timeout;
87  _mem.locked = (mem2.locked - mem1.locked) * 1000 / timeout;
88 
89  std::string net_name;
90  glibtop_netload *net1, *net2;
91  int i = 0;
92  for (auto net : _netloads) {
93  net_name = _netnames.at(i);
94  net1 = ss1->netloads().at(i);
95  net2 = ss2->netloads().at(i);
96  if (net) {
97  net->address = (net2->address - net1->address) * 1000 / timeout;
98  net->bytes_in = (net2->bytes_in - net1->bytes_in) * 1000 / timeout;
99  net->bytes_out = (net2->bytes_out - net1->bytes_out) * 1000 / timeout;
100  net->errors_in = (net2->errors_in - net1->errors_in) * 1000 / timeout;
101  net->errors_out = (net2->errors_out - net1->errors_out) * 1000 / timeout;
102  net->packets_in = (net2->packets_in - net1->packets_in) * 1000 / timeout;
103  net->packets_out =
104  (net2->packets_out - net1->packets_out) * 1000 / timeout;
105  }
106  i++;
107  }
108 }
109 
110 std::string ObSensorSystem::json(const std::string name) const {
114 
115  std::string json;
116  double precent = (double)_cpu.total;
117 
118  json += "\"";
119  json += name;
120  json += "\" : {";
121 
122  if (type() == SensorType::SPEED) {
123 
124  std::string cpu_str = fmt::format(
125  "\"cpu\" : {{ \"total\" : {{ \"value\" :{:.3f}, \"alpha\" : {:.2f} "
126  "}},\"sys\" : "
127  "{{ \"value\" :{:.3f}, \"alpha\" : {:.2f} }},\"user\" : {{ \"value\" "
128  ":{:.3f}, \"alpha\" : {:.2f} }},\"nice\" "
129  ": {{ \"value\" :{:.3f}, \"alpha\" : {:.2f} }},\"idle\" : {{ "
130  "\"value\" "
131  ":{:.3f}, \"alpha\" : {:.2f} }},\"iowait\" : "
132  "{{ \"value\" :{:.3f}, \"alpha\" : {:.2f} }},\"frequency\" : {{ "
133  "\"value\" "
134  ":{:.3f}, \"alpha\" : 0 }} }}",
135  ((double)_cpu.total / precent) * 100.0, ((double)_cpu.total / precent),
136  ((double)_cpu.sys / precent) * 100.0, ((double)_cpu.sys / precent),
137  ((double)_cpu.user / precent) * 100.0, ((double)_cpu.user / precent),
138  ((double)_cpu.nice / precent) * 100.0, ((double)_cpu.nice / precent),
139  ((double)_cpu.idle / precent) * 100.0, ((double)_cpu.idle / precent),
140  ((double)_cpu.iowait / precent) * 100.0,
141  ((double)_cpu.iowait / precent), (double)_cpu.frequency);
142  json += cpu_str;
143  json += ",";
144  }
145 
146  if (type() == SensorType::VALUE) {
147  // mem
148  std::string mem_str = fmt::format(
149  "\"mem\" : {{ \"total\" : {{ \"value\" :{}, \"alpha\" : "
150  "{:.2f} }},\"used\" : "
151  "{{ \"value\" :{}, \"alpha\" : {:.2f} }},\"free\" : {{ "
152  "\"value\" :{}, \"alpha\" : {:.2f} }},\"shared\" "
153  ": {{ \"value\" :{}, \"alpha\" : {:.2f} }},\"buffer\" : {{ "
154  "\"value\" :{}, \"alpha\" : {:.2f} }},\"cached\" : "
155  "{{ \"value\" :{}, \"alpha\" : {:.2f} }},\"locked\" : {{ "
156  "\"value\" :{}, \"alpha\" : {:.2f} }} }}",
157  _mem.total, (double)_mem.total / _mem.total, _mem.used,
158  (double)_mem.used / _mem.total, _mem.free,
159  (double)_mem.free / _mem.total, _mem.shared,
160  (double)_mem.shared / _mem.total, _mem.buffer,
161  (double)_mem.buffer / _mem.total, _mem.cached,
162  (double)_mem.cached / _mem.total, _mem.locked,
163  (double)_mem.locked / _mem.total);
164 
165  json += mem_str;
166 
167  json += ",";
168  }
169 
170  if (type() == SensorType::SPEED) {
171  json += "\"net\" : [";
172 
173  double MAX_BYTES_ALPHA = 100 * 1048576; // 100 MB
174 
175  std::string net_name;
176  int i = 0;
177  for (auto net : _netloads) {
178  net_name = _netnames.at(i);
179  if (net) {
180  if (i != 0)
181  json += ",";
182  std::string net_str = fmt::format(
183  "{{ \"adapter\" : \"{}\", "
184  "\"address\" : {}, "
185  "\"bytes_in\" : {{ \"value\" :{}, \"alpha\" : {:.2f} }}, "
186  "\"bytes_out\" : {{ \"value\" :{}, \"alpha\" : {:.2f} }}, "
187  "\"errors_in\" : {{ \"value\" :{}, \"alpha\" : 0 }}, "
188  "\"errors_out\" : {{ \"value\" :{}, \"alpha\" : 0 }}, "
189  "\"packets_in\" : {{ \"value\" :{}, \"alpha\" : 0 }}, "
190  "\"packets_out\" : {{ \"value\" :{}, \"alpha\" : 0 }} ",
191  net_name, net->address, net->bytes_in,
192  (double)net->bytes_in / MAX_BYTES_ALPHA, net->bytes_out,
193  (double)net->bytes_out / MAX_BYTES_ALPHA, net->errors_in,
194  net->errors_out, net->packets_in, net->packets_out);
195  json += net_str;
196  json += "}";
197  }
198  i++;
199  }
200  json += "]";
201  }
202 
203  if (json.back() == ',')
204  json.pop_back();
205 
206  json += "}";
207 
208  return json;
209 }
void process() override
Process function.
std::shared_ptr< spdlog::logger > _logger
Pointer to spd logger.
Definition: ObSensor.h:51
void speed(ObSensor *s1, ObSensor *s2, int timeout=1000) override
Calculate time change (speed)
System Obmon sensor class.
ObSensorSystem(std::string name={"sys"})
glibtop_mem mem() const
returns glibtop_mem
std::vector< std::string > _netnames
list of network names
void init() override
ObSensor * _first
Pointer to first sensor.
Definition: ObSensor.h:54
std::vector< glibtop_netload * > netloads() const
returns netloads
ObSensor * _change
Pointer to change sensor.
Definition: ObSensor.h:56
glibtop_mem _mem
Mem info from glitop.
ObSensor * _second
Pointer to second sensor.
Definition: ObSensor.h:55
std::string json(const std::string name={"static"}) const override
glibtop_cpu _cpu
Cpu info from glitop.
glibtop_cpu cpu() const
returns glibtop_cpu
Base Obmon sensor class.
Definition: ObSensor.h:19
virtual ~ObSensorSystem()
SensorType type() const
Returns sensor type.
Definition: ObSensor.h:45
glibtop_netlist _netlist
NetList from glitop.
std::vector< glibtop_netload * > _netloads
List of netload.