-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensorrd.cpp
150 lines (121 loc) · 3.2 KB
/
sensorrd.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
// sensorrd.cpp - main file
#include <signal.h>
#include <stdlib.h>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <set>
#include <vector>
#include <rrd.h>
#include "rrdb.h"
#include "sensor.h"
#include "log.h"
#include "util.h"
namespace chrono = std::chrono;
using namespace std::chrono_literals;
using namespace zezax::sensorrd;
using chrono::system_clock;
using std::condition_variable;
using std::memory_order_relaxed;
using std::string;
using std::string_view;
using std::to_string;
using std::unique_lock;
using std::vector;
namespace {
std::atomic<bool> gQuitReq = false;
std::mutex gMutex;
condition_variable gCond;
void handleSig(int) {
gQuitReq.store(true, memory_order_relaxed);
unique_lock<std::mutex> lock(gMutex);
gCond.notify_all();
}
vector<string> makeRrdKeys(ContextPtrT ctx) {
vector<string> rrdKeys;
{
std::set<string> seen;
for (TupleIterT tpi(ctx); tpi; ++tpi) {
TupleT tp = tpi.getTuple();
string name = tp.name_;
int n = 0;
while (seen.count(name) > 0)
name = tp.name_ + '_' + to_string(n++);
seen.insert(name);
rrdKeys.emplace_back(std::move(name));
}
}
rrdKeys.emplace_back("loadavg");
return rrdKeys;
}
} // anonymous
int main(int argc, char **argv) {
string rrdPath;
if (argc == 2)
rrdPath = argv[1];
else if (argc > 2) {
logErr("Usage: sensorrd <rrdPath>");
return 1;
}
openlog("sensorrd", 0, LOG_DAEMON);
logSys(LOG_NOTICE, "begin");
ContextPtrT ctx = std::make_shared<ContextT>();
// one-time pass at startup to derive the rrd keys in order
vector<string> rrdKeys;
if (rrdPath.empty())
logSys(LOG_INFO, "not logging to any rrd");
else {
rrdKeys = makeRrdKeys(ctx);
if (pathExists(rrdPath))
logSys(LOG_INFO, "logging to rrd ", rrdPath);
else{
rrdCreate(rrdPath, rrdKeys);
logSys(LOG_NOTICE, "created rrd ", rrdPath);
}
}
chrono::nanoseconds period = 60s;
auto when = system_clock::now();
int minutes = 0;
unique_lock<std::mutex> lock(gMutex);
signal(SIGINT, handleSig);
signal(SIGQUIT, handleSig);
signal(SIGTERM, handleSig);
while (!gQuitReq.load(memory_order_relaxed)) {
int rr = 0;
string tmpl;
string vals = "N";
for (TupleIterT tpi(ctx); tpi; ++tpi, ++rr) {
TupleT t = tpi.getTuple();
if (t.alarm_) {
logSys(LOG_ERR, "Sensor alarm: Chip ", t.chip_, ": ",
t.name_, ": ", t.val_, " [ALARM]");
}
if (minutes == 0) {
logSys(LOG_INFO, t.chip_, ": ", t.label_, ": ", t.name_, ": ", t.val_);
minutes = 0;
}
if (!rrdPath.empty()) {
concat(tmpl, rrdKeys[rr], ':');
concat(vals, ':', t.val_);
}
}
if (!rrdPath.empty()) {
double load;
if (getloadavg(&load, 1) > 0) {
concat(tmpl, rrdKeys[rr], ':');
concat(vals, ':', load);
}
if (!tmpl.empty())
tmpl.erase(tmpl.end() - 1);
if (!rrdUpdate(rrdPath, tmpl, vals))
logSys(LOG_ERR, "failed to update rrd ", tmpl, ' ', vals);
}
when += period;
gCond.wait_until(lock, when);
if (++minutes >= 30)
minutes = 0;
}
logSys(LOG_NOTICE, "exiting");
return 0;
}