Nagios Alert을 Telegram을 통해 받는 방법

Nagios는 시스템 및 네트워크 모니터링을 위한 오픈소스 도구입니다. Nagios에서는 알람 기능을 제공하여 특정 이벤트나 조건이 발생할 때 알림을 받을 수 있습니다. 이번에는 Nagios 알람을 Telegram을 통해 받는 방법을 설명해드리겠습니다.

Telegram bot 생성: 먼저, Telegram bot을 생성해야 합니다. Telegram bot을 생성하는 방법은 https://core.telegram.org/bots#3-how-do-i-create-a-bot 링크를 참고하시기 바랍니다.

Telegram bot token 확인: Telegram bot 생성 후, bot의 token을 확인합니다.

Telegram 그룹 ID 확인: 알람을 받을 Telegram 그룹의 ID를 확인합니다.

Telegram 알람 스크립트 다운로드: Nagios에서 Telegram 알람을 보내기 위한 스크립트를 다운로드합니다. 스크립트는 https://github.com/sbidy/nagios-telegram-bot 링크에서 다운로드할 수 있습니다.

스크립트 설정: 스크립트를 다운로드한 후, 파일을 열어 다음과 같이 설정합니다.

  • Telegram bot token 입력: TOKEN 변수에 Telegram bot의 token을 입력합니다.
TOKEN="<YOUR_TELEGRAM_BOT_TOKEN>"
  • Telegram 그룹 ID 입력: CHATID 변수에 알람을 받을 Telegram 그룹의 ID를 입력합니다.
CHATID="<YOUR_TELEGRAM_GROUP_ID>"
  1. Nagios 설정: Nagios에서 Telegram 알람을 받기 위해, 다음과 같은 설정을 해주어야 합니다.
  • commands.cfg 파일 수정
    아래와 같은 내용을 추가합니다.
define command {
        command_name    notify-host-by-telegram
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$HOSTOUTPUT$" | /usr/bin/telegram-send --stdin --config /etc/telegram-send.conf --format markdown --disable-web-page-preview --disable-notification --timeout 120 --chat_id $CONTACTPAGER$
}

define command {
        command_name    notify-service-by-telegram
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/bin/telegram-send --stdin --config /etc/telegram-send.conf --format markdown --disable-web-page-preview --disable-notification --timeout 120 --chat_id $CONTACTPAGER$
}
  • contacts.cfg 파일 수정
    아래와 같은 내용을 추가합니다.
define contact {
        contact_name                    telegram
        alias                           Telegram
        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-telegram
        host_notification_commands      notify-host-by-telegram
        pager                           <YOUR_TELEGRAM_BOT_TOKEN>
}
  1. Nagios 재시작: Nagios 설정 파일을 수정한 후, Nagios를 재시작합니다.

위의 방법으로 Nagios 알람을 Telegram을 통해 받을 수 있습니다. 자세한 내용은 다음 링크에서 확인할 수 있습니다.

https://www.sbidy.com/2018/01/nagios-telegram-notifications/