How to change alert email to json format

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
gouravin
Posts: 8
Joined: Thu Feb 14, 2019 1:03 am

How to change alert email to json format

Post 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 :)
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: How to change alert email to json format

Post 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 ;)
Former Nagios employee
https://www.mcapra.com/
benjaminsmith
Posts: 5324
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: How to change alert email to json format

Post by benjaminsmith »

As always, thanks @mcapra!

Let us know if you have any other related questions.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
gouravin
Posts: 8
Joined: Thu Feb 14, 2019 1:03 am

Re: How to change alert email to json format

Post by gouravin »

Thanks for the help, will let you know if i will stuck somewhere.

Really appreciated your help
benjaminsmith
Posts: 5324
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: How to change alert email to json format

Post 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.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
gouravin
Posts: 8
Joined: Thu Feb 14, 2019 1:03 am

Re: How to change alert email to json format

Post by gouravin »

Hi Benjaminsmith,

Thanks for the help, really appreciate your help here.
We could close this thread.
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: How to change alert email to json format

Post by scottwilkerson »

gouravin wrote:Hi Benjaminsmith,

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

Closing
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
Locked