#!/bin/bash # # ---------------------------------------------------------------------- # # Description: This script can check continuously # if certain server daemons are running. If not # they will be restarted. A message will be mailed # to the specified adress as defined in $MAILME. # If $MAILME is empty, then mail will be sent to # root. $SLEEP will set the frequency of the checks. # The script will test for certain distributions, # set up logging, and checks if services do not # conflict with (x)inetd while restarting. # # Note: Script output is in DUTCH LANGUAGE! # Script logic is straightforward so you could # translate yourself. # # Author: Maccus # # Initial version: 080324-01 # # Update: 110722-01 # - Changed redhat and debian # startcommands from "/etc/initdir/service start" # to "service service start" for better # compatibility. # # TODO: Support for more servers (DNS, Apache 1). # # Requires: netstat # # This script has been written and tested on # Ubuntu Linux 10.04 and Debian Testing (Wheezy) # # ---------------------------------------------------------------------- # # Usage: Place the script in "/usr/local/bin", do a # "chmod +x /usr/local/bin/net-server-monitor" as root, # and run "net-server-monitor apache2 sshd .. &" from # the command line, or put such a line in one of your # startup scripts. Currently supported servers are: # apache2 proftpd samba sshd xinetd # # ---------------------------------------------------------------------- # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # ---------------------------------------------------------------------- # # Uncomment for debugging # # set -x # Start script echo -e "\n\033[1;33mSTATUS\033[0m: Script \"$(basename $0)\" is gestart...\n" # Variabelen # Frequentie in seconden SLEEP="10" # Email-adres voor waarschuwingen. Indien # leeg gaan alle waarschuwingen naar root. MAILME="" # Feedback VERBOSE="yes" # Niet aanpassen! SERVERS="$*" # Ondersteunde servers. Niet aanpassen! SUPPD_SRV="apache2 proftpd samba sshd xinetd" # Alleen voor root check_root () { if [ "${UID}" != "0" ]; then echo -e "Dit script kan alleen door root worden uitgevoerd! Script wordt nu afgesloten...\n" sleep 2 exit 1 fi } # Afhankelijkheden check_dependencies () { for FILE in $* ; do if [ ! "$(which ${FILE} 2> /dev/null)" ]; then echo -e "Bestand \"${FILE}\" niet gevonden! Installeer het pakket waarin \"${FILE}\" voorkomt en probeer het opnieuw. Script wordt nu afgesloten...\n" sleep 2 exit 1 fi done } # Script argumenten check_args () { if [ -z "$*" ]; then echo -e "\033[1;31mFOUT\033[0m: Je moet een of meerdere servers als argument opgeven..." echo -e "Bijvoorbeeld: $0 ${SUPPD_SRV}" echo -e "Dit script wordt afgebroken...\n" sleep 5 ; exit 1 elif [ -n "$*" ]; then for SRV in $* ;do if ! ( echo "${SUPPD_SRV}" | grep -q "${SRV}" ); then echo -e "\nServer \"${SRV}\" wordt niet door dit script ondersteund." echo -e "Alleen servers \"${SUPPD_SRV}\" worden momenteel ondersteund." echo -e "\"${SRV}\" wordt verwijderd uit de argumenten lijst.\n" remove_arg fi done fi } # Check distro. Alleen de meest gangbare distributies. check_distro () { if [ -f "/etc/debian_version" -o -f "/etc/lsb-release" ]; then # Debian en Ubuntu INITDIR="/etc/init.d" DISTRO="debian" elif [ -f "/etc/redhat-release" -o -f "/etc/redhat_version" -o -f "/etc/fedora-release" -o -f "/etc/mandriva-release" ]; then # Redhat, Fedora en Mandriva INITDIR="/etc/rc.d/init.d" DISTRO="redhat" elif [ -f "/etc/SuSE-release" -o -f "/etc/novell-release" -o -f "/etc/sles-release" ]; then # SuSE INITDIR="/etc/init.d" DISTRO="suse" else echo -e "Dit script draait alleen op Debian, Fedora, Mandriva, Redhat, SuSE, of Ubuntu." echo -e "Voor een andere distributie kun je het script zelf aanpassen :-)" echo -e "Script wordt afgebroken...\n" sleep 5 exit 1 fi } # Ongeldige argumenten verwijderen remove_arg () { SERVERS="$(echo "${SERVERS}" | sed "s|${SRV}||")" } # Eeuwige iteratie... monit_servers () { while [ "x" = "x" ]; do for SRV in ${SERVERS} ; do # Feedback [ "${VERBOSE}" = "yes" ] && echo "Controleren of ${SRV} nog draait..." # Voor het gemak DATE="$(date)" HOST="$(hostname -s)" # Commando's per distro. Bij veranderen van ${SUPP_SRV} moeten onderstaande case statements ook aangepast worden! case ${SRV} in apache2) if [ "${DISTRO}" = "redhat" ]; then PROCESS="httpd" RESTARTCMD="service httpd start" elif [ "${DISTRO}" = "suse" ]; then PROCESS="httpd2-prefork" RESTARTCMD="${INITDIR}/apache2 start" elif [ "${DISTRO}" = "debian" -o "${DISTRO}" = "ubuntu" ]; then PROCESS="apache2" RESTARTCMD="service apache2 start" fi # Netstat SERVICE="http" ;; proftpd) if [ "${DISTRO}" = "redhat" ]; then PROCESS="proftpd" RESTARTCMD="service proftpd start" elif [ "${DISTRO}" = "suse" -o "${DISTRO}" = "debian" ]; then # Geen Proftpd voor SuSE, jammer dan. # Proftpd weghalen uit argumentenlijst remove_arg elif [ "${DISTRO}" = "debian" ]; then PROCESS="proftpd" RESTARTCMD="service proftpd start" fi # Netstat SERVICE="ftp" ;; samba) if [ "${DISTRO}" = "redhat" -o "${DISTRO}" = "suse" ]; then PROCESS="smbd" RESTARTCMD="service smb start" elif [ "${DISTRO}" = "debian" ]; then PROCESS="smbd" RESTARTCMD="service smbd start" fi # Netstat SERVICE="netbios" ;; sshd) if [ "${DISTRO}" = "redhat" -o "${DISTRO}" = "suse" ]; then PROCESS="sshd" RESTARTCMD="service sshd start" elif [ "${DISTRO}" = "debian" ]; then PROCESS="sshd" RESTARTCMD="service ssh start" fi # Netstat SERVICE="ssh" ;; xinetd) if [ "${DISTRO}" = "redhat" ]; then PROCESS="xinetd" RESTARTCMD="service xinetd restart" elif [ "${DISTRO}" = "suse" -o "${DISTRO}" = "debian" ]; then PROCESS="xinetd" RESTARTCMD="service xinetd restart" fi ;; esac # Servers die draaien onder (x)inetd niet proberen te herstarten ! # Deze servers worden uit argumentenlijst gehaald if [ "${SRV}" != "xinetd" ]; then if ( netstat -pl --inet | grep "inetd" | grep -q "${SERVICE}" ); then # Feedback [ "${VERBOSE}" = "yes" ] && echo "${SRV} draait onder (x)inetd en wordt overgeslagen..." remove_arg && break fi fi # Check of de server draait if ! ps -e | grep -q "${PROCESS}" ; then if ${RESTARTCMD} ; then # Verstuur notificatie bij sucesvolle herstart echo -e "Server \"${SRV}\" op \"${HOST}\" opnieuw gestart op ${DATE}." | \ mail -s "Server \"${SRV}\" opnieuw gestart!" "${MAILME:=root}" else # Verstuur waarschuwing bij mislukte herstart echo -e "Server \"${SRV}\" op \"${HOST}\" kon niet worden geherstart op ${DATE}. Onderzoek de logbestanden in /var/log !" | \ mail -s "Kritieke Fout! Server \"${SRV}\" kon niet worden geherstart!" "${MAILME:=root}" # Uitgevallen server verwijderen uit argumentenlijst # om te voorkomen dat dit script eeuwig blijft # proberen om de server te herstarten remove_arg fi fi # Niet te snel sleep 1 done # Check wordt uitgevoerd iedere $SLEEP seconden. Standaardwaard is 60 seconden. sleep ${SLEEP:=60} done } # Functies starten check_root check_dependencies netstat check_args "$*" check_distro monit_servers exit 0