使用ajax POST为Nagios中的服务安排停机时间
我正在尝试使用ajax调用为nagios中的服务安排停机时间。我可以通过NAgios GUI进行调度,并找到了使用curl进行调度的方法 我找到了一个链接,它解释了如何通过curl命令实现它使用ajax POST为Nagios中的服务安排停机时间,ajax,nagios,Ajax,Nagios,我正在尝试使用ajax调用为nagios中的服务安排停机时间。我可以通过NAgios GUI进行调度,并找到了使用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
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"
我试图通过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"
它很好用
我尝试将相同的curl功能转换为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");
}
});
HTML:
计划停机时间
服务:
詹金斯针
自日期起:
迄今为止:
评论:
提交
阿贾克斯:
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[“服务”]=select.options[select.selectedIndex].value;
posdata[“com_data”]=document.getElementById(“comment”).value;
posdata[“开始时间”]=document.getElementById(“from”).value;
posdata[“end_time”]=document.getElementById(“to”).value;
log(JSON.stringify(posdata));
$.support.cors=true;
$.ajax({
url:“http://xx.xx.xx:8087/nagios/cgi-bin/cmd.cgi“,
发送前:函数(xhr){
xhr.setRequestHeader('授权',
使基础授权(“nagiosadmin”、“nagiosadmin”);
},
键入:“POST”,
数据类型:“json”,
contentType:'应用程序/json',
processData:false,
数据:posdata,
成功:功能(数据){
警报(JSON.stringify(数据));
},
错误:函数(){
警报(“无法获取数据”);
}
});
但我有500个内部服务器错误。请指导我使用ajax实现这一点。似乎数据应该以表单而不是json的形式发送。删除
contentType:'application/json'
,它应该可以工作
contentType(默认值:“application/x-www-form-urlencoded;charset=UTF-8”)
类型:布尔或字符串
向服务器发送数据时,请使用此内容类型。默认值为“application/x-www-form-urlencoded
编辑日期:2018年5月9日
你应该更新你的代码如下
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");
}
});
将确保数据以
应用程序/x-www-form-urlencoded
的形式运行,同时jQuery检查响应并确定类型curl是否适合您?我执行了curl命令。但收到的错误消息为“抱歉,您无权提交指定的命令”。“所以第一个重点应该是让它通过curl工作,我假设它已经工作了。看看这是否有助于现在的curl工作正常。然后在jQuery中使用相同的方法,现在它是一个get url,所以一切都简单得多