使用 ajax POST 安排 Nagios 中服务的停机时间



我正在尝试使用 ajax 调用为 nagios 中的服务安排停机时间。我可以通过 NAgios GUI 来安排它,并找到了一种使用 curl 进行调度的方法。

我找到了一个链接 如何通过 curl 为任何特定的 nagios 主机设置一定时间的停机时间?这解释了如何通过 curl 命令实现它。

我试图通过 curl 命令来实现它。

curl 
--data cmd_typ=56 
--data cmd_mod=2 
--data host=jenkins 
--data "service=Jenkins+PROD+GUI" 
--data "com_author=Nagios Admin"
--data "com_data=Test" 
--data trigger=0 
--data "start_time=05-09-2018+14%3A05%3A14" 
--data "end_time=05-09-2018+16%3A05%3A14" 
--data fixed=1 
--data btnSubmit=Commit 
http://xx.xx.xx:8087/nagios/cgi-bin/cmd.cgi -u "nagiosadmin:nagiosadmin"

它工作正常。

我尝试将相同的卷曲功能转换为 ajax post 调用。

HTML :
<form name="NAME" id="avialform" class="avail" action="">
<fieldset id="availfield">
<legend style="color:white" id="availegend">SCHEDULED DOWNTIME</legend>
<table width="100%" cellpadding="0" cellspacing="0" class="vzuui-detailpanel">
<tr>
<td><label>Service :</label>
<select id = "ServiceList">
<option value = "Jenkins+PROD+GUI">Jenkins Prod</option>
</select>
</td>   
</tr>
<tr>
<td><label>From Date :</label><input id="from" type="datetime-local" name="fromdate" /></td>
</tr>
<tr>
<td><label>To Date :</label><input id="to" type="datetime-local" name="todate" /></td>
</tr>
<tr>
<td><label>Comment :</label><input id="comment" type="text" name="Servicecommt" /></td>
</tr>
</table>
</fieldset>  
<button class="vzuui-btn-red-active" type="button" id="getrepo">Submit</button>
</form>
Ajax:
var posdata = {"cmd_typ":56,"cmd_mod":2,"host":"jenkins","service":"Jenkins+PROD+GUI","com_author":"Nagios Admin","com_data":"Test","trigger":0,"start_time":"2018-05-09T18:00","end_time":"2018-05-09T19:00","fixed":1,"btnSubmit":"Commit"}
posdata["service"] = select.options[select.selectedIndex].value;
posdata["com_data"] = document.getElementById("comment").value;
posdata["start_time"] = document.getElementById("from").value;
posdata["end_time"] = document.getElementById("to").value;
console.log(JSON.stringify(posdata));
$.support.cors = true;    
$.ajax({
url: "http://xx.xx.xx:8087/nagios/cgi-bin/cmd.cgi",
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization',
make_base_auth("nagiosadmin", "nagiosadmin"));
},
type: 'POST',
dataType: 'json',
contentType: 'application/json',
processData: false,
data: posdata,
success: function (data) {
alert(JSON.stringify(data));
},
error: function(){
alert("Cannot get data");
}
});

但是我得到了500内部服务器错误。请指导我使用 ajax 实现此目的。

似乎数据应该作为表单而不是 json 发送。删除contentType: 'application/json',它应该可以工作

contentType (默认: 'application/x-www-form-urlencoded; charset=UTF-8')

类型:布尔值或字符串

将数据发送到服务器时,请使用此内容类型。默认值为"application/x-www-form-urlencoded;

http://api.jquery.com/jquery.ajax/

编辑-1: 09-五月-2018

您应该按如下方式更新代码

var posdata = {
"cmd_typ": 56,
"cmd_mod": 2,
"host": "jenkins",
"service": "Jenkins+PROD+GUI",
"com_author": "Nagios Admin",
"com_data": "Test",
"trigger": 0,
"start_time": "2018-05-09T18:00",
"end_time": "2018-05-09T19:00",
"fixed": 1,
"btnSubmit": "Commit"
}
posdata["service"] = select.options[select.selectedIndex].value;
posdata["com_data"] = document.getElementById("comment").value;
posdata["start_time"] = document.getElementById("from").value;
posdata["end_time"] = document.getElementById("to").value;
console.log(JSON.stringify(posdata));
$.support.cors = true;
$.ajax({
url: "http://xx.xx.xx:8087/nagios/cgi-bin/cmd.cgi",
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization',
make_base_auth("nagiosadmin", "nagiosadmin"));
},
type: 'POST',
data: posdata,
success: function(data) {
alert(JSON.stringify(data));
},
error: function() {
alert("Cannot get data");
}
});

将确保数据按application/x-www-form-urlencoded进行,并且jQuery检查响应并确定类型

最新更新