Postgresql vaccum

De BlaxWiki
Aller à la navigationAller à la recherche

Ce script a pour but de s'assurer que les vaccum sont bien effectués

#!/bin/bash

# CONF SUPERVISION
VISION="/opt/agarik/Vision/bin/bb_send_raw"
DISPLAY='supervision.agarik.com'
TARGET='elisweb-prd-bdd1.siop.elis.com'
SERVICE="appli2"
LIFE_MESSAGE="1560"
COLOR='green'
MESSAGE=''

# CONF SCRIPT
base='/opt/agarik/scripts/vaccumdb'
logs='/home/postgres/vacuum/logs/'
max_exec_minutes='360'
max_non_exec_jours='7'

Send(){
        "${VISION}" "${DISPLAY}" "status+${LIFE_MESSAGE} ${TARGET}.${SERVICE} ${1} $(date) ${SERVICE} ${2}"
}

cd "${logs}"
dernier_log="$(ls -r vacuum_elisweb_* | head -1)"
processus="$(pgrep vacuumdb)"
if [ "${processus}" = '' ]; then
        diff_j="$(( $(( $(date '+%s') - $(stat -c '%Y' "${dernier_log}") )) / 86400 ))"
        derniere_exec="$(stat -c '%y' "${dernier_log}")"
        if [ "${diff_j}" -gt "${max_non_exec_jours}" ]; then
                COLOR='red'
                MESSAGE="\n&red derniere execution terminee le : ${derniere_exec}"
        else
                MESSAGE="\n&green derniere execution terminee le : ${derniere_exec}"
        fi
        MESSAGE="${MESSAGE}\n${logs}${dernier_log} :"
        MESSAGE="${MESSAGE}\n$(grep -E '(vacuumdb|vacuuming|WARNING|ERROR|Date de debut|du vacuum de la base)' "${dernier_log}" | sed 's/^/   /g')"
        [ "$(echo "${MESSAGE}" | grep 'WARNING')" != '' ] && COLOR='yellow'
        [ "$(echo "${MESSAGE}" | grep 'ERROR')" != '' ] && COLOR='red'
else
        exec_m="$(( $(ps -p "${processus}" -o 'etimes' | tail -n 1) / 60 ))"
        commande="$(ps -p "${processus}" -o command | tail -n 1)"
        [ "${exec_m}" -gt "${max_exec_minutes}" ] && COLOR='red'
        MESSAGE="\nLa commande \"${commande}\" est en cours d'execution depuis ${exec_m} minutes."
fi

Send "${COLOR}" "$(echo -e "${MESSAGE}")"