X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?p=monit-cn.git;a=blobdiff_plain;f=update-monit.d;h=7de29c1afe5efd3aa2e2b7b922facc6905bcab13;hp=4ed19e1ef3b2ce152075bb62a5050091a040a0db;hb=HEAD;hpb=e29e5899e52aa4c37f9d31b91da48e26e76b3158 diff --git a/update-monit.d b/update-monit.d index 4ed19e1..d7abcbf 100755 --- a/update-monit.d +++ b/update-monit.d @@ -1,24 +1,45 @@ #!/bin/sh - -MONIT_DIR=/etc/monit.d +# +# Copyright (C) 2007 Dinko Korunic, CARNET, Grupa za izradu paketa +# +# 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. + +MONIT_DIR=/etc/monit/conf.d TEMPLATES_DIR=/usr/share/monit-cn + check_service() { RET=0 - filename="$1" + if [ ! -r "$filename" ]; then RET=1 return 1 fi - script='BEGIN { RS = "[ \t\n]+" } + scriptpid='BEGIN { RS = "[ \t\n]+" } { line[NR] = $0 "" } END { for (j in line) if (line[j] == "pidfile") print line[j + 1] }' - pidfiles=$(awk "$script" < "$filename" | sed -e 's/"//g') + pidfiles=$(awk "$scriptpid" < "$filename" | sed -e 's/"//g') check_pidfile $pidfiles + + if [ $RET -eq 1 ]; then + return 1 + fi + + scriptinit='BEGIN { RS = "[ \t\n]+" } + { line[NR] = $0 "" } + END { for (j in line) + if (((line[j] == "start") || (line[j] == "stop")) && (line[j + 1] == "program") && (line[j + 2] == "=")) + print line[j + 3] }' + initscripts=$(awk "$scriptinit" < "$filename" | sed -e 's/"//g') + check_initscript $initscripts + return $RET } @@ -41,6 +62,20 @@ check_pidfile() { return 0 } +check_initscript() { + RET=0 + + while [ -n "$1" ]; do + if [ ! -x "$1" ]; then + RET=1 + return 1 + fi + shift + done + + return 0 +} + check_pid() { RET=0 @@ -55,6 +90,35 @@ check_pid() { return 0 } +check_template() { + RET=0 + filename="$1" + serviceconf=$(basename $i) + + if [ ! -r "$1" ]; then + RET=1 + return 1 + fi + + if [ ! -r "${TEMPLATES_DIR}/${serviceconf}" ]; then + RET=0 + return 0 + fi + + if ! grep -q "^#.*monit-cn template" "$filename"; then + RET=0 + return 0 + fi + + if ! cmp -s "$filename" "${TEMPLATES_DIR}/${serviceconf}"; then + RET=1 + return 1 + fi + + return $RET +} + + ##################################################################### changed=0 @@ -63,6 +127,16 @@ if [ ! -d "$MONIT_DIR" ]; then fi for i in $(find "$MONIT_DIR" -name '*.conf'); do + check_template $i + if [ ! $RET -eq 0 ]; then + serviceconf=$(basename $i) + echo "CN: Restored default $serviceconf template" + cp -a "${TEMPLATES_DIR}/${serviceconf}" "$MONIT_DIR" + changed=1 + fi +done + +for i in $(find "$MONIT_DIR" -name '*.conf'); do check_service $i if [ ! $RET -eq 0 ]; then serviceconf=$(basename $i) @@ -74,22 +148,22 @@ done for i in $(find "$TEMPLATES_DIR" -name '*.conf'); do serviceconf=$(basename $i) - if [ -r "$MONIT_DIR"/$serviceconf.disabled ]; then - check_service "$MONIT_DIR"/$serviceconf.disabled + if [ -r "${MONIT_DIR}/${serviceconf}.disabled" ]; then + check_service "${MONIT_DIR}/${serviceconf}.disabled" if [ $RET -eq 0 ]; then - if [ ! -r "$MONIT_DIR"/$serviceconf ]; then + if [ ! -r "${MONIT_DIR}/${serviceconf}" ]; then echo "CN: Enabled $serviceconf (previously disabled)" - mv "$MONIT_DIR"/$serviceconf.disabled \ - "$MONIT_DIR"/$serviceconf + mv "${MONIT_DIR}/${serviceconf}.disabled" \ + "${MONIT_DIR}/${serviceconf}" changed=1 fi fi else check_service $i if [ $RET -eq 0 ]; then - if [ ! -r "$MONIT_DIR"/$serviceconf ]; then + if [ ! -r "${MONIT_DIR}/${serviceconf}" ]; then echo "CN: Enabled $serviceconf (new service)" - cp -a $i "$MONIT_DIR" + cp -a $i "${MONIT_DIR}" changed=1 fi fi @@ -98,9 +172,12 @@ done if [ $changed -eq 1 ]; then echo "CN: Stopping/restarting monit service" - pkill -9 -f /usr/sbin/monit || true + #pkill -9 -f /usr/bin/monit || true + service monit restart else echo "CN: No new services detected, sorry" fi exit 0 + +# vi:ft=sh