搭建Nagios監控伺服器

來源:互聯網
上載者:User

標籤:obj   pass   over   install   模板   c++   營運   cron   ++   

Linux監控 IDC監控

監控的對象:伺服器(營運崗位)
監控什麼:(資源)
1、硬體的使用率(cpu 記憶體 儲存)
2、監控應用服務的狀態(運行 停止)
3、作業系統的運行情況(進程數量 系統的使用者數量)
4、網路介面資料流量(進 出)

如何監控?
使用crond執行自訂監控指令碼
系統提供的對應命令查看
使用軟體搭建監控服務做監控

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

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

[[email protected] nagios-4.2.4]# ./configure --with-nagios-user=nagios \

--with-nagios-group=nagcmd --with-command-user=nagios \
--with-command-group=nagcmd
[[email protected] nagios-4.2.4]# make all //編譯
[[email protected] nagios-4.2.4]# make install //安裝程式
[[email protected] nagios-4.2.4]# make install-init //安裝控制指令碼
// # vim /etc/rc.d/init.d/nagios 獲得此指令碼
[[email protected] nagios-4.2.4]# make install-commandmode //調許可權
[[email protected] nagios-4.2.4]# make install-config //安裝配置
[[email protected] nagios-4.2.4]# make install-webconf //部署網站配置
[[email protected] nagios-4.2.4]# make install-exfoliation //確定網頁風格

2.3 安裝目錄說明
[[email protected] nagios-4.2.4]# ls /usr/local/nagios/
bin(可執行命令) etc(設定檔) libexec(監控外掛程式) sbin(cgi檔案) share(網頁檔案) var(記錄檔狀態資訊檔)
[[email protected] nagios-4.2.4]# cd /usr/local/nagios/
[[email protected] nagios]# ls bin
nagios(驗證配置是否正確) nagiostats(顯示監控狀態資訊)

2.4 安裝監控外掛程式
[[email protected] ~]# tar -zxf nagios-plugins-2.1.4.tar.gz
[[email protected] ~]# cd nagios-plugins-2.1.4/
[[email protected] nagios-plugins-2.1.4]# ./configure &&make &&make install
[[email protected] nagios-plugins-2.1.4]# ls /usr/local/nagios/libexec/

2.5 啟動nagios監控服務
(1) 設定登入監控頁面的使用者(nagiosadmin)和密碼(自訂)
[[email protected] nagios-plugins-2.1.4]# vim /etc/httpd/conf.d/nagios.conf 查看監控頁面使用者的路徑
[[email protected] ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
[[email protected] ~]# ls /usr/local/nagios/etc/
cgi.cfg htpasswd.users nagios.cfg objects resource.cfg
[[email protected] ~]# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:$apr1$KGjigEh.$Vtf6Vv.y7LRv4T7t48R7a1
[[email protected] ~]# systemctl restart httpd //重啟網站服務
[[email protected] ~]# /etc/rc.d/init.d/nagios status //查看nagios狀態
No lock file found in /usr/local/nagios/var/nagios.lock
[[email protected] ~]# /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 監控外掛程式的使用
[[email protected] libexec]# cd /usr/local/nagios/libexec
./外掛程式名 -h //查看外掛程式的協助資訊
[[email protected] libexec]# ./check_users -w 1 -c 3 //設定警告值1,錯誤值3
USERS WARNING - 2 users currently logged in |users=2;1;3;0
[[email protected] 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 設定檔說明
[[email protected] ~]# cd /usr/local/nagios/etc
[[email protected] etc# ls
cgi.cfg htpasswd.users nagios.cfg objects resource.cfg

nagios.cfg(主設定檔)
[[email protected] etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //驗證主設定檔
[[email protected] 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=外掛程式路徑

[[email protected] etc]# cd objects/
[[email protected] 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 [email protected] //34行寄件提醒
vim /etc/hosts //增加本機主機名稱在127.0.0.1,同時測試本機郵件是否能正常使用
[[email protected] ~]# mail -s "csadfgas" nagios < /etc/hosts
[[email protected] ~]# su - nagios
[[email protected] ~]$ 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 監控警示
[[email protected] ~]# dd if=/dev/zero of=/boot/a.txt bs=1M count=240 使得/boot下容量超標,若配置正確,會有警示郵件發出
[[email protected] ~]# su - nagios
[[email protected] ~]$ mail
N 3 [email protected] Mon Jan 8 06:08 31/897 " PROBLEM Service Al"
[[email protected] ~]# rm -rf /boot/a.txt //刪除超標檔案,類比修複成功
[[email protected] ~]$ mail
N 4 [email protected] Mon Jan 8 06:18 31/881 "
RECOVERY Service A"
監控網頁也顯示正常
+++++++++++++++++++++++++++++++++++++++++++++++

nagios擴充內容
1 監控時調用自訂監控指令碼
2 使用第三方郵件伺服器發送警示郵件
3 主機依賴監控
4 服務依賴監控
5 讓nagios 把監控到資料繪製圖片

搭建Nagios監控伺服器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.