source: trunk/jobarchived/jobarchived.py @ 296

Last change on this file since 296 was 296, checked in by bastiaans, 17 years ago

jobarchived/jobarchived.py:

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