3
|
1 |
"""
|
|
2 |
Definitions for MRTG
|
|
3 |
"""
|
|
4 |
|
|
5 |
class Target (object) :
|
|
6 |
"""
|
|
7 |
A monitoring target
|
|
8 |
"""
|
|
9 |
|
|
10 |
def __init__ (self, name, target_def, max_bytes, title, comment=None) :
|
|
11 |
"""
|
|
12 |
Store values corresponding to the MRTG configuration statements:
|
|
13 |
name - the unique identifier for this target
|
|
14 |
target_def - the Target[] definition, see target_def()
|
|
15 |
max_bytes - maximum number of bytes/s accepted for this interface, used to determine % load
|
|
16 |
title - human-readable title
|
|
17 |
comment - optional comment field
|
|
18 |
"""
|
|
19 |
|
|
20 |
self.name = name
|
|
21 |
self.target_def = target_def
|
|
22 |
self.max_bytes = max_bytes
|
|
23 |
self.title = title
|
|
24 |
self.comment = comment
|
|
25 |
|
|
26 |
def statement (self, name, value) :
|
|
27 |
"""
|
|
28 |
Yield the line for a single stanza:
|
|
29 |
Stanza[identifier]: value
|
|
30 |
"""
|
|
31 |
|
|
32 |
return "%s[%s]: %s" % (name, self.name, value)
|
|
33 |
|
|
34 |
def generate (self) :
|
|
35 |
"""
|
|
36 |
Return a series of lines for this target
|
|
37 |
"""
|
|
38 |
|
|
39 |
if self.comment :
|
|
40 |
yield "# %s" % (self.comment, )
|
|
41 |
|
|
42 |
yield self.statement('Target', self.target_def)
|
|
43 |
yield self.statement('MaxBytes', self.max_bytes)
|
|
44 |
yield self.statement('Title', self.title)
|
|
45 |
|
|
46 |
class Host (object) :
|
|
47 |
"""
|
|
48 |
A SNMP target
|
|
49 |
"""
|
|
50 |
|
|
51 |
def __init__ (self, name, hostname, snmp_community) :
|
|
52 |
"""
|
|
53 |
Store values corresponding to the MRTG Target stanza bits:
|
|
54 |
name - short name
|
|
55 |
hostname - the snmp target
|
|
56 |
snmp_community - the SNMP community to use
|
|
57 |
"""
|
|
58 |
|
|
59 |
self.name = name
|
|
60 |
self.hostname = hostname
|
|
61 |
self.snmp_community = snmp_community
|
|
62 |
|
|
63 |
def port (self, port_name, mbps, port=None, descr=None, port_title=None) :
|
|
64 |
"""
|
|
65 |
Create and return a Target describing the given port/interface on this host.
|
|
66 |
"""
|
|
67 |
|
|
68 |
# figure out which ifref to use
|
|
69 |
ifid = iftitle = None
|
|
70 |
|
|
71 |
for prefix, tprefix, value in (
|
|
72 |
('', "#", port),
|
|
73 |
('\\', "", descr),
|
|
74 |
) :
|
|
75 |
if value :
|
|
76 |
# ok, use this
|
|
77 |
ifid = prefix + str(value)
|
|
78 |
|
|
79 |
if port_title :
|
|
80 |
iftitle = port_title
|
|
81 |
|
|
82 |
else :
|
|
83 |
iftitle = tprefix + str(value)
|
|
84 |
|
|
85 |
break
|
|
86 |
|
|
87 |
if not ifid :
|
|
88 |
raise ValueError("No interface identifier given")
|
|
89 |
|
|
90 |
# compose
|
|
91 |
name = "%s_%s" % (self.hostname, port_name)
|
|
92 |
target = "%s:%s@%s" % (ifid, self.snmp_community, self.hostname)
|
|
93 |
max_bytes = mbps * 1000 * 1000 / 8
|
|
94 |
title = "Traffic @ %s %s -> %s" % (self.name, iftitle, port_name)
|
|
95 |
|
|
96 |
# build and return Target
|
|
97 |
return Target(name, target, max_bytes, title)
|
|
98 |
|
|
99 |
def write_cfg (path, targets) :
|
|
100 |
"""
|
|
101 |
Write out the given targets to the given file
|
|
102 |
"""
|
|
103 |
|
|
104 |
fh = open(path, 'w')
|
|
105 |
|
|
106 |
# generate() each target
|
|
107 |
for target in targets :
|
|
108 |
for line in target.generate() :
|
|
109 |
# output line
|
|
110 |
fh.write(line + '\n')
|
|
111 |
|
|
112 |
# separate by empty line
|
|
113 |
fh.write('\n')
|
|
114 |
|
|
115 |
fh.close()
|
|
116 |
|