nagios

Nagios

1.1 Nagios簡介

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

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

Nagios 可以監控的功能有:

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

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

3、簡單地插件設計使得用戶可以方便地擴展自己服務的檢測方法;

4、並行服務檢查機制;

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

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

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 httpd mysql-server perl-DBI perl-DBD-MySQL php php-devel php-mysqlphp-snmp php-pdo php-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

 

安裝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

wKiom1j5cG3gpD7BAAIwdZ9Gf0E306.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

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 

wKioL1j5cN7C-XcOAAAb2OJbN3s404.png-wh_50

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

wKioL1j5c4HiluZkAAALOfcgCHw524.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 thegroup

       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 客戶端配置文件修改

設置服務器端的ip

77 # NOTE: This option is ignored if NRPE is running under either inetdor 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 -c 30,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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章