1. 创建相关账号:PS:所有的命令都用root执行
创建nagios和apache的登陆账号并设定密码;(设定密码主要是考虑到系统的安全性):
Shell > useradd nagios
Shell > useradd apache
Shell > passwd nagios
Shell > passwd apache
创建一个nagcmd用户组用于web接口执行外部命令,将nagios和apache都加入到这个组中;
Shell > groupadd nagcmd
Shell > usermod -G nagcmd nagios
Shell > usermod -G nagcmd apache
2.安装nagios:
下载源码包并解压安装,地址为
http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
Shell > wget -c http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
Shell > tar zxvf nagios-3.2.3.tar.gz
运行配置脚本,并编译安装,执行配置脚本的时候要加上参数“with-command-group”,用来指定安装nagios的web接口执行命令的用户组为nagcmd:
Shell > cd nagios-3.2.3
Shell > ./configure –with-command-group=nagcmd
Shell > make all
Shell > make install
Shell > make install-init
Shell > make install-config
Shell > make install-commandmode
安装好了之后就会发现在/usr/local/下面有5个目录:etc,var,bin,sbin,share如果有那个没有,就是没有安装成功,也可以在每次执行完命令之后再执行一条shell> echo $? 用来返回执行结果,如果显示0表示执行成功,如果不是0,则说明那里有错误,需要修改;
3.配置web接口:
安装nagios的web配置文件到apache的配置文件目录/etc/httpd/conf.d/下,这个也是在刚才的路径下执行安装的,安装了之后就会发现/etc/httpd/conf.d/nagios.conf文件了:
Shell > make install-webconf
创建一个nagiosadmin的用户用于nagios的WEB接口登录。记下你所设置的登录口令,用于通过WEB登录nagios。
Shell > htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
重启httpd服务,使得设置生效:
Shell > /etc/init.d/httpd restart
4.编译安装nagios插件:(nagios插件很重要,它包含nagios所有的执行命令)
Nagios插件地址:
http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
Shell >
wget -c http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
执行配置脚本,并编译安装,执行配置脚本的时候要加上参数“–with-nagios-user=nagios –with-nagios-group=nagios”,是指定nagios的登陆账号:
shell > ./configure –with-nagios-user=nagios –with-nagios-group=nagios
shell > make
shell > make install
5.配置并启动nagios服务:
把nagios服务加入服务启动列表,使他在电脑启动的时候启动:
Shell > chkconfig –add nagios
Shell > chkconfig nagios on
验证Nagios的样例配置文件是否正确:
Shell > /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果显示如下,说明配置没有问题,如果不正确,可以根据提示修改实例配置文件:
#### 此处省略#################
Total Warnings: 0
Total Errors: 0
Things look okay – No serious problems were detected during the pre-flight check
6.修改selinux服务设置:
默认情况下,Fedora以后的操作系统(包括RHEL、CentOS)与SELinux(安全增强型Linux)同步发行,并且在安装后默认使用强制模式。这会在你尝试联入Nagios的CGI时导致一个”内部服务错误”消息。直观显示就是无法通过刚才设置的WEB帐号登录。所以需要修改selinux服务:
查看当前模式:
Shell > getenforce
显示结果如果为:enforcing,表示selinux已经使用了强制模式,使用下面的命令改变为允许模式:
Shell > setenforce 0
再次使用getenforce查看当前模式,得到的结果就是permissive了:
此处表示正常。可以使用前面设置的帐号登录nagios页面了!
PS:重启系统后,SeLinux的模式恢复为强制,必须再次使用setenforce 0
如果觉得麻烦,可以选择彻底禁用selinux的强制模式:
Shell > vim /etc/selinux/config 将selinux的值修改为disable,即SELINUX=disable
还可以不关闭SELinux,但永久性变更针对Nagios的方法是让CGI模块在SELinux下指定强制目标模式:
Shell > chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
Shell > chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
7.从WEB方式来接入Nagios的WEB接口
输入http://ip/nagios,提示下输入用户名(nagiosadmin)和之前设置的密码就登录成功了!当然,由于此时没定义主机,没定义联系人,这个页面除了监控本机外啥都没有,基本上就是个摆设,还需要进一步的设置,你可以根据你的具体情况在进行详细的设置。
PS:nagios的日志存放在../nagios/var/nagios.log中,可以看到发邮件、短信以及配置文件检测错误的信息,便于我们检错