Syslog
Rainy
sill at optonline.net
Sat Jun 2 11:34:32 EDT 2001
On Sat, 02 Jun 2001 15:40:57 +0100, Simon Faulkner <News at Titanic.co.uk> wrote:
> I have a firewall that sends a ling to syslog on my Linux box every 5
> mins with the number of bytes transferred in that time:
>
> Jun 2 14:52:37 192.168.146.100 NameOfFirewall: Stats filter 12: 1363
> (All out)
>
> I want to srtip this number out every 5 mins so I can feed it to MRTG.
>
> Any sugestions about the best way to achieve this?
>
> Simon
> Simon Faulkner
Well, I have this syslog-monitoring script I got somewhere and improved a
bit, it colorizes and selectively prints out syslog as it grows (but doesn't
handle log rotation yet).
Use it like this: # pctail.py /var/log/syslog
#!/usr/bin/env python
import string, time
import select
import sys
import getopt
import re, os
def colorize(color, text):
"""Return colorized text"""
col = '\033[0;3'
if not color: return text
elif color == 'red': return col + str(1) + 'm' + text + col + str(7) + 'm'
elif color == 'green': return col + str(2) + 'm' + text + col + str(7) + 'm'
elif color == 'yellow': return col + str(3) + 'm' + text + col + str(7)+ 'm'
elif color == 'blue': return col + str(4) + 'm' + text + col + str(7) + 'm'
elif color == 'magenta': return col + str(5) + 'm' + text + col + str(7)+'m'
elif color == 'cyan': return col + str(6) + 'm' + text + col + str(7) + 'm'
elif color == 'white': return col + str(7) + 'm' + text + col + str(7) + 'm'
elif color == 'gray': return col + str(8) + 'm' + text + col + str(7) + 'm'
class pctail_file:
def __init__(self, path='/var/log/syslog',num=10,showheaders=0):
self.file=open('/var/log/syslog','r')
self.text=self.file.readlines()
textlen=len(self.text)
start=textlen-num
lines=self.text[start:textlen]
self.printheader()
self.prettyprint(lines)
def printheader(self):
if showheaders:
print '['+self.file.name+']'
def prettyprint(self, lines):
for line in lines:
if line:
if line[-1] == '\n':
line = line[:-1]
list = string.split(line, ' ', 5)
# skip these lines:
if re.search('/USR/SBIN/CRON', list[4]):
continue
if re.search('RTL8139 Interrupt line blocked', list[5]):
continue
if re.search('[192.160.127.97] Successful lookup', list[5]):
continue
if re.search('Connection from Irc.mcs.net (192.160.127.97)',
list[5]):
continue
if re.search('skipper.frogspace.net', list[5]):
continue
# if re.search('
if re.search('ERROR : NO-USER', list[5]):
continue
print colorize('green',list[0]+list[1]+' '+list[2])+' '+\
colorize('blue',list[4])+' '+colorize('yellow',list[5])
#' [' + colorize('green', list[3]) + '] ' +
def fileno(self):
return self.file.fileno()
def founddata(self):
s=self.file.read()
if s != '':
s2=string.split(s, '\n')
self.printheader()
self.prettyprint(s2)
if __name__ == '__main__':
os.system('clear')
print '--- syslog:'
slist=[]
opt,args=getopt.getopt(sys.argv[1:],'qn:')
showheaders=0
num=10
for x in opt:
if x[0]=='-n':
num=x[1]
elif x[0]=='-q':
showheaders=1
for x in args:
s= pctail_file(x,num,showheaders)
slist.append(s)
while 1:
time.sleep(1)
try: wr,ww,we=select.select(slist,[],[])
except KeyboardInterrupt:
print 'Exiting..'
sys.exit()
for x in wr:
x.founddata()
--
Lime and limpid green
a second scene
A fight between the blue
you once knew
- Syd
More information about the Python-list
mailing list