如何在ubuntu14.04 LTS上从源代码安装nagios4

时间:2019-08-20 17:58:23  来源:igfitidea点击:

安装环境

操作系统:Ubuntu 14.04 LTS

架构:x86_64

Web服务器:Apache 2.4

Nagios:Nagios Core 4.0.8(源代码)

Nagios插件:Nagios-plugins2.0.3(源代码)

服务器IP地址:192.168.56.102

在Ubuntu 14.04 LTS上从源代码安装Nagios core 4.0的步骤:

安装Apache、PHP、OpenSSL等

在Ubuntu14.04上,Apache2.4是默认的web服务器

sudo apt-get install wget build-essential apache2 php5 openssl perl make php5-gd wget  libgd2-xpm-dev libapache2-mod-php5 libperl-dev libssl-dev daemon

为Nagios创建用户和组

sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios

下载最新的Nagios核心源代码包

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.8.tar.gz

提取下载的Nagios核心包

tar -xvzf nagios-4.0.8.tar.gz

编译安装Nagios内核

cd nagios-4.0.8
sudo ./configure --with-command-group=nagcmd

sudo make all

sudo make install
sudo make install-init
sudo make install-config
sudo sudo make install-commandmode
sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-enabled/nagios.conf

在最后一步中,如果我们根据编译指导来安装webconf,那么会报错:

Hyman@ubuntu:~/nagios-4.0.8$ sudo make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
/usr/bin/install: cannot create regular file ‘/etc/httpd/conf.d/nagios.conf’: No such file or directory
make: *** [install-webconf] Error 1
Hyman@ubuntu:~/nagios-4.0.8$

这是因为在ubuntu中,apache的路径是 /etc/apache2。 而不是/etc/httpd 。所以我们不使用脚本,直接执行安装命令。

安装后,查看配置文件:

Hyman@ubuntu:~/nagios-4.0.8$ ls -l /etc/apache2/sites-enabled/
total 4
lrwxrwxrwx 1 root root  35 Aug 25 02:34 000-default.conf -> ../sites-available/000-default.conf
-rw-r--r-- 1 root root 982 Aug 25 02:46 nagios.conf
Hyman@ubuntu:~/nagios-4.0.8$

复制eventhandlers目录

将eventhandlers目录复制到/usr/local/nagios/libexec/

sudo cp -rvf contrib/eventhandlers/ /usr/local/nagios/libexec/

更改/usr/local/nagios/libexec/eventhandlers的所有权

sudo chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers

创建Nagios init脚本

nagios init脚本用来通过服务启动/停止/重新启动nagios

sudo vi /etc/init/nagios.conf

内容如下:

description     "nagios 4.x core"
 
start on filesystems
stop on runlevel [1246]
 
respawn
 
setuid nagios
setgid nagcmd
console log
 
script
        exec /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
end script

我们可以使用以下给定的命令来启动/停止/重新启动nagios服务,或者查看其状态:

sudo service nagios start
sudo service nagios stop
sudo service nagios restart
sudo service nagios status

启用apache重写和cgi模块

sudo a2enmod rewrite
sudo a2enmod cgi
sudo service apache2 restart

启动Nagios服务

sudo service nagios start

修改nagios.cmd文件

更改nagios.cmd文件的权限

sudo chmod 666 /usr/local/nagios/var/rw/nagios.cmd

设置Nagiosadmin用户密码

默认使用的用户是 nagiosadmin,所以给它设置一个密码:

Hyman@ubuntu:~$ sudo grep nagiosadmin /usr/local/nagios/etc/cgi.cfg 
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
Hyman@ubuntu:~$
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

如果报错:“sudo:htpasswd:command not found”
说明没有安装apache工具包,使用下面命令进行安装:

sudo apt-get install apache2-utils

访问Nagios服务器控制面板

在浏览器中打开http://服务器ip/nagios进行登录。

在面板中会看到许多红色警报。这是因为 Nagios插件未安装

在Nagios服务器上安装Nagios插件

下载插件包

wget http://www.nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz

解压

tar -xvzf nagios-plugins-2.0.3.tar.gz

切换到解压目录

cd nagios-plugins-2.0.3/

编译Nagios插件

sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios
sudo make
sudo make install

nagios默认每90秒更新一次。等待几分钟,监控警告将会变成绿色。