Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Created August 27, 2013 15:44
Show Gist options
  • Select an option

  • Save v2e4lisp/6355282 to your computer and use it in GitHub Desktop.

Select an option

Save v2e4lisp/6355282 to your computer and use it in GitHub Desktop.
var request = require('request');
var ZabbixApi = function(user, password, api_url) {
this.user = user;
this.password = password;
this.api_url = api_url;
this.id = 0;
};
ZabbixApi.prototype.post = function(data, fn) {
data.jsonrpc = '2.0';
data.id = ++this.id;
request.post(this.api_url, data, fn);
};
ZabbixApi.prototype.auth = function(fn) {
if (this.auth) {
fn(this);
} else {
data = {
method: "user.login",
params: {user: this.user, password: this.password}
};
this.post(data, function(err, res, body) {
if (!err and res.statusCode == 200) {
this.auth = JSON.parse(body).auth;
fn(this);
} else {
throw new Error("Authentication failed.");
}
});
}
};
ZabbixApi.prototype.request = function (method, params, fn) {
if (!this.user or !this.password or !this.api_url) {
throw new Error("Cannot send request.");
}
this.auth(function(self) {
data = {
method: method,
params: params,
auth: self.auth,
}
self.post(data, fn)
}):
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment