一、Nagios簡介
Nagios是一款開源的電腦系統和網絡監視工具,能有效監控Windows、Linux和Unix的主機狀態,交換機路由器等網絡設置,打印機等。在系統或服務狀態異常時發出郵件或短信報警第一時間通知網站運維人員,在狀態恢復後發出正常的郵件或短信通知。
Nagios原名爲NetSaint,由Ethan Galstad開發並維護至今。NAGIOS是一個縮寫形式: "Nagios Ain't Gonna Insist On Sainthood" Sainthood 翻譯爲聖徒,而"Agios"是"saint"的希臘表示方法。Nagios被開發在Linux下使用,但在Unix下也工作得非常好。
主要功能
網絡服務監控(SMTP、POP3、HTTP、NNTP、ICMP、SNMP、FTP、SSH)
主機資源監控(CPU load、disk usage、system logs),也包括Windows主機(使用NSClient++ plugin)
可以指定自己編寫的Plugin通過網絡收集數據來監控任何情況(溫度、警告……)
可以通過配置Nagios遠程執行插件遠程執行腳本
遠程監控支持SSH或SSL加通道方式進行監控
簡單的plugin設計允許用戶很容易的開發自己需要的檢查服務,支持很多開發語言(shell scripts、C++、Perl、ruby、Python、PHP、C#等)
包含很多圖形化數據Plugins(Nagiosgraph、Nagiosgrapher、PNP4Nagios等)
可並行服務檢查
能夠定義網絡主機的層次,允許逐級檢查,就是從父主機開始向下檢查
當服務或主機出現問題時發出通告,可通過email, pager, sms 或任意用戶自定義的plugin進行通知
能夠自定義事件處理機制重新激活出問題的服務或主機
自動日誌循環
支持冗餘監控
包括Web界面可以查看當前網絡狀態,通知,問題歷史,日誌文件等
二、Nagios工作原理
Nagios的功能是監控服務和主機,但是他自身並不包括這部分功能,所有的監控、檢測功能都是通過各種插件來完成的。
啓動Nagios後,它會週期性的自動調用插件去檢測服務器狀態,同時Nagios會維持一個隊列,所有插件返回來的狀態信息都進入隊列,Nagios每次都從隊首開始讀取信息,並進行處理後,把狀態結果通過web顯示出來。
Nagios提供了許多插件,利用這些插件可以方便的監控很多服務狀態。安裝完成後,在nagios主目錄下的/libexec裏放有nagios自帶的可以使用的所有插件,如,check_disk是檢查磁盤空間的插件,check_load是檢查CPU負載的,等等。每一個插件可以通過運行./check_xxx –h 來查看其使用方法和功能。
Nagios可以識別4種狀態返回信息,即 0(OK)表示狀態正常/綠色、1(WARNING)表示出現警告/***、2(CRITICAL)表示出現非常嚴重的錯誤/紅色、3(UNKNOWN)表示未知錯誤/深***。Nagios根據插件返回來的值,來判斷監控對象的狀態,並通過web顯示出來,以供管理員及時發現故障。
四種監控狀態
再說報警功能,如果監控系統發現問題不能報警那就沒有意義了,所以報警也是nagios很重要的功能之一。但是,同樣的,Nagios 自身也沒有報警部分的代碼,甚至沒有插件,而是交給用戶或者其他相關開源項目組去完成的。
Nagios 安裝,是指基本平臺,也就是Nagios軟件包的安裝。它是監控體系的框架,也是所有監控的基礎。
打開Nagios官方的文檔,會發現Nagios基本上沒有什麼依賴包,只要求系統是Linux或者其他Nagios支持的系統。不過如果你沒有安裝apache(http服務),那麼你就沒有那麼直觀的界面來查看監控信息了,所以apache姑且算是一個前提條件。關於apache的安裝,網上有很多,照着安裝就是了。安裝之後要檢查一下是否可以正常工作。
知道Nagios 是如何通過插件來管理服務器對象後,現在開始研究它是如何管理遠端服務器對象的。Nagios 系統提供了一個插件NRPE。Nagios 通過週期性的運行它來獲得遠端服務器的各種狀態信息。
Nagios 通過NRPE 來遠端管理服務
1. Nagios 執行安裝在它裏面的check_nrpe 插件,並告訴check_nrpe 去檢測哪些服務。
2. 通過SSL,check_nrpe 連接遠端機子上的NRPE daemon
3. NRPE 運行本地的各種插件去檢測本地的服務和狀態(check_disk,..etc)
4. 最後,NRPE 把檢測的結果傳給主機端的check_nrpe,check_nrpe 再把結果送到Nagios狀態隊列中。
5. Nagios 依次讀取隊列中的信息,再把結果顯示出來。
三、實驗環境
IP | OS | Software |
192.168.1.152 | CentOS 6.5 | Apache、Php、Nagios、nagios-plugins |
192.168.1.171 | CentOS 6.5 | nagios-plugins、nrpe |
Server 安裝了nagios軟件,對監控的數據做處理,並且提供web界面查看和管理。當然也可以對本機自身的信息進行監控。
Client 安裝了NRPE等客戶端,根據監控機的請求執行監控,然後將結果回傳給監控機。
nagios監控端安裝前操作
由於我的實驗環境用88端口訪問nagios網頁,因此進行防火牆設置
sed -i '10 i\-A INPUT -m state --state NEW-m tcp -p tcp --dport 88 -j ACCEPT' /etc/sysconfig/iptables
重啓防火牆:service iptables restart
關閉selinux設置
sed -i's/SELINUX=enforcing/#SELINUX=enforcing/' /etc/selinux/config
sed -i's/SELINUXTYPE=targeted/#SELINUXTYPE=targeted/' /etc/selinux/config
echo -e "SELINUX=disabled">> /etc/selinux/config
Nagios監控端安裝
1.使用yum 安裝安裝基礎支持套件
yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel
2.創建nagios用戶和用戶組
useradd -s /sbin/nologin nagios
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios
3.編譯安裝Nagios
wgethttp://jaist.dl.sourceforge.net/project/nagios/nagios-4.x/nagios-4.1.0/nagios-4.1.0rc1.tar.gz
tar -zxvf nagios-4.1.0rc1.tar.gz
cd nagios-4.1.0rc1
./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-commandmode
make install-config
chkconfig --add nagios
chkconfig --level 35 nagios on
4.驗證程序是否被正確安裝
切換目錄到安裝路徑(這裏是/usr/local/nagios),看是否存在etc、bin、sbin、share、var 這五個目錄,如果存在則可以表明程序被正確的安裝到系統了。Nagios 各個目錄用途說明如下:
bin | Nagios 可執行程序所在目錄 |
etc | Nagios 配置文件所在目錄 |
sbin | Nagios CGI 文件所在目錄,也就是執行外部命令所需文件所在的目錄 |
share | Nagios網頁文件所在的目錄 |
libexec | Nagios 外部插件所在目錄 |
var | Nagios 日誌文件、lock 等文件所在的目錄 |
var/archives | Nagios 日誌自動歸檔目錄 |
var/rw | 用來存放外部命令文件的目錄 |
5. 安裝Nagios 插件
wget http://nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz
tar zxvf nagios-plugins-2.0.3.tar.gz
cd nagios-plugins-2.0.3
./configure --prefix=/usr/local/nagios
make && make install
6.安裝與配置Apache和Php
Apache 和Php 不是安裝nagios 所必須的,但是nagios提供了web監控界面,通過web監控界面可以清晰的看到被監控主機、資源的運行狀態,因此,安裝一個web服務是很必要的。
需要注意的是,nagios在nagios3.1.x版本以後,配置web監控界面時需要php的支持。這裏我們下載的nagios版本爲nagios-4.1.0,因此在編譯安裝完成apache後,還需要編譯php模塊,這裏選取的php版本爲php5.4.30。
a. 安裝Apache
wgethttp://mirrors.cnnic.cn/apache//httpd/httpd-2.2.29.tar.gz
tarzxvf httpd-2.2.29.tar.gz
cdhttpd-2.2.29
./configure --prefix=/usr/local/apache2
make&& make install
配置apache
sed -i 's/User daemon/User nagios/' /usr/local/apache2/conf/httpd.conf #修改
sed -i 's/Group daemon/Group nagios/' /usr/local/apache2/conf/httpd.conf #修改
sed -i 's/DirectoryIndexindex.html/DirectoryIndex index.html index.php/' /usr/local/apache2/conf/httpd.conf #修改
sed -i '309a AddTypeapplication/x-httpd-php .php' /usr/local/apache2/conf/httpd.conf #增加
sed -i 's/Listen 80/Listen 88/' /usr/local/apache2/conf/httpd.conf #修改
sed -i '98a ServerName 192.168.1.152:88' /usr/local/apache2/conf/httpd.conf #修改
爲了安全起見,一般情況下要讓nagios 的web 監控頁面必須經過授權才能訪問,這需要增加驗證配置,即在httpd.conf 文件最後添加如下信息:
echo -e "
#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>
" >> /usr/local/apache2/conf/httpd.conf
創建apache目錄驗證文件,上面的配置中,指定了目錄驗證文件htpasswd,下面要創建這個文件:
#/usr/local/apache2/bin/htpasswd-c /usr/local/nagios/etc/htpasswd xxm(密碼123456)
這樣就在/usr/local/nagios/etc 目錄下創建了一個htpasswd 驗證文件,當通過http://192.168.1.152:88/nagios/ 訪問時就需要輸入用戶名和密碼了。
查看認證文件的內容
# cat /usr/local/nagios/etc/htpasswd
由於我使用的是64位系統,因此進行軟連接
ln -s /usr/local/lib/libltdl.so.3 /usr/lib64/libltdl.so.3
ln -s /usr/local/lib/libltdl.so.3 /usr/lib/libltdl.so.3
b. 安裝Php
tar zxvf php-5.4.10.tar.gz
cd php-5.4.10
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-pcre-regex --with-libmbfl --with-pdo-mysql=/usr/local/mysql/
make && make install
配置php
cp -r /usr/local/src/php-5.4.30/php.ini-production /usr/local/php/etc/php.ini
sed -i 's#; extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"#g' /usr/local/php/etc/php.ini
sed -i "s#;date.timezone =#date.timezone = PRC#g" /usr/local/php/etc/php.ini
sed -i "s#;session.save_path = \"/tmp\"#session.save_path = \"/tmp\"#g" /usr/local/php/etc/php.ini
sed -i "s#expose_php = On#expose_php = Off#g" /usr/local/php/etc/php.ini
echo -e "extension = memcache.so\nextension = redis.so\n[opcache]\nzend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/opcache.so\nopcache.memory_consumption=128\nopcache.interned_strings_buffer=8\nopcache.max_accelerated_files=4000\nopcache.revalidate_freq=60\nopcache.fast_shutdown=1\nopcache.enable_cli=1" >> /usr/local/php/etc/php.ini
cp -r /usr/local/php/etc/php.ini /etc/php.ini
cp -r /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sed -i "s#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g" /usr/local/php/etc/php-fpm.conf
cp -r /usr/local/src/php-5.4.30/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on
7.啓動服務和訪問驗證
/etc/rc.d/init.d/php-fpm restart
/usr/local/apache2/bin/apachectl restart
/etc/init.d/nagios restart
到這裏nagios 的安裝也就基本完成了,你可以通過web來訪問了。瀏覽器輸入
http://192.168.1.152:88/nagios/
配置Nagios
Nagios 主要用於監控一臺或者多臺本地主機及遠程的各種信息,包括本機資源及對外的服務等。默認的Nagios 配置沒有任何監控內容,僅是一些模板文件。若要讓Nagios 提供服務,就必須修改配置文件,增加要監控的主機和服務,下面將詳細介紹。
默認配置文件介紹
Nagios 安裝完畢後,默認的配置文件在/usr/local/nagios/etc目錄下。
每個文件或目錄含義如下表所示:
文件名或目錄名 | 用途 |
cgi.cfg | 控制CGI訪問的配置文件 |
nagios.cfg | Nagios 主配置文件 |
resource.cfg | 變量定義文件,又稱爲資源文件,在些文件中定義變量,以便由其他配置文件引用,如$USER1$ |
objects | objects 是一個目錄,在此目錄下有很多配置文件模板,用於定義Nagios 對象 |
objects/commands.cfg | 命令定義配置文件,其中定義的命令可以被其他配置文件引用 |
objects/contacts.cfg | 定義聯繫人和聯繫人組的配置文件 |
objects/localhost.cfg | 定義監控本地主機的配置文件 |
objects/printer.cfg | 定義監控打印機的一個配置文件模板,默認沒有啓用此文件 |
objects/switch.cfg | 定義監控路由器的一個配置文件模板,默認沒有啓用此文件 |
objects/templates.cfg | 定義主機和服務的一個模板配置文件,可以在其他配置文件中引用 |
objects/timeperiods.cfg | 定義Nagios 監控時間段的配置文件 |
objects/windows.cfg | 監控Windows 主機的一個配置文件模板,默認沒有啓用此文件 |
配置文件之間的關係
在nagios的配置過程中涉及到的幾個定義有:主機、主機組,服務、服務組,聯繫人、聯繫人組,監控時間,監控命令等,從這些定義可以看出,nagios各個配置文件之間是互爲關聯,彼此引用的。
成功配置出一臺nagios監控系統,必須要弄清楚每個配置文件之間依賴與被依賴的關係,最重要的有四點:
第一:定義監控哪些主機、主機組、服務和服務組;
第二:定義這個監控要用什麼命令實現;
第三:定義監控的時間段;
第四:定義主機或服務出現問題時要通知的聯繫人和聯繫人組。
配置Nagios
爲了能更清楚的說明問題,同時也爲了維護方便,建議將nagios各個定義對象創建獨立的配置文件:
創建hosts.cfg文件來定義主機和主機組
創建services.cfg文件來定義服務
用默認的contacts.cfg文件來定義聯繫人和聯繫人組
用默認的commands.cfg文件來定義命令
用默認的timeperiods.cfg來定義監控時間段
用默認的templates.cfg文件作爲資源引用文件
a. templates.cfg文件
nagios主要用於監控主機資源以及服務,在nagios配置中稱爲對象,爲了不必重複定義一些監控對象,Nagios引入了一個模板配置文件,將一些共性的屬性定義成模板,以便於多次引用。這就是templates.cfg的作用。
下面詳細介紹下templates.cfg文件中每個參數的含義:
define contact{
name generic-contact ; 聯繫人名稱
service_notification_period 24x7 ; 當服務出現異常時,發送通知的時間段,這個時間段"24x7"在timeperiods.cfg文件中定義
host_notification_period 24x7 ; 當主機出現異常時,發送通知的時間段,這個時間段"24x7"在timeperiods.cfg文件中定義
service_notification_options w,u,c,r ; 這個定義的是“通知可以被髮出的情況”。w即warn,表示警告狀態,u即unknown,表示不明狀態;
; c即criticle,表示緊急狀態,r即recover,表示恢復狀態;
; 也就是在服務出現警告狀態、未知狀態、緊急狀態和重新恢復狀態時都發送通知給使用者。
host_notification_options d,u,r ; 定義主機在什麼狀態下需要發送通知給使用者,d即down,表示宕機狀態;
; u即unreachable,表示不可到達狀態,r即recovery,表示重新恢復狀態。
service_notification_commands notify-service-by-email ; 服務故障時,發送通知的方式,可以是郵件和短信,這裏發送的方式是郵件;
; 其中“notify-service-by-email”在commands.cfg文件中定義。
host_notification_commands notify-host-by-email ; 主機故障時,發送通知的方式,可以是郵件和短信,這裏發送的方式是郵件;
; 其中“notify-host-by-email”在commands.cfg文件中定義。
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}
define host{
name generic-host ; 主機名稱,這裏的主機名,並不是直接對應到真正機器的主機名;
; 乃是對應到在主機配置文件裏所設定的主機名。
notifications_enabled 1 ; Host notifications are enabled
event_handler_enabled 1 ; Host event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; 其值可以爲0或1,其作用爲是否啓用Nagios的數據輸出功能;
; 如果將此項賦值爲1,那麼Nagios就會將收集的數據寫入某個文件中,以備提取。
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
notification_period 24x7 ; 指定“發送通知”的時間段,也就是可以在什麼時候發送通知給使用者。
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
define host{
name linux-server ; 主機名稱
use generic-host ; use表示引用,也就是將主機generic-host的所有屬性引用到linux-server中來;
; 在nagios配置中,很多情況下會用到引用。
check_period 24x7 ; 這裏的check_period告訴nagios檢查主機的時間段
check_interval 5 ; nagios對主機的檢查時間間隔,這裏是5分鐘。
retry_interval 1 ; 重試檢查時間間隔,單位是分鐘。
max_check_attempts 10 ; nagios對主機的最大檢查次數,也就是nagios在檢查發現某主機異常時,並不馬上判斷爲異常狀況;
; 而是多試幾次,因爲有可能只是一時網絡太擁擠,或是一些其他原因,讓主機受到了一點影響;
; 這裏的10就是最多試10次的意思。
check_command check-host-alive ; 指定檢查主機狀態的命令,其中“check-host-alive”在commands.cfg文件中定義。
notification_period 24x7 ; 主機故障時,發送通知的時間範圍,其中“workhours”在timeperiods.cfg中進行了定義;
; 下面會陸續講到。
notification_interval 10 ; 在主機出現異常後,故障一直沒有解決,nagios再次對使用者發出通知的時間。單位是分鐘;
; 如果你覺得,所有的事件只需要一次通知就夠了,可以把這裏的選項設爲0
notification_options d,u,r ; 定義主機在什麼狀態下可以發送通知給使用者,d即down,表示宕機狀態;
; u即unreachable,表示不可到達狀態;
; r即recovery,表示重新恢復狀態。
contact_groups admins ; 指定聯繫人組,這個“admins”在contacts.cfg文件中定義。
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
define host{
name windows-server ; The name of this host template
use generic-host ; Inherit default values from the generic-host template
check_period 24x7 ; By default, Windows servers are monitored round the clock
check_interval 5 ; Actively check the server every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each server 10 times (max)
check_command check-host-alive ; Default command to check if servers are "alive"
notification_period 24x7 ; Send notification out at any time - day or night
notification_interval 10 ; Resend notifications every 30 minutes
notification_options d,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
hostgroups windows-servers ; Host groups that Windows servers should be a member of
register 0 ; DONT REGISTER THIS - ITS JUST A TEMPLATE
}
define service{
name generic-service ; 定義一個服務名稱
active_checks_enabled 1 ; Active service checks are enabled
passive_checks_enabled 1 ; Passive service checks are enabled/accepted
parallelize_check 1 ; Active service checks should be parallelized;
; (disabling this can lead to major performance problems)
obsess_over_service 1 ; We should obsess over this service (if necessary)
check_freshness 0 ; Default is to NOT check service 'freshness'
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; Process performance data
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
is_volatile 0 ; The service is not volatile
check_period 24x7 ; 這裏的check_period告訴nagios檢查服務的時間段。
max_check_attempts 3 ; nagios對服務的最大檢查次數。
normal_check_interval 5 ; 此選項是用來設置服務檢查時間間隔,也就是說,nagios這一次檢查和下一次檢查之間所隔的時間;
; 這裏是5分鐘。
retry_check_interval 2 ; 重試檢查時間間隔,單位是分鐘。
contact_groups admins ; 指定聯繫人組
notification_options w,u,c,r ; 這個定義的是“通知可以被髮出的情況”。w即warn,表示警告狀態;
; u即unknown,表示不明狀態;
; c即criticle,表示緊急狀態,r即recover,表示恢復狀態;
; 也就是在服務出現警告狀態、未知狀態、緊急狀態和重新恢復後都發送通知給使用者。
notification_interval 10 ; Re-notify about service problems every hour
notification_period 24x7 ; 指定“發送通知”的時間段,也就是可以在什麼時候發送通知給使用者。
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
define service{
name local-service ; The name of this service template
use generic-service ; Inherit default values from the generic-service definition
max_check_attempts 4 ; Re-check the service up to 4 times in order to determine its final (hard) state
normal_check_interval 5 ; Check the service every 5 minutes under normal conditions
retry_check_interval 1 ; Re-check the service every minute until a hard state can be determined
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
################我這裏使用sed命令進行templates.cfg文件修改###############
sed -i '31s/w,u,c,r,f,s/w,u,c,r/'/usr/local/nagios/etc/objects/templates.cfg
sed -i '32s/d,u,r,f,s/d,u,r/' /usr/local/nagios/etc/objects/templates.cfg
sed -i '55a failure_prediction_enabled 1 ; Failure prediction is enabled'/usr/local/nagios/etc/objects/templates.cfg
sed -i '75s/workhours/24x7/' /usr/local/nagios/etc/objects/templates.cfg
sed -i '78s/120/10/' /usr/local/nagios/etc/objects/templates.cfg
sed -i '97s/30/10/' /usr/local/nagios/etc/objects/templates.cfg
sed -i '161afailure_prediction_enabled 1 ; Failure prediction isenabled' /usr/local/nagios/etc/objects/templates.cfg
sed -i '169s/10/5/'/usr/local/nagios/etc/objects/templates.cfg
sed -i '173s/60/10/' /usr/local/nagios/etc/objects/templates.cfg
#######################################################################################
b. resource.cfg文件
resource.cfg是nagios的變量定義文件,文件內容只有一行:
$USER1$=/usr/local/nagios/libexec
其中,變量$USER1$指定了安裝nagios插件的路徑,如果把插件安裝在了其它路徑,只需在這裏進行修改即可。需要注意的是,變量必須先定義,然後才能在其它配置文件中進行引用。
c. commands.cfg文件
此文件默認是存在的,無需修改即可使用,當然如果有新的命令需要加入時,在此文件進行添加即可。
#notify-host-by-email命令的定義
define command{
command_name notify-host-by-email #命令名稱,即定義了一個主機異常時發送郵件的命令。
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ #命令具體的執行方式。
}#notify-service-by-email命令的定義
define command{
command_name notify-service-by-email #命令名稱,即定義了一個服務異常時發送郵件的命令
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
#check-host-alive命令的定義
define command{
command_name check-host-alive #命令名稱,用來檢測主機狀態。
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
# 這裏的變量$USER1$在resource.cfg文件中進行定義,即$USER1$=/usr/local/nagios/libexec;
# 那麼check_ping的完整路徑爲/usr/local/nagios/libexec/check_ping;
# “-w 3000.0,80%”中“-w”說明後面的一對值對應的是“WARNING”狀態,“80%”是其臨界值。
# “-c 5000.0,100%”中“-c”說明後面的一對值對應的是“CRITICAL”,“100%”是其臨界值。
# “-p 1”說明每次探測發送一個包。
}
define command{
command_name check_local_disk
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$ #$ARG1$是指在調用這個命令的時候,命令後面的第一個參數。
}
define command{
command_name check_local_load
command_line $USER1$/check_load -w $ARG1$ -c $ARG2$
}
define command{
command_name check_local_procs
command_line $USER1$/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
}
define command{
command_name check_local_users
command_line $USER1$/check_users -w $ARG1$ -c $ARG2$
}
define command{
command_name check_local_swap
command_line $USER1$/check_swap -w $ARG1$ -c $ARG2$
}
define command{
command_name check_ftp
command_line $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$
}
define command{
command_name check_http
command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
}
define command{
command_name check_ssh
command_line $USER1$/check_ssh $ARG1$ $HOSTADDRESS$
}
define command{
command_name check_ping
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5
}
define command{
command_name check_nt
command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$
}
################我這裏使用echo命令進行commands.cfg文件最後一行添加###############
echo -e "
#'check_nrpe' command definition
define command{
command_name check_nrpe
command_line \$USER1\$/check_nrpe -H \$HOSTADDRESS\$ -c\$ARG1\$
}
" >> /usr/local/nagios/etc/objects/commands.cfg
-c 後面帶的$ARG1$ 參數是傳給nrpe daemon 執行的檢測命令,之前說過了它必須是nrpe.cfg 中所定義的那5條命令中的其中一條。在services.cfg 中使用check_nrpe 的時候要用 “!” 帶上這個參數。
################################################################################
d. hosts.cfg文件
此文件默認不存在,需要手動創建,hosts.cfg主要用來指定被監控的主機地址以及相關屬性信息,根據實驗目標配置如下:
define host{
use linux-server #引用主機linux-server的屬性信息,linux-server主機在templates.cfg文件中進行了定義。
host_name 171 #主機名
alias 171 #主機別名
address 192.168.1.171 #被監控的主機地址,這個地址可以是ip,也可以是域名。
}
#定義一個主機組
define hostgroup{
hostgroup_name bsmart-servers #主機組名稱,可以隨意指定。
alias bsmart servers #主機組別名
members 171 #主機組成員,其中“Nagios-Linux”就是上面定義的主機。
}
注意:在/usr/local/nagios/etc/objects 下默認有localhost.cfg 和windows.cfg 這兩個配置文件,localhost.cfg 文件是定義監控主機本身的,windows.cfg 文件是定義windows 主機的,其中包括了對host 和相關services 的定義。所以在本次實驗中,將直接在localhost.cfg 中定義監控主機(192.168.1.152),不使用windows.cfg中定義windows 主機。根據自己的需要修改其中的相關配置。
localhost.cfg詳細如下:
define host{
use linux-server ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name 152
alias 152
address 192.168.1.152
}
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members 152 ; Comma separated list of hosts that belong to this group
}
define service{
use local-service ; Name of service template to use
host_name 152
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service ; Name of service template to use
host_name 152
service_description Root Partition
check_command check_local_disk!20%!10%!/
}
define service{
use local-service ; Name of service template to use
host_name 152
service_description Current Users
check_command check_local_users!20!50
}
define service{
use local-service ; Name of service template to use
host_name 152
service_description Total Processes
check_command check_local_procs!250!400!RSZDT
}
define service{
use local-service ; Name of service template to use
host_name 152
service_description Current Load
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
define service{
use local-service ; Name of service template to use
host_name 152
service_description Swap Usage
check_command check_local_swap!20!10
}
define service{
use local-service ; Name of service template to use
host_name 152
service_description SSH
check_command check_ssh
notifications_enabled 0
}
define service{
use local-service ; Name of service template to use
host_name 152
service_description HTTP
check_command check_http
notifications_enabled 0
}
e. services.cfg文件
此文件默認也不存在,需要手動創建,services.cfg文件主要用於定義監控的服務和主機資源,例如監控http服務、ftp服務、主機磁盤空間、主機系統負載等等。192.168.1.171相關服務已在相應的配置文件中定義,所以這裏只需要定義192.168.1.171相關服務即可,這裏只定義一個檢測是否存活的服務來驗證配置文件的正確性,其他服務的定義將在後面講到。
define service{
use local-service #引用local-service服務的屬性值,local-service在templates.cfg文件中進行了定義。
host_name 171 #指定要監控哪個主機上的服務,“Nagios-Server”在hosts.cfg文件中進行了定義。
service_description check-host-alive #對監控服務內容的描述,以供維護人員參考。
check_command check-host-alive #指定檢查的命令。
}
################我這裏使用echo命令進行services.cfg文件添加###############
echo -e "
define service{
use local-service
host_name 171
service_description Current Load
check_command check_nrpe!check_load
}
define service{
use local-service
host_name 171
service_description Check Disk sda1
check_command check_nrpe!check_sda1
}
define service{
use local-service
host_name 171
service_description Total Processes
check_command check_nrpe!check_total_procs
}
define service{
use local-service
host_name 171
service_description Current Users
check_command check_nrpe!check_users
}
define service{
use local-service
host_name 171
service_description Check Zombie Procs
check_command check_nrpe!check_zombie_procs
}
define service{
use local-service
host_name 171
service_description Check Swap
check_command check_nrpe!check_swap
}
define service{
use local-service
host_name 171
service_description HTTP
check_command check_nrpe!check_http
}
" >> /usr/local/nagios/etc/objects/services.cfg
#############################################################
f. contacts.cfg文件
contacts.cfg是一個定義聯繫人和聯繫人組的配置文件,當監控的主機或者服務出現故障,nagios會通過指定的通知方式(郵件或者短信)將信息發給這裏指定的聯繫人或者使用者。
define contact{
contact_name xxm #聯繫人的名稱,這個地方不要有空格
use generic-contact #引用generic-contact的屬性信息,其中“generic-contact”在templates.cfg文件中進行定義
alias Nagios Admin
email [email protected]
}
define contactgroup{
contactgroup_name admins #聯繫人組的名稱,同樣不能空格
alias Technical Support #聯繫人組描述
members xxm #聯繫人組成員,其中“david”就是上面定義的聯繫人,如果有多個聯繫人則以逗號相隔
}
################我這裏使用sed命令進行contacts.cfg文件修改###############
sed -i'30s/nagiosadmin/xxm/' /usr/local/nagios/etc/objects/contacts.cfg
sed -i'34s/nagios@localhost/[email protected]/' /usr/local/nagios/etc/objects/contacts.cfg
sed -i'53s/nagiosadmin/xxm/' /usr/local/nagios/etc/objects/contacts.cfg
#######################################
g. timeperiods.cfg文件
此文件只要用於定義監控的時間段,不做任何修改
h. cgi.cfg文件
此文件用來控制相關cgi腳本,如果想在nagios的web監控界面執行cgi腳本,例如重啓nagios進程、關閉nagios通知、停止nagios主機檢測等,這時就需要配置cgi.cfg文件了。
由於nagios的web監控界面驗證用戶爲david,所以只需在cgi.cfg文件中添加此用戶的執行權限就可以了,需要修改的配置信息如下:
################我這裏使用sed命令進行cgi.cfg文件修改###############
sed -i'106s/#default_user_name=guest/default_user_name=xxm/'/usr/local/nagios/etc/cgi.cfg
sed -i'118s/authorized_for_system_information=nagiosadmin/authorized_for_system_information=nagiosadmin,xxm/'/usr/local/nagios/etc/cgi.cfg
sed -i'130s/authorized_for_configuration_information=nagiosadmin/authorized_for_configuration_information=nagiosadmin,xxm/'/usr/local/nagios/etc/cgi.cfg
sed -i'143s/authorized_for_system_commands=nagiosadmin/authorized_for_system_commands=xxm/'/usr/local/nagios/etc/cgi.cfg
sed -i'156s/authorized_for_all_services=nagiosadmin/authorized_for_all_services=nagiosadmin,xxm/'/usr/local/nagios/etc/cgi.cfg
sed -i'157s/authorized_for_all_hosts=nagiosadmin/authorized_for_all_hosts=nagiosadmin,xxm/'/usr/local/nagios/etc/cgi.cfg
sed -i'170s/authorized_for_all_service_commands=nagiosadmin/authorized_for_all_service_commands=nagiosadmin,xxm/'/usr/local/nagios/etc/cgi.cfg
sed -i'171s/authorized_for_all_host_commands=nagiosadmin/authorized_for_all_host_commands=nagiosadmin,xxm/'/usr/local/nagios/etc/cgi.cfg
##################################################
i. nagios.cfg文件
nagios.cfg默認的路徑爲/usr/local/nagios/etc/nagios.cfg,是nagios的核心配置文件,所有的對象配置文件都必須在這個文件中進行定義才能發揮其作用,這裏只需將對象配置文件在Nagios.cfg文件中進行引用即可。
log_file=/usr/local/nagios/var/nagios.log # 定義nagios日誌文件的路徑cfg_file=/usr/local/nagios/etc/objects/commands.cfg # “cfg_file”變量用來引用對象配置文件,如果有更多的對象配置文件,在這裏依次添加即可。
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg # 本機配置文件
cfg_file=/usr/local/nagios/etc/objects/windows.cfg # windows 主機配置文件
object_cache_file=/usr/local/nagios/var/objects.cache # 該變量用於指定一個“所有對象配置文件”的副本文件,或者叫對象緩衝文件precached_object_file=/usr/local/nagios/var/objects.precache
resource_file=/usr/local/nagios/etc/resource.cfg # 該變量用於指定nagios資源文件的路徑,可以在nagios.cfg中定義多個資源文件。status_file=/usr/local/nagios/var/status.dat # 該變量用於定義一個狀態文件,此文件用於保存nagios的當前狀態、註釋和宕機信息等。status_update_interval=10 # 該變量用於定義狀態文件(即status.dat)的更新時間間隔,單位是秒,最小更新間隔是1秒。nagios_user=nagios # 該變量指定了Nagios進程使用哪個用戶運行。
nagios_group=nagios # 該變量用於指定Nagios使用哪個用戶組運行。check_external_commands=1 # 該變量用於設置是否允許nagios在web監控界面運行cgi命令;
# 也就是是否允許nagios在web界面下執行重啓nagios、停止主機/服務檢查等操作;
# “1”爲運行,“0”爲不允許。command_check_interval=10s # 該變量用於設置nagios對外部命令檢測的時間間隔,如果指定了一個數字加一個"s"(如10s);
# 那麼外部檢測命令的間隔是這個數值以秒爲單位的時間間隔;
# 如果沒有用"s",那麼外部檢測命令的間隔是以這個數值的“時間單位”的時間間隔。
interval_length=60 # 該變量指定了nagios的時間單位,默認值是60秒,也就是1分鐘;
# 即在nagios配置中所有的時間單位都是分鐘。
################我這裏使用sed命令進行Nagios.cfg文件修改###############
sed -i '30acfg_file=/usr/local/nagios/etc/objects/hosts.cfg'/usr/local/nagios/etc/nagios.cfg
sed -i '31acfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg
sed -i '699acommand_check_interval=10s' /usr/local/nagios/etc/nagios.cfg
#################################################################
驗證Nagios 配置文件的正確性
Nagios 在驗證配置文件方面做的非常到位,只需通過一個命令即可完成:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios提供的這個驗證功能非常有用,在錯誤信息中通常會打印出錯誤的配置文件以及文件中的哪一行,這使得nagios的配置變得非常容易,報警信息通常是可以忽略的,因爲一般那些只是建議性的。
看到上面這些信息就說明沒問題了,然後啓動Nagios 服務。
Nagios的啓動與停止
啓動Nagios
a. 通過初始化腳本啓動nagios
# /etc/init.d/nagios start
# service nagios start
b. 手工方式啓動nagios
通過nagios命令的“-d”參數來啓動nagios守護進程:
# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
重啓Nagios
當修改了配置文件讓其生效時,需要重啓/重載Nagios服務。
a. 通過初始化腳本來重啓nagios
# /etc/init.d/nagios reload
# /etc/init.d/nagios restart
# service nagios restart
b. 通過web監控頁重啓nagios
可以通過web監控頁的 "Process Info" -> "Restart the Nagios process"來重啓nagios
c. 手工方式平滑重啓
# kill -HUP <nagios_pid>
停止Nagios
a. 通過初始化腳本關閉nagios服務
# /etc/init.d/nagios stop
or
# service nagios stop
b. 通過web監控頁停止nagios
可以通過web監控頁的 "Process Info" -> "Shutdown the Nagios process"來停止nagios
c. 手工方式停止Nagios
# kill <nagios_pid>
查看初步配置情況
啓動完成之後,登錄Nagios Web監控頁http://192.168.1.152:88nagios/ 查看相關信息。
點擊左面的Current Status -> Hosts 可以看到所定義的主機
利用NRPE監控遠程Linux上的“本地信息”
上面已經對遠程Linux 主機是否存活做了監控,而判斷遠程機器是否存活,我們可以使用ping 工具對其監測。還有一些遠程主機服務,例如ftp、ssh、http,都是對外開放的服務,即使不用Nagios,我們也可以試的出來,隨便找一臺機器看能不能訪問這些服務就行了。但是對於像磁盤容量,cpu負載這樣的“本地信息”,Nagios只能監測自己所在的主機,而對其他的機器則顯得有點無能爲力。畢竟沒得到被控主機的適當權限是不可能得到這些信息的。爲了解決這個問題,nagios有這樣一個附加組件--“NRPE”,用它就可以完成對Linux 類型主機"本地信息”的監控。
NRPE 工作原理
NRPE 總共由兩部分組成:
check_nrpe 插件,位於監控主機上
NRPE daemon,運行在遠程的Linux主機上(通常就是被監控機)
按照上圖,整個的監控過程如下:
當Nagios 需要監控某個遠程Linux 主機的服務或者資源情況時:
Nagios 會運行check_nrpe 這個插件,告訴它要檢查什麼;
check_nrpe 插件會連接到遠程的NRPE daemon,所用的方式是SSL;
NRPE daemon 會運行相應的Nagios 插件來執行檢查;
NRPE daemon 將檢查的結果返回給check_nrpe 插件,插件將其遞交給nagios做處理。
注意:NRPE daemon 需要Nagios 插件安裝在遠程的Linux主機上,否則,daemon不能做任何的監控。
nagios被監控端安裝
a. 增加用戶&設定密碼
# useradd nagios
# passwd nagios(密碼123456)
b. 安裝Nagios 插件和修改目錄權限
tar zxvf nagios-plugins-2.0.3.tar.gz
cd nagios-plugins-2.0.3
./configure --prefix=/usr/local/nagios
make && make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios/usr/local/nagios/libexec
c. 安裝NRPE
wget http://liquidtelecom.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz
tar -zxvf nrpe-2.15.tar.gz
cd nrpe-2.15
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
修改配置文件
sed -i'15s/127.0.0.1/127.0.0.1 192.168.1.152/' /etc/xinetd.d/nrpe
echo -e "nrpe 5666/tcp #nrpe" >> /etc/services
重啓xinted 服務
# service xinetd restart
檢查nrpe是否啓動
netstat -antlp | grep 5666可以看到5666端口已經在監聽了。就表示成功
e. 測試NRPE是否則正常工作
使用上面在被監控機上安裝的check_nrpe 這個插件測試NRPE 是否工作正常。
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 #測試nrpe,執行此命令會輸出nrpe版本號
也就是在本地用check_nrpe連接nrpe daemon是正常的。
注:爲了後面工作的順利進行,注意本地防火牆要打開5666能讓外部的監控機訪問。
sed -i '10 i\-A INPUT -m state --state NEW-m tcp -p tcp --dport 5666 -j ACCEPT' /etc/sysconfig/iptables
service iptables restart
selinux設置
sed -i's/SELINUX=enforcing/#SELINUX=enforcing/' /etc/selinux/config
sed -i's/SELINUXTYPE=targeted/#SELINUXTYPE=targeted/' /etc/selinux/config
echo -e "SELINUX=disabled">> /etc/selinux/config
nagios被監控端安裝
之前已經將Nagios運行起來了,現在要做的事情是:
安裝check_nrpe 插件;
在commands.cfg 中創建check_nrpe 的命令定義,因爲只有在commands.cfg 中定義過的命令才能在services.cfg 中使用;
創建對被監控主機的監控項目;
安裝check_nrpe 插件
tar -zxvf nrpe-2.15.tar.gz
cd nrpe-2.15
./configure
make all
make install-plugin
只運行這一步就行了,因爲只需要check_nrpe插件。
在Nagios-Linux 上我們已經裝好了nrpe,現在我們測試一下監控機使用check_nrpe 與被監控機運行的nrpe daemon之間的通信。
看到已經正確返回了NRPE的版本信息,說明一切正常。
commands.cfg文件上述已經配置,這裏不做任何修改。
定義services.cfg文件對192.168.1.171主機的監控(上述已經添加,這裏不做任何修改)
還有一個任務是要監控192.168.1.171 的swap 使用情況。但是在nrpe.cfg 中默認沒有定義這個監控功能的命令。怎麼辦?手動在nrpe.cfg 中添加,也就是自定義NRPE命令。現在我們要監控swap 分區,如果空閒空間小於20%則爲警告狀態 -> warning;如果小於10%則爲嚴重狀態 -> critical。我們可以查得需要使用check_swap插件。上我還開啓了http 服務,需要監控一下,按照上面的做法,增加check_http 命令的定義。
sed -i '223a command[check_swap]=/usr/local/nagios/libexec/check_swap -w20% -c 10%' /usr/local/nagios/etc/nrpe.cfg
sed -i '224a command[check_http]=/usr/local/nagios/libexec/check_http -I127.0.0.1' /usr/local/nagios/etc/nrpe.cfg
所有的配置文件已經修改好了,現在重啓Nagios。
# service nagios restart
查看配置情況
登錄Nagios Web監控頁http://192.168.1.152:88/nagios/ 查看相關信息。
可以看到,對於Nagios-Server 和Nagios-Linux 上的相關服務的監控已經成功了
全文完,,,,