Home Linux ServerWare Centreon

#!/bin/bash -e
# 
# checkMonitoring.sh
# 
# Verify if we are monitoring everything on the server
# 
# 
# Author: Jean-Louis Girard
# 

if [[ $# -ne 2 ]]
then
  echo "Usage: $(basename $0) <servers>  </path/to/centreonServiceFile>"
  echo ""
  echo '       <servers> can be a list between double quotes "server1 server2 ..."'
  echo '                 or a file containing a list of servers, one per line.'

  echo ""
  echo '       </path/to/centreonServiceFile> file that contains the output from running the following command on the centron server:  '
  echo '       /usr/share/centreon/www/modules/centreon-clapi/core/centreon -u admin  -p <admin passwd> -o service -a show | awk -F'  "';' '{print \$2" ' " "' " \$4}' | sort"

  exit 1
fi

if [[ -f $1 > /dev/null 2>&1 ]]
then
  SERVERS=$(cat $1)
else
  SERVERS=$1
fi

CENTREON_FILE=$2

if [[ ! -f $CENTREON_FILE ]]
then
  echo $CENTREON_FILE does not exist.
  exit
fi

function checkServer
{
  red='\e[0;31m'
  orange='\e[0;33m'
  normal='\e[0m'

  #the function is called with one server in parameter.
  SERVER_NAME=$1

  # Test if server name is valid
  if ! nslookup $SERVER_NAME > /dev/null 2>&1
  then
    echo -e "$red $SERVER_NAME not found $normal"
  else
    readarray -t installedServices <<<"$(ssh $SERVER_NAME 'ls /etc/init.d/ | egrep "dhcp|haproxy|httpd|mariadb|mysqld|nfsd|postgres|redis|varnish|java" | grep -v avahi')"
    readarray -t enabledServices <<<"$(ssh $SERVER_NAME 'ls /etc/rc3.d/ | grep "^S" | sed "s/^...//" | egrep "dhcp|haproxy|httpd|mariadb|mysqld|nfsd|postgres|redis|varnish|java" | grep -v avahi')"
    readarray -t runningProcess <<<"$(ssh $SERVER_NAME 'ps auxwww | egrep "dhcp|haproxy|httpd|mariadb|mysqld|nfsd|postgres|redis|varnish|java" | grep -v grep | grep -v avahi')"
    readarray -t fsOnServer <<<"$(ssh $SERVER_NAME "df -l -x tmpfs -P | awk '{print \$6}'  | grep -v '^Mounted' | grep -v '^boot\$'")"

    baseServices=('cpu' 'load' 'ping' 'memory' 'eth0' 'sshd')

    # verify if installedServices are active.
    for iS in "${installedServices[[@]]}"
    do
      if ! echo ${enabledServices[[@]]} | grep -qE "(^ | )$iS"
      then
        echo -e "$SERVER_NAME: $orange Service $iS is installed but not enabled in rc3.d. $normal"
      fi
    done

    # Veriffy if base services are monitored
    for s in "${baseServices[[@]]}"
    do
      if ! grep -i $SERVER_NAME $CENTREON_FILE | grep -qi $s
      then
        echo -e "$SERVER_NAME: $red $s is not monitored. $normal"
      fi
    done

    # Verify if local filesystems are monitored
    #for fsOnServer in $(df -l -x tmpfs -P | awk '{print $6}' | grep -v "^Mounted")
    for fs in "${fsOnServer[[@]]}"
    do
      if ! grep -i $SERVER_NAME $CENTREON_FILE | grep -qi "$fs$"
      then
        echo -e  "$SERVER_NAME: $red $fs is not monitored. $normal"
      fi
    done

    # services that contain their name in the running process and init script.
    standardServices=('dhcp' 'haproxy' 'httpd' 'mariadb' 'postgres' 'redis' 'varnish' )

    for s in "${standardServices[[@]]}"
    do
      if echo ${runningProcess[[@]]} ${enabledServices[[@]]} | grep -q $s
      then
        if !  grep -i $SERVER_NAME $CENTREON_FILE | grep -qi $s
        then
          echo -e "$SERVER_NAME: $red $s is not monitored. $normal"
        fi
      fi
    done

    # should we monitor mysqld. ie mysqld is not in the standardServices list because mariadb matches also.
    if echo ${runningProcess[[@]]} ${enabledServices[[@]]} | grep  "mysqld" | grep -qv "maria"
    then
      if !  grep -i $SERVER_NAME $CENTREON_FILE | grep -qiE "(^| )mysqld"
      then
        echo -e "$SERVER_NAME: $red mysqld is not monitored. $normal"
      fi
    fi

    # Should we monitor java?
    if echo ${runningProcess[[@]]} | grep -q "java"
    then
      if ! grep -i $SERVER_NAME $CENTREON_FILE | grep -qi "java"
      then
        echo -e  "$SERVER_NAME: $red java is not monitored. $normal"
      fi
    fi

    # Should we monitor nfsd?
    if echo ${runningProcess[[@]]} | grep -q "nfsd"  || echo ${enabledServices[[@]]} | grep -qE "nfs( |$)"
    then
      if ! grep -i $SERVER_NAME $CENTREON_FILE | grep -qi "nfs"
      then
        echo -e  "$SERVER_NAME: $red nfsd is not monitored. $normal"
      fi
    fi

  fi
}

for oneServer in ${SERVERS[[@]]}
do
  checkServer "$oneServer"
  echo " "
done