Opsview REST API client for javascript

After moving our Opsview server to a “real” server (before it was a virtual machine, begging for more RAM), I started experimenting with the REST API of Opsview. First in PHP. Was fun but I wanted to load the data dynamically in javascript. So, I wrote a javascript client class for the Opsview REST API. It works pretty sweet! The usage is quite simple if I may say so:

//first we declare our opsview server. !Note! Cross domain scripting is not allowed,
//so this code must run on the same server as Opsview!
opsview = new opsviewrestapi('../');
	// if authentication succeeded, execute this code

	// Create a new api call
	hosts = new opsview.api('status/host');
	// We set our options
	hosts.ops.host_state = '0'; // Only UP hosts
	hosts.ops.host_state_type = 'hard'; // With a hard state type
	//do the request
		for (host in data.list){
			// show the up hosts in the body
			$('body').append('<div>UP - ' + data.list[host].name + '</div>');


opsview = new opsviewrestapi('../');

In this example our server is http://opsview with the html containing the above javascript at http://opsview/test/demo1.html. In the above example we define ‘../’ as the server location of opsview. Why? because cross-domain scripting is not allowed by the browser. I’ve made a workaround for this but more on that later in another blogpost.


Pretty straight forward I think? Success is executed when the request was successful and we got a login token. fail is run when there is an authentication error or request error.

hosts = new opsview.api('status/host');

Now this is the fun part! See what i did here? the opsview.api parameter is acutally the url for the REST API after the ‘rest/’ part. So what’s in this host variable now? Let’s see:

hosts = {
'url' : '/rest/status/host', //the full url of the REST API. Can be used to add GET parameters to a POST method
'ops' : {}, // An object containing the request parameters. if the request method is GET, this will be used as url parameters, else it's used in the request body.
'method' : 'GET', // Informative default method
get(), // GET the results *
post(), // POST the results *
put(), // PUT the results *
delete() // DELETE the results *

the post(),put(),delet() functions are only available when the REST API supports it.

So  that’s about it then. I’ll upload the client soon. But first I need to explain you my REST API Proxy. that’s for another time.

Update: here’s the GIT: https://github.com/subutux/opsviewrestapi-js

Update2: Little side-note: I’m using the jQuery lib. Just a headsup!


subutux wrote 34 posts

Post navigation


Leave a Reply