0
[CentOS,BASH,cron]是否有一种方法可以声明即使在系统重新启动时也能保持的变体?BASH将数值存储在数组中并检查每个值的差异
该方案是snmpwalk接口I/O错误并将值存储在数组中。 5分钟后,再次sn walk的cron工作将会有另一套价值。我想将它们与每个接口以前的对应值进行比较。如果差值超过阈值(50),则会生成警报。
所以问题是:如何存储将丢失在系统中的数组变量?以及如何检查两个数组中每个值的差异?
更新2012年3月16日我附上我的最终脚本供您参考。
#!/bin/bash
# This script is to monitor interface Input/Output Errors of Cisco devices, by snmpwalk the error values every 5 mins, and send email alert if incremental value exceeds threshold (e.g. 500).
# Author: Wu Yajun | Created: 12Mar2012 | Updated: 16Mar2012
##########################################################################
DIR="$(cd "$(dirname "$0")" && pwd)"
host=device.ip.addr.here
# Check and initiate .log file storing previous values, create .tmp file storing current values.
test -e $DIR/host1_ifInErrors.log || snmpwalk -c public -v 1 $host IF-MIB::ifInErrors > $DIR/host1_ifInErrors.log
snmpwalk -c public -v 1 $host IF-MIB::ifInErrors > $DIR/host1_ifInErrors.tmp
# Compare differences of the error values, and alert if diff exceeds threshold.
# To exclude checking some interfaces, e.g. Fa0/6, Fa0/10, Fa0/11, change the below "for loop" to style as:
# for i in {1..6} {8..10} {13..26}
totalIfNumber=$(echo $(wc -l $DIR/host1_ifInErrors.tmp) | sed 's/ \/root.*$//g')
for ((i=1; i<=$totalIfNumber; i++))
do
currentValue=$(cat $DIR/host1_ifInErrors.tmp | sed -n ''$i'p' | sed 's/^.*Counter32: //g')
previousValue=$(cat $DIR/host1_ifInErrors.log | sed -n ''$i'p' | sed 's/^.*Counter32: //g')
diff=$(($currentValue-$previousValue))
[ $diff -ge 500 ] && (ifName=$(echo $(snmpwalk -c public -v 1 $host IF-MIB::ifName.$i) | sed 's/^.*STRING: //g') ; echo "ATTENTION - Input Error detected from host1 interface $ifName" | mutt -s "ATTENTION - Input Error detected from host1 interface $ifName" <email address here>)
done
# Store current values for next time checking.
snmpwalk -c public -v 1 $host IF-MIB::ifInErrors > $DIR/host1_ifInErrors.log
这种方法让我想起**的SQLite的**。 – Andrew 2012-03-09 09:52:06
SQL数据库是另一个选项,它以更多复杂性为代价为您提供更多功能。但最后,您需要以某种方式将数据保存到磁盘。如果使用文本文件或SQL数据文件是一个设计问题。 – 2012-03-09 09:59:47
至于我的任务,实际上last_updated_value和current_value之间的区别很重要。我仍在寻找如何声明一个可以保留在系统中的变量的答案。我听说过类似“环境变量VS局部变量”,但不知道里面是什么。感谢您的建议。 – Andrew 2012-03-09 10:09:34