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

Source Code for Module ZenRRD.zentestcommand

  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  __doc__=''' ZenTestCommand 
 15   
 16  Test the run of a ZenCommand and print output 
 17   
 18  $Id$''' 
 19   
 20  __version__ = "$Revision$"[11:-2] 
 21   
 22  import os 
 23  import popen2 
 24  import fcntl 
 25  import time 
 26  import sys 
 27  import select 
 28  import logging 
 29  import signal 
 30  from copy import copy 
 31  log = logging.getLogger("zen.zentestcommand") 
 32   
 33  import Globals 
 34  from Products.ZenUtils.ZenScriptBase import ZenScriptBase 
 35   
 36  snmptemplate = ("snmpwalk -c%(zSnmpCommunity)s " 
 37                  "-%(zSnmpVer)s %(manageIp)s %(oid)s") 
 38   
 39   
40 -class TestRunner(ZenScriptBase):
41
42 - def __init__(self):
43 ZenScriptBase.__init__(self, connect=True) 44 self.getDataRoot()
45
46 - def getCommand(self, devName=None, dsName=None):
47 if not devName: devName = self.options.devName 48 if not dsName: dsName = self.options.dsName 49 devices = self.dmd.getDmdRoot("Devices") 50 device = devices.findDevice(devName) 51 if not device: 52 self.write('Could not find device %s.' % devName) 53 sys.exit(1) 54 dataSource = None 55 for templ in device.getRRDTemplates(): 56 for ds in templ.getRRDDataSources(): 57 if ds.id==dsName: 58 dataSource = ds 59 break 60 if dataSource: break 61 if not dataSource: 62 self.write('No datasource %s applies to device %s.' % (dsName, 63 devName)) 64 sys.exit(1) 65 if dataSource.sourcetype=='COMMAND': 66 return dataSource.getCommand(device) 67 elif dataSource.sourcetype=='SNMP': 68 snmpinfo = copy(device.getSnmpConnInfo().__dict__) 69 snmpinfo['oid'] = dataSource.getDescription() 70 return snmptemplate % snmpinfo 71 else: 72 self.write('No COMMAND or SNMP datasource %s applies to device %s.' % ( 73 dsName, devName))
74
75 - def execute(self, cmd):
76 child = popen2.Popen4(cmd) 77 flags = fcntl.fcntl(child.fromchild, fcntl.F_GETFL) 78 fcntl.fcntl(child.fromchild, fcntl.F_SETFL, flags | os.O_NDELAY) 79 pollPeriod = 1 80 timeout = max(self.options.timeout, 1) 81 endtime = time.time() + timeout 82 firstPass = True 83 while time.time() < endtime and ( 84 firstPass or child.poll()==-1): 85 firstPass = False 86 r,w,e = select.select([child.fromchild],[],[],pollPeriod) 87 if r: 88 t = child.fromchild.read() 89 if t: 90 self.write(t) 91 if child.poll()==-1: 92 self.write('Command timed out') 93 os.kill(child.pid, signal.SIGKILL)
94
95 - def write(self, text):
96 print text
97
98 - def run(self):
99 device, dsName = self.options.device, self.options.dsName 100 if not (device and dsName): 101 self.write("Must provide a device and datasource.") 102 sys.exit(2) 103 d = self.getCommand(device, dsName) 104 self.execute(d)
105
106 - def buildOptions(self):
107 ZenScriptBase.buildOptions(self) 108 self.parser.add_option('-d', '--device', 109 dest="device", 110 help="Device on which to test command") 111 self.parser.add_option('--datasource', 112 dest="dsName", 113 help="COMMAND datasource to test") 114 self.parser.add_option('-t', '--timeout', 115 dest="timeout", default=1, type="int", 116 help="Command timeout")
117 118 119 if __name__=='__main__': 120 tr = TestRunner() 121 tr.run() 122