1. 程式人生 > >搭建Nagios監控服務器

搭建Nagios監控服務器

su - 默認 plugin 信息 tin 2.3 -m name 狀態

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 配置遠端主機
4.4.1 監控公有數據(服務)
4.4.2 監控私有數據(系統運行情況)
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 把監控到數據繪制圖片

搭建Nagios監控服務器