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

Schedule Downtime for a Service in Nagios using ajax POST

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

我找到了一个 link How to set downtime for any specific nagios host for certain time from commandline through curl? 解释了如何通过 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"

它工作正常。

我试图将相同的 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");
                        }
                    });

但是我得到了 500 Internal Server Error。请指导我使用 ajax.

实现此目的

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

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

Type: Boolean or String

When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded;

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

编辑 1: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");
    }
});

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