Skip to content

Commit

Permalink
Rewrite the whole project with async/await and no dependency (#15)
Browse files Browse the repository at this point in the history
* v2.0.0

* shellcheck SC1117 fix

* remove postgres port forwarding from docker-compose

* remove unsed dev dependency

* bug fix

* compatibility fix for node 8

* docs update
  • Loading branch information
StarryShark committed May 24, 2019
1 parent 5e56188 commit e20f5c0
Show file tree
Hide file tree
Showing 19 changed files with 612 additions and 459 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ npm-debug.log
package-lock.json
node_modules
coverage
.nyc_output
out
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ sudo: required

language: node_js
node_js:
- "6"
- "8"
- "9"
- "10"
- "12"

services:
- docker
Expand Down
31 changes: 0 additions & 31 deletions CONTRIBUTING.md

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Sumit Goel
Copyright (c) 2016-2019 Sumit Goel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
61 changes: 49 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
# Zabbix API Client (zabbix-promise)
# Zabbix API Client

[Zabbix](https://www.zabbix.com/) is an open source monitoring software for
networks and applications. It is designed to monitor and track the status of
various network services, servers, and hardware.
Zabbix is an open source monitoring software that can monitor pretty much everything like networks, servers, applications, etc. You may learn more about Zabbix at [www.zabbix.com](https://www.zabbix.com/).

**Zabbix-promise** is an abstract module written in JavaScript for
[Node.js >= 6](https://nodejs.org/) to interface with the
[Zabbix API >= 3.0](https://www.zabbix.com/documentation/3.0/manual/api) using
ES2015 native promises.
**Zabbix-promise** is a JavaScript package for Node.js runtime environment to interact with Zabbix APIs. The package is written to support JavaScript Promise and Async/await interfaces. Zabbix-promise implements Zabbix sender protocol in pure JavaScript code, and there are no other package dependencies, just the Node.js runtime.

The latest version of zabbix-promise supports all currently maintained Node versions, see [Node Release Schedule](https://github.com/nodejs/Release#release-schedule) and all currently supported Zabbix releases, see [Zabbix Life Cycle & Release Policy](https://www.zabbix.com/life_cycle_and_release_policy).

**Table of Contents**

<!-- toc -->

- [Install](#install)
- [Usage](#usage)
- [getHost](examples/getHost.js)
- [createHost](examples/createHost.js)
- [zabbixSender](examples/zabbixSender.js)
- [Debugging](#debugging)
- [Contributing](#contributing)
- [License](#license)

<!-- tocstop -->

## Install

`npm install zabbix-promise --save`
```js
$ npm install zabbix-promise --save
```

## Usage

## Examples
Please check the examples below to get started.

- [getHost](examples/getHost.js)
- [createHost](examples/createHost.js)
- [getEvents](examples/getEvents.js)
- [sendValues](examples/sendValues.js)
- [zabbixSender](examples/zabbixSender.js)

## Debugging

Zabbix-promsie uses [`debuglog`](https://nodejs.org/dist/latest/docs/api/util.html#util_util_debuglog_section), so just run with environmental variable `NODE_DEBUG` set to `zp*`.

```js
$ NODE_DEBUG=zp* node getHost.js
```

## Contributing

👋Thanks for thinking about contributing to zabbix-promise! There are a few ways you may contribute to the project.

1. Use the package in your projects and report any bugs you may find by filing issues.
2. Submit examples to cover the Zabbix APIs that are not in examples already by sending pull request.
3. Submit test cases to cover all or most of the Zabbix APIs by sending pull request.

## License

[MIT](LICENSE)

Copyright (c) 2016-2019 Sumit Goel.
29 changes: 4 additions & 25 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ services:
zabbix-server:
hostname: zabbix-server.local
image: zabbix/zabbix-server-pgsql:${ZABTAG}
ports:
- 0.0.0.0:10051:10051/tcp
environment:
DB_SERVER_HOST: zabbix-database
POSTGRES_USER: ${DBUSER}
Expand All @@ -20,7 +22,8 @@ services:
hostname: zabbix-web.local
image: zabbix/zabbix-web-nginx-pgsql:${ZABTAG}
ports:
- 0.0.0.0:${HOSTPORT}:80/tcp
- 0.0.0.0:8080:80/tcp
- 0.0.0.0:8443:443/tcp
environment:
ZBX_SERVER_HOST: zabbix-server
DB_SERVER_HOST: zabbix-database
Expand All @@ -31,27 +34,3 @@ services:
depends_on:
- 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
ZBX_SERVER_HOST: zabbix-server
depends_on:
- zabbix-server
62 changes: 36 additions & 26 deletions examples/createHost.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
/**
* In this example, we will create a Zabbix host "yet-another-host".
* We need at least one host group and interface details as this information
* is required to create a new host. We will query the host groups and get the
* last group id from the return values.
*/

const Zabbix = require('../index')

const whiteSpaceCount = 2
const zabbix = new Zabbix(
'http://127.0.0.1:8080/api_jsonrpc.php',
'Admin',
'zabbix'
)
const zabbix = new Zabbix({
url: 'http://127.0.0.1:8080/api_jsonrpc.php',
user: 'Admin',
password: 'zabbix'
})

zabbix.login()
.then(() => zabbix.request('host.create', {
'host': 'zabbix-agent1',
'groups': [{ 'groupid': '2' }],
'interfaces': [
{
'dns': 'zabbix-agent1',
'ip': '',
'main': 1,
'port': '10050',
'type': 1,
'useip': 0
}
],
'templates': [{ 'templateid': '10001' }],
'inventory_mode': 1
}))
.then((value) => console.log(JSON.stringify(value, null, whiteSpaceCount)))
.then(() => zabbix.logout())
.catch((reson) => console.log(JSON.stringify(reson, null, whiteSpaceCount)))
const main = async () => {
try {
await zabbix.login()
const groups = await zabbix.request('hostgroup.get', {})
const groupId = groups[groups.length - 1].groupid
const host = await zabbix.request('host.create', {
host: 'yet-another-host',
groups: [{ groupid: groupId }],
interfaces: [{
type: 1,
main: 1,
useip: 1,
ip: '127.0.0.1',
dns: '',
port: '10050'
}]
})
console.log(host)
zabbix.logout()
} catch (error) {
console.error(error)
}
}
main()
37 changes: 0 additions & 37 deletions examples/getEvents.js

This file was deleted.

43 changes: 26 additions & 17 deletions examples/getHost.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
/**
* In this example, we will query Zabbix hosts and apply a filter for host
* "zabbix-promise-host" and include the interfaces as well.
*/

const Zabbix = require('../index')

const whiteSpaceCount = 2
const zabbix = new Zabbix(
'http://127.0.0.1:8080/api_jsonrpc.php',
'Admin',
'zabbix'
)
const zabbix = new Zabbix({
url: 'http://127.0.0.1:8080/api_jsonrpc.php',
user: 'Admin',
password: 'zabbix'
})

zabbix.login()
.then(() => zabbix.request('host.get', {
'output': [
'hostid',
'host'
],
'limit': 1
}))
.then((value) => console.log(JSON.stringify(value, null, whiteSpaceCount)))
.then(() => zabbix.logout())
.catch((reson) => console.log(JSON.stringify(reson, null, whiteSpaceCount)))
const main = async () => {
try {
await zabbix.login()
const hosts = await zabbix.request('host.get', {
selectInterfaces: 'extend',
filter: {
host: 'zabbix-promise-host'
}
})
console.log(JSON.stringify(hosts, null, 2))
zabbix.logout()
} catch (error) {
console.error(error)
}
}
main()
10 changes: 0 additions & 10 deletions examples/sendValues.js

This file was deleted.

22 changes: 22 additions & 0 deletions examples/zabbixSender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* In this example, we will send values to Zabbix trapper item type. There is
* no need for Zabbix Sender binary as zabbix-promise package implements the
* sender protocol in native JavaScript/Node.js.
*/

const zabbix = require('../index')

const main = async () => {
try {
const result = await zabbix.sender({
server: '127.0.0.1',
host: 'zabbix-promise-host',
key: 'zabbix.promise.key',
value: Math.random()
})
console.log(result)
} catch (error) {
console.error(error)
}
}
main()
Loading

0 comments on commit e20f5c0

Please sign in to comment.