Bash 脚本可以知道我的电话分机何时无法访问?

Bash 脚本可以知道我的电话分机何时无法访问?

有没有办法写一个 bash,当我的电话分机无法访问时可以通过电子邮件通知我?

输出来自/var/log/asterisk/full

[Nov 15 13:25:16] NOTICE[7884] chan_sip.c: Peer '7778' is now UNREACHABLE!  Last qualify: 17
[Nov 15 13:25:17] NOTICE[7884] chan_sip.c: Peer '7169' is now UNREACHABLE!  Last qualify: 17
[Nov 15 13:25:17] NOTICE[7884] chan_sip.c: Peer '7176' is now UNREACHABLE!  Last qualify: 18
[Nov 15 13:25:18] NOTICE[7884] chan_sip.c: Peer '7771' is now UNREACHABLE!  Last qualify: 14
[Nov 15 13:25:18] NOTICE[7884] chan_sip.c: Peer '7606' is now UNREACHABLE!  Last qualify: 17
[Nov 15 13:25:18] NOTICE[7884] chan_sip.c: Peer '7773' is now UNREACHABLE!  Last qualify: 14
[Nov 15 13:25:19] NOTICE[7884] chan_sip.c: Peer '7125' is now UNREACHABLE!  Last qualify: 15
[Nov 15 13:25:20] NOTICE[7884] chan_sip.c: Peer '7772' is now UNREACHABLE!  Last qualify: 15
[Nov 15 13:25:22] NOTICE[7884] chan_sip.c: Peer '7605' is now UNREACHABLE!  Last qualify: 16
[Nov 15 13:25:22] NOTICE[7884] chan_sip.c: Peer '7183' is now UNREACHABLE!  Last qualify: 18
[Nov 15 13:25:29] NOTICE[7884] chan_sip.c: Peer '7601' is now UNREACHABLE!  Last qualify: 24
[Nov 15 13:25:30] NOTICE[7884] chan_sip.c: Peer '7776' is now UNREACHABLE!  Last qualify: 47
[Nov 15 13:25:32] NOTICE[7884] chan_sip.c: Peer '7604' is now UNREACHABLE!  Last qualify: 25
[Nov 15 13:25:34] NOTICE[7884] chan_sip.c: Peer '7774' is now UNREACHABLE!  Last qualify: 46
[Nov 15 13:25:38] NOTICE[7884] chan_sip.c: Peer '7770' is now UNREACHABLE!  Last qualify: 41
[Nov 15 13:25:41] NOTICE[7884] chan_sip.c: Peer '7775' is now UNREACHABLE!  Last qualify: 42

如您所见,直到人们抱怨无法拨打电话时,我才知道电话已停机。

到目前为止我所做的:

#!/bin/bash

email="[email protected]"

offlineExtensions=$(cat /var/log/asterisk/full | grep -i unreachable)


if [ "$offlineExtensions" ]
then
printf 'Extensions that are currently offline...\n''\n'"$offlineExtensions" | mail -s 'Extensions OFFLINE' "$email"
fi

我想使用sedand awk,但我对 bash 脚本是全新的。

如果此脚本不断检查Asterisk log file以查明扩展是否无法访问,那就太好了。

答案1

不用看日志,asterisk直接询问哪些扩展不OK

asterisk -rx 'sip show peers like ^[0-9]{4}$' | awk 'NR>1 && !/ OK /'

将报告不“正常”的 4 位分机号。

答案2

这是一个简单one-liner的可以做你想做的事情:

tail -F /var/log/asterisk/full | while read -r line; do if [[ "$line" == *NREACHABLE* ]]; then mail -s "$line" [email protected] </dev/null ; fi; done

screen在或内运行此命令tmux

或者:

#!/bin/bash

    while read -r line
     do if [[ "$line" == *NREACHABLE* ]]; then 
         mail -s "$line" [email protected] </dev/null 
        fi 
     done < <(tail -F /var/log/asterisk/full)

答案3

下面的脚本可以满足您的需求。我曾经sendmail通过电子邮件发送通知。设置此脚本作为作业运行cron,它将检查无法访问的扩展,并在无法访问的扩展 > 0 时向您发送电子邮件通知

#!/bin/bash

email="[email protected]"
/usr/sbin/asterisk -rx 'sip show peers' > /tmp/allExtensions.txt
offlineExtensions=`cat /tmp/allExtensions.txt| grep 'UNREACHABLE'`
count=`cat /tmp/allExtensions.txt | wc -l`
if [ $count -gt 0 ]; then
(
echo "Subject: Offline Extensions"
printf 'Extensions that are currently offline...\n''\n'"${offlineExtensions}"
) | /usr/sbin/sendmail ${email}
fi

相关内容