Nagios監控系統

來源:互聯網
上載者:User

標籤:主機地址   top   mct   遠程   情況   window   組成   環境   RoCE   

Nagios工作原理

Nagios的功能起監控服務和主機。但是其自身井不包括這部分功能。 所有的監控、檢測功能都是通過各種外掛程式來完成的。啟動Nagios後,它會周期性地自動調用捶件去檢測伺服器狀態,同時Nagios會維持一個隊列, 所有外掛程式返回來的狀態資訊都進入隊列,Nagios每次都從隊首讀取資訊,進行處理後,再把狀態結果通過Web顯示出來。這就是所謂的被動模式,經常用於監控主機的系統資源,比如系統負載、磁碟使用率、記憶體使用量率、網路狀態、系統進程數等等。另一種是主動模式,主要是Nagios伺服器主動去擷取資料,常用於探測URL的監控和服務的狀態監控。相比於主動模式中伺服器主動去被監控機上輪詢擷取監控資料的方式,這樣做的一個很大優勢就是將除去資料處理的其他工作都放在了被監控機上面(包括資料的傳輸),就避免了被監控機數量大時,一次輪詢時間過長而導致監控反應延遲,這也是被動模式能承擔更大監控量的關鍵。Nagios提供了許多外掛程式,利用這些外掛程式可以方便地監控很多服務狀態。安裝完成後,在Nagios主日錄下的/libexec裡放有Nagios內建的可以使用的所有外掛程式,如check_disk是檢查磁碟空間的外掛程式,check_load是檢查CPU負載的外掛程式,等等。每一個外掛程式都可以通過運行./check_xxx -h命令來查看其使用方法和功能。

Nagios四種監控狀態

Nagios可以識別四種狀態返回資訊:0(OK)表示狀態正常/綠色。1(WARNING)表示出現警告/×××,2(CRITCAL)表示出現非常嚴重的錯誤/紅色,3(UNKNOWN)表示未知錯誤/深×××。Nagios根據外掛程式返回的值,來判斷監控對象的狀態,並通過Web顯示出來,以供管理員及時發現故障。

Nagios 通過nrpe外掛程式來遠端管理服務
  • Nagios執行安裝在它裡面的check_nrpe 外掛程式,並告訴check_nrpe去檢測哪些服務。
  • 通過SSL,check_nrpe串連遠端機器上的NRPE daemon。
  • NRPE運行本地的各種外掛程式去檢測本地的服務和狀態(check_disk,..etc)。
  • NRPE把檢測的結果傳給主機端的check_nrpe, check_nrpe 再把結果送到Nagios狀態隊列中。
  • Nagios依次讀取隊列中的資訊,再把結果顯示出來。
案例環境
監控主機:192.168.10.128被控主機:192.168.10.142
案例實施搭建Nagios監控系統1、關閉防火牆和SElinux
systemctl stop firewalld.servicesetenforce 0
2、安裝相關軟體包
yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel
3、建立nagios使用者和使用者組
useradd -s /sbin/nologin nagios
4、建立工作目錄,設定屬主屬組
mkdir /usr/local/nagioschown -R nagios.nagios /usr/local/nagiosll -d /usr/local/nagios/      //查看許可權
5、編譯安裝Nagios
tar -zxvf nagios-4.0.1.tar.gz -C /optcd /opt/nagios-4.0.1./configure --prefix=/usr/local/nagios       //指定安裝目錄make allmake installmake install-initmake install-commandmodemake install-configchkconfig --add /etc/rc.d/init.d/nagioschkconfig --level 35 nagios onchkconfig --list nagioscd /usr/local/nagios        //檢查是否存在etc、bin、sbin、share、var 這五個目錄
6、安裝Nagios外掛程式

Nagios提供的各種監控功能基本上都是通過外掛程式來完成的

tar xvzf nagios-plugins-1.5.tar.gz -C /optcd /opt/nagios-plugins-1.5./configure --prefix=/usr/local/nagios       //指定安裝目錄make && make install
7、安裝LAMP

建議手工編譯安裝LAMP,這裡我為節省時間用yum安裝

yum install -y httpd* mysql* php
8、修改httpd設定檔
vim /etc/httpd/conf/httpd.conf 找到:User apache Group apache 修改為 User nagios Group nagios 然後找到 <IfModule dir_module>   DirectoryIndex index.html </IfModule> 修改為 <IfModule dir_module>   DirectoryIndex index.html index.php </IfModule> 接著增加如下內容: AddType application/x-httpd-php .php 為了安全起見,一般情況下要讓nagios的web監控頁面必須經過授權才能訪問,這需要增加驗證配置,即在httpd.conf 檔案最後添加如下資訊:#setting for nagios ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin" <Directory "/usr/local/nagios/sbin">      AuthType Basic      Options ExecCGI      AllowOverride None      Order allow,deny      Allow from all      AuthName "Nagios Access"      AuthUserFile /usr/local/nagios/etc/htpasswd                   Require valid-user </Directory> Alias /nagios "/usr/local/nagios/share" <Directory "/usr/local/nagios/share">      AuthType Basic      Options None      AllowOverride None      Order allow,deny      Allow from all      AuthName "nagios Access"      AuthUserFile /usr/local/nagios/etc/htpasswd      Require valid-user </Directory> 
9、建立訪問認證檔案
htpasswd -c /usr/local/nagios/etc/htpasswd jack#使用者名稱:jack          密碼:123123     (自行定義)cat /usr/local/nagios/etc/htpasswd   //查看檔案資訊,密碼為加密資訊
10、啟動httpd服務
systemctl start httpd.service
配置Nagios監控系統

Nagios安裝完畢後,預設的設定檔在/usr/local/nagios/etc/目錄下

設定檔介紹及修改
cd /usr/local/nagios/etc/


1.objects/templates.cfg
主要用於監控主機資源以及服務,在nagios配置中稱為對象,為了不必重複定義一些監控對象,Nagios引入了一個模板設定檔,將一些共性的屬性定義成模板,以便於多次引用。vim objects/templates.cfgcontact_groups  聯絡組屬性改成 ts,ts將在後面的contacts.cfg檔案中定義
2.resource.cfg檔案
resource.cfg是nagios的變數定義檔案 一般無需更改$USER1$=/usr/local/nagios/libexec
3.objects/commands.cfg檔案
此檔案預設是存在的,無需修改即可使用,當然如果有新的命令需要加入時,在此檔案進行添加即可
4. objects/hosts.cfg檔案
此檔案預設不存在,需要手動建立,hosts.cfg主要用來指定被監控的主機地址以及相關屬性資訊(不能有任何空格)vim objects/hosts.cfgdefine host{           use                     linux-server       #引用主機linux-server的屬性資訊,linux-server主機在templates.cfg檔案中進行了定義。        host_name               Nagios-Linux     #主機名稱        alias                   Nagios-Linux     #主機別名        address                 192.168.10.142     #被監控的主機地址,這個地址可以是ip,也可以是網域名稱。        }   #定義一個主機群組   define hostgroup{              hostgroup_name          bsmart-servers     #主機群組名稱,可以隨意指定。        alias                   bsmart servers     #主機群組別名        members                 Nagios-Linux    #主機群組成員,其中“Nagios-Linux”就是上面定義的主機。             }

5.objects/localhost.cfg檔案

用來監控本機 一般無需更改

6.objects/windows.cfg檔案

用來監控windows 這裡無需使用

7.objects/services.cfg檔案

此檔案預設也不存在,需要手動建立,主要用於定義監控的服務和主機資源

define service{          use                     local-service      #引用local-service服務的屬性值,local-service在templates.cfg檔案中進行了定義。        host_name               Nagios-Linux      #指定要監控哪個主機上的服務,“Nagios-Server”在hosts.cfg檔案中進行了定義。        service_description     check-host-alive      #對監控服務內容的描述,以供維護人員參考。        check_command           check-host-alive      #指定檢查的命令。        }  

8.objects/contacts.cfg檔案

contacts.cfg是一個定義連絡人和連絡人群組的設定檔(不能有空格)

define contact{        contact_name                    jack     #連絡人的名稱,這個地方不要有空格        use                             generic-contact     #引用generic-contact的屬性資訊,其中“generic-contact”在templates.cfg檔案中進行定義        alias                           Nagios Admin        email                           [email protected]        }define contactgroup{        contactgroup_name       ts     #連絡人群組的名稱,同樣不能空格        alias                   Technical Support     #連絡人群組描述        members                 jack    #連絡人群組成員,其中“jack”就是上面定義的連絡人,如果有多個連絡人則以逗號相隔        }

9.cgi.cfg檔案

此檔案用來控制相關cgi指令碼,由於nagios的web監控介面驗證使用者為jack,所以只需在cgi.cfg檔案中添加此使用者的執行許可權,在最後加入

default_user_name=jackauthorized_for_system_information=nagiosadmin,jack  authorized_for_configuration_information=nagiosadmin,jack  authorized_for_system_commands=jackauthorized_for_all_services=nagiosadmin,jack  authorized_for_all_hosts=nagiosadmin,jackauthorized_for_all_service_commands=nagiosadmin,jack  authorized_for_all_host_commands=nagiosadmin,jack

10. nagios.cfg檔案

Nagios主設定檔,將對象設定檔在Nagios.cfg檔案中進行引用

cfg_file=/usr/local/nagios/etc/objects/hosts.cfgcfg_file=/usr/local/nagios/etc/objects/services.cfg
command_check_interval=10s       //該變數用於設定nagios對外部命令檢測的時間間隔

11.檢查設定檔
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg         //驗證設定檔

登入Nagios
systemctl restart httpd.service瀏覽器輸入http://192.168.10.128/nagios/

使用開始設定好的jack使用者及123123密碼進行登入

登入進來的頁面

查看被監控的主機,本機及Nagios-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.