Ajouter une machine dans Nagios Core

Un script interactif pour ajouter une machine Linux dans Nagios core.

#!/bin/bash

# Ajout d"un client sur le serveur Nagios
# Source : https://www.digitalocean.com/community/tutorials/how-to-install-nagios-4-and-monitor-your-servers-on-ubuntu-14-04#install-nagios-4
# Source : https://blog.nicolargo.com/2008/07/exemples-de-check-de-services-nagios.html
GREEN="\033[0;32m"
NC="\033[0m" # No Color

echo "Auteur : Tommy Gingras"
echo "Date : 2017-07-20"
echo "But : Ajouter un client sur le serveur Nagios."
echo "Version : 1.2.0"

read -p "Entrer le nom du nouvel hôte : " newhost
read -p "Entrer l'alias du nouvel hôte : " newalias
read -p "Entrer l'adresse ip du nouvel hôte : " newip

echo "define host {" > /usr/local/nagios/etc/servers/$newhost.cfg
echo "        use                             linux-server" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "        host_name                       $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "        alias                           $newalias" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "        address                         $newip" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "        max_check_attempts              5" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "        check_period                    24x7" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "        notification_interval           30" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "        notification_period             24x7" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "        check_interval                  0.05" >> /usr/local/nagios/etc/servers/$newhost.cfg
echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg

echo
echo -e "${GREEN}Les services de base${NC}"
echo
read -p "Voulez-vous vérifier le ping (y/n)[n] " q
if [ "$q" == "y" ]
then
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description PING" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_ping!100.0,20%!500.0,60%" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

read -p "Voulez-vous vérifier le service SSH (y/n)[n] " q
if [ "$q" == "y" ]
then
    read -p "Quel est le port SSH [22] " portssh
    if [ "$portssh" != "" ]
    then
        echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
        echo "    command_name check_ssh_$portssh_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
        echo "    command_line \$USER1\$/check_ssh -p $portssh" >> /usr/local/nagios/etc/servers/$newhost.cfg
        echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
        variable="_$portssh_$newhost"
    fi
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description SSH" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_ssh$variable" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

read -p "Voulez-vous vérifier le service users (y/n)[n] " q
if [ "$q" == "y" ]
then
    echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_name check_users_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_line \$USER1\$/check_users -w1 -c2" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description USERS" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_users_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

read -p "Voulez-vous vérifier la charge du système (y/n)[n] " q
if [ "$q" == "y" ]
then
    echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_name check_load_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_line \$USER1\$/check_load -w 15,10,5 -c 30,25,20" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description LOAD" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_load_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

read -p "Voulez-vous vérifier la partition système / (y/n)[n] " q
if [ "$q" == "y" ]
then
    echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_name check_disk_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_line \$USER1\$/check_disk -w 20% -c 10% -p /" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description ROOT" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_disk_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

read -p "Voulez-vous vérifier le swap (y/n)[n] " q
if [ "$q" == "y" ]
then
    echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_name check_swap_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_line \$USER1\$/check_swap -a -w 50% -c 10%" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description SWAP" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_swap_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

read -p "Voulez-vous vérifier tous les processus (y/n)[n] " q
if [ "$q" == "y" ]
then
    echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_name check_procs_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_line \$USER1\$/check_procs -w 150 -c 200" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description PROCESS" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_procs_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

echo 
echo -e "${GREEN}Serveur Web${NC}"
echo 

read -p "Voulez-vous vérifier le service web HTTP (y/n)[n] " q

if [ "$q" == "y" ]
then
    read -p "Quel est le port d'écoute du serveur web [80]" port
    if [ "$port" != "" ]
    then
        echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
        echo "    command_name check_http_$port_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
        echo "    command_line \$USER1\$/check_http -I \$HOSTADDRESS\$ \$ARG1\$-p $port" >> /usr/local/nagios/etc/servers/$newhost.cfg
        echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg    
    fi
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    if [ "$port" == "" ]
    then
        echo "    service_description HTTP" >> /usr/local/nagios/etc/servers/$newhost.cfg
        echo "    check_command check_http" >> /usr/local/nagios/etc/servers/$newhost.cfg
    else
        echo "    service_description HTTP_$port" >> /usr/local/nagios/etc/servers/$newhost.cfg
        echo "    check_command check_http_$port_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    fi    
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

read -p "Voulez-vous vérifier le service web HTTPS (y/n)[n]" q
if [ "$q" == "y" ]
then
    echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_name check_https_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_line \$USER1\$/check_http -S -I \$HOSTADDRESS\$ \$ARG1\$" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description HTTPS" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_https_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

echo
echo -e "${GREEN}Serveur base de données${NC}"
echo

read -p "Voulez-vous vérifier le service MongoDB (y/n)[n]" q
if [ "$q" == "y" ]
then
    echo "define command{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_name check_mongodb_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    command_line \$USER1\$/check_tcp -H \$HOSTADDRESS\$ -p 27017" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "define service{" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    use generic-service" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    host_name $newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    service_description MONGODB" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "    check_command check_mongodb_$newhost" >> /usr/local/nagios/etc/servers/$newhost.cfg
    echo "}" >> /usr/local/nagios/etc/servers/$newhost.cfg
    q=""
fi

echo -e "${GREEN}Veuillez attendre que le service redémarre pour appliquer les changements.${NC}"
service nagios stop
service nagios start

echo -e "${GREEN}Le nouvel hôte $newhost a été ajouté.${NC}"

 

Laisser un commentaire