2013-09-01 機器のサービス監視スクリプト

いままで新しい機器を構築した時は、zabbix等の監視システムがあれば良いが、
監視システムが無い場合は、監視スクリプトを都度作成してきた
もういいかげんに、記録として残して使いまわそうと思う

動作には、netcat が必要です
RedHat 系は、yum install nc
Debian 系は、apt-get install netcat
でインストールしてください

--- host-check.sh ---
#!/bin/bash
###
### yum install nc
###

MAILCOUNT="3"
MAILTO="aaabbb@ddd.com"
MAILSUB_OK="["`hostname -s`"]host check OK"
MAILSUB_NG="["`hostname -s`"]host check NG"

CHECKHOSTS="### CHECK HOST LIST #############
############### IP address で記載願います ###
173.194.38.127:80
www.google.co.jp:80
133.24.255.146:80
133.24.255.153:80
133.24.255.161:80
###################################"

PG_MAIL="mail"


echo -n "" >/tmp/__"${0##*/}"_diff.tmp
touch /tmp/__"${0##*/}"_last-time.tmp
touch /tmp/__"${0##*/}"_this-time.tmp
###debug
###echo "${CHECKHOSTS}"
###echo "============="

CHECKHOSTS=`echo "${CHECKHOSTS}" \
            | sed 's/#.*//g' \
            | sed 's/[[:space:]]//g' \
            | sed '/^$/d' `

###debug
###echo "${CHECKHOSTS}"

echo -n "" >/tmp/__"${0##*/}"_this-time.tmp
echo -n "" >/tmp/__"${0##*/}"_mail.txt

  for LINE in ${CHECKHOSTS}
  do
    TARGETHOST=`echo "${LINE}" | sed 's/:[0-9][0-9]*$//g'`
    TARGETPORT=`echo "${LINE}" | sed 's/.*:\([0-9][0-9]*\)$/\\1/g'`
    ###echo "TARGETHOST= ${TARGETHOST}"
    ###echo "  TARGETPORT= ${TARGETPORT}"
    CHECKLOG=`nc -z -v -w 2 "${TARGETHOST}" "${TARGETPORT}" 2>&1 ` ; RESULT="${?}"
      if [ "${RESULT}" = "0" ] ; then
        ###echo "= OK = ${TARGETHOST} :${TARGETPORT}"
        echo "= OK = ${TARGETHOST} :${TARGETPORT}" >>/tmp/__"${0##*/}"_mail.txt
      else
        ###echo "= NG = ${TARGETHOST} : "` echo "${CHECKLOG}" | head -1 `
        echo "= NG = ${TARGETHOST} : "` echo "${CHECKLOG}" | head -1 ` \
          >>/tmp/__"${0##*/}"_mail.txt
        echo "${TARGETHOST}:${TARGETPORT}" \
          >>/tmp/__"${0##*/}"_this-time.tmp
      fi
  done

diff /tmp/__"${0##*/}"_last-time.tmp /tmp/__"${0##*/}"_this-time.tmp \
  >/tmp/__"${0##*/}"_diff.tmp
CHECKSTR=`cat /tmp/__"${0##*/}"_diff.tmp | sed '/^$/d' \
          ``cat /tmp/__"${0##*/}"_last-time.tmp \
            | sed '/^$/d'`

cp /tmp/__"${0##*/}"_this-time.tmp /tmp/__"${0##*/}"_last-time.tmp
rm /tmp/__"${0##*/}"_this-time.tmp


  if [ "${CHECKSTR}" = "" ] ; then
    echo "0" >/tmp/__"${0##*/}"_mail-count.txt
    exit 0
  fi


RECOVER=`cat /tmp/__"${0##*/}"_diff.tmp | grep '<' | wc -l`
NEWALERT=`cat /tmp/__"${0##*/}"_diff.tmp | grep '>' | wc -l`

###echo "==================================="
###echo "${RECOVER}"
###echo "==================================="
###echo "${NEWALERT}"
###echo "==================================="


  if [ ! "${NEWALERT}" = "0" ] ; then
    ###debug
    rm /tmp/__"${0##*/}"_mail_tmp.txt 2>/dev/null
    touch /tmp/__"${0##*/}"_mail_tmp.txt
    cat /tmp/__"${0##*/}"_mail.txt >/tmp/__"${0##*/}"_mail_tmp.txt
    echo "### new alert !!! "`date '+%Y-%m-%d %H:%M:%S'`" ###" \
                                                  >/tmp/__"${0##*/}"_mail.txt
    cat /tmp/__"${0##*/}"_diff.tmp | grep '>'     >>/tmp/__"${0##*/}"_mail.txt
    echo "########"                               >>/tmp/__"${0##*/}"_mail.txt
    echo ""                                       >>/tmp/__"${0##*/}"_mail.txt
    cat /tmp/__"${0##*/}"_mail_tmp.txt            >>/tmp/__"${0##*/}"_mail.txt
    echo "1" >/tmp/__"${0##*/}"_mail-count.txt
    cat /tmp/__"${0##*/}"_mail.txt | ${PG_MAIL} -s "${MAILSUB_NG}" "${MAILTO}"
    exit 0
  fi


  if [ "${NEWALERT}" = "0" ] ; then
    DIFFSTR=`cat /tmp/__"${0##*/}"_diff.tmp`

    if [ "${DIFFSTR}" = "" ] ; then
      touch /tmp/__"${0##*/}"_mail-count.txt
      COUNT=`cat /tmp/__"${0##*/}"_mail-count.txt | tail -1`
      COUNT=`expr 0${COUNT} + 1`
      echo "${COUNT}" >/tmp/__"${0##*/}"_mail-count.txt
      ###debug
      ###echo "==================================="
      ###echo "COUNT=${COUNT}"
      ###echo "==================================="

        if [ "${COUNT}" -gt "${MAILCOUNT}" ] ; then
          exit 0
        fi

      cat /tmp/__"${0##*/}"_mail.txt | ${PG_MAIL} -s "${MAILSUB_NG}" "${MAILTO}"
      exit 0
    ###else
      ###debug
      ###echo "==================================="
      ###echo "COUNT=${COUNT}"
      ###echo "==================================="
      ###cat /tmp/__"${0##*/}"_mail.txt
    fi
    if [ ! "${RECOVER}" = 0 ] ; then
      ###debug
      ###echo ""
      ###echo "### RECOVER ####"
      ###echo ""
      echo ""                                   >>/tmp/__"${0##*/}"_mail.txt
      echo "### RECOVER "`date '+%Y-%m-%d %H:%M:%S'`" ###" \
                                                >>/tmp/__"${0##*/}"_mail.txt
      cat /tmp/__"${0##*/}"_diff.tmp \
      | grep '<' \
      | sed 's/= NG = //g'                      >>/tmp/__"${0##*/}"_mail.txt
      cat /tmp/__"${0##*/}"_mail.txt | ${PG_MAIL} -s "${MAILSUB_OK}" "${MAILTO}"
    fi

  fi

--------