source: trunk/daemon/togad.py @ 148

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

daemon/togad.py:

  • Fixed: rrdtool pipes were never closed, cuasing memory hogs and zombie processes
File size: 31.9 KB
Line 
1#!/usr/bin/env python
2
3import xml.sax
4import xml.sax.handler
5import socket
6import sys
7import string
8import os
9import os.path
10import time
11import threading
12import random
13from types import *
14import DBClass
15
16# Specify debugging level here;
17#
18# 11 = XML: metrics
19# 10 = XML: host, cluster, grid, ganglia
20# 9  = RRD activity, gmetad config parsing
21# 8  = RRD file activity
22# 6  = SQL
23# 1  = daemon threading
24#
25DEBUG_LEVEL = 1
26
27# Where is the gmetad.conf located
28#
29GMETAD_CONF = '/etc/gmetad.conf'
30
31# Where to grab XML data from
32# Normally: local gmetad (port 8651)
33#
34# Syntax: <hostname>:<port>
35#
36ARCHIVE_XMLSOURCE = "localhost:8651"
37
38# List of data_source names to archive for
39#
40# Syntax: [ "<clustername>", "<clustername>" ]
41#
42ARCHIVE_DATASOURCES = [ "LISA Cluster" ]
43
44# Where to store the archived rrd's
45#
46ARCHIVE_PATH = '/data/toga/rrds'
47
48# Amount of hours to store in one single archived .rrd
49#
50ARCHIVE_HOURS_PER_RRD = 12
51
52# Toga's SQL dbase to use
53#
54# Syntax: <hostname>/<database>
55#
56TOGA_SQL_DBASE = "localhost/toga"
57
58# Wether or not to run as a daemon in background
59#
60DAEMONIZE = 0
61
62######################
63#                    #
64# Configuration ends #
65#                    #
66######################
67
68###
69# You'll only want to change anything below here unless you
70# know what you are doing (i.e. your name is Ramon Bastiaans :D )
71###
72
73# What XML data types not to store
74#
75UNSUPPORTED_ARCHIVE_TYPES = [ 'string' ]
76
77# Maximum time (in seconds) a parsethread may run
78#
79PARSE_TIMEOUT = 60
80
81# Maximum time (in seconds) a storethread may run
82#
83STORE_TIMEOUT = 360
84
85"""
86This is TOrque-GAnglia's data Daemon
87"""
88
89class DataSQLStore:
90
91        db_vars = None
92        dbc = None
93
94        def __init__( self, hostname, database ):
95
96                self.db_vars = DBClass.InitVars(DataBaseName=database,
97                                User='root',
98                                Host=hostname,
99                                Password='',
100                                Dictionary='true')
101
102                try:
103                        self.dbc     = DBClass.DB(self.db_vars)
104                except DBClass.DBError, details:
105                        print 'Error in connection to db: %s' %details
106                        sys.exit(1)
107
108        def setDatabase(self, statement):
109                ret = self.doDatabase('set', statement)
110                return ret
111               
112        def getDatabase(self, statement):
113                ret = self.doDatabase('get', statement)
114                return ret
115
116        def doDatabase(self, type, statement):
117
118                debug_msg( 6, 'doDatabase(): %s: %s' %(type, statement) )
119                try:
120                        if type == 'set':
121                                result = self.dbc.Set( statement )
122                                self.dbc.Commit()
123                        elif type == 'get':
124                                result = self.dbc.Get( statement )
125                               
126                except DBClass.DBError, detail:
127                        operation = statement.split(' ')[0]
128                        print "%s operation on database failed while performing\n'%s'\n%s"\
129                                %(operation, statement, detail)
130                        sys.exit(1)
131
132                debug_msg( 6, 'doDatabase(): result: %s' %(result) )
133                return result
134
135        def getNodeId( self, hostname ):
136
137                id = self.getDatabase( "SELECT node_id FROM nodes WHERE node_hostname = '%s'" %hostname )
138
139                if len( id ) > 0:
140
141                        id = id[0][0]
142
143                        return id
144                else:
145                        return None
146
147        def getNodeIds( self, hostnames ):
148
149                ids = [ ]
150
151                for node in hostnames:
152
153                        id = self.getNodeId( node )
154
155                        if id:
156                                ids.append( id )
157
158                return ids
159
160        def getJobId( self, jobid ):
161
162                id = self.getDatabase( "SELECT job_id FROM jobs WHERE job_id = '%s'" %jobid )
163
164                if id:
165                        id = id[0][0]
166
167                        return id
168                else:
169                        return None
170
171        def addJob( self, job_id, jobattrs ):
172
173                if not self.getJobId( job_id ):
174
175                        self.mutateJob( 'insert', job_id, jobattrs ) 
176                else:
177                        self.mutateJob( 'update', job_id, jobattrs )
178
179        def mutateJob( self, action, job_id, jobattrs ):
180
181                job_values = [ 'name', 'queue', 'owner', 'requested_time', 'requested_memory', 'ppn', 'status', 'start_timestamp', 'stop_timestamp' ]
182
183                insert_col_str = 'job_id'
184                insert_val_str = "'%s'" %job_id
185                update_str = None
186
187                debug_msg( 6, 'mutateJob(): %s %s' %(action,job_id))
188
189                ids = [ ]
190
191                for valname, value in jobattrs.items():
192
193                        if valname in job_values and value != '':
194
195                                column_name = 'job_' + valname
196
197                                if action == 'insert':
198
199                                        if not insert_col_str:
200                                                insert_col_str = column_name
201                                        else:
202                                                insert_col_str = insert_col_str + ',' + column_name
203
204                                        if not insert_val_str:
205                                                insert_val_str = value
206                                        else:
207                                                insert_val_str = insert_val_str + ",'%s'" %value
208
209                                elif action == 'update':
210                                       
211                                        if not update_str:
212                                                update_str = "%s='%s'" %(column_name, value)
213                                        else:
214                                                update_str = update_str + ",%s='%s'" %(column_name, value)
215
216                        elif valname == 'nodes' and value:
217
218                                ids = self.addNodes( value )
219                                node_list = value
220
221                if action == 'insert':
222
223                        self.setDatabase( "INSERT INTO jobs ( %s ) VALUES ( %s )" %( insert_col_str, insert_val_str ) )
224
225                        if len( ids ) > 0:
226                                self.addJobNodes( job_id, ids )
227
228                elif action == 'update':
229
230                        self.setDatabase( "UPDATE jobs SET %s WHERE job_id=%s" %(update_str, job_id) )
231
232        def addNodes( self, hostnames ):
233
234                ids = [ ]
235
236                for node in hostnames:
237
238                        id = self.getNodeId( node )
239       
240                        if not id:
241                                self.setDatabase( "INSERT INTO nodes ( node_hostname ) VALUES ( '%s' )" %node )
242                                id = self.getNodeId( node )
243
244                        ids.append( id )
245
246                return ids
247
248        def addJobNodes( self, jobid, nodes ):
249
250                for node in nodes:
251                        self.addJobNode( jobid, node )
252
253        def addJobNode( self, jobid, nodeid ):
254
255                self.setDatabase( "INSERT INTO job_nodes (job_id,node_id) VALUES ( %s,%s )" %(jobid, nodeid) )
256
257        def storeJobInfo( self, jobid, jobattrs ):
258
259                self.addJob( jobid, jobattrs )
260
261class RRDMutator:
262        """A class for performing RRD mutations"""
263
264        binary = '/usr/bin/rrdtool'
265
266        def __init__( self, binary=None ):
267                """Set alternate binary if supplied"""
268
269                if binary:
270                        self.binary = binary
271
272        def create( self, filename, args ):
273                """Create a new rrd with args"""
274
275                return self.perform( 'create', '"' + filename + '"', args )
276
277        def update( self, filename, args ):
278                """Update a rrd with args"""
279
280                return self.perform( 'update', '"' + filename + '"', args )
281
282        def grabLastUpdate( self, filename ):
283                """Determine the last update time of filename rrd"""
284
285                last_update = 0
286
287                debug_msg( 8, self.binary + ' info "' + filename + '"' )
288
289                for line in os.popen( self.binary + ' info "' + filename + '"' ).readlines():
290
291                        if line.find( 'last_update') != -1:
292
293                                last_update = line.split( ' = ' )[1]
294
295                if last_update:
296                        return last_update
297                else:
298                        return 0
299
300        def perform( self, action, filename, args ):
301                """Perform action on rrd filename with args"""
302
303                arg_string = None
304
305                if type( args ) is not ListType:
306                        debug_msg( 8, 'Arguments needs to be of type List' )
307                        return 1
308
309                for arg in args:
310
311                        if not arg_string:
312
313                                arg_string = arg
314                        else:
315                                arg_string = arg_string + ' ' + arg
316
317                debug_msg( 8, self.binary + ' ' + action + ' ' + filename + ' ' + arg_string  )
318
319                cmd = os.popen( self.binary + ' ' + action + ' ' + filename + ' ' + arg_string )
320                lines = cmd.readlines()
321                cmd.close()
322
323                for line in lines:
324
325                        if line.find( 'ERROR' ) != -1:
326
327                                error_msg = string.join( line.split( ' ' )[1:] )
328                                debug_msg( 8, error_msg )
329                                return 1
330
331                return 0
332
333class XMLProcessor:
334        """Skeleton class for XML processor's"""
335
336        def daemon( self ):
337                """Run as daemon forever"""
338
339                # Fork the first child
340                #
341                pid = os.fork()
342
343                if pid > 0:
344
345                        sys.exit(0)  # end parent
346
347                # creates a session and sets the process group ID
348                #
349                os.setsid()
350
351                # Fork the second child
352                #
353                pid = os.fork()
354
355                if pid > 0:
356
357                        sys.exit(0)  # end parent
358
359                # Go to the root directory and set the umask
360                #
361                os.chdir('/')
362                os.umask(0)
363
364                sys.stdin.close()
365                sys.stdout.close()
366                sys.stderr.close()
367
368                os.open('/dev/null', 0)
369                os.dup(0)
370                os.dup(0)
371
372                self.run()
373
374        def run( self ):
375                """Do main processing of XML here"""
376
377                pass
378
379class TorqueXMLProcessor( XMLProcessor ):
380        """Main class for processing XML and acting with it"""
381
382        def __init__( self ):
383                """Setup initial XML connection and handlers"""
384
385                self.myXMLGatherer = XMLGatherer( ARCHIVE_XMLSOURCE.split( ':' )[0], ARCHIVE_XMLSOURCE.split( ':' )[1] ) 
386                self.myXMLSource = self.myXMLGatherer.getFileObject()
387                self.myXMLHandler = TorqueXMLHandler()
388                self.myXMLError = XMLErrorHandler()
389                self.config = GangliaConfigParser( GMETAD_CONF )
390
391        def run( self ):
392                """Main XML processing"""
393
394                debug_msg( 1, printTime() + ' - torque_xml_thread(): started.' )
395
396                while( 1 ):
397
398                        self.myXMLSource = self.myXMLGatherer.getFileObject()
399                        debug_msg( 1, printTime() + ' - torque_xml_thread(): Parsing..' )
400                        xml.sax.parse( self.myXMLSource, self.myXMLHandler, self.myXMLError )
401                        debug_msg( 1, printTime() + ' - torque_xml_thread(): Done parsing.' )
402                        debug_msg( 1, printTime() + ' - torque_xml_thread(): Sleeping.. (%ss)' %(str( self.config.getLowestInterval() ) ) )
403                        time.sleep( self.config.getLowestInterval() )
404
405class TorqueXMLHandler( xml.sax.handler.ContentHandler ):
406        """Parse Torque's jobinfo XML from our plugin"""
407
408        jobAttrs = { }
409
410        def __init__( self ):
411
412                self.ds = DataSQLStore( TOGA_SQL_DBASE.split( '/' )[0], TOGA_SQL_DBASE.split( '/' )[1] )
413                self.jobs_processed = [ ]
414                self.jobs_to_store = [ ]
415
416        def startElement( self, name, attrs ):
417                """
418                This XML will be all gmetric XML
419                so there will be no specific start/end element
420                just one XML statement with all info
421                """
422               
423                heartbeat = 0
424               
425                jobinfo = { }
426
427                if name == 'METRIC':
428
429                        metricname = attrs.get( 'NAME', "" )
430
431                        if metricname == 'TOGA-HEARTBEAT':
432                                self.heartbeat = attrs.get( 'VAL', "" )
433
434                        elif metricname.find( 'TOGA-JOB' ) != -1:
435
436                                job_id = metricname.split( 'TOGA-JOB-' )[1]
437                                val = attrs.get( 'VAL', "" )
438
439                                if not job_id in self.jobs_processed:
440                                        self.jobs_processed.append( job_id )
441
442                                check_change = 0
443
444                                if self.jobAttrs.has_key( job_id ):
445                                        check_change = 1
446
447                                valinfo = val.split( ' ' )
448
449                                for myval in valinfo:
450
451                                        if len( myval.split( '=' ) ) > 1:
452
453                                                valname = myval.split( '=' )[0]
454                                                value = myval.split( '=' )[1]
455
456                                                if valname == 'nodes':
457                                                        value = value.split( ';' )
458
459                                                jobinfo[ valname ] = value
460
461                                if check_change:
462                                        if self.jobinfoChanged( self.jobAttrs, job_id, jobinfo ) and self.jobAttrs[ job_id ]['status'] != 'E':
463                                                self.jobAttrs[ job_id ]['stop_timestamp'] = ''
464                                                self.jobAttrs[ job_id ] = self.setJobAttrs( self.jobAttrs[ job_id ], jobinfo )
465                                                if not job_id in self.jobs_to_store:
466                                                        self.jobs_to_store.append( job_id )
467
468                                                debug_msg( 6, 'jobinfo for job %s has changed' %job_id )
469                                else:
470                                        self.jobAttrs[ job_id ] = jobinfo
471
472                                        if not job_id in self.jobs_to_store:
473                                                self.jobs_to_store.append( job_id )
474
475                                        debug_msg( 6, 'jobinfo for job %s has changed' %job_id )
476                                       
477        def endDocument( self ):
478                """When all metrics have gone, check if any jobs have finished"""
479
480                for jobid, jobinfo in self.jobAttrs.items():
481
482                        # This is an old job, not in current jobinfo list anymore
483                        # it must have finished, since we _did_ get a new heartbeat
484                        #
485                        mytime = int( jobinfo['reported'] ) + int( jobinfo['poll_interval'] )
486
487                        if mytime < self.heartbeat and jobid not in self.jobs_processed and jobinfo['status'] == 'R':
488
489                                if not jobid in self.jobs_processed:
490                                        self.jobs_processed.append( jobid )
491
492                                self.jobAttrs[ jobid ]['status'] = 'F'
493                                self.jobAttrs[ jobid ]['stop_timestamp'] = str( int( jobinfo['reported'] ) + int( jobinfo['poll_interval'] ) )
494
495                                if not jobid in self.jobs_to_store:
496                                        self.jobs_to_store.append( jobid )
497
498                debug_msg( 1, printTime() + ' - torque_xml_thread(): Storing..' )
499
500                for jobid in self.jobs_to_store:
501                        self.ds.storeJobInfo( jobid, self.jobAttrs[ jobid ] )   
502
503                debug_msg( 1, printTime() + ' - torque_xml_thread(): Done storing.' )
504
505                self.jobs_processed = [ ]
506                self.jobs_to_store = [ ]
507
508        def setJobAttrs( self, old, new ):
509                """
510                Set new job attributes in old, but not lose existing fields
511                if old attributes doesn't have those
512                """
513
514                for valname, value in new.items():
515                        old[ valname ] = value
516
517                return old
518               
519
520        def jobinfoChanged( self, jobattrs, jobid, jobinfo ):
521                """
522                Check if jobinfo has changed from jobattrs[jobid]
523                if it's report time is bigger than previous one
524                and it is report time is recent (equal to heartbeat)
525                """
526
527                ignore_changes = [ 'reported' ]
528
529                if jobattrs.has_key( jobid ):
530
531                        for valname, value in jobinfo.items():
532
533                                if valname not in ignore_changes:
534
535                                        if jobattrs[ jobid ].has_key( valname ):
536
537                                                if value != jobattrs[ jobid ][ valname ]:
538
539                                                        if jobinfo['reported'] > jobattrs[ jobid ][ 'reported' ] and jobinfo['reported'] == self.heartbeat:
540                                                                return 1
541
542                                        else:
543                                                return 1
544
545                return 0
546
547class GangliaXMLHandler( xml.sax.handler.ContentHandler ):
548        """Parse Ganglia's XML"""
549
550        def __init__( self, config ):
551                """Setup initial variables and gather info on existing rrd archive"""
552
553                self.config = config
554                self.clusters = { }
555                debug_msg( 0, printTime() + ' - Checking existing toga rrd archive..' )
556                self.gatherClusters()
557                debug_msg( 0, printTime() + ' - Check done.' )
558
559        def gatherClusters( self ):
560                """Find all existing clusters in archive dir"""
561
562                archive_dir = check_dir(ARCHIVE_PATH)
563
564                hosts = [ ]
565
566                if os.path.exists( archive_dir ):
567
568                        dirlist = os.listdir( archive_dir )
569
570                        for item in dirlist:
571
572                                clustername = item
573
574                                if not self.clusters.has_key( clustername ) and clustername in ARCHIVE_DATASOURCES:
575
576                                        self.clusters[ clustername ] = RRDHandler( self.config, clustername )
577
578        def startElement( self, name, attrs ):
579                """Memorize appropriate data from xml start tags"""
580
581                if name == 'GANGLIA_XML':
582
583                        self.XMLSource = attrs.get( 'SOURCE', "" )
584                        self.gangliaVersion = attrs.get( 'VERSION', "" )
585
586                        debug_msg( 10, 'Found XML data: source %s version %s' %( self.XMLSource, self.gangliaVersion ) )
587
588                elif name == 'GRID':
589
590                        self.gridName = attrs.get( 'NAME', "" )
591                        self.time = attrs.get( 'LOCALTIME', "" )
592
593                        debug_msg( 10, '`-Grid found: %s' %( self.gridName ) )
594
595                elif name == 'CLUSTER':
596
597                        self.clusterName = attrs.get( 'NAME', "" )
598                        self.time = attrs.get( 'LOCALTIME', "" )
599
600                        if not self.clusters.has_key( self.clusterName ) and self.clusterName in ARCHIVE_DATASOURCES:
601
602                                self.clusters[ self.clusterName ] = RRDHandler( self.config, self.clusterName )
603
604                                debug_msg( 10, ' |-Cluster found: %s' %( self.clusterName ) )
605
606                elif name == 'HOST' and self.clusterName in ARCHIVE_DATASOURCES:     
607
608                        self.hostName = attrs.get( 'NAME', "" )
609                        self.hostIp = attrs.get( 'IP', "" )
610                        self.hostReported = attrs.get( 'REPORTED', "" )
611
612                        debug_msg( 10, ' | |-Host found: %s - ip %s reported %s' %( self.hostName, self.hostIp, self.hostReported ) )
613
614                elif name == 'METRIC' and self.clusterName in ARCHIVE_DATASOURCES:
615
616                        type = attrs.get( 'TYPE', "" )
617
618                        if type not in UNSUPPORTED_ARCHIVE_TYPES:
619
620                                myMetric = { }
621                                myMetric['name'] = attrs.get( 'NAME', "" )
622                                myMetric['val'] = attrs.get( 'VAL', "" )
623                                myMetric['time'] = self.hostReported
624
625                                self.clusters[ self.clusterName ].memMetric( self.hostName, myMetric )
626
627                                debug_msg( 11, ' | | |-metric: %s:%s' %( myMetric['name'], myMetric['val'] ) )
628
629        def storeMetrics( self ):
630                """Store metrics of each cluster rrd handler"""
631
632                for clustername, rrdh in self.clusters.items():
633
634                        ret = rrdh.storeMetrics()
635
636                        if ret:
637                                debug_msg( 9, 'An error occured while storing metrics for cluster %s' %clustername )
638                                return 1
639
640                return 0
641
642class XMLErrorHandler( xml.sax.handler.ErrorHandler ):
643
644        def error( self, exception ):
645                """Recoverable error"""
646
647                debug_msg( 0, 'Recoverable error ' + str( exception ) )
648
649        def fatalError( self, exception ):
650                """Non-recoverable error"""
651
652                exception_str = str( exception )
653
654                # Ignore 'no element found' errors
655                if exception_str.find( 'no element found' ) != -1:
656                        debug_msg( 1, 'No XML data found: probably socket not (re)connected.' )
657                        return 0
658
659                debug_msg( 0, 'Non-recoverable error ' + str( exception ) )
660                sys.exit( 1 )
661
662        def warning( self, exception ):
663                """Warning"""
664
665                debug_msg( 0, 'Warning ' + str( exception ) )
666
667class XMLGatherer:
668        """Setup a connection and file object to Ganglia's XML"""
669
670        s = None
671        fd = None
672
673        def __init__( self, host, port ):
674                """Store host and port for connection"""
675
676                self.host = host
677                self.port = port
678                self.connect()
679                self.makeFileDescriptor()
680
681        def connect( self ):
682                """Setup connection to XML source"""
683
684                for res in socket.getaddrinfo( self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM ):
685
686                        af, socktype, proto, canonname, sa = res
687
688                        try:
689
690                                self.s = socket.socket( af, socktype, proto )
691
692                        except socket.error, msg:
693
694                                self.s = None
695                                continue
696
697                        try:
698
699                                self.s.connect( sa )
700
701                        except socket.error, msg:
702
703                                self.disconnect()
704                                continue
705
706                        break
707
708                if self.s is None:
709
710                        debug_msg( 0, 'Could not open socket' )
711                        sys.exit( 1 )
712
713        def disconnect( self ):
714                """Close socket"""
715
716                if self.s:
717                        self.s.shutdown( 2 )
718                        self.s.close()
719                        self.s = None
720
721        def __del__( self ):
722                """Kill the socket before we leave"""
723
724                self.disconnect()
725
726        def reconnect( self ):
727                """Reconnect"""
728
729                if self.s:
730                        self.disconnect()
731
732                self.connect()
733
734        def makeFileDescriptor( self ):
735                """Make file descriptor that points to our socket connection"""
736
737                self.reconnect()
738
739                if self.s:
740                        self.fd = self.s.makefile( 'r' )
741
742        def getFileObject( self ):
743                """Connect, and return a file object"""
744
745                self.makeFileDescriptor()
746
747                if self.fd:
748                        return self.fd
749
750class GangliaXMLProcessor( XMLProcessor ):
751        """Main class for processing XML and acting with it"""
752
753        def __init__( self ):
754                """Setup initial XML connection and handlers"""
755
756                self.config = GangliaConfigParser( GMETAD_CONF )
757
758                self.myXMLGatherer = XMLGatherer( ARCHIVE_XMLSOURCE.split( ':' )[0], ARCHIVE_XMLSOURCE.split( ':' )[1] ) 
759                self.myXMLSource = self.myXMLGatherer.getFileObject()
760                self.myXMLHandler = GangliaXMLHandler( self.config )
761                self.myXMLError = XMLErrorHandler()
762
763        def run( self ):
764                """Main XML processing; start a xml and storethread"""
765
766                xml_thread = threading.Thread( None, self.processXML, 'xmlthread' )
767                store_thread = threading.Thread( None, self.storeMetrics, 'storethread' )
768
769                while( 1 ):
770
771                        if not xml_thread.isAlive():
772                                # Gather XML at the same interval as gmetad
773
774                                # threaded call to: self.processXML()
775                                #
776                                xml_thread = threading.Thread( None, self.processXML, 'xml_thread' )
777                                xml_thread.start()
778
779                        if not store_thread.isAlive():
780                                # Store metrics every .. sec
781
782                                # threaded call to: self.storeMetrics()
783                                #
784                                store_thread = threading.Thread( None, self.storeMetrics, 'store_thread' )
785                                store_thread.start()
786               
787                        # Just sleep a sec here, to prevent daemon from going mad. We're all threads here anyway
788                        time.sleep( 1 ) 
789
790        def storeMetrics( self ):
791                """Store metrics retained in memory to disk"""
792
793                debug_msg( 1, printTime() + ' - ganglia_store_thread(): started.' )
794
795                # Store metrics somewhere between every 360 and 640 seconds
796                #
797                STORE_INTERVAL = random.randint( 360, 640 )
798
799                store_metric_thread = threading.Thread( None, self.storeThread, 'store_metric_thread' )
800                store_metric_thread.start()
801
802                debug_msg( 1, printTime() + ' - ganglia_store_thread(): Sleeping.. (%ss)' %STORE_INTERVAL )
803                time.sleep( STORE_INTERVAL )
804                debug_msg( 1, printTime() + ' - ganglia_store_thread(): Done sleeping.' )
805
806                if store_metric_thread.isAlive():
807
808                        debug_msg( 1, printTime() + ' - ganglia_store_thread(): storemetricthread() still running, waiting to finish..' )
809                        store_metric_thread.join( STORE_TIMEOUT ) # Maximum time is for storing thread to finish
810                        debug_msg( 1, printTime() + ' - ganglia_store_thread(): Done waiting.' )
811
812                debug_msg( 1, printTime() + ' - ganglia_store_thread(): finished.' )
813
814                return 0
815
816        def storeThread( self ):
817                """Actual metric storing thread"""
818
819                debug_msg( 1, printTime() + ' - ganglia_store_metric_thread(): started.' )
820                debug_msg( 1, printTime() + ' - ganglia_store_metric_thread(): Storing data..' )
821                ret = self.myXMLHandler.storeMetrics()
822                debug_msg( 1, printTime() + ' - ganglia_store_metric_thread(): Done storing.' )
823                debug_msg( 1, printTime() + ' - ganglia_store_metric_thread(): finished.' )
824               
825                return ret
826
827        def processXML( self ):
828                """Process XML"""
829
830                debug_msg( 1, printTime() + ' - xmlthread(): started.' )
831
832                parsethread = threading.Thread( None, self.parseThread, 'parsethread' )
833                parsethread.start()
834
835                debug_msg( 1, printTime() + ' - ganglia_xml_thread(): Sleeping.. (%ss)' %self.config.getLowestInterval() )
836                time.sleep( float( self.config.getLowestInterval() ) ) 
837                debug_msg( 1, printTime() + ' - ganglia_xml_thread(): Done sleeping.' )
838
839                if parsethread.isAlive():
840
841                        debug_msg( 1, printTime() + ' - ganglia_xml_thread(): parsethread() still running, waiting to finish..' )
842                        parsethread.join( PARSE_TIMEOUT ) # Maximum time for XML thread to finish
843                        debug_msg( 1, printTime() + ' - ganglia_xml_thread(): Done waiting.' )
844
845                debug_msg( 1, printTime() + ' - ganglia_xml_thread(): finished.' )
846
847                return 0
848
849        def parseThread( self ):
850                """Actual parsing thread"""
851
852                debug_msg( 1, printTime() + ' - ganglia_parse_thread(): started.' )
853                debug_msg( 1, printTime() + ' - ganglia_parse_thread(): Parsing XML..' )
854                self.myXMLSource = self.myXMLGatherer.getFileObject()
855                ret = xml.sax.parse( self.myXMLSource, self.myXMLHandler, self.myXMLError )
856                debug_msg( 1, printTime() + ' - ganglia_parse_thread(): Done parsing.' )
857                debug_msg( 1, printTime() + ' - ganglia_parse_thread(): finished.' )
858
859                return ret
860
861class GangliaConfigParser:
862
863        sources = [ ]
864
865        def __init__( self, config ):
866                """Parse some stuff from our gmetad's config, such as polling interval"""
867
868                self.config = config
869                self.parseValues()
870
871        def parseValues( self ):
872                """Parse certain values from gmetad.conf"""
873
874                readcfg = open( self.config, 'r' )
875
876                for line in readcfg.readlines():
877
878                        if line.count( '"' ) > 1:
879
880                                if line.find( 'data_source' ) != -1 and line[0] != '#':
881
882                                        source = { }
883                                        source['name'] = line.split( '"' )[1]
884                                        source_words = line.split( '"' )[2].split( ' ' )
885
886                                        for word in source_words:
887
888                                                valid_interval = 1
889
890                                                for letter in word:
891
892                                                        if letter not in string.digits:
893
894                                                                valid_interval = 0
895
896                                                if valid_interval and len(word) > 0:
897
898                                                        source['interval'] = word
899                                                        debug_msg( 9, 'polling interval for %s = %s' %(source['name'], source['interval'] ) )
900       
901                                        # No interval found, use Ganglia's default     
902                                        if not source.has_key( 'interval' ):
903                                                source['interval'] = 15
904                                                debug_msg( 9, 'polling interval for %s defaulted to 15' %(source['name']) )
905
906                                        self.sources.append( source )
907
908        def getInterval( self, source_name ):
909                """Return interval for source_name"""
910
911                for source in self.sources:
912
913                        if source['name'] == source_name:
914
915                                return source['interval']
916
917                return None
918
919        def getLowestInterval( self ):
920                """Return the lowest interval of all clusters"""
921
922                lowest_interval = 0
923
924                for source in self.sources:
925
926                        if not lowest_interval or source['interval'] <= lowest_interval:
927
928                                lowest_interval = source['interval']
929
930                # Return 15 when nothing is found, so that the daemon won't go insane with 0 sec delays
931                if lowest_interval:
932                        return lowest_interval
933                else:
934                        return 15
935
936class RRDHandler:
937        """Class for handling RRD activity"""
938
939        myMetrics = { }
940        lastStored = { }
941        timeserials = { }
942        slot = None
943
944        def __init__( self, config, cluster ):
945                """Setup initial variables"""
946
947                self.block = 0
948                self.cluster = cluster
949                self.config = config
950                self.slot = threading.Lock()
951                self.rrdm = RRDMutator()
952                self.gatherLastUpdates()
953
954        def gatherLastUpdates( self ):
955                """Populate the lastStored list, containing timestamps of all last updates"""
956
957                cluster_dir = '%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster )
958
959                hosts = [ ]
960
961                if os.path.exists( cluster_dir ):
962
963                        dirlist = os.listdir( cluster_dir )
964
965                        for dir in dirlist:
966
967                                hosts.append( dir )
968
969                for host in hosts:
970
971                        host_dir = cluster_dir + '/' + host
972                        dirlist = os.listdir( host_dir )
973
974                        for dir in dirlist:
975
976                                if not self.timeserials.has_key( host ):
977
978                                        self.timeserials[ host ] = [ ]
979
980                                self.timeserials[ host ].append( dir )
981
982                        last_serial = self.getLastRrdTimeSerial( host )
983                        if last_serial:
984
985                                metric_dir = cluster_dir + '/' + host + '/' + last_serial
986                                if os.path.exists( metric_dir ):
987
988                                        dirlist = os.listdir( metric_dir )
989
990                                        for file in dirlist:
991
992                                                metricname = file.split( '.rrd' )[0]
993
994                                                if not self.lastStored.has_key( host ):
995
996                                                        self.lastStored[ host ] = { }
997
998                                                self.lastStored[ host ][ metricname ] = self.rrdm.grabLastUpdate( metric_dir + '/' + file )
999
1000        def getClusterName( self ):
1001                """Return clustername"""
1002
1003                return self.cluster
1004
1005        def memMetric( self, host, metric ):
1006                """Store metric from host in memory"""
1007
1008                if self.myMetrics.has_key( host ):
1009
1010                        if self.myMetrics[ host ].has_key( metric['name'] ):
1011
1012                                for mymetric in self.myMetrics[ host ][ metric['name'] ]:
1013
1014                                        if mymetric['time'] == metric['time']:
1015
1016                                                # Allready have this metric, abort
1017                                                return 1
1018                        else:
1019                                self.myMetrics[ host ][ metric['name'] ] = [ ]
1020                else:
1021                        self.myMetrics[ host ] = { }
1022                        self.myMetrics[ host ][ metric['name'] ] = [ ]
1023
1024                # Push new metric onto stack
1025                # atomic code; only 1 thread at a time may access the stack
1026
1027                # <ATOMIC>
1028                #
1029                self.slot.acquire()
1030
1031                self.myMetrics[ host ][ metric['name'] ].append( metric )
1032
1033                self.slot.release()
1034                #
1035                # </ATOMIC>
1036
1037        def makeUpdateList( self, host, metriclist ):
1038                """
1039                Make a list of update values for rrdupdate
1040                but only those that we didn't store before
1041                """
1042
1043                update_list = [ ]
1044                metric = None
1045
1046                while len( metriclist ) > 0:
1047
1048                        metric = metriclist.pop( 0 )
1049
1050                        if self.checkStoreMetric( host, metric ):
1051                                update_list.append( '%s:%s' %( metric['time'], metric['val'] ) )
1052
1053                return update_list
1054
1055        def checkStoreMetric( self, host, metric ):
1056                """Check if supplied metric if newer than last one stored"""
1057
1058                if self.lastStored.has_key( host ):
1059
1060                        if self.lastStored[ host ].has_key( metric['name'] ):
1061
1062                                if metric['time'] <= self.lastStored[ host ][ metric['name'] ]:
1063
1064                                        # This is old
1065                                        return 0
1066
1067                return 1
1068
1069        def memLastUpdate( self, host, metricname, metriclist ):
1070                """
1071                Memorize the time of the latest metric from metriclist
1072                but only if it wasn't allready memorized
1073                """
1074
1075                if not self.lastStored.has_key( host ):
1076                        self.lastStored[ host ] = { }
1077
1078                last_update_time = 0
1079
1080                for metric in metriclist:
1081
1082                        if metric['name'] == metricname:
1083
1084                                if metric['time'] > last_update_time:
1085
1086                                        last_update_time = metric['time']
1087
1088                if self.lastStored[ host ].has_key( metricname ):
1089                       
1090                        if last_update_time <= self.lastStored[ host ][ metricname ]:
1091                                return 1
1092
1093                self.lastStored[ host ][ metricname ] = last_update_time
1094
1095        def storeMetrics( self ):
1096                """
1097                Store all metrics from memory to disk
1098                and do it to the RRD's in appropriate timeperiod directory
1099                """
1100
1101                for hostname, mymetrics in self.myMetrics.items():     
1102
1103                        for metricname, mymetric in mymetrics.items():
1104
1105                                metrics_to_store = [ ]
1106
1107                                # Pop metrics from stack for storing until none is left
1108                                # atomic code: only 1 thread at a time may access myMetrics
1109
1110                                # <ATOMIC>
1111                                #
1112                                self.slot.acquire() 
1113
1114                                while len( self.myMetrics[ hostname ][ metricname ] ) > 0:
1115
1116                                        if len( self.myMetrics[ hostname ][ metricname ] ) > 0:
1117                                                metrics_to_store.append( self.myMetrics[ hostname ][ metricname ].pop( 0 ) )
1118
1119                                self.slot.release()
1120                                #
1121                                # </ATOMIC>
1122
1123                                # Create a mapping table, each metric to the period where it should be stored
1124                                #
1125                                metric_serial_table = self.determineSerials( hostname, metricname, metrics_to_store )
1126
1127                                update_rets = [ ]
1128
1129                                for period, pmetric in metric_serial_table.items():
1130
1131                                        create_ret = self.createCheck( hostname, metricname, period )   
1132
1133                                        update_ret = self.update( hostname, metricname, period, pmetric )
1134
1135                                        if update_ret == 0:
1136
1137                                                debug_msg( 9, 'stored metric %s for %s' %( hostname, metricname ) )
1138                                        else:
1139                                                debug_msg( 9, 'metric update failed' )
1140
1141                                        update_rets.append( create_ret )
1142                                        update_rets.append( update_ret )
1143
1144                                if not (1) in update_rets:
1145
1146                                        self.memLastUpdate( hostname, metricname, metrics_to_store )
1147
1148        def makeTimeSerial( self ):
1149                """Generate a time serial. Seconds since epoch"""
1150
1151                # Seconds since epoch
1152                mytime = int( time.time() )
1153
1154                return mytime
1155
1156        def makeRrdPath( self, host, metricname, timeserial ):
1157                """Make a RRD location/path and filename"""
1158
1159                rrd_dir = '%s/%s/%s/%s' %( check_dir(ARCHIVE_PATH), self.cluster, host, timeserial )
1160                rrd_file = '%s/%s.rrd' %( rrd_dir, metricname )
1161
1162                return rrd_dir, rrd_file
1163
1164        def getLastRrdTimeSerial( self, host ):
1165                """Find the last timeserial (directory) for this host"""
1166
1167                newest_timeserial = 0
1168
1169                for dir in self.timeserials[ host ]:
1170
1171                        valid_dir = 1
1172
1173                        for letter in dir:
1174                                if letter not in string.digits:
1175                                        valid_dir = 0
1176
1177                        if valid_dir:
1178                                timeserial = dir
1179                                if timeserial > newest_timeserial:
1180                                        newest_timeserial = timeserial
1181
1182                if newest_timeserial:
1183                        return newest_timeserial
1184                else:
1185                        return 0
1186
1187        def determinePeriod( self, host, check_serial ):
1188                """Determine to which period (directory) this time(serial) belongs"""
1189
1190                period_serial = 0
1191
1192                if self.timeserials.has_key( host ):
1193
1194                        for serial in self.timeserials[ host ]:
1195
1196                                if check_serial >= serial and period_serial < serial:
1197
1198                                        period_serial = serial
1199
1200                return period_serial
1201
1202        def determineSerials( self, host, metricname, metriclist ):
1203                """
1204                Determine the correct serial and corresponding rrd to store
1205                for a list of metrics
1206                """
1207
1208                metric_serial_table = { }
1209
1210                for metric in metriclist:
1211
1212                        if metric['name'] == metricname:
1213
1214                                period = self.determinePeriod( host, metric['time'] )   
1215
1216                                archive_secs = ARCHIVE_HOURS_PER_RRD * (60 * 60)
1217
1218                                if (int( metric['time'] ) - int( period ) ) > archive_secs:
1219
1220                                        # This one should get it's own new period
1221                                        period = metric['time']
1222
1223                                        if not self.timeserials.has_key( host ):
1224                                                self.timeserials[ host ] = [ ]
1225
1226                                        self.timeserials[ host ].append( period )
1227
1228                                if not metric_serial_table.has_key( period ):
1229
1230                                        metric_serial_table[ period ] = [ ]
1231
1232                                metric_serial_table[ period ].append( metric )
1233
1234                return metric_serial_table
1235
1236        def createCheck( self, host, metricname, timeserial ):
1237                """Check if an rrd allready exists for this metric, create if not"""
1238
1239                debug_msg( 9, 'rrdcreate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) )
1240               
1241                rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial )
1242
1243                if not os.path.exists( rrd_dir ):
1244
1245                        try:
1246                                os.makedirs( rrd_dir )
1247
1248                        except OSError, msg:
1249
1250                                if msg.find( 'File exists' ) != -1:
1251
1252                                        # Ignore exists errors
1253                                        pass
1254
1255                                else:
1256
1257                                        print msg
1258                                        return
1259
1260                        debug_msg( 9, 'created dir %s' %( str(rrd_dir) ) )
1261
1262                if not os.path.exists( rrd_file ):
1263
1264                        interval = self.config.getInterval( self.cluster )
1265                        heartbeat = 8 * int( interval )
1266
1267                        params = [ ]
1268
1269                        params.append( '--step' )
1270                        params.append( str( interval ) )
1271
1272                        params.append( '--start' )
1273                        params.append( str( int( timeserial ) - 1 ) )
1274
1275                        params.append( 'DS:sum:GAUGE:%d:U:U' %heartbeat )
1276                        params.append( 'RRA:AVERAGE:0.5:1:%s' %(ARCHIVE_HOURS_PER_RRD * 240) )
1277
1278                        self.rrdm.create( str(rrd_file), params )
1279
1280                        debug_msg( 9, 'created rrd %s' %( str(rrd_file) ) )
1281
1282        def update( self, host, metricname, timeserial, metriclist ):
1283                """
1284                Update rrd file for host with metricname
1285                in directory timeserial with metriclist
1286                """
1287
1288                debug_msg( 9, 'rrdupdate: using timeserial %s for %s/%s' %( timeserial, host, metricname ) )
1289
1290                rrd_dir, rrd_file = self.makeRrdPath( host, metricname, timeserial )
1291
1292                update_list = self.makeUpdateList( host, metriclist )
1293
1294                if len( update_list ) > 0:
1295                        ret = self.rrdm.update( str(rrd_file), update_list )
1296
1297                        if ret:
1298                                return 1
1299               
1300                        debug_msg( 9, 'updated rrd %s with %s' %( str(rrd_file), string.join( update_list ) ) )
1301
1302                return 0
1303
1304def main():
1305        """Program startup"""
1306
1307        myTorqueProcessor = TorqueXMLProcessor()
1308        myGangliaProcessor = GangliaXMLProcessor()
1309
1310        if DAEMONIZE:
1311                torque_xml_thread = threading.Thread( None, myTorqueProcessor.daemon, 'torque_proc_thread' )
1312                ganglia_xml_thread = threading.Thread( None, myGangliaProcessor.daemon, 'ganglia_proc_thread' )
1313        else:
1314                torque_xml_thread = threading.Thread( None, myTorqueProcessor.run, 'torque_proc_thread' )
1315                ganglia_xml_thread = threading.Thread( None, myGangliaProcessor.run, 'ganglia_proc_thread' )
1316
1317        torque_xml_thread.start()
1318        ganglia_xml_thread.start()
1319
1320# Global functions
1321
1322def check_dir( directory ):
1323        """Check if directory is a proper directory. I.e.: Does _not_ end with a '/'"""
1324
1325        if directory[-1] == '/':
1326                directory = directory[:-1]
1327
1328        return directory
1329
1330def debug_msg( level, msg ):
1331        """Only print msg if it is not below our debug level"""
1332
1333        if (DEBUG_LEVEL >= level):
1334                sys.stderr.write( msg + '\n' )
1335
1336def printTime( ):
1337        """Print current time in human readable format"""
1338
1339        return time.strftime("%a %d %b %Y %H:%M:%S")
1340
1341# Ooohh, someone started me! Let's go..
1342if __name__ == '__main__':
1343        main()
Note: See TracBrowser for help on using the repository browser.