nagios --> plugins ( 插件 ) --> (host ,service)( 主機,服務 ) --> 返回值( 0--成功,1--警告,2--緊急 ,3--不知道 )--> web
**********************************************************************************************************************************
nagios 報警
**********************************************************************************************************************************
* 1.基本安裝
(1)下載包 lftp i
get nagios-4.0.7.tar.gz
nagios-plugins-2.0.3.tar.gz
gd-devel-2.0.35-11.el6.x86_64.rpm
解壓 tar zfx nagios-4.0.7.tanagios r.gz
(2)添加用戶 useradd -u 2000 -s /sbin/nologin -M nagios
(3)安裝gd ( 若Map沒有出現安裝此包 )
yum localinstall gd-devel-2.0.35-11.el6.x86_64.rpm -y
(4)安裝nagios cd nagios-4.0.7
./configure
make all
make install
make install-init
make install-commandmode
make install-config
make install-webconf
make install-exfoliation
make install-classicui
(5)安裝nagios-plugins( 當/usr/local/nagios/libexec/裏沒有文件,安裝此nagios-plugins包)
解壓 tar zfx nagios-plugins-2.0.3.tar.gz
安裝 cd nagios-plugins-2.0.3
./configure
./configure --enable-perl-modules --enable-libtap
make
make install
改權限 chown -R nagios.nagios /usr/local/nagios/*
用戶 htpasswd -cm /usr/local/nagios/etc/htpasswd.users nagiosadmin
密碼 westos(2)
(5)檢測 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0 ( 出現兩個0就行了 )
Total Errors: 0
(6)啓動服務 /etc/init.d/nagios start
/etc/init.d/httpd restart
(7)http訪問 192.168.2.74/nagios/
(8)當安裝問成後,若是還需有添加功能以下操做,以gd爲例
下載 yum localinstall gd-devel-2.0.35-11.el6.x86_64.rpm -y
cd nagios-4.0.7
清除 make clean
./configure
安裝 make all
make install
查看 ll /usr/local/nagios/sbin/statusmap.cgi
重啓 /etc/init.d/nagios reload
訪問 192.168.2.74/nagios/
~~~~~~~~~
* 2.添加功能 添加服務組 添加mysql監控 監控遠程主機
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@1.添加服務組
(1)http出現警告( Services裏 )
vim /var/www/html/index.html
server74.example.com ( 隨意 )
(2)主機和服務分開管理
複製 cp /usr/local/nagios/etc/objects/localhost.cfg /usr/local/nagios/etc/objects/hosts.cfg
cp /usr/local/nagios/etc/objects/localhost.cfg /usr/local/nagios/etc/objects/services.cfg
主配置 vim /usr/local/nagios/etc/nagios.cfg
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg (35)
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
分別刪除不須要的信息( hosts裏刪除services信息,services裏刪除hosts信息 )
vim /usr/local/nagios/etc/objects/hosts.cfg
vim /usr/local/nagios/etc/objects/services.cfg
(3)添加服務組
vim /usr/local/nagios/etc/objects/services.cfg
define servicegroup{
servicegroup_name 系統狀態 ; The name of the hostgroup
alias 本機負載 ; Long name of the group
members localhost,PING,localhost,Root Partition,localhost,Total Processes,localhost,Current Load
}
(4)修改本機名稱方法
vim /usr/local/nagios/etc/objects/hosts.cfg
host_name server74.example.com (16)
members server74.example.com (44)
vim /usr/local/nagios/etc/objects/services.cfg
:%s/localhost/server74.example.com/g ***
監控遠程主機:
check_nrpe < --- ssl --- > NRPE( tcp 5666 )--> libexec/*.sh( 腳本 )--監控主機的信息
利用腳本經過ssl鏈接要監控的主機,再經過NRPE調用libexec下的腳本去捕獲監控主機的監控信息
遠程監控主機的服務:mysql
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@2.添加遠程監控主機的服務:mysql
遠程監控主機的服務,此處以mysql爲例
*1.本地(不須要如下的第一步)
cd /usr/local/nagios/libexec/ ( 此文件存儲命令,在commands.cfg裏沒有時能夠在此執行調用 )
./check_mysql -ucacti -pwestos ( 存在一個用戶可登陸便可,此處用cacti用戶測試 -u指定用戶 -p設置密碼 ,)
運行結果:( 出現如下信息,表明檢測成功 )
Uptime: 8522 Threads: 1 Questions: 88973 Slow queries: 0 Opens: 84 Flush tables: 2 Open tables:
43 Queries per second avg: 10.440|Connections=175c;;; Open_files=53;;; Open_tables=43;;; Qcache_free_memory=0;;;
Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=0c;;; Qcache_queries_in_cache=0;;;
Queries=88973c;;; Questions=88973c;;; Table_locks_waited=0c;;; Threads_connected=1;;; Threads_running=1;;; Uptime=8522c;;;
*2.監控遠程主機的mysql
(1)遠程主機配置
1. yum install -y mysql-server ( 安裝mysql服務,要遠程監控主機的mysql,監控主機固然須要有mysql啦,rhel7要安裝mariadb )
2. /etc/init.d/mysqld start ( 開啓mysql服務,監控遠程主機的服務,須要遠程主機的服務處於開啓的狀態 )
3. mysql_secure_installation ( 安全校驗,爲了安全須要給予mysql一個密碼 )
4. mysql -pwestos ( 登陸mysql,作如下操做 )
create database nagdb; ( 建立一個數據庫,也可不建立而用裏面的test數據庫 )
grant select on nagdb.* to nagios@'192.168.2.74'; ( 受權,給74主機的nagios用戶授予遠程主機的nagdb數據庫的選擇權限 )
5. /etc/init.d/mysqld restart ( 完成以上操做,重啓mysql服務 )
(2)本地配置
1.檢測:
cd /usr/local/nagios/libexec/
./check_mysql -unagios -d nagdb -H 192.168.2.149 ( -u指定本地的nagios用戶 -d指定遠程主機的數據庫 -H指定遠程主機的ip )
( 上面的命令是爲了測試是否能夠鏈接遠程主機獲取遠程主機服務信息,若有下信息則表明成功 )
運行結果:
Uptime: 2880 Threads: 1 Questions: 19 Slow queries: 0 Opens: 0 Flush tables: 2 Open tables:
26 Queries per second avg: 0.006|Connections=11c;;; Open_files=21;;; Open_tables=26;;; Qcache_free_memory=0;;;
Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=0c;;;
Qcache_queries_in_cache=0;;; Queries=19c;;; Questions=19c;;; Table_locks_waited=0c;;; Threads_connected=1;;;
Threads_running=1;;; Uptime=2880c;;;
2.配置( 過程: services.cfg --> hosts.cfg --> commands.cfg )
1.vim /usr/local/nagios/etc/objects/hosts.cfg ( 主機配置文件 )
( 此模塊是本地主機的信息,是原來配置的,只須要修改一行 )
define host{
use linux-server
host_name server74.example.com
alias Manager
parents server49.example.com *** ( 此處填寫要遠程控制的主機的主機名,別名不識別 )
address 192.168.2.74
icon_image server.gif
statusmap_image server.gd2
2d_coords 500,200
3d_coords 500,200,100
}
( 此模塊是遠程主機的信息,複製上面的模塊進行修改的便可 )
define host{
use linux-server
host_name server49.example.com ( 遠程主機的主機名 )
alias mysql主機 ( 遠程主機的別名 )
address 192.168.2.149 ( 遠程主機的ip )
icon_image server.gif
statusmap_image server.gd2
2d_coords 500,200
3d_coords 500,200,100
}
2.vim /usr/local/nagios/etc/objects/commands.cfg ( 命令配置文件 )
#'check_mysql' command definition ( 此行僅爲註釋 )
( 此模塊是check_mysql,複製一個模塊進行修改便可 )
define command{
command_name check_mysql ( 命令的名稱,能夠修改 )
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -d $ARG2$
( $USER1$:表明/usr/local/nagios/libexec路徑 -H:指定要監控的主機地址 -u:指定本地的nagios用戶 -d:指定遠程主機的數據庫 -p:指定數據庫密碼)
}
3.vim /usr/local/nagios/etc/objects/services.cfg ( 服務配置文件 )
( 此模塊是遠程主機的服務,複製一個模塊進行修改便可 )
define service{
use local-service
host_name server49.example.com ( 監控遠程主機的主機名 )
service_description MYSQL ( 監控遠程主機的服務 )
check_command check_mysql!nagios!nagdb ( 監控遠程主機的服務用的命令 )
}
(6)檢測語法 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg ( 0 0)
重啓服務 /etc/init.d/nagios reload
/etc/init.d/httpd restart
訪問 192.168.2.74/nagios/
* mysql啓動報錯時
重啓時 /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Another MySQL daemon already running with the same unix socket.
Starting mysqld: [FAILED]
刪除文件 rm -fr /var/lib/mysql/mysql.sock
重啓mysql /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
*httpd有警告
vim /var/www/html/index.html ( 寫一個測試頁面便可 )
*ssh有一個喇叭(打着叉叉)表示禁用:當須要網絡頁面開啓時:
usermod -G nagios apache ( 把apache加入nagios用戶組中,apache就有了權限了 )
systemctl restart nagios
*監控頁面出現雲時處理方法 ( 出現雲是由於服務頻繁出故障 )
修改權限 usermod -G nagios apache
重啓nagios /etc/init.d/nagios reload
重啓http /etc/init.d/httpd restart
遠程監控主機的信息
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@3.添加監控遠程主機信息
* 1.遠程主機配置
(1)下載包 lftp 192.168.2.251
get nagios-plugins-2.0.3.tar.gz( 插件,先安裝插件 )
get nrpe-2.15.tar.gz ( 在本地主機上沒有監控遠程主機須要的命令時安裝nrpe,而nagios-plugins是一個插件 )
(2)創建用戶 useradd -u 2000 -s /sbin/nologin -M nagios ( -u:指定uid,用戶的id -s:指定用戶的shell,此處是不等錄 -M:不建立家目錄 最後是用戶名 )
(3)安裝所需服務
yum install gcc make openssl-devel xinetd -y
( gcc和make是編譯時須要用到的,鏈接遠程主機是用ssl的,xinetd是守護進程 )
(4)安裝插件 tar zfx nagios-plugins-2.0.3.tar.gz
cd nagios-plugins-2.0.3
./configure
make && make install
(5)安裝nrpe
解壓 tar zfx nrpe-2.15.tar.gz
安裝 cd nrpe-2.15
./configure
make all
mkae install-plugin ( plugin插件 )
make install-daemon ( Daemon程序,又稱爲守護進程,一般在系統後臺長時間運行,因爲沒有控制終端而沒法與前臺交互,Daemon程序通常做爲系統服務使用。)
make install-daemon-config ( Daemon程序的配置 )
make install-xinetd ( xinetd後臺進程 )
配置 vim /etc/xinetd.d/nrpe ( xinetd是後臺守護進程,nrpe是其中之一 )
only_from = 192.168.2.74 (15) ( 只容許此ip訪問監控 )
vim /etc/services ( 系統全部的服務 )
nrpe 5666/tcp (23) ( 雖然nrpe的端口是5666,可是系統不識別,因此要在此添加 )
vim /usr/local/nagios/etc/nrpe.cfg ( 此文件裏的check_* 就是crpe調用的命令 )
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p / (221)( 此處用的是絕對命令,直接調用便可,沒有變量 )
( 須要監控什麼信息,在此文件裏定義便可,格式如上 )
權限 chown nagios.nagios /usr/local/nagios/* -R ( 修改nagios裏的全部文件的用戶和組爲nagios )
(6)啓服務 /etc/init.d/xinetd start ( 開啓xinetd,nrpe就開啓了,xinetd守護nrpe )
查看端口 netstat -antlp | grep 5666 ( 查看端口,有5666就帶表成功了,nrpe開啓了 )
* 2.本地服務機配置
(1)安裝nrpe
下載 lftp i
get nrpe-2.15.tar.gz
解壓 tar zfx nrpe-2.15.tar.gz
cd nrpe-2.15
安裝 ./configure
make all
make install-plugin
(2)檢測是否安裝成功
查看( 安裝完nrpe後會出現此目錄 )
ll /usr/local/nagios/libexec/check_nrpe ( 監控遠程主機的信息的命令 )
-rwxrwxr-x 1 nagios nagios 76769 Jul 18 22:25 /usr/local/nagios/libexec/check_nrpe
驗證( 驗證是否能夠連接遠程主機得到信息 )
cd /usr/local/nagios/libexec/
./check_nrpe -H 192.168.2.149
./check_nrpe -H 192.168.2.149 -c check_disk
結果:DISK OK - free space: / 5540 MB (80% inode=99%);| /=1317MB;5486;6172;0;6858
./check_nrpe -H 192.168.2.149 -c check_load
結果:OK - load average: 0.00, 0.01, 0.05|load1=0.000;15.000;30.000;0; load5=0.010;10.000;25.000;0; load15=0.050;5.000;20.000;0;
(3)配置命令和服務
命令 vim /usr/local/nagios/etc/objects/commands.cfg ( 命令配置文件 )
#'check_nrpe' command definition (220) ( 添加監控遠程主機的信息的命令 )
define command{
command_name check_nrpe ( 監控遠程主機的信息的命令的名稱 )
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ ( 監控遠程主機的信息的命令的格式 )
}
服務 vim /usr/local/nagios/etc/objects/services.cfg ( 添加遠程主機要檢測的內容 )
#define server49 nrpe
( 監控遠程主機的服務,如下的四個模塊都是相似的,複製本地的模板進行修改便可 )
define service{
use local-service ( 使用服務模板名稱,修改後會出錯 )
host_name server49.example.com *** ( 監控的遠程主機的主機名 )
service_description Root Partition ( 根分區 )
check_command check_nrpe!check_disk *** ( check_nrpe是commands.cfg定義的命令,check_disk )
(check_nrpe是/usr/local/nagios/etc/objects/commands.cfg定義的命令 ,check_disk是/usr/local/nagios/etc/nrpe.cfg定義的 )
}
define service{
use local-service
host_name server49.example.com ***
service_description Current Users ( 登陸用戶數 )
check_command check_nrpe!check_users ***
}
define service{
use local-service
host_name server49.example.com ***
service_description Total Processes ( 進程總數 )
check_command check_nrpe!check_total_procs ***
}
define service{
use local-service
host_name server49.example.com ***
service_description Current Load ( 系統負荷 )
check_command check_nrpe!check_load ***
}
(4)檢測並重啓 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg ( 語法檢測 )
/etc/init.d/nagios reload ( 修改了配置文件,重啓時配置文件生效 )
192.168.2.74/nagios/ ( 訪問頁面,監控的主機信息添加了四項,如今的狀態是未訣,過五分鐘就好(變成綠色的) )
監控主機出現問題時發送郵件
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@3.監控主機出現問題時發送郵件
(1)郵箱 vim /usr/local/nagios/etc/objects/contacts.cfg
email 976807618@qq.com ( 35 )
( 在qq郵箱設置裏的白名單添加nagios@server74.example.com 用戶@主機名 )
( qq郵箱 --> 設置 --> 反垃圾 --> 設置郵件地址白名單--> nagios@server74.example.com )
(2)給予nagios用戶登陸權限
vim /etc/passwd
nagios:x:2000:2000::/usr/local/nagios:/bin/bash
(3)切換用戶,測試發郵件
su - nagios
-bash-4.1$ mail 976807618@qq.com
mailq
(4)登陸郵箱查看,收到郵箱則正常
(5)測試報警功能,停掉監控的任一個服務,看是否會收到郵件,如下以mysql爲例。
/etc/init.d/mysqld stop ( server49 )
/etc/init.d/nagios reload ( server74 )
等待大概5分鐘,192.168.2.74/nagios/頁面監控的MYSQL會變成紅色,而且收到mysql報警的郵件。
/etc/init.d/mysqld start ( server49 )
/etc/init.d/nagios reload ( server74 )
等待大概5分鐘,192.168.2.74/nagios/頁面監控的MYSQL會變成綠色,而且收到mysql恢復的郵件。
監控主機出現問題時發送飛信
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@3.監控主機出現問題時發送飛信
飛信 與nagios整合
* 1.安裝飛信並測試 ( 在nagios用戶下執行 )
(1)權限下發 vim /etc/sudoers.d/ ( 權限下方 )
nagios ALL=NOPASSWD:ALL
(2)切換用戶 su - nagios ( 切換到nagios用戶裏 )
cd usr/local/nagios/libexec/
下載包 lftp i ( 下載飛信包 )
get fetion linuxso_20101113.tar.gz ( 下載飛信包所用到的庫 )
解壓 tar zfx /usr/local/nagios/libexec/linuxso_20101113.tar.gz -C /lib ( 解壓飛信包所用到的庫到系統庫中,保證飛信的庫和系統庫不重複 )
(3)安裝fetion
權限 chmod +x /usr/local/nagios/libexec/fetion ( 給予fetion執行權限 )
chown nagios.nagios /usr/local/nagios/libexec/fetion ( 修改fetion的用戶和用戶組爲nagios )
/usr/local/nagios/libexec/fetion ( 根據提示安裝所需的.so )
安裝 sudo yum install -y /lib/ld-linux.so.2 libstdc++.so.6 libgssapi_krb5.so.2 libz.so.1
/usr/local/nagios/libexec/fetion ( 出現如下狀況表示安裝成功 )
************************ IMPORTANT STATEMENT ************************
** **
** PLEASE DON'T USE THIS SOFTWARE TO SEND JUNK SHORT MESSAGES. **
** OTHERWISE PLEASE BEAR YOUR OWN CONSEQUENCES. **
** **
** Version:[20101205002-linux] **
*********************************************************************
This program is the console version of China Fetion!
It's free for personal user.
This project website: http://www.it-adv.net/
AUTHOR:KelvinH MSN/EMAIL:shichangguo@msn.com
Usage:
--mobile=[mobile] 手機號
--sid=[sid] 飛信號
--pwd=[pwd] 飛信密碼
--config=[config file] *format:index mobile password
--index=[index no in config file,refer to sample.conf]
--debug *debug mode on
--hide *login fetion in hidden state
--to=[mobile/sid] 收信人手機號或飛信號
--command-path=[command file path]
--robotmode
--daemon(linux only)
--proxy-ip(http proxy ip)
--proxy-port(http proxy port)
--msg-gb=[gb2312/gbk message]
--msg-utf8=[utf8 message] 飛信內容
--msg-type=[0/1/2 sms longsms smartmsg]
--file-gb=[gb2312/gbk file]
--file-utf8=[utf8 file]
--query-cmcc-no
--auto-retry
--exit-on-verifycode
--t3key=[http://www.tui3.com/page/smssend/]
(4)測試 /usr/local/nagios/libexec/fetion --mobile=15029986994 --pwd=wangjiaxing0425 --to=15029986994 --msg-utf8="hai"
scp /usr/local/nagios/libexec/15029986994.jpg 192.168.2.1:/root/Desktop/ ( 查看驗證碼 )
( 若是虛擬機是最小化安裝,無圖形界面時看不了圖片,須要遠程複製到物理機桌面 )
輸入驗證碼 mxhy ( 僅第一次須要輸入驗證碼,輸入後出現如下信息便可發送成功 )
SIP-C/4.0 280 Send SMS OK
T: sip:780381999@fetion.com.cn;p=9200
I: 2
Q: 1 M
RQ: 49444554834
L: 112
XI: ae51f4469e4e4f628d580e7ca21fb298
(5)簡化,寫成腳本,方便
ll -d /usr/local/nagios/ ( 查看是不是屬於nagios的,是否能夠編輯文件 )
drwxr-xr-x 11 nagios nagios 4096 Jul 19 03:04 /usr/local/nagios/
( 若是用戶和用戶組都不是nagios,進行以下操做 )
chown nagios.nagios /usr/local/nagios/ -R
腳本 vim /usr/local/nagios/libexec/fetion.sh
/usr/local/nagios/libexec/fetion --mobile=15029986994 --pwd=wangjiaxing0425 --to="$1" --msg-utf8="$2"
權限 chmod +x /usr/local/nagios/libexec/fetion.sh
chown nagios.nagios /usr/local/nagios/libexec/fetion.sh
測試 /usr/local/nagios/libexec/fetion.sh 15029986994 "測試" ( 15029986994是發送的手機號 )
* 2.飛信與nagios整合,監控服務出現問題時會發出飛信( 在nagios用戶下執行 )
(1)聯繫方式 vim /usr/local/nagios/etc/objects/contacts.cfg
pager 15029986994 (35)
(2)模板 vim /usr/local/nagios/etc/objects/templates.cfg
service_notification_commands notify-service-by-email,notify-service-by-fetion (33)
host_notification_commands notify-host-by-email,notify-host-by-fetion
(3)命令 vim /usr/local/nagios/etc/objects/commands.cfg
(如下內容空餘複製文件上面的進行修改便可,留下最後「」裏的內容刪除**,前面添加$USER1$/fetion.sh $CONTACTPAGER$)
# 'notify-host-by-fetion' command definition
define command{
command_name notify-host-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$"
}
# 'notify-service-by-fetion' command definition
define command{
command_name notify-service-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}
* 3.在root下執行
檢測語法 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg ( 0 0 )
重啓服務 /etc/init.d/nagios reload
測試 /etc/init.d/mysqld stop ( server49 )
( 網頁中mysql的 通知要啓用,抖動要禁用 )
五分鐘後會收到關於mysql報警的短信
html