Changeset 8 for trunk/daemon
- Timestamp:
- 03/21/05 15:50:19 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/daemon/togad.py
r7 r8 6 6 import sys 7 7 8 DEBUG = 1 8 # Specify debugging level here; 9 # 10 # >10 = metric XML 11 # >9 = host,cluster,grid,ganglia XML 12 # 13 DEBUG_LEVEL = 9 14 15 """ 16 This is TOrque-GAnglia's data Daemon 17 """ 9 18 10 19 class GangliaXMLHandler( ContentHandler ): 11 """ 12 Parse/Handle XML 13 """ 20 "Parse Ganglia's XML" 14 21 15 22 metrics = [ ] 16 23 17 #def __init__ ( self ):18 #self.isHostElement, self.isMetricElement, self.isGridElement, self.isClusterElement = 0, 0, 0, 019 #self.isGangliaXMLElement = 020 21 24 def startElement( self, name, attrs ): 25 "Store appropriate data from xml start tags" 22 26 23 27 if name == 'GANGLIA_XML': 24 28 self.XMLSource = attrs.get('SOURCE',"") 25 29 self.gangliaVersion = attrs.get('VERSION',"") 26 if DEBUG: print 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion )30 if (DEBUG_LEVEL>9): print 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion ) 27 31 28 32 elif name == 'GRID': 29 33 self.gridName = attrs.get('NAME',"") 30 if DEBUG: print '`-Grid found: %s' %( self.gridName )34 if (DEBUG_LEVEL>9): print '`-Grid found: %s' %( self.gridName ) 31 35 32 36 elif name == 'CLUSTER': 33 37 self.clusterName = attrs.get('NAME',"") 34 if DEBUG: print ' |-Cluster found: %s' %( self.clusterName )38 if (DEBUG_LEVEL>9): print ' |-Cluster found: %s' %( self.clusterName ) 35 39 36 40 elif name == 'HOST': … … 38 42 self.hostIp = attrs.get('IP',"") 39 43 self.hostReported = attrs.get('REPORTED',"") 40 if DEBUG: print ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported )44 if (DEBUG_LEVEL>9): print ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported ) 41 45 42 46 elif name == 'METRIC': … … 46 50 47 51 self.metrics.append( myMetric ) 48 if DEBUG: print ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] )52 if (DEBUG_LEVEL>10): print ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] ) 49 53 50 54 return … … 62 66 63 67 class GangliaXMLGatherer: 64 """ 65 Connect to a gmetad and return fd 66 """ 68 "Setup a connection and file object to Ganglia's XML" 67 69 68 def __init__(self, host, port): 70 s = None 71 72 def __init__( self, host, port ): 73 "Store host and port for connection" 74 69 75 self.host = host 70 76 self.port = port 71 77 72 def getFileDescriptor(self): 73 s = None 74 for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM): 78 def __del__( self ): 79 "Kill the socket before we leave" 80 81 self.s.close() 82 83 def getFileObject( self ): 84 "Connect, and return a file object" 85 86 for res in socket.getaddrinfo( self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM ): 75 87 af, socktype, proto, canonname, sa = res 76 88 try: 77 s = socket.socket(af, socktype, proto)89 self.s = socket.socket( af, socktype, proto ) 78 90 except socket.error, msg: 79 print socket.error 80 print msg 81 s = None 91 self.s = None 82 92 continue 83 93 try: 84 print 'connected' 85 s.connect(sa) 86 #s.setblocking(1) 94 self.s.connect( sa ) 87 95 except socket.error, msg: 88 s.close() 89 print socket.error 90 print msg 91 s = None 96 self.s.close() 97 self.s = None 92 98 continue 93 99 break 94 100 95 if s is None:96 print ' could not open socket'101 if self.s is None: 102 print 'Could not open socket' 97 103 sys.exit(1) 98 104 99 return s.makefile( 'r' ) 100 #return s 105 return self.s.makefile( 'r' ) 101 106 102 #s.send('Hello, world') 103 #data = s.recv(1024) 104 #s.close() 105 #print 'Received', `data` 107 class GangliaXMLProcessor: 108 109 def daemon(self): 110 "Run as daemon forever" 111 112 self.DAEMON = 1 113 114 # Fork the first child 115 # 116 pid = os.fork() 117 if pid > 0: 118 sys.exit(0) # end parrent 119 120 # creates a session and sets the process group ID 121 # 122 os.setsid() 123 124 # Fork the second child 125 # 126 pid = os.fork() 127 if pid > 0: 128 sys.exit(0) # end parrent 129 130 # Go to the root directory and set the umask 131 # 132 os.chdir('/') 133 os.umask(0) 134 135 sys.stdin.close() 136 sys.stdout.close() 137 sys.stderr.close() 138 139 os.open('/dev/null', 0) 140 os.dup(0) 141 os.dup(0) 142 143 self.run() 144 145 def run(self): 146 "Main thread" 147 148 while ( 1 ): 149 self.processXML() 150 time.sleep( 5 ) 151 152 def processXML( self ): 153 "Process XML" 154 155 myXMLGatherer = GangliaXMLGatherer( 'localhost', 8651 ) 156 157 myParser = make_parser() 158 myHandler = GangliaXMLHandler() 159 myParser.setContentHandler( myHandler ) 160 161 myParser.parse( myXMLGatherer.getFileObject() ) 106 162 107 163 def main(): 108 """ 109 My Main 110 """ 164 "Program startup" 111 165 112 myXMLGatherer = GangliaXMLGatherer( 'localhost', 8651 ) 113 114 myParser = make_parser() 115 myHandler = GangliaXMLHandler() 116 myParser.setContentHandler( myHandler ) 117 118 #for line in myXMLGatherer.getFileDescriptor().readlines(): 119 # print line 120 121 myParser.parse( myXMLGatherer.getFileDescriptor() ) 166 myProcessor = GangliaXMLProcessor() 167 myProcessor.processXML() 122 168 123 169 # Let's go
Note: See TracChangeset
for help on using the changeset viewer.