-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (118 loc) · 2.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var request = require('request');
/**
* Expose `ZabbixApi`
*/
module.exports = ZabbixApi;
/**
* Zabbix Api constructor
*
* @param {string} user
* @param {string} password
* @param {string} api_url
* @api public
*/
function ZabbixApi (user, password, api_url) {
this.user = user;
this.password = password;
this.api_url = api_url;
this.id = 0;
};
/**
* Send api request
*
* @param {string} method
* @param {string} params
* @param fn
* @api public
*/
ZabbixApi.prototype.request = function (method, params, fn) {
var self = this;
this.auth(function(error, self) {
if (error) {
fn(error, null);
} else {
params.output = params.output || 'extend';
var data = {
method: method,
params: params,
auth: self.auth_token,
};
self.post(data, self.wrap(fn));
}
});
}
/**
* Api `user.login` action . It's wrapper of `auth`.
* It's public but you don't have to call this method before you send other api request.
* We'll handle the case. Any time you send a request, we'll check if you're authenticated.
* if not we'll first send a `user.login` request to get an `auth_token`.
*
* @param {function} fn
* @api public
*/
ZabbixApi.prototype.login = function (fn) {
this.auth (function (error, self) {
fn(error, self.auth_token);
});
};
/* private */
/**
* send a http post request
*
* @param {object} data
* @param {function} fn
* @api private
*/
ZabbixApi.prototype.post = function(data, fn) {
data.jsonrpc = '2.0';
data.id = ++this.id;
var option = {
method: 'POST',
url: this.api_url,
headers: {'Content-Type': 'application/json-rpc'},
body: JSON.stringify(data),
};
request(option, fn);
};
/**
* Authenticate the user.
*
* @param {function} fn
* @api private
*/
ZabbixApi.prototype.auth = function(fn) {
if (this.auth_token) {
fn(null, this);
} else {
var data = {
method: "user.login",
params: {user: this.user, password: this.password}
};
var self = this;
this.post(data, function(err, res, body) {
if (err || res.statusCode != 200) {
fn(new Error("HTTP request error. message: " + err + ". statusCode: " + res.statusCode), self);
}
body = JSON.parse(body);
self.auth_token = body.result;
fn(body.error, self);
});
}
};
/**
* wrap the `request` callback function
*
* @param {function} fn
* @api private
*/
ZabbixApi.prototype.wrap = function (fn) {
var self = this;
return function(err, res, body) {
if (err || res.statusCode != 200) {
fn(new Error("HTTP request error. message: " + err + ". statusCode: " + res.statusCode), null);
} else {
body = JSON.parse(body);
fn(body.error, body.result);
}
}
};