-
Notifications
You must be signed in to change notification settings - Fork 21
/
mksmokeping
executable file
·93 lines (73 loc) · 2.79 KB
/
mksmokeping
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
#!/usr/bin/env python3
from textwrap import dedent
from argparse import ArgumentParser
try:
from natsort import natsorted as sorted
except ImportError:
# use the list.sorted() function
pass
from formatter import Formatter
from filereader import get_communities_data
class SmokePingFormatter(Formatter):
"""
Formatter for SmokePing (http://oss.oetiker.ch/smokeping/)
"""
def add_data(self, name, ip, probe):
self.config.append(dedent("""
++ %(name)s
menu = %(name)s
title = %(name)s
probe = %(probe)s
host = %(ip)s
#alerts = someloss
""" % {"name": name, "ip": ip, "probe": probe}))
def add_section(self, name):
self.config.append(dedent("""
+ %(name)s
menu = %(name)s
title = %(name)s
""" % {"name": name}))
def create_config(srcdir, exclude, fmtclass):
"""
Generates a configuration using all files in srcdir
(non-recursively) excluding communities from 'exclude'.
The files are read in lexicographic order to produce deterministic
results.
"""
formatter = fmtclass()
for community, data in get_communities_data(srcdir, exclude):
try:
bgp = data['bgp']
except (TypeError, KeyError):
continue
formatter.add_section(community)
for host in sorted(bgp.keys()):
d = bgp[host]
if 'ipv4' in d:
peer = d['ipv4']
formatter.add_data("ipv4-{}".format(host), peer, 'FPing')
if 'ipv6' in d:
peer = d['ipv6']
formatter.add_data("ipv6-{}".format(host), peer, 'FPing6')
print(formatter.finalize())
if __name__ == '__main__':
formatters = {
'SmokePing': SmokePingFormatter,
}
PARSER = ArgumentParser()
PARSER.add_argument('-f', '--format', dest='fmt',
help='Create config in format FMT. Possible values: {formatters}. '
'Default: SmokePing'.format(formatters=", ".join(formatters.keys())),
metavar='FMT',
choices=list(formatters.keys()),
default='SmokePing')
PARSER.add_argument('-s', '--sourcedir', dest='src',
help='Use files in DIR as input files. Default: ../icvpn-meta/',
metavar='DIR',
default='../icvpn-meta/')
PARSER.add_argument('-x', '--exclude', dest='exclude', action='append',
help='Exclude the comma-separated list of COMMUNITIES',
metavar='COMMUNITIES',
default=[])
ARGS = PARSER.parse_args()
create_config(ARGS.src, set(ARGS.exclude), formatters[ARGS.fmt])