Query on Rest Integration with PHP client

32 views
Skip to first unread message

vikrant sundriyal

unread,
Oct 24, 2016, 1:42:30 PM10/24/16
to nagrestconf-users

Hi,
Am pretty new to Nagios and Nagrestconf.
My requirement is to integrate nagrest with CRM via REST in php.
As per documentation check and show commands are executed via http GET requests which am able to execute by passing json in query string.

My problem is with Post request. While trying to add host via http post am getting error:ERROR 1029.

Can someone help whether my below rest paramaters are in correct format: 

Json: [{"folder":"local"},[{"name":"VikHostttt"},{"alias":"VikHost"},{"ipaddress":"xx.xx.xx.xx"},{"template":"hsttmpl-local"},{"servicesets":"example-lin"}]]
Am posting this json, is this right approach OR i need to pass as query string even for add/modify operations.

I  even tried below json format which also gave same error:
[{"folder":"local"},{"name":"VikHostttt","alias":"VikHost","ipaddress":"xx.xxx.xx.xx.x","template":"hsttmpl-local","servicesets":"example-lin"}]


Regards
Vikrant

Mark Clarkson

unread,
Oct 24, 2016, 5:20:17 PM10/24/16
to nagrestc...@googlegroups.com
On Mon, 2016-10-24 at 10:42 -0700, vikrant sundriyal wrote:

Can someone help whether my below rest paramaters are in correct format: 


Hi Vikrant,
I'm going to paste an answer I gave someone else as it might help you. The person asked if there are any 'curl' examples...

There's the REST tutorial on WayBackMachine of this page, which uses resty instead of nrcq, which is much closer to curl, which might help:

http://web.archive.org/web/201...


And also the example set up script still has all the curl commands for a sample setup:

https://github.com/mclarkson/n...

That will take a bit of deciphering but not huge amounts. However, If you uncomment the line near the top, '#curl() { :; }', then you can run the script without it changing anything or connecting to anything and it will show you all the curl commands that it would have run.


Lastly, is the REST reference page:

http://nagrestconf.smorg.co.uk...

Look for fields that should be encoded ('E' - The field should be URL encoded.), and you will need to url encode those fields, which is why I wrote nrcq - it does it for you. If you don't use any special characters anywhere then you could get by without url encoding. That page is worth a quick scan though.


However, you might want to try using nrcq to get to grips with the api first. The REST tutorial at http://nagrestconf.smorg.co.uk/documentation/resttut.php uses nrcq.


Cheers!

Mark

vikrant sundriyal

unread,
Oct 27, 2016, 11:48:54 AM10/27/16
to nagrestconf-users
Thanks Mark!

Am working on a big scale monitoring project involving CRM, portal, Nagios and many other third party leading SaaS systems. Nagrestconf would have really helped us in performing small runtime changes in Nagios via rest.
Unfortunately though am not new to third party Rest integrations, am unable to crack this puzzle. 

Leaving "add host" function, I started trying the simpler Post action- restart. But this is also not working, am getting same error ""ERROR 1011: 'folder' is undefined"".

I even tried taking curl from the url which you shared "curl -knX POST -d "'json={\"folder\":\"$FOLDER\"}'" http://${IP}/rest/restart/nagios", converted that to php client. That too gave same error.

Am copying code below, please have a look, feedback if you see any issue .

=======
<?php


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "'json={\"folder\":\"local\"}'");
curl_setopt($ch, CURLOPT_POST, 1);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
echo "<pre/>";
echo($result);
echo "</pre>";

curl_close ($ch);

?>
===========

Mark Clarkson

unread,
Oct 28, 2016, 6:02:31 AM10/28/16
to nagrestc...@googlegroups.com
Hi Vikrant,
So sorry this does not work for you :(

On a host running nagrestconf I did:

$ cd
$ cp /usr/share/doc/nagrestconf-1.174.6/bulk-loading/REST_setup_local.sh .
$ vim REST_setup_local.sh
.. uncommented '#curl() { :; }' line at the top ..
$ bash REST_setup_local.sh

.. produces lots of output including the following bit at the end ..

-----------------------------------------------------------
- Apply, Check and Restart
-----------------------------------------------------------

curl -knX POST -d 'json={"folder":"local"}' http://127.0.0.1/rest/apply/nagiosconfig

curl -knX GET 'http://127.0.0.1/rest/check/nagiosconfig?json=\{"folder":"local","verbose":"false"\}'

curl -knX POST -d 'json={"folder":"local"}' http://127.0.0.1/rest/restart/nagios

There are tons of examples in that output so please run it.
NOTE the escaping. CURL needs '{' escaped when it is a URL, but not for POST data.

Now, for php requests someone wrote an example php in the comments here:
I've pasted the comment below...

----------------------- snip ----------------------

Christopher Bonitz Mark Clarkson a year ago

Thanks. Ended up getting it to work.
Wrote a method to handle all writing, the json being sent as a post variable kind of tricked me.

protected function post($commandarg, $post_data = array())
{
if($this->valid)
{
// build the urlencoded data
$postvars = http_build_query($post_data);

// open connection
$connection = curl_init();

// set the url, number of POST vars, POST data
curl_setopt($connection, CURLOPT_URL, $this->url . $commandarg);
curl_setopt($connection, CURLOPT_POST, count($post_data));
curl_setopt($connection, CURLOPT_POSTFIELDS, $postvars);

// execute post
$result = curl_exec($connection);

// close connection
curl_close($connection);

return $result;
}
return false;
}

From my other objects i build the json data like array("json" => $jsonData)
ending up with something like this.
$this->post("add/hosts",array("json" => $jsonData));

It is object oriented, (hence the $this-> )

Hope it might help others in similar situations.


----------------------- snip ----------------------


Hope this helps!

Cheers,
Mark.
--
You received this message because you are subscribed to the Google Groups "nagrestconf-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nagrestconf-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vikrant sundriyal

unread,
Nov 1, 2016, 1:57:16 PM11/1/16
to nagrestconf-users
Thanks Mark,

It worked!!! Able to perform actions like restart, addhost etc..

For others facing similar problem, here are the steps:
  1. As Mark suggested, execute following commands from terminal -> $ cd
  1. $ cp /usr/share/doc/nagrestconf-1.174.6/bulk-loading/REST_setup_local.sh .
    $ vim REST_setup_local.sh
    .. uncommented '#curl() { :; }' line at the top ..
    $ bash REST_setup_local.sh
  1. You will find sample curl commands for almost all actions.
  2. Pick the one which you want to execute. e.g for restart -> "curl -knX POST -d 'json={"folder":"local"}' http://127.0.0.1/rest/restart/nagios"
  3. Convert curl to php Rest code.
  4. For above example code will be: $ch = curl_init();


    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/rest/restart/nagios");

  1. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, "json={\"folder\":\"local\"}");

    curl_setopt($ch, CURLOPT_POST, 1);


    $result = curl_exec($ch);

    if (curl_errno($ch)) {

        echo 'Error:' . curl_error($ch);

    }

  1. curl_close ($ch);

  2.  Change url and execute.

Cheers!!!
Reply all
Reply to author
Forward
0 new messages