Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2549897
  • 博文数量: 271
  • 博客积分: 6659
  • 博客等级: 准将
  • 技术积分: 3141
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-17 10:24
文章分类

全部博文(271)

文章存档

2016年(2)

2015年(12)

2014年(7)

2013年(19)

2012年(22)

2011年(81)

2010年(128)

分类: 系统运维

2011-11-07 17:47:40

  1. # vim check_log
  2. #! /bin/sh
  3. ##从nagios 插件当中提取出来作为一个单独的脚本来跑
  4. ##另外加了smtp.pl作为发信程序

  5. ECHO="/bin/echo"
  6. GREP="/bin/egrep"
  7. DIFF="/usr/bin/diff"
  8. TAIL="/usr/bin/tail"
  9. CAT="/bin/cat"
  10. RM="/bin/rm"
  11. CHMOD="/bin/chmod"
  12. TOUCH="/bin/touch"


  13. ##定义邮件发送
  14. PERL="/usr/bin/perl"
  15. #邮件发送脚本
  16. PWD="/bin/pwd"
  17. SENTMAIL="$PWD/smtp.pl"
  18. #标题 [自己来定义邮件 的主题.我是拿来作mysql 日志报警的]
  19. NAME="MYSQL-ERROR-日志报警"
  20. INPUT1=`ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | awk -F: '{print $2}'`
  21. INPUT1="$INPUT1--$NAME"

  22. PROGNAME=`/bin/basename $0`
  23. PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
  24. REVISION="1.4.15"

  25. print_usage() {
  26.     echo "Usage: $PROGNAME -F logfile -O oldlog -q query"
  27.     echo "Usage: $PROGNAME --help"
  28.     echo "Usage: $PROGNAME --version"
  29. }

  30. print_help() {
  31.     echo ""
  32.     print_usage
  33.     echo ""
  34. }

  35. if [ $# -lt 1 ]; then
  36.     print_usage
  37.     exit $STATE_UNKNOWN
  38. fi

  39. # Make sure the correct number of command line
  40. # arguments have been supplied

  41. while test -n "$1"; do
  42.     case "$1" in
  43.         --help)
  44.             print_help
  45.             exit
  46.             ;;
  47.         -h)
  48.             print_help
  49.             exit
  50.             ;;
  51.         --version)
  52.             echo $PROGNAME $REVISION
  53.             exit
  54.             ;;
  55.         -V)
  56.             echo $PROGNAME $REVISION
  57.             exit
  58.             ;;
  59.         --filename)
  60.             logfile=$2
  61.             shift
  62.             ;;
  63.         -F)
  64.             logfile=$2
  65.             shift
  66.             ;;
  67.         --oldlog)
  68.             oldlog=$2
  69.             shift
  70.             ;;
  71.         -O)
  72.             oldlog=$2
  73.             shift
  74.             ;;
  75.         --query)
  76.             query=$2
  77.             shift
  78.             ;;
  79.         -q)
  80.             query=$2
  81.             shift
  82.             ;;
  83.         -x)
  84.             exitstatus=$2
  85.             shift
  86.             ;;
  87.         --exitstatus)
  88.             exitstatus=$2
  89.             shift
  90.             ;;
  91.         *)
  92.             echo "Unknown argument: $1"
  93.             print_usage
  94.             exit
  95.             ;;
  96.     esac
  97.     shift
  98. done

  99. # If the source log file doesn't exist, exit

  100. if [ ! -e $logfile ]; then
  101.     $ECHO "Log check error: Log file $logfile does not exist!\n"
  102.     exit
  103. elif [ ! -r $logfile ] ; then
  104.     $ECHO "Log check error: Log file $logfile is not readable!\n"
  105.     exit
  106. fi

  107. # If the old log file doesn't exist, this must be the first time
  108. # we're running this test, so copy the original log file over to
  109. # the old diff file and exit

  110. #初使化日志文件
  111. if [ ! -e $oldlog ]; then
  112.     $CAT $logfile > $oldlog
  113.     $ECHO "Log check data initialized..."
  114.     exit
  115. fi

  116. # The old log file exists, so compare it to the original log now

  117. # The temporary file that the script should use while
  118. # processing the log file.
  119. if [ -x /bin/mktemp ]; then
  120.     tempdiff=`/bin/mktemp /tmp/check_log.XXXXXXXXXX`
  121. else
  122.     tempdiff=`/bin/date '+%H%M%S'`
  123.     tempdiff="/tmp/check_log.${tempdiff}"
  124.     $TOUCH $tempdiff
  125.     $CHMOD 600 $tempdiff
  126. fi
  127. #用linux的diff工具,对比两个文件,并以grep提取"关键字”,如果有输出,则表明有异常
  128. $DIFF $logfile $oldlog | $GREP -v "^>" |sed 1d | sed 's/< //'> $tempdiff

  129. # matching log entries we have
  130. #整个tempdiff文件的内容
  131. content=`$CAT $tempdiff`
  132. #tempdiff中包含有$query的字段
  133. querycontent=`$GREP  "$query" $tempdiff`
  134. if [ ! -z "$content" ]; then

  135.     if [ "$querycontent" != "" ];then
  136.        perl smtp.pl  $INPUT1 $tempdiff
  137.     else
  138.        $ECHO "status is OK  no error!!" 
  139.     fi
  140.     $CAT $logfile > $oldlog
  141. fi
  142. $RM -f $tempdiff


  143. # vim smtp.pl
    1. #!/usr/bin/perl
    2. use Net::SMTP;
    3. my ($input1,$input2) = @ARGV;
    4. my $mailhost = "smtp.xxx.net"; # the smtp host
    5. my $mailfrom = 'server@xxx.net'; # your email address
    6. my @mailto = ('hujj@xxx.net'); # the recipient list
    7. my $subject = $input1;
    8. my $text = `/bin/cat $input2`;


    9. $smtp = Net::SMTP->new($mailhost, Hello => 'localhost', Timeout => 120);

    10. # anth login, type your user name and password here
    11. $smtp->auth('username','password');

    12. foreach my $mailto (@mailto) {
    13.     # Send the From and Recipient for the mail servers that require it
    14.     $smtp->mail($mailfrom);
    15.     $smtp->to($mailto);

    16.     # Start the mail
    17.     $smtp->data();

    18.     # Send the header
    19.     $smtp->datasend("To: $mailto\n");
    20.     $smtp->datasend("From: $mailfrom\n");
    21.     $smtp->datasend("Subject: $subject\n");
    22.     $smtp->datasend("\n");

    23.     # Send the message
    24.     $smtp->datasend("$text\n\n");

    25.     # Send the termination string
    26.     $smtp->dataend();
    27. }
    28. $smtp->quit;
阅读(2936) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~