Nagios

NRPE默認端口號:5666
Nagios運行在服務端

監控

1,收集信息,對監控主機,服務等進行檢測,記錄相關信息   
2,圖形表現   
3,郵件(短信,微信等)報警
4,開發接口(寫程序自定義監控)


開源的監控軟件

mrtg
ntop
cacti

nagios
zabbix
Ganglia
centreon

監控寶
360監控
阿里雲監控
等

=============================================================================

nagios
www.nagios.org

Nagios Core        --主監控引擎和基本web管理接口
Nagios Core Plugins    --監控命令包
Nagios Core Frontends    --相似皮膚
Nagios Core Addons    --其它的project(支持其它各類功能的額外項目)



準備一臺虛擬機開始安裝nagios(橋接網絡)
安裝前準備:
1,主機名
2,關閉firewalld,selinux
3,關閉NetworkManager,並配置靜態ip
4,配置本地yum,epel源,163源
5,時間同步


安裝步驟:
1,搭建rpm版lamp,不須要mysql(源碼版lamp也能夠,但nginx不行,由於後面nagios的web子配置文件裏的語法都是apache的語法)
# yum install httpd httpd-devel gd gd-devel php


2,安裝nagios
# yum install nagios\*


安裝完後確認用戶
# id nagios
uid=988(nagios) gid=983(nagios) groups=983(nagios)
# id apache
uid=48(apache) gid=48(apache) groups=48(apache),983(nagios)


apache子配置文件路徑
# /etc/httpd/conf.d/nagios.conf

主配置文件路徑
# /etc/nagios/nagios.cfg

子配置文件路徑
# ls /etc/nagios/objects/
commands.cfg  localhost.cfg  switch.cfg     timeperiods.cfg
contacts.cfg  printer.cfg    templates.cfg  windows.cfg

plugins(監控命令)路徑,目錄下有不少check開頭的命令
# ls /usr/lib64/nagios/plugins/



3,設置http訪問nagios的驗證用戶和密碼
# htpasswd /etc/nagios/passwd nagiosadmin
New password: 
Re-type new password: 
Updating password for user nagiosadmin


# nagios -v /etc/nagios/nagios.cfg     --檢查配置文件正確性

# systemctl restart httpd
# systemctl restart nagios
# systemctl enable httpd
# systemctl enable nagios
# systemctl status httpd
# systemctl status nagios



使用firefox訪問:
訪問路徑http://IP/nagios
用戶 nagiosadmin




        nagios server     ----------  client


=============================================================================


如今查看web界面,默認只監控了localhost,並監控了其8個服務

一些小操做:
1,若是http服務爲黃色,是警告,則須要把網站家目錄里加一個主頁進去(家目錄爲空,他就會警告)。
但須要等它下一次check纔會OK。若是要手動check,能夠點http,再右邊點Re-schedule the next check of this service去強制check,就OK了
echo haha > /var/www/html/index.html

2,默認http和ssh是關閉通知的,是由於在localhost.cfg裏這兩個服務有一句 notifications_enabled       0。
也能夠手動打開,點進去,再右邊點enabled notifications for this service.
若是要永久打開這兩個服務的通知,那麼就修改下面文件裏的notifications_enabled       0爲1;或者直接刪除notifications_enabled       0,由於默認通知是打開的.
vim /etc/nagios/objects/localhost.cfg



3,關閉ssh服務,刷新web界面,仍是沒有critical.
   點擊ssh,能夠看到下一次計劃的check時間。若是不等的話,在右邊點Re-schedule the next check of this service強制check,再刷新就critical

=============================================================================

關於nagios配置文件之間的聯繫講解示例

# vim /etc/nagios/nagios.cfg    
cfg_file=/etc/nagios/objects/localhost.cfg


# vim /etc/nagios/objects/localhost.cfg 


define host{
        use                     linux-server      --模版
        host_name               localhost      --主機名    
        alias                   localhost      --主機別名    
        address                 127.0.0.1      --被監控機器的IP
        }


define hostgroup{
        hostgroup_name  linux-servers 
        alias           Linux Servers 
        members         localhost    --linux Servers組如今只有localhost這一個成員 
        }


--下面是8個默認定義的服務,我以監控磁盤利用率的這一段爲例
define service{
        use                             local-service    --模版,在templates.cfg 裏定義的    
        host_name                       localhost    --主機名,調用的是同配置文件裏define host裏定義的host_name
        service_description             Root Partition    --描述,會在web界面顯示的一個標題
        check_command                   check_local_disk!20%!10%!/    --檢測利用率的命令,free空間小於20%就報警,小於10就critcal警告
        }


# vim /etc/nagios/objects/templates.cfg 


define host{
        name                            linux-server
    use                             generic-host    --linux主機模版也使用了一個叫generic-host的模版,也在templates.cfg裏
    check_period                    24x7         --在timeperiods.cfg 裏定義的時間段
    check_interval                  5 
    retry_interval                  1 
    max_check_attempts              10 
    check_command                   check-host-alive  --在commands.cfg 裏定義的命令
    notification_period             workhours    --通知時間在timeperiods.cfg裏定義的
    notification_interval           120        --通知間隔
    notification_options            d,u,r         --通知選項
    contact_groups                  admins        --通知組,在contacts.cfg 裏定義
    register                        0          --不註冊,表示這只是一個模版,被調用,不會被nagios進程認爲就是一臺主機
    }



# vim /etc/nagios/objects/commands.cfg
define command{
        command_name    check-host-alive
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
        }

--命令都在libexec下,用--help去查
# /usr/lib64/nagios/plugins/check_ping --help


=============================================================================



問題:
如何監控本地的/boot分區    使用80%警告,使用90% critical

define service{
        use                             local-service       
        host_name                       localhost
        service_description             Boot Partition
        check_command                   check_local_disk!20%!10%!/boot
      }



問題:
如何監控本機zombie進程    5個警告 10個 critical


define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Zombie Total Processes
        check_command                   check_local_procs!5!10!Z
        }





例:如何增長監控本機的ftp服務
思路步驟:
1,看/usr/lib64/nagios/plugins/下是否有檢測ftp的命令,若是沒有,本身開發
2,查看相關檢測命令的參數文檔,按照需求定義你的監控方法,並加入到command.cfg裏
3,在localhost.cfg裏定義這個服務,並使用第二步定義的命令,並傳入值



# vim /etc/nagios/objects/commands.cfg  --下面一段默認就有,不須要加,直接改一下

define command{
        command_name    check_ftp
        command_line    $USER1$/check_ftp -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ 
        }



# vim /etc/nagios/objects/localhost.cfg  --加上下面一段

define service{
        use                             local-service
        host_name                       localhost
        service_description             FTP
        check_command                   check_ftp!1!3
        }



# /etc/init.d/nagios restart






練習:
1,若是本機ftp服務爲監聽2121端口,應該如何監控


# vim /etc/vsftpd/vsftpd.conf    
listen_port=2121        --加上這一句

# /etc/init.d/vsftpd restart

# netstat -ntlup |grep ftp



# vim /etc/nagios/objects/localhost.cfg 

---加下面一段
define service{
        use                             local-service
        host_name                       localhost
        service_description             FTP    --標題改爲FTP
        check_command                   check_ftp_2121!1!3!2121
--命令我這裏是沒有的,在command.cfg裏默認有一個check_ftp,沒有
--check_ftp_2121這個,因此要手動去加;!爲參數分隔符,1是第一個參數,3是第二個參數,2121是第三個參數;它們對應於我下面定義的-w -c  -p
        }

# vim /etc/nagios/objects/commands.cfg 

define command{
        command_name    check_ftp_2121
        command_line    $USER1$/check_ftp -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $ARG3$
        }


--直接使用監控命令去手工check一下,OK的
# /usr/lib64/nagios/plugins/check_ftp -w 1 -c 3 -p 2121
FTP OK - 0.004 second response time on port 2121 [220-#############################
220-#]|time=0.00389s;1.000000;3.000000;0.000000;10.000000


# systemctl restart nagios




練習:
監控本機的mysql

# vim /etc/nagios/objects/localhost.cfg 

define service{
        use                             local-service
        host_name                       localhost
        service_description             MYSQL
        check_command                   check_mysql!root!123
        }


# vim /etc/nagios/objects/commands.cfg 
define command{
        command_name    check_mysql
        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$    --第一個參數對應上面的root,第二個對應密碼123
        }



--手動check一下mysql,OK
# /usr/lib64/nagios/plugins/check_mysql -u root -p123
Uptime: 189  Threads: 1  Questions: 5  Slow queries: 0  Opens: 12  Flush tables: 1  Open tables: 6  Queries per second avg: 0.026


# systemctl restart nagios




練習:
加一個本機監控的service,監控機器運行時間,運行了超過365天就warning,超過1000天就critical


答案:
# vim /etc/nagios/objects/commands.cfg    
define command{
        command_name    check_local_uptime
        command_line    $USER1$/check_uptime -w $ARG1$ -c $ARG2$ -u $ARG3$
        }

# vim /etc/nagios/objects/localhost.cfg
define service{
        use                             local-service 
        host_name                       localhost
        service_description             UPTIME
        check_command                   check_local_uptime!365!1000!days
        }

# systemctl restart nagios



=============================================================================================


            nagios server ----》 nagios client

            10.1.1.2        10.1.1.3



咱們把監控的服務分爲公共和私有

公共(public):如ssh,http,ftp,mysql等。監控本地或遠程的公共服務,均可以直接配置
私有(private):如load,users,disk usage等。監控本地私有服務直接配置就好,監控遠程私有服務,須要服務和被監控端安裝nrpe




例:監控遠程服務器的普通服務(公共服務)。如ssh,http,ftp,mysql等


如:個人被監控端IP爲10.1.1.3
1.在nagios服務器的主配置文件里加上10.1.1.3的主機配置文件

# vim /etc/nagios/nagios.cfg

cfg_file=/etc/nagios/objects/10.1.1.3.cfg


2,建立這個10.1.1.3.cfg
# vim /etc/nagios/objects/10.1.1.3.cfg

define host{
        use                     linux-server
    host_name               10.1.1.3    --主機名,最好/etc/hosts裏對應好IP,我這裏沒有作,就直接寫IP
        alias                   10.1.1.3    --顯示到web上的名字
        address                 10.1.1.3    --實際被監控主機IP
        }

define hostgroup{
        hostgroup_name  remote linux-servers    --這裏我定義了一個新組,不能和localhost.cfg裏的組同名,會衝突
        alias           remote Linux Servers
        members         10.1.1.3
        }




--下面是公共服務,這裏我只寫了四個,你能夠自行增長
define service{
        use                             local-service  
        host_name                       10.1.1.3
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }


define service{
        use                             local-service   
        host_name                       10.1.1.3
        service_description             SSH
        check_command                   check_ssh
        }

define service{
        use                             local-service 
        host_name                       10.1.1.3
        service_description             HTTP
        check_command                   check_http
        }


define service{
        use                             local-service
        host_name                       10.1.1.3
        service_description             FTP
        check_command                   check_ftp!1!3
        }


# nagios -v /etc/nagios/nagios.cfg 
# systemctl restart nagios


==========================================================================



例:監控遠程的私有服務



       10.1.1.2                  10.1.1.3    
       nagios監控端                  被監控linux
                             check_disk
    check_nrpe   ---------  check_nrpe  check_swap
                 SSL傳輸                   check_load等

第一大步:nagios監控端上的操做
1,確認有以下的命令,若是沒有,則yum install nagios-plugins-nrpe
/usr/lib64/nagios/plugins/check_nrpe


2,增長check_nrpe命令到commands.conf文件裏
# vim /etc/nagios/objects/commands.cfg 

define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$        
        }

--c參數後接command, 也就說check_nrpe能夠調用別的check命令

3,在nagios服務器上對10.1.1.3的配置文件增長遠程私有服務

# vim /etc/nagios/objects/10.1.1.3.cfg 

define service{
        use                             local-service
        host_name                       10.1.1.3
        service_description             Current Users
        check_command                   check_nrpe!check_remote_users
        }
--check_remote_users就是check_nrpe的C參數要調用的命令,此命令在nagios服務器上的commands.cfg裏是不存在,它會在後面的步驟中加到被監控端

# systemctl restart nagios


4,用下面的命令作測試,但如今是會報對方5666端口拒絕(由於被監控端尚未安裝配置)
# /usr/lib64/nagios/plugins/check_nrpe -H 10.1.1.3 -c check_remote_users
connect to address 10.1.1.3 port 5666: Connection refused
connect to host 10.1.1.3 port 5666: Connection refused




第二大步:nagios被監控端上的操做
1,安裝nrpe和其它監控命令包(須要本地,centos163,epel源)
# yum install nrpe nagios-plugins-users


2,修改nrpe主配置文件
# vim /etc/nagios/nrpe.cfg

allowed_hosts=10.1.1.2        --把默認的127.0.0.1改爲nagios服務器的ip

command[check_remote_users]=/usr/lib64/nagios/plugins/check_users -w 5 -c 10    --修改或增長這一行,主要是定義一個叫check_remote_users的命令(和服務器那邊的配置對應)


3,啓動服務,並檢查5666端口是否開啓
# systemctl restart nrpe
# systemctl status nrpe
# systemctl enable nrpe

# lsof -i:5666


第三大步:回到nagios服務器端測試
再次使用下面的命令,就能夠監控到遠程的實際登陸用戶數了
# /usr/lib64/nagios/plugins/check_nrpe -H 10.1.1.3 -c check_remote_users
USERS WARNING - 9 users currently logged in |users=9;5;10;0


最後,清firefox緩存,在firefox查看遠程監控也正確了


=============================================================================
練習:監控遠程/分區,/boot分區,swap,cpu load等



=============================================================================

郵件報警驗證:

1,確認你至少有一個service爲crital狀態


2,保證nagios服務器能上公網,還有確認有mail命令了


3,# vim /etc/nagios/objects/contacts.cfg
    

    email                           linuxdaniel@126.com --改爲你的一個公網測試郵箱

4,# systemctl restart nagios



若是你想作成免費手機短信通知,可使用相似139郵箱這種(有郵件到達就短信通知的功能)的郵箱

如今有智能手機就方便多了,直接報警郵件發給外部一個郵箱,而後在你的手機上下載對應郵箱的app軟件就ok了
如今nagios官方直接都有手機客戶端管理軟件


=============================================================================


nagios圖表插件一:

nagiosgraph-1.4.4.tar.gz        --此軟件包最後的版本是2011年出的,目前在centos7測試過不兼容

如下這份文檔是在rhel6.5上成功的文件(僅作參考)


tar xf nagiosgraph-1.4.4.tar.gz -C /usr/src
cd /usr/src/nagiosgraph-1.4.4



[root@li nagiosgraph-1.4.4]# ./install.pl --check-prereq
checking required PERL modules
  Carp...1.11
  CGI...3.51
  Data::Dumper...2.124
  File::Basename...2.77
  File::Find...1.14
  MIME::Base64...3.08
  POSIX...1.17
  RRDs... ***FAIL***            --沒有rrd,就算你在rhel6上yum install *rrd*  再來測試也是fail
  Time::HiRes...1.9721
checking optional PERL modules
  GD...fail    --沒有gd,yum install *gd* 也不能搞定
checking nagios installation
  found nagios at /usr/local/nagios/bin/nagios
checking web server installation
  found apache at /usr/sbin/httpd



安裝gd

tar xf libgd-2.1.0.tar.gz -C /usr/src/
cd /usr/src/libgd-2.1.0/
./configure ;make ;make install
echo /usr/local/lib >> /etc/ld.so.conf
ldconfig



tar xf GD-2.56.tar.gz -C /usr/src/
cd /usr/src/GD-2.56/
perl Build.PL
./Build
./Build install
ldconfig




安裝rrdtool
# tar xf rrdtool-1.4.8.tar.gz -C /usr/src/

# cd /usr/src/rrdtool-1.4.8/
# ./configure ;make ;make install

# echo /opt/rrdtool-1.4.8/lib > /etc/ld.so.conf.d/rrdtools.conf

# ldconfig

# cd /usr/src/rrdtool-1.4.8/bindings/perl-shared
# make clean
# perl Makefile.PL && make && make install



# cd /usr/src/nagiosgraph-1.4.4/

--再次用下面的命令檢測就都OK了
[root@li nagiosgraph-1.4.4]# ./install.pl --check-prereq
checking required PERL modules
  Carp...1.11
  CGI...3.51
  Data::Dumper...2.124
  File::Basename...2.77
  File::Find...1.14
  MIME::Base64...3.08
  POSIX...1.17
  RRDs...1.4008
  Time::HiRes...1.9721
checking optional PERL modules
  GD...2.56
checking nagios installation
  found nagios at /usr/local/nagios/bin/nagios
checking web server installation
  found apache at /usr/sbin/httpd


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


參考下面這份文檔
http://www.linuxfunda.com/2013/04/02/steps-to-configure-nagiosgraph-with-nagios-core/



開始安裝
1,
[root@qianyun nagiosgraph-1.4.4]# ./install.pl --install
checking required PERL modules
  Carp...1.11
  CGI...3.51
  Data::Dumper...2.124
  File::Basename...2.77
  File::Find...1.14
  MIME::Base64...3.08
  POSIX...1.17
  RRDs...1.4008
  Time::HiRes...1.9721
checking optional PERL modules
  GD...2.53
checking nagios installation
  found nagios at /usr/local/nagios/bin/nagios
checking web server installation
  found apache at /usr/sbin/httpd
Destination directory (prefix)? [/usr/local/nagiosgraph] 
Location of configuration files (etc-dir)? [/usr/local/nagiosgraph/etc] 
Location of executables? [/usr/local/nagiosgraph/bin] 
Location of CGI scripts? [/usr/local/nagiosgraph/cgi] 
Location of documentation (doc-dir)? [/usr/local/nagiosgraph/doc] 
Location of examples? [/usr/local/nagiosgraph/examples] 
Location of CSS and JavaScript files? [/usr/local/nagiosgraph/share] 
Location of utilities? [/usr/local/nagiosgraph/util] 
Location of state files (var-dir)? [/usr/local/nagiosgraph/var] 
Location of RRD files? [/usr/local/nagiosgraph/var/rrd] 
Location of log files (log-dir)? [/usr/local/nagiosgraph/var] 
Path of log file? [/usr/local/nagiosgraph/var/nagiosgraph.log] 
Path of CGI log file? [/usr/local/nagiosgraph/var/nagiosgraph-cgi.log] 
URL of CGI scripts? [/nagiosgraph/cgi-bin] 
URL of CSS file? [/nagiosgraph/nagiosgraph.css] 
URL of JavaScript file? [/nagiosgraph/nagiosgraph.js] 
Path of Nagios performance data file? [/tmp/perfdata.log] 
URL of Nagios CGI scripts? [/nagios/cgi-bin] 
username or userid of Nagios user? [nagios] 
username or userid of web server user? [apache] 
Modify the Nagios configuration? [n] 
Modify the Apache configuration? [n] 
configuration:
  ng_layout            standalone
  ng_prefix            /usr/local/nagiosgraph
  ng_etc_dir           /usr/local/nagiosgraph/etc
  ng_bin_dir           /usr/local/nagiosgraph/bin
  ng_cgi_dir           /usr/local/nagiosgraph/cgi
  ng_doc_dir           /usr/local/nagiosgraph/doc
  ng_examples_dir      /usr/local/nagiosgraph/examples
  ng_www_dir           /usr/local/nagiosgraph/share
  ng_util_dir          /usr/local/nagiosgraph/util
  ng_var_dir           /usr/local/nagiosgraph/var
  ng_rrd_dir           /usr/local/nagiosgraph/var/rrd
  ng_log_dir           /usr/local/nagiosgraph/var
  ng_log_file          /usr/local/nagiosgraph/var/nagiosgraph.log
  ng_cgilog_file       /usr/local/nagiosgraph/var/nagiosgraph-cgi.log
  ng_url               /nagiosgraph
  ng_cgi_url           /nagiosgraph/cgi-bin
  ng_css_url           /nagiosgraph/nagiosgraph.css
  ng_js_url            /nagiosgraph/nagiosgraph.js
  nagios_cgi_url       /nagios/cgi-bin
  nagios_perfdata_file /tmp/perfdata.log
  nagios_user          nagios
  www_user             apache
  modify_nagios_config n
  nagios_config_file   
  nagios_commands_file 
  modify_apache_config n
  apache_config_dir    
  apache_config_file   
Continue with this configuration? [y] 

.............

2,
# vim /usr/local/nagios/etc/nagios.cfg    --最後加上下面一段

process_performance_data=1
service_perfdata_file=/tmp/perfdata.log
service_perfdata_file_template=$LASTSERVICECHECK$||$HOSTNAME$||$SERVICEDESC$||$SERVICEOUTPUT$||$SERVICEPERFDATA$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=30
service_perfdata_file_processing_command=process-service-perfdata-for-nagiosgraph


3,
# vim /usr/local/nagios/etc/objects/commands.cfg     --加上這一段,定義此命令

define command {
command_name process-service-perfdata-for-nagiosgraph
command_line /usr/local/nagiosgraph/bin/insert.pl
}


4,
# vim /etc/httpd/conf/httpd.conf     --在你的apache裏include這個文件
Include /usr/local/nagiosgraph/etc/nagiosgraph-apache.conf


# /etc/init.d/httpd  restart
# /etc/init.d/nagios  restart


5,
http://10.1.1.8/nagiosgraph/cgi-bin/showconfig.cgi    --用此頁面查看信息

# vim /usr/local/nagios/etc/objects/templates.cfg    --加上下面的模版

define service {
name nagiosgraph
action_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$
register 0
}

6,
# vim /usr/local/nagios/etc/objects/localhost.cfg   --在你全部的要加圖形的監控主機裏的服務的use後加上nagiosgraph模版,以下
define service{
        use                             local-service,nagiosgraph      
        host_name                       localhost
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }


7,
# /etc/init.d/nagios restart

最後到你的http://10.1.1.8/nagios/下去查看,會發現只要加了nagiosgraph模版的被監控服務會多了一個圖標,按圖標就會產生圖


=============================================================================



nagios圖表插件二:

pnp4nagios


第一步:目前版本都是最新版,我這裏使用的是源碼版(rpm版也能夠,課後有興趣去嘗試一下)
# rpm -qa |grep pnp4
pnp4nagios-0.6.25-1.el7.x86_64
# rpm -e --nodeps pnp4nagios-0.6.25-1.el7.x86_64
warning: /etc/httpd/conf.d/pnp4nagios.conf saved as /etc/httpd/conf.d/pnp4nagios.conf.rpmsave


第二步:解壓源碼版pnp4nagios(軟件包我共享在筆記目錄/program/nagios_soft/下)

# tar xf pnp4nagios-0.6.25.tar.gz -C /usr/src/
# cd /usr/src/pnp4nagios-head/

# ./configure    --直接configure,統計信息以下

  General Options:
  -------------------------         -------------------
  Nagios user/group:                nagios nagios
  Install directory:                /usr/local/pnp4nagios
  HTML Dir:                         /usr/local/pnp4nagios/share
  Config Dir:                       /usr/local/pnp4nagios/etc
  Location of rrdtool binary:       /usr/bin/rrdtool Version 1.4.8
  RRDs Perl Modules:                FOUND (Version 1.4008)
  RRD Files stored in:              /usr/local/pnp4nagios/var/perfdata
  process_perfdata.pl Logfile:      /usr/local/pnp4nagios/var/perfdata.log
  Perfdata files (NPCD) stored in:  /usr/local/pnp4nagios/var/spool

  Web Interface Options:
  -------------------------         -------------------
  HTML URL:                         http://localhost/pnp4nagios
  Apache Config File:               /etc/httpd/conf.d/pnp4nagios.conf

# make all
# make install

# make install-webconf    --安裝/etc/httpd/conf.d/pnp4nagios.conf文件
# make install-config    --安裝了一些配置文件模版到/usr/local/pnp4nagios/etc/目錄下
# make install-init    --安裝BULK Mode with NPCD須要的服務腳本


第三步:配置nagios主配置文件
# vim /etc/nagios/nagios.cfg    

process_performance_data=1        --找到這行,把0改成1;並在此文件最後的空白地方,複製粘貼下面的一段


# service performance data        
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=process-service-perfdata-file

#
# host performance data starting with Nagios 3.0
# 
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file



第四步:定義命令,在commands.cfg子配置文件裏最後空白地方複製粘貼下面的一段
# vim /etc/nagios/objects/commands.cfg 


define command{
       command_name    process-service-perfdata-file
       command_line    /bin/mv /usr/local/pnp4nagios/var/service-perfdata /usr/local/pnp4nagios/var/spool/service-perfdata.$TIMET$
}

define command{
       command_name    process-host-perfdata-file
       command_line    /bin/mv /usr/local/pnp4nagios/var/host-perfdata /usr/local/pnp4nagios/var/spool/host-perfdata.$TIMET$
}


第五步:啓動npcd(咱們如今使用的是bulk模式和npcd,一共五種模式,具體參考官檔說明)
# systemctl start  npcd
# chkconfig npcd on



第六步:定義模版,在templates.cfg子配置文件裏最後空白地方複製粘貼下面的一段
# vim /etc/nagios/objects/templates.cfg 


define host {
        name       hosts-pnp
        register   0
        action_url /pnp4nagios/graph?host=$HOSTNAME$&srv=_HOST_  
}
define service {
        name       service-pnp
        register   0
        action_url /pnp4nagios/graph?host=$HOSTNAME$&srv=$SERVICEDESC$  
}


第七步:apache的子配置文件修改(由於這個源碼版對應的語法爲apache2.2,而咱們centos7.3上用的是apache2.4,因此須要改一下;還有就是咱們的nagios使用的是rpm版,因此驗證文件路徑也改一下)
# vim /etc/httpd/conf.d/pnp4nagios.conf

Require all granted            --把Order allow,deny\nAllow from all這兩行刪除,換成這句


AuthUserFile /etc/nagios/passwd        --驗證文件路徑也改爲這個



# systemctl restart httpd
# systemctl enable httpd



第八步:把全部須要加圖表功能的hosts和services加上圖表須要的templates(hosts-pnp和service-php)
# vim /etc/nagios/objects/localhost.cfg
# vim /etc/nagios/objects/10.1.1.3.cfg    --這兩個配置裏的hosts和services均可以加,效果以下示例:

define host{
        use                     linux-server,hosts-pnp   --如這裏加上hosts-pnp模版
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }


define service{
        use                             local-service,service-pnp   --如這裏加上service-pnp模版
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }



# nagios -v /etc/nagios/nagios.cfg 
# systemctl restart nagios


第九步:使用firefox訪問測試
訪問後,加了圖表模版的hosts或services會有一個明顯的圖表圖標,點進去,第一次會提示刪除下面文件,刪除後再點進去就ok了
# rm -rf /usr/local/pnp4nagios/share/install.php
相關文章
相關標籤/搜索