-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats_table.cpp
359 lines (276 loc) · 10.9 KB
/
stats_table.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* @file stats_table.cpp
* @author matthewv
* @date Sept 16, 2020
* @date Copyright 2012-2014
*
* @brief
*/
#include "stats_table.h"
#include "stats_table_impl.h"
#include "snmpagent/val_integer64.h"
#include "snmpagent/val_string.h"
/**
* Enterprise: 1.3.6.1.4.1
*Agent prefix: 38693 matthewv.com enterprise
* .1 generic
* .1 /proc/diskstats
* .2 hellodirect
* .1 log monitors
* .2 tomcat5 gc log
* .3 stm
* .1 log monitors
* .2 haproxy monitors
* .3 memcached
* .4 riak
* .1 diskstats
* .5 rocksdb
* .1 ticker statistics
* .x instance id
*/
// .1.3.6.1.4 is implied in communications
static unsigned sAgentPrefix[] = {1, 38693, 5};
static SnmpAgent::SnmpAgentId sAgentId = {
sAgentPrefix, sizeof(sAgentPrefix) / sizeof(sAgentPrefix[0]),
"RocksMonitor"};
StatsTable* StatsTable::NewStatsTable(bool StartWorker) {
return new StatsTableImpl(StartWorker);
} // StatsTable::NewStatsTable
/**
* Initialize the data members.
* @date Created 05/21/12
* @author matthewv
*/
StatsTableImpl::StatsTableImpl(bool StartWorker) {
// everything is a "make_shared" object in libmevent & snmpagent world
m_Mgr = std::make_shared<MEventMgr>();
if (StartWorker) {
m_Mgr->StartThreaded();
} // if
m_Agent = std::make_shared<SnmpAgent>(sAgentId, 0x7f000001, 705);
MEventPtr mo_sa = m_Agent->GetMEventPtr();
m_Mgr->AddEvent(mo_sa);
}
StatsTableImpl::~StatsTableImpl() {
m_Mgr->Stop();
m_Mgr->ThreadWait();
} // StatsTableImpl::~StatsTable
class SnmpValTicker : public SnmpValCounter64 {
protected:
rocksdb::Tickers m_Ticker;
const std::shared_ptr<rocksdb::Statistics> m_Stats;
public:
SnmpValTicker() = delete;
SnmpValTicker(unsigned ID, rocksdb::Tickers Ticker,
const std::shared_ptr<rocksdb::Statistics> Stats)
: SnmpValCounter64(ID), m_Ticker(Ticker), m_Stats(Stats) {}
virtual ~SnmpValTicker(){};
void AppendToIovec(std::vector<struct iovec> &IoArray) override {
m_Unsigned64 = m_Stats->getTickerCount(m_Ticker);
SnmpValCounter64::AppendToIovec(IoArray);
}
}; // class SnmpValTicker
bool StatsTableImpl::AddTable(const std::shared_ptr<rocksdb::Statistics> &stats,
unsigned TableId, const std::string &TableName) {
SnmpValInfPtr shared;
SnmpValStringPtr new_string;
OidVector_t table_prefix = {TableId};
int idx;
OidVector_t row_oid, null_oid;
UpdateTableNameList(TableId, TableName);
//
// Retrieve the statistics map once immediately to get the
// map and its std::string members allocated and initialized
//
row_oid.push_back(0);
idx = 0;
for (auto ticker : rocksdb::TickersNameMap) {
std::shared_ptr<SnmpValTicker> new_counter;
row_oid[0] = ticker.first;
new_counter = std::make_shared<SnmpValTicker>(1, ticker.first, stats);
new_counter->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
shared = new_counter->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
new_string = std::make_shared<SnmpValString>(2);
new_string->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
new_string->assign(ticker.second.c_str());
shared = new_string->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
++idx;
}
return true;
} // StatsTableImpl::AddTable (statistics)
typedef size_t (rocksdb::Cache::*CacheGetFunction)(void) const;
class CacheValCounter64 : public SnmpValUnsigned64 {
public:
CacheValCounter64() = delete;
CacheValCounter64(const SnmpOid &Oid, const std::shared_ptr<rocksdb::Cache> & cache,
CacheGetFunction Func)
: SnmpValUnsigned64(Oid, gVarCounter64), cache_weak(cache), value_func(Func) {};
CacheValCounter64(const OidVector_t &Oid, const std::shared_ptr<rocksdb::Cache> & cache,
CacheGetFunction Func)
: SnmpValUnsigned64(Oid, gVarCounter64), cache_weak(cache), value_func(Func) {};
CacheValCounter64(unsigned ID, const std::shared_ptr<rocksdb::Cache> & cache,
CacheGetFunction Func)
: SnmpValUnsigned64(ID, gVarCounter64), cache_weak(cache), value_func(Func) {};
void AppendToIovec(std::vector<struct iovec> &IoArray) override {
std::shared_ptr<rocksdb::Cache> strong_ptr;
strong_ptr = cache_weak.lock();
if (strong_ptr) {
m_Unsigned64 = (uint64_t)((*strong_ptr).*value_func)();
} else {
m_Unsigned64 = 0;
}
SnmpValUnsigned64::AppendToIovec(IoArray);
};
protected:
const std::weak_ptr<rocksdb::Cache> cache_weak;
CacheGetFunction value_func;
}; // CacheValCounter64
bool StatsTableImpl::AddTable(const std::shared_ptr<rocksdb::Cache> &cache,
unsigned TableId, const std::string &TableName) {
SnmpValInfPtr shared;
SnmpValStringPtr new_string;
OidVector_t table_prefix = {TableId};
OidVector_t row_oid, null_oid;
std::shared_ptr<CacheValCounter64> new_counter;
UpdateTableNameList(TableId, TableName);
//
// Build the string elements and value
// map and its std::string members allocated and initialized
//
row_oid.push_back(0);
// GetCapacity
row_oid[0] = 0;
new_counter = std::make_shared<CacheValCounter64>(1, cache, &rocksdb::Cache::GetCapacity);
new_counter->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
shared = new_counter->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
new_string = std::make_shared<SnmpValString>(2);
new_string->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
new_string->assign("rocksdb.cache.get.capacity");
shared = new_string->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
// GetUsage (two versions of GetUsage. static_cast to say want void param version)
row_oid[0] = 1;
CacheGetFunction xx = static_cast<size_t(rocksdb::Cache::*)(void) const>(&rocksdb::Cache::GetUsage);
new_counter = std::make_shared<CacheValCounter64>(1, cache, xx);
new_counter->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
shared = new_counter->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
new_string = std::make_shared<SnmpValString>(2);
new_string->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
new_string->assign("rocksdb.cache.get.usage");
shared = new_string->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
// GetPinnedUsage
row_oid[0] = 2;
new_counter = std::make_shared<CacheValCounter64>(1, cache, &rocksdb::Cache::GetPinnedUsage);
new_counter->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
shared = new_counter->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
new_string = std::make_shared<SnmpValString>(2);
new_string->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
new_string->assign("rocksdb.cache.get.pinned.usage");
shared = new_string->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
return true;
} // StatsTableImpl::AddTable (cache)
void StatsTableImpl::UpdateTableNameList(unsigned TableId, const std::string &TableName) {
SnmpValInfPtr shared;
SnmpValStringPtr new_string;
OidVector_t table_prefix = {TableId};
OidVector_t row_oid, null_oid;
//
// Put a table name in snmp tree
//
row_oid.push_back(0);
row_oid.push_back(TableId);
new_string = std::make_shared<SnmpValString>(0);
new_string->InsertTablePrefix(m_Agent->GetOidPrefix(), null_oid, null_oid,
row_oid);
new_string->assign(TableName.c_str());
shared = new_string->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
} // StatsTableImpl::UpdateTableNameList
class RocksValCounter64 : public SnmpValUnsigned64 {
public:
RocksValCounter64() = delete;
RocksValCounter64(const SnmpOid &Oid, rocksdb::DB * DBptr, const char * Property)
: SnmpValUnsigned64(Oid, gVarCounter64), dbase(DBptr), property(Property) {}
RocksValCounter64(const OidVector_t &Oid, rocksdb::DB * DBptr, const char * Property)
: SnmpValUnsigned64(Oid, gVarCounter64), dbase(DBptr), property(Property) {}
RocksValCounter64(unsigned ID, rocksdb::DB * DBptr, const char * Property)
: SnmpValUnsigned64(ID, gVarCounter64), dbase(DBptr), property(Property) {}
void AppendToIovec(std::vector<struct iovec> &IoArray) override {
if (nullptr != dbase) {
bool flag;
flag = dbase->GetAggregatedIntProperty(property, &m_Unsigned64);
if (!flag) {
m_Unsigned64 = 0;
}
} else {
m_Unsigned64 = 0;
}
SnmpValUnsigned64::AppendToIovec(IoArray);
};
protected:
rocksdb::DB * dbase = nullptr;
std::string property;
}; // RocksValCounter64
bool StatsTableImpl::AddTable(rocksdb::DB * DBase,
unsigned TableId, const std::string &TableName) {
SnmpValInfPtr shared;
SnmpValStringPtr new_string;
OidVector_t table_prefix = {TableId};
OidVector_t row_oid, null_oid;
std::shared_ptr<RocksValCounter64> new_counter;
UpdateTableNameList(TableId, TableName);
//
// Build the string elements and value
// map and its std::string members allocated and initialized
//
row_oid.push_back(0);
std::map<uint64_t, const char *> int_properties
{
// partial list of variables available per rocksdb/db.h
{0, "rocksdb.estimate-table-readers-mem"},
{1, "rocksdb.background-errors"},
{2, "rocksdb.cur-size-active-mem-table"},
{3, "rocksdb.cur-size-all-mem-tables"},
{4, "rocksdb.size-all-mem-tables"},
{5, "rocksdb.num-snapshots"},
{6, "rocksdb.estimate-live-data-size"},
{7, "rocksdb.total-sst-files-size"},
{8, "rocksdb.live-sst-files-size"},
{9, "rocksdb.block-cache-capacity"},
{10,"rocksdb.block-cache-usage"},
{11,"rocksdb.block-cache-pinned-usage"},
{12,"rocksdb.table-cache-capacity"},
{13,"rocksdb.table-cache-usage"},
{14,"rocksdb.is-write-stopped"}
};
for (auto item : int_properties) {
row_oid[0] = item.first;
new_counter = std::make_shared<RocksValCounter64>(1, DBase, item.second);
new_counter->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
shared = new_counter->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
new_string = std::make_shared<SnmpValString>(2);
new_string->InsertTablePrefix(m_Agent->GetOidPrefix(), table_prefix,
null_oid, row_oid);
new_string->assign(item.second);
shared = new_string->GetSnmpValInfPtr();
m_Agent->AddVariable(shared);
} // for
return true;
} // StatsTableImpl::AddTable (db)