1
2
3
4
5
6
7
8
9
10
11 """JSON
12
13 General purpose JSON parser for handling JSON objects matching the following
14 template. This allows many datapoints for many components to be collected
15 from a single COMMAND datasource. It also allows for collection of many
16 events from a single COMMAND datasource.
17
18 {
19 "values": {
20 "component_id_1": {
21 "datapoint1": 123.4,
22 "datapoint2": 987.6
23 },
24
25 "component_id_2": {
26 "datapoint1": 56.7,
27 "datapoint2": 54.3
28 }
29 },
30
31 "events": [
32 {
33 "severity": 2,
34 "other_field_1": "value for other field",
35 "summary": "event summary"
36 },
37
38 {
39 "severity": 3,
40 "other_field_1": "another value for other field",
41 "summary": "another event summary"
42 }
43 ]
44 }
45
46 """
47
48 import json
49
50 from Products.ZenRRD.CommandParser import CommandParser
51
52
54 """Convert all keys of given dictionary to strings.
55
56 During serialization and deserialization between the collector and hub we
57 need to enforce that dictionary keys are plan, not unicode, strings.
58
59 """
60 fixed_dictionary = {}
61 for k, v in dictionary.items():
62 fixed_dictionary[str(k)] = v
63
64 return fixed_dictionary
65
66
67 -class JSON(CommandParser):
69 data = None
70
71 try:
72 data = json.loads(cmd.result.output)
73 except Exception, ex:
74
75 result.events.append({
76 'severity': cmd.severity,
77 'summary': 'error parsing command output',
78 'eventKey': cmd.command,
79 'eventClass': cmd.eventClass,
80 'command_output': cmd.result.output,
81 'exception': str(ex),
82 })
83
84 return
85
86
87
88
89
90
91
92 result.events.extend(map(stringify_keys, data.get('events', [])))
93
94
95 if len(data.get('values', {}).keys()) > 0:
96 for point in cmd.points:
97 if point.component not in data['values']:
98 continue
99
100 if point.id not in data['values'][point.component]:
101 continue
102
103 result.values.append((
104 point, data['values'][point.component][point.id]))
105
106 return result
107