Logwatch python

Discussion in 'Python' started by Cleuson Alves, Feb 9, 2013.

  1. 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()
     
    Cleuson Alves, Feb 9, 2013
    #1
    1. Advertising

  2. Cleuson Alves

    Roy Smith Guest

    In article <>,
    Cleuson Alves <> wrote:

    > 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.
     
    Roy Smith, Feb 9, 2013
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    770
  2. Paul Moore
    Replies:
    0
    Views:
    654
    Paul Moore
    Mar 1, 2008
  3. Martin v. Löwis
    Replies:
    0
    Views:
    691
    Martin v. Löwis
    Mar 1, 2008
  4. Senthil Kumaran
    Replies:
    0
    Views:
    600
    Senthil Kumaran
    Jan 17, 2011
  5. R. David Murray
    Replies:
    0
    Views:
    792
    R. David Murray
    Jan 17, 2011
Loading...

Share This Page