Saturday, October 16, 2010

scanlogs-0.0.1-1

First draft of the log scanning script. Only searches through '.log' files, purposefully ommitting '.log.n' files created by logrotate. Special case search terms can be added in a case statement, as seen with the Xorg.0.log example. It then goes through each file, and if any instances of the search-term are found, it displays the name of the file in red and asks if it should be displayed, which is then piped through less (later it will only be piped through less if it's too long to fit on the screen). If no instances are found in the file, it prints the name of the file in blue and "clean" under it, then moves to the next file.

Note: right now the auth.log example doesn't work properly. I also plan to code in multiple search terms.

#!/bin/bash

chkroot && [[ $? = 0 ]] || exit 1

logpath='/var/log/'

alllogs=`ls $logpath | grep 'log$'`

clear

for i in $alllogs; do


    #special log cases
    case $i in
        "Xorg.0.log")
            search_term="(EE)"
        ;;
        "auth.log")
            search_term='authentication failure'
        ;;
        *)
            search_term="error"
    esac

    num_errors=`cat $logpath$i | grep -c "$search_term"`
    if [[ "$num_errors" -gt "0" ]]; then
        echo -e "\033[1;31m$i\033[0m"
        echo " :: found $num_errors instances of '$search_term', expand? [y/N]"
        read choice
        if [[ $choice = ['y','Y'] ]]; then
            cat $logpath$i | grep $search_term | less
        fi
    else
        echo -e "\033[1;34m$i\033[0m"
        echo " :: clean"
        echo
    fi
done

exit 0

No comments:

Post a Comment