溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

發(fā)布時間:2020-06-01 16:25:29 來源:網(wǎng)絡(luò) 閱讀:20057 作者:haidongqing07 欄目:移動開發(fā)

   Nagios的功能以及它所使用的場景在此不必多說.網(wǎng)上很多這方面資料.本文主要介紹Nagios-SNMP-Plugins 來實(shí)現(xiàn)對企業(yè)中的服務(wù)器的硬件的監(jiān)控,如:磁盤,CPU ,內(nèi)存,網(wǎng)卡等在出現(xiàn)異常或恢復(fù)時通知管理員并使用PNP插件結(jié)合rrdtool實(shí)現(xiàn)繪圖


一.實(shí)驗(yàn)環(huán)境

1.監(jiān)控機(jī):CentOS5.4_x32(192.168.1.253)

2.兩臺被監(jiān)控機(jī)器:Windows (192.168.1.206),Linux(192.168.1.124)

3.所需軟件:

       Nagios3.3.1

       Nagios-plugins-1.4.16    

       Nagios-snmp-plugins.1.1.1

       Rrdtool

       Pnp4nagios-0.6.18

       sendEmail


二.部署過程

1.確保系統(tǒng)開發(fā)組件已安裝

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

2.使用yum安裝LAMP環(huán)境

   yum -y install php php-cli php-common php-pdo php-gd php-mysql php-devel php-mbstrin

   yum -y install httpd httpd-devel system-config-httpd

   yum -y install mysql-devel mysql-server mysql

   yum -y install gd gd-devel

注:以上組件件中有PHP擴(kuò)展,關(guān)于gd gd-devel必需安裝不然map會出現(xiàn)異常.ndo2db會將的nagios信息轉(zhuǎn)入MySQL數(shù)據(jù)庫

3.啟動Apache和MySQL

   chkconfig --add httpd || chkconfig httpd on//添加apache服務(wù)開機(jī)啟動

   chkconfig --add mysqld || chkconfig mysqld on  //添加MySQL服務(wù) 開機(jī)啟動

   service httpd restart  啟動Apache

   service mysqld restart 啟動MySQL

4.建立php測試文件,并驗(yàn)證

   vim /var/www/index.php

   <?

       phpinfo();

   ?>

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

三.依次安裝所需軟件

1.安裝Nagios

   groupadd nagcmd

   useradd -G nagcmd nagios

   passwd nagios //手動設(shè)置密碼

   usermod -a -G nagcmd apache

   cd nagios-3.3.1

   ./configure --with-command-group=nagcmd --enable-event-broker //--enable-enevnt-broker參數(shù)用于實(shí)現(xiàn)NDO的功能

   make all

   make install

   make install-init

   make install-commandmode

   make install-config

   htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //指定Nagios的web接口登錄密碼

   make install-webconf

2.安裝Nagios-plugins

   ./configure --with-nagios-user=nagios --with-nagios-group=nagios

   make

   make install

   chkconfig --add nagios || chkconfig nagios on //配置Nagios開機(jī)啟動    

   /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //檢測無異常即可以啟動服務(wù)

   service nagios restart //重啟apache服務(wù)

   service httpd restart //啟動nagios服務(wù)

3.安裝Nagios-snmp-plugins

   Crypt-DES-2.07.tar.gz

   Digest-HMAC-1.03.tar.gz

   Digest-MD5-2.53.tar.gz

   Digest-SHA1-2.13.tar.gz

   Net-SNMP-v6.0.1.tar.gz    

   分別解壓進(jìn)入各目錄執(zhí)行以下操作

   perl Makefile

   make test

   make install

   tar -zxvf nagios-snmp-plugins.tar.gz

   cd nagios_plugins

   ./install //依次回車即可,完成后會在/usr/local/nagios/libexec目錄中生成相應(yīng)的健康腳本

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

重啟apache 和nagios

瀏覽器中輸入:http://192.168.1.253/nagios

用戶名:nagiosadmin

密碼:上面設(shè)置的密碼

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

三.配置監(jiān)控

1,配置/usr/local/.nagios/etc/object/command.cfg文件,在文件結(jié)尾處添加以下內(nèi)容.


###############################SNMP

define command {

command_name          check_tcpport

command_line          $USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$ -w $ARG2$ -c $ARG3$

}

define command {

command_name          check_snmp_disk

command_line          $USER1$/check_snmp_storage.pl -H $HOSTADDRESS$ $USER7$ -p $ARG1$ -m $ARG2$ -w $ARG3$ -c $ARG4$

}

define command {

command_name          check_snmp_mem

command_line          $USER1$/check_snmp_storage.pl -H $HOSTADDRESS$ $USER7$ -p $ARG1$ -m $ARG2$ -w $ARG3$ -c $ARG4$ -f

}

define command {

command_name          check_snmp_cpuload

command_line          $USER1$/check_snmp_load.pl -H $HOSTADDRESS$ $USER7$ -p $ARG1$ -w $ARG2$ -c $ARG3$ -f

}

(-f 不可缺少否則無法出圖)

配置監(jiān)控之前先檢測被檢控主機(jī)狀態(tài),被控機(jī)上開啟SNMP服務(wù)并配置好團(tuán)體名及所接收來自哪些區(qū)域的請求.

windows

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

linux

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

測試成功就可以進(jìn)行配置了.

2.定義監(jiān)控主機(jī)

  在nagios.cfg中開啟被監(jiān)控主機(jī)的目錄

   開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

添加windows主機(jī)監(jiān)控文件開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

以下為192.168.1.206這臺機(jī)器的監(jiān)控配置

define host{

       use             windows-server

       host_name       web-192.168.1.206

       alias           My Windows Server

       address         192.168.1.206

       hostgroups      windows-server

       }



##################################### //這里是分組,注,分組中需要指定一次

define hostgroup{

       hostgroup_name  windows-server

       alias           windows-server

}


define hostgroup{

       hostgroup_name  linux-server

       alias           linux-server

}

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

define service{

       use                     generic-service

       host_name               web-192.168.1.206

       service_description     PING

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

       }


###########Memory

define service{

       use                     generic-service

       host_name               web-192.168.1.206

       service_description     Memor

       check_command           check_snmp_mem!161!"Physical Memory"!10!20

檢測語法是否有錯

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

無報(bào)錯即可重啟nagios 服務(wù)

service nagios restart

刷新監(jiān)控頁面

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

三.配置監(jiān)控對像使用量繪圖功能

1.安裝cgilib

tar -zxvf cgilib-0.6.tar.gz

cd cgilib-0.6

make

cp aux.h /usr/include/

cp cgi.h /usr/include/

cp libcgi.a /usr/lib/


2.安裝rrdtool

yum -y install ruby

yum -y install pango pango-devel

rpm -ivh rrdtool *

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

3.安裝PNP

cd pnp4nagios-0.6.18

./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata

make all

make install

make install-config


4.配置apache

vi /etc/httpd/conf/httpd.conf

末尾添加如下信息

Alias /pnp4nagios "/usr/local/pnp4nagios/share"

<Directory "/usr/local/pnp4nagios/share">

       AllowOverride None

       Order allow,deny

       Allow from all

       AuthName "Nagios Access"

       AuthType Basic

       AuthUserFile /usr/local/nagios/etc/htpasswd.users

       Require valid-user

       <IfModule mod_rewrite.c>

               RewriteEngine On

               Options FollowSymLinks

               RewriteBase /pnp4nagios/

               RewriteRule ^(application|modules|system) - [F,L]

               RewriteCond %{REQUEST_FILENAME} !-f

               RewriteCond %{REQUEST_FILENAME} !-d

               RewriteRule .* index.php/$0 [PT,L]

       </IfModule>

</Directory>


5.配置template.cfg

define host {

 name       host-pnp

 action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_

 register   0

}

define service {

  name       srv-pnp

action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$

  register   0

}

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

6.配置commands.cfg

先注釋原有的再添加

define command {

      command_name  process-service-perfdata

      command_line  /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl

}

define command {

      command_name  process-host-perfdata

      command_line  /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA

}

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

7.配置nagios.cfg

process_performance_data=1

enable_environment_macros=1

host_perfdata_command=process-host-perfdata

service_perfdata_command=process-service-perfdata


8.更新PHP至 5.2(CentOS 6.2可yum安裝)

yum install php-gd

rpm --import http://repo.webtatic.com/yum/RPM-GPG-KEY-webtatic-andy

wget -P /etc/yum.repos.d/ http://repo.webtatic.com/yum/webtatic.repo

yum --enablerepo=webtatic --exclude=php*5.3* list php

yum --enablerepo=webtatic --exclude=php*5.3* update -y php

驗(yàn)證:

rpm -qa |grep php

service httpd restart

測試PNP .刪除或移動/usr/local/pnp4nagios/share/install.php配

9.為主機(jī)或服務(wù)配置繪圖

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

重新啟動服務(wù).并查看監(jiān)控頁面.需要等一會才能出圖

service httpd restart

service nagios restart


開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

三.配置監(jiān)控LINUX主機(jī)也一樣,調(diào)用的模版文件改成linux-server.分組使用linux-server,前面定義過分組這里不用定義

cd /usr/local/nagios/etc/object/linux

vim 192.168.1.124.cfg

define host{

       use             linux-server,host-pnp

       host_name       web-192.168.1.124

       alias           My Linux Server

       address         192.168.1.124

       hostgroups      linux-server

       }

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

define service{

       use                     generic-service,srv-pnp

       host_name               web-192.168.1.124

       service_description     PING

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

       }


###########Memory

define service{

       use                     generic-service,srv-pnp

       host_name               web-192.168.1.124

       service_description     Mem

       check_command           check_snmp_mem!161!"Real Memory"!90!99

       }


###########disk/

define service{

       use                     generic-service,srv-pnp

       host_name               web-192.168.1.124

       service_description     disk-/

       check_command           check_snmp_stro!161!/!80!90

       }

#####CPU

define service{

       use                     generic-service,srv-pnp

       host_name               web-192.168.1.124

       service_description     cpuload

       check_command           check_snmp_cpuload!161!80!90

       }


###########Traffic

define service{

       use                     generic-service,srv-pnp

       host_name               web-192.168.1.124

       service_description     eth0

       check_command           check_snmp_traffic!2!200,100!300,200

       }

#####Process

define service{

       use                     generic-service,srv-pnp

       host_name               web-192.168.1.124

       service_description     httpd

       check_command           check_snmp_proc!httpd!3!20!100,200

       }

define service{

       use                     generic-service,srv-pnp

       host_name               web-192.168.1.124

       service_description     port80

       check_command           check_tcpport!80!0.0023!0.0067

       }


開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

五:配置告警功能

1.定義宏變量

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

2.sendEmail的用法

下載 wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

tar -zxvf sendEmail

cd sendEmail

cp -p sendEmail  /usr/local/bin

檢測發(fā)信

/usr/local/bin/sendEmail –f nagios@test.com –t yahoon@test.com –s mail.test.com –u “from nagios” –xu nagios –xp p#3isoda –m happy

解釋:-f 表示發(fā)送者的郵箱

-t 表示接收者的郵箱

-s 表示SMTP服務(wù)器的域名或者ip

-u 表示郵件的主題-xu

表示SMTP驗(yàn)證的用戶名-xp

表示SMTP驗(yàn)證的密碼(注意,這個密碼貌似有限制,例如我用d!5neyland就不能被正確識別)

-m 表示郵件的內(nèi)容

注:以上內(nèi)容來源于網(wǎng)絡(luò)

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

添加告警配置

# 'notify-service-by-email' command definition

define command{

      command_name notify-host-by-email

      command_line /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTA

TE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$nnDate/Time: $LONGDATETIME$n" | /usr/local/bin/sendEmail -s $USER8$ -xu $USER9$ -xp $US

ER10$ -t $CONTACTEMAIL$ -f $USER9$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "***

** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTATE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$

nnDate/Time: $LONGDATETIME$n"

}

# 'notify-service-by-email' command definition

define command{

      command_name notify-service-by-email

      command_line /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nnService: $SERVICEDESC$nHost: $H

OSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nnDate/Time: $LONGDATETIME$nnAdditional Info:nn$SERVICEOUTPUT$" | /usr/local/b

in/sendEmail -s $USER8$ -xu $USER9$ -xp $USER10$ -t $CONTACTEMAIL$ -f $USER9$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Servic

e Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -m "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nnService: $SE

RVICEDESC$nHost: $HOSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nnDate/Time: $LONGDATETIME$nnAdditional Info:nn$SERVICEOUTP

UT$"

}

定義聯(lián)系人,可以定義多個用逗號隔開

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

關(guān)掉系統(tǒng)自帶的sendmai

service sendmail stop

chkconfig sendmail off

重啟服務(wù).觀測頁面上的notification選項(xiàng)和上面定義的收信郵箱

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)

開源監(jiān)控軟件Nagios之SNMP實(shí)戰(zhàn)


本次實(shí)驗(yàn)先做到這里,稍后附上ndo2db+nagvis的配置..

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI