Nagios
CentOS

CentOS7にyumでNAGIOSをインストール&設定例

net-snmpをインストール

これが無いと始まらないよ!

shell
$ yum install -y net-snmp*

デーモン登録

shell
$ systemctl enable snmpd.service
$ systemctl list-unit-files snmpd.service

コンフィグ設定

shell
$ vi /etc/snmp/snmpd.conf

### snmpd.conf ここから ########################################
#以下のサンプル設定をコメントアウト
# com2sec notConfigUser  default       public
# group   notConfigGroup v1           notConfigUser
# group   notConfigGroup v2c           notConfigUser
# view    systemview    included   .1.3.6.1.2.1.1
# view    systemview    included   .1.3.6.1.2.1.25.1.1
# access  notConfigGroup ""      any       noauth    exact  systemview none none

#以下を追加
# 必要に応じてサブネットのアドレスとユーザを追加してください
com2sec local     localhost          private

group MyRWGroup  v1        local
group MyRWGroup  v2c       local
group MyRWGroup  usm       local

#以下のコメントを外す
view all    included  .1   80

access MyROGroup ""      any       noauth    0      all    none   none
access MyRWGroup ""      any       noauth    0      all    all    all

disk / 10%

#以下のコメントを外す
load 12 14 14

#mysqlのレプリケーションチェック用のスクリプト
exec mysqlSlaveRunning /usr/local/snmp/check_slave_running.sh

### snmpd.conf ここまで ########################################

mysqlSlaveRunningのスクリプト設置

shell
$ mkdir -p /usr/local/snmp/
$ vi /usr/local/snmp/check_slave_running.sh

### スクリプト ここから ########################################
#!/bin/sh

mysql=/var/lib/mysql
user=root
password=***

running=`${mysql} -u${user} -p${password} -e "SHOW SLAVE STATUS\G" | grep "Slave.*Running"`
much=`expr index "$running" "No"`

if [ $much -eq 0 ];
then
        echo "Slave is Running!"
        exit 0
else
        echo "Slave is Not Running!" 1>&2
        exit 1
fi
exit 0
### スクリプト ここまで ########################################

$ chmod 755 /usr/local/snmp/check_slave_running.sh
$ sh /usr/local/snmp/check_slave_running.sh

起動確認

shell
$ systemctl start snmpd.service
$ snmpwalk -v 2c -c private 127.0.0.1

apacheをインストール

shell
$ yum install -y httpd httpd-devel

デーモン登録

shell
$ systemctl enable httpd.service
$ systemctl list-unit-files httpd.service

nagiosのインストール

shell
$ yum install -y nagios nagios-plugins-all

デーモン登録

shell
$ systemctl enable nagios.service
$ systemctl list-unit-files nagios.service

コンフィグ設定

contacts.cfg

nagiosadminのメールアドレスを設定

define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full name of user

        email                           xxx@xxx.xxx     ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        }

commands.cfg

ファイルの最後尾に以下の監視用コマンドを追加

### custom commands
define command{
        command_name    check_snmp_disk
        command_line    $USER1$/check_snmp -H $ARG1$ -P 2c -C private -o dskPercent.$ARG2$ -w 75 -c 90 -l 'Disk Usage %(SNMP)'
        }

define command{
        command_name    check_snmp_la
        command_line    $USER1$/check_snmp -H $ARG1$ -P 2c -C private -o laLoadInt.1 -w $ARG2$ -c $ARG3$ -l 'Load Average x100(SNMP)'
        }

define command{
        command_name    check_mysql
        command_line    $USER1$/check_mysql -u $ARG1$ -p $ARG2$ -H $ARG3$ -P 3306
        }

define command{
        command_name    check_snmp_mysqlrep                                                                                                             command_line    $USER1$/check_snmp -H $ARG1$ -P 2c -C private -o extOutput.$ARG2$ -s "Slave is Running!" -l 'MySQL Slave check(SNMP CUSTOM)'
        }

nagios.cfg

L/AやDISKなど監視項目別にファイルを分けると管理しやすいので以下のように変更

#cfg_file=/etc/nagios/objects/localhost.cfg
cfg_file=/etc/nagios/objects/host.cfg
cfg_file=/etc/nagios/objects/la.cfg
cfg_file=/etc/nagios/objects/http.cfg
cfg_file=/etc/nagios/objects/disk.cfg
cfg_file=/etc/nagios/objects/mysql.cfg

host.cfg

define hostgroup {
    hostgroup_name  localhost
    alias           hostgroup_localhost
}

define servicegroup {
    servicegroup_name       localhost
    alias                   servicegroup_localhost
}

define host {
    use                             linux-server         ; Inherit default values from a template
    host_name                       localhost            ; The name we're giving to this host
    alias                           localhost            ; A longer name associated with the host
    address                         127.0.0.1            ; IP address of the host
    hostgroups                      localhostgroup       ; Host groups this host is associated with
}

la.cfg

define service {
    use                             generic-service        ; Name of service template to use
    name                            snmp-la-service        ; The 'name' of this service template
    service_description             LA
    register                        0
}

define service {
    use                             snmp-la-service
    host_name                       localhost
    service_description             LA
    check_command                   check_snmp_la!127.0.0.1!100!200
    servicegroups                   localhost
}

http.cfg

define service {
    use                             generic-service      ; Name of service template to use
    name                            http-service         ; The 'name' of this service template
    service_description             HTTP
    register                        0
}

define service {
    use                             http-service
    host_name                       localhost
    check_command                   check_http!/!1!10
    servicegroups                   localhost
}

disk.cfg

define service {
    use                             generic-service        ; Name of service template to use
    name                            snmp-disk-service      ; The 'name' of this service template
    service_description             DISK
    register                        0
}

define service {
    use                             snmp-disk-service
    host_name                       localhost
    service_description             DISK1
    check_command                   check_snmp_disk!127.0.0.1!1
    servicegroups                   localhost
}

mysql.cfg

define service {
    use                             generic-service         ; Name of service template to use
    name                            mysql-service           ; The 'name' of this service template
    service_description             MYSQL
    register                        0
}

define service {
    use                             mysql-service
    host_name                       localhost
    check_command                   check_mysql!vagrant!vagrant!127.0.0.1
    servicegroups                   localhost
}

define service {
    use                             mysql-service
    host_name                       localhost
    check_command                   check_snmp_mysqlrep!127.0.0.1!1
    service_description             MYSQL_SLAVE_REP
    servicegroups                   localhost
}

起動確認

shell
$ systemctl start nagios.service

NAIOGSの管理画面で状態が確認できれば無事完了です♪

http://${HOST_ADDRESS}/nagios/