1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__ = """Nagios
15 Uses the Nagios API defintions from http://nagios.sourceforge.net/docs/3_0/pluginapi.html
16 and from
17 http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOUTPUT
18 """
19
20 import re
21
22
23 perfParser = re.compile(r"""(([^ =']+|'([^']+)')=([-0-9.eE]+)\S*)""")
24
25 from Products.ZenUtils.Utils import getExitMessage
26 from Products.ZenRRD.CommandParser import CommandParser
27
29
31 """
32 Convert the plugin output into component parts:
33 summary, message, performance_data
34
35 If the message is None, then there is an error.
36 """
37 summary, msg, rawPerfData = "", [], []
38 if not output.strip():
39 return "No output from plugin", None, None
40
41
42 first_plus_rest = output.split('\n',1)
43 firstLine = first_plus_rest[0].strip()
44 if not firstLine:
45 return "No output from plugin", None, None
46
47 summaryNPerf = firstLine.split('|')
48 if len(summaryNPerf) > 2:
49 return "Too many |'s in output from plugin", None, None
50
51 summary = summaryNPerf[0]
52
53 rawPerfData = summaryNPerf[1:]
54
55
56 multi = first_plus_rest[1:]
57 if not multi:
58 return summary, summary, rawPerfData
59
60 service_output_plus_perf = multi[0].split('|')
61 if len(service_output_plus_perf) > 2:
62 return "Too many |'s in output from plugin", None, None
63
64 msg = service_output_plus_perf[0]
65 if len(service_output_plus_perf) == 2:
66 rawPerfData += service_output_plus_perf[1].split('\n')
67 return summary, msg, rawPerfData
68
69
71 """
72 Create a dictionary of datapoint:value entries from
73 the plugin output.
74 This funtion removes a ' (represented as '' in the label)
75 from the label. There's just too much opportunity to mess
76 something up by keeping a shell meta-character.
77 """
78 perfData = {}
79 all_data = ' '.join(rawPerfData)
80
81 all_data = all_data.replace("''", "")
82
83 for full_match, label, quote_label, value in perfParser.findall(all_data):
84 if quote_label:
85 label = quote_label
86 try:
87 value = float(value.strip())
88 except:
89 value = 'U'
90 perfData[label] = value
91
92 return perfData
93
94
96 output = cmd.result.output
97 exitCode = cmd.result.exitCode
98 severity = cmd.severity
99 if exitCode == 0:
100 severity = 0
101 elif exitCode == 2:
102 severity = min(severity + 1, 5)
103
104 summary, msg, rawPerfData = self.splitMultiLine(output)
105
106 evt = dict(device=cmd.deviceConfig.device,
107 summary=summary, message=output,
108 severity=severity, component=cmd.component,
109 eventKey=cmd.eventKey, eventClass=cmd.eventClass,
110 performanceData=rawPerfData,
111 )
112
113 if msg is None:
114 evt['error_codes'] = 'Datasource: %s - Code: %s - Msg: %s' % (
115 cmd.name, exitCode,
116 getExitMessage(exitCode))
117 result.events.append(evt)
118 return
119
120 result.events.append(evt)
121
122 perfData = self.processPerfData(rawPerfData)
123 for dp in cmd.points:
124 if dp.id in perfData:
125 result.values.append( (dp, perfData[dp.id]) )
126