1 #include "ObSensorSystem.h"
8 char **devices = glibtop_get_netlist(&
_netlist);
11 for (
unsigned int i = 0; i <
_netlist.number; i++) {
12 net =
new glibtop_netload;
44 _logger->trace(
"ObSensorSystem::process cpu ...");
45 glibtop_get_cpu(&
_cpu);
46 _logger->trace(
"ObSensorSystem::process mem ...");
47 glibtop_get_mem(&
_mem);
53 _logger->trace(
"ObSensorSystem::process net {}", net_name.data());
54 glibtop_get_netload(net, net_name.data());
65 glibtop_cpu cpu1 = ss1->
cpu();
66 glibtop_cpu cpu2 = ss2->
cpu();
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);
78 glibtop_mem mem1 = ss1->
mem();
79 glibtop_mem mem2 = ss2->
mem();
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;
90 glibtop_netload *net1, *net2;
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;
104 (net2->packets_out - net1->packets_out) * 1000 / timeout;
116 double precent = (double)
_cpu.total;
122 if (
type() == SensorType::SPEED) {
124 std::string cpu_str = fmt::format(
125 "\"cpu\" : {{ \"total\" : {{ \"value\" :{:.3f}, \"alpha\" : {:.2f} "
127 "{{ \"value\" :{:.3f}, \"alpha\" : {:.2f} }},\"user\" : {{ \"value\" "
128 ":{:.3f}, \"alpha\" : {:.2f} }},\"nice\" "
129 ": {{ \"value\" :{:.3f}, \"alpha\" : {:.2f} }},\"idle\" : {{ "
131 ":{:.3f}, \"alpha\" : {:.2f} }},\"iowait\" : "
132 "{{ \"value\" :{:.3f}, \"alpha\" : {:.2f} }},\"frequency\" : {{ "
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);
146 if (
type() == SensorType::VALUE) {
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} }} }}",
170 if (
type() == SensorType::SPEED) {
171 json +=
"\"net\" : [";
173 double MAX_BYTES_ALPHA = 100 * 1048576;
175 std::string net_name;
182 std::string net_str = fmt::format(
183 "{{ \"adapter\" : \"{}\", "
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);
203 if (json.back() ==
',')
void process() override
Process function.
std::shared_ptr< spdlog::logger > _logger
Pointer to spd logger.
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
ObSensor * _first
Pointer to first sensor.
std::vector< glibtop_netload * > netloads() const
returns netloads
ObSensor * _change
Pointer to change sensor.
glibtop_mem _mem
Mem info from glitop.
ObSensor * _second
Pointer to second sensor.
std::string json(const std::string name={"static"}) const override
glibtop_cpu _cpu
Cpu info from glitop.
glibtop_cpu cpu() const
returns glibtop_cpu
virtual ~ObSensorSystem()
SensorType type() const
Returns sensor type.
glibtop_netlist _netlist
NetList from glitop.
std::vector< glibtop_netload * > _netloads
List of netload.