重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
CentOS下Nagios的安装与配置
成都创新互联是一家专注于成都网站制作、做网站、外贸营销网站建设与策划设计,平山网站建设哪家好?成都创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:平山等地区。平山做网站价格咨询:028-86922220
Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。
Nagios可以识别4种状态返回信息,即 0(OK)表示状态正常/绿色、1(WARNING)表示出现警告/***、2(CRITICAL)表示出现非常严重的错误/红色、3(UNKNOWN)表示未知错误/深***。
一、环境
Host NameOSIPSoftware
Nagios-ServerCentOS release 6.3 (Final)192.168.1.108Apache、Php、Nagios、nagios-plugins
Nagios-LinuxCentOS release 5.8 (Final)192.168.1.111nagios-plugins、nrpe
Nagios-WindowsWindows XP192.168.1.113 NSClient++
防火墙已关闭/iptables: Firewall is not running.
SELINUX=disabled
二、Nagios服务端安装
基础支持套件:gcc glibc glibc-common gd gd-devel xinetd openssl-devel
2.1创建nagios用户和用户组
useradd -s /sbin/nologin nagios
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios
2.2编译安装Nagios
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.3.tar.gz
tar zxvf nagios-3.4.3.tar.gz
cd nagios
./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-commandmode
make install-config
cd
chkconfig --add nagios
chkconfig --level 35 nagios on
chkconfig --list nagios
2.3安装Nagios 插件
wget https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz
or
wget http://nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz
tar xzvf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure --prefix=/usr/local/nagios
make && make install
2.4安装配置Apache、PHP
yum -y install httpd php //如果已经安装此步骤略过;
2.4.1配置apache主配置文件
vim /etc/httpd/conf/httpd.conf
找到
User apache
Group apache
更改为
User nagios
Group nagios
找到
DirectoryIndex index.html
更改为
DirectoryIndex index.html index.php
在下一行新增如下内容:
AddType application/x-httpd-php .php
为了安全起见,一般情况下要让nagios 的web 监控页面必须经过授权才能访问,这需要增加验证配置,即在httpd.conf 文件最后添加如下信息:
#setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
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"
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
保存退出;
2.4.2创建apache目录验证文件及启动apache服务
htpasswd -c /usr/local/nagios/etc/htpasswd david
cat /usr/local/nagios/etc/htpasswd
service httpd restart
到这里 Nagios 安装及基本配置已经完成了;测试访问一下:
http://ip地址/nagios
用户名
密码
2.5配置Nagios
2.5.1配置联系人信息
cp /usr/local/nagios/etc/objects/contacts.cfg /usr/local/nagios/etc/objects/contacts.cfg.bak
vim /usr/local/nagios/etc/objects/contacts.cfg
更改
contact_name david
email 744120325@qq.com
contactgroup_name ts
members david
2.5.2配置主机信息
touch /usr/local/nagios/etc/objects/hosts.cfg
vim /usr/local/nagios/etc/objects/hosts.cfg
新增如下内容:
define host{
use linux-server
host_name hostname01
alias hostname01
address 192.168.1.111
}
define host{
use linux-server
host_name hostname02
alias hostname02
address 192.168.1.112
}
define hostgroup{
hostgroup_name bsmart-servers
alias bsmart servers
members hostname01,hostname02
}
--------------------------------------
vim /etc/hosts
新增
ip地址 hostname //192.168.122.128 localhost
--------------------------------------
由于本实验也测试监控安装Nagios服务端的这台服务器,所以需要配置 localhost.cfg 文件;
cp /usr/local/nagios/etc/objects/localhost.cfg /usr/local/nagios/etc/objects/localhost.cfg.bak
vim /usr/local/nagios/etc/objects/localhost.cfg
在文件尾新增如下内容:
define service{
use local-service ; Name of service template to use
host_name localhost
service_description MySQL
check_command check_mysql
notifications_enabled 0
}
define service{
use local-service ; Name of service template to use
host_name localhost
service_description FTP
check_command check_ftp
notifications_enabled 0
}
2.5.3配置时间信息
cp /usr/local/nagios/etc/objects/timeperiods.cfg /usr/local/nagios/etc/objects/timeperiods.cfg.bak
vim /usr/local/nagios/etc/objects/timeperiods.cfg
根据需要配置,一般不需要更改,默认即可;
2.5.4配置服务信息
touch /usr/local/nagios/etc/objects/services.cfg
vim /usr/local/nagios/etc/objects/services.cfg
新增如下内容:
define service{
use local-service
host_name localhost
service_description check-host-alive
check_command check-host-alive
}
2.5.5配置命令信息
cp /usr/local/nagios/etc/objects/commands.cfg /usr/local/nagios/etc/objects/commands.cfg.bak
vim /usr/local/nagios/etc/objects/commands.cfg
新增如下内容:
# 'check_mysql' command definition
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ $ARG1$
}
2.5.6配置模板配置信息
nagios主要用于监控主机资源以及服务,在nagios配置中称为对象,为了不必重复定义一些监控对象,Nagios引入了一个模板配置文件,将一些共性的属性定义成模板,以便于多次引用。这就是templates.cfg的作用。
更改
contact_groups ts
2.5.7配置CGI信息
想通过web监控界面执行cgi脚本,例如重启nagios进程、关闭nagios通知、停止nagios主机检测等;需要配置,否则默认即可;
cp /usr/local/nagios/etc/cgi.cfg /usr/local/nagios/etc/cgi.cfg.bak
vim /usr/local/nagios/etc/cgi.cfg
更改
default_user_name=david
authorized_for_system_information=nagiosadmin,david
authorized_for_configuration_information=nagiosadmin,david
authorized_for_system_commands=david
authorized_for_all_services=nagiosadmin,david
authorized_for_all_hosts=nagiosadmin,david
authorized_for_all_service_commands=nagiosadmin,david
authorized_for_all_host_commands=nagiosadmin,david
2.5.8配置Nagios主配置文件信息
cp /usr/local/nagios/etc/nagios.cfg /usr/local/nagios/etc/nagios.cfg.bak
vim /usr/local/nagios/etc/nagios.cfg
新增如下内容:
cfg_file=/usr/local/nagios/etc/objects/services.cfg
保存退出即可;
2.5.9验证Nagios配置文件的正确性
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果最后显示出来如下信息,即表是没问题:
Total Warnings: 0
Total Errors: 0
---------------------------------------
2.6安装NRPE
cd
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
tar zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
make install-plugin
-------------------------
#/usr/local/nagios/libexec/check_nrpe -H 192.168.1.235 //测试一下监控机使用check_nrpe 与被监控机运行的nrpe daemon之间的通信;
-------------------------
vim //usr/local/nagios/etc/objects/commands.cfg
新增如下内容:
# 'check_nrpe' command definition
define command{
command_name check_nrpe # 定义命令名称为check_nrpe,在services.cfg中要使用这个名称.
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ #这是定义实际运行的插件程序.
# 这个命令行的书写要完全按照check_nrpe这个命令的用法,不知道用法的就用check_nrpe –h查看.
}
2.7重启各相关的服务
service httpd restart
service nagios restart
service postfix restart
三、客户端(被监控机)安装配置(Linux)
基础支持套件:gcc glibc glibc-common gd gd-devel xinetd openssl-devel
3.1新建用户
useradd nagios
3.2安装Nagios插件
cd
wget https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz
or
wget http://nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz
tar xzvf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure --prefix=/usr/local/nagios
make && make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec
3.3安装NRPE
cd
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
tar zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
vim /etc/xinetd.d/nrpe
在only_from 后增加监控主机的IP地址;
vim /etc/services
增加NRPE服务:nrpe 5666/tcp
service xinetd restart
netstat -an | grep 5666 //查看NRPE 是否已经启动;
/usr/local/nagios/libexec/check_nrpe -H localhost //测试NRPE 是否工作正常;
注:为了后面工作的顺利进行,注意本地防火墙要打开5666能让外部的监控机访问。
3.4定义对Nagios-Linux 主机的监控
下面就可以在services.cfg(服务器上的配置文件)中定义对Nagios-Linux 主机的监控了:
define service{
use local-service
host_name Nagios-Linux
service_description Current Load
check_command check_nrpe!check_load
}
define service{
use local-service
host_name Nagios-Linux
service_description Check Disk
check_command check_nrpe!check_disk
}
define service{
use local-service
host_name Nagios-Linux
service_description Total Processes
check_command check_nrpe!check_total_procs
}
define service{
use local-service
host_name Nagios-Linux
service_description Current Users
check_command check_nrpe!check_users
}
define service{
use local-service
host_name Nagios-Linux
service_description Check Zombie Procs
check_command check_nrpe!check_zombie_procs
}
---------------------------------------------------------
如果还需要监控其它服务的,请在上面的文件增加相应的服务项以及在被监控机上相应增加,如:
例:增加HTTP监控:
监控机:
vim /usr/local/nagios/etc/objects/services.cfg
新增如下内容:
define service{
use local-service
host_name Nagios-Linux
service_description HTTP
check_command check_nrpe!check_http
}
被监控机:
vim /usr/local/nagios/etc/nrpe.cfg
新增如下内容:
command[check_http]=/usr/local/nagios/libexec/check_http -I 127.0.0.1
注:更改过配置的都要重启一下相应服务才能生效;
参考 http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html