搭建Nagios監控服務器

Linux監控 IDC監控

監控的對象:服務器(運維崗位)
監控什麼:(資源)
1、硬件的使用率(cpu 內存 存儲)
2、監控應用服務的狀態(運行 停止)
3、操作系統的運行情況(進程數量 系統的用戶數量)
4、網絡接口數據流量(進 出)

如何監控?
使用crond執行自定義監控腳本
系統提供的對應命令查看
使用軟件搭建監控服務做監控

Nagios監控服務器
監控服務器環境192.168.4.21
一、部署LAMP服務
yum安裝httpd、mariadb、mariadb-server、php、php-mysql
[root@monitor21 ~]# vim /var/www/html/test.php
<?php
$x=mysql_connect("localhost","root","123456");
if($x){ echo "ok"; }else{ echo "err"; };
?>

#################################################################
二、安裝Nagios(源碼包)
2.1 安裝準備:編譯工具 創建用戶和組
[root@monitor21 ~]# rpm -q gcc gcc-c++
[root@monitor21 ~]# useradd nagios
[root@monitor21 ~]# groupadd nagcmd
[root@monitor21 ~]# usermod -G nagcmd nagios
2.2 裝包:解包 配置 編譯 安裝 查看安裝信息
[root@monitor21 ~]# tar xf nagios-4.2.4.tar.gz
[root@monitor21 ~]# cd nagios-4.2.4
[root@monitor21 nagios-4.2.4]# ./configure --help | more
--with-command-user=

[root@monitor21 nagios-4.2.4]# ./configure --with-nagios-user=nagios \

--with-nagios-group=nagcmd --with-command-user=nagios \
--with-command-group=nagcmd
[root@monitor21 nagios-4.2.4]# make all //編譯
[root@monitor21 nagios-4.2.4]# make install //安裝程序
[root@monitor21 nagios-4.2.4]# make install-init //安裝控制腳本
// # vim /etc/rc.d/init.d/nagios 獲得此腳本
[root@monitor21 nagios-4.2.4]# make install-commandmode //調權限
[root@monitor21 nagios-4.2.4]# make install-config //安裝配置
[root@monitor21 nagios-4.2.4]# make install-webconf //部署網站配置
[root@monitor21 nagios-4.2.4]# make install-exfoliation //確定網頁風格

2.3 安裝目錄說明
[root@monitor21 nagios-4.2.4]# ls /usr/local/nagios/
bin(可執行命令) etc(配置文件) libexec(監控插件) sbin(cgi文件) share(網頁文件) var(日誌文件狀態信息文件)
[root@monitor21 nagios-4.2.4]# cd /usr/local/nagios/
[root@monitor21 nagios]# ls bin
nagios(驗證配置是否正確) nagiostats(顯示監控狀態信息)

2.4 安裝監控插件
[root@monitor21 ~]# tar -zxf nagios-plugins-2.1.4.tar.gz
[root@monitor21 ~]# cd nagios-plugins-2.1.4/
[root@monitor21 nagios-plugins-2.1.4]# ./configure &&make &&make install
[root@monitor21 nagios-plugins-2.1.4]# ls /usr/local/nagios/libexec/

2.5 啓動nagios監控服務
(1) 設置登錄監控頁面的用戶(nagiosadmin)和密碼(自定義)
[root@monitor21 nagios-plugins-2.1.4]# vim /etc/httpd/conf.d/nagios.conf 查看監控頁面用戶的路徑
[root@monitor21 ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
[root@monitor21 ~]# ls /usr/local/nagios/etc/
cgi.cfg htpasswd.users nagios.cfg objects resource.cfg
[root@monitor21 ~]# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:$apr1$KGjigEh.$Vtf6Vv.y7LRv4T7t48R7a1
[root@monitor21 ~]# systemctl restart httpd //重啓網站服務
[root@monitor21 ~]# /etc/rc.d/init.d/nagios status //查看nagios狀態
No lock file found in /usr/local/nagios/var/nagios.lock
[root@monitor21 ~]# /etc/rc.d/init.d/nagios start //起nagios服務
Reloading systemd: [ 確定 ]
Starting nagios (via systemctl): [ 確定 ]

默認不用任何配置,就監控本機:
cpu負載 check_load
登錄系統用戶數量 check_users -w 1 -c 3
網站服務運行狀態 check_http -H ip地址 [ -p 端口 ]
PING check_ping
根分區 check_disk
ssh服務 check_ssh
交換分區 check_swap
進程數量 check_procs
連接目標主機的tcp協議端口 check_tcp -H ip地址 -p 端口
###################################################################

三、在客戶端 訪問監控頁面查看監控信息
ping -c 2 192.168.4.21
firefox http://192.168.4.21/nagios

###################################################################

四、配置nagios服務
4.1 監控過程
監控狀態:Ok(正常) Warning(警告) Unknown(不知道) Critical(嚴重錯誤) Pending(監控中)
nagios服務運行是加載主配置文件ngaios.cfg,在配置文件中調用監控插件,運維人員是可以設置插件的監控閥值(警告值 和 錯誤值);nagios服務的插件把監控到的數據和 監控閥值 比較,根據比較結果來顯示監控狀態:
監控到的數據 < 警告值 顯示OK
監控到的數據 > 警告值 且<錯誤值 顯示Warning
監控到的數據 > 錯誤值 顯示Critical
找不到調用的監控插件時 Unknown
正在獲取數據 Pending
#################################################################
4.2 監控插件的使用
[root@monitor21 libexec]# cd /usr/local/nagios/libexec
./插件名 -h //查看插件的幫助信息
[root@monitor21 libexec]# ./check_users -w 1 -c 3 //設置警告值1,錯誤值3
USERS WARNING - 2 users currently logged in |users=2;1;3;0
[root@monitor21 libexec]# ./check_ping -H 192.168.4.17 -w 10,50% -c 20,75%
PING OK - Packet loss = 0%, RTA = 0.48 ms|rta=0.476000ms;10.000000;20.000000;0.000000 pl=0%;50;75;0

###########################################################
4.3 配置文件說明
[root@monitor21 ~]# cd /usr/local/nagios/etc
[root@monitor21 etc# ls
cgi.cfg htpasswd.users nagios.cfg objects resource.cfg

nagios.cfg(主配置文件)
[root@monitor21 etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //驗證主配置文件
[root@monitor21 etc]# /usr/local/nagios/bin/nagiostats -c /usr/local/nagios/etc/nagios.cfg //命令行下輸出監控統計信息
在/etc/bashrc下定義別名:alias checknagios='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'

resource.cfg 宏定義文件(設置nagios服務使用的變量)
$USER1$=/usr/local/nagios/libexec //默認的變量USER1=插件路徑

[root@monitor21 etc]# cd objects/
[root@monitor21 objects]# ls
commands.cfg localhost.cfg switch.cfg timeperiods.cfg
contacts.cfg printer.cfg templates.cfg windows.cfg
++++++++++++++++++++++++++++++++++++++
commands.cfg 定義監控命令的文件

define command{
command_name 命令名
command_line 路徑/插件名 參數
}
vim commands.cfg
在最後一行自定義一條監控命令
define command{
command_name check_local_boot
command_line /usr/local/nagios//libexec/check_disk -w 50% -c 25% -p /boot
}
+++++++++++++++++++++++++++++++++++++++++++++++++
localhost.cfg 監控本機配置文件
監控主機
define host{
use linux-server模板名
host_name 主機名
address 被監控主機的ip地址
}

監控資源
define service{
use local-service模板名
host_name 主機名
service_description 描述信息
check_command 命令名
}
+++++++++++++++++++++++++++++++++++++++++++++
contacts.cfg 指定接收監控報警消息郵箱地址
34 email nagios@localhost //34行郵件提醒
vim /etc/hosts //增加本機主機名在127.0.0.1,同時測試本機郵件是否能正常使用
[root@monitor21 ~]# mail -s "csadfgas" nagios < /etc/hosts
[root@monitor21 ~]# su - nagios
[nagios@monitor21 ~]$ mail
+++++++++++++++++++++++++++++++++++++++++++
timeperiods.cfg 定義監控時間模版配置文件
templates.cfg 定義監控模版配置文件

4.4 配置遠端主機(192.168.4.18)
4.4.1 監控公有數據(服務)
網站服務 sshd服務 監控數據庫服務
1) 定義監控命令 commands.cfg

/usr/local/nagios/libexec/check_tcp -H 192.168.4.18
/usr/local/nagios/libexec/check_tcp -H 192.168.4.18 -p 80
/usr/local/nagios/libexec/check_tcp -H 192.168.4.18 -p 3306
[root@monitor21 objects]# vim /usr/local/nagios/etc/objects/commands.cfg

###################### monitor host 18 ########################
define command{
command_name check_18_ssh
command_line $USER1$/check_tcp -H 192.168.4.18 -p 22
}

define command{
command_name check_18_http
command_line $USER1$/check_tcp -H 192.168.4.18 -p 80
}

define command{
command_name check_18_mysql
command_line $USER1$/check_tcp -H 192.168.4.18 -p 3306
}

2)創建監控主機18配置文件 ser18.cfg

[root@monitor21 objects]# vim ser18.cfg

define host{
use linux-server
host_name web.example.com
address 192.168.4.18
}

define service{
use local-service
host_name web.example.com
service_description sshd
check_command check_18_ssh
}

define service{
use local-service
host_name web.example.com
service_description httpd
check_command check_18_http
}

define service{
use local-service
host_name web.example.com
service_description mysqld
check_command check_18_mysql
}

3) 在主配置文件里加載監控遠端主機18的配置文件 nagios.cfg
[root@monitor21 objects]# vim /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/ser18.cfg
4) 檢查以上配置 checknagios (定義的別名)
[root@monitor21 objects]# checknagios
5)重啓nagios服務
[root@monitor21 ~]# /etc/rc.d/init.d/nagios restart
6)訪問監控頁面查看監控信息
4.4.2 監控私有數據(系統運行情況)
A 配置被監控端192.168.4.18
1 安裝獲取數據插件
安裝nagios-plugins-2.1.4.tar.gz ,解壓,yum源安裝gcc,gcc-c++
[root@418 ~]# tar -zxf nagios-plugins-2.1.4.tar.gz
[root@418 ~]# cd nagios-plugins-2.1.4
[root@418 nagios-plugins-2.1.4]## yum -y install gcc gcc-c++
[root@418 nagios-plugins-2.1.4]## ./configure && make && make install
[root@418 nagios-plugins-2.1.4]## /usr/local/nagios/libexec/check_users -w 1 -c 2
[root@418 nagios-plugins-2.1.4]## /usr/local/nagios/libexec/check_disk -w 30% -c 25% -p /
[root@418 nagios-plugins-2.1.4]## /usr/local/nagios/libexec/check_procs -w 40 -c41 -s R
2 運行NRPE服務
2.1 裝包準備
2.2 裝包
2.3 修改配置文件
2.4 啓動服務
2.5 測試配置

openssl-devel
useradd nagios
tar -zxf nrpe-3.0.1.tar.gz
cd nrpe-3.0.1/
./configure
make all
make install
make install-config
make install-init
[root@418 nrpe-3.0.1]# systemctl status nrpe
[root@418 nrpe-3.0.1]# vim /usr/local/nagios/etc/nrpe.cfg
98 allowed_hosts=127.0.0.1, 192.168.4.21
289 command[nrpe_check_root]=/usr/local/nagios/libexec/checkdisk -w 20% -c 10% -p / //287-291[]內加nrpe
[root@418 nrpe-3.0.1]# netstat -untalp | grep :5666 //沒有端口爲5666的進程
[root@418 nrpe-3.0.1]# systemctl start nrpe
[root@418 nrpe-3.0.1]# systemctl enable nrpe
[root@418 nrpe-3.0.1]# netstat -untalp | grep :5666
[root@418 nrpe-3.0.1]# /usr/local/nagios/libexesc/check_nrpe -H 127.0.0.1 -p 5666 -c nrpe_check_users

192.168.4.21
[root@monitor21 ~]# /etc/rc.d/init.d/nagios restart
查看監控頁面

B 配置監控服務器 192.168.4.21
1 提供連接nrpe服務的插件
2 定義獲取私有數據的命令
3 在監控主機的配置文件裏調用定義的命令
4 檢查以上配置 checknagios
5 重啓nagios服務
6 訪問監控頁面查看監控信息

[root@monitor21 ~]# tar -xf nrpe-3.0.1.tar.gz
[root@monitor21 ~]# cd nrpe-3.0.1/
[root@monitor21 nrpe-3.0.1]# ./configure
[root@monitor21 nrpe-3.0.1]# make all
[root@monitor21 nrpe-3.0.1]# make install-plugin
[root@monitor21 nrpe-3.0.1]# /usr/local/nagios/libexec/check_nrpe -H 192.168.4.18 -p 5666 -c nrpe_check_users
[root@monitor21 nrpe-3.0.1]# vim /usr/local/nagios/etc/objects/commands.cfg
#users
define command{
command_name check_18_user
command_line $USER1$/check_nrpe -H 192.168.4.18 -p 5666 -c nrpe_check_users
}
[root@monitor21 nrpe-3.0.1]# vim /usr/local/nagios/etc/objects/ser18.cfg
define service{
use local-service
host_name web.example.com
service_description users
check_command check_18_user
}
[root@monitor21 nrpe-3.0.1]# checknagios

4.5 監控本機 localhost.cfg
添加新的監控項 監控本機的引導分區
修改已有監控項的監控閥值 用戶數量
刪除已有的監控項 不監控交換分區
vim localhost.cfg
###############my monitor############################
define service{
use local-service ; Name of service template to use
host_name localhost
service_description boot
check_command check_local_boot
}

4.6 監控報警
[root@monitor21 ~]# dd if=/dev/zero of=/boot/a.txt bs=1M count=240 使得/boot下容量超標,若配置正確,會有報警郵件發出
[root@monitor21 ~]# su - nagios
[nagios@monitor21 ~]$ mail
N 3 [email protected] Mon Jan 8 06:08 31/897 " PROBLEM Service Al"
[root@monitor21 ~]# rm -rf /boot/a.txt //刪除超標文件,模擬修復成功
[nagios@monitor21 ~]$ mail
N 4 [email protected] Mon Jan 8 06:18 31/881 "
RECOVERY Service A"
監控網頁也顯示正常
+++++++++++++++++++++++++++++++++++++++++++++++

nagios擴展內容
1 監控時調用自定義監控腳本
2 使用第三方郵件服務器發送報警郵件
3 主機依賴監控
4 服務依賴監控
5 讓nagios 把監控到數據繪製圖片

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章