1. 程式人生 > >Nagios的安裝

Nagios的安裝

nagios monitor

監控對於對於系統管理人員的日常運維工作來說是非常重要的,而比較知名的開源監控軟件有Ganglia、Cacti、Nagios、Zabbix等等,而本次就簡單的說下Nagios的安裝部署,因為Nagios是基於web頁面查看管理的,那可以選用lamp或者是lnmp這些比較容易實現的web系統框架來實現即可,Nagios的官網是:https://www.nagios.org/downloads/,當然下載版本建議用最新的穩定版。Nagios的監控實現是由各個插件來完成的它本身是沒有監控功能的,所以Nagios使用非常靈活,而在需要監控不同的平臺主機、硬件時用不同的插件,下面從網頁上找的一張圖能很好的說明:技術分享

從圖中得知Nagios監控中的插件SNMP在不同平臺的主機、硬件上都有用到,而Linux中有send-nsca和NRPE一般我們常用的是NRPE,監聽的是tcp的5666端口;而在Windows主機上使用的是NSClient++,監聽的是12489和5666端口,這一點很重要做為被監控的主機上一定要開放這些端口。

在安裝Nagios之前需要先安裝Nagios的監控主機(server端),然後再到被監控主機(clinet端)上安裝相應的插件。

1、server端:

在安裝之前先要檢查依賴包

[[email protected] ~]# yum -y groupinstall "Development Tools" "Development Libraries"#在配置好yum源的主機上,先檢查下開發包組是否安裝
[[email protected]
/* */ ~]# yum -y install httpd php php-mysql gd gd-devel sendmail openssl-devel#這裏是測試就沒有用編譯包的php和apache,一般來說建議使用編譯包

在依賴關系解決後就開始正常的Nagios安裝,先安裝Nagios的核心引擎和web頁面

[[email protected] ~]# groupadd -r nagcmd#添加nagcmd組
[[email protected] ~]# useradd -M -G nagcmd -r -s /sbin/nologin nagios
[[email protected]
/* */ ~]# passwd nagios [[email protected] ~]# usermod -a -G nagcmd apache#把nagcmd組附屬在apache組 [[email protected] ~]# cd /usr/local/src/nagios-4.3.1 [[email protected] nagios-4.3.1]# ./configure --sysconfdir=/etc/nagios --prefix=/usr/local/nagios --with-command-group=nagcmd --enable-event-broker [[email protected] nagios-4.3.1]# make all && make install [[email protected] nagios-4.3.1]# make install-init && make install-commandmode && make install-config#添加相應的插件 [[email protected] nagios-4.3.1]# vim /etc/nagios/objects/contacts.cfg#修改郵件接收人的配置文件,在此就不做過多的說明,配置文件的註釋裏都有,主要改的就是郵箱地址 [[email protected] nagios-4.3.1]# make install-webconf#配置web配置文件 [[email protected] nagios-4.3.1]# htpasswd -c /etc/nagios/htpasswd.users nagiosadmin#配置Nagios的頁面密碼 New password: Re-type new password: Adding password for user nagiosadmin

到此nagios的核心引擎和基本web頁面就已經安裝完畢,在此直接啟動httpd後使用瀏覽器測試下技術分享

此時的Nagios是不具備任何監控功能,因為這臺Nagios主機是Linux服務器,所以監控插件在這裏選比較常用的nrpe,在安裝nrpe之前要先安裝好Nagios的插件plugins

[[email protected] nagios-4.3.1]# cd ../nagios-plugins-2.1.4
[[email protected] nagios-plugins-2.1.4]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[[email protected] nagios-plugins-2.1.4]# make && make install
[[email protected] nagios-plugins-2.1.4]# chkconfig --add nagios#添加Nagios的服務
[[email protected] nagios-plugins-2.1.4]# chkconfig nagios on#Nagios開機自啟動
[[email protected] nagios-plugins-2.1.4]# /etc/init.d/nagios start
[[email protected] nagios-plugins-2.1.4]# getenforce#此處要註意的是要檢查是否服務器上有開啟selinux,如果有開啟要選擇關閉吧或者在selinux中添加nagios的相應文件到可以執行的標簽中
Enforcing
[[email protected] nagios-plugins-2.1.4]# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
[[email protected] nagios-plugins-2.1.4]# chcon -R -t httpd_sys_content_t /usr/local/nagios/share/

此時在server端上的Nagios的插件就已經安裝完畢,此時便可以安裝Nagios的nrpe插件

[[email protected] nagios-plugins-2.1.4]# cd ../nrpe-3.0.1/
[[email protected] nrpe-3.0.1]# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl --sysconfdir=/etc/nagios
[[email protected] nrpe-3.0.1]# make all && make install-plugin

這樣在server端上的Nagios就已經安裝部署完畢,此時Nagios已經可以自行監控本機。技術分享

2、clinet端:

此處的客戶端為了快速簡單的部署實現,在這裏就用另外一臺Linux服務器來搭建。同理,在Nagios的client端下也要添加相應的用戶。當然,也要先安裝好Nagios的插件

[[email protected] ~]# useradd -M -r -s /sbin/nologin nagios
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# cd nagios-plugins-2.1.4
[[email protected] nagios-plugins-2.1.4]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[[email protected] nagios-plugins-2.1.4]# make all && make install

在插件安裝完畢後開始安裝nrpe

[[email protected] nagios-plugins-2.1.4]# cd ../nrpe-3.0.1/
[[email protected] nrpe-3.0.1]# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl --sysconfdir=/etc/nagios
[[email protected] nrpe-3.0.1]# make all && make install-plugin
[[email protected] nrpe-3.0.1]# make install-daemon && make install-config && make install-init && make install-inet

到這裏Nagios的nrpe部分就安裝結束了,以下做一些基本配置

[[email protected] nrpe-3.0.1]# vim /etc/nagios/nrpe.cfg
…略…
allowed_hosts=192.168.218.128
#在這裏的ip改成server端的Nagios的監聽ip
…略…
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
#此處根據磁盤的實際使用的盤符情況修改
…略…
[[email protected] nrpe-3.0.1]# /etc/init.d/nrpe start

在安裝好後做一些基本的驗證,這裏要註意的是這些驗證需要在監控server主機上做,被監控主機需要開啟5666端口

[[email protected] nrpe-3.0.1]# cd /usr/local/nagios/libexec/
[[email protected] libexec]# ./check_nrpe -H 192.168.218.129#檢查被監控主機的IP,這樣有打印出版本號就是正常的
NRPE v3.0.1

這樣Nagios的安裝就完成了,而Nagios的家目錄或文件的含義也很簡單,如下:

目錄
解釋
bin
Nagios的二進制文件的目錄
sbinNagios CGI 文件所在目錄,也就是執行外部命令所需文件所在的目錄
shareNagios網頁文件所在的目錄,建議這裏去找一下中文的資料便於查閱
libexecNagios 外部插件所在目錄
varNagios 日誌文件、lock 等文件所在的目錄
var/archivesNagios 日誌自動歸檔目錄
var/rw用來存放外部命令文件的目錄

當然我這裏是指定Nagios的配置文件路徑在/etc/nagios路徑下,如果沒有指定的話默認缺省是在Nagios的家目錄下,而配置的一些文件或目錄的含義也很簡單,關系如下:

文件名或目錄名用途
cgi.cfg控制CGI訪問的配置文件
nagios.cfgNagios 主配置文件
resource.cfg變量定義文件,又稱為資源文件,在些文件中定義變量,以便由其他配置文件引用,如$USER1$
objectsobjects 是一個目錄,在此目錄下有很多配置文件模板,用於定義Nagios 對象
objects/commands.cfg命令定義配置文件,其中定義的命令可以被其他配置文件引用
objects/contacts.cfg
定義聯系人和聯系人組的配置文件
objects/localhost.cfg定義監控本地主機的配置文件
objects/printer.cfg定義監控打印機的一個配置文件模板,默認沒有啟用此文件
objects/switch.cfg定義監控路由器的一個配置文件模板,默認沒有啟用此文件
objects/templates.cfg定義主機和服務的一個模板配置文件,可以在其他配置文件中引用
objects/timeperiods.cfg定義Nagios 監控時間段的配置文件
objects/windows.cfg監控Windows 主機的一個配置文件模板,默認沒有啟用此文件

在此時Nagios還是不能用的還有一些基礎配置需要修改才能正常的使用,在server端進入sysconfigdir目錄,先要根據實際情況修改一下commandss.cfg文件,添加以下內容:

[[email protected] nagios]# vim objects/commands.cfg
define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

在Nagios的默認配置文件下是沒有Linux的配置文件所以如果是用於監控Linux主機,需要自行定義寫配置文件,在此就用簡單的寫了一個Linux下使用的模板:

[[email protected] nagios]# vim objects/linux.cfg
define host{
        use             linux-server
        host_name       linuxhost
        alias           My linux Server
        address         192.168.218.129    ;這裏填被監控主機的IP
        }

define service{
        use                     generic-service
        host_name               linuxhost
        service_description     CHECK_USERS
        check_command           check_nrpe!check_users
        }

define service{
        use                     generic-service
        host_name               linuxhost
        service_description     CHECK_LOAD
        check_command           check_nrpe!check_load
        }

define service{
        use                     generic-service
        host_name               linuxhost
        service_description     CHECK_SDA1
        check_command           check_nrpe!check_sda1
        }

define service{
        use                     generic-service
        host_name               linuxhost
        service_description     CHECK_ZOMBIE_PROCS
        check_command           check_nrpe!check_zombie_procs
        }

define service{
        use                     generic-service
        host_name               linuxhost
        service_description     CHECK_TOTAL_PROCS
        check_command           check_nrpe!check_total_procs
        }
[[email protected] nagios]# chown nagios:nagios linux.cfg#修改下配置文件的屬組

在linux.cfg配置文件修改好後還需要 在nagios.cfg添加環境變量:

[[email protected] nagios]# vim nagios.cfg
…略…
cfg_file=/etc/nagios/objects/linux.cfg
…略…

正在修改後可以用Nagios的配置文件驗證檢查

[[email protected] nagios]# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg
Nagios Core 4.3.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 02-23-2017
License: GPL

Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
        Checked 13 services.
        Checked 2 hosts.
        Checked 1 host groups.
        Checked 0 service groups.
        Checked 1 contacts.
        Checked 1 contact groups.
        Checked 25 commands.
        Checked 5 time periods.
        Checked 0 host escalations.
        Checked 0 service escalations.
Checking for circular paths...
        Checked 2 hosts
        Checked 0 service dependencies
        Checked 0 host dependencies
        Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check
[[email protected] nagios]# /etc/init.d/nagios restart

像這樣就是沒有報錯的,再重啟下Nagios就可以了,這樣基本的Nagios的安裝就完畢了,這裏需要註意的是在Nagios的server端需要開啟80、443端口用於Web管理頁面的正常瀏覽,當然這也是一些基本的配置部署,如果要實現更多的功能還需要進一步的修改配置文件。

本文出自 “技術隨筆” 博客,謝絕轉載!

Nagios的安裝