-
Notifications
You must be signed in to change notification settings - Fork 16
/
promon.cc
288 lines (243 loc) · 8.79 KB
/
promon.cc
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "simplomon.hh"
#include "minicurl.hh"
using namespace std;
/*
# HELP apt_upgrades_pending Apt packages pending updates by origin.
# TYPE apt_upgrades_pending gauge
apt_upgrades_pending{arch="all",origin="Debian:bookworm-security/stable-security"} 2
apt_upgrades_pending{arch="all",origin="Debian:bookworm/stable"} 4
apt_upgrades_pending{arch="amd64",origin="Debian:bookworm-security/stable-security"} 8
apt_upgrades_pending{arch="amd64",origin="Debian:bookworm/stable"} 24
...
node_filesystem_avail_bytes{device="/dev/nvme0n1p2",fstype="ext4",mountpoint="/"} 8.650682368e+10
*/
namespace {
struct PromLine
{
std::string metric;
map<string,string> sels;
double value;
};
}
PrometheusParser::PrometheusParser()
{
d_parser.set_logger([](size_t line, size_t col, const string& msg, const string &rule) {
fmt::print("line {}, col {}: {}\n", line, col,msg, rule);
});
// node_filesystem_avail_bytes{device="/dev/nvme0n1p2",fstype="ext4",mountpoint="/"} 8.650682368e+10
// xyz 3.14
auto ok = d_parser.load_grammar(R"(
ROOT <- ( ( ~COMMENTLINE / VLINE ) '\n')+
COMMENTLINE <- '#' (!'\n' .)*
VLINE <- KWORD SELS? ' ' VALUE
KWORD <- [a-zA-Z0-9_]+
SELS <- '{' KVPAIR (',' KVPAIR)* '}'
KVPAIR <- KWORD '=' '"' KVAL '"'
KVAL <- (!'"' .)*
VALUE <- [0-9.+e-]+
)" );
if(!ok)
throw runtime_error("Failed to configure prometheus parser");
d_parser["VALUE"] = [](const peg::SemanticValues &vs) {
return atof(&vs.token()[0]);
};
d_parser["KVPAIR"] = [](const peg::SemanticValues &vs) {
return std::make_pair(any_cast<string>(vs[0]), any_cast<string>(vs[1]));
};
d_parser["KWORD"] = [](const peg::SemanticValues &vs) {
return vs.token_to_string();
};
d_parser["KVAL"] = [](const peg::SemanticValues &vs) {
return vs.token_to_string();
};
d_parser["VLINE"] = [](const peg::SemanticValues &vs) {
PromLine ret;
ret.metric = any_cast<string>(vs[0]);
if(vs.size() == 2) // no sels
ret.value = any_cast<double>(vs[1]);
else {
ret.sels = any_cast<map<string,string>>(vs[1]);
ret.value = any_cast<double>(vs[2]);
}
return ret;
};
d_parser["SELS"] = [](const peg::SemanticValues &vs) {
map<string,string> sels;
for(const auto& a : vs) {
sels.insert(any_cast<pair<string,string>>(a));
}
return sels;
};
d_parser["ROOT"] = [](const peg::SemanticValues &vs) {
vector<PromLine> ret;
for(const auto& v : vs) {
ret.push_back(any_cast<PromLine>(v));
}
return ret;
};
/*
d_parser.set_logger([](size_t line, size_t col, const string& msg) {
fmt::print("Error parsing col {}: {}\n", col, msg);
});
*/
}
void PrometheusParser::parse(const std::string& cont)
{
d_prom.clear();
vector<PromLine> ret;
if(!d_parser.parse(cont, ret))
throw runtime_error("Unable to parse prometheus result");
for(const auto& pl : ret) {
d_prom[pl.metric][pl.sels] = pl.value;
}
}
struct PromDiskFreeCheck : PromCheck
{
PromDiskFreeCheck(sol::table data)
{
checkLuaTable(data, {"kind"}, {"mountpoint", "gbMin"});
d_mp = data.get_or("mountpoint", string("/"));
d_gbMin = data.get_or("gbMin", 1);
}
void doCheck(CheckResult& cr, const PrometheusParser::prom_t& prom, const std::string& url, std::map<std::string, std::map<std::string, SQLiteWriter::var_t>>& results) override
{
auto iter = prom.find("node_filesystem_avail_bytes");
if(iter == prom.end())
return;
// node_filesystem_avail_bytes
for(const auto& ent : iter->second) {
map<string, string> cop = ent.first;
if(cop["mountpoint"] == d_mp) {
double gbFree = ent.second/1000000000 ;
results["gbDiskFree"][d_mp] = ent.second/1000000000.0;
if(gbFree < d_gbMin)
cr.d_reasons[d_mp].push_back(fmt::format("on {}, mountpoint had less than {} gb free: {:.0f} gb",
url, d_gbMin, gbFree));
}
}
}
string d_mp;
int d_gbMin;
};
struct PromAptPendingCheck : PromCheck
{
PromAptPendingCheck(sol::table data)
{
checkLuaTable(data, {"kind"}, {"maxSec", "maxTot"});
d_maxSec = data.get_or("maxSec", 0);
d_maxTot = data.get_or("maxTot", -1);
}
void doCheck(CheckResult& cr, const PrometheusParser::prom_t& prom, const std::string& url, std::map<std::string, std::map<std::string, SQLiteWriter::var_t>>& results) override
{
auto iter = prom.find("apt_upgrades_pending");
if(iter == prom.end())
return;
int totsecpend=0;
int totpend=0;
for(const auto& ent : iter->second) {
map<string, string> cop = ent.first;
if(cop["origin"].find("security") != string::npos) {
totsecpend += ent.second;
}
totpend += ent.second;
}
results["aptPending"]["total"] = totpend;
results["aptPending"]["security"] = totsecpend;
if((d_maxSec>= 0 && totsecpend > d_maxSec) ||
(d_maxTot>= 0 && totpend > d_maxTot))
cr.d_reasons[""].push_back(fmt::format("There are {} pending security updates, out of {} total pending updates ({})",
totsecpend, totpend, url));
}
int d_maxSec, d_maxTot;
};
/*
node_network_receive_bytes_total{device="eno1"} 1.66779768142e+11
*/
struct PromBandwidthCheck : PromCheck
{
PromBandwidthCheck(sol::table data)
{
checkLuaTable(data, {"kind"}, {"maxMbit", "minMbit", "device", "direction"} );
d_maxMbit = data.get_or("maxMbit", -1.0);
d_minMbit = data.get_or("minMbit", -1.0);
d_device = data.get_or("device", string());
d_direction = data.get_or("direction", string("both"));
if(d_direction!= "in" && d_direction != "out" && d_direction != "both")
throw std::runtime_error("Valid values for Bandwidth direction: in, out, both");
if(d_minMbit < 0 && d_maxMbit < 0)
throw std::runtime_error("Need to set a minum or a maximum bandwidth");
}
void doCheck(CheckResult& cr, const PrometheusParser::prom_t& prom, const std::string& url, std::map<std::string, std::map<std::string, SQLiteWriter::var_t>>& results) override
{
double bytes=0;
vector<string> metrics;
if(d_direction =="both" || d_direction=="out")
metrics.push_back("node_network_transmit_bytes_total");
if(d_direction =="both" || d_direction=="in")
metrics.push_back("node_network_receive_bytes_total");
for(const auto& m : metrics) {
auto iter = prom.find(m);
if(iter == prom.end())
return;
for(const auto& ent : iter->second) {
map<string, string> cop = ent.first;
if(d_device.empty() || cop["device"]==d_device)
bytes += ent.second;
}
}
string devstring = d_device.empty() ? string("") : fmt::format(" on dev {}", d_device);
time_t now = time(nullptr);
if(d_prevTime > 0) {
double mbit = ((bytes - d_prevBytes)*8.0 / (now - d_prevTime))/1000000.0;
results["Bandwidth "+d_direction+devstring]["Mbit"] = mbit;
if(d_maxMbit > 0 && mbit > d_maxMbit)
cr.d_reasons[""].push_back(fmt::format("From {}, bandwidth{} exceeded limit of {} Mbit/s (direction '{}')",
url, devstring, d_maxMbit, d_direction));
if(d_minMbit > 0 && mbit < d_minMbit)
cr.d_reasons[""].push_back(fmt::format("From {}, bandwidth{} lower than limit of {} Mbit/s (direction '{}')",
url, devstring, d_minMbit, d_direction));
}
d_prevBytes = bytes;
d_prevTime = now;
}
int d_maxMbit=-1;
int d_minMbit = -1;
string d_device;
time_t d_prevTime=-1;
double d_prevBytes;
string d_direction;
};
PrometheusChecker::PrometheusChecker(sol::table data) : Checker(data)
{
checkLuaTable(data, {"url"}, {"checks"});
d_url = data["url"];
d_attributes["url"] = d_url;
sol::optional<sol::table> checks = data["checks"];
if(checks != sol::nullopt) {
for(unsigned int n=0 ; n < checks->size(); ++n) {
sol::table check = (*checks)[n+1];
sol::optional<string> kind = check["kind"];
if(kind == sol::nullopt)
throw std::runtime_error(fmt::format("A prometheus check needs a 'kind' field"));
if(kind =="DiskFree")
d_checkers.emplace_back(std::make_unique<PromDiskFreeCheck>(check));
else if(kind =="AptPending")
d_checkers.emplace_back(std::make_unique<PromAptPendingCheck>(check));
else if(kind =="Bandwidth")
d_checkers.emplace_back(std::make_unique<PromBandwidthCheck>(check));
else
throw std::runtime_error(fmt::format("Unknown prometheus check {}", *kind));
}
}
}
CheckResult PrometheusChecker::perform()
{
MiniCurl mc;
string res = mc.getURL(d_url);
d_parser.parse(res);
d_results.clear();
CheckResult cr;
for(auto& c : d_checkers)
c->doCheck(cr, d_parser.d_prom, d_url, d_results);
return cr;
}