[Tutor] datetime substraction

Anirudh Tamsekar anirudh.tamsekar at gmail.com
Tue Aug 26 11:10:30 CEST 2014


Hi,

I'm writing a script to monitor rsyslog (second level check).

Overview:

We have Monit for monitoring rsyslog, however we need a script to do a
second level checks.

I'm looking for a script which will check if rsyslog is being updated, also
log rotation is happening.
Incase of the new file created by logrotate, script should monitor the
latest file. Also monitor last modified time and tail of the log.

Filename Ex. 20140826_debug_log (datechanges with log rotate)

In case of any issues such as file not being updated within the interval of
5 min, alert with disk status.


I have written a script, however I'm not able to get the date substraction
math right, getting the following error
(Searched google and other resources too).

*Traceback (most recent call last):*
*  File "./rsyslog_check.py", line 22, in <module>*
*    difft=cur_time-mt*
*TypeError: unsupported operand type(s) for -: 'str' and 'str'*

Request your help. Also point any good documentation where I can get quick
reference.

##########################
#! /usr/bin/python

# import libraries
import os
import sys
import datetime
import time
import smtplib
import Queue
import threading
import subprocess

# Defining main variables
tfile='/local/rsyslog/20140727_msg_debug.log'
dt = datetime.datetime.now().strftime("%Y%m%d")
file = "_msg_debug.log"
filename = "%s%s" % (dt,file)
filepath = '/local/rsyslog/'+filename
ct = time.ctime(os.path.getctime(tfile))
mt = time.ctime(os.path.getctime(tfile))
cur_time = time.ctime()
difft=int(cur_time-mt)
tdelta=datetime.timedelta(minute=5)
# Main


if os.path.isfile(filepath) == True:

        print "%s exists \n" % (tfile)
        print "This file was created at %s UTC \n" % (ct)
     *   if difft > tdelta:*
*                print "File is not modified, last modified at %s UTC" %
(mt)*
*        else:*
*                print "File is being modified"*


*else:*
*                os.path.isfile(filepath)*


# Tailing the file
tailq = Queue.Queue(maxsize=10) # buffer at most 100 lines

def tail_forever(filepath):
    p = subprocess.Popen(["tail", tfile], stdout=subprocess.PIPE)
    while 1:
        line = p.stdout.readline()
        tailq.put(line)
        if not line:
            break

threading.Thread(target=tail_forever, args=(tfile,)).start()
print "\n # Tailing Log file %s" % (tfile)
print "\n ***********************************"
print tailq.get() # blocks
print tailq.get_nowait() # throws Queue.Empty if there are no lines to read
exit()

-Thanks,
Anirudh Tamsekar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140826/a3e74e01/attachment.html>


More information about the Tutor mailing list