nagios监控系统——实战篇

一、nagios简介

Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设备,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。Nagios所需要的运行条件是机器必须可以运行Linux(或是Unix变种)并且有C语言编译器。你必须正确地配置TCP/IP协议栈以使大多数的服务检测可以通过网络得以进行。你需要但并非必须正确地配置Nagios里的CGIs程序,而一旦你要使用CGI程序时,你必须要安装以下这些软件...一个WEB服务(最好是Apache)

Nagios 可以监控的功能有:

1、监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);

2、监控主机资源(处理器负荷、磁盘利用率等);

3、简单地插件设计使得用户可以方便地扩展自己服务的检测方法;

4、并行服务检查机制;

5、具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;

6、当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);

7、可以定义一些处理程序,使之能够在服务或者主机发生故障时起到预防作用;

8、自动的日志滚动功能;

9、可以支持并实现对主机的冗余监控;

10、可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等;

11、可以通过手机查看系统监控信息;

12、可指定自定义的事件处理控制器;

二、环境

主机 os IP地址 主要软件
nagios centos6.5 192.168.10.1

nagios-4.0.1.tar.gz

nagios-plugins-1.5.tar.gz

nrpe-2.15.tar.gz

MySQL/web centos6.5 192.168.10.2

nagios-plugins-1.5.tar.gz

nrpe-2.15.tar.gz

 

三、实战

1)为每台主机配置ip和关闭防火墙

2)创建nagios用户和用户组

useradd -s /sbin/nologin nagios
mkdir /usr/local/nagios
chown -R nagios:nagios /usr/local/nagios

3)安装nagios前先安装nagios的依赖包

yum -y install httpd php  mysql-devel openssl openssl-devel
tar zxf nagios-...  -C  /usr/src
cd /usr/src/nagios-4...
./configure --prefix=/usr/local/nagios && make all && make install && make install-init && make install-commandmode && make install-config && make install-webconf

注:
    make install      //安装主程序,CGI和HTML文件 
    make install-init  //在/etc/rc.d/init.d安装启动脚本 
    make install-commandmode  //配置目录权限 
    make install-config  //安装示例配置文件 
    make install-webconf  //安装nagios的web接口,会在/etc/httpd/conf.d目录中创建nagios.conf文件。

安装完成之后会在/usr/local/nagios目录下产生6个目录。

    bin:nagios执行程序所在的目录,nagios文件即为主程序。 
    etc:nagios配置文件目录,当make install-config完以后etc下面就会出现默认的配置文件。 
    sbin:nagios CGI文件所在目录,这里存放的是一些外部命令执行程序。 
    share:nagios网页文件目录,存放一些html文件。 
    var:nagios日志文件、pid等文件目录。 
    Libexec:系统默认插件的存储位置
#添加nagios系统服务
chkconfig --add nagios
chkconfig --level 35 nagios on

4)安装nagios插件(监控功能通过插件完成)和   安装nrpe(为了监控远程服务器)

安装nagios-plugins
tar zxf nagios-plugins
cd /usr/src/nagios-plugins..
./configure --prefix=/usr/local/nagios && make && make install
安装nrpe
tar zxf nrpe-2...
cd /usr/src/nrpe-2..	
./configure && make all && make install-plugin

5)给web授权


vim /etc/httpd/conf/httpd.conf
:r  /etc/httpd/conf.d/nagios.conf

/usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin   #添加一个访问nagios页面的授权用户
service httpd start 
service nagios start

注:到这可以访问nagios页面了。(http://192.168.10.1/nagios)

6)配置nagios

vim /usr/local/nagios/etc/nagios.cgf   #主配置文件,定义各种配置文件的名称和位置
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg
cfg_dir=/usr/local/nagios/etc/conf
mkdir /usr/local/nagios/etc/conf  #创建监控主机配置文件目录

1) vim /usr/local/nagios/etc/objects/commands.cfg  #命令配置文件,定义各种命令格式,以备其他文件调用

define command {
			command_name	check_nrpe
			command_line	$USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$    
}

2) vim /usr/local/nagios/etc/objects/contacts.cfg #联系人和组,发邮件等告警信息时可以调用
define contact {
    contact_name                    zm
    alias                           zm        
	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                 root; 
     }
define contactgroup {
    contactgroup_name       zm
    alias                   zm
   	members                 zm       
}

3)vim /usr/local/nagios/etc/objects/hostgroups.cfg #定义监控的主机(组),需手动创建。
   define hostgroup {
			hostgroup_name webmysql
			alias	webmysql
			members 192.168.1.20
		}

4) vim /usr/local/nagios/etc/conf/192.168.10.2.cfg #
    define host {
host_name 192.168.10.2 #被监控主机的名称,最好别带空格
alias 192.168.10.2
address 192.168.10.2   #被监控主机的IP地址  
check_command check-host-alive
max_check_attempts 5
check_period 24x7
contact_groups zm
notification_period 24x7
notification_options d,u,r	#指定什么情况下提醒 
}

define service {
host_name 192.168.10.2
service_description check-host-alive
check_command check-host-alive
max_check_attempts 3     #检查失败后重试的次数
normal_check_interval 2  #正常检测间隔时间
retry_check_interval 2   #重试检测间隔时间
check_period 24x7        #检查的时间段24x7,同样来自timeperiods.cfg中定义 
notification_interval 10 #提醒的间隔,每隔10秒提醒一次
notification_period 24x7 #提醒的周期, 24x7,同样来自timeperiods.cfg中定义
notification_options w,u,c,r	
contact_groups zm	     #联系人组,上面在contactgroups.cfg中定义的zm
}

define service {	
host_name 192.168.10.2	
service_description check-procs
check_command check_nrpe!check_total_procs
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7	
notification_interval 10
notification_period 24x7	
notification_options w,u,c,r	
contact_groups zm	
}

define service {
    host_name 192.168.10.2
	service_description check-load
    check_command check_nrpe!check_load
	max_check_attempts 3	
    normal_check_interval 2
	retry_check_interval 2
	check_period 24x7
    notification_interval 10
    notification_period 24x7
    notification_options w,u,c,r
    contact_groups zm
	}

#重启nagios服务器
service  nagios reload
#如果你开启了selinux 需要配置如下
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/ 
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/

7)在被监控端配置

yum -y install openssl openssl-devel
useradd nagios -s /sbin/nologin
tar zxf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure --prefix=/usr/local/nagios && make && make install
chown -R nagios:nagios /usr/local/nagios
tar zxf nrpe-2.15.tar.gz
cd nrpe-1.15
./configure --prefix=/usr/local/nagios && make all && make install-plugin && make install-daemon && make install-daemon-config
vim /usr/local/nagios/etc/nrpe.cfg
 allowed_hosts=127.0.0.1,192.168.10.1  #找到这一行添加ip
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d  #启动nrpe


在监控服务器上测试nrpe运行是否正常。
/usr/local/nagios/libexec/check_nrpe -H 192.168.10.2

9)访问即可

10)配置nagios监控飞信报警。(本次不涉及飞信报警)

tar zxf library_linux.tar.gz
mv libACE* libcryto.so.0.9.8 libssl.so.0.9.8 /usr/lib
tar zxf fetion20091117-linux.tar.gz
cp fx/* /usr/local/fetion
vim /etc/ld.so.conf    
   Include ld.so.conf.d/*.conf   #添加该信息

ldconfig
mv fetion /usr/local/fetion/   #使用资源中的飞信文件覆盖掉解压出的飞信文件
cd /usr/local/fetion
/usr/local/fetion/fetion -mobile=151**** --pwd=**** --to=151**** --msg-utf8="testing." -debug   #测试短信是否能够成功发出

注:   --mobile=手机号      #以手机号登陆飞信
      --sid=飞信号          #以飞信号来登陆飞信
      --pwd=密码            #登陆飞信的密码
      --to=手机号           #接收者的手机号,支持多个号码,用逗号隔开即可
      --msg-utf8=字符集     #指定发送消息格式采用utf8编码
      --debug              #显示调试信息

/etc/init.d/nagios  restart 

 

你可能感兴趣的:(日常笔记)