Nagios監控系統
Nagios是一款開源的免費網絡監視工具,能夠監控Windows、Linux和Unix的主機狀態,交換機路由器等網絡設備,在系統或服務狀態異常時發出郵件或短信報警,第一時間通知網站運維人員。流量監控不是他的強項,流量監控建議使用cacti,能夠繪製很是直觀的圖形。
.phpnagios主要能夠監控如下方面:html
- 主機是否宕機(經過ping命令,若是ping不通會認爲主機屬於宕機狀態,但不影響所監控的其餘服務)
- 服務器資源(cpu使用率、硬盤剩餘空間等)
- 網絡服務(smtp\pop3\http\)
- 監控網絡設備(路由器、交換機等)
.nagios工做原理
Nagios自己不包括監控主機和服務的功能。全部的監控、監測功能都是經過各類插件來完成的。安裝完nagios以後,在nagios主目錄下的/libexex裏面放有nagios自帶的插件,如:check_disk是檢查磁盤空間的插件,check_load是檢查cpu負載的插件,每個插件能夠經過運行./check_xxx -h命令來檢查其使用方法和功能。
.mysqlnagios的四種監控狀態
Nagios能夠識別四種狀態返回信息。0(OK)表示狀態正常(綠色顯示)linux
- (WARNING)表示出現警告(×××),
- (CRITICAL)表示出現很是嚴重錯誤(紅色),
- (UNKNOWN)表示未知錯誤(深×××),nagios根據插件返回來的值來判斷監控對象的狀態,並經過web顯示出來,以供管理員即時發現故障。
.nagios經過nrpe插件來遠程管理服務的工做過程ios
- Nagios執行安裝在它裏面的check_nrpe插件,並告訴check_nrpe去檢測哪些服務。
- 經過ssl,check_nrpe鏈接遠端機器上的NRPE daemon。
- NRPE運行本地的各類插件去檢測本地服務器和狀態(check_disk,...etc)。
- NRPE把檢測的結果傳給主機端的check_nrpe,check_nrpe再把結果送到nagios狀態隊列中。
- Nagios依次讀取隊列中的信息,再把結果顯示出來。
實驗環境:
搭建nagios監控系統
.web
建立nagios用戶和用戶組 [root@centos1 /]# mount /dev/cdrom /media/ [root@centos1 /]# useradd -s /sbin/nologin nagios [root@centos1 /]# mkdir /usr/local/nagios [root@centos1 /]# chown -R nagios:nagios /usr/local/nagios/
.sql
編譯安裝nagios(須要提早配置yum) 安裝支持包: [root@centos1 /]# yum -y install httpd php mysql-devel openssl openssl-devel [root@centos1 /]# umount /dev/cdrom /media/ [root@centos1 /]# mount /dev/cdrom /media/ [root@centos1 /]# cd /media/
.vim
配置: [root@centos1 /]# tar zxf nagios-4.0.1.tar.gz -C /usr/src/ [root@centos1 /]# cd /usr/src/nagios-4.0.1/ [root@centos1 /]# ./configure --prefix=/usr/local/nagios/
.centos
編譯和安裝: [root@centos1 /]# make install //安裝主程序,CGI和HTML文件 [root@centos1 /]# make install-init //在/etc/rc.d/init.d安裝啓動腳本 [root@centos1 /]# make install-commandmode //配置目錄權限 [root@centos1 /]# make install-config //安裝示例配置文件 [root@centos1 /]# make install-webconf //安裝nagios的web接口,會在/etc/httpd/conf.d目錄中建立nagios.conf文件。
.
安裝完成以後會在/usr/local/nagios目錄下產生6個目錄
.
bin:nagios執行程序所在的目錄,nagios文件即爲主程序。
etc:nagios配置文件目錄,當make install-config完之後etc下面就會出現默認的配置文件。
sbin:nagios CGI文件所在目錄,這裏存放的是一些外部命令執行程序。
share:nagios網頁文件目錄,存放一些html文件。
var:nagios日誌文件、pid等文件目錄。
Libexec:系統默認插件的存儲位置
.服務器
添加爲系統服務器 [root@centos1 /]# chkconfig --add nagios [root@centos1 /]# chkconfig --level 35 nagios on
.
安裝nagios插件(監控功能經過插件完成) [root@centos1 /]# cd /media/ [root@centos1 /]# tar zxf nagios-plugins-1.5.tar.gz -C /usr/src/ [root@centos1 /]# cd /usr/src/nagios-plugins-1.5/ [root@centos1 /]# ./configure --prefix=/usr/local/nagios/
.
編譯並安裝: [root@centos1 /]# make && make install
.
安裝nrpe(爲了監控遠程服務器) [root@centos1 /]# cd /media/ [root@centos1 /]# tar zxf nrpe-2.15.tar.gz -C /usr/src/ [root@centos1 /]# cd /usr/src/nrpe-2.15/ [root@centos1 /]# ./configure && make all && make install-plugin
.
在/etc/httpd/conf/httpd.conf文件最後添加受權,咱們能夠到/etc/httpd/conf.d/nagios.conf文件中複製,不用手打。
[root@centos1 /]# vim /etc/httpd/conf/httpd.conf 使用:r導入便可 r /etc/httpd/conf.d/nagios.conf 導入便可,不用修改,保存退出。
.
執行htpasswd命令添加一個訪問nagios頁面的受權用戶 [root@centos1 /]# /usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin 用戶名和密碼都是nagiosadmin
.
啓動nagios和httpd [root@centos1 /]# service nagios start [root@centos1 /]# service httpd start
...............
.
配置nagios監控系統文件
- nagios的配置文件:
- Nagios.cfg:主配置文件,定義各類配置文件的名稱和位置
- Cgi.cfg:控制CGI的配置文件
Resource.cfg:資源文件,定義各類變量,以便於其餘文件調用- Objects:其餘配置文件存放目錄,此目錄下主要有:
.
Command.cfg:命令配置文件,定義各類命令格式,以備其餘文件調用
contacts.cfg:聯繫人和組,發郵件等告警信息時能夠調用
localhost.cfg:監控本機的配置文件
timeperiods.cfg:定義監控時間的配置文件,便於其餘文件調用
Hostgroups.cfg:定義監控的主機(組),需手動建立。~~
.
配置文件之間的關係
在nagios的配置過程當中涉及的幾個定義有主機、主機組、服務、服務組、聯繫人、聯繫人組、監控時間和監控命令等。從這些定義能夠看出,nagios各個配置文件之間互爲關聯、彼此引用的。成功配置出一臺nagios監控系統,每一個配置文件之間依賴與被依賴的關係,最重要的有四點:
- 定義監控那些主機,主機組,服務和服務組
- 定義這個監控要用什麼命令實現
- 定義監控的時間段
- 定義主機或服務器出現問題時要通知的聯繫人和聯繫人祖
.
配置nagios
爲了能更清楚的說明問題,同時也爲了維護方便,建議將nagios各個定義的對象建立獨立的配置文件。
- 建立conf目錄來定義host主機
- 建立hostgroups.cfg文件來定義主機組
- 用默認的contacts.cfg文件來定義聯繫人和聯繫人組
- 用默認的commands.cfg文件來定義命令
- 用默認的timeperiods.cfg來定義監控時間段
- 用默認的templetes.cfg文件做爲資源引用文件
.
配置nagios,修改配置文件
[root@centos1 /]# vim /usr/local/nagios/etc/nagios.cfg 在cfg_file下面添加兩行 cfg_file=/usr/local/nagios/etc/object/hostgroups.cfg cfg_dir=/usr/local/nagios/etc/conf [root@centos1 /]# mkdir /usr/local/nagios/etc/conf
.
[root@centos1 /]# vim /usr/local/nagios/etc/objects/commands.cfg 在最下方添加 define command{ command_name check_nrpe command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ }
.
[root@centos1 /]# vim /usr/local/nagios/etc/objects/contacts.cfg 添加到contactgroup_name admins的下面 define contact{ contact_name ydw alias ydw service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r host_notification_options d,u,r service_notification_commands notify-service-by-email host_notification_commands notify-host-by-email email root }
.
新建/usr/local/nagios/etc/objects/hostgroups.cfg(定義主機組) [root@centos1 /]# vim /usr/local/nagios/etc/objects/hostgroups.cfg define hosrgroup{ hostgroup_name webmysql alias webmysql members 192.168.1.20 }
.
在/usr/local/nagios/etc/conf下面新建192.168.1.20.cfg文件(用於監控192.168.1.20的主機存活,負載,進程)
[root@centos1 /]# cd /usr/local/nagios/etc/conf/ [root@centos1 /]# vim 192.168.1.20.cfg define host{ host_name 192.168.1.20 alias 192.168.1.20 address 192.168.1.20 check_command check-host-alive max_check_attempts 5 check_period 24x7 contact_groups ydw notification_period 24x7 notification_options d,u,r } define service{ host_name 192.168.1.20 service_description check-host-alive check_command check-host-alive max_check_attempts 3 normal_check_interval 2 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups ydw } define service{ host_name 192.168.1.20 service_description check-proce check_command check_nrpe!check_total_procs max_check_attempts 3 normal_check_interval 2 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups ydw } define service{ host_name 192.168.1.20 service_description check-load check_command check_nrpe!check_load max_check_attempts 3 normal_check_interval 2 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7
.
.
命令解釋: define host{ use linux-server //定義使用的模板 host_name nagios //被監控主機的名稱,最好別帶空格 alias nagios //別名 address 127.0.0.1 //被監控主機的IP地址 check_command check-host-alive normal_check_interval 3 //正常檢測間隔時間 retry_check_interval 2 //重試檢測間隔時間 //監控的命令check-host-alive,這個命令來自commands.cfg,用來監控主機是否存活 max_check_attempts 5 //檢查失敗後重試的次數 check_period 24x7 //檢查的時間段24x7,一樣來自timeperiods.cfg中定義 notification_interval 10 //提醒的間隔,每隔10秒提醒一次 notification_period 24x7 //提醒的週期, 24x7,一樣來自timeperiods.cfg中定義 contact_groups admins //聯繫人組,上面在contactgroups.cfg中定義的admins notification_options d,u,r //指定什麼狀況下提醒 } .
進入/usr/local/nagios/etc/objects/contacts.cfg在最後添加
[root@centos1 /]# vim /usr/local/nagios/etc/objects/commands.cfg define contacatgroup{ contactgroup_name ydw alias ydw members ydw }
.
重啓nagios服務 [root@centos1 /]# service nagios restart
.
開防火牆例外
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/ chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
配置被控端192.168.1.20(mysql和web)
直接用腳本安裝
[root@centos1 /]# mount /dev/cdrom /media/ [root@centos1 /]# cd /media/ [root@centos1 /]# chmod +x nagiosclient.sh
.
將腳本和軟件複製到/usr/src
[root@centos1 /]# cp nagios-plugins-1.5.tar.gz /usr/src/ [root@centos1 /]# cp nrpe-2.15.tar.gz /usr/src/ [root@centos1 /]# cp nagiosclient.sh /usr/src/
.
換6.5光盤
[root@centos1 /]# umount /dev/cdrom /media/ [root@centos1 /]# mount /dev/cdrom /media/ [root@centos1 /]# cd /usr/src/ .
執行腳本: [root@centos1 /]# sh nagiosclient.sh
.
安裝完成以後,須要打開vim /usr/local/nagios/etc/nrpe.cfg
添加nagios服務器的地址
[root@centos1 /]# vim /usr/local/nagios/etc/nrpe.cfg allowed_hosts=127.0.0.1,192.168.1.10 .
啓動nrpe, [root@centos1 /]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
.
如在服務端測試不行能夠重啓主機後重試
[root@centos1 /]# reboot
.
(服務器測試)
[root@centos1 /]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.20 NRPE v2.15
..........
.
補充:也可在services.cfg文件中添加192.168.1.20.cgf文件中的參數
#vi /usr/local/nagios/etc/objects/services.cfg 內容以下: 1define service{ use local-service host_name nagios service_description ping check_command check-host-alive } 2define service{ use local-service host_name nagios service_groups 系統情況檢查 service_description 登錄用戶數 check_command check-host-users!20!50 } 3define service{ use local-service host_name nagios service_groups 系統情況檢查 service_description 根分區 check_command check-local_disk!20%!10%!/ } 4define service{ use local-service host_name nagios service_groups 系統情況檢查 service_description 進程總數 check_command check-local_procs!250!400!RSZDT } 5define service{ use local-service host_name nagios service_groups 系統情況檢查 service_description 系統負載 check_command check-host-load!5.0,4.0,3.0!10.0,6.0,4.0 } 6define service{ use local-service host_name nagios service_groups 系統情況檢查 service_description 交換空間利用率 check_command check-local_swap!20!10 } 7define service{ servicegroup_name 系統情況檢查 alias 系統概況 }
check_local_users!20!50 //監測遠程主機當前的登陸用戶數量,若是大於20用戶則報warning,若是大於50則報critical . check_local_disk!20%!10%!/ //若是可用空間低於20%會報Warning,若是可用空間低於10%則報Critical: . check_local_procs!250!400!RSZDT //監測遠程主機當前的進程總數,若是大於250進程則報warning,若是大於400進程則報critical,S(休眠)、R(運行)、Z(僵死)、D (不可中斷)、T (中止) . check_load -w 5,4,3 -c 10,6,4這個命令的意義以下 * 當1分鐘多於5個進程等待,5分鐘多於4個,15分鐘多於3個則爲warning狀態 * 當1分鐘多於10個進程等待,5分鐘多於6個,15分鐘多於4個則爲critical狀態 服務組並非必須的,這是配合nagios的監控頁面的顯示