nagios

Posted jiashengyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nagios相关的知识,希望对你有一定的参考价值。

 

 ubuntu 16.04 上安装nagios

sudo apt-get install build-essential

sudo agt-get install openssh-server

/etc/init.d/ssh start

服务器端:

安装基础环境:

/*add-apt-repository ppa:ondrej/php

apt-get update

apt-get install apache2

apt-get install php5.6*/

apt-get install wget build-essential apache2 php apache2-mod-php7.0 php-gd libgd-dev sendmail unzip

安装失败的包执行如下命令后继续安装

apt-get update --fix-missing

用户和组配置

Useradd nagios

Groupadd nagcmd

Usermod -a -G nagcmd nagios

usermod -a -G nagios,nagcmd www-data

一、安装Nagios

1.下载源文件         

cd ~
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.0.tar.gz
tar -xzf nagios*.tar.gz
cd nagios-4.2.0

2.编译Nagios

构建Nagios之前,必须使用用户和之前创建的组来配置它

./configure --with-nagios-group=nagios --with-command-group=nagcmd

make all

sudo make install

sudo make install-commandmode

sudo make install-init

sudo make install-config

/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf

并将evenhandler目录复制到nagios目录中:

cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers

3.安装nagios插件

cd ~
wget https://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
tar -xzf nagios-plugins*.tar.gz
cd nagios-plugin-2.1.2/

./configure --prefix=/data/program/nagios --with-nagios-user=nagios --with-nagios-group=nagios && make && make install

技术图片 

测试已安装的插件,修改nagios.cfg的配置文件,service_check_timeout=300

cd /data/program/nagios/libexec/

./check_ping -H www.baidu.com -w 100.0,10% -c 100.0,30% -p 10 -t 2

/*

报错CRITICAL - Plugin timed out待解决

---------------------------------------------------------------------------------------------------------------------------------

:nagios-plugins的检测脚本也是需要系统命令的支持,在安装的时候,如果系统里没有相应的命令,对应的脚本是不会安装在系统上面的。如果发现缺少自己想要的脚本,有可能就是系统缺少脚本对应的命令,导致安装检测不到所以才没有装的

*/

4.配置nagios

安装阶段完成后,在/usr/local/nagios/中找到nagios的默认配置

Vim /usr/local/nagios/etc/nagios.cfg

取消注释该配置项

cfg_dir=/usr/local/nagios/etc/servers

新建文件夹

mkdir -p /usr/local/nagios/etc/servers

contact.cfg文件中配置Nagios联系人

vim /usr/local/nagios/etc/objects/contacts.cfg

vim /usr/local/nagios/etc/objects/templates.cfg

define host{

        name                            generic-host    ; The name of this host template

        notifications_enabled           1

/usr/local/nagios/etc/objects/commands.cfg

echo "Nagios Test Email " | sendmail -s "Alert" [email protected]@qq.com

 

 

技术图片 

二、配置Apache

1.启用apache模块

sudo a2enmod rewrite

Sudo a2enmod cgi

sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

输入密码用于浏览器端登陆

2. 启用nagios虚拟主机

sudo ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/

3. 启动ApacheNagios

service apache2 restart

Service nagios start

/*

启东时可能出错1Starting nagios (via systemctl): nagios.serviceFailed

解决

cd /etc/init.d/
cp /etc/init.d/skeleton /etc/init.d/nagios

vim /etc/init.d/nagios

添加如下代码

DESC="Nagios"
NAME=nagios
DAEMON=/usr/local/nagios/bin/$NAME
DAEMON_ARGS="-d /usr/local/nagios/etc/nagios.cfg"
PIDFILE=/usr/local/nagios/var/$NAME.lock

*/

可能出错:Failed to start nagios.service

vi /etc/systemd/system/nagios.service

[Unit]
Description=Nagios
BindTo=network.target
[Install]
WantedBy=multi-user.target
[Service]
User=nagios
Group=nagios
Type=simple
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg

重启nagios

/etc/init.d/nagios restart

执行命令 /etc/init.d/nagios restart 

三、测试Nagios服务器

四、添加主机监控

1.安装nrpe服务

apt-get install nagios-nrpe-server nagios-plugins

2.配置nrpe

vim /etc/nagios/nrpe.cfg

暂时可加可不加

server_address = 192.168.238.151

3.重新启动NRPE

service nagios-nrpe-server restart

4.将被监控主机添加到Nagios服务器

接下来操作在nagios服务器端

vim /usr/local/nagios/etc/servers/ubuntu_host.cfg

添加以下行

# Ubuntu Host configuration file

define host {

        use                          linux-server

        host_name                    ubuntu_host

        alias                        ubuntu Host

        address                     192.168.238.151

        register                     1

}

define service {

      host_name                       ubuntu_host

      service_description             PING

      check_command                   check_ping!100.0,20%!500.0,60%

      max_check_attempts              2

      check_interval                  2

      retry_interval                  2

      check_period                    24x7

      check_freshness                 1

      contact_groups                  admins

      notification_interval           2

      notification_period             24x7

      notifications_enabled           1

      register                        1

}

define service {

      host_name                       ubuntu_host

      service_description             Check Users

      check_command           check_local_users!20!50

      max_check_attempts              2

      check_interval                  2

      retry_interval                  2

      check_period                    24x7

      check_freshness                 1

      contact_groups                  admins

      notification_interval           2

      notification_period             24x7

      notifications_enabled           1

      register                        1

}

define service {

      host_name                       ubuntu_host

      service_description             Local Disk

      check_command                   check_local_disk!20%!10%!/

      max_check_attempts              2

      check_interval                  2

      retry_interval                  2

      check_period                    24x7

      check_freshness                 1

      contact_groups                  admins

      notification_interval           2

      notification_period             24x7

      notifications_enabled           1

      register                        1

}

define service {

      host_name                       ubuntu_host

      service_description             Check SSH

      check_command                   check_ssh

      max_check_attempts              2

      check_interval                  2

      retry_interval                  2

      check_period                    24x7

      check_freshness                 1

      contact_groups                  admins

      notification_interval           2

      notification_period             24x7

      notifications_enabled           1

      register                        1

}

define service {

      host_name                       ubuntu_host

      service_description             Total Process

      check_command                   check_local_procs!250!400!RSZDT

      max_check_attempts              2

      check_interval                  2

      retry_interval                  2

      check_period                    24x7

      check_freshness                 1

      contact_groups                  admins

      notification_interval           2

      notification_period             24x7

      notifications_enabled           1

      register                        1

}

5.检查配置

 

 

 

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg技术图片 

//如果检测不到hosts vim /usr/local/nagios/etc/nagios.cfg去掉server所在行的注释

6.重启所有服务

service nagios-nrpe-server restart

service apache2 restart
service nagios restart

 

 

以上是关于nagios的主要内容,如果未能解决你的问题,请参考以下文章

najios 怎么监控nginx日志

linux 除了zabbix还有啥监控软件

Linux监控系统之Nagios

运维监控三剑客之Nagios

如何编写一个 Linux bash 脚本来告诉我 LAN 中哪些计算机处于打开状态?