Skip to content

Commit

Permalink
Merge pull request #11 from sumitgoelpw/devel
Browse files Browse the repository at this point in the history
code refactor and option to pass params directly to request object
  • Loading branch information
Sumit Goel authored Sep 5, 2017
2 parents 603c0db + e5901e9 commit 6175c29
Show file tree
Hide file tree
Showing 21 changed files with 832 additions and 484 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
[libs]

[options]
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ install:
- npm install

script:
- shellcheck -s bash -Calways ./tasks/test.sh
- bash ./tasks/test.sh
- npm test

matrix:
fast_finish: true
11 changes: 10 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
version: '2'
services:
zabbix-database:
hostname: zabbix-database.local
image: postgres:alpine
environment:
POSTGRES_USER: ${DBUSER}
POSTGRES_PASSWORD: ${DBPASS}
zabbix-server:
hostname: zabbix-server.local
image: zabbix/zabbix-server-pgsql:${ZABTAG}
environment:
DB_SERVER_HOST: zabbix-database
Expand All @@ -14,9 +16,13 @@ services:
depends_on:
- zabbix-database
zabbix-web:
hostname: zabbix-web.local
image: zabbix/zabbix-web-nginx-pgsql:${ZABTAG}
volumes:
- ${NGINXDIRSSL}:/etc/ssl/nginx
ports:
- ${HOSTPORT}:80/tcp
- 0.0.0.0:${HOSTPORT}:80/tcp
- 0.0.0.0:${HOSTPORTSSL}:443/tcp
environment:
ZBX_SERVER_HOST: zabbix-server
DB_SERVER_HOST: zabbix-database
Expand All @@ -27,20 +33,23 @@ services:
- zabbix-server
- zabbix-database
zabbix-agent1:
hostname: zabbix-agent1.local
image: zabbix/zabbix-agent:${ZABTAG}
environment:
ZBX_HOSTNAME: zabbix-agent1
ZBX_SERVER_HOST: zabbix-server
depends_on:
- zabbix-server
zabbix-agent2:
hostname: zabbix-agent2.local
image: zabbix/zabbix-agent:${ZABTAG}
environment:
ZBX_HOSTNAME: zabbix-agent2
ZBX_SERVER_HOST: zabbix-server
depends_on:
- zabbix-server
zabbix-agent3:
hostname: zabbix-agent3.local
image: zabbix/zabbix-agent:${ZABTAG}
environment:
ZBX_HOSTNAME: zabbix-agent3
Expand Down
3 changes: 1 addition & 2 deletions examples/createHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ zabbix.login()
.then((value) => console.log(JSON.stringify(value, null, whiteSpaceCount)))
.then(() => zabbix.logout())
.catch((reason) =>
console.log(JSON.stringify(reason, null, whiteSpaceCount))
);
console.log(JSON.stringify(reason, null, whiteSpaceCount)));
3 changes: 1 addition & 2 deletions examples/getEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ zabbix.login()
})
.then(() => zabbix.logout())
.catch((reason) =>
console.log(JSON.stringify(reason, null, whiteSpaceCount))
);
console.log(JSON.stringify(reason, null, whiteSpaceCount)));
3 changes: 1 addition & 2 deletions examples/getHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ zabbix.login()
.then((value) => console.log(JSON.stringify(value, null, whiteSpaceCount)))
.then(() => zabbix.logout())
.catch((reason) =>
console.log(JSON.stringify(reason, null, whiteSpaceCount))
);
console.log(JSON.stringify(reason, null, whiteSpaceCount)));
10 changes: 10 additions & 0 deletions examples/sendValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const zabbix = require('../index');

zabbix.sender({
'path': '/usr/local/bin/zabbix_sender',
'server': 'zabbix-dev',
'host': 'CloudGenix-Events',
'values': '- testing 444\n- testing 111\n'
})
.then((value) => console.log(value))
.catch((reason) => console.log(reason));
58 changes: 21 additions & 37 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const req = require('./wrapper');

const HTTPOK = 200;
const debug = require('debug')('api');

class Zabbix {

Expand All @@ -10,46 +9,41 @@ class Zabbix {
* @param {string} url - Zabbix API endpoint.
* @param {string} user - login name.
* @param {string} password - login password.
* @param {boolean} rejectUnauthorized - Use false for cert verification.
* @param {Object} options - specify request options.
* Link: https://github.com/request/request#requestoptions-callback
*/
constructor(url, user, password, rejectUnauthorized = true) {
constructor(url, user, password, options = {}) {

this.url = url;
this.user = user;
this.password = password;
this.rejectUnauthorized = rejectUnauthorized;
this.options = options;
this.rpcid = 0;
this.authid = null;
} // eslint: constructor

static reqValidation(value) {

return new Promise((resolve, reject) => {

const { result } = value.body;

if (value.statusCode === HTTPOK && result) {

resolve(result);
} else {

reject(result);
}
});
} // eslint: reqValidation

request(method, params) {

const opts = {
'id': this.rpcid += 1,
'uri': this.url,
'auth': this.authid,
'rejectUnauthorized': this.rejectUnauthorized,
'options': this.options,
method,
params
};

return req.post(opts).then(value => this.constructor.reqValidation(value));
return req.post(opts).then(value => {

debug('HTTP response: %o', value);

if (!Object.prototype.hasOwnProperty.call(value, 'result')) {

throw new Error(value);
}

return value.result;
});
} // eslint: request

/**
Expand All @@ -65,13 +59,8 @@ class Zabbix {
'password': this.password
}).then(value => {

if (typeof value === 'string' || value instanceof String) {

this.authid = value;
return value;
}

throw value;
this.authid = value;
return value;
});
} // eslint: login

Expand All @@ -85,13 +74,8 @@ class Zabbix {

return this.request('user.logout', []).then(value => {

if (typeof value === 'boolean' && value) {

this.authid = null;
return value;
}

throw value;
this.authid = null;
return value;
});
} // eslint: logout

Expand Down
62 changes: 58 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
const Zabbix = require('./api');
const { exec } = require('child_process');

class Client extends Zabbix {

sendValues() {
static sender(options) {

console.log(`Coming soon... ${this.url}`);
}
// $FlowFixMe: more research needed
return new Promise((resolve, reject) => {
// eslint-disable-line max-statements, max-len

}
if (!Object.prototype.hasOwnProperty.call(options, 'path')) {

reject(new Error('Please provide full path for zabbix_sender ' + 'utility.'));
} // eslint: path

if (!Object.prototype.hasOwnProperty.call(options, 'server')) {

reject(new Error('Please provide zabbix server or proxy name.'));
} // eslint: server

if (!Object.prototype.hasOwnProperty.call(options, 'port')) {

options.port = '10051';
} // eslint: port

if (!Object.prototype.hasOwnProperty.call(options, 'host')) {

reject(new Error('Please provide the zabbix host name as ' + 'registered in Zabbix frontend.'));
} // eslint: host

if (!Object.prototype.hasOwnProperty.call(options, 'values')) {

reject(new Error('Specify values each line contains whitespace ' + 'delimited: - <key> <value>'));
} // eslint: values

resolve(options);
}).then(val => {

const { path, server, port, host, values } = val;

return new Promise((resolve, reject) => {

let cmd = `printf -- '${values}' | ${path} -vv -z ${server} `;
cmd += `-p ${port} -s ${host} -r -i -`;

exec(cmd, (error, stdout, stderr) => {

if (error) {

return reject(error);
}

return resolve({
stdout,
stderr
});
}); // eslint: exec
}); // eslint: promise
}); // eslint: then

} // eslint: sender

} // eslint: Class

module.exports = Client;
11 changes: 6 additions & 5 deletions lib/wrapper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const rp = require('request-promise-native');
const debug = require('debug')('wrapper');

module.exports = {

Expand All @@ -11,14 +12,10 @@ module.exports = {
*/
'post': opts => {

const options = {
'method': 'POST',
let options = {
'uri': opts.uri,
'json': true,
'gzip': true,
'rejectUnauthorized': opts.rejectUnauthorized,
'simple': false,
'resolveWithFullResponse': true,
'body': {
'jsonrpc': '2.0',
'id': opts.id,
Expand All @@ -28,6 +25,10 @@ module.exports = {
}
};

options = Object.assign(options, opts.options);

debug('HTTP POST Options: %o', options);

return rp.post(options);
}
};
Loading

0 comments on commit 6175c29

Please sign in to comment.