How to change alert email to json format

Engage with the community of users including those using the open source solutions.
Includes Nagios Core, Plugins, and NCPA

How to change alert email to json format

Postby gouravin » Fri Mar 22, 2019 6:42 am

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 :)
gouravin
 
Posts: 8
Joined: Thu Feb 14, 2019 1:03 am

Re: How to change alert email to json format

Postby mcapra » Fri Mar 22, 2019 11:21 am

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
http://www.mcapra.com/
User avatar
mcapra
 
Posts: 3600
Joined: Thu May 05, 2016 3:54 pm

Re: How to change alert email to json format

Postby benjaminsmith » Fri Mar 22, 2019 1:59 pm

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!
User avatar
benjaminsmith
 
Posts: 2336
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: How to change alert email to json format

Postby gouravin » Wed Mar 27, 2019 7:40 am

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

Really appreciated your help
gouravin
 
Posts: 8
Joined: Thu Feb 14, 2019 1:03 am

Re: How to change alert email to json format

Postby benjaminsmith » Wed Mar 27, 2019 9:08 am

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!
User avatar
benjaminsmith
 
Posts: 2336
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: How to change alert email to json format

Postby gouravin » Tue Apr 30, 2019 3:08 am

Hi Benjaminsmith,

Thanks for the help, really appreciate your help here.
We could close this thread.
gouravin
 
Posts: 8
Joined: Thu Feb 14, 2019 1:03 am

Re: How to change alert email to json format

Postby scottwilkerson » Tue Apr 30, 2019 6:47 am

gouravin wrote:Hi Benjaminsmith,

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

Great!

Closing
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
User avatar
scottwilkerson
DevOps Engineer
 
Posts: 17961
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises


Return to Community Support

Who is online

Users browsing this forum: No registered users and 15 guests