Changeset 8 for trunk/daemon


Ignore:
Timestamp:
03/21/05 15:50:19 (19 years ago)
Author:
bastiaans
Message:

daemon/togad.py:

Added debugging levels
Added processor class and daemon/run functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/togad.py

    r7 r8  
    66import sys
    77
    8 DEBUG = 1
     8# Specify debugging level here;
     9#
     10# >10 = metric XML
     11# >9  = host,cluster,grid,ganglia XML
     12#
     13DEBUG_LEVEL = 9
     14
     15"""
     16This is TOrque-GAnglia's data Daemon
     17"""
    918
    1019class GangliaXMLHandler( ContentHandler ):
    11         """
    12         Parse/Handle XML
    13         """
     20        "Parse Ganglia's XML"
    1421
    1522        metrics = [ ]
    1623
    17         #def __init__ ( self ):
    18                 #self.isHostElement, self.isMetricElement, self.isGridElement, self.isClusterElement = 0, 0, 0, 0
    19                 #self.isGangliaXMLElement = 0
    20    
    2124        def startElement( self, name, attrs ):
     25                "Store appropriate data from xml start tags"
    2226
    2327                if name == 'GANGLIA_XML':
    2428                        self.XMLSource = attrs.get('SOURCE',"")
    2529                        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 )
    2731
    2832                elif name == 'GRID':
    2933                        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 )
    3135
    3236                elif name == 'CLUSTER':
    3337                        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 )
    3539
    3640                elif name == 'HOST':     
     
    3842                        self.hostIp = attrs.get('IP',"")
    3943                        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 )
    4145
    4246                elif name == 'METRIC':
     
    4650
    4751                        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'] )
    4953
    5054                return
     
    6266
    6367class GangliaXMLGatherer:
    64         """
    65         Connect to a gmetad and return fd
    66         """
     68        "Setup a connection and file object to Ganglia's XML"
    6769
    68         def __init__(self, host, port):
     70        s = None
     71
     72        def __init__( self, host, port ):
     73                "Store host and port for connection"
     74
    6975                self.host = host
    7076                self.port = port
    7177
    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 ):
    7587                        af, socktype, proto, canonname, sa = res
    7688                        try:
    77                                 s = socket.socket(af, socktype, proto)
     89                                self.s = socket.socket( af, socktype, proto )
    7890                        except socket.error, msg:
    79                                 print socket.error
    80                                 print msg
    81                                 s = None
     91                                self.s = None
    8292                                continue
    8393                        try:
    84                                 print 'connected'
    85                                 s.connect(sa)
    86                                 #s.setblocking(1)
     94                                self.s.connect( sa )
    8795                        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
    9298                                continue
    9399                        break
    94100
    95                 if s is None:
    96                         print 'could not open socket'
     101                if self.s is None:
     102                        print 'Could not open socket'
    97103                        sys.exit(1)
    98104
    99                 return s.makefile( 'r' )
    100                 #return s
     105                return self.s.makefile( 'r' )
    101106
    102                 #s.send('Hello, world')
    103                 #data = s.recv(1024)
    104                 #s.close()
    105                 #print 'Received', `data`
     107class 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() )
    106162
    107163def main():
    108         """
    109         My Main
    110         """
     164        "Program startup"
    111165
    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()
    122168
    123169# Let's go
Note: See TracChangeset for help on using the changeset viewer.