Package ZenRRD :: Module CommandParser
[hide private]
[frames] | no frames]

Source Code for Module ZenRRD.CommandParser

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  import logging 
 15  log = logging.getLogger('zen.ZenRRD.CommandParser') 
 16   
 17  from pprint import pformat 
 18   
19 -class ParsedResults:
20
21 - def __init__(self):
22 self.events = [] # list of event dictionaries 23 self.values = [] # list of (DataPointConfig, value)
24
25 - def __repr__(self):
26 args = (pformat(self.events), pformat(self.values)) 27 return "ParsedResults\n events: %s\n values: %s}" % args
28
29 -class CommandParser:
30
31 - def dataForParser(self, context, datapoint):
32 return {}
33
34 - def processResults(self, cmd, results):
35 """ 36 Process the results of a running a command. 37 38 @type cmd: Products.ZenRRD.zencommand.Cmd 39 40 @param cmd: the results of running a command, with the 41 configuration from ZenHub 42 @param results: the values and events from the command output 43 @return: None. 44 """ 45 raise NotImplementedError
46 47 48 ParserCache = {} 49
50 -def _getParser(name):
51 """ 52 Import and create the parser for this command 53 """ 54 try: 55 return ParserCache[name] 56 except KeyError: 57 from Products.ZenUtils.Utils import importClass 58 klass = importClass('Products.ZenRRD.parsers.' + name) 59 instance = klass() 60 ParserCache[name] = instance 61 return instance
62
63 -def _getPackParser(name):
64 """ 65 Import and create the parser for this command 66 """ 67 try: 68 return ParserCache[name] 69 except KeyError: 70 from Products.ZenUtils.Utils import importClass 71 klass = importClass(name) 72 instance = klass() 73 ParserCache[name] = instance 74 return instance
75
76 -def getParser(name):
77 """ 78 Import and create the parser for this command 79 """ 80 err = ImportError("%s not found" % name) 81 try: 82 return _getParser(name) 83 except ImportError, err: 84 msg = "%s is not a core parser. Attempting to import it from " \ 85 "installed zenpacks." 86 log.debug(msg, name) 87 return _getPackParser(name)
88 89
90 -def getParserNames(dmd):
91 "Get the list of all parsers" 92 93 def looksLikeAPlugin(f): 94 if not f.startswith('_') and f.endswith('.py'): 95 return f[:-3]
96 97 import os 98 from Products.ZenUtils.Utils import zenPath 99 result = [] 100 for d, ds, fs in os.walk(zenPath('Products','ZenRRD', 'parsers')): 101 for f in fs: 102 plugin = looksLikeAPlugin(f) 103 if plugin: 104 plugin = os.path.join(d, plugin) 105 plugin = plugin.split(os.path.sep) 106 plugin = plugin[plugin.index('parsers') + 1:] 107 plugin = '.'.join(plugin) 108 result.append(plugin) 109 110 for pack in dmd.ZenPackManager.packs(): 111 root = pack.path('parsers') 112 for d, ds, fs in os.walk(root): 113 for f in fs: 114 plugin = looksLikeAPlugin(f) 115 if plugin: 116 plugin = os.path.join(d, plugin) 117 plugin = plugin.split(os.path.sep) 118 plugin = plugin[plugin.index('ZenPacks'):] 119 plugin = '.'.join(plugin) 120 result.append(plugin) 121 return sorted(result) 122