Package ZenStatus :: Module StatusMonitor
[hide private]
[frames] | no frames]

Source Code for Module ZenStatus.StatusMonitor

  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__="""StatusMonitor 
 15   
 16  Base class for makeing deamon programs 
 17   
 18  $Id: StatusMonitor.py,v 1.9 2003/08/29 20:33:10 edahl Exp $""" 
 19   
 20  __version__ = "$Revision: 1.9 $"[11:-2] 
 21   
 22  import signal 
 23  import os 
 24  import sys 
 25  import socket 
 26  import time 
 27   
 28  from Products.ZenUtils.ZenDaemon import ZenDaemon 
 29   
30 -class StatusMonitor(ZenDaemon):
31
32 - def __init__(self):
33 ZenDaemon.__init__(self) 34 self.dnstries = 3 35 self.forwarddnscache = {} 36 self.reversednscache = {}
37 38
39 - def forwardDnsLookup(self, hostname):
40 """try the forward lookup dnstries times if it fails look in cache""" 41 try: 42 ip = self._dnsLookup(socket.gethostbyname, hostname) 43 self.forwarddnscache[hostname] = ip 44 return ip 45 except socket.error: 46 if self.forwarddnscache.has_key(hostname): 47 return self.forwarddnscache[hostname] 48 else: 49 raise
50 51
52 - def reverseDnsLookup(self, ip):
53 """try the reverse lookup dnstries times if it fails look in cache""" 54 try: 55 hostname = self._dnsLookup(socket.gethostbyaddr, ip) 56 self.reversednscache[hostname] = ip 57 return ip 58 except socket.error: 59 if self.reversednscache.has_key(hostname): 60 return self.reversednscache[hostname] 61 else: 62 raise
63 64
65 - def _dnsLookup(self, function, target):
66 """try dns lookup dnstries times""" 67 ip = None 68 i=0 69 while 1: 70 try: 71 i+=1 72 ip = function(target) 73 except socket.error: 74 if i > self.getDnsTries(): 75 raise 76 if ip: break 77 return ip
78 79
80 - def getDnsTries(self):
81 if not hasattr(self, 'dnstries'): 82 self.dnstries=3 83 return self.dnstries
84 85
86 - def getConfig(self):
87 """handle errors when loading config from server 88 we try configtries times if no previous config is found""" 89 for i in range(self.options.configtries): 90 try: 91 self.loadConfig() 92 return 93 except SystemExit: raise 94 except: 95 if self.validConfig(): 96 self.log.exception( 97 "configuration load exception using previous configuration") 98 return 99 else: 100 self.log.exception('config load failed') 101 if i <= (self.options.configtries - 2): 102 self.log.warn( 103 "initial config load failed will retry") 104 time.sleep(self.options.configsleep) 105 else: 106 self.log.critical( 107 "initial config load failed %d times exiting" 108 % self.options.configtries) 109 sys.exit(2)
110 111
112 - def buildOptions(self):
113 ZenDaemon.buildOptions(self) 114 self.parser.add_option('-T', '--configtries', 115 dest='configtries', 116 default=5, 117 action="store", 118 help="How many times to retry config connection") 119 self.parser.add_option('-S', '--configsleep', 120 dest='configsleep', 121 default=20, 122 action="store", 123 help="How long to sleep between config connections")
124