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

Source Code for Module Products.ZenRRD.zentestcommand

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