-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
97 lines (78 loc) · 2.73 KB
/
main.py
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
import vici
import os
import json
import time
from typing import Any, Tuple, Iterable, Dict
from collections import Counter
from itertools import chain
SAS_GRAPH_KEY = 'strongswan.sas'
POOLS_GRAPH_KEY = 'strongswan.pools'
SAS_ACTIVE_KEY = 'active'
POOLS_ONLINE_KEY = 'online'
POOLS_SIZE_KEY = 'size'
Metric = Tuple[str, Any]
def meta():
sas_graph_name = '{0}.#'.format(SAS_GRAPH_KEY)
pools_graph_name = '{0}.#'.format(POOLS_GRAPH_KEY)
meta = {
"graphs": {
sas_graph_name: {
"label": "StrongSwan IKE_SAs",
"unit": "integer",
"metrics": [{
"name": SAS_ACTIVE_KEY,
"label": SAS_ACTIVE_KEY
}]
},
pools_graph_name: {
"label": "StrongSwan Address pool",
"unit": "integer",
"metrics": [
{
"name": POOLS_SIZE_KEY,
"label": POOLS_SIZE_KEY
},
{
"name": POOLS_ONLINE_KEY,
"label": POOLS_ONLINE_KEY
}
]
},
},
}
print("# mackerel-agent-plugin")
print(json.dumps(meta))
def _print_metric(metric: Metric):
epoch = int(time.time())
print('{0}\t{1}\t{2}'.format(metric[0], metric[1], epoch))
def _sas(sess: vici.Session) -> Iterable[Metric]:
conns = (_conn.decode('UTF-8') for _conn in sess.get_conns()['conns'])
sas = sess.list_sas()
sa_names = chain.from_iterable(sa.keys() for sa in sas)
counter = Counter(sa_names)
def _create_metric(counter: Counter, conn: str) -> Metric:
metric_name = '{0}.{1}.{2}'.format(SAS_GRAPH_KEY, conn, SAS_ACTIVE_KEY)
count = counter[conn]
return metric_name, count
return (_create_metric(counter, conn) for conn in conns)
def _pools(sess: vici.Session) -> Iterable[Metric]:
pools = sess.get_pools(options=None)
def _create_metrics(pool_name: str, pool: Dict) -> Iterable[Metric]:
metric_base_name = '{0}.{1}'.format(POOLS_GRAPH_KEY, pool_name)
return [
('{0}.{1}'.format(metric_base_name, POOLS_SIZE_KEY), int(pool['size'])),
('{0}.{1}'.format(metric_base_name, POOLS_ONLINE_KEY), int(pool['online'])),
]
metricses = (_create_metrics(name, pool) for name, pool in pools.items())
return chain.from_iterable(metricses)
def main():
sess = vici.Session()
for metric in _sas(sess):
_print_metric(metric)
for metric in _pools(sess):
_print_metric(metric)
if __name__ == '__main__':
if os.environ.get('MACKEREL_AGENT_PLUGIN_META') == '1':
meta()
else:
main()