在路上...

The development of life
我们一直都在努力,有您的支持,将走得更远...

站内搜索: Google

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
不要以为用shell和perl做网管简单,我贴个shell程序来看看吧:
#!/bin/sh
#programmed by BingoSek,20040219
#Function:
#1)compare raid controller event log, if it's different, something must happened on controller, send all status to receiver within the mail-list
#2)backup status of raid controller every running
#3)backup event of raid conftriller once event changed
#IBM command-line tool,ippsend must be placed at /bin/
#zip tool /usr/sbin/bzip2 must exist
#log file,be placed at /var/log/storage_backup/
#shell script and mail-list must be placed /home/storage_backup/
#Setup:set cronjob as this:
#0 */4 * * * /home/storage_backup/backup-raid-controller >> /var/log/storage_backup/error_cron.log 2>&1

home_dir=/home/storage_backup
log_dir=/var/log/storage_backup
date=`date +%Y%m%d`
time=`date +%H%M%S`
hostname=`/bin/hostname`
sender="$hostname""@Raid_Server"
subject='Raid controller may be critical'
compressor="/usr/bin/bzip2"
mailer="/usr/sbin/sendmail"
raid_event_diff="NO"
event_file="$log_dir/raid_controller_event"
status_file="$log_dir/raid_controller_status.$date.$time"
mail_list=$home_dir/mail_list


#function for send mail,usage: smail receiver sender subject mail_text_file
smail()
{
  receiver=$1
  sender=$2
  subject=$3
  mail_text_file=$4
  tmp=/tmp
  mailer="/usr/sbin/sendmail"

  echo "From: $sender" > $tmp/mail
  echo "To: $receiver" >> $tmp/mail
  echo "Subject: $subject"  >> $tmp/mail
  echo "Content-Type: text; charset=big5;"  >> $tmp/mail
  echo $mail_text_file
  cat $mail_text_file  >> $tmp/mail
  $mailer -v  $receiver < $tmp/mail
  rm -f $tmp/mail
}

#function for multiple send,usage:smail_group sender subject mail_list mail_text_file
smail_group()
{
  sender=$1
  subject=$2
  mail_list=$3
  mail_text_file=$4

  for i in  `cat $mail_list | grep -v "#"`
   do
    smail $i $sender "$subject" $mail_text_file
   done
}


#function for dump raid controller status,usage:get_status controller_number status_file
get_status()
{
  controller_number=$1
  status_file=$2
  backup_program=/bin/ipssend
  date=`date +%Y%m%d`

  echo $date > $status_file
  echo  "-------------------------------------------------------------------------------------" >> $status_file
  $backup_program GETBST $controller_number >> $status_file  2>&1
  echo  "-------------------------------------------------------------------------------------" >> $status_file
  $backup_program GETCONFIG $controller_number AL >> $status_file  2>&1
  echo  "-------------------------------------------------------------------------------------" >> $status_file
  $backup_program GETSTATUS $controller_number  >> $status_file  2>&1

}

#function for dump event,usage:get_event conftroller_number event_file
get_event()
{
  controller_number=$1
  event_file=$2
  backup_program=/bin/ipssend

  echo "---------------------------------------------------------------------------" > $event_file
  $backup_program GETEVENT $controller_number HARD > $event_file  2>&1
  echo "---------------------------------------------------------------------------" >> $event_file  
  $backup_program GETEVENT $controller_number DEVICE >> $event_file  2>&1
  echo "---------------------------------------------------------------------------" >> $event_file  
  $backup_program GETEVENT $controller_number DDD >> $event_file  2>&1
}

#main program

#initialize file
test -f /bin/ipssend || (echo "install ippsend to /bin first!!" ; exit)
test -d $log_dir || (mkdir $log_dir ; echo "$date-$time:First time running, create $log_dir")
#test -f $event_file || ( echo "$date-$time:First time running, create $event_file" ; get_event 1 $event_file)
test -f $event_file || ( echo "$date-$time:First time running, create $event_file" ; get_status 1 $event_file)

#backup status file
get_status 1 $status_file

#compare event log
mv $event_file $event_file."old"
#get_event 1 $event_file
cat $status_file > $event_file
diff -b -B $event_file $event_file."old" | grep "<" > $log_dir/diff.tmp

#if any event generate, send event and status to receiver
if [ -s $log_dir/diff.tmp ]
then
cat $log_dir/diff.tmp > $log_dir/mail.tmp
cat $status_file >> $log_dir/mail.tmp
echo "$subject"
smail_group $sender "$subject" $mail_list $log_dir/mail.tmp
cp $event_file $event_file."changed".$date.$time; $compressor $event_file."changed".$date.$time
mv $event_file."old" $event_file."original".$date.$time ;$compressor $event_file."original".$date.$time
fi  

#clean temp file
test -f $log_dir/diff.tmp && rm $log_dir/diff.tmp
test -f $log_dir/mail.tmp && rm $log_dir/mail.tmp
test -f $event_file."old" && rm $event_file."old"

#compress file
$compressor $status_file
这个程序是把raid controller的告警信息下log,并sendmail给网管邮箱的,还有其它一些程序,例如对voip进行录音并且转换为mp3的,给一百多台unix服务器一次性配置的script,我就不贴出来了,我认为自己还只是学了一点皮毛而已,如果精通perl和linux机制,虽说不是上天入地无所不能,到也可以说到达网管的顶端了
posted on 2009-08-20 15:59  palam  阅读(225)  评论(0编辑  收藏  举报