source: trunk/daemon/togad.py @ 8

Last change on this file since 8 was 8, checked in by bastiaans, 19 years ago

daemon/togad.py:

Added debugging levels
Added processor class and daemon/run functions

File size: 3.4 KB
RevLine 
[3]1#!/usr/bin/env python
2
[5]3from xml.sax import make_parser
4from xml.sax.handler import ContentHandler
5import socket
6import sys
[3]7
[8]8# Specify debugging level here;
9#
10# >10 = metric XML
11# >9  = host,cluster,grid,ganglia XML
12#
13DEBUG_LEVEL = 9
[6]14
[8]15"""
16This is TOrque-GAnglia's data Daemon
17"""
18
[6]19class GangliaXMLHandler( ContentHandler ):
[8]20        "Parse Ganglia's XML"
[3]21
[6]22        metrics = [ ]
23
24        def startElement( self, name, attrs ):
[8]25                "Store appropriate data from xml start tags"
[3]26
[7]27                if name == 'GANGLIA_XML':
28                        self.XMLSource = attrs.get('SOURCE',"")
29                        self.gangliaVersion = attrs.get('VERSION',"")
[8]30                        if (DEBUG_LEVEL>9): print 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion )
[6]31
[7]32                elif name == 'GRID':
33                        self.gridName = attrs.get('NAME',"")
[8]34                        if (DEBUG_LEVEL>9): print '`-Grid found: %s' %( self.gridName )
[6]35
[7]36                elif name == 'CLUSTER':
37                        self.clusterName = attrs.get('NAME',"")
[8]38                        if (DEBUG_LEVEL>9): print ' |-Cluster found: %s' %( self.clusterName )
[6]39
[7]40                elif name == 'HOST':     
41                        self.hostName = attrs.get('NAME',"")
42                        self.hostIp = attrs.get('IP',"")
43                        self.hostReported = attrs.get('REPORTED',"")
[8]44                        if (DEBUG_LEVEL>9): print ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported )
[6]45
[7]46                elif name == 'METRIC':
[6]47                        myMetric = { }
[7]48                        myMetric['name'] = attrs.get('NAME',"")
49                        myMetric['val'] = attrs.get('VAL',"")
[6]50
51                        self.metrics.append( myMetric ) 
[8]52                        if (DEBUG_LEVEL>10): print ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] )
[6]53
[5]54                return
[3]55
[7]56        #def endElement( self, name ):
57                #if name == 'ganglia_xml':
[3]58
[7]59                #if name == 'grid':
[3]60
[7]61                #if name == 'cluster':
[6]62
[7]63                #if name == 'host':     
[6]64
65                #if name == 'metric':
66
[5]67class GangliaXMLGatherer:
[8]68        "Setup a connection and file object to Ganglia's XML"
[3]69
[8]70        s = None
71
72        def __init__( self, host, port ):
73                "Store host and port for connection"
74
[5]75                self.host = host
76                self.port = port
[3]77
[8]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 ):
[5]87                        af, socktype, proto, canonname, sa = res
88                        try:
[8]89                                self.s = socket.socket( af, socktype, proto )
[5]90                        except socket.error, msg:
[8]91                                self.s = None
[5]92                                continue
93                        try:
[8]94                                self.s.connect( sa )
[5]95                        except socket.error, msg:
[8]96                                self.s.close()
97                                self.s = None
[5]98                                continue
99                        break
[3]100
[8]101                if self.s is None:
102                        print 'Could not open socket'
[5]103                        sys.exit(1)
104
[8]105                return self.s.makefile( 'r' )
[5]106
[8]107class GangliaXMLProcessor:
[5]108
[8]109        def daemon(self):
110                "Run as daemon forever"
[5]111
[8]112                self.DAEMON = 1
[5]113
[8]114                # Fork the first child
115                #
116                pid = os.fork()
117                if pid > 0:
118                        sys.exit(0)  # end parrent
[7]119
[8]120                # creates a session and sets the process group ID
121                #
122                os.setsid()
[7]123
[8]124                # Fork the second child
125                #
126                pid = os.fork()
127                if pid > 0:
128                        sys.exit(0)  # end parrent
[5]129
[8]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() )
162
163def main():
164        "Program startup"
165
166        myProcessor = GangliaXMLProcessor()
167        myProcessor.processXML()
168
[5]169# Let's go
170main()
Note: See TracBrowser for help on using the repository browser.