"Fossies" - the Fresh Open Source Software Archive

Member "mosshe/functions.netchecks" (31 Aug 2020, 23531 Bytes) of package /linux/privat/old/mosshe.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/sh
    2 
    3 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    4 #
    5 #   function library - please call with external script
    6 #
    7 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    8 
    9 #############################################################################
   10 # MoSSHe: remote server monitoring environment
   11 #
   12 # Copyright (C) 2009- Volker Tanger
   13 #
   14 # This program is free software; you can redistribute it and/or
   15 # modify it under the terms of the GNU General Public License
   16 # as published by the Free Software Foundation; either version 2
   17 # of the License, or (at your option) any later version.
   18 #
   19 # For bug reports and suggestions or if you just want to talk to me please
   20 # contact me at volker.tanger@wyae.de
   21 #
   22 # Updates will be available at  http://www.wyae.de/software/mosshe/
   23 # please check there for updates prior to submitting patches!
   24 #
   25 # For list of changes please refer to the HISTORY file. Thanks.
   26 #############################################################################
   27 
   28 
   29 
   30 #############################################################################
   31 # network checks - functions below
   32 #############################################################################
   33 
   34 
   35 #---------------------------------------------------------
   36 # PingPartner - FQDN / NumPing / ALERT-Percent / WARN ms roundtrip
   37 #---------------------------------------------------------
   38 PingPartner () {
   39     MossheLog "PingPartner $1"
   40     typeset -i ALERTPERC WARNMS VALUE LOSS WAIT
   41     PARTNER=$1
   42     NUMPING=$2
   43     ALERTPERC=$3
   44     WARNMS=$4
   45     
   46     # ~# ping -c 3 -W 5 -n -q localhost
   47     # PING localhost (127.0.0.1) 56(84) bytes of data.
   48     #
   49     # --- localhost ping statistics ---
   50     # 3 packets transmitted, 3 received, 0% packet loss, time 1998ms
   51     # rtt min/avg/max/mdev = 0.057/0.058/0.060/0.001 ms
   52     ping -c $NUMPING -W $NETWAIT -n -q $PARTNER > $TEMPDIR/tmp.$$.ping_$PARTNER
   53 
   54     STATUS="OK"
   55     MESSAGE="Good ping from $MYGROUP: $MYNAME to $PARTNER"
   56     
   57     LOSS=`fgrep " packet loss, time " $TEMPDIR/tmp.$$.ping_$PARTNER | cut -d " " -f 6 | cut -d "%" -f 1`
   58     VALUE=`fgrep "rtt min/avg/max/mdev" $TEMPDIR/tmp.$$.ping_$PARTNER | cut -d "/" -f 5 | cut -d "." -f 1`
   59     if [ "$VALUE" -gt "$WARNMS" ]; then 
   60         STATUS="WARN"
   61     MESSAGE="High Latency from $MYGROUP: $MYNAME to $PARTNER"
   62     fi
   63     if [ "$LOSS" -gt "$ALERTPERC" ]; then 
   64         STATUS="ALERT"
   65     MESSAGE="Too much packet loss from $MYGROUP: $MYNAME to $PARTNER : ${LOSS}%"
   66     VALUE=$(( WAIT * 1000 ))
   67     fi
   68 
   69     echo "${DATIM};$MYGROUP;$MYNAME;ping_$PARTNER;$STATUS;$VALUE;$MESSAGE : $VALUE ms" >> $TEMPDIR/tmp.$$.collected.tmp
   70     rm $TEMPDIR/tmp.$$.ping_$PARTNER
   71 }
   72 
   73 #---------------------------------------------------------
   74 # Ping6Partner - FQDN / NumPing / ALERT-Percent / WARN ms roundtrip
   75 #---------------------------------------------------------
   76 Ping6Partner () {
   77     MossheLog "PingPartner $1"
   78     typeset -i ALERTPERC WARNMS VALUE LOSS WAIT
   79     PARTNER=$1
   80     NUMPING=$2
   81     ALERTPERC=$3
   82     WARNMS=$4
   83     
   84     # ~# ping -c 3 -W 5 -n -q localhost
   85     # PING localhost (127.0.0.1) 56(84) bytes of data.
   86     #
   87     # --- localhost ping statistics ---
   88     # 3 packets transmitted, 3 received, 0% packet loss, time 1998ms
   89     # rtt min/avg/max/mdev = 0.057/0.058/0.060/0.001 ms
   90     ping6 -c $NUMPING -W $NETWAIT -n -q $PARTNER > $TEMPDIR/tmp.$$.ping_$PARTNER
   91 
   92     STATUS="OK"
   93     MESSAGE="Good ping from $MYGROUP: $MYNAME to $PARTNER"
   94     
   95     LOSS=`fgrep " packet loss, time " $TEMPDIR/tmp.$$.ping_$PARTNER | cut -d " " -f 6 | cut -d "%" -f 1`
   96     VALUE=`fgrep "rtt min/avg/max/mdev" $TEMPDIR/tmp.$$.ping_$PARTNER | cut -d "/" -f 5 | cut -d "." -f 1`
   97     if [ "$VALUE" -gt "$WARNMS" ]; then 
   98         STATUS="WARN"
   99     MESSAGE="High Latency from $MYGROUP: $MYNAME to $PARTNER"
  100     fi
  101     if [ "$LOSS" -gt "$ALERTPERC" ]; then 
  102         STATUS="ALERT"
  103     MESSAGE="Too much packet loss from $MYGROUP: $MYNAME to $PARTNER : ${LOSS}%"
  104     VALUE=$(( WAIT * 1000 ))
  105     fi
  106 
  107     echo "${DATIM};$MYGROUP;$MYNAME;ping6_$PARTNER;$STATUS;$VALUE;$MESSAGE : $VALUE ms" >> $TEMPDIR/tmp.$$.collected.tmp
  108     rm $TEMPDIR/tmp.$$.ping_$PARTNER
  109 }
  110 
  111 #---------------------------------------------------------
  112 # PingLoss - FQDN / NumPing / PacketLoss% WARN / PacketLoss% ALERT
  113 #---------------------------------------------------------
  114 PingLoss () {
  115     MossheLog "PingLoss $1"
  116     typeset -i ALERTPERC WARNPERC VALUE WAIT
  117     PARTNER=$1
  118     NUMPING=$2
  119     WARNPERC=$3
  120     ALERTPERC=$4
  121     
  122     ping -c $NUMPING -W $NETWAIT -n -q $PARTNER > $TEMPDIR/tmp.$$.ping_$PARTNER
  123 
  124     STATUS="OK"
  125     MESSAGE="Ignorable packet loss $MYGROUP: $MYNAME to $PARTNER"
  126     
  127     VALUE=`fgrep " packet loss, time " $TEMPDIR/tmp.$$.ping_$PARTNER | cut -d " " -f 6 | cut -d "%" -f 1`
  128     if [ "$VALUE" -gt "$ALERTPERC" ]; then
  129         STATUS="ALERT"
  130     MESSAGE="Too much packet loss from $MYGROUP: $MYNAME to $PARTNER : ${VALUE}%"
  131     elif [ "$VALUE" -gt "$WARNPERC" ]; then 
  132         STATUS="WARN"
  133     MESSAGE="High packet loss from $MYGROUP: $MYNAME to $PARTNER : ${VALUE}%"
  134     fi
  135 
  136     echo "${DATIM};$MYGROUP;$MYNAME;pingloss_$PARTNER;$STATUS;$VALUE;$MESSAGE : ${VALUE}%" >> $TEMPDIR/tmp.$$.collected.tmp
  137     rm $TEMPDIR/tmp.$$.ping_$PARTNER
  138 }
  139 
  140 #---------------------------------------------------------
  141 # Ping6Loss - FQDN / NumPing / PacketLoss% WARN / PacketLoss% ALERT
  142 #---------------------------------------------------------
  143 Ping6Loss () {
  144     MossheLog "PingLoss $1"
  145     typeset -i ALERTPERC WARNPERC VALUE WAIT
  146     PARTNER=$1
  147     NUMPING=$2
  148     WARNPERC=$3
  149     ALERTPERC=$4
  150     
  151     ping6 -c $NUMPING -W $NETWAIT -n -q $PARTNER > $TEMPDIR/tmp.$$.ping_$PARTNER
  152 
  153     STATUS="OK"
  154     MESSAGE="Ignorable packet loss $MYGROUP: $MYNAME to $PARTNER"
  155     
  156     VALUE=`fgrep " packet loss, time " $TEMPDIR/tmp.$$.ping_$PARTNER | cut -d " " -f 6 | cut -d "%" -f 1`
  157     if [ "$VALUE" -gt "$ALERTPERC" ]; then
  158         STATUS="ALERT"
  159     MESSAGE="Too much packet loss from $MYGROUP: $MYNAME to $PARTNER : ${VALUE}%"
  160     elif [ "$VALUE" -gt "$WARNPERC" ]; then 
  161         STATUS="WARN"
  162     MESSAGE="High packet loss from $MYGROUP: $MYNAME to $PARTNER : ${VALUE}%"
  163     fi
  164 
  165     echo "${DATIM};$MYGROUP;$MYNAME;ping6loss_$PARTNER;$STATUS;$VALUE;$MESSAGE : ${VALUE}%" >> $TEMPDIR/tmp.$$.collected.tmp
  166     rm $TEMPDIR/tmp.$$.ping_$PARTNER
  167 }
  168 
  169 #---------------------------------------------------------
  170 # PingTime - FQDN / NumPing / WARN ms roundtrip / ALERT ms roundtrip
  171 #---------------------------------------------------------
  172 PingTime () {
  173     MossheLog "PingTime $1"
  174     typeset -i ALERTPERC WARNMS VALUE WAIT
  175     PARTNER=$1
  176     NUMPING=$2
  177     WARNMS=$3
  178     ALERTMS=$4
  179     
  180     ping -c $NUMPING -W $NETWAIT -n -q $PARTNER > $TEMPDIR/tmp.$$.ping_$PARTNER
  181 
  182     STATUS="OK"
  183     MESSAGE="Good ping from $MYGROUP: $MYNAME to $PARTNER"
  184     
  185     VALUE=`fgrep "rtt min/avg/max/mdev" $TEMPDIR/tmp.$$.ping_$PARTNER | cut -d "/" -f 5 | cut -d "." -f 1`
  186     if [ "$VALUE" -gt "$ALERTMS" ]; then 
  187         STATUS="ALERT"
  188     MESSAGE="Excessive Latency from $MYGROUP: $MYNAME to $PARTNER"
  189     elif [ "$VALUE" -gt "$WARNMS" ]; then 
  190         STATUS="WARN"
  191     MESSAGE="High Latency from $MYGROUP: $MYNAME to $PARTNER"
  192     fi
  193 
  194     echo "${DATIM};$MYGROUP;$MYNAME;pingtime_$PARTNER;$STATUS;$VALUE;$MESSAGE : $VALUE ms" >> $TEMPDIR/tmp.$$.collected.tmp
  195     rm $TEMPDIR/tmp.$$.ping_$PARTNER
  196 }
  197 
  198 #---------------------------------------------------------
  199 # Ping6Time - FQDN / NumPing / WARN ms roundtrip / ALERT ms roundtrip
  200 #---------------------------------------------------------
  201 Ping6Time () {
  202     MossheLog "PingTime $1"
  203     typeset -i ALERTPERC WARNMS VALUE WAIT
  204     PARTNER=$1
  205     NUMPING=$2
  206     WARNMS=$3
  207     ALERTMS=$4
  208     
  209     ping6 -c $NUMPING -W $NETWAIT -n -q $PARTNER > $TEMPDIR/tmp.$$.ping_$PARTNER
  210 
  211     STATUS="OK"
  212     MESSAGE="Good ping from $MYGROUP: $MYNAME to $PARTNER"
  213     
  214     VALUE=`fgrep "rtt min/avg/max/mdev" $TEMPDIR/tmp.$$.ping_$PARTNER | cut -d "/" -f 5 | cut -d "." -f 1`
  215     if [ "$VALUE" -gt "$ALERTMS" ]; then 
  216         STATUS="ALERT"
  217     MESSAGE="Excessive Latency from $MYGROUP: $MYNAME to $PARTNER"
  218     elif [ "$VALUE" -gt "$WARNMS" ]; then 
  219         STATUS="WARN"
  220     MESSAGE="High Latency from $MYGROUP: $MYNAME to $PARTNER"
  221     fi
  222 
  223     echo "${DATIM};$MYGROUP;$MYNAME;ping6time_$PARTNER;$STATUS;$VALUE;$MESSAGE : $VALUE ms" >> $TEMPDIR/tmp.$$.collected.tmp
  224     rm $TEMPDIR/tmp.$$.ping_$PARTNER
  225 }
  226 
  227 #---------------------------------------------------------
  228 # ARPing -  FQDN / NumPing / PacketLoss% WARN / PacketLoss% ALERT
  229 #---------------------------------------------------------
  230 ARPing () {
  231     MossheLog "ARPing $1"
  232     typeset -i ALERTPERC WARNPERC VALUE WAIT
  233     PARTNER=$1
  234     NUMPING=$2
  235     WARNPERC=$3
  236     ALERTPERC=$4
  237     
  238     STATUS="UNDEF"
  239     if [ -z which arping ]; then
  240         echo "${DATIM};$MYGROUP;$MYNAME;arping;$STATUS;99;arping not installed" >> $TEMPDIR/tmp.$$.collected.tmp                
  241     exit 0
  242     fi
  243     
  244     VALUE=`arping -c $NUMPING -w $NETWAIT $PARTNER | fgrep Received | cut -d " " -f 2`
  245 
  246     STATUS="OK"
  247     MESSAGE="Ignorable ARP packet loss $MYGROUP: $MYNAME to $PARTNER"
  248     
  249     if [ "$VALUE" -gt "$ALERTPERC" ]; then
  250         STATUS="ALERT"
  251     MESSAGE="Too much packet loss from $MYGROUP: $MYNAME to $PARTNER : ${VALUE} von ${NUMPING}"
  252     elif [ "$VALUE" -gt "$WARNPERC" ]; then 
  253         STATUS="WARN"
  254     MESSAGE="High packet loss from $MYGROUP: $MYNAME to $PARTNER : ${VALUE} von ${NUMPING}"
  255     fi
  256 
  257     echo "${DATIM};$MYGROUP;$MYNAME;arping_$PARTNER;$STATUS;$VALUE;$MESSAGE : ${VALUE} von ${NUMPING}" >> $TEMPDIR/tmp.$$.collected.tmp
  258 }
  259 
  260 #---------------------------------------------------------
  261 # TCPing   SERVER  PORT
  262 #---------------------------------------------------------
  263 # needs NETCAT command for service check
  264 
  265 TCPing () {
  266     MossheLog "TCPing $1 : $2"
  267     SRV=$1
  268     PORT=$2
  269 
  270     STATUS="UNDEF"
  271     typeset -i RTNCODE=0
  272 
  273     if [ -z `which nc` ]; then
  274     echo "${DATIM};$MYGROUP;$SRV;TCPing;UNDEF;-1;NC not installed - cannot perform network check"  >> $TEMPDIR/tmp.$$.collected.tmp
  275     else
  276     echo " " | nc -w $NETWAIT -q 1 -i 1 $SRV $PORT > /dev/null
  277     if [ $? -gt 0 ]; then 
  278         STATUS="ALERT";
  279         RTN="Service tcp/$PORT unreachable"
  280         RTNCODE=3
  281     else
  282         STATUS="OK"
  283         RTN="Service tcp/$PORT answers"
  284         RTNCODE=1
  285        fi
  286        echo "${DATIM};$MYGROUP;$MYNAME;TCPing_${SRV}_${PORT};$STATUS;$RTNCODE;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  287    fi
  288 }
  289 
  290 
  291 
  292 #---------------------------------------------------------
  293 # DNSquery   server  fqdn  querytype
  294 #---------------------------------------------------------
  295 # needs DIG command for DNS lookups
  296 
  297 DNSquery () {
  298     MossheLog "DNSquery @ $1  $2 $3"
  299     typeset -i RTNCODE
  300     DNSSERVER=$1
  301     FQDN=$2
  302     QTYPE=$3
  303 
  304     STATUS="ALERT"
  305     if [ -z `which dig` ]; then
  306     echo "${DATIM};$MYGROUP;$MYNAME;dns (general);UNDEF;-1;DIG not installed - cannot perform DNS check"  >> $TEMPDIR/tmp.$$.collected.tmp
  307     else
  308     RTN=`dig +short @$DNSSERVER $FQDN $QTYPE | tail -n 1`
  309     RTNCODE=`echo $RTN | wc -c | sed -e "s/ //g"`
  310     if [ "$RTNCODE" -gt 7 ]; then 
  311         STATUS="OK";
  312     else
  313         RTN="(not resolved)"
  314         RTNCODE=$(( RTNCODE + 100 ))
  315     fi
  316     echo "${DATIM};$MYGROUP;${MYNAME};DNSquery_${FQDN}_${QTYPE}_at_${DNSSERVER};$STATUS;$RTNCODE;resolves to: $RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  317     fi  
  318 }
  319 
  320 #---------------------------------------------------------
  321 # DNSmatch   server  fqdn  querytype  match
  322 #---------------------------------------------------------
  323 # needs DIG command for DNS lookups
  324 
  325 DNSmatch () {
  326     MossheLog "DNSmatch @$1 $2 $3 = $4"
  327     typeset -i RTNCODE
  328     DNSSERVER=$1
  329     FQDN=$2
  330     QTYPE=$3
  331     EXPECTED=$4
  332 
  333     STATUS="ALERT"
  334     if [ -z `which dig` ]; then
  335     echo "${DATIM};$MYGROUP;$DNSSERVER;dns (general);UNDEF;-1;DIG not installed - cannot perform DNS check"  >> $TEMPDIR/tmp.$$.collected.tmp
  336     else
  337     RTN=`dig +short +tries=5 @$DNSSERVER $FQDN $QTYPE | tail -n 1 | sed -e 's/</&lt;/g'`
  338     RTNCODE=`echo "$RTN" | wc -c | sed -e "s/ //g"`
  339     if [ "$RTN" = "$EXPECTED" ]; then
  340         STATUS="OK";
  341     elif [ "$RTNCODE" -gt 7 ]; then 
  342         STATUS="WARN";
  343         RTNCODE=$(( RTNCODE + 100 ))
  344     else
  345         RTN="(not resolved)"
  346         RTNCODE=$(( RTNCODE + 200 ))
  347     fi
  348     echo "${DATIM};$MYGROUP;$MYNAME;DNSmatch_${FQDN}_${QTYPE}_at_${DNSSERVER};$STATUS;$RTNCODE;resolves to: $RTN , expected: $EXPECTED"  >> $TEMPDIR/tmp.$$.collected.tmp
  349     fi  
  350 }
  351 
  352 #---------------------------------------------------------
  353 # HTTPheader   URL (http/https)
  354 #---------------------------------------------------------
  355 # needs LYNX command for web access
  356 # set lynx.cfg to FORCE_SSL_PROMPT:YES
  357 
  358 HTTPheader () {
  359     MossheLog "HTTPheader $1"
  360     URL=$1
  361 
  362     typeset -i RTNCODE=0
  363     SRV=`echo $URL | cut -d "/" -f 3 | cut -d ":" -f 1`
  364 
  365     if [ -z `which lynx` ]; then
  366     echo "${DATIM};$MYGROUP;$MYNAME;HTTPheader;UNDEF;-1;LYNX not installed - cannot perform HTTP check"  >> $TEMPDIR/tmp.$$.collected.tmp
  367     else    
  368     RTN=`lynx -connect_timeout=30 -dump -head $URL 2>/dev/null| head --lines=1 | sed -e 's/</&lt;/g'`
  369     RTNCODE=`echo $RTN | cut -d " " -f 2`
  370 
  371     if [ -z "$RTN" ]; then STATUS="ALERT"; RTN="Service unreachable"; RTNCODE=666
  372     elif [ "$RTNCODE" -ge 300 ]; then STATUS="WARN"
  373     elif [ "$RTNCODE" -ge 200 ]; then STATUS="OK"
  374     else STATUS="UNDEF"
  375     fi
  376     echo "${DATIM};$MYGROUP;$MYNAME;HTTPheader_$SRV;$STATUS;$RTNCODE;$URL - $RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  377     fi
  378 }
  379 
  380 #---------------------------------------------------------
  381 # HTTPheadermatch   RETURNCODE  URL (http/https)   
  382 #---------------------------------------------------------
  383 # needs LYNX command for web access
  384 # set lynx.cfg to FORCE_SSL_PROMPT:YES
  385 
  386 HTTPheadermatch () {
  387     MossheLog "HTTPheadermatch $2 = $1"
  388     EXPECTED=$1
  389     URL=$2
  390 
  391     typeset -i RTNCODE=0
  392     SRV=`echo $URL | cut -d "/" -f 3 | cut -d ":" -f 1`
  393 
  394     if [ -z `which lynx` ]; then
  395     echo "${DATIM};$MYGROUP;$MYNAME;HTTPheader;UNDEF;-1;LYNX not installed - cannot perform HTTP check"  >> $TEMPDIR/tmp.$$.collected.tmp
  396     else
  397     RTN=`lynx -connect_timeout=30 -dump -head $URL 2>/dev/null | head --lines=1 | sed -e 's/</&lt;/g'`
  398     RTNCODE=`echo $RTN | cut -d " " -f 2`
  399 
  400     if [ -z "$RTN" ]; then STATUS="ALERT"; RTN="Service unreachable"; RTNCODE=666
  401     elif [ "$RTNCODE" = "$EXPECTED" ]; then STATUS="OK"
  402     elif [ "$RTNCODE" -ge 400 ]; then STATUS="ALERT"
  403     elif [ "$RTNCODE" -ge 200 ]; then STATUS="WARN"
  404     else STATUS="UNDEF"
  405     fi
  406     echo "${DATIM};$MYGROUP;$MYNAME;HTTPheadermatch_${SRV}_$EXPECTED;$STATUS;$RTNCODE;$URL - $RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  407     fi
  408 }
  409 
  410 #---------------------------------------------------------
  411 # HTTPcontentmatch   URL  match
  412 #---------------------------------------------------------
  413 # needs NETCAT command for service check
  414 
  415 HTTPcontentmatch () {
  416     MossheLog "HTTPcontentmatch $1 = $2"
  417     URL=$1
  418     MATCH=$2
  419 
  420     SRV=`echo $URL | cut -d "/" -f 3 | cut -d ":" -f 1`
  421     PORT=`echo $URL | cut -d "/" -f 3 | cut -d ":" -f 2`
  422     if [ "$SRV" = "$PORT" ]; then PORT=80; fi
  423 
  424     if [ -z `which wget` ]; then
  425     echo "${DATIM};$MYGROUP;$MYNAME;HTTPcontentmatch;UNDEF;-1;WGET not installed - cannot perform HTTP check"  >> $TEMPDIR/tmp.$$.collected.tmp
  426     else
  427     wget -q --timeout=30 --tries=1 -O $TEMPDIR/tmp.$$.HTTPcontent_${SRV}.rtn "$URL"
  428     fgrep "$MATCH" $TEMPDIR/tmp.$$.HTTPcontent_$SRV.rtn >  $TEMPDIR/tmp.$$.HTTPcontent_${SRV}.match
  429 
  430     if [ -s $TEMPDIR/tmp.$$.HTTPcontent_${SRV}.match ]; then RTNCODE=200; STATUS="OK"; MSG="$MATCH found"
  431     elif [ -s $TEMPDIR/tmp.$$.HTTPcontent_$SRV.rtn ]; then RTNCODE=0; STATUS="WARN"; MSG="$MATCH missing"
  432     else RTNCODE=400; STATUS="ALERT"; MSG="Service unreachable"
  433     fi
  434     echo "${DATIM};$MYGROUP;$MYNAME;HTTPcontentmatch_$SRV;$STATUS;$RTNCODE;$URL - $MSG"  >> $TEMPDIR/tmp.$$.collected.tmp
  435     rm $TEMPDIR/tmp.$$.HTTPcontent_${SRV}.*
  436     fi
  437 }
  438 
  439 #---------------------------------------------------------
  440 # FTPcheck  SERVER
  441 #---------------------------------------------------------
  442 # needs NETCAT command for service check
  443 
  444 FTPcheck () {
  445     MossheLog "FTPcheck $1"
  446     SRV=$1
  447 
  448     STATUS="UNDEF"
  449     typeset -i RTNCODE=0
  450 
  451     if [ -z `which netcat` ]; then
  452     echo "${DATIM};$MYGROUP;$MYNAME;FTPcheck;UNDEF;-1;NC not installed - cannot perform network check"  >> $TEMPDIR/tmp.$$.collected.tmp
  453     else
  454     RTN=`echo "quit" | nc -w $NETWAIT -q 1 -i 1 $SRV 21 | head -n 1`
  455     if [ $? -gt 0 ]; 
  456          then STATUS="ALERT";
  457     else
  458             typeset -i RTNCODE=`echo $RTN | head -c 3`
  459         if [ -z "$RTN" ]; then STATUS="ALERT"; RTN="Service unreachable"
  460         elif [ "$RTNCODE" -ge 500 ]; then STATUS="ALERT"; 
  461         elif [ "$RTNCODE" -ge 400 ]; then STATUS="WARN"; 
  462         elif [ "$RTNCODE" -ge 200 ]; then STATUS="OK"; fi
  463        fi
  464        echo "${DATIM};$MYGROUP;$MYNAME;FTPcheck_$SRV;$STATUS;$RTNCODE;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  465    fi
  466 }
  467 
  468 #---------------------------------------------------------
  469 # SSHcheck  SERVER PORT
  470 #---------------------------------------------------------
  471 # needs NETCAT command for service check
  472 
  473 SSHcheck () {
  474     MossheLog "SSHcheck $1 : 2"
  475     SRV="$1"
  476     PORT="$2"
  477     if [ -z "$PORT" ]; then PORT="22"; fi
  478 
  479     STATUS="UNDEF"
  480     typeset -i RTNCODE=0
  481 
  482     if [ -z `which netcat` ]; then
  483     echo "${DATIM};$MYGROUP;$MYNAME;SSHcheck;UNDEF;-1;NC not installed - cannot perform SSH check"  >> $TEMPDIR/tmp.$$.collected.tmp
  484     else
  485     FULLRTN=`echo " " | nc -w $NETWAIT -q 1 -i 1 $SRV $PORT | head -n 1`
  486     RTN=`echo "$FULLRTN" | head -c 4`
  487     if [ "$RTN" = "SSH-" ]; then 
  488         STATUS="OK";
  489         RTNCODE=1;
  490         RTN="SSH running: $FULLRTN"
  491     elif [ -z "$RTN" ]; then
  492         STATUS="ALERT";
  493         RTNCODE=3;
  494         RTN="No response from SSH: $FULLRTN"
  495     else
  496         STATUS="WARN";
  497         RTNCODE=2;
  498         RTN="Strange response from SSH: $FULLRTN"
  499        fi
  500        echo "${DATIM};$MYGROUP;$MYNAME;SSHcheck_$SRV;$STATUS;$RTNCODE;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  501    fi
  502 }
  503 
  504 #---------------------------------------------------------
  505 # POP3check  SERVER
  506 #---------------------------------------------------------
  507 # needs NETCAT command for service check
  508 
  509 POP3check () {
  510     MossheLog "POP3check $1"
  511     SRV=$1
  512 
  513     STATUS="ALERT"
  514     typeset -i RTNCODE=0
  515 
  516     if [ -z `which netcat` ]; then
  517     echo "${DATIM};$MYGROUP;$MYNAME;POP3check;UNDEF;-1;NC not installed - cannot perform network check"  >> $TEMPDIR/tmp.$$.collected.tmp
  518     else    
  519     RTN=`echo "quit" | nc -w $NETWAIT -q 1 -i 1 $SRV 110 | head --bytes=3`
  520 
  521     if [ $? -gt 0 ]; then 
  522         STATUS="ALERT"
  523         WERT=3
  524     elif [ "$RTN" == "+OK" ]; then 
  525         STATUS="OK"
  526         WERT="0"; 
  527     else 
  528         STATUS="WARN"
  529         WERT="1"
  530     fi
  531 
  532        echo "${DATIM};$MYGROUP;$MYNAME;POP3check_$SRV;$STATUS;$RTNCODE;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  533    fi
  534 }
  535 
  536 #---------------------------------------------------------
  537 # IMAPcheck  SERVER
  538 #---------------------------------------------------------
  539 # needs NETCAT command for service check
  540 
  541 IMAPcheck () {
  542     MossheLog "IMAPcheck $1"
  543     SRV=$1
  544 
  545     STATUS="ALERT"
  546     typeset -i RTNCODE=0
  547 
  548     if [ -z `which netcat` ]; then
  549     echo "${DATIM};$MYGROUP;$MYNAME;IMAPcheck;UNDEF;-1;NC not installed - cannot perform network check"  >> $TEMPDIR/tmp.$$.collected.tmp
  550     else    
  551     RTN=`echo "IC Logout" | nc -w $NETWAIT -q 1 -i 1 $SRV 143 | head --bytes=4`
  552 
  553     if [ $? -gt 0 ]; then 
  554         STATUS="ALERT"
  555         WERT=3
  556     elif [ "$RTN" == "* OK" ]; then 
  557         STATUS="OK"
  558         WERT="0"; 
  559     else 
  560         STATUS="WARN"
  561         WERT="1"
  562     fi
  563 
  564        echo "${DATIM};$MYGROUP;$MYNAME;IMAPcheck_$SRV;$STATUS;$WERT;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  565    fi
  566 }
  567 
  568 #---------------------------------------------------------
  569 # SMTPcheck  SERVER
  570 #---------------------------------------------------------
  571 # needs NETCAT command for service check
  572 
  573 SMTPcheck () {
  574     MossheLog "SMTPcheck $1"
  575     SRV=$1
  576 
  577     STATUS="UNDEF"
  578     typeset -i RTNCODE=0
  579 
  580     if [ -z `which netcat` ]; then
  581     echo "${DATIM};$MYGROUP;$MYNAME;SMTPcheck;UNDEF;-1;NC not installed - cannot perform network check"  >> $TEMPDIR/tmp.$$.collected.tmp
  582     else
  583     RTN=`echo "quit" | nc -w $NETWAIT -q 1 -i 1 $SRV 25 | head -n 1`
  584     if [ $? -gt 0 ]; 
  585          then STATUS="DOWN";
  586     else
  587             typeset -i RTNCODE=`echo $RTN | cut -d " " -f 1`
  588         if [ -z "$RTN" ]; then STATUS="ALERT"; RTN="Service unreachable"
  589         elif [ "$RTNCODE" -ge 500 ]; then STATUS="ALERT"; 
  590         elif [ "$RTNCODE" -ge 400 ]; then STATUS="WARN"; 
  591         elif [ "$RTNCODE" -ge 200 ]; then STATUS="OK"; fi
  592        fi
  593        echo "${DATIM};$MYGROUP;$MYNAME;SMTPcheck_$SRV;$STATUS;$RTNCODE;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  594    fi
  595 }
  596 
  597 #---------------------------------------------------------
  598 # RBLcheckFQDN  MailserverFQDN  RBLserver
  599 #---------------------------------------------------------
  600 # needs DIG command for service check
  601 
  602 RBLcheckFQDN () {
  603     MossheLog "RBLcheckFQDN $1 @ $2"
  604     SRV=$1
  605     RBL=$2
  606 
  607     STATUS="UNDEF"
  608     RTN="Huh? Check RBLcheckFQDN routine - something's wrong here"
  609     typeset -i RTNCODE=33
  610 
  611     if [ -z `which dig` ]; then
  612     echo "${DATIM};$MYGROUP;$MYNAME;RBLcheckFQDN;UNDEF;-1;DIG not installed - cannot perform network check"  >> $TEMPDIR/tmp.$$.collected.tmp
  613     else
  614     IP=`dig +short $SRV | grep -e "^[0-9.]\+$" | head -n 1`
  615     set `IFS="."; echo $IP`
  616     REVIP=`echo $4.$3.$2.$1`
  617     
  618     RESOLVED=`dig ${REVIP}.${RBL} +short | grep -e "^127.0.0."`
  619     RTNCODE=$?
  620     if [ "$RTNCODE" -gt 1 ]; then
  621         STATUS="WARN"
  622         RTN="Blacklist $RBL somehow wrong (IP=$IP / revIP=$REVIP) - ExitCode $RTNCODE"
  623     elif [ -z "$RESOLVED" ]; then
  624         STATUS="OK"; 
  625         RTN="$IP not blacklisted"
  626         RTNCODE=0
  627     else 
  628         STATUS="ALERT"; 
  629         RTN="$RBL blacklists $IP : $RESOLVED"
  630         RTNCODE=22
  631        fi
  632        echo "${DATIM};$MYGROUP;$MYNAME;RBLcheckFQDN_${RBL}_at_$SRV;$STATUS;$RTNCODE;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  633    fi
  634 }
  635 
  636 #---------------------------------------------------------
  637 # RBLcheckIP  MailserverIP  RBLserver
  638 #---------------------------------------------------------
  639 # needs DIG command for service check
  640 
  641 RBLcheckIP () {
  642     MossheLog "RBLcheckIP $1 @ $2"
  643     SRV=$1
  644     RBL=$2
  645 
  646     STATUS="UNDEF"
  647     RTN="Huh? Check RBLcheck routine - something's wrong here"
  648     typeset -i RTNCODE=33
  649 
  650     if [ -z `which dig` ]; then
  651     echo "${DATIM};$MYGROUP;$MYNAME;RBLcheckIP;UNDEF;-1;DIG not installed - cannot perform network check"  >> $TEMPDIR/tmp.$$.collected.tmp
  652     else
  653     IP=$SRV
  654     set `IFS="."; echo $IP`
  655     REVIP=`echo $4.$3.$2.$1`
  656     
  657     RESOLVED=`dig ${REVIP}.${RBL} +short | grep -e "^127.0.0."`
  658     RTNCODE=$?
  659     if [ "$RTNCODE" -gt 1 ]; then
  660         STATUS="WARN"
  661         RTN="Blacklist $RBL somehow wrong (IP=$IP / revIP=$REVIP) - ExitCode $RTNCODE"
  662     elif [ -z "$RESOLVED" ]; then
  663         STATUS="OK"; 
  664         RTN="$IP not blacklisted"
  665         RTNCODE=0
  666     else 
  667         STATUS="ALERT"; 
  668         RTN="$RBL blacklists $IP : $RESOLVED"
  669         RTNCODE=22
  670        fi
  671        echo "${DATIM};$MYGROUP;$MYNAME;RBLcheckIP_${RBL}_at_$SRV;$STATUS;$RTNCODE;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  672    fi
  673 }
  674 
  675 #---------------------------------------------------------
  676 # SAMBAcheck  SERVER
  677 #---------------------------------------------------------
  678 # needs SMBCLIENT command for service check
  679 
  680 SAMBAcheck () {
  681     MossheLog "SAMBAcheck $1"
  682     IP=$1
  683 
  684     STATUS="UNDEF"
  685     typeset -i RTNCODE=0
  686 
  687     if [ -z `which smbclient` ]; then
  688     echo "${DATIM};$MYGROUP;$MYNAME;SAMBAcheck;UNDEF;-1;SMBCLIENT not installed - cannot perform network check"  >> $TEMPDIR/tmp.$$.collected.tmp
  689     else
  690     RTN=`smbclient -N -L $IP 2>/dev/null`
  691     RTNCODE=$?
  692     if [ "$RTNCODE" -gt 0 ]; then 
  693         STATUS="ALERT";
  694         RTN="No SMB/CIFS services on $IP"
  695     else
  696         STATUS="OK";
  697         RTN="SMB/CIFS services running on $IP"
  698     fi
  699        echo "${DATIM};$MYGROUP;$MYNAME;SAMBAcheck_$IP;$STATUS;$RTNCODE;$RTN"  >> $TEMPDIR/tmp.$$.collected.tmp
  700    fi
  701 }
  702 
  703 #############################################################################