1.下载软件
链接地址:
http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
或在http://www.nagios.org/download这里可以下载Nagios的主程序、插件、附加的软件把Nagios的主程序和插件都下载好
2.安装Nagios
A、安装环境要求
Apache
PHP
GCC compiler
GD development libraries
需要有root权限
B、建立Nagios用户和组
passwd nagios
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache
C、安装Nagios
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios
cd nagios-3.2.1
./configure --prefix=/usr/local/nagios --with-command-group=nagcmd --with-gd-lib=/usr/lib --with-gd-inc=/usr/include
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
3.安装Nagios插件
tar xzf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure--enable-redhat-pthread-workaroundNormal 0 0 2 false false false EN-US ZH-TW X-NONE4.安装NRPE服务
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --prefix=/usr/local/nagios
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
添加 nrpe 服务到xinetd:
vi /etc/services
nrpe 5666/tcp # nrpe
重新启动Xinetd服务:
service xinetd restart
测试Nrpe是否能正常工作:
/usr/local/nagios/libexec/check_nrpe -H localhost
二、Nagios配置
1.Nagios配置
A、修改Nagios的配置文件
vi /usr/local/nagios/etc/nagios.cfg
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg //注释此行
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg //监视时段配置文件路径
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg //联系组配置文件路径
cfg_file=/usr/local/nagios/etc/objects/commands.cfg //监控命令配置文件路径
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg //主机配置文件路径
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg //主机组配置文件路
cfg_file=/usr/local/nagios/etc/objects/services.cfg //服务配置文件路径
check_external_commands=1 //允许在web接口下执行重启Nagios
command_check_interval=60s //改成 60s, 命令检查时间间隔
因为默认的nagios配置文件没有 hosts.cfg、hostgroups.cfg等档,因此在检查的时候会报错,这时需要手工的去创建这些档:
cd /usr/local/nagios/etc/objects
touch hosts.cfg
touch hostgroups.cfg
..........
Normal 0 0 2 false false false EN-US ZH-TW X-NONEB、配置cgi.cfgvi /usr/local/nagios/etc/cgi.cfg //修改cgi脚本控制档
use_authentication=1 //确保值为 1
default_user_name=nagios //修改为认证用户
后面修改内容如下:
authorized_for_system_information=nagiosadmin,nagios
authorized_for_configuration_information=nagiosadmin,nagios
authorized_for_system_commands=nagios //多个用户之间用逗号隔开
authorized_for_all_services=nagiosadmin,nagios
authorized_for_all_hosts=nagiosadmin,nagios
authorized_for_all_service_commands=nagiosadmin,nagios
authorized_for_all_host_commands=nagiosadmin,nagios
Normal 0 0 2 false false false EN-US ZH-TW X-NONEC、配置command.cfg,定义通知方式需要定义notify-service-by-email与notify-host-by-email两个通知
# 'notify-host-by-email' command definition
define command{
command_name notify-host-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/mailx -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-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$\n" | /bin/mailx -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
Normal 0 0 2 false false false EN-US ZH-TW X-NONED、配置contacts.cfg,定义联系人vi /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
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 XXX@YY.COM;
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}
Normal 0 0 2 false false false EN-US ZH-TW X-NONE然后检查配置文件是否出错/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果正常就显示如下信息
Total Warnings: 0
Total Errors: 0
启动Nagios
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
设置开机自动启动
echo "/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg" >> /etc/rc.local
2.配置Apache
配置httpd.conf使用nagios在httpd.conf后面加入如下内容
vi /etc/httpd/conf/httpd.conf
#Setting for nagios
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
Alias /nagios /usr/local/nagios/share
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
对增加后的参数进行验证
/usr/sbin/apachectl -t //检查配置文件是否正确
创建apache目录验证文件
htpasswd -c /usr/local/nagios/etc/htpasswd nagiosadmin
New password: (输入密码)
Re-type new password: (再输入一次密码)
生成apache访问的认证档并启动apache进行验证
service httpd restart //启动apache
到目前为止,监控主机上的nagios的所有安装配置已经全部完成,接下来就是要配置具体的监控项目了,在此可以输入:http://10.192.28.245/nagios/ 登陆WEB监控接口了,登陆账号输入刚才建立的认证账号
Normal 0 0 2 false false false EN-US ZH-TW X-NONE三、客服端的实施
1.监控 Linux主机
Client端配置:
A.建用户:
useradd nagios //可以在 /etc/password 里修改此账户的shell为 /sbin/nologin
passwd nagios
B.安装插件:
tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/usr/local/nagios --enable-redhat-pthread-workaround
make all
make install
C.修改权限:
chown nagios:nagios /usr/local/nagios -R
D.安装NRPE服务:
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-ssl
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
E.添加服务器IP:
vi /etc/xinetd.d/nrpe
only_from = 127.0.0.1 10.192.28.245
F.添加 NRPE 服务:
vi /etc/services
nrpe 5666/tcp # nrpe
G.定义要检查的项目
vi /usr/local/nagios/etc/nrpe.cfg
# The following examples use hardcoded command arguments...
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 2,2,2 -c 2,2,2
command[check_data]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /data
command[check_u01]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /u01
command[check_var]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /var
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 150 -c 200
H.重新启动Xinetd服务:
service xinetd restart
I.查看NRPE服务是否开启:
netstat -ant|grep 5666
J.测试Nrpe是否能正常工作:
/usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12 (这表示在本地用check_nrpe连接nrpe daemon是正常的)
Server端配置:
A.在hosts.cfg中添加主机定义
vi /usr/local/nagios/etc/objects/hosts.cfg
添加以下内容
define host {
host_name wmssty
alias wmssty
address 10.192.29.32
contact_groups admins
check_command check-host-alive
max_check_attempts 5
notification_interval 1
notification_period 24x7
notification_options d,u,r
}
B.将监控主机划分到指定组中
vi /usr/local/nagios/etc/objects/hostgroups.cfg
define hostgroup {
hostgroup_name Linux DBSTY
alias Linux DBSTY
members wmssty,multidb,emdi_sty,dt2_sty,dt8sty,pdmsty,rac_sty
}
C.定义要检查的项目
vi /usr/local/nagios/etc/objects/services.cfg
#########wms_sty check begin##########
define service {
host_name wmssty
service_description check-data
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 1
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_data
}
define service {
host_name wmssty
service_description check-u01
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 1
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_u01
}
define service {
host_name wmssty
service_description check-load
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 1
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
}
define service {
host_name wmssty
service_description check-host-alive
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service {
host_name wmssty
service_description check-var
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_var
}
#########wms_sty check end#########
D.验证配置文件是否正确,并重启
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/object/nagios.cfg
service nagios restart //重启nagios 使配置生效
E.查看naigios监控页面,发现主机已经被正常添加
2、监控windows主机
Client端配置:
Nagios监控windows需要在被监控Windows主机上安装NSClient++
A.安装 NSClient++
到http://nsclient.org/nscp/downloads下载最近版本的NSClient++并安装
B.配置NSClient++
编辑 C:\NSClient++ 下的 NSC.ini 檔 ,修改以下地方:
[modules]
所有的 .dll 注释都全都去掉, CheckWMI.dll 和 RemoteConfiguration.dll两个仍保留注释装套
[Settings]
'password' 这个项目是在设置密码,作用是在 nagios 监控主机进行联机时,要求提供密码才能进一步进行联机,这里为了方便起见,跳过它,不要设定密码。
'allowed_hosts' 选项的注释去掉,并且加上 nagios 的监控主机的 IP,修改如下 allowed_hosts=127.0.0.1/32,10.192.28.245 以逗点相隔。
[NSClient]
'port' 选项的注释须要拿掉,并且它的值是 '12489',这是 NSClient 的预设 port
设定完成之后,启动 NSClient++
在 cmd 中 C:\NSClient++ 下,执行 NSClient++ /start
在 cmd 下,执行 netstat -an ,检查 port 12489 是否开启
启动完后,在windows上的操作就结束了
Server端配置:
A.在hosts.cfg与hostgroup.cfg中添加主机定义
此步操作与监控Linux主机一致
B. 定义要检查的项目
以/usr/local/nagios/etc/objects/windows.cfg档为蓝本,修改usr/local/nagios/etc/objects
/services.cfg文件,添加必要的监控项目
vi /usr/local/nagios/etc/objects/services.cfg
##################tstfox01 check begin#############################
define service {
host_name tstfox01
service_description check-host-alive
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service{
use generic-service
host_name tstfox01
service_description CPU Load
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!CPULOAD!-l 5,80,90
}
define service{
use generic-service
host_name tstfox01
service_description Memory Usage
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!MEMUSE!-w 80 -c 90
}
define service{
use generic-service
host_name tstfox01
service_description C:\ Drive Space
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!USEDDISKSPACE!-l c -w 90 -c 95
}
define service{
use generic-service
host_name tstfox01
service_description D:\ Drive Space
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!USEDDISKSPACE!-l d -w 80 -c 90
}
define service{
use generic-service
host_name tstfox01
service_description E:\ Drive Space
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!USEDDISKSPACE!-l e -w 80 -c 90
}
define service{
use generic-service
host_name tstfox01
service_description WEB
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
check_command check_http_status!$HOSTADDRESS$!8080
}
##################tstfox01 check end#############################
C.验证配置文件是否正确,并重启
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios restart //重启nagios 使配置生效
D.查看naigios监控页面,发现主机已经被正常添加