ajout de la supervision
|
1 |
#!/bin/bash |
2 | ||
3 |
basename=$(basename $0) |
|
juste un renommage de variab...
|
4 |
monitoring_status="/dev/shm/${basename}${USER:+.$USER}.status" |
5 |
monitoring_disabled="/dev/shm/${basename}${USER:+.$USER}.disabled" |
|
6 |
monitoring_turns="/dev/shm/${basename}${USER:+.$USER}.turns" |
|
7 |
touch $monitoring_status $monitoring_disabled $monitoring_turns |
|
ajout de la supervision
|
8 |
now=$(date +%s) |
9 | ||
10 |
function _err () { |
|
11 |
test $# -eq 2 || return |
|
12 |
err[${#err[@]}]="$1" |
|
13 |
err[${#err[@]}]="$2%0a" |
|
14 |
} |
|
15 | ||
16 |
function _load () { |
|
17 |
local conf= |
|
18 |
local l= |
|
19 |
local d= |
|
20 |
local l_ok= |
|
21 |
local count_loaded= |
|
22 |
local msg= |
|
passage du répertoire de sou...
|
23 |
local directories=( |
24 |
"$HOME/.config/$basename" |
|
25 |
"${0}.d" |
|
26 |
"/usr/local/share/${basename}" |
|
27 |
) |
|
ajout de la supervision
|
28 |
for conf in $*; do |
29 |
count_loaded=0 |
|
juste un renommage de variab...
|
30 |
if grep -q "^@$conf$" $monitoring_disabled; then |
ajout de la supervision
|
31 |
disabled[${#disabled[@]}]=$conf |
32 |
else |
|
passage du répertoire de sou...
|
33 |
for dir in ${directories[@]}; do |
34 |
test -r "$dir/$conf" && source "$dir/$conf" $dir && let count_loaded++ |
|
ajout de la supervision
|
35 |
done |
36 |
test $count_loaded -gt 0 && loaded[${#loaded[@]}]=$conf |
|
37 |
fi |
|
38 |
done |
|
39 |
if test ${#loaded[@]} -ne $#; then |
|
40 |
for conf in $*; do |
|
41 |
l_ok=0 |
|
42 |
for l in ${loaded[@]}; do |
|
43 |
test $conf == $l && l_ok=1 && break |
|
44 |
done |
|
45 |
if test $l_ok -eq 0; then |
|
46 |
for d in ${disabled[@]}; do |
|
47 |
test $conf == $d && msg+="$conf (disabled)%0a" && l_ok=1 && break |
|
48 |
done |
|
49 |
fi |
|
50 |
test $l_ok -eq 0 && msg+="$conf%0a" |
|
51 |
done |
|
52 |
_err "_load" "config not loaded:%0a${msg/%%0a}" |
|
53 |
fi |
|
54 |
} |
|
55 | ||
_turn X || return: ajoute la...
|
56 |
function _turn () { |
57 |
test -n "$1" && [[ $1 =~ [0-9]+ ]] || return 1 |
|
juste un renommage de variab...
|
58 |
echo ${FUNCNAME[1]} >> $monitoring_turns |
correction filtre pour les t...
|
59 |
test $(( $(grep -c ${FUNCNAME[1]} $monitoring_turns) % $1 )) -eq 0 && sed -i "/^${FUNCNAME[1]}$/d" $monitoring_turns |
_turn X || return: ajoute la...
|
60 |
} |
ajout de la supervision
|
61 | |
62 |
function _do_tests () { |
|
quelques corrections sur la ...
|
63 |
local t= |
ajout de la supervision
|
64 |
for t in $(declare -f | sed -rn '/^[[:alpha:]]/s/^([[:alnum:]_]+)\s*\(\)\s*$/\1/p'); do |
quelques corrections sur la ...
|
65 |
local msg= |
juste un renommage de variab...
|
66 |
if grep -q "^${t}$" $monitoring_disabled; then |
ajout de la supervision
|
67 |
echo supervision by $t disabled |
68 |
else |
|
69 |
msg=$( $t | sed 's/$/%0a/g' ) |
|
70 |
if test -n "$msg"; then |
|
71 |
echo "problem: ${t}" |
|
72 |
err[${#err[@]}]=$t |
|
73 |
err[${#err[@]}]="${msg}" |
|
74 |
else |
|
75 |
if test -n "${!t}"; then |
|
juste un renommage de variab...
|
76 |
sed -ri "/^$t=.+$/d" $monitoring_status |
ajout de la supervision
|
77 |
fi |
78 |
fi |
|
79 |
fi |
|
80 |
done |
|
81 |
} |
|
82 | ||
83 |
function _do_manage_errors () { |
|
juste un renommage de variab...
|
84 |
source $monitoring_status |
quelques corrections sur la ...
|
85 |
local msg= |
86 |
local t= |
|
87 |
local errors= |
|
88 |
local notification=0 |
|
ajout de la supervision
|
89 | |
90 |
if test ${#err[@]} -gt 0; then |
|
91 |
errors="Supervision $HOSTNAME (${loaded[@]}):%0a" |
|
92 |
for ((i=0; i<${#err[@]}; i++)); do |
|
93 |
t=${err[i]} |
|
94 |
msg=${err[++i]} |
|
95 |
errors="${errors}"'*'" [${t}] ${msg}" |
|
96 |
if test -n "${!t}"; then |
|
97 |
if test ${!t} -lt $(( $now - 86400 )); then |
|
juste un renommage de variab...
|
98 |
sed -ri "s/^(${t}=).+$/\1$now/" $monitoring_status |
ajout de la supervision
|
99 |
let notification++ |
100 |
fi |
|
101 |
else |
|
juste un renommage de variab...
|
102 |
echo "${t}=$now" >> $monitoring_status |
ajout de la supervision
|
103 |
let notification++ |
104 |
fi |
|
105 |
done |
|
quelques corrections sur la ...
|
106 |
errors=$(tr -d '\n' <<< $errors) |
ajout de la supervision
|
107 |
if test "$SHLVL" -eq 1; then |
108 |
if test $notification -gt 0; then |
|
109 |
$(dirname "$0")/smsapi "$errors" |
|
110 |
fi |
|
111 |
else |
|
112 |
echo -e ${errors//\%0a/\\n} |
|
113 |
fi |
|
114 |
fi |
|
115 |
} |
|
116 | ||
117 |
_load ${*:-common-tests $HOSTNAME} |
|
118 |
_do_tests |
|
119 |
_do_manage_errors |