ubuntu下nagios配置

xwdreamer發表於2014-06-17

參考文獻:

http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html
http://my.oschina.net/duangr/blog/183160

宣告

本文是我參照上述兩篇參考文獻做nagios配置的一個記錄,當中的理論部分內容大多數拷貝自上述兩篇文章。如果想看詳細內容,可以參考上述兩篇文章。

一、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連線遠端伺服器在執行命令的麻煩)
  • 遠端監控支援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 來檢視其使用方法和功能。比如下述程式碼就是check_disk的使用方法。

enadmin@ubuntu-server:/usr/local/nagios/libexec$ ./check_disk -h
check_disk v2.0 (nagios-plugins 2.0)
Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
Copyright (c) 1999-2014 Nagios Plugin Development Team
        <devel@nagios-plugins.org>

This plugin checks the amount of used disk space on a mounted file system
and generates an alert if free space is less than one of the threshold values


Usage:
 check_disk -w limit -c limit [-W limit] [-K limit] {-p path | -x device}
[-C] [-E] [-e] [-f] [-g group ] [-k] [-l] [-M] [-m] [-R path ] [-r path ]
[-t timeout] [-u unit] [-v] [-X type] [-N type] [-n]

Options:
 -h, --help
    Print detailed help screen
 -V, --version
    Print version information
 --extra-opts=[section][@file]
    Read options from an ini file. See
    https://www.nagios-plugins.org/doc/extra-opts.html
    for usage and examples.
 -w, --warning=INTEGER
    Exit with WARNING status if less than INTEGER units of disk are free
 -w, --warning=PERCENT%
    Exit with WARNING status if less than PERCENT of disk space is free
 -c, --critical=INTEGER
    Exit with CRITICAL status if less than INTEGER units of disk are free
 -c, --critical=PERCENT%
    Exit with CRITICAL status if less than PERCENT of disk space is free
 -W, --iwarning=PERCENT%
    Exit with WARNING status if less than PERCENT of inode space is free
 -K, --icritical=PERCENT%
    Exit with CRITICAL status if less than PERCENT of inode space is free
 -p, --path=PATH, --partition=PARTITION
    Mount point or block device as emitted by the mount(8) command (may be repeated)
 -x, --exclude_device=PATH <STRING>
    Ignore device (only works if -p unspecified)
 -C, --clear
    Clear thresholds
 -E, --exact-match
    For paths or partitions specified with -p, only check for exact paths
 -e, --errors-only
    Display only devices/mountpoints with errors
 -f, --freespace-ignore-reserved
    Don't account root-reserved blocks into freespace in perfdata
 -g, --group=NAME
    Group paths. Thresholds apply to (free-)space of all partitions together
 -k, --kilobytes
    Same as '--units kB'
 -l, --local
    Only check local filesystems
 -L, --stat-remote-fs
    Only check local filesystems against thresholds. Yet call stat on remote filesystems
    to test if they are accessible (e.g. to detect Stale NFS Handles)
 -M, --mountpoint
    Display the mountpoint instead of the partition
 -m, --megabytes
    Same as '--units MB'
 -A, --all
    Explicitly select all paths. This is equivalent to -R '.*'
 -R, --eregi-path=PATH, --eregi-partition=PARTITION
    Case insensitive regular expression for path/partition (may be repeated)
 -r, --ereg-path=PATH, --ereg-partition=PARTITION
    Regular expression for path or partition (may be repeated)
 -I, --ignore-eregi-path=PATH, --ignore-eregi-partition=PARTITION
    Regular expression to ignore selected path/partition (case insensitive) (may be repeated)
 -i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION
    Regular expression to ignore selected path or partition (may be repeated)
 -t, --timeout=INTEGER
    Seconds before plugin times out (default: 10)
 -u, --units=STRING
    Choose bytes, kB, MB, GB, TB (default: MB)
 -v, --verbose
    Show details for command-line debugging (Nagios may truncate output)
 -X, --exclude-type=TYPE
    Ignore all filesystems of indicated type (may be repeated)
 -N, --include-type=TYPE
    Check only filesystems of indicated type (may be repeated)
 -n, --newlines
    Multi-line output of each disk's status information on a new line

Examples:
 check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /
    Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB
 check_disk -w 100 -c 50 -C -w 1000 -c 500 -g sidDATA -r '^/oracle/SID/data.*$'
    Checks all filesystems not matching -r at 100M and 50M. The fs matching the -r regex
    are grouped which means the freespace thresholds are applied to all disks together
 check_disk -w 100 -c 50 -C -w 1000 -c 500 -p /foo -C -w 5% -c 3% -p /bar
    Checks /foo for 1000M/500M and /bar for 5/3%. All remaining volumes use 100M/50M

Send email to help@nagios-plugins.org if you have questions regarding use
of this software. To submit patches or suggest improvements, send email to
devel@nagios-plugins.org
View Code

  Nagios可以識別4種狀態返回資訊,即 0(OK)表示狀態正常/綠色、1(WARNING)表示出現警告/黃色、2(CRITICAL)表示出現非常嚴重的錯誤/紅色、3(UNKNOWN)表示未知錯誤/深黃色。Nagios根據外掛返回來的值,來判斷監控物件的狀態,並通過web顯示出來,以供管理員及時發現故障。

四種監控狀態

2

  再說報警功能,如果監控系統發現問題不能報警那就沒有意義了,所以報警也是nagios很重要的功能之一。但是,同樣的,Nagios 自身也沒有報警部分的程式碼,甚至沒有外掛,而是交給使用者或者其他相關開源專案組去完成的。(我使用的是ubuntu下msmtp+mutt的安裝和配置這個方法)

  Nagios 安裝,是指基本平臺,也就是Nagios軟體包的安裝。它是監控體系的框架,也是所有監控的基礎。

  開啟Nagios官方的文件,會發現Nagios基本上沒有什麼依賴包,只要求系統是Linux或者其他Nagios支援的系統。不過如果你沒有安裝apache(http服務),那麼你就沒有那麼直觀的介面來檢視監控資訊了,所以apache姑且算是一個前提條件。關於apache的安裝,網上有很多,照著安裝就是了。安裝之後要檢查一下是否可以正常工作。

  知道Nagios 是如何通過外掛來管理伺服器物件後,現在開始研究它是如何管理遠端伺服器物件的。Nagios 系統提供了一個外掛NRPE。Nagios 通過週期性的執行它來獲得遠端伺服器的各種狀態資訊。它們之間的關係如下圖所示:

1

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 依次讀取佇列中的資訊,再把結果顯示出來。

nagios配置

1.建立使用者並設定密碼
建立使用者
# useradd -s /sbin/nologin nagios
修改nagios密碼
# sudo passwd nagios
密碼設定為nagios

2.建立目錄並設定許可權

# mkdir /usr/local/nagios
# ls -al --檢視目錄許可權
# chown -R nagios.nagios /usr/local/nagios
# ls -al --再次檢視目錄許可權
# mkdir /home/nagios
# chown -R nagios.nagios /home/nagios
# su nagios
# echo "test" |mutt -s "my_first_test" aaa@126.com --測試mutt傳送郵件

3.下載安裝

解壓縮
tar -zxf nagios-4.0.4.tar.gz

進入根目錄

cd nagios-4.0.4/

配置

enadmin@cgnmon:~/software/nagios-4.0.4$ ./configure --prefix=/usr/local/nagio

./configure --prefix 作用
不指定prefix,則可執行檔案預設放在/usr/local/bin,庫檔案預設放在/usr/local/lib,配置檔案預設放在/usr/local/etc。其它的資原始檔放在/usr/local/share。你要解除安裝這個程式,要麼在原來的make目錄下用一次make uninstall(前提是make檔案指定過uninstall),要麼去上述目錄裡面把相關的檔案一個個手工刪掉。
指定prefix,直接刪掉一個資料夾就夠了。

編譯安裝
make all
make install && make install-init && make install-commandmode && make install-config

4. 安裝Nagios 外掛

# wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.16.tar.gz
# tar zxvf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --prefix=/usr/local/nagios
# make && make install

6.使用chkconfig管理服務

ubuntu中使用chkconfig可能有bug,解決方法參考:
具體解決方法
sudo ln -s /usr/lib/insserv/insserv /sbin/insserv
root@ubuntu-server:/sbin# ll /sbin/insserv
lrwxrwxrwx 1 root root 24 Mar 28 09:21 /sbin/insserv -> /usr/lib/insserv/insserv*

7.常見故障處理

7.1Nagios 啟動時告警:

Starting nagios:No directory, logging in with HOME=/

done.

解決方法

修改/etc/passwd

將nagios:x:1001:1001::/home/nagios:/bin/sh

修改為:

nagios:x:1001:1001::/usr/local/nagios:/bin/sh

7.2apache重啟告警
enadmin@ubuntu-server:~$ sudo service apache2 restart
* Restarting web server apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
   ...done.
解決方法

修改 httpd.conf 檔案

開啟終端,輸入以下命令:

sudo vim /etc/apache2/httpd.conf

預設情況下,這個是一個空檔案,在檔案中加入以下內容:

ServerName localhost

儲存檔案退出,再次重啟apache,錯誤提示沒有了。

 

 

 

 

相關文章