Restart de service

De BlaxWiki
Révision datée du 24 mai 2018 à 09:06 par 127.0.0.1 (discussion)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigationAller à la recherche

Ce script vérifie l état de la ram prise par un processus (ici squid) et le redémarre si le seuil est atteint

#!/bin/bash

## CONF
ram_max='80' # seuil d'utilisation max en pourcent
SSH='/opt/applis/openssh/bin/ssh'

## COMMANDE DE REDMERRAGE
redemarre="sed -i 's/icap_enable on/icap_enable off/g' /opt/applis/squid/etc/squid.conf"
redemarre="${redemarre}; date '+%d/%m/%Y - %H:%M:%S' > /root/.autorestart_squid"
redemarre="${redemarre}; /etc/init.d/squid restart"
redemarre="${redemarre}; /etc/init.d/kav4proxy stop"
redemarre="${redemarre}; nohup /etc/init.d/kav4proxy restart >/dev/null 2>&1"
redemarre="${redemarre}; sleep 3;sed -i 's/icap_enable off/icap_enable on/g' /opt/applis/squid/etc/squid.conf"
redemarre="${redemarre}; /etc/init.d/squid reload"

function IP_nom {
        IP="${1}"
        nom='not_found'
        [ "${IP}" = '172.30.100.129' ] && nom='proxy1.intersec.para.real.not'
        [ "${IP}" = '172.30.100.134' ] && nom='proxy1.intersec.parb.real.not'
        [ "${IP}" = '172.30.100.130' ] && nom='proxy2.intersec.para.real.not'
        [ "${IP}" = '172.30.100.135' ] && nom='proxy2.intersec.parb.real.not'
        [ "${IP}" = '172.30.100.131' ] && nom='proxy3.intersec.para.real.not'
        [ "${IP}" = '172.30.100.136' ] && nom='proxy3.intersec.parb.real.not'
        [ "${IP}" = '172.30.100.132' ] && nom='proxy4.intersec.para.real.not'
        [ "${IP}" = '172.30.100.137' ] && nom='proxy4.intersec.parb.real.not'
        [ "${IP}" = '172.30.100.133' ] && nom='proxy5.intersec.para.real.not'
        [ "${IP}" = '172.30.100.138' ] && nom='proxy5.intersec.parb.real.not'
        [ "${IP}" = '172.30.100.145' ] && nom='proxy7.intersec.para.real.not'
        [ "${IP}" = '172.30.100.146' ] && nom='proxy7.intersec.parb.real.not'
        [ "${IP}" = '172.30.100.147' ] && nom='proxy8.intersec.para.real.not'
        [ "${IP}" = '172.30.100.149' ] && nom='proxy8.intersec.parb.real.not'
        echo "${nom}"
}

## POUR CHAQUE PROXY, EXECUTE $redemarre SI RAM>=$ram_max
nb_redemarrage='0'
liste_restart=''
for ip_serveur in '172.30.100.129' '172.30.100.134' '172.30.100.130' '172.30.100.135' '172.30.100.131' '172.30.100.136' '172.30.100.132' '172.30.100.137' '172.30.100.133' '172.30.100.138' '172.30.100.145' '172.30.100.146' '172.30.100.147' '172.30.100.149' ; do
        ram_utilisee="$("${SSH}" "root@${ip_serveur}" 'free | grep Mem | awk "{print (\$3-\$7)/\$2 * 100.0}" | cut -d. -f1')"
        if [ "${ram_utilisee}" -ge "${ram_max}" ];then
                "${SSH}" "root@${ip_serveur}" "${redemarre}"
                nb_redemarrage="$(( ${nb_redemarrage} + 1 ))"
                liste_restart="${liste_restart}$(IP_nom "${ip_serveur}")\n"
        fi
done

if [ "${nb_redemarrage}" -gt '0' ]; then

        ## CLEAR STICKY
        /opt/applis/rancid/bin/clogin -autoenable -noenable -c 'clear sticky database group GROUP1;' -u adm-intersec lb1.intersec.parb.real.not

        ## ENVOIE MAIL
        sed "s/SRVLIST/${liste_restart}/" /opt/agarik/scripts/autorestart_squid_mail.txt > /opt/agarik/scripts/autorestart_squid_mail_tmp.txt
        /usr/sbin/sendmail -f exploitation.real@notaires.fr -t exploitation.real@notaires.fr -s smtp.notaires.fr -v -o message-charset=utf-8 < /opt/agarik/scripts/autorestart_squid_mail_tmp.txt
        rm -rf /opt/agarik/scripts/autorestart_squid_mail_tmp.txt
fi