一、前提:系统、
PHP
、
Apache
、
MySQL
、
Mail
都工作正常,在这不做介绍;
二、需要安装: gcc 、 glibc 、 gd 、 gd-devel 、 openssl-devel 、 httpd-devel 、 per-GD 、 net-snmp-devel 、 php-pdo 、 php-gd 、 perl-Crypt-DES ,为了方便避开烦人的包依赖问题,我直接
yum install httpd* 、 php* 、 net-snmp 、 mysql* 、 glibc*;
需要下载的源码包:
nagios : http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.2.tar.gz
nagios-plugins : http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
nrpe : http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
nagios-snmp-plugins : http://nagios.manubulon.com/nagios-snmp-plugins.1.1.1.tgz
############################################### 开始安装、配置 #####################################################
1、安装Nagios;
groupadd nagios
useradd -g nagios nagios
chmod 755 /usr/local/nagios/
usermod -G nagios apache // 将 apache 、 nagios 用户加入到 nagios 组里;
usermod -G nagios nagios
tar zxf nagios-3.2.2.tar.gz
cd nagios-3.2.2
./configure(nagios默认安装目录即为/usr/local/nagios,可不用加 --prefix参数)
make install
make install-init
make install-commandmode
make install-config
- This installs *SAMPLE* config files in /usr/local/nagios/etc
You'll have to modify these sample files before you can use Nagios. Read the HTML documentation for more info on doing this. Pay particular attention to the docs on
- This installs the Apache config file for the Nagios web interface
把虚拟目录配置文件写入/etc/httpd/conf.d/nagios.conf,不需再往apache配置文件中另加nagios的目录.
2、安装nagios-plugins;
tar zxf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group =nagios(默认安装路径为/usr/local/nagios)
make
make install
6 、 Apache 里的配置文件自动加了 nagios.conf, 路径为 /etc/httpd/conf.d 内容如下:
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
重启 apache
service httpd restart
3、为web访问加入认证的用户名和密码;
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin (新增用户必须和cgi.cfg中authorized_for_后面验证的用户一致)
#vi /usr/local/nagios/etc/objects/contacts.cfg
此处我只修改了 email 加入了自己的邮箱地址;
B 、 vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1 改成
use_authentication=0,
即不用验证 . 不然有一些页面不会显示。 // 在网上看到这个,不知道这点是什么意思 ,我也做了此修改;
C 、检查配置文件是否正确:
[root@host etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 3.2.1
Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 03-09-2010
License: GPL
....................................
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
D 、修改日志文件的属主,以后有问题可以查看此文件;
chown -R nagios:nagios /usr/local/nagios/var/nagios.log
E 、以上各步配置均没有错误,此时可以启动 nagios 了,
service nagios start
到此 nagios 配置完成,可以通过下面 web 访问访问了;
http://ip/nagios/
A 、 nagios-plugins:wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
B 、 nrpe:wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
安装如下;
(1) 在系统中添加 nagios 用户;
useradd nagios
passwd nagios
(2) 安装 nagios-plugins;
tar -zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure
make
make install
(3) 改变 nagios 下的属主;
chown nagios.nagios /usr/local/nagios -Rf
(4) 安装 nrpe-2.12
tar -zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
(5) 修改 nrpe.cfg ,允许主机为对应公司网关;
vi /usr/local/nagios/etc/nrpe.cfg 将 allowed_hosts=127.0.0.1 改成: X.X.X.X , 127.0.0.1
添加相应要监控的对象,如下所示:
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 500 -c 600
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
(6) 启动 nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
可以查看 nrpe 是否启动;
netstat -an|grep 5666
察看是否能返回 nrpv 版本号 ;
/usr/local/nagios/libexec/check_nrpe -H localhost
添加如下内容;
#check nrpe
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
添加被监控端的配置文件;
vi /usr/local/nagios/etc/nagios.cfg
在此处中间添加;
cfg_file=/usr/local/nagios/etc/objects/Test.cfg
编辑该文件;
define host{
use linux-server
host_name Test
alias linux-server
address 192.168.0.110
}
define service{
use generic-service
host_name Test
service_description HTTP
check_command check_http
}
define service{
use generic-service
host_name Test
service_description FTP
check_command check_ftp
}
define service{
use generic-service
host_name Test
service_description SSH
check_command check_ssh
}
define service{
use generic-service
host_name Test
service_description SMTP
check_command check_smtp
}
define service{
use generic-service
host_name Test
service_description POP3
check_command check_pop
}
define service{
use generic-service
host_name Test
service_description check-swap
check_command check_nrpe!check_swap
}
define service{
use generic-service
host_name Test
service_description check-load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name Test
service_description check-disk
check_command check_nrpe!check_disk
}
define service{
use generic-service
host_name Test
service_description zombie_procs
check_command check_nrpe!check_zombie_procs
}
define service{
use generic-service
host_name Test
service_description check-users
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name Test
service_description total_procs
check_command check_nrpe!check_total_procs
}
define service {
use generic-service
host_name Test
service_description check_mysql
check_command check_mysql!192.168.0.110!3306!nagios!nagios!nagdb!60!600
}
这里面定义的是监控的对象,修改完成后保存退出,就可以了。
service nagios restart
8、此时可以访问该页面,相关的被监控的对象也将出现。
************************************** M ySQL 监控配置 *************************************
1 、在要监控的 MYSQL 数据库里新建库和相应的用户;
CREATE DATABASE nagdb DEFAULT CHARSET=utf8;
GRANT SELECT ON nagdb.* TO 'nagios'@'%';
UPDATE mysql.user SET 'Password' = PASSWORD('********') WHERE 'User' = 'nagios'
FLUSH PRIVILEGES;
2 、修改 command.cfg ,添加对 mysql 和主从同步的定义;
vi /usr/local/nagios/etc/objects/command.cfg;
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$ -d $ARG5$
}
define command{
command_name check_mysql_slave
command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$ -d $ARG5$ -S -w $ARG6$ -c $ARG7$
}
3 、在被监控的主数据库和从数据库的 cfg 文件中分别添加;
define service {
use generic-service
host_name Master-Mysql
service_description check_mysql
check_command check_mysql!192.168.0.111!3306!nagios!******!nagdb
}
define service {
use generic-service
host_name Slave-Mysql
service_description check_mysql_slave
check_command check_mysql_slave!192.168.0.112!3306!nagios!******!nagdb!60!600
}
4 、检查配置文件,重启 nagios 服务;
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios restart
5 、到此 MySQL 的监控配置完成。
二、需要安装: gcc 、 glibc 、 gd 、 gd-devel 、 openssl-devel 、 httpd-devel 、 per-GD 、 net-snmp-devel 、 php-pdo 、 php-gd 、 perl-Crypt-DES ,为了方便避开烦人的包依赖问题,我直接
yum install httpd* 、 php* 、 net-snmp 、 mysql* 、 glibc*;
需要下载的源码包:
nagios : http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.2.tar.gz
nagios-plugins : http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
nrpe : http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
nagios-snmp-plugins : http://nagios.manubulon.com/nagios-snmp-plugins.1.1.1.tgz
############################################### 开始安装、配置 #####################################################
1、安装Nagios;
groupadd nagios
useradd -g nagios nagios
mkdir /usr/local/nagios
chmod 755 /usr/local/nagios/
usermod -G nagios apache // 将 apache 、 nagios 用户加入到 nagios 组里;
usermod -G nagios nagios
tar zxf nagios-3.2.2.tar.gz
cd nagios-3.2.2
./configure(nagios默认安装目录即为/usr/local/nagios,可不用加 --prefix参数)
根据如下提示:
依次执行如下操作:
make all------编译
make install
- This installs the main program, CGIs, and HTML files
安装主要的程序、CGI及HTML文件
make install-init
- This installs the init script in /etc/rc.d/init.d
把nagios做成一个运行脚本,使nagios随系统开机启动,这是一个很方便的措施。
make install-commandmode
- This installs and configures permissions on the directory for holding the external command file
给外部命令访问nagios配置文件的权限
make install-config
- This installs *SAMPLE* config files in /usr/local/nagios/etc
You'll have to modify these sample files before you can use Nagios. Read the HTML documentation for more info on doing this. Pay particular attention to the docs on
object configuration files, as they determine what/how things get monitored!
把配置文件的例子复制到nagios的安装目录
make install-webconf(如果apache用的是自己另外安装的,可以不执行此安装,直接在apache的配置文件httpd.conf中加入6所示内容)
- This installs the Apache config file for the Nagios web interface
把虚拟目录配置文件写入/etc/httpd/conf.d/nagios.conf,不需再往apache配置文件中另加nagios的目录.
2、安装nagios-plugins;
tar zxf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group =nagios(默认安装路径为/usr/local/nagios)
make
make install
6 、 Apache 里的配置文件自动加了 nagios.conf, 路径为 /etc/httpd/conf.d 内容如下:
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
重启 apache
service httpd restart
3、为web访问加入认证的用户名和密码;
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin (新增用户必须和cgi.cfg中authorized_for_后面验证的用户一致)
最好能运行 chmod 755 /usr/local/nagios/etc/htpasswd.users ,允许其他用户可以读取.
4、配置nagios;
#vi /usr/local/nagios/etc/objects/contacts.cfg
此处我只修改了 email 加入了自己的邮箱地址;
B 、 vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1 改成
use_authentication=0,
即不用验证 . 不然有一些页面不会显示。 // 在网上看到这个,不知道这点是什么意思 ,我也做了此修改;
C 、检查配置文件是否正确:
[root@host etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 3.2.1
Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 03-09-2010
License: GPL
....................................
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
D 、修改日志文件的属主,以后有问题可以查看此文件;
chown -R nagios:nagios /usr/local/nagios/var/nagios.log
E 、以上各步配置均没有错误,此时可以启动 nagios 了,
service nagios start
到此 nagios 配置完成,可以通过下面 web 访问访问了;
http://ip/nagios/
5、配置被监控端;(监控linux服务器)
A 、 nagios-plugins:wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
B 、 nrpe:wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
安装如下;
(1) 在系统中添加 nagios 用户;
useradd nagios
passwd nagios
(2) 安装 nagios-plugins;
tar -zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure
make
make install
(3) 改变 nagios 下的属主;
chown nagios.nagios /usr/local/nagios -Rf
(4) 安装 nrpe-2.12
tar -zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
(5) 修改 nrpe.cfg ,允许主机为对应公司网关;
vi /usr/local/nagios/etc/nrpe.cfg 将 allowed_hosts=127.0.0.1 改成: X.X.X.X , 127.0.0.1
添加相应要监控的对象,如下所示:
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 500 -c 600
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
(6) 启动 nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
可以查看 nrpe 是否启动;
netstat -an|grep 5666
察看是否能返回 nrpv 版本号 ;
/usr/local/nagios/libexec/check_nrpe -H localhost
6、配置command.cfg、nagios.cfg……
添加如下内容;
#check nrpe
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
添加被监控端的配置文件;
vi /usr/local/nagios/etc/nagios.cfg
在此处中间添加;
cfg_file=/usr/local/nagios/etc/objects/Test.cfg
编辑该文件;
define host{
use linux-server
host_name Test
alias linux-server
address 192.168.0.110
}
define service{
use generic-service
host_name Test
service_description HTTP
check_command check_http
}
define service{
use generic-service
host_name Test
service_description FTP
check_command check_ftp
}
define service{
use generic-service
host_name Test
service_description SSH
check_command check_ssh
}
define service{
use generic-service
host_name Test
service_description SMTP
check_command check_smtp
}
define service{
use generic-service
host_name Test
service_description POP3
check_command check_pop
}
define service{
use generic-service
host_name Test
service_description check-swap
check_command check_nrpe!check_swap
}
define service{
use generic-service
host_name Test
service_description check-load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name Test
service_description check-disk
check_command check_nrpe!check_disk
}
define service{
use generic-service
host_name Test
service_description zombie_procs
check_command check_nrpe!check_zombie_procs
}
define service{
use generic-service
host_name Test
service_description check-users
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name Test
service_description total_procs
check_command check_nrpe!check_total_procs
}
define service {
use generic-service
host_name Test
service_description check_mysql
check_command check_mysql!192.168.0.110!3306!nagios!nagios!nagdb!60!600
}
这里面定义的是监控的对象,修改完成后保存退出,就可以了。
7、检查配置文件,重启nagios服务;
service nagios restart
8、此时可以访问该页面,相关的被监控的对象也将出现。
************************************** M ySQL 监控配置 *************************************
1 、在要监控的 MYSQL 数据库里新建库和相应的用户;
CREATE DATABASE nagdb DEFAULT CHARSET=utf8;
GRANT SELECT ON nagdb.* TO 'nagios'@'%';
UPDATE mysql.user SET 'Password' = PASSWORD('********') WHERE 'User' = 'nagios'
FLUSH PRIVILEGES;
2 、修改 command.cfg ,添加对 mysql 和主从同步的定义;
vi /usr/local/nagios/etc/objects/command.cfg;
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$ -d $ARG5$
}
define command{
command_name check_mysql_slave
command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$ -d $ARG5$ -S -w $ARG6$ -c $ARG7$
}
3 、在被监控的主数据库和从数据库的 cfg 文件中分别添加;
define service {
use generic-service
host_name Master-Mysql
service_description check_mysql
check_command check_mysql!192.168.0.111!3306!nagios!******!nagdb
}
define service {
use generic-service
host_name Slave-Mysql
service_description check_mysql_slave
check_command check_mysql_slave!192.168.0.112!3306!nagios!******!nagdb!60!600
}
4 、检查配置文件,重启 nagios 服务;
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios restart
5 、到此 MySQL 的监控配置完成。