author | Tero Marttila <terom@fixme.fi> |
Tue, 02 Nov 2010 05:36:16 +0200 | |
changeset 25 | 9fa9d881fd87 |
parent 9 | 32553ab44472 |
permissions | -rw-r--r-- |
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 |
||
5
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
26 |
def rrd_name (self) : |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
27 |
""" |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
28 |
Return the relative path to the .rrd file for this target |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
29 |
""" |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
30 |
|
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
31 |
return "%s.rrd" % (self.name, ) |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
32 |
|
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
33 |
def img_name (self) : |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
34 |
""" |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
35 |
Return the relative path to the .png image for this target |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
36 |
""" |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
37 |
|
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
38 |
return "%s.png" % (self.name, ) |
e716718482c3
hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
39 |
|
3 | 40 |
def statement (self, name, value) : |
41 |
""" |
|
42 |
Yield the line for a single stanza: |
|
43 |
Stanza[identifier]: value |
|
44 |
""" |
|
45 |
||
46 |
return "%s[%s]: %s" % (name, self.name, value) |
|
47 |
||
48 |
def generate (self) : |
|
49 |
""" |
|
50 |
Return a series of lines for this target |
|
51 |
""" |
|
52 |
||
53 |
if self.comment : |
|
54 |
yield "# %s" % (self.comment, ) |
|
55 |
||
56 |
yield self.statement('Target', self.target_def) |
|
57 |
yield self.statement('MaxBytes', self.max_bytes) |
|
58 |
yield self.statement('Title', self.title) |
|
59 |
||
60 |
class Host (object) : |
|
61 |
""" |
|
62 |
A SNMP target |
|
63 |
""" |
|
64 |
||
65 |
def __init__ (self, name, hostname, snmp_community) : |
|
66 |
""" |
|
67 |
Store values corresponding to the MRTG Target stanza bits: |
|
68 |
name - short name |
|
69 |
hostname - the snmp target |
|
70 |
snmp_community - the SNMP community to use |
|
71 |
""" |
|
72 |
||
73 |
self.name = name |
|
74 |
self.hostname = hostname |
|
75 |
self.snmp_community = snmp_community |
|
76 |
||
9 | 77 |
def port (self, port_name, mbps, port=None, desc=None, port_title=None) : |
3 | 78 |
""" |
79 |
Create and return a Target describing the given port/interface on this host. |
|
80 |
""" |
|
81 |
||
82 |
# figure out which ifref to use |
|
83 |
ifid = iftitle = None |
|
84 |
||
85 |
for prefix, tprefix, value in ( |
|
86 |
('', "#", port), |
|
9 | 87 |
('\\', "", desc), |
3 | 88 |
) : |
89 |
if value : |
|
90 |
# ok, use this |
|
91 |
ifid = prefix + str(value) |
|
92 |
||
93 |
if port_title : |
|
94 |
iftitle = port_title |
|
95 |
||
96 |
else : |
|
97 |
iftitle = tprefix + str(value) |
|
98 |
||
99 |
break |
|
100 |
||
101 |
if not ifid : |
|
102 |
raise ValueError("No interface identifier given") |
|
103 |
||
104 |
# compose |
|
105 |
name = "%s_%s" % (self.hostname, port_name) |
|
106 |
target = "%s:%s@%s" % (ifid, self.snmp_community, self.hostname) |
|
107 |
max_bytes = mbps * 1000 * 1000 / 8 |
|
108 |
title = "Traffic @ %s %s -> %s" % (self.name, iftitle, port_name) |
|
109 |
||
110 |
# build and return Target |
|
111 |
return Target(name, target, max_bytes, title) |
|
112 |
||
113 |
def write_cfg (path, targets) : |
|
114 |
""" |
|
115 |
Write out the given targets to the given file |
|
116 |
""" |
|
117 |
||
118 |
fh = open(path, 'w') |
|
119 |
||
120 |
# generate() each target |
|
121 |
for target in targets : |
|
122 |
for line in target.generate() : |
|
123 |
# output line |
|
124 |
fh.write(line + '\n') |
|
125 |
||
126 |
# separate by empty line |
|
127 |
fh.write('\n') |
|
128 |
||
129 |
fh.close() |
|
130 |