#!/usr/bin/python -O # # Dinko Korunic 'kreator', 2005. # wlan2-stat.py # script for WLAN statistics from Cisco syslog output # - statistics per AP per month and log message # # Copyright (C) 2005 Dinko Korunic, InfoMAR d.o.o. # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # $Id: wlan2-stat.py 162 2005-07-19 22:25:21Z kreator $ import sys, re def main(): months = { 'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12 } # helpful regexes time_regex = '^(\w{3}) +\d+ \d{2}:\d{2}:\d{2}' ro_name = '([a-zA-Z0-9_\-]+)' misc_regex = '.+' space = ' ' # standard Cisco log messages ap_logmsg = { 'assoc': '%DOT11-6-ASSOC:', 'disassoc': '%DOT11-6-DISASSOC:', 'maxret': '%DOT11-4-MAXRETRIES:', 'authfail': '%DOT11-7-AUTH_FAILED:', 'updownif': '%LINK-3-UPDOWN:', 'resetif': '%LINK-5-CHANGED:' } ap_stats = {} # generate prefix regexes for key, val in ap_logmsg.iteritems(): ap_logmsg[key] = re.compile(time_regex + space + ro_name + space + misc_regex + val) # match lines for line in sys.stdin: for msg_key, msg_val in ap_logmsg.iteritems(): result = msg_val.match(line) if result: msg_mth = months[result.group(1)] msg_ro = result.group(2) # ro/mth/key (+)= counter # ro_stats[msg_ro][msg_mth][msg_key] += msg_counter try: local_ro_mth = ap_stats[msg_ro] except KeyError: ap_stats[msg_ro] = {} local_ro_mth = ap_stats[msg_ro] try: local_ro_mth_key = local_ro_mth[msg_mth] except KeyError: local_ro_mth[msg_mth] = {} local_ro_mth_key = local_ro_mth[msg_mth] try: local_ro_mth_key[msg_key] += 1 except KeyError: local_ro_mth_key[msg_key] = 1 # DEBUG output ap_sorted = ap_stats.keys() ap_sorted.sort() for k in ap_sorted: print k, ':', ap_stats[k] if __name__ == '__main__': main()