系统环境:红帽4.8(64位)安装操作系统时所有软件包都已安装。
软件环境:nagios-3.2.1、nagios-plugins-1.4.15、nrpe-2.12(可以和我的不一样)
监控机:192.168.5.58(安装操作系统的时候是所有软件包都安装,apache用系统自带的)
被监控机:192.168.3.64(随便取上图中的一台服务器)
说明:监控机上需要部署nagios、nagios-plugins、nrpe(nrpe是监控cpu负载,进程数,磁盘空间使用率)。如果说你只想监控本机的ping、或者80端口什么的那就不需要安装nrpe插件。同理,如果需要监控被监控机的存活、80端口什么的也不需要安装nrpe插件,如果要监控被监控机的cpu负载、进程数、磁盘空间使用率就需要在被监控机上安装nrpe插件。
监控机下载如下软件:
wget http://prodownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
wget http://prodownloads.sourceforge.net/sourceforge/nagios/nagios-plugins-1.4.15.tar.gz
wget http://prodownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
监控机操作如下:
安装nagios的时候,需要先创建一个系统用户:nagios,命令如下:
groupadd nagios
useradd –g nagios nagios
mkdir /usr/local/nagios
chown –R nagios:nagios /usr/local/nagios
安装nagios,解压编译安装
tar –zxvf nagios-3.2.1.tar.gz
cd nagios-3.2.1
./configure –prefix=/usr/local/nagios
make all
make install
make install-init
make install-commandmode
make install-config
安装nagios的插件,解压编译安装
tar –zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure –prefix=/usr/local/nagios
make
make install
修改apache的配置文件,在最后面加上如下内容:
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/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.users
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/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.users
Require valid-user
</Directory>
修改下面这行注释去掉,修改如下:
#ServerName new.host.name:80
修改后:
ServerName 192.168.5.58:80
增加验证用户,使用如下命令:
用户:admin,密码:abc#123
/usr/bin/htpasswd –c /usr/local/nagios/etc/htpasswd.user admin
New password:输入密码
Re-type new password:再输入一次密码
Adding password for user admin
至此nagios安装就完毕了。可以启动apache和nagios服务看看首页是什么样子,这样我们基本就测试通过了!安装超级简单,配置起来可比较麻烦!
/etc/init.d/httpd start
/etc/init.d/nagios start
简单测试:http://192.168.5.58/nagios
输入用户名admin和密码访问
注意在启动apache的时候可能会报错,至于为什么报错我也不太懂,反正网页查看是没有任何问题的。
Starting httpd: [Thu Aug 30 13:34:45 2012] [warn] The ScriptAlias directive in /etc/httpd/conf/httpd.conf at line 1024 will probably never match because it overlaps an earlier ScriptAlias.
[Thu Aug 30 13:34:45 2012] [warn] The Alias directive in /etc/httpd/conf/httpd.conf at line 1035 will probably never match because it overlaps an earlier Alias.
[ OK ]
网页查看没问题的话,下面开始配置nagios,现在我把监控机定义为一个组,组中成员也只有它,暂时只监控它的存活和80端口。
nagios的主配置文件为nagios.cfg,它里面会定义调用其他的配置文件,比如监控命令,被监控的服务,联系人,联系组等。
打开nagios.cfg主配置文件
Vi /usr/local/nagios/etc/nagios.cfg
找到如下内容并且修改和我下图一样:
# You can specify individual object config files as shown below:
cfg_file=/usr/local/nagios/etc/objects/commands.cfg ====>命令配置文件
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg ====>联系人配置文件
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg ====>监视时段配置文件
cfg_file=/usr/local/nagios/etc/objects/templates.cfg ====>模版配置文件
#cfg_file=/usr/local/nagios/etc/objects/localhost.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 ====>监控项目配置文件
cfg_file=/usr/local/nagios/etc/objects/contactgroup.cfg ====>联系人组配置文件
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
另外还有如下两个参数只需要修改一个,并说明参数的含义:
check_external_commands=1 表示允许在web界面下执行重启nagios、停止主机/服务检查等操作,more就是1不用修改
command_check_interval=-1 表示检查间隔时间,根据自己的情况定这个时间,这里我用的是5秒检查一次,所以我修改为如下:
command_check_interval=5s
修改cgi脚本控制文件cgi.cfg文件
有个参数需要知道一下
use_authentication=1 表示控制相关的cgi脚本
另外找的如下的行:
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin
在所有的行后面增加一个用户admin,这个用户就是上述创建的用户
authorized_for_system_information=nagiosadmin,admin
authorized_for_configuration_information=nagiosadmin,admin
authorized_for_system_commands=nagiosadmin,admin
authorized_for_all_services=nagiosadmin,admin
authorized_for_all_hosts=nagiosadmin,admin
authorized_for_all_service_commands=nagiosadmin,admin
authorized_for_all_host_commands=nagiosadmin,admin
下面我们开始新建上述nagios.cfg文件里面调用的那些配置文件吧!因为有些文件有,所以我备份一下!其实nagios配置很灵活的,一般位置对应上,格式正确就行。
cd /usr/local/nagios/etc/objects
mv timeperiods.cfg timeperiods.cfg.bak
定义监控时间段,创建文件内容如下:
vi 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
}
定义了一个监控时间段,它的名称是24x7,监控的时间是每天全天24小时,注意这里不是*号,而是小写字母x。
mv contacts.cfg contacts.cfg.bak
定义联系人,创建文件内容如下:
vi contacts.cfg
define contact{
contact_name admin =====>联系人名称
alias sys admin =====>别名
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-host-by-email
host_notification_commands notify-host-by-email
email [email protected]
pager 13601298217
}
w是报警(warning),u是未知(unkown),c是严重(critical),r是恢复
定义联系组,创建文件内容如下:
Vi contactgroup.cfg
define contactgroup{
contactgroup_name sagroup
alias System Administrator
members admin
}
定义监控主机,创建文件内容如下:
Vi hosts.cfg
define host {
host_name 192.168.5.58
alias 192.168.5.58
address 127.0.0.1
check_command check-host-alive
max_check_attempts 5
check_period 24x7
contact_groups sagroup
notification_period 24x7
notification_options d,u,r
}
定义监控组,创建文件内容如下:
Vi hostgroups.cfg
define hostgroup{
hostgroup_name nagios-server
alias nagios-server
members 192.168.5.58
}
最重要的文件,定义监控项,创建文件内容如下:
Vi services.cfg
define service{
host_name 192.168.5.58
service_description check-host-alive
check_command check-host-alive ====>监控主机存活项
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.5.58
service_description check-http
check_command check_http ========>监控主机80端口
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
这就基本就配置完了本机监控本机的存活、80端口两项了。
下面我们检查一下nagios.cfg的配置文件是否有问题,执行如下命令:
/usr/local/nagios/bin/nagios –v /usr/local/nagios/etc/nagios.cfg
如果没有错误的话最后面会提示:
Total Warnings: 0
Total Errors: 0
重启nagios服务
/etc/init.d/nagios restart
网页登陆后点击host groups按钮查看应该会出现如下图,我的5个OK是我加了监控cpu负载、进程数和磁盘空间使用率的(如果没配置错误的话你的应该会出现2个pending状态,等待几分钟之后应该就是2个OK状态了):
接下来我们配置本机监控本机的cpu负载、进程数和磁盘空间使用率,并且监控192.168.3.64这台被监控机上所有的项,正如上文说到要监控这三项指标的话,必须安装nrpe插件。所以本机和3.64都要安装nrpe插件。
安装nrpe插件请参考如下链接:
http://blog.chinaunix.net/uid-23916356-id-3062081.html
记住本机因为已经安装了nagios-plugins了,所以只需安装nrpe就行。(安装过程略)
被监控机需要安装nagios-plugins和nrpe两个插件。(安装过程略)
因为要用到nrpe模版,所以commands.cfg文件增加如下内容:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
上述很多配置文件都已建立,只需要修改即可!如我要把单独加进来的被监控机单独分一个组。修改hostgroups.cfg文件,增加如下内容:
define hostgroup{
hostgroup_name ceshi-hadoop
alias ceshi-hadoop
members 192.168.3.64 =====>组成员,多个组员就以逗号隔开写
}
新加的被监控机添加到监控主机文件,修改hosts.cfg文件,增加如下内容:
define host {
host_name 192.168.3.64
alias 192.168.3.64
address 192.168.3.64
check_command check-host-alive
max_check_attempts 5
check_period 24x7
contact_groups sagroup
notification_period 24x7
notification_options d,u,r
}
因为上述要求有新增的监控项,所以肯定要修改监控项文件。最后修改services.cfg文件,增加如下内容:
define service{
host_name 192.168.5.58
service_description check-local-load
check_command check_nrpe!check_load =====> cpu负载
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.5.58
service_description check-local-procs
check_command check_nrpe!check_total_procs =====>进程数
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.5.58
service_description check-local-disk
check_command check_nrpe!check_df =====> 磁盘使用率
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64 ====>主机存活
service_description check-host-alive
check_command check-host-alive
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64
service_description check-local-disk
check_command check_nrpe!check_df ======>磁盘使用率
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64
service_description check-http
check_command check_http ========>80端口
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64
service_description check-tcp-1099
check_command check_tcp!1099 ==========>1099端口
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64
service_description check-tcp-2222
check_command check_tcp!2222 ===========>2222端口
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64
service_description check-tcp-60030
check_command check_tcp!60030 ==============>60030端口
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64
service_description check-tcp-50010
check_command check_tcp!50010 ===========>50010端口
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64
service_description check-local-load
check_command check_nrpe!check_load =========>cpu负载
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service{
host_name 192.168.3.64
service_description check-total-procs
check_command check_nrpe!check_total_procs =======>进程数
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
这样就配置完毕了。重启nagios服务,重启之前检查一下配置文件。
/usr/local/nagios/bin/nagios –v /usr/local/nagios/etc/nagios.cfg
没有任何问题的话,我们重新启动nagios
重启完之后点击host groups按钮应该会出现2个组,每个组里面各监控一台机器,本机监控了5项指标,3.64监控了9项指标。
配置总结:
假如你有新的机器要被监控,你们你需要修改监控主机、监控主机组、监控项三个配置文件。
假如你的一台机器已经监控了,需要再监控一个端口什么的,那么你只需要修改监控项就OK了。总之呢?被监控机上不部署nagios插件之类的话,只可以监控它的存活、端口开放等。但是监控不了cpu负载、进程数和磁盘使用率等之类指标。最后说一下,我也是菜鸟,有问题的地方请联系QQ:316189480
开始配置邮件报警
测试工作:你需要有一个合法的邮箱地址,当然可以是很多,例如谷歌、yahoo、163、126、139邮箱等。随便选取一个测试一下吧!
我这里就拿监控机自带的sendmail测试吧!
确定sendmail已经启动!
[root@slave3 etc]# /etc/init.d/sendmail status
sendmail (pid 5291 5282) is running...
执行如下命令:
echo “test” | mail 你的邮箱地址
过几分钟确定你的邮箱能收到这封test内容的邮件
然后修改contacts.cfg文件,修改后的内容如下:
define contact{
contact_name zengzhunzhun
alias zengzhunzhun
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 [email protected],zengzhunzhun@gmail.
com,[email protected],[email protected]
pager 13601298217
}
修改contactgroups.cfg文件,修改后的内容如下:
define contactgroup{
contactgroup_name sagroup
alias System Administrator
members zengzhunzhun
}
然后重启nagios服务
/etc/init.d/nagios restart
接下来我们的邮箱应该就能收到报警了!可能会有一点延迟,因为我们使用的本机自带的sendmail服务器不是合法的邮件服务器!我这里这是简单的测试一下。如果收不到的话,请查看/var/log/maillog是否发送成功,如果遇到下列类似错误的话那么请修改nagios.cfg文件,将参数notification_timeout=30修改为120,时间单位是秒,修改后记得重新启动nagios。如果生产环境需要的话还得配置邮件服务器!这就另说了!
[1346316317] Warning: Contact 'admin' service notification command '/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: PROBLEM\nHost: 192.168.2.161\nState: UP\nAddress: 192.168.2.161\nInfo: PING OK - Packet loss = 0%, RTA = 5.50 ms\n\nDate/Time: Thu Aug 30 16:44:46 CST 2012\n" | /bin/mail -s "** PROBLEM Host Alert: 192.168.2.161 is UP **" [email protected]' timed out after 30 seconds
开始配置短信报警
众所周知移动推出的139邮箱是可以接收短信的,意思就是移动的邮箱接收到邮件时候同时也会给绑定的手机发一封邮件!所以我们只需要把你的contacts.cfg文件的邮箱改为你的移动139的邮箱就OK了!测试时可以发送短信通知的。