Package DataCollector :: Module CollectorClient
[hide private]
[frames] | no frames]

Source Code for Module DataCollector.CollectorClient

  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__="""CollectorClient 
 15   
 16  Base class for Telnet and Ssh client collectors 
 17   
 18  Device Tree Parameters are: 
 19   
 20  zCommandLoginTries - number of times to try login default: 1 
 21  zCommandPathList - list of path to check for a command 
 22  zCommandExistanceCheck - shell command issued to look for executible 
 23                          must echo succ if executible is found 
 24                          default: test -f executible 
 25   
 26  $Id: CollectorClient.py,v 1.5 2004/04/05 02:05:30 edahl Exp $""" 
 27   
 28   
 29  __version__ = "$Revision: 1.5 $"[11:-2] 
 30   
 31  import os, sys, getpass 
 32  import logging 
 33  log = logging.getLogger("zen.CmdClient") 
 34   
 35  from twisted.internet import protocol, reactor 
 36   
 37  #Default option values 
 38  if os.environ.has_key('USER'): 
 39      defaultUsername = os.environ['USER'] 
 40  else: 
 41      defaultUsername = "" 
 42  defaultPassword = "" 
 43  defaultLoginTries = 1 
 44  defaultLoginTimeout = 10 
 45  defaultCommandTimeout = 10  
 46  defaultKeyPath = '~/.ssh/id_dsa' 
 47  defaultSearchPath = [] 
 48  defaultExistanceTest = 'test -f %s' 
 49           
 50   
51 -class CollectorClient(protocol.ClientFactory):
52 53 maintainConnection = False 54 cmdindex = 0 55
56 - def __init__(self, hostname, ip, port, commands=None, options=None, 57 device=None, datacollector=None, log=None):
58 59 self.hostname = hostname 60 self.ip = ip 61 self.port = port 62 commands = commands or [] 63 self.cmdmap = {} 64 self._commands = [] 65 for pname, cmd in commands: 66 self.cmdmap[cmd] = pname 67 self._commands.append(cmd) 68 self.device = device 69 self.results = [] 70 self.protocol = None 71 self.datacollector = datacollector 72 73 if options: 74 defaultUsername = options.username 75 defaultPassword = options.password 76 defaultLoginTries = options.loginTries 77 defaultLoginTimeout = options.loginTimeout 78 defaultCommandTimeout = options.commandTimeout 79 defaultKeyPath = options.keyPath 80 defaultSearchPath = options.searchPath 81 defaultExistanceTest = options.existenceTest 82 83 if device: # if we are in zope look for parameters in aq path 84 self.username = getattr(device, 85 'zCommandUsername', defaultUsername) 86 self.password = getattr(device, 87 'zCommandPassword', defaultPassword) 88 self.loginTries = getattr(device, 89 'zCommandLoginTries', defaultLoginTries) 90 self.loginTimeout = getattr(device, 91 'zCommandLoginTimeout', defaultLoginTimeout) 92 self.commandTimeout = getattr(device, 93 'zCommandCommandTimeout', defaultCommandTimeout) 94 self.keyPath = getattr(device, 95 'zKeyPath', defaultKeyPath) 96 self.port = getattr(device, 'zCommandPort', self.port) 97 self.searchPath = getattr(device, 98 'zCommandSearchPath', defaultSearchPath) 99 self.existenceTest = getattr(device, 100 'zCommandExistanceTest', defaultExistanceTest) 101 else: 102 self.username = defaultUsername 103 self.password = defaultPassword 104 self.loginTries = defaultLoginTries 105 self.loginTimeout = defaultLoginTimeout 106 self.commandTimeout = defaultCommandTimeout 107 self.keyPath = defaultKeyPath 108 self.searchPath = defaultSearchPath 109 self.existenceTest = defaultExistanceTest
110 111 112 113
114 - def addCommand(self, command):
115 if type(command) == type(''): 116 self._commands.append(command) 117 else: 118 self._commands.extend(command)
119 120
121 - def addResult(self, command, data, exitCode):
122 "add a result pair to the results store" 123 pname = self.cmdmap.get(command, command) 124 self.results.append((pname, data))
125 126
127 - def getCommands(self):
128 return self._commands
129 130
131 - def getResults(self):
132 return self.results
133 134
135 - def commandsFinished(self):
136 """called by protocol to see if all commands have been run""" 137 return len(self.results) == len(self._commands)
138 139
140 - def clientFinished(self):
141 """tell the datacollector that we are all done""" 142 log.info("command client finished collection for %s",self.hostname) 143 self.cmdindex = 0 144 if self.datacollector: 145 self.datacollector.clientFinished(self)
146 147 148
149 -def buildOptions(parser=None, usage=None):
150 "build options list that both telnet and ssh use" 151 152 153 if not usage: 154 usage = "%prog [options] hostname[:port] command" 155 156 if not parser: 157 from optparse import OptionParser 158 parser = OptionParser(usage=usage, 159 version="%prog " + __version__) 160 161 parser.add_option('-u', '--user', 162 dest='username', 163 default=defaultUsername, 164 help='Login username') 165 parser.add_option('-P', '--password', 166 dest='password', 167 default=defaultPassword, 168 help='Login password') 169 parser.add_option('-t', '--loginTries', 170 dest='loginTries', 171 default=defaultLoginTries, 172 type = 'int', 173 help='number of times to try login') 174 parser.add_option('-L', '--loginTimeout', 175 dest='loginTimeout', 176 type = 'float', 177 default = defaultLoginTimeout, 178 help='timeout login expect statments') 179 parser.add_option('-T', '--commandTimeout', 180 dest='commandTimeout', 181 type = 'float', 182 default = defaultCommandTimeout, 183 help='timeout when issuing a command') 184 parser.add_option('-K', '--keyPath', 185 dest='keyPath', 186 default = defaultKeyPath, 187 help='Path to use when looking for keys') 188 parser.add_option('-s', '--searchPath', 189 dest='searchPath', 190 default=defaultSearchPath, 191 help='Path to use when looking for commands') 192 parser.add_option('-e', '--existenceTest', 193 dest='existenceTest', 194 default=defaultExistanceTest, 195 help='how to check for command') 196 if not parser.has_option('-v'): 197 parser.add_option('-v', '--logseverity', 198 dest='logseverity', 199 default=logging.INFO, 200 type='int', 201 help='Logging severity threshold') 202 return parser
203 204
205 -def parseOptions(parser, port):
206 options, args = parser.parse_args() 207 if len(args) < 2: 208 parser.print_help() 209 sys.exit(1) 210 if args[0].find(':') > -1: 211 hostname,port = args[0].split(':') 212 else: 213 hostname = args[0] 214 options.hostname = hostname 215 options.port = port 216 options.commands = args[1:] 217 return options
218