3 # Extract statistics from ftp daemon log.
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.
9 # Default file is /usr/adm/ftpd; a "-" means read stdandard input.
11 # The script must be run on the host where the ftp daemon runs.
12 # (At CWI this is currently buizerd.)
20 pat
= '^\([a-zA-Z0-9 :]*\)!\(.*\)!\(.*\)!\([<>].*\)!\([0-9]+\)!\([0-9]+\)$'
21 prog
= regex
.compile(pat
)
27 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'm:s:')
28 except getopt
.error
, msg
:
30 print 'usage: ftpstats [-m maxitems] [file]'
34 maxitems
= string
.atoi(a
)
37 file = '/usr/adm/ftpd'
38 if args
: file = args
[0]
60 if search
and string
.find(line
, search
) < 0:
62 if prog
.match(line
) < 0:
63 print 'Bad line', lineno
, ':', `line`
65 items
= prog
.group(1, 2, 3, 4, 5, 6)
66 (logtime
, loguser
, loghost
, logfile
, logbytes
,
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 //...
81 i
= string
.find(logfile
, '//')
83 logfile
= logfile
[i
+1:]
84 add(byfile
, logfile
+ ' ' + direction
, items
)
85 logdir
= os
.path
.dirname(logfile
)
86 ## logdir = os.path.normpath(logdir) + '/.'
88 add(bydir
, logdir
+ ' ' + direction
, items
)
89 dirhead
= os
.path
.dirname(logdir
)
90 if dirhead
== logdir
: break
92 add(byhost
, loghost
, items
)
93 add(byuser
, loguser
, items
)
94 add(bytype
, direction
, items
)
95 except KeyboardInterrupt:
96 print 'Interrupted at line', lineno
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')
105 def showbar(dict, title
):
107 print '='*((70-n
)/2), title
, '='*((71-n
)/2)
113 list.append((len(dict[key
]), key
))
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
))
123 print '%5d %-*s %s' % (count
, maxkeylength
, key
, bar
)
125 def show(dict, title
, maxitems
):
126 if len(dict) > maxitems
:
127 title
= title
+ ' (first %d)'%maxitems
129 print '='*((70-n
)/2), title
, '='*((71-n
)/2)
133 list.append((-len(dict[key
]), key
))
135 for count
, key
in list[:maxitems
]:
136 print '%5d %s' % (-count
, key
)
138 def add(dict, key
, item
):
139 if dict.has_key(key
):
140 dict[key
].append(item
)