Package Products :: Package ZenRRD :: Package parsers :: Module JSON
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenRRD.parsers.JSON

  1  ############################################################################## 
  2  #  
  3  # Copyright (C) Zenoss, Inc. 2012, all rights reserved. 
  4  #  
  5  # This content is made available according to terms specified in 
  6  # License.zenoss under the directory where your Zenoss product is installed. 
  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   
53 -def stringify_keys(dictionary):
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):
68 - def processResults(self, cmd, result):
69 data = None 70 71 try: 72 data = json.loads(cmd.result.output) 73 except Exception, ex: 74 # See NOTE below. If this event ever occurs it will not auto-clear. 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 # NOTE: It might be a good idea to send a clear event for the potential 87 # parse error above. However, this would end up flooding clear events 88 # that are almost always useless. I've chosen to trust the executed 89 # plugin to always return JSON data of some sort. 90 91 # Pass incoming events straight through. 92 result.events.extend(map(stringify_keys, data.get('events', []))) 93 94 # Map incoming values to their components and datapoints. 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