Nagios3.0.5在CentOS5.2中安装

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
Nagios3.0.5在CentOS5.2中安装  
  用nagios主要是监控一台主机的各种信息,包括本机资源以及对外的 服务 等等.这些在nagios里面都是被定义为一个个的 项目 (nagios称之为服务,为了与主机提供的服务相区别,我这里用项目这个词),而实现每个监控项目,则需要通过commands.cfg 文件 中定义的 命令 。为了不必重复定义一些项目,Nagios引入了一个模板配置文件(templates.cfg),将一些共性的属性定义成模板,以便于多次引用。  我们现在有一个监控项目是监控一台机器的 web 服务是否正常, 我们需要哪些元素呢?最重要的有下面三点:首先是监控哪台机器,然后是这个监控要用什么命令实现,最后就是出了问题的时候要通知哪个联系人。  我们首先应该在commands.cfg中定义监控远程服务和资源的命令,以及如何发送邮件的命令。大部分监控远程服务和资源的命令的命令通过/usr/local/nagios/libexec下的脚本实现,如ping命令为check_ping。/usr/local/nagios/libexec下的脚本命令的使用发法可以通过-h参数查看,如:-----------------------------------------------------------------------------------------[root@tech ~]# /usr/local/nagios/libexec/check_ping -hcheck_ping v1991 (nagios-plugins 1.4.13)Copyright (c) 1999 Ethan Galstad Copyright (c) 2000-2007 Nagios Plugin Development Team    Use ping to check connection statistics for a remote host.Usage:check_ping -H  -w ,% -c ,% [-p packets] [-t timeout] [-4|-6]Options: -h, --help    Print detailed help screen -V, --version    Print version information -4, --use-ipv4    Use IPv4 connection -6, --use-ipv6    Use IPv6 connection -H, --hostname=HOST    host to ping -w, --warning=THRESHOLD    warning threshold pair -c, --critical=THRESHOLD    critical threshold pair -p, --packets=INTEGER    number of ICMP ECHO packets to send (Default: 5) -L, --link    show HTML in the plugin output (obsoleted by urlize) -t, --timeout=INTEGER    Seconds before connection times out (default: 10)-----------------------------------------------------------------------------------------然后我们在contacts.cfg文件中定义联系人和联系人组,在timeperiods.cfg中定义监控时间段。最后我们在 服务器 监控配置文件中引用前面定义的元素来监控服务器状态。===============================================================================下面引用配置文件中部分配置做说明:vi /usr/local/nagios/etc/resource.cfg# 定义$USER1$变量,设置插件路径$USER1$=/usr/local/nagios/libexecvi /usr/local/nagios/etc/objects/commands.cfg# 定义check-host-alive命令define command{        command_name    check-host-alive  # 命令名称        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5        }# 上面的$USER1$和$HOSTADDRESS$引用自已定义的配置文件。变量不需现定义才能被引用。########################################################################## 2008.11.18 add by Stone# NRPE COMMAND# 自己定义check_nrpe命令,此命令后接必需接一个参数,用于告诉远程服务器上的NRPE daemon需要监控的内容,如check_swap参数为监控远程机器的交换分区。######################################################################### 'check_nrpe ' command definitiondefine command{        command_name check_nrpe        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$        }vi /usr/local/nagios/etc/objects/contacts.cfg# 定义联系人define contact{        contact_name                    nagiosadmin             ; Short name of user        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)        alias                           Nagios Admin            ; Full name of user        email                           test@gmaile.com         ;         }# 上面的generic-contact在templates.cfg中定义。# 定义联系人组define contactgroup{        contactgroup_name       admins        alias                   Nagios Administrators        members                 nagiosadmin   #在此可以加入多个联系人,中间用逗号隔开        }vi /usr/local/nagios/etc/objects/timeperiods.cfg# 定义监控的时间段define timeperiod{        timeperiod_name 24x7   #监控所有时间段(7*24小时)        alias           24 Hours A Day, 7 Days A Week        sunday          00:00-24:00        monday          00:00-24:00        tuesday         00:00-24:00        wednesday       00:00-24:00        thursday        00:00-24:00        friday          00:00-24:00        saturday        00:00-24:00        }vi /usr/local/nagios/etc/objects/templates.cfg# 定义generic-contact联系人模板,并非真正的联系人,真正的联系人在contacts.cfg中定义define contact{        name                            generic-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,r,f,s             ; send notifications for all service states, flapping events, and scheduled downtime events        host_notification_options       d,u,r,f,s               ; send notifications for all host states, flapping events, and scheduled downtime events        service_notification_commands   notify-service-by-email ; send service notifications via email        host_notification_commands      notify-host-by-email    ; send host notifications via email        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!        }------------------------------------------------------------------------------------------------------------------service_notification_period     24x7服务出了状况通知的时间段,这个时间段就是上面在timeperiods.cfg中定义的.host_notification_period        24x7主机出了状况通知的时间段, 这个时间段就是上面在timeperiods.cfg中定义的service_notification_options    w,u,c,r当服务出现w—报警(warning),u—未知(unkown),c—严重(critical),或者r—从异常情况恢复正常,在这四种情况下通知联系人.host_notification_options       d,u,r当主机出现d­­­­—当机(down),u—返回不可达(unreachable),r—从异常情况恢复正常,在这3种情况下通知联系人service_notification_commands   notify-service-by-email服务出问题通知采用的命令notify-service-by-email,这个命令是在commands.cfg中定义的,作用是给联系人发邮件.host_notification_commands      notify-host-by-email同上,主机出问题时采用的也是发邮件的方式通知联系人------------------------------------------------------------------------------------------------------------------# 定义generic-host主机模板define host{        name                            generic-host    ; The name of this host template        notifications_enabled           1               ; Host notifications are enabled        event_handler_enabled           1               ; Host event handler is enabled        flap_detection_enabled          1               ; Flap detection is enabled        failure_prediction_enabled      1               ; Failure prediction is enabled        process_perf_data               1               ; Process performance data        retain_status_information       1               ; Retain status information across program restarts        retain_nonstatus_information    1               ; Retain non-status information across program restarts        notification_period             24x7            ; Send host notifications at any time        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!        }# 定义Linux主机模板define host{        name                             linux -server    ; The name of this host template        use                             generic-host    ; This template inherits other values from the generic-host template        check_period                    24x7            ; By default, Linux hosts are checked round the clock        check_interval                  5               ; Actively check the host every 5 minutes        retry_interval                  1               ; Schedule host check retries at 1 minute intervals        max_check_attempts              10              ; Check each Linux host 10 times (max)        check_command                   check-host-alive ; Default command to check Linux hosts        notification_period             workhours       ; Linux admins hate to be woken up, so we only notify during the day                                                        ; Note that the notification_period variable is being overridden from                                                        ; the value that is inherited from the generic-host template!        notification_interval           120             ; Resend notifications every 2 hours        notification_options            d,u,r           ; Only send notifications for specific host states        contact_groups                  admins          ; Notifications get sent to the admins by default        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!        }# 在nagios.cfg配置文件中开启对/usr/local/nagios/etc/servers/中配置文件的引用。cfg_dir=/usr/local/nagios/etc/servers# 远程Linux主机监控文件,如果监控多台主机只需简单复制修改即可。#我们应该牢记wiki-l-11.cfg用到的命令在commands.cfg中定义,在commands.cfg中定义的命令用到/usr/local/nagios/libexec下的插件(命令)。vi /usr/local/nagios/etc/servers/wiki-l-11.cfg# 定义主机define host{        use                     linux-server            ; Name of host template to use                                                        ; This host definition will inherit all variables that are defined                                                        ; in (or inherited by) the linux-server host template definition.        host_name               wiki        alias                   Docs        address                 192.168.0.11        }# 定义Ping远程Linux主机define service{        use                             generic-service         ; Name of service template to use        host_name                       wiki        service_description             PING        check_command                   check_ping!100.0,20%!500.0,60%   ;check_ping命令在commands.cfg中定义,后跟两个参数,命令及参数间用!分割。        }#检查远程Linux主机根分区使用情况,check_nrpe命令必须在/usr/local/nagios/etc/objects/commands.cfg中定义(默认未定义)define service{        use                             generic-service         ; Name of service template to use        host_name                       wiki        service_description             Root Partition        check_command                   check_nrpe!check_disk_root        }# 检查远程Linux主机的登录人数define service{        use                             generic-service         ; Name of service template to use        host_name                       wiki        service_description             Current Users        check_command                   check_nrpe!check_users        }# 检查远程Linux的主机的负载define service{        use                             generic-service         ; Name of service template to use        host_name                       wiki        service_description             Current Load        check_command                   check_nrpe!check_load        }# 检查远程Linux主机swap分区使用情况define service{        use                             generic-service         ; Name of service template to use        host_name                       wiki        service_description             Swap Usage        check_command                   check_nrpe!check_swap        }# 检查远程Linux主机的SSH服务define service{        use                             generic-service         ; Name of service template to use        host_name                       wiki        service_description             SSH        check_command                   check_ssh        notifications_enabled           0        }# 检查远程Linux主机的HTTP服务define service{        use                             generic-service         ; Name of service template to use        host_name                       wiki        service_description             HTTP        check_command                   check_http        notifications_enabled           0        }vi /usr/local/nagios/etc/servers/hostgroup.cfg# 定义主机组(localhost.cfg中有类似的主机组设置,我已将其注释掉,否则可能会有冲突)define hostgroup{        hostgroup_name  linux-servers ; The name of the hostgroup        alias           Linux Servers ; Long name of the group        members         localhost,wiki     ; Comma separated list of hosts that belong to this group        }#define hostgroup{#        hostgroup_name  windows-servers ; The name of the hostgroup#        alias            Windows  Servers ; Long name of the group#        members         print     ; Comma separated list of hosts that belong to this group#        }===============================================================================# 完成监控主机配置文件的配置后使用下面命令检查配置文件的正确性:/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg# 确定无误后重启Nagios:service nagios restart
使用Nagios监控Windows服务器
12.Nagios使用NSClient++监控远程Windows主机

下载 NSClient++-Win32-0.3.5.msi并安装。
到安装目录打开NSC.ini文件进行修改:
在[modules]模块,将除CheckWMI.dll和RemoteConfiguration.dll外的所有dll文件明前的注释(;)去掉。
在 [Settings]模块可以设置一个连接密码password=PWD,为了简单,在此不设密码。设置 allowed_hosts=127.0.0.1/32,192.168.0.19,可以连接的监控服务器的地址,如果写成192.168.0.0/24 则表示该子网内的所有机器都可以访问;如果这个地方是空白则表示所有的主机都可以连接上来(注意在[NSClient]有allowed_hosts的同样设置,不要设置错了),最后不要忘记去掉前面的注释符(;)。
运行nsclient++
NSClient++ /install
NSClient++ SysTray install
NSClient++ /start
如果有防火墙,请开放相应端口。
创建监控配置文件,使用check_nt命令监控windows 系统 信息(此命令默认已定义)。
Windows监控示例配置文件:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[root@tech etc]# cat /usr/local/nagios/etc/servers/print-w-80.cfg
###############################################################################
# WINDOWS.CFG - SAMPLE CONFIG FILE FOR MONITORING A WINDOWS MACHINE
#
# Last Modified: 06-13-2007
#
# NOTES: This config file assumes that you are using the sample configuration
#     files that get installed with the Nagios quickstart guide.
#
###############################################################################
###############################################################################
###############################################################################
#
# HOST DEFINITIONS
#
###############################################################################
###############################################################################
# Define a host for the Windows machine we'll be monitoring
# Change the host_name, alias, and address to fit your situation
define host{
    use        windows-server    ; Inherit default values from a template
    host_name    print80        ; The name we're giving to this host
    alias        Print80        ; A longer name associated with the host
    address        192.168.0.80    ; IP address of the host
    }
###############################################################################
###############################################################################
#
# HOST GROUP DEFINITIONS
# 主机组在/usr/local/nagios/etc/servers/hostgroup.cfg中单独配置
###############################################################################
###############################################################################
# Define a hostgroup for Windows machines
# All hosts that use the windows-server template will automatically be a member of this group
#define hostgroup{
#    hostgroup_name    windows-servers    ; The name of the hostgroup
#    alias        Windows Servers    ; Long name of the group
#    }
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Create a service for monitoring the version of NSCLient++ that is installed
# Change the host_name to match the name of the host you defined above
define service{
    use            generic-service
    host_name        print80
    service_description    NSClient++ Version
    check_command        check_nt!CLIENTVERSION
    }
# Create a service for monitoring the uptime of the server
# Change the host_name to match the name of the host you defined above
define service{
    use            generic-service
    host_name        print80
    service_description    Uptime
    check_command        check_nt!UPTIME
    }
# Create a service for monitoring  CPU  load
# Change the host_name to match the name of the host you defined above
define service{
    use            generic-service
    host_name        print80
    service_description    CPU Load
    check_command        check_nt!CPULOAD!-l 5,80,90
    }
# Create a service for monitoring
# Change the host_name to match the name of the host you defined above
define service{
    use            generic-service
    host_name        print80
    service_description    Memory Usage
    check_command        check_nt!MEMUSE!-w 80 -c 90
    }
# Create a service for monitoring C:\ disk usage
# Change the host_name to match the name of the host you defined above
define service{
    use            generic-service
    host_name        print80
    service_description    C_Drive_Space
    check_command        check_nt!USEDDISKSPACE!-l c -w 80 -c 90
    }
# Create a service for monitoring the W3SVC service
# Change the host_name to match the name of the host you defined above
define service{
    use            generic-service
    host_name        print80
    service_description    W3SVC
    check_command        check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
    }
# Create a service for monitoring the Explorer.exe process
# Change the host_name to match the name of the host you defined above
define service{
    use            generic-service
    host_name        print80
    service_description    Explorer
    check_command        check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
    }
#主机组配置文件
[root@tech etc]# cat /usr/local/nagios/etc/servers/hostgroup.cfg
define hostgroup{
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         localhost,wiki     ; Comma separated list of hosts that belong to this group
        }
define hostgroup{
        hostgroup_name  windows-servers ; The name of the hostgroup
        alias           Windows Servers ; Long name of the group
        members         print80     ; Comma separated list of hosts that belong to this group
        }
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
使用NDOUtils将Nagios监控信息存入 数据
1.NDOUtils安装需求:
Nagios正确安装运行,版本>=2.0安装目录:/usr/local/nagios
MySQL正确安装并运行,我用的是二进制安装包,安装目录:/usr/local/ mysql

2.安装
下载安装包:http://www.nagios.org/download/
# 确认DBD-mysql包安装,如没有安装,请先安装。
# 如果DBI包没安装DBD-mysql前需要先安装DBI包
tar xzvf DBI-1.605.tar.gz
cd DBI-1.605
perl  Makefile.PL
make
make install
tar xzvf DBD-mysql-3.0008.tar.gz
cd DBD-mysql-3.0008
unset LANG
perl Makefile.PL --libs="-L/usr/local/mysql/lib/mysql -lmysqlclient -L/usr/lib -lz " --cflags="-I/usr/local/mysql/include"  --testhost=127.0.0.1 --mysql_config=/usr/local/mysql/bin/mysql_config --testuser=root --testpassword=baihe.com
make
make install
# 做好MySQL库文件lib和include文件的连接(这点很重要)。
ln -s /usr/local/mysql/include/* /usr/include/
mkdir /usr/include/mysql
ln -s /usr/local/mysql/include/* /usr/include/mysql/
ln -s /usr/local/mysql/lib/* /usr/lib/
mkdir /usr/lib/mysql
ln -s /usr/local/mysql/lib/* /usr/lib/mysql
tar xzvf ndoutils-1.4b7.tar.gz
cd ndoutils-1.4b7
./configure --enable-mysql
# 执行完成后,注意向上看看打印出的信息,看看是否找到了MySQL的库文件和include文件。
make
cd src/
# 我的Nagios是3.0.5,所以拷贝的是ndomod-3x.o、ndo2db-3x,如果你的是2.X.x请拷贝ndomod-2x.o、ndo2db-2x
cp ndomod-3x.o ndo2db-3x log2ndo file2sock /usr/local/nagios/bin
cd ../db
# 创建 数据库 ,使用root用户,密码是baihe.com
mysqladmin -u root -pbaihe.com create nagios
./installdb -u root -p baihe.com -d nagios
# 拷贝配置文件
cd ../config
cp ndo* /usr/local/nagios/etc/
# 修改/usr/local/nagios/etc/ndo2db.cfg文件的数据库信息。
vi /usr/local/nagios/etc/ndo2db.cfg
vi nagios.cfg
# 复制下面内容粘贴到/usr/local/nagios/etc/nagios.cfg配置文件的#broker_module=...下面。
# Uncomment the line below if you're running Nagios 3.x
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
# 修改/usr/local/nagios/etc/nagios.cfg配置文件下面参数的值为-1(一般默认如此)。
event_broker_options=-1
# 启动ndo2db
/usr/local/nagios/bin/ndo2db-3x -c /usr/local/nagios/etc/ndo2db.cfg
# 查看系统日志是否有错误信息:
tail -30 /var/log/messages
Nov 27 14:09:26 tech nagios: ndomod: NDOMOD 1.4b7 (10-31-2007) Copyright (c) 2005-2007 Ethan Galstad (nagios@nagios.org)
Nov 27 14:09:26 tech nagios: ndomod: Successfully connected to data sink.  0 queued items to flush.
Nov 27 14:09:26 tech nagios: Event broker module '/usr/local/nagios/bin/ndomod-3x.o' initialized successfully.
Nov 27 14:09:26 tech nagios: Finished daemonizing... (New PID=18848)
# 重启Nagios
service nagios restart
# 查看Nagios日志,看是否正常启动。
tail -20 /usr/local/nagios/var/nagios.log
[1227766166] ndomod: NDOMOD 1.4b7 (10-31-2007) Copyright (c) 2005-2007 Ethan Galstad (nagios@nagios.org)
[1227766166] ndomod: Successfully connected to data sink.  0 queued items to flush.
[1227766166] Event broker module '/usr/local/nagios/bin/ndomod-3x.o' initialized successfully.
[1227766166] Finished daemonizing... (New PID=18848) 

3.如何用NDOUtils将多个Nagios实例的性能数据写入一个数据库?

配置方法类似于上面的单实例单数据库 模式 ,只是在装第二个实例时省去安装DB一步,并需要修改/usr/local/nagios/etc/ndomod.cfg中的实例名。
实例名默认为instance_name=default,如果有多个实例必需修改成不同的实例名。
如:instance_name=nagios1
注意修改/usr/local/nagios/etc/ndo2db.cfg中数据库连接的相关配置。
安装使用Nagvis插件
1.安装需求:
1).Nagios已正常运行。
2).使用NDOUtils将Nagios监控信息存入数据库(见NDOUtils安装)。
3).PHP5.0以上版本,并需要安装 php -gd、php-mysql、php5-mbstring、php5-session、php5-xml模块。
4). 安装Graphviz >= 2.14,下载http://www.graphviz.org/graphviz-rhel.repo文件放到/etc/yum.repos.d/目录下,yum list available 'graphviz*';yum install 'graphviz*'或者安装webdot:yum install 'webdot'。
2.Nagvis安装
1).下载Nagivs,http://www.nagvis.org。
2).tar xvzf nagvis-1.3.x.tar.gz
3).mv nagvis /usr/local/nagios/share
4).配置nagvis
cd /usr/local/nagios/share/nagvis
cp etc/nagvis.ini.php-sample etc/nagvis.ini.php
vi etc/nagvis.ini.php
在[paths]模块修改URL path。
在[backend_ndomy_1]模块修改连接数据库的一下信息(如用户名、密码等)。
注意:修改过的参数需要去掉前面的分号注释符。
5).将/usr/local/nagios/share/nagvis设为运行 apache 的用户(daemon)所有。
chown daemon:nagios /usr/local/nagios/share/nagvis -R
chmod 664 /usr/local/nagios/share/nagvis/etc/nagvis.ini.php
chmod 775 /usr/local/nagios/share/nagvis/nagvis/images/maps
chmod 664 /usr/local/nagios/share/nagvis/nagvis/images/maps/*
chmod 775 /usr/local/nagios/share/nagvis/etc/maps
chmod 664 /usr/local/nagios/share/nagvis/etc/maps/*
chmod 775 /usr/local/nagios/share/nagvis/var
chmod 664 /usr/local/nagios/share/nagvis/var/*
3.访问图形配置工具:http:////config.php
4.访问监控图: http:////index.php?map=



5.Nagivs如何使用多个Nagios实例的性能数据作图?
《如何用NDOUtils将多个Nagios实例的性能数据写入一个数据库》见NDOUtils的安装。
修改/usr/local/nagios/share/nagvis/etc/nagvis.ini.php,定义多个backend_ndomy_x
[backend_ndomy_2]
; type of backend - MUST be set
;backendtype="ndomy"
; hostname for NDO-db
;dbhost="localhost"
; portname for NDO-db
;dbport=3306
; database-name for NDO-db
;dbname="nagios"
; username for NDO-db
;dbuser="root"
; password for NDO-db
dbpass="111111"
; prefix for tables in NDO-db
;dbprefix="nagios_"
; instace-name for tables in NDO-db
dbinstancename="nagios1"
; maximum delay of the NDO Database in Seconds
;maxtimewithoutupdate=180
; path to the cgi-bin of this backend
;htmlcgi="/nagios/cgi-bin"
注意上面的数据库连接设置和dbinstancename设置。
在作图时如需引用nagios1实例的性能数据,backend_id栏需要选择ndomy_2。



Nagios使用飞信短信报警
1.下载安装飞信机器人
下载地址:
http://www.it-adv.net/
注意:ReaHat和CentOS用户应使用LINUX X86/32(REDHAT ES4X32)版。
其中支持库和安装包内容如下:
支持库:libraryrh4x32.tar.gz
最新飞信机器人为fetion20080522004-linrh4.tar.gz
cd /usr/local
# 将飞信解压到/usr/local下
tar zxvf fetion20080522004-linrh4.tar.gz
mv install fetion
# 创建飞信支持库目录
mkdir /usr/local/fetion/lib
tar zxvf libraryrh4x32.tar.gz
cd libraryrh4x32
cp lib*so* /usr/local/fetion/lib
ln -s /usr/local/fetion/lib/libcrypto.so.0.9.7a /usr/local/fetion/lib/libcrypto.so.4
ln -s /usr/local/fetion/lib/libssl.so.0.9.7a /usr/local/fetion/lib/libssl.so.4
vi /etc/ld.so.conf
# 加入下面行
/usr/local/fetion/lib
# 保存退出,并执行ldconfig命令使配置生效。

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:[20080522004-linrh4]      **
*********************************************************************
This program is the console version of China Fetion!
It's free for personal user.
Fetion official website: 
http://www.fetion.com.cn/
This project website: 
http://www.it-adv.net/
AUTHOR:KelvinH MSN/EMAIL:shichangguo@msn.com
Usage:
    fetion -h
        -h: help
    fetion -u mobile -p pwd [-b batchfile] [-EN] [-d]
    fetion -u mobile -p pwd [-b batchfile] [-EN] [-d]
        -u: Fetion user account(only supports mobile phone No.)
        -p: Account password
        -b: Batch file name
        -d: Debug on and write logs to [mobile]-debug.log
        -EN: English

3.编写发送脚本
vi /usr/local/fetion/sendsms.sh
#!/bin/sh
fetionDir=/usr/local/fetion
cd $fetionDir
DIR=`pwd`
# 设置发短信的号码和飞信登录密码
user=158xxxxxxxx
pwd=xxxxxx
for phone in `cat $DIR/phonelist.txt`
do
    echo "$phone" |  sed  '/^[ \t]*$/d' | sed 's/^[ \t]*//' | sed 's/[ \t]*$//' | grep '^1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
    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 -u $user -p $pwd -b $DIR/msg.txt
    else
        continue
    fi
done
# phonelist.txt为接收短信号码列表,必须是发送飞信用户的好友
# 在此控制短信接收者而不是在nagios联系人中,好处是修改接收者后不需要重启nagios
# 可以使用#号注释接收者的号码以屏蔽接收者
vi /usr/local/nagios/fetion/phonelist.txt
# Phone NO. 1
138xxxxxxxx
# Phone NO. 2
158xxxxxxxx
# 修改飞信目录权限(否则运行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
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$."
       }

定义联系人模板
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 - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
        }
  
定义联系人及联系人组
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                           
alert@xxxxxx.com
        }
define contactgroup{
        contactgroup_name       admins-sendmsg
        alias                   BaiHe Monitor
        members                 msgreceiver
        }

5.测试故障时是否能够触发短信报警
Nagios事件处理

我们可以使用事件处理来在任何人收到通知之前由Nagios来做一些前期故障修复。
事件处理会在下面情况触发:
1).主机或服务处于一个软态故障状态时
2).主机或服务初始进入一个硬态故障时
3).主机或服务从软态或硬态的故障状态中初始恢复时
通过在主配置文件(nagios.cfg)中设置enable_event_handlers=1来打开全局事件处理,特定主机的和服务的事件处理可用主机和服务对象里的event_handler_enabled域来开关。如果全局的enable_event_handlers域是关闭的,那么特定主机的和服务的事件处理也不会运行。
事件处理命令可以用 shell 或是perl脚本,脚本中应该处理以下宏:
对服务的:$SERVICESTATE$、$SERVICESTATETYPE$和$SERVICEATTEMPT$;
对主机的:$HOSTSTATE$、$HOSTSTATETYPE$和$HOSTATTEMPT$。
脚本须检测这些作为命令行参数传入的值,并采取必要动作来处理这些值。
事件处理命令通常是与运行于本机上的Nagios程序的权限是相同的(下面例子中Nagios服务是以nagios用户运行的)。这可能会有问题,如果你想写成一个用于系统服务重启的命令,它需要有root权限才能执行一系列命令与任务。你或许会尝使用sudo命令来实现它。

本例通过Nagios检测远程机器上的MySQL服务,当服务出现问题时通过Nagios的事件处理逻辑来重启远程机器上的MySQL服务。
1.配置在Nagios服务器(192.168.0.200)上无密码登录远程机器(MySQL服务运行在上面-192.168.0.210)
[root@nagios ~]# su - nagios
[nagios@nagios ~]$ ssh-keygen -t rsa
# 下面一直回车,不要设置密码
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nagios/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nagios/.ssh/id_rsa.
Your public key has been saved in /home/nagios/.ssh/id_rsa.pub.
The key fingerprint is:
d2:82:61:12:53:f9:53:75:77:8d:32:c0:ca:c8:20:60 
nagios@nagios.itech.com
# 将生成的密钥拷贝到要远程登录的机器上
[nagios@nagios ~]$ scp .ssh/id_rsa.pub 192.168.0.210:/home/nagios/
[email=nagios@192.168.0.210%27s] nagios@192.168.0.210's [/email]
password:
id_rsa.pub                                                        100%  233     0.2KB/s   00:00
# 在要远程登录的机器上配置公钥
[nagios@nagios ~]$ ssh 
nagios@192.168.0.210
[email=nagios@192.168.0.210%27s] nagios@192.168.0.210's [/email]
password:
Last login: Sat Nov 29 22:30:55 2008 from 192.168.0.200
[nagios@nagios1 ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[nagios@nagios1 ~]$ chmod 600 .ssh/authorized_keys
[nagios@nagios1 ~]$ exit
logout
Connection to 192.168.0.210 closed.
# 测试无密码登录
[nagios@nagios ~]$ ssh 
nagios@192.168.0.210
Last login: Sat Nov 29 22:35:27 2008 from 192.168.0.200

2.在远程机器上配置sudo
使nagios用户可以以root身份运行/usr/local/nagios/libexec/eventhandlers/restart-mysql脚本
[root@MySQL ~]# visudo
nagios  ALL=(root) NOPASSWD:/usr/local/nagios/libexec/eventhandlers/restart-mysql
3.在远程机器上编写MySQL重启脚本
[root@MySQL ~]# vi /usr/local/nagios/libexec/eventhandlers/restart-mysql
#!/bin/sh
#
# Event handler script for restarting the MySQL server on the remote machine
#
# Note: This script will only restart the MySQL server if the service is
#       retried 2 times (in a "soft" state) or if the web service somehow
#       manages to fall into a "hard" error state.
#
#
# What state is the MySQL service in?
case "$1" in
OK)
        ;;
WARNING)
        ;;
UNKNOWN)
        ;;
CRITICAL)
        # Is this a "soft" or a "hard" state?
        case "$2" in
        SOFT)
                # What check attempt are we on?  We don't want to restart the MySQL server on the first
                # check, because it may just be a fluke!
                case "$3" in
                2)
                        echo -n "Restarting MySQL service..."
                        /sbin/service mysqld restart
                        ;;
                        esac
                ;;
        HARD)
                echo -n "Restarting MySQL service..."
                /sbin/service mysqld restart
                ;;
        esac
        ;;
esac
exit 0
上面的脚本只会在MySQL处于软状态,且第二次检查出现故障时或者进入硬状态时重启MySQL。
4.配置Nagios服务器上的配置文件
[root@nagios ~]# cd /usr/local/nagios/etc/
# 检查全局事件处理是否打开
[root@nagios etc]# vi nagios.cfg
enable_event_handlers=1
# 在命令配置文件中定义重启MySQL的命令
[root@nagios etc]# vi objects/commands.cfg
# restart the service on a remote server
define command{
        command_name restart-mysql
        command_line /usr/bin/ssh 
[email=nagios@$HOSTADDRESS$] nagios@$HOSTADDRESS$ [/email]
"sudo /usr/local/nagios/libexec/eventhandlers/restart-mysql $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$"
        }
  
# 配置主机监控文件
[root@nagios etc]# vi servers/mysql.cfg
# 省略主机定义和其他服务定义
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       MySQL
        service_description             MySQL
        check_command                   check_nrpe!check_mysql
        notifications_enabled           1
        event_handler_enabled           1
        event_handler                   restart-mysql
        }

这个脚本理论上在服务转入硬态故障之前可以重启MySQL服务以修复故障,这里包含了首次重启没有成功的情况。须注意的是事件处理将只是第一次进入硬态紧急状态时才会被触发,这将阻止Nagios在服务一直处于硬态故障状态时反复地重启MySQL服务.



本文转自holy2009 51CTO博客,原文链接:http://blog.51cto.com/holy2010/335049

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
Linux 网络安全 数据安全/隐私保护
如何在 VM 虚拟机中安装 CentOS Linux 9 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 CentOS Linux 9 操作系统保姆级教程(附链接)
161 0
|
2月前
|
关系型数据库 MySQL Linux
centos7.0环境下安装MySql_8.0.12
centos7.0环境下安装MySql_8.0.12
|
1月前
|
存储 JavaScript Linux
Linux环境下安装nmp(Centos环境)保姆级教学 一步到位
Linux环境下安装nmp(Centos环境)保姆级教学 一步到位
|
1天前
|
Linux
centos 6.5安装yum
centos 6.5安装yum
10 0
|
1天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置PXE服务
PXE是Intel开发的预启动执行环境,允许工作站通过网络从远程服务器启动操作系统。它依赖DHCP分配IP,DNS服务分配主机名,TFTP提供引导程序,HTTP/FTP/NFS提供安装源。要部署PXE服务器,需关闭selinux和防火墙,安装dhcpd、httpd、tftp、xinetd及相关服务,配置引导文件和Centos7安装源。最后,通过syslinux安装引导文件,并创建pxelinux.cfg/default配置文件来定义启动参数。
5 0
|
1天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置postfix服务
安装CentOS7的Postfix和Dovecot,配置Postfix的`main.cf`文件,包括修改完全域名、允许所有IP、启用邮箱等。然后,配置Dovecot的多个配置文件以启用auth服务和调整相关设置。重启Postfix和Dovecot,设置开机自启,并关闭防火墙进行测试。最后,创建邮箱账户并在Windows邮箱客户端中添加账户设置。
9 0
|
1天前
|
Linux 网络安全
Centos6.5安装并配置NFS服务
该内容描述了在Linux系统中设置NFS服务的步骤。首先挂载yum源,然后安装NFS服务,并编辑配置文件。接着,重启rpcbind和NFS服务,可能需要重复此过程以解决初始可能出现的问题。此外,关闭防火墙策略,并再次重启服务。最终,根目录被共享,特定IP网段被允许访问。
7 0
|
2天前
|
存储 Linux 网络安全
centos7使用yum网络安装
这些是使用Yum进行网络安装的基本步骤。根据你的需求,你可以重复步骤3和4来安装其他软件包。请注意,执行Yum操作需要root或具有sudo权限的用户。
14 1
|
8天前
|
关系型数据库 MySQL Linux
centos7安装mysql-带网盘安装包
centos7安装mysql-带网盘安装包
53 2
|
15天前
|
存储 Linux Shell
centos 部署docker容器 安装 、基本使用方法(一)
centos 部署docker容器 安装 、基本使用方法(一)
27 0