def process_IN_MODIFY(self, event): ''' Handles a file modification event, typically when one or more event records have been appended to the accounting file. The new records are sent to the accounting database here. ''' if event.name == os.path.basename(self.acctfile): if self.dryrun: self.logger.info("Would normally send records") else: if self.recs == None: # Segfaults if file doesn't exist -- No exception thrown if os.path.isfile(self.acctfile): self.recs = pylsf.lsb_geteventrec(self.acctfile) else: # Report to log fmt = "Couldn't open acct file: %s" strerr = "No such file or directory" self.logger.error(fmt % strerr) return insertc, errorc, heartbeat = common.insert(self.logger, common.LOCALTAB, self.recs, self.connection, self.insertc, self.errorc, self.heartbeat, self.heartbeatdelta) self.insertc = insertc self.errorc = errorc self.heartbeat = heartbeat
def process_IN_CREATE(self, event): ''' Handles a file creation event, which happens on the second and last stage of a logrotation. A new lsb_geteventrec instance is created (i.e. the new accounting file with the original accounting name is opened again). ''' # Don't wm.close() here, as it would close all watches. Don't # wm.del_watch() either because I don't think you'd be able to add # the watch again without restarting the notifier loop (and I don't # know if it's possible to restart the notifier loop either). And # don't wm.rm_watch() because it makes pyinotify complain. In fact, # leave the watches alone and just make sure you don't have any watch # looking at files directly, as opposed to looking at directories. if event.name == os.path.basename(self.acctfile): # Shouldn't be necessary but read any possibly unhandled event # records from the old file, in case new entries have been # appended to it, it's been renamed and the new file has been # created atomically. # It's possible to do it this way because seems to be targeting # inodes, not file names. if self.dryrun: self.logger.info("Would normally send records") elif self.recs != None: insertc, errorc, heartbeat = common.insert(self.logger, common.LOCALTAB, self.recs, self.connection, self.insertc, self.errorc, self.heartbeat, self.heartbeatdelta) self.insertc = insertc self.errorc = errorc self.heartbeat = heartbeat self.logger.info('Created %s' % event.name) if os.path.isfile(self.acctfile): # Previous file implicitly closed here when the lsb_geteventrec # is deallocated self.recs = pylsf.lsb_geteventrec(self.acctfile) else: # Report to log strerr = "No such file or directory" self.logger.error("Couldn't open acct file: %s" % strerr)
import pylsf a = pylsf.lsb_init("pylsf-lsb.acct") b = pylsf.lsb_geteventrec("/usr/share/lsf/work/makalu/logdir/lsb.events") while 1: c = b.read() if not c: break else: print c