Nagios JSON查询返回不正确的值

时间:2021-03-22 20:28:06

I have installed Nagios (Nagios® Core™ Version 4.2.2) in Linux Server. I have been using JSON Query Generator to generate an Availablity report in JSON format. It will provide an API. I have created a Javascript which will pass this URL to the Ajax call and based on success result, will print the percentage in our Own dashboard.

我在Linux Server中安装了Nagios(Nagios®Core™版本4.2.2)。我一直在使用JSON查询生成器生成JSON格式的可用性报告。它将提供API。我创建了一个Javascript,它将此URL传递给Ajax调用,并根据成功结果,将在我们自己的仪表板中打印百分比。

JSON URL : http://xx.xx.xx.xx/nagios/cgi-bin/archivejson.cgi?query=availability&availabilityobjecttype=hostgroups&hostgroup=ALM&assumedinitialhoststate=up&assumedinitialservicestate=ok&starttime=1514297016&endtime=1514383416

The start and End time contains time in EPOCH format.

开始和结束时间包含EPOCH格式的时间。

In Javascript, I have created a function, which will pass the start and end time as a variable to the URL.

在Javascript中,我创建了一个函数,它将开始和结束时间作为变量传递给URL。

var time = new Date();
var end = Math.floor((new Date).getTime() / 1000);
//var end = ~~(Date.now() /1000) ;
var start = Math.floor(time.setDate(time.getDate() - 1) / 1000);
Availreport = "http://xx.xx.xx.xx/nagios/cgi-bin/archivejson.cgi?query=availability&availabilityobjecttype=hostgroups&hostgroup=ALM&assumedinitialhoststate=up&assumedinitialservicestate=ok&starttime=" + start + "&endtime=" + end;

$.ajax({
  url: Availreport,
  timeout: 30000,
  beforeSend: function(xhr) {
    xhr.setRequestHeader('Authorization',
      make_base_auth("nagiosadmin", "nagiosadmin"));
  },

  dataType: 'json', //data format
  success: onOutboundReceived //on receive of reply

});

In the browser, when I check the dashboard, the time_up json key gives an incorrect value (value as 0). The same URL, when executed directly in the browser, gives the correct output.

在浏览器中,当我检查仪表板时,time_up json键给出了一个不正确的值(值为0)。直接在浏览器中执行时,相同的URL会提供正确的输出。

How can I resolve this issue?

我该如何解决这个问题?

1 个解决方案

#1


1  

I tried with my own Nagios XI 5.4.11 installation (you did not referenced any specific version) and I didn't find any difference among availability reports obtained, directly from UI, invoking API in the browser or using a JS script.

我尝试使用自己的Nagios XI 5.4.11安装(您没有引用任何特定版本),我发现直接从UI获取的可用性报告,在浏览器中调用API或使用JS脚本没有任何区别。

So I could not reproduce your issue, but just for reference and to help in debugging here below is the snippet I used successfully to obtain the correct JSON answer from Nagios API archivejson.cgi?query=availability.

所以我无法重现您的问题,但仅供参考和帮助调试,下面是我成功使用的片段从Nagios API archivejson.cgi获取正确的JSON答案?query = availability。

The script uses two ways of authentication, the first one, with credentials passed directly in the url, really unsecure:

该脚本使用两种身份验证方式,第一种方式,凭证直接在URL中传递,真的不安全:

var time = new Date();
var end = Math.floor((new Date).getTime() / 1000);
var start = Math.floor(time.setDate(time.getDate() - 1) / 1000);

var user = "admin";
var pwd = "admin";

var baseurl = "mynagioshost:443/nagios";

var url1 = "https://"+user+":"+pwd+"@"+baseurl+"/cgi-bin/archivejson.cgi?query=availability&availabilityobjecttype=hostgroups&hostgroup=application-servers&assumedinitialhoststate=up&assumedinitialservicestate=ok&starttime=" + start + "&endtime=" + end;
var url2 = "https://"+baseurl+"/cgi-bin/archivejson.cgi?query=availability&availabilityobjecttype=hostgroups&hostgroup=application-servers&assumedinitialhoststate=up&assumedinitialservicestate=ok&starttime=" + start + "&endtime=" + end;

function go1() {
	$.support.cors = true;

	$.ajax({
		url: url1,
		timeout: 30000,
		crossDomain: true,
		dataType: 'json', //data format
		success: function(data) {
			console.log("success: ", data);
			$("#resp1").html(JSON.stringify(data, null, 2));
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log("error: ", errorThrown);
		}
	});
}

function go2() {
	$.support.cors = true;

	$.ajax({
		url: url2,
		timeout: 30000,
		crossDomain: true,
		xhrFields: {
			withCredentials: true
		},
		beforeSend: function(xhr) {
			var auth = make_base_auth(user, pwd);
			console.log("beforeSend: ", auth);
			xhr.setRequestHeader('Authorization', auth);
		},
		dataType: 'json',
		success: function(data) {
			console.log("success: ", data);
			$("#resp2").html(JSON.stringify(data, null, 2));
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log("error: ", errorThrown);
		}
	});
}

function make_base_auth(user, password) {
  var tok = user + ':' + password;
  // Base64 encoding for basic auth encoding username:password
  var hash = btoa(tok);
  // return the auth header
  return "Basic " + hash;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="btn1" onclick="go1()">get report - mode1</button>
<br>
<button id="btn2" onclick="go2()">get report - mode2</button>
<br>
<table>
  <tr>
    <td style="width: 45%">
      <pre id="resp1"></pre>
    </td>
    <td style="width: 45%">
      <pre id="resp2"></pre>
    </td>
  </tr>
</table>

As already stated data received from API are identical anyway (except for timings, obviously) and are about this way:

如前所述,从API收到的数据无论如何都是相同的(显然除了时间)并且是这样的:

{
  "format_version": 0,
  "result": {
    "query_time": 1515654147000,
    "cgi": "archivejson.cgi",
    "user": "nagiosadmin",
    "query": "availability",
    "query_status": "released",
    "program_start": 1512375100000,
    "last_data_update": 1515651099000,
    "type_code": 0,
    "type_text": "Success",
    "message": ""
  },
  "data": {
    "selectors": {
      "availabilityobjecttype": 4,
      "starttime": 1515567802000,
      "endtime": 1515654202000,
      "hostgroup": "application-servers",
      ...OMISSIS...
    },
    "hostgroup": {
      "name": "application-servers",
      "hosts": [
        {
          "name": "192.168.2.20",
          "time_up": 86345,
          "time_down": 22,
          "time_unreachable": 0,
          "scheduled_time_up": 0,
          "scheduled_time_down": 0,
          "scheduled_time_unreachable": 0,
          "time_indeterminate_nodata": 0,
          "time_indeterminate_notrunning": 0
        },
        ...OMISSIS...
      ]
    }
  }
}

#1


1  

I tried with my own Nagios XI 5.4.11 installation (you did not referenced any specific version) and I didn't find any difference among availability reports obtained, directly from UI, invoking API in the browser or using a JS script.

我尝试使用自己的Nagios XI 5.4.11安装(您没有引用任何特定版本),我发现直接从UI获取的可用性报告,在浏览器中调用API或使用JS脚本没有任何区别。

So I could not reproduce your issue, but just for reference and to help in debugging here below is the snippet I used successfully to obtain the correct JSON answer from Nagios API archivejson.cgi?query=availability.

所以我无法重现您的问题,但仅供参考和帮助调试,下面是我成功使用的片段从Nagios API archivejson.cgi获取正确的JSON答案?query = availability。

The script uses two ways of authentication, the first one, with credentials passed directly in the url, really unsecure:

该脚本使用两种身份验证方式,第一种方式,凭证直接在URL中传递,真的不安全:

var time = new Date();
var end = Math.floor((new Date).getTime() / 1000);
var start = Math.floor(time.setDate(time.getDate() - 1) / 1000);

var user = "admin";
var pwd = "admin";

var baseurl = "mynagioshost:443/nagios";

var url1 = "https://"+user+":"+pwd+"@"+baseurl+"/cgi-bin/archivejson.cgi?query=availability&availabilityobjecttype=hostgroups&hostgroup=application-servers&assumedinitialhoststate=up&assumedinitialservicestate=ok&starttime=" + start + "&endtime=" + end;
var url2 = "https://"+baseurl+"/cgi-bin/archivejson.cgi?query=availability&availabilityobjecttype=hostgroups&hostgroup=application-servers&assumedinitialhoststate=up&assumedinitialservicestate=ok&starttime=" + start + "&endtime=" + end;

function go1() {
	$.support.cors = true;

	$.ajax({
		url: url1,
		timeout: 30000,
		crossDomain: true,
		dataType: 'json', //data format
		success: function(data) {
			console.log("success: ", data);
			$("#resp1").html(JSON.stringify(data, null, 2));
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log("error: ", errorThrown);
		}
	});
}

function go2() {
	$.support.cors = true;

	$.ajax({
		url: url2,
		timeout: 30000,
		crossDomain: true,
		xhrFields: {
			withCredentials: true
		},
		beforeSend: function(xhr) {
			var auth = make_base_auth(user, pwd);
			console.log("beforeSend: ", auth);
			xhr.setRequestHeader('Authorization', auth);
		},
		dataType: 'json',
		success: function(data) {
			console.log("success: ", data);
			$("#resp2").html(JSON.stringify(data, null, 2));
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log("error: ", errorThrown);
		}
	});
}

function make_base_auth(user, password) {
  var tok = user + ':' + password;
  // Base64 encoding for basic auth encoding username:password
  var hash = btoa(tok);
  // return the auth header
  return "Basic " + hash;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="btn1" onclick="go1()">get report - mode1</button>
<br>
<button id="btn2" onclick="go2()">get report - mode2</button>
<br>
<table>
  <tr>
    <td style="width: 45%">
      <pre id="resp1"></pre>
    </td>
    <td style="width: 45%">
      <pre id="resp2"></pre>
    </td>
  </tr>
</table>

As already stated data received from API are identical anyway (except for timings, obviously) and are about this way:

如前所述,从API收到的数据无论如何都是相同的(显然除了时间)并且是这样的:

{
  "format_version": 0,
  "result": {
    "query_time": 1515654147000,
    "cgi": "archivejson.cgi",
    "user": "nagiosadmin",
    "query": "availability",
    "query_status": "released",
    "program_start": 1512375100000,
    "last_data_update": 1515651099000,
    "type_code": 0,
    "type_text": "Success",
    "message": ""
  },
  "data": {
    "selectors": {
      "availabilityobjecttype": 4,
      "starttime": 1515567802000,
      "endtime": 1515654202000,
      "hostgroup": "application-servers",
      ...OMISSIS...
    },
    "hostgroup": {
      "name": "application-servers",
      "hosts": [
        {
          "name": "192.168.2.20",
          "time_up": 86345,
          "time_down": 22,
          "time_unreachable": 0,
          "scheduled_time_up": 0,
          "scheduled_time_down": 0,
          "scheduled_time_unreachable": 0,
          "time_indeterminate_nodata": 0,
          "time_indeterminate_notrunning": 0
        },
        ...OMISSIS...
      ]
    }
  }
}