#! /bin/sh
. /usr/lib/innshellvars

##  Display status of INN.
##  Written by Landon Curt Noll <chongo@toad.com>.

SYSLOG_CRIT=news.crit
SYSLOG_ERR=news.err
SYSLOG_NOTICE=news.notice
SYSLOGS="${SYSLOG_CRIT} ${SYSLOG_ERR} ${SYSLOG_NOTICE}"

##  Set up the list of log files.
LOGS="${SYSLOGS}"
if [ -f "${MOST_LOGS}/$(basename ${ERRLOG})" ]; then
    LOGS="${LOGS} $(basename ${ERRLOG})"
else
    LOGS="${LOGS} ${ERRLOG}"
fi
if [ -f "${MOST_LOGS}/$(basename ${LOG})" ]; then
    LOGS="${LOGS} $(basename ${LOG})"
else
    LOGS="${LOGS} ${LOG}"
fi

##  Show INND status.
echo 'Server status:'
ctlinnd mode 2>&1

##  Show disk usage.  You might have to change this.
echo ''
echo 'Disk usage:'
DISKUSAGE="${SPOOL} ${OVERVIEWDIR} ${PATHETC} ${INCOMING} ${BATCH}"
DISKUSAGE="${DISKUSAGE} ${PATHDB} ${TMPDIR} ${MOST_LOGS}"
if [ -d "${PATHARCHIVE}" ]; then
    DISKUSAGE="${DISKUSAGE} ${PATHARCHIVE}"
fi
if [ -d "${PATHHTTP}" ]; then
    DISKUSAGE="${DISKUSAGE} ${PATHHTTP}"
fi
${INNDF} ${DISKUSAGE} | ${SORT} -u

##  Show overview usage for buffindexed.
[ ${OVMETHOD} = 'buffindexed' ] && {
    echo ''
    echo 'Overview buffer usage:'
    ${INNDF} -no
}

##  Show size of batch files.
echo ''
echo 'Batch file sizes (in Kbytes):'
(
    cd ${BATCH}
    ls -Cs | sed 1d
)

##  Show size of log files.
echo ''
echo 'Log file sizes (in Kbytes):'
(
    cd ${MOST_LOGS}
    ls -Cs ${LOGS} *.log 2>&1
)

##  Show the lock files
echo ''
(
    cd ${LOCKS}
    set -$- LOCK.*
    if [ -f "$1" ]; then
        echo 'Lock files:'
        ls -C LOCK.* 2>&1
    else
        echo 'innwatch is not running'
    fi
)

echo ''
echo 'Server connections:'
ctlinnd -t60 name '' 2>&1 \
    | ${PERL} -ne '
     next if m/(^((rem|local)conn|control)|:proc|:file):/;
     s/^(\S+):(\d+):.*:.*:.*$/${1}:${2}/;
     m/^(\S+):(\d+)$/;
     $c{$1} = [] unless $c{$1};
     @l = @{$c{$1}};
     push(@l, $2);
     $c{$1} = [@l];
     $m++;

     END {
       $n = 0;
       foreach $f (sort {$#{$c{$b}} <=> $#{$c{$a}}} keys %c) {
          printf "%-35.35s %3d  (%s", $f, 1 + $#{$c{$f}}, "@{$c{$f}})\n";
          $n++;
       }
       printf "\n%-35s %3d\n", "TOTAL: $n", $m;
     }'
