##############################################################################
監控:
1、監控
監視和控制
2、監控對象
服務器
3、監控的資源
硬件使用率(cpu、內存、存儲)
服務的狀態(啓動 停止)
操作系統的運行情況(用戶數 進程數)
網絡接口的流量
4、方法
使用cron的執行,自定義監控腳本
系統對應的命令查看
監控軟件
###############################################################################
nagios監控軟件:
一、部署LAMP環境
#yum -y install httpd
#yum -y install mariadb
#yum -y install php php-mysql
#systemctl restart httpd
#systemctl enable httpd
#systemctl start mariadb.service
#systemctl enbale mariadb.service
####################################
二、安裝nagios(源碼)
2.1、安裝準備
編譯環境
#yum -y install gcc gcc-c++
創建運行nagios服務的用戶和組
#useradd nagios
#groupadd nagcmd
#usermod -G nagcmd nagios
2.2、裝包
1、解壓
#tar xzf nagios-4.2.4.tar.gz
#cd nagios-4.2.4/
2、配置
#./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --with-command-user=nagios --with-command-group=nagcmd
3、編譯
#make all //根據提示進行安裝
4、安裝
# make install //安裝主程序
#make install-init //控制腳本
#make install-commandmode //調權限
#make install-config //安裝配置文件
#make install-webconf //網站配置
#make install-exfoliation //網站風格
5、安裝目錄說明
#ls /usr/local/nagios
bin //可執行命令
etc //配置文件
libexec //監控插件
sbin //cgi文件
share //網頁文件
var //日誌文件狀態信息文件
2.3、安裝監控插件
#tar -zxvf nagios-plugins-2.1.4.tar.gz
#cd nagios-plugins-2.1.4/
#./configure
#make
#make install
#ls /usr/local/nagios/libexec/check_*
2.4、設置登陸監控網頁的用戶和密碼
#rpm -q httpd-tools-2.4.6-40.el7.x86_64 //提供htpasswd命令
# htpasswd -c /usr/local/nagios/etc/htpasswd.users //文件名以及路徑必須正確,用戶名必須爲nagiosadmin
#cat /usr/local/nagios/etc/htpasswd.users
# grep "nagiosadmin" /usr/local/nagios/etc/cgi.cfg
2.5、啓動nagios服務
#/etc/rc.d/init.d/nagios start|stop|status
2.6、在客戶端254訪問監控服務器的監控頁面
#http://192.168.4.21/nagios //192.168.4.21監控服務器地址
##############################################################
三、
1、默認監控本機:
cpu負載 check_load
登陸用戶數 check_user
http服務 check_http
PING check_ping
根分區 check_disk
ssh服務 check_ssh
交換分區 check_swap
進程數量 check_procs
連接目標主機的tcp協議端口 check_tcp
2、監控狀態
正常 ok
警告 warning
不知道 unknown
嚴重錯誤 critical
監控中 pending
###############################################################################
四、配置nagios服務
4.1、監控過程
nagios服務加載主配置文件nagios.cfg,主配置文件調用監控插件,運維人員可以設置監控的監控閥值(警告值 和 錯誤值)
nagios服務的插件將監控到的數據和監控閥值進行比較,根據結果來顯示監控狀態
監控到的數據 < 警告值 顯示 ok
監控到的數據 > 警告值 < 錯誤值 顯示 WARNING
監控到的數據 > 錯誤值 顯示 critical
UNKNOWN 配置有問題
pending 正在獲取數據
##############################################
4.2、監控插件的使用
#cd /usr/local/nagios/libexec/ //切換到監控插件目錄下
#./插件名
#監控插件目錄路徑/插件名 --help //查看幫助
舉例:
#./check_user -w 10 -c 20
//當前系統登陸用戶數超過10則警告,超過20則嚴重錯誤
###############################################
4.3、配置文件說明
#ls /usr/local/nagios/etc
cgi.cfg //授權文件(訪問權限)
htpasswd.users //登陸監控頁面的用戶信息文件
nagios.cfg //主配置文件
objects //監控插件配置文件目錄
resource.cfg //宏定義文件
#ls /usr/local/nagios/etc/objects
commands.cfg //定義監控命令的配置文件
timeperiods.cfg //定義監控時間的文件
templates.cfg //定義監控模塊文件
contacts.cfg //定義報警郵件發送的文件
localhost.cfg //監控本機配置文件
###############################################################
4.4、監控本機
1、默認監控本機的幾種服務以及系統運行情況。
2、添加監控項:
2.1、自定義監控命令
格式:
#cd /usr/local/nagios/etc/objects
#vim commands.cfg
define command {
command_name 命令名 //自定義
command_line 監控命令
}
舉例:自定義監控本機/boot分區的容量
define command {
command_name check_local_boot
command_line /usr/local/nagios/libexec/check_disk -w 50% -c 25% -p /boot
}
//監控本機/boot分區,剩餘空間小於50%發警告郵件,少於25%發嚴重錯誤郵件
2.2、配置監控本機配置文件
格式:
#vim localhost.cfg
define host {
use linux-server //模塊名templates.cfg文件中,有各個模塊的配置
host_name 主機名 //自定義
address 被監控主機的ip地址
define service{
use local-service //模塊名
host_name 主機名 //和define host一致,監控本機就是localhost
service_description 服務描述 //對被監控服務的簡單描述
check_command 監控命令 //commands.cfg文件中定義的命令名稱
}
舉例:
#vim localhost.cfg
define host {
use linux-server
host_name localhost
address 192.168.4.21
}
define service {
use local-service
host_name localhost
service_description boot
check_command check_local_boot
}
#############################################################
2.3、重啓服務
#/etc/rc.d/init.d/nagios restart
#############################################################
2.4、驗證
http://192.168.4.21/nagios
//查看service下的結果
########################################################################################
五、監控遠端主機
(一)監控遠端主機進程:因爲進程和服務不同的在於。服務可以通過監控對應的端口來獲取狀態,進程時沒有端口的,所以進程的監控需要通過以下步驟來實現
原理:
監控端:nagios+check_nrpe
被監控端:NPRE+check_disk check_load ...
被監控端的NRPE服務,被監控端的NPRE服務獲取到本地監控插件的監控數據,監控端通過check_nrpe插件來連接被監控端的NRPE服務來獲取數據,交給nagios處理。這個獲取過程採用的時SSL加密通信。
1、配置被監控端
裝監控插件
#yum -y install gcc gcc-c++
#tar xzf nagios-plugins-2.1.4.tar.gz
#cd nagios-plugins-2.1.4/
#./configure
#make && make install
裝NRPE服務
#yum -y install openssl openssl-devel //加密通信
#useradd nagios //NRPE服務運行的用戶
#tar xzf nrpe-3.0.1.tar.gz
#cd nrpe-3.0.1/
#./configure
#make //列出所有安裝項
#make all //編譯
#ls /root/nrpe-3.0.1/docs/ //NRPE.pdf安裝說明書
#make install //安裝NRPE和check_nrpe插件
#make install-config //安裝配置文件
#make install-init //安裝控制腳本
修改配置文件
#vim /usr/local/nagios/etc/nrpe.cfg
98 allowed_hosts=127.0.0.1, 192.168.4.21 //默認僅允許本機連接,添加監控服務器的地址192.168.4.21
287 command[nrpe_check_users]=/usr/local/nagios/libexec/check_users -w 1 -c 2
288 command[nrpe_check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
289 command[nrpe_check_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
290 command[nrpe_check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
291 command[nrpe_check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
//定義監控命令
重啓本機測試:
#systemctl restart nrpe
#/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -p 5666 -c nrpe_check_users
//調用check_nrpe插件 -H 被監控主機地址(測試NRPE服務,所以使用本機迴環地址) -p (NRPE服務的端口) -c 監控命令(nrpe.cfg中定義)
##############################################################################
2、配置監控端
安裝check_nrpe插件
#tar -zxf nrpe-3.0.1.tar.gz
#cd nrpe-3.0.1/
#./configure
#make
#make all
#make install-plugin
測試插件
#/usr/local/nagios/libexec/check_nrpe -H 192.168.4.19 -p 5666 -c nrpe_check_users
修改配置文件
#cd /usr/local/nagios/etc/objects
#vim commands.cfg
define command {
command_name check_19_user //自定義
command_line $USER1$/check_nrpe -H 192.168.4.19 -p 5666 -c nrpe_check_users //調用插件
}
//進程需要通過nrpe來獲取192.168.4.19上的監控數據
define command {
command_name check_19_http
command_line $USER1$/check_http -H 192.168.4.19 -p 80
}
//監控服務
#vim monitor19.cfg //自己創建
define host {
use linux-server
host_name www.tarena.com
address 192.168.4.19
}
define service {
use local-service
host_name www.tarena.com
service_description http
check_command check_19_http
define service {
use local-service
host_name www.tarena.com
service_description users
check_command check_19_user
}
#vim /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/monitor19.cfg //主配置文件添加,重啓服務讀取monitor.cfg文件
##################################################################
3、驗證
#systemctl restart nagios
#http://192.168.4.21/nagios
4、設置本機nagios用戶接收報警郵件
#yum -y install postfix
#systemctl restart postfix
#vim /usr/local/nagios/etc/contacks.cfg //文件中默認的收件用戶爲nagios
#mail -u nagios //如果有報警郵件,就可以在這裏查看