1
2
3
4
5
6
7
8
9
10
11 """Nagios
12
13 Uses the Nagios API defintions from
14 http://nagios.sourceforge.net/docs/3_0/pluginapi.html and from
15 http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOUTPUT
16 """
17
18 import re
19
20 from Products.ZenUtils.Utils import getExitMessage
21 from Products.ZenRRD.CommandParser import CommandParser
22
23
24
25
26
27
28
29
30
31
32
33
34 perfParser = re.compile(r"""(([^ =']+|'([^']+)')=([-0-9.eE]+)\S*)""")
38 """
39 Raised by splitMultLine when plugin output is not parseable.
40 """
41
42
43 -class Nagios(CommandParser):
44
45 @staticmethod
47 """
48 Convert the plugin output into component parts:
49 summary, performance_data
50 Empty output or no performance data raises a _BadData exception.
51 """
52 output = output.strip()
53 if not output:
54 raise _BadData("No output from plugin")
55
56
57
58
59
60
61
62
63
64
65 lines = output.splitlines()
66 firstLine = lines[0].strip()
67 additionalLines = ' '.join(lines[1:])
68 text, perf = [], []
69
70 segments = firstLine.split('|')
71
72 if segments:
73 text.append(segments.pop(0))
74
75 while len(segments) > 1:
76 text.append(segments.pop(0))
77
78 if segments:
79 perf.append(segments.pop(0))
80
81 if additionalLines:
82
83 segments = additionalLines.split('|')
84
85 if segments:
86 text.extend(segments.pop(0).splitlines())
87
88 if segments:
89 perf.extend(segments.pop(0).splitlines())
90
91
92 if not perf:
93 raise _BadData("No performance data from plugin")
94
95 return text, perf
96
98 """
99 Create a dictionary of datapoint:value entries from
100 the plugin output.
101 This funtion removes a ' (represented as '' in the label)
102 from the label. There's just too much opportunity to mess
103 something up by keeping a shell meta-character.
104 """
105 perfData = {}
106 all_data = ' '.join(rawPerfData)
107
108 all_data = all_data.replace("''", "")
109
110 for _, label, quote_label, value in perfParser.findall(all_data):
111 if quote_label:
112 label = quote_label
113 try:
114 value = float(value.strip())
115 except:
116 value = 'U'
117 perfData[label] = value
118
119 return perfData
120
122 output = cmd.result.output
123 exitCode = cmd.result.exitCode
124 severity = cmd.severity
125 if exitCode == 0:
126 severity = 0
127 elif exitCode == 2:
128 severity = min(severity + 1, 5)
129
130 evt = {
131 "device": cmd.deviceConfig.device,
132 "message": output,
133 "severity": severity,
134 "component": cmd.component,
135 "eventKey": cmd.eventKey,
136 "eventClass": cmd.eventClass,
137 }
138 try:
139 summary, rawPerfData = self.splitMultiLine(output)
140 except _BadData as ex:
141 evt.update({
142 "error_codes": "Datasource: %s - Code: %s - Msg: %s" % (
143 cmd.name, exitCode, getExitMessage(exitCode)
144 ),
145 "performanceData": None,
146 "summary": str(ex),
147 })
148 else:
149 evt.update({
150 "performanceData": rawPerfData,
151 "summary": summary,
152 })
153 perfData = self.processPerfData(rawPerfData)
154 for dp in cmd.points:
155 if dp.id in perfData:
156 result.values.append((dp, perfData[dp.id]))
157 result.events.append(evt)
158