ubuntu下nagios配置


參考文獻:

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,錯誤提示沒有了。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM