Reading nagios host and service status from a bash script

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
hoffmp
Posts: 2
Joined: Mon Jan 23, 2017 4:00 pm

Reading nagios host and service status from a bash script

Post by hoffmp »

I'm currently running Nagios Core 4.2.1 and would like to write a bash script that can determine current host and service status information without having to re-run the underlying checks. (I need reasonably current, but not instantaneous information) Is there a way to do this that's more direct than simply parsing the status.dat file? Thanks very much in advance for any help you can offer
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Reading nagios host and service status from a bash scrip

Post by dwhitfield »

I realize the documentation is daunting, but I think NDOUtils is what you want: https://assets.nagios.com/downloads/nag ... OUtils.pdf

Please let us know if you get stuck at any particular point in the document or if this is definitely not what you want.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Reading nagios host and service status from a bash scrip

Post by mcapra »

If you don't mind JSON, the JSON Query Generator is probably good for this:

http://<your_nagios_host>/nagios/jsonquery.html

In action, to get the status of the HTTP service on localhost:

Code: Select all

[root@xi-stable scheduledbackups]# curl -XGET 'http://nagiosadmin:[email protected]/nagios/cgi-bin/statusjson.cgi?query=service&hostname=localhost&servicedescription=HTTP'
{
  "format_version": 0,
  "result": {
    "query_time": 1485210998000,
    "cgi": "statusjson.cgi",
    "user": "nagiosadmin",
    "query": "service",
    "query_status": "released",
    "program_start": 1484945781000,
    "last_data_update": 1485210990000,
    "type_code": 0,
    "type_text": "Success",
    "message": ""
  },
  "data": {
    "service": {
      "host_name": "localhost",
      "description": "HTTP",
      "host_name": "localhost",
      "description": "HTTP",
      "plugin_output": "HTTP OK: HTTP/1.1 200 OK - 3302 bytes in 0.001 second response time",
      "long_plugin_output": "",
      "perf_data": "time=0.000647s;;;0.000000 size=3302B;;;0",
      "max_attempts": 4,
      "current_attempt": 1,
      "status": 2,
      "last_update": 1485210990000,
      "has_been_checked": true,
      "should_be_scheduled": true,
      "last_check": 1485210723000,
      "check_options": 0,
      "check_type": 0,
      "checks_enabled": true,
      "last_state_change": 1483717750000,
      "last_hard_state_change": 1483717750000,
      "last_hard_state": 0,
      "last_time_ok": 1485210723000,
      "last_time_warning": 0,
      "last_time_unknown": 0,
      "last_time_critical": 0,
      "state_type": 1,
      "last_notification": 0,
      "next_notification": 0,
      "next_check": 1485211023000,
      "no_more_notifications": false,
      "notifications_enabled": true,
      "problem_has_been_acknowledged": false,
      "acknowledgement_type": 0,
      "current_notification_number": 0,
      "accept_passive_checks": true,
      "event_handler_enabled": true,
      "flap_detection_enabled": true,
      "is_flapping": false,
      "percent_state_change": 0.00,
      "latency": 0.00,
      "execution_time": 0.00,
      "scheduled_downtime_depth": 0,
      "process_performance_data": true,
      "obsess": true
    }
  }
}
Note that the "status" field follows slightly different conventions than regular old plugin output codes (0=OK, 1=WARNING, etc).
Former Nagios employee
https://www.mcapra.com/
[email protected]
Posts: 1
Joined: Tue Feb 10, 2015 7:35 am

Re: Reading nagios host and service status from a bash scrip

Post by [email protected] »

I've not written any JSON, but am certainly willing to give it a try. That's an excellent example and I think it'll get me through what I need for now. Thanks again for taking the time to spell it all out in such detail
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Reading nagios host and service status from a bash scrip

Post by dwhitfield »

Did you want us to leave this open or lock it up?
hoffmp
Posts: 2
Joined: Mon Jan 23, 2017 4:00 pm

Re: Reading nagios host and service status from a bash scrip

Post by hoffmp »

I think I'm good for now. Please go ahead and close the thread. Thanks again!
Locked