Logwatch python

C

Cleuson Alves

Hello, I am trying to run this code, but I get an answer incorrect arguments numbers. someone could put an example of arguments for me to use in the / var / log?

Thank you.

########################################################################

import os, sys
from optparse import OptionParser

def main():
usage = "%prog [options] args"
parser = OptionParser(usage)
parser.add_option("-l", "--logfile", dest="logfile", help="Logfile to read data")
parser.add_option("-p", "--logpos", dest="logpos", help="File to store last log line read position")
parser.add_option("-f", "--find", dest="findstring", help="String to find in Logfile")
(options, args) = parser.parse_args()
if options.logfile is None or options.findstring is None or options.logpos is None:
print("Incorrect arguments numbers.\n")
parser.print_help()
sys.exit(-1)
else:
logfile = options.logfile
tofind = options.findstring
logpos = options.logpos

pos = 0
count = 0
if os.path.isfile(logpos):
pos = int(open(logpos).readline() or 0)
file = open(logfile)
file.seek(pos)
for line in file:
if line.find(tofind) != -1:
count += 1
pos = file.tell()
file.close()
file = open(logpos, 'w')
file.write(str(pos))
file.close()
print count

if __name__ == '__main__':

main()
 
R

Roy Smith

Cleuson Alves said:
Hello, I am trying to run this code, but I get an answer incorrect arguments
numbers. someone could put an example of arguments for me to use in the / var
/ log?

Since the first cave man tried to sort his rock collection into
descending order of mastodon kills, people have been inventing really
awesome debugging tools. One of the earliest, and still near the top of
most people's toolkits is the print statement.

You've got:
if options.logfile is None or options.findstring is None or
options.logpos is None:
print("Incorrect arguments numbers.\n")

Start by printing out the values of options.logfile, options.findstring,
and options.logpos. Then at least you will know which of those three is
causing the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top