JSON result

79 views
Skip to first unread message

Tomasz K.

unread,
Aug 10, 2018, 9:39:46 AM8/10/18
to mementodatabase
Hello
I have a problem with javascript. I need to get data from the Nagios server.
The script connects to the server and gets the answer from Nagios.
I can not get a specific value of the "address" field.


ACTION script:

var e = entry();
var hostAdres = 'SOME HOST';
var url = ("http://"SOME IP"/nagios//cgi-bin/objectjson.cgi?query=host&hostname=" + hostAdres);
var http = http();
http.headers( { 'Authorization' : 'Basic '+ 'bWVtZW50bzpkYXRhYmFzZQ==' });
var result = http.get(url);
if(result.code == 200){
var hostStatus = JSON.parse(result.body);
message(result.body);
} else {
cancel();

JSON result body:

{
"format_version": 0,
"result": {
"query_time": 1533906635000,
"cgi": "objectjson.cgi",
"user": "USER",
"query": "host",
"query_status": "released",
"program_start": 1533630528000,
"last_data_update": 1533630529000,
"type_code": 0,
"type_text": "Success",
"message": ""
},
"data": {
"host": {
"name": "host_name",
"display_name": "host_name",
"alias": "HOST NAME",
"address": "10.255.44.125",
"parent_hosts": [
],
"child_hosts": [
],
"services": [
"SERVICE"
],
"check_command": "check-host-alive-f",
"initial_state": 0,
"check_interval": 1,
"retry_interval": 1,
"max_attempts": 1,
"event_handler": "",
"contact_groups": [
],
"contacts": [
],
"notification_interval": 60,
"first_notification_delay": 0,
"notifications_options": 3,
"notification_period": "24x7",
"check_period": "24x7",
"flap_detection_enabled": true,
"low_flap_threshold": 0,
"high_flap_threshold": 0,
"flap_detection_options": -1,
"stalking_options": 0,
"check_freshness": false,
"freshness_threshold": 0,
"process_performance_data": true,
"checks_enabled": true,
"accept_passive_checks": true,
"event_handler_enabled": true,
"retain_status_information": true,
"retain_nonstatus_information": true,
"obsess": true,
"hourly_value": 0,
"notes": "",
"notes_url": "",
"action_url": "",
"icon_image": "",
"icon_image_alt": "",
"vrml_image": "",
"statusmap_image": "",
"have_2d_coords": false,
"x_2d": -1,
"y_2d": -1,
"have_3d_coords": false,
"x_3d": 0,
"y_3d": 0,
"z_3d": 0,
"should_be_drawn": true,
"custom_variables": {
}
}
}
}
 
Can somebody help me with this?

Tomasz K.

unread,
Aug 12, 2018, 12:26:43 PM8/12/18
to mementodatabase
I have a solution to my problem.
I asked Vasiliy Egorov for help:
I got the answer from him:

You can try next:
var response = JSON.parse (result.body);
var address = response.data.host.address;

Vasiliy, thank you so much for your help. If it was not for you, I would not be able to do it with this script.
Best Regards

FIXED SCRIPT:

var host = 'SOME HOST';

var url = ("http://"

    SOME IP
"/nagios//cgi-bin/objectjson.cgi?query=host&hostname=" + host);

var http = http();
http
.headers({
   
'Authorization': 'Basic ' + 'bWVtZW50bzpkYXRhYmFzZQ=='
});
var result = http.get(url);
if (result.code == 200) {

   
var response = JSON.parse(result.body);
   
var address = response.data.host.address;
} else {
    cancel
();


Reply all
Reply to author
Forward
0 new messages