def do_rotate(self): if not self._savefile: return self._savefile.close() now = time.time() from_time = self.format_time(self._starting_timestamp) to_time = self.format_time(now) new_name = "from-%s---to-%s.flog" % (from_time, to_time) new_name = os.path.join(self.basedir, new_name) move_into_place(self._savefile_name, new_name) self._open_savefile(now) if self.bzip: # we spawn an external bzip process because it's easier than # using the stdlib bz2 module and spreading the work out over # several ticks. We're trying to resume accepting log events # quickly here. We don't save the events using BZ2File because # the gatherer might be killed at any moment, and BZ2File doesn't # flush its output until the file is closed. d = utils.getProcessOutput(self.bzip, [new_name], env=os.environ) new_name = new_name + ".bz2" def _compression_error(f): print f d.addErrback(_compression_error) # note that by returning this Deferred, the rotation timer won't # start again until the bzip process finishes else: d = defer.succeed(None) d.addCallback(lambda res: new_name) return d # for tests
def save_service_data(basedir, data): assert data["version"] == 1 services_file = os.path.join(basedir, "services.json") tmpfile = services_file + ".tmp" with open(tmpfile, "w") as f: json.dump(data, f, indent=2) move_into_place(tmpfile, services_file)
def run(self, options): stdout = options.stdout newfilename = options.newfile if options.newfile == options.oldfile: print >>stdout, "modifying event file in place" newfilename = newfilename + ".tmp" if options.newfile.endswith(".bz2"): newfile = bz2.BZ2File(newfilename, "w") else: newfile = open(newfilename, "wb") newfile.write(flogfile.MAGIC) after = options['after'] if after is not None: print >>stdout, " --after: removing events before %s" % time.ctime(after) before = options['before'] if before is not None: print >>stdout, " --before: removing events after %s" % time.ctime(before) above = options['above'] if above: print >>stdout, " --above: removing events below level %d" % above from_tubid = options['from'] if from_tubid: print >>stdout, " --from: retaining events only from tubid prefix %s" % from_tubid strip_facility = options['strip-facility'] if strip_facility is not None: print >>stdout, "--strip-facility: removing events for %s and children" % strip_facility total = 0 copied = 0 for e in flogfile.get_events(options.oldfile): if options['verbose']: if "d" in e: print >>stdout, e['d']['num'] else: print >>stdout, "HEADER" total += 1 if "d" in e: if before is not None and e['d']['time'] >= before: continue if after is not None and e['d']['time'] <= after: continue if above is not None and e['d']['level'] < above: continue if from_tubid is not None and not e['from'].startswith(from_tubid): continue if (strip_facility is not None and e['d'].get('facility', "").startswith(strip_facility)): continue copied += 1 flogfile.serialize_raw_wrapper(newfile, e) newfile.close() if options.newfile == options.oldfile: if sys.platform == "win32": # Win32 can't do an atomic rename to an existing file. try: os.unlink(options.newfile) except OSError: pass move_into_place(newfilename, options.newfile) print >>stdout, "copied %d of %d events into new file" % (copied, total)
def save_service_data(basedir, data): assert data["version"] == 1 services_file = os.path.join(basedir, "services.json") tmpfile = services_file+".tmp" f = open(tmpfile, "wb") json.dump(data, f, indent=2) f.close() move_into_place(tmpfile, services_file)
def finished_recording(self): self.f2.close() move_into_place(self.abs_filename_bz2_tmp, self.abs_filename_bz2) # the compressed logfile has closed successfully. We no longer care # about the uncompressed one. self.f1.close() os.unlink(self.abs_filename) # now we can tell the world about our new incident report eventually(self.logger.incident_recorded, self.abs_filename_bz2, self.name, self.trigger)
def run(self, options): stdout = options.stdout newfilename = options.newfile if options.newfile == options.oldfile: print("modifying event file in place", file=stdout) newfilename = newfilename + ".tmp" if options.newfile.endswith(".bz2"): newfile = bz2.BZ2File(newfilename, "w") else: newfile = open(newfilename, "wb") newfile.write(flogfile.MAGIC) after = options['after'] if after is not None: print(" --after: removing events before %s" % time.ctime(after), file=stdout) before = options['before'] if before is not None: print(" --before: removing events after %s" % time.ctime(before), file=stdout) above = options['above'] if above: print(" --above: removing events below level %d" % above, file=stdout) from_tubid = options['from'] if from_tubid: print(" --from: retaining events only from tubid prefix %s" % from_tubid, file=stdout) strip_facility = options['strip-facility'] if strip_facility is not None: print("--strip-facility: removing events for %s and children" % strip_facility, file=stdout) total = 0 copied = 0 for e in flogfile.get_events(options.oldfile): if options['verbose']: if "d" in e: print(e['d']['num'], file=stdout) else: print("HEADER", file=stdout) total += 1 if "d" in e: if before is not None and e['d']['time'] >= before: continue if after is not None and e['d']['time'] <= after: continue if above is not None and e['d']['level'] < above: continue if from_tubid is not None and not e['from'].startswith( from_tubid): continue if (strip_facility is not None and e['d'].get( 'facility', "").startswith(strip_facility)): continue copied += 1 flogfile.serialize_raw_wrapper(newfile, e) newfile.close() if options.newfile == options.oldfile: if sys.platform == "win32": # Win32 can't do an atomic rename to an existing file. try: os.unlink(options.newfile) except OSError: pass move_into_place(newfilename, options.newfile) print("copied %d of %d events into new file" % (copied, total), file=stdout)