0

[CentOS, BASH, cron] Is there a method to declare variants that would keep even when system restarts?

The scenario is to snmpwalk interface I/O errors and store the values in an array. A cron job to snmpwalk again, say 5 mins later, would have another set of values. I would like to compare them with previous corresponding value of each interface. If the difference exceeds the threshold (50), an alert would generate.

So the question is: how to store an array variable that would lost in the system? and how to check the difference of each value in two arrays?


UPDATE Mar 16, 2012 I attach my final script here for your reference.

#!/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
Andrew
  • 113
  • 1
  • 5
  • 14

2 Answers2

0

Use a proper language for that (Perl, Python or others) and just store the array into a file.

Sven
  • 98,649
  • 14
  • 180
  • 226
  • Yes, finally I realized that variables would keep only within processes. Define the variables and save the data locally would be an approach. – Andrew Mar 10 '12 at 02:49
0

As SvenW, this would be easiest in a programming language like Perl, Python, or Ruby. To preserve your array after a restart, just write the contents to a file, then read that file when the script starts up. Add a comment if you would like examples on any part of this process.

muirbot
  • 221
  • 2
  • 4