Chinaunix首页 | 论坛 | 博客
  • 博客访问: 394214
  • 博文数量: 89
  • 博客积分: 1386
  • 博客等级: 中尉
  • 技术积分: 827
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-20 10:32
个人简介

12年 linux 系统运维工程师,网络架构设计、优化,故障处理。

文章分类

全部博文(89)

文章存档

2019年(1)

2015年(2)

2014年(11)

2013年(14)

2012年(2)

2011年(59)

分类: 系统运维

2011-11-15 16:55:48

 网络监控软件Nagios,最大的好处是可以发故障报警短信—只要Nagios监控的对象发生故障,系统就会自动发送短信到手机上。下面摘录Nagios官方网站的描述: Nagios is an open source host, service and network monitoring program. Who uses it? Lots of people, including many big companies and organizations:Nagios是一个用来监控主机、服务和网络的开放源码软件,很多大的公司或组织都在使用它。

(一)  安装所需软件
 
一、安装Nagios
Nagios 可以运行在各种版本的linux及主流的unix环境,我试过的环境有Redhat linux,Centos,Debian等。在实际的运维中,我是以centos 4来部署nagios的。安装完操作系统之后,需要把多余的服务都关掉,只留sshd这个服务。然后用wget下载源码包nagios- 2.6.tar.gz和httpd-2.2.0.tar.gz。接下来先分别安装软件,过程如下:
先建个nagios用户和组 useradd nagios
1、  解压 tar zxvf nagios-2.6.tar.gz
2、  配置 cd nagios ; ./configure –prefix=/usr/local/nagios
3、  编译 make all

4、安装nagios。与别的软件安装稍有不同,nagios的安装要好几步才能完成。第一步执行make install安装主要的程序、CGI及HTML文件,第二步执行 make install-commandmode 给外部命令访问nagios配置文件的权限,第三步执行 make install-config 把配置文件的例子复制到nagios的安装目录。按照安装向导的提示,其实这里还有一个 make install-init的步骤,它的作用是把nagios做成一个运行脚本,使nagios随系统开机启动,这是一个很方便的措施。
5、 验证程序是否被正确安装。切换目录到安装路径(这里是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。后表是五个目录功能的简要说明:
 
bin
Nagios执行程序所在目录,这个目录只有一个文件nagios
etc
Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件
sbin
Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share
Nagios网页文件所在的目录
Var
Nagios日志文件、spid 等文件所在的目录
      
二、安装nagios的插件
         没有插件,nagios将什么作用也没有,插件也是nagios扩展功能的强大武器,除了下载常用的插件外,我们还可以根据实际要求编写自己的插件。Nagios的插件nagios-plugins-1.4.5在上可以找到,接着我们用wget下载它。注意:插件与nagios之间的版本关联不大,不一定非得用nagios-plugins-1.4.5这个版本。下载完成后,安装它是很简单的:
先执行配置 ./configure –prefix=/usr/local/nagios ,
接着编译安装 make ; make install即可。
这里需要说明一下的是在配置过程指定的安装路径是/usr/local/nagios,而不是/usr/local/nagios- plus,安装完成后,将在目录/usr/local/nagios生成目录libexec(里面有很多文件),这正是nagios所需要的。

三、安装web服务器apache
Web服务不是nagios所必须的,但是如果nagios没有web,查看监控对象的状态将是非常费事和没有趣味的事情(只有通过查看nagios的日志来判断状态)。我不愿干特无聊的事,所以就花少许时间把web安装一下。
在unix/linux世界,apache是web服务器的首选对象,其下载网站为  。建议下载源码。因为我们不需要很复杂的web功能,因此简单的执行一下几个步骤就可以正确的把apache安装到系统:
1、   解包、配置:tar zxvf httpd-2.2.0.tar.gz ; cd httpd-2.2.0 ; ./configure   –prefix=/usr/local/apache 。
2、   编译安装: make ; make install 。
安装完成后,执行命令 ./usr/local/apache/bin/apachectl –t 检查一下apache是否正确安装

(二)、配置前的处理
最主要的工作是创建nagios用户及其组,让nagios的运行用户为nagios而不是root。再把目录/usr/local/nagios的属主设置为nagios,以保证系统的安全。Nagios可以以root用户运行,但并不推荐这样做。用下面的步骤来完成上述过程:
1、添加系统帐户useradd nagios 就很容易的把用户和组nagios添加到系统。有的类型的linux发行版添加用户和组要麻烦一些-需要手动添加组,然后再执行 useradd –g nagios nagios这样的操作。在实际的运用场景,nagios用户并没有必要作为系统用户来登录linux系统,因此可以不必设置nagios的用户密码,甚至可以把nagios用户的登录shell设置成/bin/false。
2、更改目录属组:chown –R nagios.nagios /usr/local/nagios 。
3、 sendmail。看看sendmail是否正常运行?我们需要使用sendmail来发送故障报警信息,所以这个包必须能够正常工作。Sendmail 分为服务器和客户端两部分,有2种发送报警邮件的方式:(1)nagios所在的机器通过sendmail客户端程序把邮件发送到专门的邮件服务器,再由邮件服务器把消息发送到用户邮箱。(2)邮件客户端和服务器端就用nagios所在系统sendmail。第一种方式用起来非常规范,但更麻烦,例如需要做地址解析、修改邮件服务器的配置;另外还有一个问题-它还依赖别的系统,增加了故障点和复杂度。第二种方法十分简单,只需启动sendmail服务即可,而且它不再依赖于别的系统和服务。在我工作的实际场景,这两种方法都使用,用专门的邮件服务器会有发送延迟的情况(因为邮件服务器要处理很多其他用户邮件的收发);而直接用sendmail做服务器和客户端就异常简单和方便了。非常幸运的是,几乎所有的linux/unix发行版都默认安装 sendmail,费了这么多笔墨,其实就做一个动作-把sendmail服务运行起来。

(二)、配置
配置是nagios最复杂的部分,它涉及到多个文件的配置,为了方便描述,这里逐个的进行配置。
 
一、apache 配置。
我们分两个步骤来完成这个配置。第一步是修改apache的配置文件httpd.conf,这里的文件路径是 /usr/local/apache/conf/httpd.conf 。把apache的运行用户[1]和运行组改成nagios,往下把下面的行追加到文件httpd.conf的末尾:
 
#setting for nagios
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
     //Cgi文件所在目录
    AuthType Basic
    Options ExecCGI
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "Nagios Access"
    AuthUserFile /usr/local/nagios/etc/htpasswd  //验证文件路径
    Require valid-user
 
Alias /nagios /usr/local/nagios/share
   //nagios页面文件目录
    AuthType Basic
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "nagios Access"
    AuthUserFile /usr/local/nagios/etc/htpasswd  //验证文件路径
    Require valid-user
上述文本块的作用是对nagios的目录进行用户验证,只有合法的授权用户才可以访问nagios的页面文件。第二步是生成用户验证文件:
只要执行命令 /usr/local/apache/bin/htpasswd  -c /usr/local/nagios/etc/htpasswd 
chenwenming ,就会生成web的合法访问用户;命令交互执行,需要输入2次密码,然后就在文件/usr/local/nagios/etc/htpasswd 写入一行-第一个字段是刚生成的用户名,第二个是加密后的密码,如果还要添加更多的用户,执行命令 htpasswd 就不需要选项 “-c”,否则就会覆盖所有已经生成的行。
配置完成后,执行/usr/local/apache/bin/apachctl –t  检查apache配置文件是否有语法错误,无误后用/usr/local/apache/bin/apachctl start & 把apache启动,然后从另外的机器的浏览器输入nagios 的访问地址(如:),如果正常,将出现下图的登录验证窗口等待用户输入:
输入用htpasswd创建的用户名和密码测试一下,没有问题的话,进行下一步配置操作。
二、nagios配置
刚安装完成的nagios,其配置文件的目录是/usr/local/nagios/etc,下图是其etc目录的文件:


先把这些文件改名,如 cgi.cfg-sample改成cgi.cfg ,用命令cp cgi.cfg-sample cgi.cfg …依样把余下的几个*.cfg-sample都复制成*.cfg文件。从nagios2.6版开始,不用修改配置文件localhost.cfg就可以直接运行../bin/nagios –v nagios.cfg验证程序是否能正常运行(nagios2.5及以前版本的最小运行的配置文件是minimal.cfg,但需要修改这个文件多处才能验证成功)。当然,我们不能指望这个最小的配置文件能够满足实际的需求,因此,需要对现有的配置文件进行修改,其次增加自定义的一些配置文件。这里,我们分两步进行:先修改配置文件再增添自定义文件。
 
(一)   修改配置文件
Nagios的主配置文件是nagios.cfg,我们就从这个文件开始修改。用vi编辑nagios.cfg,注释行 #cfg_file=/usr/local/nagios/etc/localhost.cfg[2],然后把下面几行的注释去掉:
 
cfg_file=/usr/local/nagios/etc/contactgroups.cfg  //联系组配置文件路径
cfg_file=/usr/local/nagios/etc/contacts.cfg       //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/hostgroups.cfg     //主机组配置文件路径
cfg_file=/usr/local/nagios/etc/hosts.cfg          //主机配置文件路径
cfg_file=/usr/local/nagios/etc/services.cfg       //服务配置文件路径
cfg_file=/usr/local/nagios/etc/timeperiods.cfg    //监视时段配置文件路径
改check_external_commands =0为check_external_commands=1 .这行的作用是允许在web界面下执行重启nagios、停止主机/服务检查等操作。把command_check_interval的值从默认的1改成 command_check_interval=10s(根据自己的情况定这个命令检查时间间隔,不要太长也不要太短)。主配置文件要改的基本上就是这些,通过上面的修改,发现/usr/local/nagios/etc并没有文件hosts.cfg等一干文件,怎么办?稍后手动创建它们。
第二个要修改的配置文件是cgi.cfg,它的作用是控制相关cgi脚本。先确保use_authentication=1。曾看过不少的文章,都是建议把 use_authentication的值设置成”0”来取消验证,这是一个十分糟糕的想法。接下来修改default_user_name=sery ,再后面的修改在下表列出:
 
authorized_for_system_information=nagiosadmin,chenwenming
authorized_for_configuration_information=nagiosadmin,chenwenming
authorized_for_system_commands=chenwenming  //多个用户之间用逗号隔开
authorized_for_all_services=nagiosadmin,chenwenming
authorized_for_all_hosts=nagiosadmin,chenwenming
authorized_for_all_service_commands=nagiosadmin,chenwenming
authorized_for_all_host_commands=nagiosadmin,chenwenming
那么上述用户名打那里来的呢?是执行命令 /usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd chenwenming 所生成的,这个要注意,不能随便加没有存在的验证用户,为了安全起见,不要添加过多的验证用户。

(一)添加 commands.cfg里有以下代码 ,这样能达到报警发邮件。还有很多命令模板里有可以都添加上。

[root@ytlzsms objects]# more commands.cfg

# 'notify-host-by-email' command definition

define command{
        command_name host-notify-by-email
        command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState:$
HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n"
| /bin/mail -s "** $NOTIFICATIONTYPE$ Host Al
ert: $HOSTNAME$ is $HOSTSTATE$ **"
$CONTACTEMAIL$
        }

# 'notify-service-by-email' command definition

define command{
        command_name service-notify-by-email
        command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\
nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT
$"
| /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }

# 'check-host-alive' command definition

define command{
        command_name check-host-alive
        command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
        }

##上面这几行替换以前的配置


(二)增加新的配置文件
先创建简单的配置文件timeperiods.cfg,其内容如下:

定义监控时间

define timeperiod{
        timeperiod_name 24x7
        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
        }


第二个手动创建的配置文件是 contacts.cfg,其格式如下:


define contact {
        contact_name chenwenming
        alias system administrator
        service_notification_period 24x7
        host_notification_period 24x7
        service_notification_options w,u,c,r
        host_notification_options d,u,r
        service_notification_commands service-notify-by-email
        host_notification_commands host-notify-by-email
        email XXX@126.com,xxx@163.com   #
收报警邮件 这是用本地的sendmail发的,很多要检查是否有MX记录,就阻止了,但126的没有阻止我测试过.
        }


第三个手动创建的配置文件是contactgroups.cfg文件,这个文件是依照上一个文件contacts.cfg来的,contactgroups文件相对简单一些,其格式如下:
因为我的服务器分别在北京,广东,江苏,黑龙江 四个机房,所以定义了四个组方面管理。

define contactgroup {
        contactgroup_name BJ-servers
        alias system administrator group
        members chenwenming
}

define contactgroup {
        contactgroup_name hn-servers
        alias system administrator group
        members chenwenming
}

define contactgroup {
        contactgroup_name gd-servers
        alias system administrator group
        members chenwenming
}

define contactgroup {
        contactgroup_name hlj-servers
        alias system administrator group
        members chenwenming
}


配置文件hosts.cfg。下面是我定义的两个主机的基本样式:

定义2台主机,要添加主机依次添加

define host {
       host_name ns1
       alias 别名
       address ip地址1
       contact_groups BJ-servers #这是上面定义过的组
       check_command check-host-alive
       max_check_attempts 5
       notification_interval 10
       notification_period 24x7
       notification_options d,u,r
       }
 
define host {
       host_name 112
       alias
别名
       address ip地址2
       contact_groups BJ-servers
#这是上面定义过的组
       check_command check-host-alive
       max_check_attempts 5
       notification_interval 10
       notification_period 24x7
       notification_options d,u,r
       }


再一个重量级的配置文件是services.cfg,没有这个文件,什么监控也没用。下面给出一个样式文件:
这里的配置与hosts.cfg是一一对应的

define service {
        host_name ns1
        service_description check-host-alive
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups BJ-servers
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
        check_command check-host-alive
        }
define service {
        host_name 112
        service_description check-host-alive
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups BJ-servers
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
        check_command check-host-alive
        }


主机组配置文件hostgroups.cfg,这是一个可选的项目,它建立在文件hosts之上,其格式如下:
把hosts.cfg里添加的主机归类分组(也可以不要)

define hostgroup {
         hostgroup_name BJ-servers
         alias beijin IDC servers
         members ns1,112,116,118,149,156,101,192,123,119,114
         }

define hostgroup {
         hostgroup_name hn-servers
         alias hn IDC servers
         members 160,161,162,163,158,159
         }



运行程序/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg来检查所有配置文件的正确性。如果十分幸运的话,运行完毕将在输出尾部出现
Total Warnings: 0
Total Errors:   0
 
Things look okay - No serious problems were detected during the pre-flight check
这样的情况,大功告成;但我却没有这么幸运,修改了好多个地方才成功。
验证通过以后,就可以执行命令/usr/local/nagios/bin/nagios -d  /usr/local/nagios/etc/nagios.cfg 把nagios作为守护进程。
  三、验收 
用浏览器输入nagios所在服务器的ip及目录,如,再输验证所需的用户名和密码,就可点击页面右边的相关连接来查看各种状态。关掉某个被nagios监控主机的服务或者拔掉某个服务器的网线,等几分钟,点击超连接“Service Detail”观察页面状态看是否有红色的醒目的报警出现。
关收邮件是不够的,在网易上注册一个每个月10元的手机短信提醒功能,这样报警能够及时知道。

本人是参考田老师的然后结合自己服务器安装的 http://netsecurity.51cto.com/art/200706/48728.htm

 
巧用escalations限制Nagios报警次数 

如果服务器出现故障而未能及时的解决,Nagios就会不断的发送告警信息,实在令人头疼。现在用如下方法可以解决Nagios的告警次数问题。
hosts.cfg
define host{
        host_name                       WWW-Server
        alias                                 WWW-Server
        address                            193.1.16.100
        check_command                check-host-alive
        max_check_attempts         5
        check_period                     24x7
        notification_interval           10
        notification_period             24x7
        notification_options            d,u,r
        notifications_enabled          1
        contact_groups                 chengnan
        }
Services.cfg
define service{
        host_name                        WWW-Server
        service_description             Check_HTTP
        check_command                 check_http
        max_check_attempts          10
        normal_check_interval         3
        retry_check_interval            2
        check_period                      24x7
        notification_interval             5
        notification_period              24x7
        notification_options            w,u,c,r
        contact_groups                  admin
        }
       
define service{
        host_name                      WWW-Server
        service_description           Check_Jetty
        check_command               check_tcp!8080
        max_check_attempts        10
        normal_check_interval       3
        retry_check_interval          2
        check_period                    24x7
        notification_interval           5
        notification_period            24x7
        notification_options          w,u,c,r
        contact_groups                admin
        }
Contacts.cfg
define contact{
        contact_name                          chengnan
        alias                                        chengnan
        service_notification_period         24x7
        host_notification_period             24x7
        service_notification_options        w,u,c,r
        host_notification_options           d,u,r
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                                                   //手机邮箱
        }
define contactgroup{
        contactgroup_name       chengnan
        alias                             Nagios Administrators
        members                      chengnan
        }
除此之外再定义一个联系人
define contact{
        contact_name                           chengnan_cor
        alias                                         chengnan_cor
        service_notification_period         24x7
        host_notification_period             24x7
        service_notification_options        w,u,c,r
        host_notification_options           d,u,r
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                                             //公司邮箱
        }
define contactgroup{
        contactgroup_name       cwm
        alias                             cwm
        members                      chengnan_cor
        }
然后创建一个配置文件:
vi escalations.cfg
escalations有自动调整;不断增加; 逐步上升等意思,本身配置文件的功能是当服务在某一告警次数前没有恢复,告警频率周期将会缩短,同时将告警信息发送至指定联系人。
其内容为:
define hostescalation{
host_name                192.168.0.132            //被监控主机名称,与Hosts.cfg中一致
first_notification         4                            // 第n条信息起,改变频率间隔
last_notification          0                            // 第n条信息起,恢复频率间隔
notification_interval    30                          // 通知间隔(分)
contact_groups          cwn               
}
说明:从第4条告警信息起至服务器恢复前,告警信息发送至sysadmin组下的联系人,告警间隔为30分钟1条信息。
define serviceescalation{
host_name                192.168.0.132                           //被监控主机名称,与Hosts.cfg中一致
service_description     Check_HTTP       //被监控服务名称,与Services.cfg中一致
first_notification         4
last_notification          0
notification_interval    30
contact_groups          cwm
}
保存
修改nagios.cfg
vi nagios.cfg
添加:
cfg_file=/usr/local/nagios/etc/objects/escalations.cfg
检查nagios配置文件是否正确
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
重新启动nagios服务:
service nagios restart
测试:
服务器启动后停掉被监控测试机的相应服务,确认告警信息是否按照设置发送至不同信箱
后记
escalations
这个功能官方给的定义是notification的扩充,使notification变得更加灵活,方便。文中我使用的方法算是耍了个小聪明,将第四条告
警信息后的所有信息全部发送至我公司邮箱直至服务器恢复(recovery的信息还是会发送至手机的),从而实现限制告警信息发送至手机的条数。
 

以下是官方给出服务器出现问题的一个例子:

Notification escalation definitions can have notification ranges that overlap. Take the following example:

define serviceescalation{
host_name webserver
service_description HTTP
first_notification 3
last_notification 5
notification_interval 20
contact_groups nt-admins,managers
}

define serviceescalation{
host_name webserver
service_description HTTP
first_notification 4
last_notification 0
notification_interval 30
contact_groups on-call-support
}

In the example above:

  • The nt-admins and managers contact groups get notified on the third notification
  • All three contact groups get notified on the fourth and fifth notifications
  • Only the on-call-support contact group gets notified on the sixth (or higher) notification
escalations的官方文档给的说明已经很清楚了,特别是这个例子。

也就是说如果我想让手机收到前三条信息,但是第4以及一直到服务器回复正常状态之前的信息发送到邮件里面可以这么做
define serviceescalation{
host_name webserver
service_description HTTP
first_notification 0
last_notification 3
notification_interval 20
contact_groups sms_contract
}

define serviceescalation{
host_name webserver
service_description HTTP
first_notification 4
last_notification 0
notification_interval 30
contact_groups mail_contract
}这样的话 就能实现上述的功能了。
……………………………………………………………………………………………………………………………………
官方给出的一个recovery例子:

Recovery Notifications

Recovery notifications are slightly different than problem notifications when it comes to escalations. Take the following example:

define serviceescalation{
host_name webserver
service_description HTTP
first_notification 0
last_notification 3
notification_interval 20
contact_groups sms_contract
}

define serviceescalation{
host_name webserver
service_description HTTP
first_notification 4
last_notification 0
notification_interval 30
contact_groups mail_contract
}

If, after three problem notifications, a recovery notification is sent out for the service, who gets notified? The recovery is actually the fourth notification that gets sent out. However, the escalation code is smart enough to realize that only those people who were notified about the problem on the third notification should be notified about the recovery. In this case, the nt-admins and managers contact groups would be notified of the recovery.

阅读(1034) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~