Page 1 of 1

How to change alert email to json format

Posted: Fri Mar 22, 2019 6:42 am
by gouravin
Hi Team,

This is first my question on forum. Just started working on Nagios and We need to change format of alert email to json format. Currently it is coming as plain text, Can anyone please change text to json format as per below sample.

{
"nagios_alert": {
"server_name": "Nagios Master Production",
"app_name": " pepS",
"service_name": " sscp",
"host": " localhost",
"state": " WARNING",
"last_state": " WARNING",
"check_attempt": " 4",
"max_check_attempt": "4",
"state_since": " 0d 0h 7m 0s",
"is_downtime": " 0",
"service_last_ok": "16",
"service_check_output_short": " PROCS WARNING: 271 processes with STATE = RSZDT ",
"service_check_output_long": " ",
"notification_type": " PROBLEM",
"date_time": " Wed Feb 27 14:32:16 UTC 2019",
"additional_info": "PROCS WARNING: 271 processes with STATE = RSZDT"
}
}

Thanks in advance for the help :)

Re: How to change alert email to json format

Posted: Fri Mar 22, 2019 11:21 am
by mcapra
Nagios Core dispatches notifications via notification/event handlers, which reference a specific command to execute.

Here are the default ones:

Code: Select all

# 'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
        }

# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email
        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$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }
If you wanted to change the templating, you'd simply change what was fed into the /bin/mail command and add whatever macros you like:

Code: Select all

{
  "nagios_alert": {
    "additional_info": "$HOSTOUTPUT$",
    "app_name": "dunno what this is",
    "check_attempt": "$HOSTATTEMPT$",
    "date_time": "maybe use $(date +%s)?",
    "host": "$HOSTNAME$",
    "is_downtime": "$SERVICEDOWNTIME$",
    "last_state": "$LASTSERVICESTATE$",
    "max_check_attempt": "$MAXHOSTATTEMPTS$",
    "notification_type": "$NOTIFICATIONTYPE$",
    "server_name": "Nagios Master Production",
    "service_check_output_long": "$LONGSERVICEOUTPUT$",
    "service_check_output_short": "$SERVICEOUTPUT$",
    "service_last_ok": "$LASTSERVICEOK$",
    "service_name": "$SERVICEDISPLAYNAME$",
    "state": "$SERVICESTATE$",
    "state_since": "$LASTSERVICESTATECHANGE$"
  }
}
Then just feed that body into your /usr/bin/printf command. I wouldn't directly copy+paste that though because it has exactly zero JSON linting or CLI escaping ;)

Re: How to change alert email to json format

Posted: Fri Mar 22, 2019 1:59 pm
by benjaminsmith
As always, thanks @mcapra!

Let us know if you have any other related questions.

Re: How to change alert email to json format

Posted: Wed Mar 27, 2019 7:40 am
by gouravin
Thanks for the help, will let you know if i will stuck somewhere.

Really appreciated your help

Re: How to change alert email to json format

Posted: Wed Mar 27, 2019 9:08 am
by benjaminsmith
Hi @gouravin,
Thanks for the help, will let you know if i will stuck somewhere.
Really appreciated your help
Sounds good. We'll keep this topic open for now.

Re: How to change alert email to json format

Posted: Tue Apr 30, 2019 3:08 am
by gouravin
Hi Benjaminsmith,

Thanks for the help, really appreciate your help here.
We could close this thread.

Re: How to change alert email to json format

Posted: Tue Apr 30, 2019 6:47 am
by scottwilkerson
gouravin wrote:Hi Benjaminsmith,

Thanks for the help, really appreciate your help here.
We could close this thread.
Great!

Closing