2016年9月20日 星期二

在 CentOS7/RHEL7 上安裝 Nagios (for Nginx)

設定提要:
  • 利用 Nagios 即時監控系統資源運作情形!
  • 利用 Nginx 作為 Web 平台
快速設定流程:
  1. 利用 yum 進行相關套件安裝:
    #yum -y httpd-tool gcc glibc glibc-common gd gd-devel net-snmp openssl-devel wget unzip spawn-fcgi perl-FCGI fcgi fcgi-devel
    
  2. 設定 Nagios 專用帳號與群組:
    #useradd nagios
    #passwd nagios
    #groupadd nagcmd
    #usermod -a -G nagcmd nagios
    #usermod -a -G nagcmd nginx
    
  3. 下載 Nagios 的最新版本:
    # cd /tmp
    #wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.1.tar.gz
    #wget https://nagios-plugins.org/download/nagios-plugins-2.1.3.tar.gz
    
  4. 解開 Nagios 壓縮檔:
    #tar zxvf nagios-4.2.1.tar.gz
    #tar zxvf nagios-plugins-2.1.3.tar.gz
    
  5. 編譯 Nagios 原始碼:
    #cd nagios-4.2.1
    #./configure --sysconfdir=/etc/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-user=nagios --with-command-group=nagcmd
    #make all
    #make install
    #make install-init
    #make install-commandmode
    
  6. 編譯 Nagios-plugins 原始碼:
    #cd ../nagios-plugins-2.1.3
    #./configure --with-nagios-user=nagios --with-nagios-group=nagios
    #make all
    #make install
    
  7. 修改 Nagios 相關設定檔:
    #vim /etc/nagios/nagios.cfg
    (修改下列設定:)
    log_file=/var/log/nagios/nagios.log
    
  8. 配合設定檔,新增目錄及檔案:
    #mkdir /var/log/nagios
    #touch /var/log/nagios/nagios.log
    #chown -R nagios:nagios /var/log/nagios/
    
  9. 新增帳密,鎖住 Nagios 網頁:
    #htpasswd -cb /etc/nagios/htpasswd.users nagiosadmin !AGoodPassword
    
  10. 設定 fcgiwrap:
    #cd /etc/yum.repos.d/
    #wget https://copr.fedorainfracloud.org/coprs/jorti/fcgiwrap/repo/epel-7/jorti-fcgiwrap-epel-7.repo
    #yum install fcgiwrap
    #vim /etc/init.d/fcgiwrap
    (編寫內容如下:)
    #!/usr/bin/perl
    
    use strict;
    use warnings FATAL => qw( all );
    
    use IO::Socket::UNIX;
    
    my $bin_path = '/usr/sbin/fcgiwrap';
    my $socket_path = $ARGV[0] || '/tmp/cgi.sock';
    my $num_children = $ARGV[1] || 1;
    
    close STDIN;
    
    unlink $socket_path;
    my $socket = IO::Socket::UNIX->new(
        Local => $socket_path,
        Listen => 100,
    );
    
    die "Cannot create socket at $socket_path: $!\n" unless $socket;
    
    for (1 .. $num_children) {
        my $pid = fork;
        die "Cannot fork: $!" unless defined $pid;
        next if $pid;
    
        exec $bin_path;
        die "Failed to exec $bin_path: $!\n";
    }
    (存檔後離開)
    # chmod +x /etc/init.d/fcgiwrap 
    #vim /etc/rc.local
    (加入下列該行指令:)
    sudo -u nginx /etc/init.d/fcgiwrap
    
  11. 設定 Nginx 內容:
    #cd /etc/yum.repos.d/
    
    
  12. 起動 Nagios :
    #
    #chmod +x /etc/init.d/nagios
    #chkconfig --add nagios
    #service nagios start
    
參考文獻:
  • http://www.phpini.com/linux/rhel-centos-7-install-nagios
  • https://assets.nagios.com/downloads/nagioscore/docs/Nagios-Core-Installing-On-Centos7.pdf
  • http://unix.rocks/2014/nginx-and-nagios-a-howto/
  • https://copr.fedorainfracloud.org/coprs/jorti/fcgiwrap/
  • https://blog.linuxeye.com/312.html
  • http://idevit.nl/node/93