我正在寻找替代方法来计算两台机器(mA 和 mB)之间的 ping,并将其报告给 Nagios(在 mC 上)。
我目前的想法是编写一个 BASH 脚本,该脚本将在 cron 作业中对机器执行 ping 操作,将数据输出到一个文件,然后让 Nagios 可以使用另一个 bash 脚本来读取该文件。不过,这感觉不是最好/正确的方法吗?
这是我计划在 cron 作业中运行的脚本:
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]
then
echo $0: usage: $0 file? ip? pingcount? deadline?
exit 126
else
FILE=$1
IP=$2
PCOUNT=$3
DLINE=$4
while read line
do
if [[ $line == rtt* ]]
then
#replace forward slash with underscore
line=${line////_}
#replace spaces with underscore
line=${line// /_}
#get the 8 item when splitting string on underscore
#echo $line| cut -d'_' -f 8 >> $FILE #Append
#echo $line| cut -d'_' -f 8 > $FILE #Overwrite
echo $line| cut -d'_' -f 8
fi
done < <(ping $IP -c $PCOUNT -q -w $DLINE) #-q output summary / -w deadline / -c pint count
我虽然关于使用跟踪路由,但我认为这会产生更慢的 ping?,还有其他方法可以实现我想要的吗?
注意:我知道 Nagios 可以直接 ping 一台机器,但这不是我想做的,也不会告诉我我想要什么。这也是我的第二个脚本,所以它可能是垃圾。另外,如果 ICMP 被阻止,我还有什么选择?