Mini Shell

Direktori : /opt/sharedrads/
Upload File :
Current File : //opt/sharedrads/check_exim

#!/bin/bash

LINESDEF="120000"
LINES=$LINESDEF;
MAIL_LOG="/var/log/exim_mainlog";
if [ ! -f $MAIL_LOG ]; then
        echo "Can't find $MAIL_LOG - bailing."
        exit 1;
fi

function printUsage () {
    echo;echo "Usage: $0 --command (--lines NUM_LINES)"
        echo;echo " --status"
        echo -e "\tSorts the output of the exiwhat command, giving you total incoming connetions, also gives you top IPs/subnets connecting."
        echo;echo " --queuebysender"
        echo -e "\tShow the count of messages in the mail queue by sender."
        echo;echo " --queuebybounceback"
        echo -e "\tShow a count of who bouncebacks are going to."
        echo;echo " --bouncesbydomain"
        echo -e "\tShow a count of bouncebacks per domain."
        echo;echo " --recipientisp"
        echo -e "\tShow the recipient isp with the most queued."
        echo;echo " --recipienttest mail.remoteserver.com"
        echo -e "\tTest the recipient mail server for connectivity or rejection errors."
        echo;echo " --fullmbox "
        echo -e "\tShow errors about accounts reaching mail quota."
    echo;echo " --listdirs";
    echo -e "\tLists all the dirs where mail has originated from in the last $LINES lines, with timestamps."
    echo;echo " --dirtotals"
    echo -e "\tLists all the dirs where mail has originated from in the last $LINES lines, sorted and counted."
    echo; echo " --fwdtotals"
    echo -e "\tLists counts of the highest number of forward requests recieved by user/email"
    echo;echo " --lines NUM_LINES"
    echo -e "\tOptional third argument for listdirs, dirtotals, fullmbox and fwdtotals: define how many NUM_LINES back in the log files you are checking. Default: 120000"
        echo
}
function topconns () {
        tail -${LINES-$LINEDEF} $MAIL_LOG | awk '/SMTP connection from/{gsub(/.:[0-9]+/,"",$0);gsub(/\[/,"",$0);print $7 }' |sort | uniq -c | sort -nk1
}

function listdirs () {
         tail -${LINES-$LINESDEF} $MAIL_LOG | awk '$4 ~ /cwd=\/home./ {print $1,$2,$4}' | sort -k3;
}

function dirtotals () {
         tail -${LINES:-$LINESDEF} $MAIL_LOG | awk '$4 ~ /cwd=\/home./ {print $4}' | sort | uniq -c | sort -nk1;
}
function fwdtotals () {
        tail -${LINES:-$LINESDEF} $MAIL_LOG | egrep "O=.*@[a-zA-Z0-9_\.\=\%+-]*.*U=[a-zA-Z0-9]{5,12}"  -o |  sed 's/O=//;s/\ E=/:/;s/\ M=.*U=/:/' | awk -F ":" '{print "User:" $3"\t" $1" -> "$2}' | sort | uniq -c | sort -nk1
}
function queuebysender () {
        exiqgrep -f ".*" | grep -E "<.*>" -o | sort | uniq -c | sort -nk1
}
function queuebybounceback()
{
    exiqgrep -f "<>" | egrep  "[a-zA-Z_\.\=\%+-].*@.*" -o |tr '[:upper:]' '[:lower:]'| sort | uniq -c | sort -nk1
}
function bouncesbydomain()
{
        exiqgrep -f "<>" | egrep "@.*" -o | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -nk1
}
function recipientisp () {
        exim -bp |exiqsumm |sort -nk1
}
function recipienttest () {
        echo "testing" | mail -vv -s 'testing connectivity' test@$2
}
function fullmbox () {
        tail -${LINES:-$LINESDEF} $MAIL_LOG | grep "mailbox is full " | awk '{print $1,$6}' | sort | uniq -c |sort -nk1
}
function check_time () {
        START=$(tail -${LINES:-$LINESDEF} $MAIL_LOG | head -1|awk '{print $1,$2}')
        END=$(tail -${LINES:-$LINESDEF} $MAIL_LOG | tail -1|awk '{print $1,$2}')
        echo "-Logs from $START to $END";
}


if [ $# -eq 3 ] && [ "$2" == "--lines" ];
then
        LINES=$3;
        #echo $LINES;
fi
case $1 in
--status)
        exiwhat | awk -F'from' '{print $1}' | awk '{$1=""; print}' | sort | uniq -c |sort -nk1;
        echo "===IPs Connecting==="
        exiwhat | awk '$NF ~ /[0-9]*\.[0-9]*\.[0-9]*\./ && /incoming/{gsub(/\[|\]/,"",$NF);print $NF}' | sort | uniq -c | sort -nk1
        echo "===Subnets Connecting==="
        exiwhat | awk '$NF ~ /[0-9]*\.[0-9]*\.[0-9]*\./ && /incoming/{gsub(/\[|\]/,"",$NF);print $NF}'| sort | uniq |
         awk -F. '{print $1"."$2"."$3}' | sort | uniq -c | sort -nk1
        ;;
--listdirs)
        listdirs
        check_time
        ;;
--dirtotals)
        dirtotals
        check_time
        ;;
--topconns)
        topconns
        check_time
        ;;
--queuebysender)
        queuebysender
        ;;
--queuebybounceback)
        queuebybounceback
        ;;
--bouncesbydomain)
        bouncesbydomain
        ;;
--fwdtotals)
        fwdtotals
        check_time
        ;;
--recipientisp)
        recipientisp
        ;;
--recipienttest)
        recipienttest
        ;;
--fullmbox)
        fullmbox
        check_time
        ;;
--help) printUsage; exit;;
*) printUsage; exit 1;;
esac

Zerion Mini Shell 1.0