nagios監控部署。php
在部署以前把依賴包安裝了。html
[root@tiandong63 ~]# yum install -y gcc glibc glibc-common php gd gd-devel libpng libmng libjpeg zlib前端
[root@tiandong63 ~]# yum install -y httpd ; service httpd restartnode
[root@tiandong63 ~]# useradd nagios
[root@tiandong63 ~]# groupadd nagcmd
[root@tiandong63 ~]# usermod -G nagcmd nagios
[root@tiandong63 ~]# usermod -G nagcmd apachemysql
核心包的安裝:linux
[root@tiandong63 ~]#tar xf nagios-3.5.1.tar.gz -C /usr/local/src/ios
[root@tiandong63 ~]#cd /usr/local/src/nagiosweb
[root@tiandong63 nagios]# ./configure --with-command-group=nagcmd #指定nagios運行用戶組sql
[root@tiandong63 nagios]# make allapache
[root@tiandong63 nagios]# make install && make install-init && make install-commandmode && make install-config && make install-webconf
//如果單步執行,每次執行一部,都會提示你下一步須要幹什麼,很是easy
//make install安裝生成/usr/local/nagios/,其中/usr/local/nagios/share即nagiosWEB訪問界面的站點目錄
//make install-init 安裝生成/etc/rc.d/init.d/nagios 啓動腳本
//make install-config 安裝生成/usr/local/nagios/etc下的nagios相關配置文件
//make install-commandmode 設定相應nagios工做目錄的權限
//make install-webconf 安裝Nagios的WEB配置文件到Apache的conf.d目錄下
[root@tiandong63 nagios]# ll /usr/local/nagios/ #生成安裝目錄
total 24
drwxrwxr-x 2 nagios nagios 4096 Mar 19 23:06 bin
drwxrwxr-x 3 nagios nagios 4096 Mar 19 23:06 etc
drwxrwxr-x 2 nagios nagios 4096 Mar 19 23:06 libexec 腳本文件
drwxrwxr-x 2 nagios nagios 4096 Mar 19 23:06 sbin
drwxrwxr-x 10 nagios nagios 4096 Mar 19 23:06 share 瀏覽器訪問的頁面文件
drwxrwxr-x 5 nagios nagios 4096 Mar 19 23:06 var
[root@tiandong63 ~]# ll /usr/local/nagios/libexec/ 此時這個文件夾下面是空的
nagios主目錄
[root@XueGod63 Nagios]# ll /usr/local/nagios/
bin #Nagios執行程序所在目錄
etc #nagios配置文件所在目錄,初始安裝只有幾個*.cfg文件
libexec #監控所用命令,須要安裝了nagios-plugins插件了纔會有,檢測命令,不裝是空的
sbin #Nagios的Cgi文件所在目錄,外部命令所須要的文件存放目錄
share #Nagios前端頁面
var #日誌文件,pid文件等
nagios的配置文件:
[root@tiandong63 nagios]# vim /usr/local/nagios/etc/nagios.cfg
log_file=/var/log/nagios/nagios.log #日誌位置
cfg_file=/etc/nagios/objects/commands.cfg #這個文件定義了不少命令
cfg_file=/etc/nagios/objects/contacts.cfg #定義聯繫人,怎麼聯繫
cfg_file=/etc/nagios/objects/timeperiods.cfg #定義了時間段
cfg_file=/etc/nagios/objects/templates.cfg #模板(聯繫人,主機,時間)
cfg_file=/etc/nagios/objects/localhost.cfg #監控本機相關配置文件
#cfg_file=/etc/nagios/objects/windows.cfg #windows,默認不監控
#cfg_file=/etc/nagios/objects/switch.cfg #交換機路由器監控,默認不監控
#cfg_file=/etc/nagios/objects/printer.cfg #打印機監控,默認不監控
#cfg_dir=/etc/nagios/servers #定義了服務合集(多個使用)
#cfg_dir=/etc/nagios/printers #定義了打印機合集(多個使用)
#cfg_dir=/etc/nagios/switches #定義了交換合集(多個使用)
#cfg_dir=/etc/nagios/routers #定義了路由合集(多個使用)
resource_file=/etc/nagios/private/resource.cfg 資源變量配置文件,包括$USER1$變量(一個路徑)等
status_update_interval=10 #狀態更新時間,單位s
log_rotation_method=d #日誌滾動,默認天
service_check_timeout=60 #服務檢查超時時間
host_check_timeout=30 #主機檢查超時時間
event_handler_timeout=30
notification_timeout=30
ocsp_timeout=5
perfdata_timeout=5
[root@tiandong63 nagios]# vim /usr/local/nagios/etc/cgi.cfg 在這裏查看默認的認證用戶
//此配置文件比nagios.cfg優先級高
main_config_file= /usr/local/nagios/etc/nagios.cfg #主配置文件
physical_html_path= /usr/local/nagios/share #物理路徑
url_html_path=/nagios #在URL後面加上/nagios才能訪問
use_authentication=1 #使用認證
use_ssl_authentication=0 #不使用ssl
authorized_for_system_information=nagiosadmin #認證用戶
重啓httpd服務,測試。
須要用戶名和密碼,如今來建立用戶名和密碼。
[root@tiandong63 ~]# vim /etc/httpd/conf.d/nagios.conf 查看密碼爲位置
[root@tiandong63 ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin 生成用戶名和密碼
New password:
Re-type new password:
Adding password for user nagiosadmin
[root@tiandong63 ~]# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:MwGeiyh1pyQuY
重啓服務再次訪問
此時點擊其中的東西是報錯的,nagios服務沒有啓動
[root@tiandong63 ~]# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
指定配置文件,以守護進程方式啓動此時就能夠了(默認是對本地主機進行監控的)
檢查配置文件:
[root@tiandong63 ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors: 0 此時表示配置文件沒有問題
安裝插件
[root@tiandong63 ~]# tar -xf nagios-plugins-2.1.1.tar.gz -C /usr/local/src/
[root@tiandong63 nagios-plugins-2.1.1]# ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd
[root@tiandong63 nagios-plugins-2.1.1]# make && make install
此時來看/usr/local/nagios/libexec/ 以前的時候下面是空的,此時下面沒有check-mysql
[root@tiandong63 ~]# yum install mysql mysql-devel -y 安裝了MySQL的包,在從新編譯 主要是devel這個包
[root@tiandong63 nagios-plugins-2.1.1]# ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd
[root@tiandong63 nagios-plugins-2.1.1]# make && make install
重啓httpd服務和nagios服務
監控本地nfs服務:
[root@tiandong63 ~]# cd /usr/local/nagios/etc/objects/
[root@tiandong63 objects]# ls
commands.cfg localhost.cfg switch.cfg timeperiods.cfg
contacts.cfg printer.cfg templates.cfg windows.cfg
[root@tiandong63 objects]# cp localhost.cfg localhost.cfg.bak 備份本地的配置文件
安裝一下nfs
[root@tiandong63 ~]# yum install -y nfs*
[root@tiandong63 ~]# /etc/init.d/nfs restart (先開啓rpc服務)
[root@tiandong63 ~]# vim /etc/exports 配置共享的目錄
/tmp *(rw)
[root@tiandong63 ~]# /etc/init.d/nfs restart
[root@tiandong63 ~]# showmount -e 192.168.1.63
Export list for 192.168.1.63:
/tmp *
重啓web服務和nagios服務
[root@tiandong63 ~]# service httpd restart && service nagios restart
此時監控裏面就有nfs了。
監控遠端MySQL:
[root@tiandong64 ~]# yum install mysql-server mysql -y
[root@tiandong64 ~]# mysql -uroot -p123456
mysql> create database nagiosdb;
Query OK, 1 row affected (0.00 sec)
mysql> grant select on nagiosdb.* to nagiosdb@192.168.1.63;
Query OK, 0 rows affected (0.00 sec)
[root@tiandong63 ~]# vim /usr/local/nagios/etc/nagios.cfg
34 cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
35 cfg_file=/usr/local/nagios/etc/objects/service.cfg
[root@tiandong63 objects]# vim /usr/local/nagios/etc/objects/hosts.cfg
define host {
use linux-server
host_name xuegod64
alias mysql-server
address 192.168.1.64
}
[root@tiandong63 objects]# vim /usr/local/nagios/etc/objects/service.cfg
define service{
use local-service
host_name xuegod64
service_description mysqlser
check_command check_mysql
}
[root@tiandong63 objects]# vim /usr/local/nagios/etc/objects/commands.cfg
212 #'check mysql'
213 define command{
214 command_name check_mysql
215 command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u nagiosdb -d nagiosdb
216 }
[root@tiandong63 objects]# /usr/local/nagios/libexec/check_mysql -H 192.168.1.64 -u nagiosdb -d nagiosdb #手動探測監控的MySQL
Uptime: 1067 Threads: 1 Questions: 31 Slow queries: 0 Opens: 16 Flush tables: 1 Open tables: 9 Queries per second avg: 0.29|Connections=15c;;; Open_files=18;;; Open_tables=9;;; Qcache_free_memory=0;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=0c;;; Qcache_queries_in_cache=0;;; Queries=31c;;; Questions=31c;;; Table_locks_waited=0c;;; Threads_connected=1;;; Threads_running=1;;; Uptime=1067c;;;
[root@tiandong63 objects]# service nagios restart 重啓服務
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
此時去測試:(多了遠端的監控)
關閉遠程的MySQL服務時:
[root@tiandong64 ~]# /etc/init.d/mysqld stop
Stopping mysqld: [ OK ]
在去查看:
監控遠端web服務:
[root@tiandong64 ~]# yum install httpd -y
[root@tiandong64 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.1.64 for ServerName
[ OK ]
每監控一臺主機都必須寫一個hosts.cfg文件
[root@tiandong63 objects]# vim /usr/local/nagios/etc/objects/hosts.cfg
define host {
use linux-server
host_name xuegod64
alias mysql-server
address 192.168.1.64
}
因爲監控的是同一臺主機的服務,因此主機不用再定義了
[root@tiandong63 objects]# vim /usr/local/nagios/etc/objects/service.cfg
define service{
use local-service
host_name xuegod64
service_description mysqlser
check_command check_mysql
}
define service{
use local-service
host_name xuegod64
service_description apache
check_command check_apache
}
[root@tiandong63 objects]# vim /usr/local/nagios/etc/objects/commands.cfg
218 define command{
219 command_name check_apache
220 command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p 80
221 }
手工探測是否能夠探測到:
[root@tiandong63 objects]# /usr/local/nagios/libexec/check_tcp -H 192.168.1.64 -p 80
TCP OK - 0.002 second response time on 192.168.1.64 port 80|time=0.001831s;;;0.000000;10.000000
[root@tiandong63 objects]# service nagios restart 重啓服務
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
nagios監控外部主機私有信息
解決依賴:
[root@tiandong63 ~]# yum install -y openssl openssl-devel
[root@tiandong64 ~]# yum install -y openssl openssl-devel
[root@tiandong63 ~]# tar xf nrpe-2.15.tar.gz -C /usr/local/src/
[root@tiandong63 nrpe-2.15]# ./configure && make && make install
[root@tiandong63 nrpe-2.15]# make install-plugin && make install-daemon 前者安裝插件,後者以守護進程來運行
[root@tiandong63 nrpe-2.15]# ll /usr/local/nagios/libexec/check_nrpe 主要是爲了查看是否生成以下的插件
-rwxrwxr-x 1 nagios nagios 76785 Sep 24 08:47 /usr/local/nagios/libexec/check_nrpe 這個命令須要安裝nrpe以後纔有
被監控端配置:
[root@tiandong64 ~]# yum install -y openssl openssl-devel
[root@tiandong64 ~]# useradd -s /sbin/nologin nagios
[root@tiandong64 ~]# groupadd nagcmd
[root@tiandong64 ~]# usermod -G nagcmd nagios
[root@tiandong64 ~]# tar -xf nrpe-2.15.tar.gz -C /usr/local/src/
[root@tiandong64 ~]# tar -xf nagios-plugins-2.1.1.tar.gz -C /usr/local/src/
[root@tiandong64 nagios-plugins-2.1.1]# ./configure && make && make install
[root@tiandong64 nrpe-2.15]# ./configure && make && make install
[root@tiandong64 nrpe-2.15]# make install-daemon-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 644 -o nagios -g nagios sample-config/nrpe.cfg /usr/local/nagios/etc
[root@tiandong64 nrpe-2.15]#
[root@tiandong64 nrpe-2.15]# make install-xinetd
/usr/bin/install -c -m 644 sample-config/nrpe.xinetd /etc/xinetd.d/nrpe
[root@tiandong64 nrpe-2.15]#
[root@tiandong64 nrpe-2.15]# vim /etc/xinetd.d/nrpe
[root@tiandong64 nrpe-2.15]# echo "nrpe 5666/tcp # NRPE" >> /etc/services 端口註冊
nrpe 5666/tcp # NRPE
#/etc/services文件的做用:
#做用1:xinet.d來啓動服務時他就會在/etc/services找相應服務對應的端口來啓動服務。找不到對應端口,將不啓動服務。
#做用2: 顯示對應端口對應的協議名。
#例如 iptables -L 不加-n參數, 查看時,把 80轉 -> www http
#做用3:查看經常使用端口
[root@tiandong64 nrpe-2.15]# yum install -y xinetd
[root@tiandong64 nrpe-2.15]# /etc/init.d/xinetd start
Starting xinetd: [ OK ]
[root@tiandong64 nrpe-2.15]#
[root@tiandong64 ~]# netstat -antup|grep 5666
tcp 0 0 :::5666 :::* LISTEN 26870/xinetd
客戶端nrpe命令
[root@tiandong64 ~]# vim /usr/local/nagios/etc/nrpe.cfg
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 38% -c 35% -p /dev/sda1
command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 42% -c 43% -p /dev/sda2
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
#-w爲警告 -C爲告急 sda2中,咱們是指佔用42%
[root@tiandong64 ~]# /etc/init.d/xinetd restart
服務端手動測試
[root@tiandong63 ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.64
NRPE v2.15 對端的版本
[root@tiandong63 ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.64 -c check_sda1
DISK OK - free space: /boot 154 MB (84% inode=99%);| /boot=28MB;119;125;0;193
主機在上面的時候就已經定義了。
定義服務:
[root@tiandong63 ~]# vim /usr/local/nagios/etc/objects/service.cfg
定義NRPE監控命令
[root@tiandong63 ~]# vim /usr/local/nagios/etc/objects/commands.cfg
#'check nrpe'
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
[root@tiandong63 ~]# service nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
前段監控
測試:硬盤數據寫入測試,看看root partition會不會告急
[root@tiandong64 ~]# dd if=/dev/zero of=1.txt count=600 bs=30M
此時監控端已經告警了
刪掉被監控主機的數據;再次查看:
郵件報警
[root@tiandong63 ~]# yum install -y sendmail
[root@tiandong63 ~]# /etc/init.d/sendmail startStarting sendmail: [ OK ]Starting sm-client: [ OK ]