監控系統nagios(1)

Nagiosphp

1.1 Nagios簡介

Nagios是一個監視系統運行狀態和網絡信息的監視系統。Nagios能監視所指定的本地或遠程主機以及服務,同時提供異常通知功能等。[1] mysql

Nagios可運行在Linux/Unix平臺之上,同時提供一個可選的基於瀏覽器的WEB界面以方便系統管理人員查看網絡狀態,各類系統問題,以及日誌等等。linux

Nagios 能夠監控的功能有:ios

1、監控網絡服務(SMTPPOP3HTTPNNTPPING等);web

2、監控主機資源(處理器負荷、磁盤利用率等);sql

3、簡單地插件設計使得用戶能夠方便地擴展本身服務的檢測方法;apache

4、並行服務檢查機制;windows

5、具有定義網絡分層結構的能力,用"parent"主機定義來表達網絡主機間的關係,這種關係可被用來發現和明晰主機宕機或不可達狀態;瀏覽器

6、當服務或主機問題產生與解決時將告警發送給聯繫人(經過EMail、短信、用戶定義方式);bash

7、能夠定義一些處理程序,使之可以在服務或者主機發生故障時起到預防做用;

8、自動的日誌滾動功能;

9、能夠支持並實現對主機的冗餘監控;

10、可選的WEB界面用於查看當前的網絡狀態、通知和故障歷史、日誌文件等;[1] 

11、能夠經過手機查看系統監控信息;

12、可指定自定義的事件處理控制器;[2] 

 

系統的安裝

1.2 環境設置

同步時間:

crontab –e

*/5 * * * * /usr/sbin/ntpdate pool.ntp.org>/dev/null 2>&1

關閉防火牆:

/etc/init.d/iptables stop

關閉selinux

[root@olwang-2 etc]# getenforce

Disabled

1.3 服務器端安裝:

創建用戶和組:

# useradd -s /sbin/nologin nagios
# mkdir /usr/local/nagios
# chown -R nagios.nagios /usr/local/nagios

 

並將nagios以及apache用戶加入到nagcmd組中,確保nagiosapache有權限:

# groupadd nagcmd

# usermod –G nagcmd nagios

# usermod –G nagcmd apache

 

安裝lamp

yum -y install httpdmysql-server perl-DBI perl-DBD-MySQL php php-devel php-mysqlphp-snmp php-pdophp-gd lm_sensors net-snmp net-snmp-libs net-snmp-utilsnet-snmp-devel

 

依賴庫的安裝:

yum install gccyum install gcc glibc glibc-common-y

yum install gd gd-devel -y

yum install mysql-server -y

yum instll httpd php php-gd -y

yum install httpd php php-gd –y

 

安裝nagios

tar xf nagios.tar.gz

cd nagios

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

make all

make install

make install-init

make install-commandmode

make install-config

make install-webconf

/usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg

當以上安裝完畢之後就能夠在web界面看到ngios

wKioL1gjyT7xM1yVAAIwdiVmOKs750.png-wh_50

1.3.1 安裝插件

1.3.1.1       安裝插件nagios-plugs

cd nagios-plugins-1.4.16

./configure --with-nagios-user=nagios--with-nagios-group=nagios --enable-perl-modules --with-mysql

Make

Make install

檢查插件

ls /usr/local/nagios/libexec/|wc –l

59

1.3.1.2       安裝nrpe插件

,這個插件式客戶端的插件,由於服務器這臺機器也要監控,因此這臺機器咱們也裝上。

tar xf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config

ls /usr/local/nagios/libexec/check_nrpe

ls /usr/local/nagios/libexec/|wc –l

60

1.3.1.3       其餘插件安裝

tar xf Class-Accessor-0.31.tar.gz

cd Class-Accessor-0.31

perl Makefile.PL

make

make install

#

tar xf Config-Tiny-2.12.tar.gz

cd Config-Tiny-2.12

perl Makefile.PL

make

make install

cd ..

 

###

tar xf Math-Calc-Units-1.07.tar.gz

cd Math-Calc-Units-1.07

perl Makefile.PL

make

make install

cd ..

#

tar xf Nagios-Plugin-0.34.tar.gz

cd Nagios-Plugin-0.34

perl Makefile.PL

make

make install

cd ..

#################

tar xf Params-Validate-0.91.tar.gz

cd Params-Validate-0.91

perl Makefile.PL

make

make install

cd ..

####

tar xf Regexp-Common-2010010201.tar.gz

cd Regexp-Common-2010010201

perl Makefile.PL

make

make install

1.3.2 配置並啓動nagios服務

chkconfig nagios on

/etc/init.d/nagios start

echo "/etc/init.d/nagios start">>/etc/rc.local

配置文件驗證:

[root@olwang-2 nrpe-2.12]# /etc/init.d/nagioscheckconfig

Running configuration check... OK.

1.4 客戶端安裝:

1.4.1 環境準備

同步時間:

crontab –e

*/5 * * * * /usr/sbin/ntpdate pool.ntp.org>/dev/null 2>&1

關閉防火牆:

/etc/init.d/iptables stop

關閉selinux

[root@olwang-2 etc]# getenforce

Disabled

 

建立用戶:

useradd nagios -M -s /sbin/nologin

安裝依賴庫:

yum install perl-devel perl-CPAN openssl-devel -y

yum install perl-devel openssl-devel –y

1.4.2 插件的安裝

1.4.2.1       nagios-plugins安裝

tar xf nagios-plugins-1.4.16.tar.gz

cd nagios-plugins-1.4.16

./configure --with-nagios-user=nagios--with-nagios-group=nagios --enable-perl-modules --with-mysql

make

make install

cd ..

插件檢查

[root@olwang-2 ~]# ls/usr/local/nagios/libexec/|wc -l

62

1.4.2.2       Nrpe安裝

tar xf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure

make all

make install-daemon

make install-daemon-config

make install-plugin

1.4.2.3       Class-Accessor安裝

tar xf Class-Accessor-0.31.tar.gz

cd Class-Accessor-0.31

perl Makefile.PL

make

make install

#

 

 

tar xf Config-Tiny-2.12.tar.gz

cd Config-Tiny-2.12

perl Makefile.PL

make

make install

cd ..

 

###

tar xf Math-Calc-Units-1.07.tar.gz

cd Math-Calc-Units-1.07

perl Makefile.PL

make

make install

cd ..

#

tar xf Nagios-Plugin-0.34.tar.gz

cd Nagios-Plugin-0.34

perl Makefile.PL

make

make install

cd ..

#################

tar xf Params-Validate-0.91.tar.gz

cd Params-Validate-0.91

perl Makefile.PL

make

make install

cd ..

####

tar xfRegexp-Common-2010010201.tar.gz

cd Regexp-Common-2010010201

perl Makefile.PL

make

make install

 

1.4.3 生成登錄web的密碼

htpasswd -bc /usr/local/nagios/etc/htpasswd.usersusername password

 

 

1.4.4 客戶端nrpe啓動腳本:

chmod +x /etc/init.d/nrpe

 

 

[root@olwang-2 etc]# cat /etc/init.d/nrpe

#/bin/sh

Usage(){

echo "pls input (start|stop|restart)"

 

}

case $1 in

         start)

               /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d          

             ;;

          stop)

             pkill nrpe

              ;;

       restart)

             pkill nrpe

             /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

             ;;

            *)

             Usage

esac

1.5 服務器端配置文件

 

下面目錄中的文件只是重要的幾個 不是所有。其餘不重要的就不在這裏列舉了。

[root@olwang ngios]# tree/usr/local/nagios/etc/

|--cgi.cfg        配置一些用戶權限的文件           

|--htpasswd.users   保存用戶名和密碼的文件

|--nagios.cfg      

|--nrpe.cfg     這個文件主要是來配置nrpe模塊的具體命令,以及設置語序訪問的服務器ip

|--objects    項目目錄

|   |-- commands.cfg  命令模板

|   |-- contacts.cfg  配置郵件

|   |-- hosts.cfg    配置客戶端的信息

|   |-- services   

|   |-- templates.cfg 模板

 

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

增長如下幾行

cfg_file=/usr/local/nagios/etc/objects/hosts.cfg

cfg_file=/usr/local/nagios/etc/objects/services.cfg

cfg_dir=/usr/local/nagios/etc/objects/services 

wKiom1gjyeXhJtuRAAAb2OJbN3s469.png-wh_50

註釋掉下面一行,此行是監控本地機器的


 wKioL1gjyfCjkCq5AAALOfcgCHw679.png-wh_50

修改配置文件cgi.cfg

此文件解決nagiosweb界面不顯示服務的問題。

[root@olwang-2 etc]# sed -i's#nagiosadmin#oldboy#g' cgi.cfg

[root@olwang-2 etc]# grep oldboy cgi.cfg

authorized_for_system_information=oldboy

authorized_for_configuration_information=oldboy

authorized_for_system_commands=oldboy

authorized_for_all_services=oldboy

authorized_for_all_hosts=oldboy

authorized_for_all_service_commands=oldboy

authorized_for_all_host_commands=oldboy

 

添加如下文件並給權限

cd objects/

head -51 localhost.cfg >hosts.cfg

chown nagios.nagios hosts.cfg

touch services.cfg

chown nagios.nagios services.cfg

mkdir services

chown nagios.nagios services

 

 

配置文件hosts.cfg 

# Define a host for the local machine

 

define host{

       use                    linux-server                                                                    

       host_name               olwang-1

       alias                  nagios-client-2

       address                192.168.5.130

 

       max_check_attempts      3

       normal_check_interval   2

        process_perf_data       1

       action_url             /nagios/pnp/index.php?host=$HOSTNAME$

 

        }

define host{

        use                     linux-server                                                                        host_name               olwang

       alias                  nagios-server

       address                192.168.5.129

 

       max_check_attempts      3

       normal_check_interval   2

       process_perf_data       1

       action_url             /nagios/pnp/index.php?host=$HOSTNAME$

 

        }

 

define host{

       use                    linux-server                                                                                                                 

       host_name               olwang-2

        alias                   nagios-client-2

       address                192.168.5.131

 

       max_check_attempts      3

       normal_check_interval   2

       process_perf_data       1

       action_url             /nagios/pnp/index.php?host=$HOSTNAME$

        }

#

# HOST GROUP DEFINITION

define hostgroup{

       hostgroup_name  linux-servers ;The name of the hostgroup

       alias           Linux Servers ;Long name of the group

       members        olwang,olwang-1,olwang-2

                                                        }

 

 

 

添加監控模板

配置文件commands.cfg

#'check_nrpe'

define command{

       command_name    check_nrpe

       command_line    $USER1$/check_nrpe-H "$HOSTADDRESS$" -c $ARG1$ -t 30

        }

#'check_mem'

define command{

        command_name    check_mem

       command_line    $USER1$/check_mem-w $ARG1$ -c $ARG2$

        }

#'check_iostat'

define command{

       command_name    check_iostat

       command_line   $USER1$/check_iostat -w $ARG1$ -c $ARG2$

        }

 

define command{

       command_name    check_weburl

       command_line    $USER1$/check_http$ARG1$ -w 10 -c 30

        }

 

1.6 客戶端配置文件修改

配置文件nrpe.cfg

 設置服務器端的ip

 77# NOTE: This option is ignored if NRPE is running under either inetd or xinetd

  78

  79 allowed_hosts=192.168.5.129

 這裏註釋掉199-203,添加205-209.(針對主機性能的監控)

199#command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10

200#command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c30,25,20

 201#command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p/dev/hda1

 202#command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10-s Z

203#command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

204

205command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

206command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p /

207command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

208command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10

209command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%

 

啓動客戶端守護進程

/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg –d

1.7報錯

 

 

 wKiom1gjygry7_6EAACJv1FGQhM492.png-wh_50

錯誤日誌:

 [1477273522] SERVICE NOTIFICATION:nagiosadmin;olwang-2;Disk Partition;UNKNOWN;notify-service-by-email;Invalidhost name -c

[1477273552] SERVICE NOTIFICATION:nagiosadmin;olwang-1;Disk Iostat;UNKNOWN;notify-service-by-email;Invalid hostname -c

[1477273602] SERVICE NOTIFICATION:nagiosadmin;olwang-2;Iostat;UNKNOWN;notify-service-by-email;Invalid host name-c

[1477273652] SERVICE NOTIFICATION:nagiosadmin;olwang-1;Disk Partition;UNKNOWN;notify-service-by-email;Invalidhost name -c

[1477273702] SERVICE NOTIFICATION:nagiosadmin;olwang-2;Load;UNKNOWN;notify-service-by-email;Invalid host name -c

[1477273752] SERVICE NOTIFICATION:nagiosadmin;olwang-1;MEM Usage;UNKNOWN;notify-service-by-email;Invalid hostname -c

[1477273802] SERVICE NOTIFICATION:nagiosadmin;olwang-2;MEM Usage;UNKNOWN;notify-service-by-email;Invalid hostname -c

[1477273852] SERVICE ALERT:olwang-1;Ping;OK;HARD;3;PING OK - Packet loss = 0%, RTA = 1.95 ms

[1477273952] SERVICE NOTIFICATION:nagiosadmin;olwang-1;Swap Usage;UNKNOWN;notify-service-by-email;Invalid hostname -c

[1477274002] SERVICE NOTIFICATION: nagiosadmin;olwang-2;SwapUsage;UNKNOWN;notify-service-by-email;Invalid host name -c

[1477274052] SERVICE NOTIFICATION:nagiosadmin;olwang-1;Current Load;UNKNOWN;notify-service-by-email;Invalid hostname -c

解決辦法:

Hosts解析問題。

修改文件/etc/hosts

問題2

[root@olwang-2 ~]#/usr/local/nagios/libexec/check_memory

-bash:/usr/local/nagios/libexec/check_memory: /usr/bin/perl^M: bad interpreter: Nosuch file or directory

wKiom1gjyiKyTVIyAABZAHpAtuI832.png-wh_50

問題總結:

*nix系統下使用Perl腳本有時會遇到以下錯誤:
/usr/bin/perl^M: bad interpreter: No such file ordirectory
最多見的緣由是由於該腳本在windows系統下進行了編輯。
windows系統下的換行符是\r\n,而unix下面是隻有\n的。若是要解決這個問題,只要去掉\r便可。


第一種解決方案是用sed(假設出問題的腳本名叫filename):

解決辦法:

 sed-i 's/\r$//' /usr/local/nagios/libexec/check_memory

 

 

問題3

wKiom1gjylPhXx2yAABr5PMZbws112.png-wh_50

解決辦法:

       遇到這個問題,首先要檢查咱們是否咱裝了openssl openssl-devel,若是檢查沒問題。

       下面就來檢查一下客戶端配置文件/usr/local/nagios/etc/nrpe.cfg

        wKioL1gjyl_AOl0yAABPaBvR3_k121.png-wh_50


若以上兩個問題解決,這個報錯也就解決了。

相關文章
相關標籤/搜索