Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 4e8aef8 | 1994-01-07 10:53:41 +0000 | [diff] [blame] | 2 | |
| 3 | # Extract statistics from ftp daemon log. |
| 4 | |
| 5 | # Usage: |
| 6 | # ftpstats [-m maxitems] [-s search] [file] |
| 7 | # -m maxitems: restrict number of items in "top-N" lists, default 25. |
| 8 | # -s string: restrict statistics to lines containing this string. |
Andrew M. Kuchling | 0ec0b72 | 2004-07-17 14:44:17 +0000 | [diff] [blame] | 9 | # Default file is /usr/adm/ftpd; a "-" means read standard input. |
Guido van Rossum | 4e8aef8 | 1994-01-07 10:53:41 +0000 | [diff] [blame] | 10 | |
| 11 | # The script must be run on the host where the ftp daemon runs. |
| 12 | # (At CWI this is currently buizerd.) |
| 13 | |
| 14 | import os |
| 15 | import sys |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 16 | import re |
Guido van Rossum | 4e8aef8 | 1994-01-07 10:53:41 +0000 | [diff] [blame] | 17 | import string |
| 18 | import getopt |
| 19 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 20 | pat = '^([a-zA-Z0-9 :]*)!(.*)!(.*)!([<>].*)!([0-9]+)!([0-9]+)$' |
| 21 | prog = re.compile(pat) |
Guido van Rossum | 4e8aef8 | 1994-01-07 10:53:41 +0000 | [diff] [blame] | 22 | |
| 23 | def main(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 24 | maxitems = 25 |
| 25 | search = None |
| 26 | try: |
| 27 | opts, args = getopt.getopt(sys.argv[1:], 'm:s:') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 28 | except getopt.error as msg: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 29 | print(msg) |
| 30 | print('usage: ftpstats [-m maxitems] [file]') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 31 | sys.exit(2) |
| 32 | for o, a in opts: |
| 33 | if o == '-m': |
| 34 | maxitems = string.atoi(a) |
| 35 | if o == '-s': |
| 36 | search = a |
| 37 | file = '/usr/adm/ftpd' |
| 38 | if args: file = args[0] |
| 39 | if file == '-': |
| 40 | f = sys.stdin |
| 41 | else: |
| 42 | try: |
| 43 | f = open(file, 'r') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 44 | except IOError as msg: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 45 | print(file, ':', msg) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 46 | sys.exit(1) |
| 47 | bydate = {} |
| 48 | bytime = {} |
| 49 | byfile = {} |
| 50 | bydir = {} |
| 51 | byhost = {} |
| 52 | byuser = {} |
| 53 | bytype = {} |
| 54 | lineno = 0 |
| 55 | try: |
| 56 | while 1: |
| 57 | line = f.readline() |
| 58 | if not line: break |
| 59 | lineno = lineno + 1 |
| 60 | if search and string.find(line, search) < 0: |
| 61 | continue |
| 62 | if prog.match(line) < 0: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 63 | print('Bad line', lineno, ':', repr(line)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 64 | continue |
| 65 | items = prog.group(1, 2, 3, 4, 5, 6) |
| 66 | (logtime, loguser, loghost, logfile, logbytes, |
| 67 | logxxx2) = items |
| 68 | ## print logtime |
| 69 | ## print '-->', loguser |
| 70 | ## print '--> -->', loghost |
| 71 | ## print '--> --> -->', logfile |
| 72 | ## print '--> --> --> -->', logbytes |
| 73 | ## print '--> --> --> --> -->', logxxx2 |
| 74 | ## for i in logtime, loghost, logbytes, logxxx2: |
| 75 | ## if '!' in i: print '???', i |
| 76 | add(bydate, logtime[-4:] + ' ' + logtime[:6], items) |
| 77 | add(bytime, logtime[7:9] + ':00-59', items) |
| 78 | direction, logfile = logfile[0], logfile[1:] |
| 79 | # The real path probably starts at the last //... |
| 80 | while 1: |
| 81 | i = string.find(logfile, '//') |
| 82 | if i < 0: break |
| 83 | logfile = logfile[i+1:] |
| 84 | add(byfile, logfile + ' ' + direction, items) |
| 85 | logdir = os.path.dirname(logfile) |
| 86 | ## logdir = os.path.normpath(logdir) + '/.' |
| 87 | while 1: |
| 88 | add(bydir, logdir + ' ' + direction, items) |
| 89 | dirhead = os.path.dirname(logdir) |
| 90 | if dirhead == logdir: break |
| 91 | logdir = dirhead |
| 92 | add(byhost, loghost, items) |
| 93 | add(byuser, loguser, items) |
| 94 | add(bytype, direction, items) |
| 95 | except KeyboardInterrupt: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 96 | print('Interrupted at line', lineno) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 97 | show(bytype, 'by transfer direction', maxitems) |
| 98 | show(bydir, 'by directory', maxitems) |
| 99 | show(byfile, 'by file', maxitems) |
| 100 | show(byhost, 'by host', maxitems) |
| 101 | show(byuser, 'by user', maxitems) |
| 102 | showbar(bydate, 'by date') |
| 103 | showbar(bytime, 'by time of day') |
Guido van Rossum | 4e8aef8 | 1994-01-07 10:53:41 +0000 | [diff] [blame] | 104 | |
| 105 | def showbar(dict, title): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 106 | n = len(title) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 107 | print('='*((70-n)/2), title, '='*((71-n)/2)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 108 | list = [] |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 109 | keys = list(dict.keys()) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 110 | keys.sort() |
| 111 | for key in keys: |
| 112 | n = len(str(key)) |
| 113 | list.append((len(dict[key]), key)) |
| 114 | maxkeylength = 0 |
| 115 | maxcount = 0 |
| 116 | for count, key in list: |
| 117 | maxkeylength = max(maxkeylength, len(key)) |
| 118 | maxcount = max(maxcount, count) |
| 119 | maxbarlength = 72 - maxkeylength - 7 |
| 120 | for count, key in list: |
| 121 | barlength = int(round(maxbarlength*float(count)/maxcount)) |
| 122 | bar = '*'*barlength |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 123 | print('%5d %-*s %s' % (count, maxkeylength, key, bar)) |
Guido van Rossum | 4e8aef8 | 1994-01-07 10:53:41 +0000 | [diff] [blame] | 124 | |
| 125 | def show(dict, title, maxitems): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 126 | if len(dict) > maxitems: |
| 127 | title = title + ' (first %d)'%maxitems |
| 128 | n = len(title) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 129 | print('='*((70-n)/2), title, '='*((71-n)/2)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 130 | list = [] |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 131 | keys = list(dict.keys()) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 132 | for key in keys: |
| 133 | list.append((-len(dict[key]), key)) |
| 134 | list.sort() |
| 135 | for count, key in list[:maxitems]: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 136 | print('%5d %s' % (-count, key)) |
Guido van Rossum | 4e8aef8 | 1994-01-07 10:53:41 +0000 | [diff] [blame] | 137 | |
| 138 | def add(dict, key, item): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 139 | if key in dict: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 140 | dict[key].append(item) |
| 141 | else: |
| 142 | dict[key] = [item] |
Guido van Rossum | 4e8aef8 | 1994-01-07 10:53:41 +0000 | [diff] [blame] | 143 | |
Johannes Gijsbers | 7a8c43e | 2004-09-11 16:34:35 +0000 | [diff] [blame] | 144 | if __name__ == "__main__": |
| 145 | main() |