source: trunk/daemon/togad.py @ 190

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

daemon/togad.py:

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