苍天有泪 发表于 2019-1-16 10:59:15

用nagios监控linux和windows服务器(七)

  十一、Nagios使用飞信短信报警
  1.安装飞信机器人
  将下载下来的fetion20091117-linux.tar.gz、fetion放在/tmp下
  cd /usr/local
  tar -zxvf /tmp/fetion20091117-linux.tar.gz    //将fetion解压到/usr/local下
  mv fx fetion       //将安装目录更名为fetion
  cp /tmp/fetion /usr/local/fetion/       //将fetion文件替换成fetion补丁文件
  # 必须将libACE-5.7.2.so、libACE_SSL-5.7.2.so、libcrypto.so.4、libssl.so.4四个文件copy到/lib目录下,因为fetion是默认到/lib下找这四个文件,否则fetion将无法启动。
  cp /usr/local/fetion/libACE-5.7.2.so /lib/
  cp /usr/local/fetion/libACE_SSL-5.7.2.so /lib/
  cp /usr/local/fetion/libcrypto.so.4 /lib/
  cp /usr/local/fetion/libssl.so.4 /lib/
  2.测试飞信安装是否成功
  root@web74 fetion]# ./fetion
  出现跟下面一样的内容则表示安装成功
  ************************ IMPORTANT STATEMENT ************************
  **                                                               **
  **   PLEASE DON'T USE THIS SOFTWARE TO SEND JUNK SHORT MESSAGES.   **
  **   OTHERWISE PLEASE BEAR YOUR OWN CONSEQUENCES.                  **
  **                                                               **
  **                              Version:      **
  *********************************************************************
  This program is the console version of China Fetion!
  It's free for personal user.
  This project website: http://www.it-adv.net/
  AUTHOR:KelvinH MSN/EMAIL:shichangguo@msn.com
  Usage:
  --mobile=
  --sid=
  --pwd=
  --config= *format:index mobile password
  --index=
  --debug*debug mode on
  --hide*login fetion in hidden state
  --to=
  --add=
  --command-path=
  --robotmode
  --daemon(linux only)
  --proxy-ip(http proxy ip)
  --proxy-port(http proxy port)
  --msg-gb=
  --msg-utf8=
  --msg-type=
  --file-gb=
  --file-utf8=
  --query-cmcc-no
  --testaccount
  --auto-retry
  --get-web-session
  --action=getpiccode --mobile=13910000000 --pwd= (--piccode-url=)
  --action=appsubscribe --mobile= --pwd= --pic-certificate-id= --pic-cert-code= --apply-sub-service-url=
  --action=subscribe --mobile= --pwd= --sms-code= --subscribe-url=
  --action=updatepwd --mobile= --pwd= --sms-code= --update-pwd-url=
  3.编写发送短信的脚本
  vi /usr/local/fetion/sendsms.sh
  -------------------------------------――
  #!/bin/sh
  fetionDir=/usr/local/fetion
  cd $fetionDir
  DIR=`pwd`
  # 设置发短信的号码和飞信登录密码
  user=159xxxxxxxx
  pwd=x123dsl5dow
  for phone in `cat $DIR/phonelist.txt`
  do
  echo "$phone" | sed '/^[ \t]*$/d' | sed 's/^[ \t]*//' | sed 's/[ \t]*$//' | grep '^1'
  if (($? == 0 ));then
  if [[ -f $DIR/msg.txt ]];then
  cat /dev/null > msg.txt
  fi
  phone=`echo "$phone" | sed 's/^[ \t]*//' | sed 's/[ \t]*$//'`
  echo "sms $phone $1" >> $DIR/msg.txt
  echo "quit" >> $DIR/msg.txt
  # 下面行定义飞信发送报警短信的命令
  $fetionDir/fetion --mobile=$user --pwd=$pwd --to=$phone --msg-utf8="$1"
  else
  continue
  fi
  done
  ---------------------------------------
  # phonelist.txt为接收短信号码列表,必须是发送飞信用户的好友
  # 在此控制短信接收者面不是在nagios联系人中,好处是修改接收者后不需要重启nagios
  # 可以使用#号注释接收者的号码以屏蔽接收者
  vi /usr/local/fetion/phonelist.txt
  # Phone NO.1
  136xxxxxxxx
  # Phone NO.2
  159xxxxxxxx
  #修改飞信目录权限(否则运行Nagios的用户不能写入信息内容到/usr/local/fetion/msg.txt)
  chown -R nagios.nagios /usr/local/fetion
  chmod +x /usr/local/fetion/sendsms.sh
  chmod +x /usr/local/fetion/fetion
  4.配置Nagios
  1).定义发送飞信报警短信的命令
  vi /usr/local/nagios/etc/objects/commands.cfg
  #########################################################################
  #
  # notify-host-by-sendmsg/notify-service-by-sendmsg //定义飞信发送报警短信的命令
  #
  #########################################################################
  # 下面是定义系统报警短信命令
  define command {
  command_name notify-host-by-sendmsg
  command_line /usr/local/fetion/sendsms.sh "Host $HOSTSTATE$ alert for $HOSTNAME$($HOSTADDRESS$) on $TIME$."
  }
  # 下面是定义服务报警短信命令
  define command {
  command_name notify-service-by-sendmsg
  command_line /usr/local/fetion/sendsms.sh ""$TIME$":$SERVICEDESC$($HOSTADDRESS$) is $SERVICESTATE$."
  }
  2).定义联系人模板
  vi /usr/local/nagios/etc/objects/templates.cfg
  define contact{
  name sendmsg-contact ; The name of this contact template
  service_notification_period   24x7                  ; service notifications can be sent anytime
  host_notification_period      24x7                  ; host notifications can be sent anytime
  service_notification_options    w,u,c,s             ; send notifications for all service states, flapping events, and scheduled downtime events
  host_notification_options       d,u,s               ; send notifications for all host states, flapping events, and scheduled downtime events
  service_notification_commands notify-service-by-sendmsg ; 使用定义的飞信报警命令
  host_notification_commands notify-host-by-sendmsg ; 使用定义的飞信报警命令
  register 0 ; DONT REGISTER THIS DEFINITION ¨C ITS NOT A REAL CONTACT, JUST A TEMPLATE!
  }
  3).定义联系人及修改联系人组
  vi /usr/local/nagios/etc/objects/contacts.cfg
  # 下面是定义接收报警短信的联系人,之前已经创建了接收短信的号码文件,所以此处无需再添加联系人的手机号码
  define contact{
  contact_name msgreceiver ; Short name of user
  use sendmsg-contact ; 使用上面定义的联系人模板
  alias MsgReceiver ; Full name of user
  email xuanding@yxqz.net
  将新添加的联系人增加到联系人组里,将msgreceiver增加到联系人组的members行后面,联系人中间用,分隔
  5.测试故障时是否能够触发短信报警
  # /usr/local/fetion/sendsms.sh "nagios Host alert for"
  #显示下面信息则代表短信正常发送出去了
  159xxxxxxxx
  Send Msg to:
  TP=3 ID=823479704   LO=0 SU=0 RE=0 SI=423500854 SE=6764MO=159xxxxxxxx GR=      ST=101RE=1 OL=499NN=15986367479LN=
  SIP-C/2.0 280 Send SMS OK
  T: sip:423500854@fetion.com.cn;p=6764
  I: 2
  Q: 1 M
  D: Tue, 21 Sep 2010 06:40:58 GMT
  XI: bc00ddb33ee84def8ca7ee0d42401e2a
  短信正常收到,则测试OK。



页: [1]
查看完整版本: 用nagios监控linux和windows服务器(七)