Muninで通知を設定する

Muninの記事-ネットワーク監視は簡単です! 監視が必要であり、作者はMuninを使用していると言われ、プラグインの書き方も説明されました。 この記事では、問題の通知をメールボックスに送信するようにMuninを構成する方法について説明します。

これを行うには、/ etc / munin / munin.confファイルに連絡先を作成し、この連絡先のparameters \ディレクティブを定義します。 これは次のように行われます。

contacts someuser
contact.someuser.command mail -s "Munin notification" somejuser@fnord.comm
contact.someuser.always_send warning critical


この構成は、連絡先someuserを作成し、そのための通知送信コマンド(この場合はmailコマンド)と、どの通知を送信するか( 警告 \警告およびクリティカル \クリティカル)を発行することを意味します。

なぜなら Muninは5分ごとに情報を収集し、5分ごとに通知(たとえば、ディスク容量不足、df_プラグイン)が送信されます。 理論的には、 contact.contact.max_messages numberというディレクティブがありcontact.contact.max_messages number 。これは、理解しているように、「警告/クリティカル」イベントの最大通知数を設定する必要があります。 こちらのディレクティブのリストをご覧ください
この機能は役に立たなかったため、この状況を解決する独自のスクリプトを作成する必要がありました。

また、 contact.someuser.commandディレクティブを使用すると、メッセージを送信するときに実際の値の代わりに使用される特別な変数を使用できます。 ここに変数のリスト。

このすべてを見てみましょう。 必須:2人のユーザーに通知を送信します。 最初のユーザーは、重大なメッセージと警告メッセージの両方を受け取る必要があります。 2番目は重要です。

/etc/munin/munin.confを開き、連絡先を作成します。

contacts user1 user2
contact.user1.command mail_send_mutt user1mail@gmail.com "Munin notification ${var:host}" "[${var:group};${var:host}] -> ${var:graph_title}: warnings: ${loop<,>:wfields ${var:label}=${var:value}}; criticals: ${loop<,>:cfields ${var:label}=${var:value}}"
contact.user1.always_send warning critical

contact.user2.command mail_send_mutt user2mail@yandex.ru "Munin notification ${var:host}" "[${var:group};${var:host}] -> ${var:graph_title}: warnings: ${loop<,>:wfields ${var:label}=${var:value}}; criticals: ${loop<,>:cfields ${var:label}=${var:value}}"
contact.user2.always_send critical


ここでは、3つの入力パラメーターを取るスクリプトmail_send_muttを使用しました。
1.ユーザーのメール
2.投稿タイトル
3.メッセージテキスト

レターを送信するには、muttコンソールメールクライアントが使用されます。 迅速かつ確実に機能し、追加の設定は必要ありません。

スクリプトテキスト:
#!/bin/bash
# mutt
# :
# 1.
# 2.
# 3.

# -,
# .
# ,
# . " "

email=$1
theme="$2"
ptext="\"$3\""

# : 0 - , 1 - , 2 - .
type_out=0
# .
function out_text () {
dbg_logfile="/var/log/munin/send_mess.log"
if ! test -e "$dbg_logfile"
then
cat "" > "$dbg_logfile"
fi

case $type_out in
1) echo "$1" ;;
2) echo "$1" >> $dbg_logfile ;;
*)
esac
}

#
var_path="/var/log/munin"
# . $email
logfile=""
# ,
if ! test -e "$var_path"
then
mkdir -p $var_path
fi

#
if test -z $email
then
out_text " !"
exit
fi

if test -z "$theme"
then
theme="null"
fi

if test -z "$ptext"
then
ptext="null"
fi

# - $email
#logfile=${email/@/_}
logfile=$var_path"/"${email/@/_}
# , . -
if ! test -e "$logfile"
then
cat "" > "$logfile"
fi

# ,
#
str_found=0
# .
dpos=0
# ,
tdate=""
tmail=""
ttext=""
#
FS="|"
#
FS_d='.'
#
curr_date=$(date +%Y-%m-%d)
out_text "Current date: \"$curr_date\""

# ,
while read fstr
do
# 3 : , e-mail,
# "|"
out_text "str_found begin: $str_found"
out_text "Find symbol: "$FS "; STR:" "$fstr"
dpos=`expr index "$fstr" $FS`
out_text "dpos: $dpos"
tdate=`expr substr "$fstr" 1 $dpos`
fstr=${fstr/$tdate/}
tdate=${tdate/$FS/}
out_text "NEW STR:" "$fstr"
dpos=`expr index "$fstr" $FS`
tmail=`expr substr "$fstr" 1 $dpos`
ttext=${fstr/$tmail/}
tmail=${tmail/$FS/}

dpos=`expr index "$tdate" $FS_d`
let "dpos-=1"
tdate=`expr substr "$tdate" 1 $dpos`

out_text "date: \"$tdate\""
out_text "email: $tmail"
out_text "ttext: $ttext"
out_text "ptext: $ptext"
out_text "------------------------------------------------------"
# ,
if [ "$ptext" == "$ttext" ]
then
#
# , . ( )
out_text "Text EQ found!"
if [ "$tdate" == "$curr_date" ]
then
out_text "Date EQ found!"
let "str_found+=1"
fi
fi
out_text "str_found end: $str_found"
out_text "------------------------------------------------------"

done < "$logfile" #
out_text "str_found: $str_found"
if [ 0 -lt "$str_found" ]
then
out_text " !"
exit
fi

#
echo "$ptext" | mutt $email -s "$theme"

# , ,
echo "$(date +%Y-%m-%d.%H-%M)|$email|$ptext" >> $logfile


デバッグ情報の出力だけでなく、すべてのコメントをスクリプトに残しました。 たぶん誰かがするでしょう。 デフォルトでは、メッセージ出力は無効になっています。 スクリプトを改善する方法についてアイデアをお持ちの方がいらっしゃいましたら、訂正させていただきます。
たとえば、次のような設計があります。
while IFS=: read name passwd uid gid fullname ignore
do
echo "$name ($fullname)"
done </etc/passwd # .


ログファイルを読み取って既に送信されたメッセージを表示するサイクルは、次のようにやり直すことができるようです。
while FS=| read tdate tmail ttext
do
.....
done < "$logfile"

しかし、何らかの理由で、説明どおりに動作しません:(

Source: https://habr.com/ru/post/J48758/


All Articles