#!/usr/bin/python -O # # Dinko Korunic 'kreator', 2005. # ro-stat.py # script for router statistics from Cisco syslog output # - statistics per router 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: ro-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 = '.+' number = '\d+' number_g = '(' + number + ')' space = ' ' # standard Cisco log messages ro_logmsg = { 'updownif': '%LINK-3-UPDOWN:' + misc_regex + 'changed state', 'resetif': '%LINK-5-CHANGED:' + misc_regex + 'changed state', 'isdnconn': '%ISDN-6-CONNECT:' + misc_regex + 'is now connected to' + space + number + space + ro_name, 'isdndisconn': '%ISDN-6-DISCONNECT:' + misc_regex + 'disconnected from' + space + number + space + ro_name + misc_regex + 'call lasted' + space + number_g + space + 'seconds', 'cryptoup': '%CRYPTO-5-SESSION_STATUS: Crypto tunnel is UP', 'cryptodown': '%CRYPTO-5-SESSION_STATUS: Crypto tunnel is DOWN' } ro_stats = {} # generate prefix regexes for key, val in ro_logmsg.iteritems(): ro_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 ro_logmsg.iteritems(): result = msg_val.match(line) if result: msg_mth = months[result.group(1)] msg_ro = result.group(2) # associate NAS with calling user try: msg_ro_dst = result.group(3) msg_ro += '/' + msg_ro_dst except IndexError: pass # are we using calltime or we are just incrementing # the counter? try: msg_counter = int(result.group(4)) except IndexError: msg_counter = 1 # ro/mth/key (+)= counter # ro_stats[msg_ro][msg_mth][msg_key] += msg_counter try: local_ro_mth = ro_stats[msg_ro] except KeyError: ro_stats[msg_ro] = {} local_ro_mth = ro_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] += msg_counter except KeyError: local_ro_mth_key[msg_key] = msg_counter # DEBUG output ro_sorted = ro_stats.keys() ro_sorted.sort() for k in ro_sorted: print k, ':', ro_stats[k] if __name__ == '__main__': main()