/usr/lib/python3/dist-packages/cronlog.py is in python3-crontab 1.8.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #
# Copyright 2013, Martin Owens <doctormo@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import re
import sys
import string
import platform
py3 = platform.python_version()[0] == '3'
if py3:
unicode = str
from dateutil import parser as dateparse
MATCHER = r'(?P<date>\w+ +\d+ +\d\d:\d\d:\d\d) (?P<host>\w+) ' + \
r'CRON\[(?P<pid>\d+)\]: \((?P<user>\w+)\) CMD \((?P<cmd>.*)\)'
def size(filename):
return os.stat(filename)[6]
class LogReader(object):
"""Opens a Log file, reading backwards and watching for changes"""
def __init__(self, filename, mass=4096):
self.filename = filename
if py3:
self.pipe = open(filename, 'r', encoding='utf-8')
else:
self.pipe = open(filename, 'r')
self.mass = mass
self.size = -1
self.read = -1
def readlines(self, until=0):
"""Iterator for reading lines from a file backwards"""
if not self.pipe:
raise IOError("Can't readline, no opened file.")
# Always seek to the end of the file, this accounts for file updates
# that happen during our running process.
self.size = size(self.filename)
block_num = 0
location = self.size
halfline = ''
while location > until:
location -= self.mass
if location < 0:
location = 0
self.pipe.seek(location)
data = self.pipe.read(self.mass) + halfline
data = data.strip().split('\n')
if location != 0:
halfline = data.pop(0)
loc = location + self.mass
data.reverse()
for line in data:
if line.strip() == '':
continue
yield (loc, line)
loc -= len(line)
def __iter__(self):
for (offset, line) in self.readlines():
yield line
class CronLog(LogReader):
def __init__(self, filename=None, user=None):
if not filename:
filename = '/var/log/syslog'
LogReader.__init__(self, filename)
self.user = user
def for_program(self, command):
return ProgramLog(self, command)
def __iter__(self):
for (offset, line) in self.readlines():
c = re.match(MATCHER, unicode(line))
datum = c and c.groupdict()
if datum and (not self.user or datum['user'] == self.user):
datum['date'] = dateparse.parse(datum['date'])
yield datum
class ProgramLog(object):
"""Specific log control for a single command/program"""
def __init__(self, log, command):
self.log = log
self.command = command
def __iter__(self):
for entry in self.log:
if entry['cmd'] == unicode(self.command):
yield entry
#else:
# print "'%s' != '%s'" % (entry['cmd'], self.command)
|