본문 바로가기
Programming/Performance

Performance, Nagios Email Notification Setting

by ★용호★ 2014. 11. 3.

외부 SMTP를 이용하기 위해서 SendMail 설치

$ sudo apt-get install sendemail


/etc/nagios3/conf.d/contacts_nagios2.cfg 파일 수정

  • 옵션 설정과 email 설정

############################################################################### ############################################################################### # # CONTACTS # ############################################################################### ############################################################################### # In this simple config file, a single contact will receive all alerts. define contact{ contact_name                    nagiosadmin alias                             NagiosAdmin service_notification_period                24x7 host_notification_period                24x7 service_notification_options              w,u,c,r host_notification_options               d,r service_notification_commands        notify-service-by-email host_notification_commands         notify-host-by-email email                         yongho1037@gmail.com } ############################################################################### ############################################################################### # # CONTACT GROUPS # ############################################################################### ############################################################################### # We only have one contact in this simple configuration file, so there is # no need to create more than one contact group. define contactgroup{ contactgroup_name admins alias              Nagios Administrators members         nagiosadmin }


  • contact 설정을 타입별로 따로 설정 후 contactgroup으로 묶어서 용도 별로 사용할 수도 있다.



/etc/nagios3/conf.d/generic_service_nagios2.cfg와 /etc/nagios3/conf.d/generic_host_nagios2.cfg 수정


# generic service template definition define service{ name                 generic-service ; The 'name' of this service template 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 notification_interval     60 ; Only send notifications on status change by default. is_volatile          0 check_period     24x7 normal_check_interval 5 retry_check_interval 1 max_check_attempts 4 notification_period 24x7 notification_options w,u,c,r,f,s contact_groups admins register                  0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE! }

  • 노란색으로 표시한 옵션들이 중요하다고 생각하는 부분이다.
  • contact_groups가 앞서 설정한 contact 그룹과 일치 해야 설정한 이메일로 메일이 오게 되고, notification_options 값에 따라 메일을 보내는 타이밍이 결정된다.  그리고 notifications_enabled 값이 1일 경우 이메일 알림이 활성화, 0일 경우에는 비활성화이다.
  • host 설정도 service와 거의 비슷하므로 pass



/etc/nagios3/commands.cfg

  • "printf "메일본문" | sendemail -s SMTP주소 -xu SMTP계정 -xp SMTP패스워드 -l 로그가 저장 될 경로 -f 송신자 -t 수신자 -u 메일제목"의 형식


define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" \
         "***** Nagios *****\n\n"\
        "Notification Type: $NOTIFICATIONTYPE$\n" \
        "Host: $HOSTNAME$\n" \
        "State: $HOSTSTATE$\n" \
        "Address: $HOSTADDRESS$\n" \
        "Info: $HOSTOUTPUT$\n\n" \
        "Date/Time: $LONGDATETIME$\n" \
        | /usr/bin/sendemail -s $USER5$ -xu $USER7$ -xp $USER8$ \
        -l /var/log/sendemail \
        -f $USER6$ \
        -t $CONTACTEMAIL$ \
        -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
        }

# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" \
        "***** Nagios *****\n\n" \
        "Notification Type: $NOTIFICATIONTYPE$\n\n" \
        "Service: $SERVICEDESC$\n" \
        "Host: $HOSTALIAS$\n" \
        "Address: $HOSTADDRESS$\n" \
        "State: $SERVICESTATE$\n\n" \
        "Date/Time: $LONGDATETIME$\n\n" \
        "Additional Info:\n\n" \
        "$SERVICEOUTPUT$\n" \
        | /usr/bin/sendemail -s $USER5$ -xu $USER7$ -xp $USER8$ \
        -l /var/log/sendemail \
        -f $USER6$ \
        -t $CONTACTEMAIL$ \
        -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **"
        }


/etc/nagios3/resource.cfg


  • 위의 commands.cfg에서 사용 할 $USERn$ 형식의 매크로 값을 지정한다. 
  • $USER1$ ~ $USER32$ 까지 예약어이므로 임의의 다른 형태를 사용할 수는 없다.


# Mail Setting $USER5$=smtp.gmail.com $USER6$=yongho1037@gmail.com $USER7$=yongho1037@gmail.com $USER8$=password


Nagios 재시작

$ sudo service nagios3 restart


메일 확인




댓글