-
Notifications
You must be signed in to change notification settings - Fork 1
/
prometheus_plugin.cc
263 lines (218 loc) · 8.19 KB
/
prometheus_plugin.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
#include <time.h>
#include <vector>
#include "trunk-recorder/source.h"
#include "trunk-recorder/plugin_manager/plugin_api.h"
#include <trunk-recorder/json.hpp>
#include <boost/dll/alias.hpp> // for BOOST_DLL_ALIAS
#include <boost/log/trivial.hpp>
#include <iostream>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/registry.h>
typedef struct prometheus_plugin_t prometheus_plugin_t;
struct prometheus_plugin_t
{
std::vector<Source *> sources;
std::vector<System *> systems;
std::vector<Call *> calls;
Config *config;
};
using namespace std;
using namespace prometheus;
class Prometheus : public Plugin_Api
{
std::vector<Call *> calls;
Config *config;
uint16_t port;
prometheus::Exposer *exposer;
std::shared_ptr<prometheus::Registry> registry;
prometheus::Family<prometheus::Gauge> *active_calls;
prometheus::Family<prometheus::Gauge> *source_digital_recorders_gauge;
prometheus::Family<prometheus::Gauge> *source_analog_recorders_gauge;
prometheus::Family<prometheus::Counter> *calls_counter;
prometheus::Family<prometheus::Counter> *message_counter;
prometheus::Family<prometheus::Counter> *spike_counter;
prometheus::Family<prometheus::Counter> *error_counter;
prometheus::Family<prometheus::Counter> *call_duration_counter;
prometheus::Family<prometheus::Counter> *source_error_counter;
public:
// Factory method
static boost::shared_ptr<Prometheus> create()
{
return boost::shared_ptr<Prometheus>(new Prometheus());
}
int parse_config(json config_data) override
{
this->port = config_data.value("port", 9842);
BOOST_LOG_TRIVIAL(info) << " Prometheus Plugin Port: " << std::to_string(this->port);
return 0;
}
int init(Config *config, std::vector<Source *> sources, std::vector<System *> systems) override
{
this->config = config;
this->exposer = new Exposer(std::to_string(this->port));
this->registry = std::make_shared<Registry>();
auto prefix = std::string("trunk_recorder_");
this->active_calls = &BuildGauge()
.Name(prefix+"active_calls")
.Help("Number of active calls")
.Register(*registry);
this->source_digital_recorders_gauge = &BuildGauge()
.Name(prefix+"source_digital_recorders")
.Help("Number of available digital recorders")
.Register(*registry);
this->source_analog_recorders_gauge = &BuildGauge()
.Name(prefix+"source_analog_recorders")
.Help("Number of available analog recorders")
.Register(*registry);
this->calls_counter = &BuildCounter()
.Name(prefix+"calls")
.Help("Call history")
.Register(*registry);
this->message_counter = &BuildCounter()
.Name(prefix+"message_decodes")
.Help("Message decode count")
.Register(*registry);
this->spike_counter = &BuildCounter()
.Name(prefix+"call_spike_count")
.Help("Spike count")
.Register(*registry);
this->error_counter = &BuildCounter()
.Name(prefix+"call_error_count")
.Help("Error count")
.Register(*registry);
this->call_duration_counter = &BuildCounter()
.Name(prefix+"call_duration")
.Help("Call duration")
.Register(*registry);
this->source_error_counter = &BuildCounter()
.Name(prefix+"source_error_count")
.Help("Source error count")
.Register(*registry);
this->exposer->RegisterCollectable(this->registry);
return 0;
}
int start() override
{
return 0;
}
int calls_active(std::vector<Call *> calls) override
{
std::map<std::string, int> callsPerSystem;
for (std::vector<Call *>::iterator it = calls.begin(); it != calls.end(); it++)
{
Call *call = *it;
std::string callSystem = call->get_short_name();
if (callsPerSystem.find(callSystem) == callsPerSystem.end())
{
callsPerSystem[callSystem] = 1;
}
else
{
callsPerSystem[callSystem] += 1;
}
this->calls_counter->Add({
{"encrypted", std::to_string(call->get_encrypted())},
{"talkgroup", std::to_string(call->get_talkgroup())},
{"talkgroup_display", call->get_talkgroup_display()},
{"talkgroup_tag", call->get_talkgroup_tag()},
{"freq", std::to_string(call->get_freq())},
{"system", callSystem},
}).Increment();
}
for (auto& callPerSystem : callsPerSystem)
{
this->active_calls->Add({
{"system", callPerSystem.first},
}).Set(callPerSystem.second);
}
return 0;
}
int call_end(Call_Data_t call_info) override
{
return this->update_call_end_metrics(&call_info);
}
int setup_config(std::vector<Source *> sources, std::vector<System *> systems) override
{
auto ret = 0;
for (std::vector<Source *>::iterator it = sources.begin(); it != sources.end(); it++)
{
Source *source = *it;
ret = this->update_source_metrics(source);
if (ret != 0)
{
return ret;
}
}
return ret;
}
int system_rates(std::vector<System *> systems, float timeDiff) override
{
for (std::vector<System *>::iterator it = systems.begin(); it != systems.end(); it++)
{
System *system = *it;
this->message_counter->Add({
{"system", system->get_short_name()}
}).Increment(system->get_message_count());
}
return 0;
}
protected:
int update_call_end_metrics(Call_Data_t *call_info) {
this->call_duration_counter->Add({
{"encrypted", std::to_string(call_info->encrypted)},
{"talkgroup", std::to_string(call_info->talkgroup)},
{"talkgroup_display", call_info->talkgroup_display},
{"talkgroup_tag", call_info->talkgroup_alpha_tag},
{"freq", std::to_string(call_info->freq)},
{"system", call_info->short_name},
}).Increment(call_info->length);
this->spike_counter->Add({
{"encrypted", std::to_string(call_info->encrypted)},
{"talkgroup", std::to_string(call_info->talkgroup)},
{"talkgroup_display", call_info->talkgroup_display},
{"talkgroup_tag", call_info->talkgroup_alpha_tag},
{"freq", std::to_string(call_info->freq)},
{"system", call_info->short_name},
}).Increment(call_info->spike_count);
this->error_counter->Add({
{"encrypted", std::to_string(call_info->encrypted)},
{"talkgroup", std::to_string(call_info->talkgroup)},
{"talkgroup_display", call_info->talkgroup_display},
{"talkgroup_tag", call_info->talkgroup_alpha_tag},
{"freq", std::to_string(call_info->freq)},
{"system", call_info->short_name},
}).Increment(call_info->error_count);
return 0;
}
int update_source_metrics(Source * sources) {
this->source_error_counter->Add({
{"device", sources->get_device()},
{"driver", sources->get_driver()},
{"rate", std::to_string(sources->get_rate())},
{"antenna", sources->get_antenna()},
}).Increment(sources->get_error());
this->source_digital_recorders_gauge->Add({
{"device", sources->get_device()},
{"driver", sources->get_driver()},
{"rate", std::to_string(sources->get_rate())},
{"antenna", sources->get_antenna()},
}).Set(sources->get_num_available_digital_recorders());
this->source_analog_recorders_gauge->Add({
{"device", sources->get_device()},
{"driver", sources->get_driver()},
{"rate", std::to_string(sources->get_rate())},
{"antenna", sources->get_antenna()},
}).Set(sources->get_num_available_analog_recorders());
return 0;
}
};
BOOST_DLL_ALIAS(
Prometheus::create, // <-- this function is exported with...
create_plugin // <-- ...this alias name
)