linux Nagios監控

來源:互聯網
上載者:User

標籤:預設   拒絕   htpasswd   path   templates   查看cpu   類資訊   輸送量   設定檔   

監控目標
監控目標主機分為四個部分
硬體資源
作業系統
資料庫
應用軟體

監控目的:
進行伺服器效能調整前,知道調整什麼,系統瓶頸在什麼地方
被一部分必須同時監控,內容包括輸送量,反應時間,使用率等

監控命令:
進程監控
ps
top
儲存監控
free
swapon -s
df -h
網路監控
ifconfig
netstat
ping
traceroute
效能監控
常用的效能監控命令
uptime cpu負載
mpastat 查看cpu狀態
vmstat 查看記憶體狀態
iostat 查看硬碟狀態
netstat 查看網路狀態
sar 查看以上四類資訊

——————————————————————————————————————————————————————————————————

監控服務
Nagios
是一款用來監視系統和網路的開源應用軟體,可以在linux或unix作業系統上運行
利用眾多的外掛程式實現對本機和遠端服務的監控,當監控對象出現異常,nagios就會及時給管理員警示
可以自訂shell指令碼進行監控
監控主機資源和網路服務,通過web頁面來監控對象狀態

Nagios服務運行時,自動調用監控外掛程式目錄下的外掛程式對指定伺服器的資源做監控
在調用外掛程式時,管理員可以配置,調用監控外掛程式對資源做監控的值,

值分兩種,一種警告值(數字,百分比),一種是錯誤值(數字,百分百)
如果監控對象的使用率小於警告值就是正常狀態,顯示ok
如果監控對象的使用率大於警告值且小於錯誤值,顯示warning
監控對象的使用率大於錯誤值,是錯誤狀態,顯示crtual

Cacti
一套基於php,mysql,snmp,rrdtool開發的網路流量監測圖形分析工具
cacti通過snmpget擷取資料,使用rrdtool繪畫圖形,最後通過web方式呈現出來
cacti三層架構
資料擷取層:通過snmp或自訂指令碼進行資料擷取
資料存放區層:通過cacti模板等資料存放至mysql中
資料展現層:通過web方式呈現出來

Nagios與Cacti區別
Nagios:故障分析比較好,警示機制比較好
Cacti:流量與圖形展現比較好

————————————————————————————————————————————————————————————————————

搭建監控伺服器
配置思路
1.定義監控命令:
[[email protected] objects]# vim commands.cfg
define command{
command_name (命令名)
command_line (設定使用的外掛程式)
}
2.在監控伺服器的設定檔裡調用定義的監控命令:
[[email protected] objects]# vim localhost.cfg
define service{
use (監控資源使用的模板)
host_name (監控的主機名稱)
service_description (監控的描述資訊)
check_command(監控命令)
}

3.驗證nagios配置格式:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
4.重啟監控服務:
/etc/init.d/nagios restart
5.訪問監控頁面,查看監控資訊

執行個體:
在zhuji1伺服器上部署nagios監控服務

安裝環境:
[[email protected] ~]# groupadd nagcmd(nagios的設定檔裡只有這個使用者組才有許可權)
[[email protected] ~]# useradd nagios(添加使用者)
[[email protected] ~]# usermod -G nagcmd nagios(放入組)
[[email protected] ~]# yum -y install gcc gcc-c++(源碼包安裝,安裝編譯工具)
[[email protected] ~]# yum -y install httpd(監控通過web頁面查看,所以要安裝httpd)
[[email protected] ~]# echo hail hydra > /var/www/html/index.html(測試頁面)
[[email protected] ~]# yum -y install php(頁面是php寫的,安裝php以解析頁面)
[[email protected] ~]# vim /var/www/html/test.php(測試頁面)
<?php
echo "hail hydra";
?>
[[email protected] ~]# yum -y install elinks(命令列訪問工具)
安裝Nagios
[[email protected] ~]# unzip nagios.zip
[[email protected] ~]# cd nagios
[[email protected] nagios]# ls
nagios-3.2.1.tar.gz nrpe-2.12.tar.gz
nagios-plugins-1.4.14.tar.gz ntop-3.3.7.tar.gz
[[email protected] nagios]# tar -xf nagios-3.2.1.tar.gz
[[email protected] nagios-3.2.1]# ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --with-command-user=nagios --with-command-group=nagcmd(可以使用這個軟體的使用者,使用者組)
type ‘make all‘ to compile the main program and CGIs.(提示執行make all)
[[email protected] nagios-3.2.1]# make all(執行後會提示下一步)
make install(安裝)
make install-init(安裝初始化啟動指令碼)
make install-commandmode(修改存取權限)
make install-config(安裝設定檔的列子檔案)
make install-webconf(安裝網頁設定檔)
[[email protected] nagios-3.2.1]# make install
[[email protected] nagios-3.2.1]# make install-init
[[email protected] nagios-3.2.1]# make install-commandmode
[[email protected] nagios-3.2.1]# make install-config
[[email protected] nagios-3.2.1]# make install-webconf
[[email protected] ~]# ls /usr/local/nagios/(安裝目錄)
bin etc libexec sbin share var
安裝Nagios監控外掛程式
[[email protected] nagios]# tar -xf nagios-plugins-1.4.14.tar.gz
[[email protected] nagios-plugins-1.4.14]# ./configure
[[email protected] nagios-plugins-1.4.14]# make
[[email protected] nagios-plugins-1.4.14]# make install
[[email protected] ~]# ls /usr/local/nagios/libexec/check_*(外掛程式目錄)
啟動Nagios監控服務:(預設不用作任何配置,Nagios服務預設監控本機)
[[email protected] ~]# /etc/rc.d/init.d/nagios start
[[email protected] ~]# vim /etc/httpd/conf.d/nagios.conf(開啟設定檔)
39 AuthUserFile /usr/local/nagios/etc/htpasswd.users(使用者認證的目錄)
[[email protected] etc]# vim cgi.cfg(根據這裡面的使用者資訊,建立使用者)
authorized_for_system_information=nagiosadmin(看系統認證資訊的名)
...... .......... ........... .....
[[email protected] ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin(建立使用者)
New password: 密碼
Re-type new password: 確認密碼
訪問Nagios監控服務的web頁面,查看監控資訊
[[email protected] ~]# /etc/init.d/httpd restart
[[email protected] ~]# firefox http://192.168.4.1/nagios
預設監控的資源
Current Load:cpu負載
Current Users:登陸的使用者數
HTTP:網站服務
PING:是否線上
Root Partition:系統根分區使用量
SSH:遠程服務
Swap Usage:交換分區的使用方式
Total Processes:總進程數

nagios服務組態檔說明
[[email protected] ~]# cd /usr/local/nagios/etc/
[[email protected] etc]# ls
cgi.cfg(定義訪問cgi檔案的使用者名稱) htpasswd.users nagios.cfg(nagios服務的主設定檔) objects resource.cfg(定義nagios服務使用的變數)
[[email protected] objects]# ls
commands.cfg(定義監控命令) localhost.cfg(監控原生設定檔) switch.cfg timeperiods.cfg(定義監控時間模板)
contacts.cfg(定義接收警示郵件的郵箱地址) printer.cfg templates.cfg(定義監控模板) windows.cfg

監控外掛程式的使用
[[email protected] ~]# cd /usr/local/nagios/libexec/
監控使用者:
[[email protected] libexec]# ./check_users
格式:check_users -w <users> -c <users>
選項:
-w:警告值,登陸使用者大於這個值
-c:嚴重錯誤,大於這個數錯誤
[[email protected] libexec]# ./check_users -w 3 -c 5(使用者登陸數大於3,小於5警告,)
USERS OK - 2 users currently logged in |users=2;3;5;0
[[email protected] libexec]# ./check_users -w 1 -c 1(使用者登陸數大於1,錯誤)
USERS CRITICAL - 2 users currently logged in |users=2;1;1;0

監控磁碟:
[[email protected] libexec]# ./check_disk
格式:
check_disk -w limit -c limit [-W limit] [-K limit] {-p path | -x device}
[-C] [-E] [-e] [-g group ] [-k] [-l] [-M] [-m] [-R path ] [-r path ]
[-t timeout] [-u unit] [-v] [-X type]
選項:
-w:警告值,登陸使用者大於這個值
-c:嚴重錯誤,大於這個數錯誤
-p:指定磁碟
樣本:
[[email protected] libexec]# ./check_disk -w 10% -c 5% -p /tmp
DISK OK - free space: / 43784 MB (96% inode=98%);| /=1568MB;43007;45396;0;47786

監控進程:
[[email protected] libexec]# ./check_procs
格式:
check_procs -w <range> -c <range> [-m metric] [-s state] [-p ppid]
[-u user] [-r rss] [-z vsz] [-P %cpu] [-a argument-array]
[-C command] [-t timeout] [-v]
選項:
-w:警告值,登陸使用者大於這個值
-c:嚴重錯誤,大於這個數錯誤
--metric:要監控的進程名
樣本:
[[email protected] libexec]# ./check_procs -w 10 -c 20 --metric=CPU
CPU WARNING: 1 warn out of 101 processes

監控http:
[[email protected] libexec]# ./check_http
格式:
check_http -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]
選項:
-H:監控的主機
-I:監控的ip地址
-p:監控的連接埠
-u:監控的url地址
-w:警告值,登陸使用者大於這個值
-c:嚴重錯誤,大於這個數錯誤
執行個體:
[[email protected] libexec]# ./check_http -H 192.168.4.1 -p 21
拒絕串連(沒開21連接埠)
HTTP CRITICAL - Unable to open TCP socket


配置監控本機
[[email protected] ~]# cd /usr/local/nagios/etc/objects/
localhost.cfg(監控原生設定檔)
[[email protected] objects]# vim localhost.cfg
define host{
use linux-server(監控模板名)
host_name localhost(定義主機名稱)
alias localhost(主機的描述資訊,別名)
address 127.0.0.1(指定被監控機器的ip地址)
}
define service{
use local-service(監控資源使用的模板)
host_name localhost(監控的主機名稱)
service_description Swap Usage(監控的描述資訊)
check_commandcheck_local_swap!20!10(監控命令)
}

自訂監控資源 commands.cfg
[[email protected] ~]# cd /usr/local/nagios/etc/objects/
[[email protected] objects]# vim commands.cfg
define command{
command_name check_http(命令名)
command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$(設定使用的外掛程式)
}


執行個體:
自訂監控本機資源,監控原生boot分區,監控原生ftp服務的狀態,不監控交換分區
[[email protected] objects]# vim localhost.cfg
define service{
use local-service
host_name localhost
service_description ftp
check_command check_local_ftp
}

define service{
use local-service
host_name localhost
service_description boot
check_command check_local_boot
}
[[email protected] objects]# vim commands.cfg
define command{
command_name check_local_boot
command_line $USER1$/check_disk -w 60 -c 20 -p /boot
}

define command{
command_name check_local_ftp
command_line $USER1$/check_ftp -H localhost
}

[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg(驗證nagios配置格式)
[[email protected] objects]# /etc/init.d/nagios restart(重啟監控服務)
[[email protected] objects]# firefox http://192.168.4.1/nagios(訪問測試)

配置監控警示
[[email protected] objects]# vim contacts.cfg
email [email protected]
[email protected]
[[email protected] objects]# mail -u nagios(查看郵件)
Heirloom Mail version 12.4 7/29/08. Type ? for help.
"/var/mail/nagios": 1 message 1 new
>N 1 [email protected] Thu Sep 14 15:26 31/869 "** PROBLEM Service Ale"
& 1
Message 1:
From [email protected] Thu Sep 14 15:26:30 2017
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Thu, 14 Sep 2017 15:26:30 +0800
To: [email protected]
Subject: ** PROBLEM Service Alert: localhost/ftp is CRITICAL **
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=utf-8
From: [email protected]
Status: R

***** Nagios *****

執行個體:監控遠端(192.168.4.2)伺服器的ftp服務和http服務
[[email protected] etc]# vim nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/192.168.4.2.cfg(建立新的監控)
[[email protected] etc]# vim /usr/local/nagios/etc/objects/192.168.4.2.cfg(寫入配置)
define host {
use linux-server(監控模板)
host_name server2(被監控名主機,隨便寫)
alias this my server(被監控主機標識)
address 192.168.4.2(被監控主機ip地址)
}

define service {(監控的服務)
use local-service
host_name server2
service_description ftp(監控的服務名)
check_command check_2_ftp(監控命令)
}

define service {(監控的服務)
use local-service
host_name server2
service_description httpd(監控的服務名)
check_command check_2_httpd(監控命令)
}
[[email protected] etc]# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_2_ftp
command_line $USER1$/check_ftp -H 192.168.4.2
}

define command{
command_name check_2_httpd
command_line $USER1$/check_httpd -H 192.168.4.2
[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[[email protected] objects]# /etc/init.d/nagios restart


執行個體:配置監控遠端伺服器上的私人資料(登陸使用者數量 引導分區使用量 進程數量)

被監控端配置(zhuji2:192.168.4.2)
安裝監控外掛程式
[[email protected] ~]# tar -xf nagios-plugins-1.4.14.tar.gz
[[email protected] nagios-plugins-1.4.14]# ./configure
[[email protected] nagios-plugins-1.4.14]# make
[[email protected] nagios-plugins-1.4.14]# make install(預設就安裝在/usr/local/nagios目錄下)
[[email protected] ~]# /usr/local/nagios/libexec/check_users -w 3 -c 5(測試回合外掛程式)
USERS OK - 2 users currently logged in |users=2;3;5;0
[[email protected] ~]# /usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /boot
DISK OK - free space: /boot 415 MB (92% inode=99%);| /boot=35MB;380;428;0;476
運行nrpe服務
[[email protected] ~]# tar -xf nrpe-2.12.tar.gz
[[email protected] nrpe-2.12]# useradd nagios
[[email protected] nrpe-2.12]# groupadd nagcmd
[[email protected] nrpe-2.12]# usermod -G nagios nagios
[[email protected] nrpe-2.12]# yum -y install openssl-devel(安裝加密傳輸)
[[email protected] nrpe-2.12]# ./configure
[[email protected] nrpe-2.12]# make
[[email protected] nrpe-2.12]# make install
[[email protected] nrpe-2.12]# make install-plugin(安裝外掛程式,這外掛程式專門用來串連的)
[[email protected] nrpe-2.12]# make install-daemon
[[email protected] nrpe-2.12]# make install-daemon-config(此服務的設定檔)
[[email protected] nrpe-2.12]# make install-xinetd
[[email protected] nrpe-2.12]# yum -y install xinetd(這個服務需要xinetd叫醒nrpe)
[[email protected] nrpe-2.12]# vim /etc/xinetd.d/nrpe (nrpe的主設定檔)
only_from = 127.0.0.1 192.168.4.1(監控伺服器的ip地址)
[[email protected] nrpe-2.12]# vim /etc/services(添加連接埠)
nrpe 5666/tcp #nrpe
[[email protected] ~]# /etc/init.d/xinetd restart
修改nrp服務的設定檔nrpe.cfg,定義監控命令
[[email protected] ~]# vim /usr/local/nagios/etc/nrpe.cfg
command[命令名]=使用的外掛程式
command[check_2_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_2_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30, 25,20


監控端配置(zhuji1:192.168.4.1)
定義監控命令
[[email protected] nagios]# tar -xf nrpe-2.12.tar.gz
[[email protected] nagios]# yum -y install openssl-devel
[[email protected] nrpe-2.12]# ./configure
[[email protected] nrpe-2.12]# make all
[[email protected] nrpe-2.12]# make install-plugin(安裝外掛程式)
使用外掛程式串連被監控主機的nrpe服務並測試定義的監控命令
[[email protected] nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 192.168.4.2 -c check_2_users(調用zhuji2的監控命令)
USERS OK - 2 users currently logged in |users=2;5;10;0
[[email protected] nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 192.168.4.2 -c check_2_boot
DISK OK - free space: /boot 415 MB (92% inode=99%);| /boot=35MB;380;428;0;476
定義監控命令
[[email protected] ~]# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_2_users
command_line $USER1$/check_nrpe -H 192.168.4.2 -c check_2_users
}

define command{
command_name check_2_boot
command_line $USER1$/check_nrpe -H 192.168.4.2 -c check_2_boot
}

define command{
command_name check_2_process
command_line $USER1$/check_nrpe -H 192.168.4.2 -c check_2_procs
}

define command{
command_name check_2_zombieprocess
command_line $USER1$/check_nrpe -H 192.168.4.2 -c check_2_zombie_procs
}
[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[[email protected] objects]# /etc/init.d/nagios restart

linux 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.