While familiarizing myself with v2 of the API I noticed that there is an issue when a single item is returned for the host & service class of the status endpoint.
Here's an example python using a limited query to isolate localhost and return only one result.
Code: Select all
#VARS FOR API
myxi = "192.168.1.1"
myep = "status"
mycls = "host"
qry = "&name=localhost"
mytkn = "auth_token" #provided by nagiosxi api
#NAGIOSXI API V2
def api2(myxi,myep,mycls,qry,mytkn):
try:
myurl = "https://{}/nagiosxi/api/v2/{}/{}?{}&token={}".format(myxi,myep,mycls,qry,mytkn)
#GET RESULTS
response = requests.get(myurl, verify=False)
r = response.json()
except Exception as e:
r = e
finally:
return r
Code: Select all
HOSTCOUNT=1
<class 'str'>
@attributes2
<class 'str'>
instance_id
<class 'str'>
host_id
<class 'str'>
name
<class 'str'>
display_name
<class 'str'>
address
<class 'str'>
alias
<class 'str'>
status_update_time
<class 'str'>
status_text
<class 'str'>
status_text_long
<class 'str'>
current_state
<class 'str'>
icon_image
<class 'str'>
icon_image_alt
<class 'str'>
performance_data
<class 'str'>
should_be_scheduled
<class 'str'>
check_type
<class 'str'>
last_state_change
<class 'str'>
last_hard_state_change
<class 'str'>
last_hard_state
<class 'str'>
last_time_up
<class 'str'>
last_time_down
<class 'str'>
last_time_unreachable
<class 'str'>
last_notification
<class 'str'>
next_notification
<class 'str'>
no_more_notifications
<class 'str'>
acknowledgement_type
<class 'str'>
current_notification_number
<class 'str'>
event_handler_enabled
<class 'str'>
process_performance_data
<class 'str'>
obsess_over_host
<class 'str'>
modified_host_attributes
<class 'str'>
event_handler
<class 'str'>
check_command
<class 'str'>
normal_check_interval
<class 'str'>
retry_check_interval
<class 'str'>
check_timeperiod_id
<class 'str'>
has_been_checked
<class 'str'>
current_check_attempt
<class 'str'>
max_check_attempts
<class 'str'>
last_check
<class 'str'>
next_check
<class 'str'>
state_type
<class 'str'>
notifications_enabled
<class 'str'>
problem_acknowledged
<class 'str'>
passive_checks_enabled
<class 'str'>
active_checks_enabled
<class 'str'>
flap_detection_enabled
<class 'str'>
is_flapping
<class 'str'>
percent_state_change
<class 'str'>
latency
<class 'str'>
execution_time
<class 'str'>
scheduled_downtime_depth
<class 'str'>
notes
<class 'str'>
notes_url
<class 'str'>
action_url
Now if the query is formatted to return 2 or more hosts/services, we get something that looks more usable.
Using the same example from above but, passing in a query to isolate two hosts.
Code: Select all
qry = "&name=in:localhost,u2204ncpa"
Code: Select all
HOSTCOUNT=2
<class 'dict'>
{'@attributes': {'id': '3'}, 'instance_id': '1', 'host_id': '205', 'name': 'u2204ncpa', 'display_name': 'u2204ncpa', 'address': '192.168.1.136', 'alias': 'linux-columbia-mo', 'status_update_time': '2024-09-26 16:30:36', 'status_text': 'Server is Up', 'status_text_long': {}, 'current_state': '0', 'icon_image': 'ubuntu.png', 'icon_image_alt': {}, 'performance_data': {}, 'should_be_scheduled': '1', 'check_type': '1', 'last_state_change': '2024-09-25 18:56:48', 'last_hard_state_change': '2024-09-25 18:56:48', 'last_hard_state': '0', 'last_time_up': '2024-09-23 19:03:50', 'last_time_down': '2024-09-25 18:56:48', 'last_time_unreachable': '1970-01-01 00:00:00', 'last_notification': '2024-09-25 18:56:48', 'next_notification': '1970-01-01 00:00:00', 'no_more_notifications': '0', 'acknowledgement_type': '0', 'current_notification_number': '0', 'event_handler_enabled': '1', 'process_performance_data': '1', 'obsess_over_host': '1', 'modified_host_attributes': '1', 'event_handler': {}, 'check_command': {}, 'normal_check_interval': '5', 'retry_check_interval': '3', 'check_timeperiod_id': '173', 'has_been_checked': '1', 'current_check_attempt': '1', 'max_check_attempts': '2', 'last_check': '2024-09-25 18:56:48', 'next_check': '2024-09-26 16:35:36', 'state_type': '1', 'notifications_enabled': '1', 'problem_acknowledged': '0', 'passive_checks_enabled': '1', 'active_checks_enabled': '1', 'flap_detection_enabled': '1', 'is_flapping': '0', 'percent_state_change': '0', 'latency': '0.0031169999856501818', 'execution_time': '0', 'scheduled_downtime_depth': '0', 'notes': 'ht-linux-columbia-mo (38.95171,-92.33407)', 'notes_url': {}, 'action_url': {}}
<class 'dict'>
{'@attributes': {'id': '1'}, 'instance_id': '1', 'host_id': '178', 'name': 'localhost', 'display_name': 'localhost', 'address': '127.0.0.1', 'alias': 'localhost', 'status_update_time': '2024-09-26 16:30:51', 'status_text': 'OK - 127.0.0.1: rta 0.018ms lost 0%', 'status_text_long': {}, 'current_state': '0', 'icon_image': {}, 'icon_image_alt': {}, 'performance_data': 'rta=0.018ms;3000.000;5000.000;0; pl=0%;80;100;0;100 rtmax=0.036ms;;;; rtmin=0.013ms;;;;', 'should_be_scheduled': '1', 'check_type': '0', 'last_state_change': '2024-08-14 05:07:28', 'last_hard_state_change': '2024-08-14 05:07:28', 'last_hard_state': '0', 'last_time_up': '2024-09-26 16:30:51', 'last_time_down': '1970-01-01 00:00:00', 'last_time_unreachable': '1970-01-01 00:00:00', 'last_notification': '1970-01-01 00:00:00', 'next_notification': '1970-01-01 00:00:00', 'no_more_notifications': '0', 'acknowledgement_type': '0', 'current_notification_number': '0', 'event_handler_enabled': '1', 'process_performance_data': '1', 'obsess_over_host': '1', 'modified_host_attributes': '0', 'event_handler': {}, 'check_command': 'check-host-alive', 'normal_check_interval': '5', 'retry_check_interval': '1', 'check_timeperiod_id': '167', 'has_been_checked': '1', 'current_check_attempt': '1', 'max_check_attempts': '10', 'last_check': '2024-09-26 16:30:51', 'next_check': '2024-09-26 16:35:51', 'state_type': '1', 'notifications_enabled': '1', 'problem_acknowledged': '0', 'passive_checks_enabled': '1', 'active_checks_enabled': '1', 'flap_detection_enabled': '1', 'is_flapping': '0', 'percent_state_change': '0', 'latency': '0.00368200009688735', 'execution_time': '0.009706', 'scheduled_downtime_depth': '0', 'notes': {}, 'notes_url': {}, 'action_url': {}}
--SN