3

I'm trying to schedule a downtime for the service in nagios using ajax call. I can able to schedule it through NAgios GUI and found a method to use curl for scheduling.

I Found one link How to set downtime for any specific nagios host for certain time from commandline through curl? which explains how to achieve it through curl command.

I tried to achieve it through curl command.

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"

It works fine.

I tried to convert the same curl functionality to ajax post call.

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");
                        }
                    });

But I got 500 Internal Server Error. Kindly guide me to achieve this using ajax.

user2439278
  • 1,222
  • 7
  • 41
  • 75

1 Answers1

1

Seems like the data is supposed to be sent as a form and not as json. Remove contentType: 'application/json' and it should work

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/

Edit-1: 09-May-2018

You should update your code as below

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");
    }
});

The will make sure that data goes as application/x-www-form-urlencoded and also jQuery checks the response and decides the types

Community
  • 1
  • 1
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265