-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrdb.cpp
98 lines (77 loc) · 2.21 KB
/
rrdb.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
// rrdb.cpp - wrapper around rrd api
#include "rrdb.h"
#include <stdexcept>
#include "util.h"
namespace zezax::sensorrd {
using std::runtime_error;
using std::string;
using std::vector;
namespace {
void guessMinMax(const string &key, string &minStr, string &maxStr) {
if (startsWith(key, "temp")) { // celsius
minStr = "-100";
maxStr = "250";
}
else if (startsWith(key, "fan")) { // rpm
minStr = "0";
maxStr = "12000";
}
else if (startsWith(key, "in")) { // volts
minStr = "-25";
maxStr = "25";
}
else if (key == "loadavg") { // count
minStr = "0";
maxStr = "NaN";
}
else {
minStr = "NaN";
maxStr = "NaN";
}
}
} // anonymous
void rrdCreate(const string &rrdPath, const vector<string> &rrdKeys) {
vector<string> args;
args.emplace_back("create");
args.emplace_back(rrdPath);
args.emplace_back("-s");
args.emplace_back("60");
string minStr;
string maxStr;
for (const string &key : rrdKeys) {
guessMinMax(key, minStr, maxStr);
string ds = "DS:";
ds += key + ":GAUGE:300:" + minStr + ':' + maxStr;
args.emplace_back(std::move(ds));
}
args.emplace_back("RRA:AVERAGE:0.5:1:1800"); // 30 hours of minutes
args.emplace_back("RRA:AVERAGE:0.5:5:2880"); // 10 days of 5 minutes
args.emplace_back("RRA:AVERAGE:0.5:60:960"); // 40 days of hours
args.emplace_back("RRA:AVERAGE:0.5:1440:3653"); // 10 years of days
vector<const char *> argv;
for (string &s : args)
argv.emplace_back(s.c_str());
rrd_clear_error();
int res = rrd_create(static_cast<int>(argv.size()),
const_cast<char **>(argv.data()));
if (res < 0)
throw runtime_error(rrd_get_error());
}
bool rrdUpdate(const string &rrdPath,
const string &templat,
const string &values) {
vector<string> args;
args.emplace_back("update");
args.emplace_back(rrdPath);
args.emplace_back("-t");
args.emplace_back(templat);
args.emplace_back(values);
vector<const char *> argv;
for (string &s : args)
argv.emplace_back(s.c_str());
rrd_clear_error();
int res = rrd_update(static_cast<int>(argv.size()),
const_cast<char **>(argv.data()));
return (res == 0);
}
} // namespace zezax::sensorrd