Event handler read host "notifications_enabled" setting?

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
grinnell
Posts: 5
Joined: Sun May 01, 2016 11:32 am

Event handler read host "notifications_enabled" setting?

Post by grinnell »

Hello,

I've written a Nagios plugin that I currently have running as an event handler. It collects logs and emails them when a host changes states. The only remaining thing that I'd like to implement is the ability to mute it through the host notification enable/disable setting. I already have an option to not send the logs if the host is in scheduled down time. I used the $HOSTDOWNTIME$ macro for that. I don't see a macro or other method that would indicate whether notifications are enabled or not though. Any suggestions?

Instead of running it as an event handler I also experimented with running the plugin as a wrapper to the notification command. This would accomplish what I describe above in a different way. I couldn't figure out a way to send custom on demand host macros to the notification command though, a feature that I need for this plugin. For instance with the custom macro "$_FEXID$" I can get the value using the on demand macro "$_HOST_FEXID:HOSTNAME$", but hostname needs to be a static value, not another macro. I couldn't get it to resolve with the hostname as a sub macro. I did a bunch of experimenting with that and either got "$" or something that contained the hostname of the host that the Nagios server is running on instead of the host that the notification is for. The macro $HOSTNAME$ alone works just fine. Running the plugin as a host event handler had some complications since I am working around Nagios's built in structure for notifications. One problem was the ability to read the contacts for that host from within the scope of a host. I worked around that with another on demand macro. $CONTACTGROUPMEMBERS:receive_log_event_group$. Only in this instance "receive_log_event_group" is a static value instead of a variable. Using that I was able to subscibe contacts to the email list by adding them to the receive_log_event_group.

Two separate questions for the same basic problem. I'm open to other ideas for ways to accomplish what I am looking for as well.

Thanks,

Ethan Grinnell
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Event handler read host "notifications_enabled" setting?

Post by dwhitfield »

grinnell wrote: Instead of running it as an event handler I also experimented with running the plugin as a wrapper to the notification command.
This was going to be my thought when I started reading this.

Since you've already thought about that, I think the best thing to do is post an issue on github: https://github.com/NagiosEnterprises/na ... issues/new

Here's the relevant piece of the contributing doc:
First, let's define what an "Idea" really is. An Idea is simply an Enhancement request in its infancy. There's really nothing to it!

Something as simple as "I think that this project should somehow connect with a widget" is a valid Idea.
We can certainly have the developers look at the suggestions here, and we can file a github issue ourselves, but eventually it's going to end up on github, so if you want to follow it, it's best to have an account. It's probably better to just write it yourself rather than have us translate. I do understand that it's yet another account, so if you want to keep things here, that will certainly work.
grinnell
Posts: 5
Joined: Sun May 01, 2016 11:32 am

Re: Event handler read host "notifications_enabled" setting?

Post by grinnell »

Alrighty, thank you.

I take it you can't think of a better way to accomplish my goals? Of the many different things that I tried one was trying to force the macro expansion to follow a particular sequence:

"$_HOST_FEXID:($HOSTNAME$)$"

Is there perhaps a tricky way to get hostname to expand first, then use that for the on-demand macro?

Thanks again,

Ethan Grinnell
Ethan Grinnell
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Event handler read host "notifications_enabled" setting?

Post by mcapra »

You could use the JSON CGIs described here (ignore all the setup stuff and skip to "To get started"):
https://labs.nagios.com/2014/06/19/expl ... -7-part-1/

Lets say I have a host with the name esprod00. I could get the notifcations_enabled flag for this host by making a call to statusjson.cgi like so:

Code: Select all

http://nagiosadmin:password@10.35.6.233/nagios/cgi-bin/statusjson.cgi?query=host&hostname=esprod00
Which returns the following JSON object:

Code: Select all

[root@nagios ~]# curl -XGET "http://nagiosadmin:hunter2@10.35.6.233/nagios/cgi-bin/statusjson.cgi?query=host&hostname=esprod00"
{
  "format_version": 0,
  "result": {
    "query_time": 1503341357000,
    "cgi": "statusjson.cgi",
    "user": "nagiosadmin",
    "query": "host",
    "query_status": "released",
    "program_start": 1503341214000,
    "last_data_update": 1503341354000,
    "type_code": 0,
    "type_text": "Success",
    "message": ""
  },
  "data": {
    "host": {
      "name": "esprod00",
      "name": "esprod00",
      "plugin_output": "OK - Can connect to ElasticSearch on port 9200",
      "long_plugin_output": "",
      "perf_data": "",
      "status": 2,
      "last_update": 1503341353000,
      "has_been_checked": true,
      "should_be_scheduled": true,
      "current_attempt": 1,
      "max_attempts": 10,
      "last_check": 1503341140000,
      "next_check": 1503341441000,
      "check_options": 0,
      "check_type": 0,
      "last_state_change": 1503334833000,
      "last_hard_state_change": 1503334833000,
      "last_hard_state": 0,
      "last_time_up": 1503341141000,
      "last_time_down": 1503334833000,
      "last_time_unreachable": 0,
      "state_type": 1,
      "last_notification": 1503334833000,
      "next_notification": 0,
      "no_more_notifications": false,
      "notifications_enabled": false,
      "problem_has_been_acknowledged": false,
      "acknowledgement_type": 0,
      "current_notification_number": 0,
      "accept_passive_checks": true,
      "event_handler_enabled": true,
      "checks_enabled": true,
      "flap_detection_enabled": true,
      "is_flapping": false,
      "percent_state_change": 0.00,
      "latency": 0.00,
      "execution_time": 0.53,
      "scheduled_downtime_depth": 0,
      "process_performance_data": true,
      "obsess": true
    }
  }
}
Notice notifications_enabled field. It's a bit expensive, but short of a proper macro I can't think of a better way to do it. You could bake that into your event handler, I imagine.
Former Nagios employee
https://www.mcapra.com/
grinnell
Posts: 5
Joined: Sun May 01, 2016 11:32 am

Re: Event handler read host "notifications_enabled" setting?

Post by grinnell »

Ahh cool, that's something I can work with then. Expensive sure, but only used when a host changes states, which doesn't happen very often, thankfully. ;)

Thanks for the idea
Ethan Grinnell
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Event handler read host "notifications_enabled" setting?

Post by dwhitfield »

Thanks @mcapra!
grinnell wrote: Expensive sure
Might still be worth a feature request. This is definitely not the first time adding things on the fly has come up. The new context may provide the clue for moving forward on that.
grinnell
Posts: 5
Joined: Sun May 01, 2016 11:32 am

Re: Event handler read host "notifications_enabled" setting?

Post by grinnell »

I submitted two requests, one for each of my questions.

Thanks again,
Last edited by grinnell on Tue Aug 22, 2017 9:26 am, edited 1 time in total.
Ethan Grinnell
bolson

Re: Event handler read host "notifications_enabled" setting?

Post by bolson »

May we close this topic?
grinnell
Posts: 5
Joined: Sun May 01, 2016 11:32 am

Re: Event handler read host "notifications_enabled" setting?

Post by grinnell »

Yes, my questions have all been addressed.

Thank
Ethan Grinnell
Locked