Merge remote-tracking branch 'xo-server-transport-nagios/master'
This commit is contained in:
commit
37a4e108be
65
packages/xo-server-transport-nagios/.editorconfig
Normal file
65
packages/xo-server-transport-nagios/.editorconfig
Normal file
@ -0,0 +1,65 @@
|
||||
# http://EditorConfig.org
|
||||
#
|
||||
# Julien Fontanet's configuration
|
||||
# https://gist.github.com/julien-f/8096213
|
||||
|
||||
# Top-most EditorConfig file.
|
||||
root = true
|
||||
|
||||
# Common config.
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespaces = true
|
||||
|
||||
# CoffeeScript
|
||||
#
|
||||
# https://github.com/polarmobile/coffeescript-style-guide/blob/master/README.md
|
||||
[*.{,lit}coffee]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
|
||||
# Markdown
|
||||
[*.{md,mdwn,mdown,markdown}]
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
|
||||
# Package.json
|
||||
#
|
||||
# This indentation style is the one used by npm.
|
||||
[/package.json]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
|
||||
# Jade
|
||||
[*.jade]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
|
||||
# JavaScript
|
||||
#
|
||||
# Two spaces seems to be the standard most common style, at least in
|
||||
# Node.js (http://nodeguide.com/style.html#tabs-vs-spaces).
|
||||
[*.{js,jsx,ts,tsx}]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
|
||||
# Less
|
||||
[*.less]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
|
||||
# Sass
|
||||
#
|
||||
# Style used for http://libsass.com
|
||||
[*.s[ac]ss]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
|
||||
# YAML
|
||||
#
|
||||
# Only spaces are allowed.
|
||||
[*.yaml]
|
||||
indent_size = 2
|
||||
indent_style = space
|
7
packages/xo-server-transport-nagios/.gitignore
vendored
Normal file
7
packages/xo-server-transport-nagios/.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
/dist/
|
||||
/node_modules/
|
||||
|
||||
npm-debug.log
|
||||
npm-debug.log.*
|
||||
pnpm-debug.log
|
||||
pnpm-debug.log.*
|
10
packages/xo-server-transport-nagios/.npmignore
Normal file
10
packages/xo-server-transport-nagios/.npmignore
Normal file
@ -0,0 +1,10 @@
|
||||
/examples/
|
||||
example.js
|
||||
example.js.map
|
||||
*.example.js
|
||||
*.example.js.map
|
||||
|
||||
/test/
|
||||
/tests/
|
||||
*.spec.js
|
||||
*.spec.js.map
|
9
packages/xo-server-transport-nagios/.travis.yml
Normal file
9
packages/xo-server-transport-nagios/.travis.yml
Normal file
@ -0,0 +1,9 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- stable
|
||||
- 6
|
||||
- 4
|
||||
|
||||
# Use containers.
|
||||
# http://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
||||
sudo: false
|
57
packages/xo-server-transport-nagios/README.md
Normal file
57
packages/xo-server-transport-nagios/README.md
Normal file
@ -0,0 +1,57 @@
|
||||
# xo-server-transport-nagios [](https://travis-ci.org/vatesfr/xo-server-transport-nagios)
|
||||
|
||||
> ${pkg.description}
|
||||
|
||||
## Install
|
||||
|
||||
Installation of the [npm package](https://npmjs.org/package/xo-server-transport-nagios):
|
||||
|
||||
```
|
||||
> npm install --global xo-server-transport-nagios
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Like all other xo-server plugins, it can be configured directly via
|
||||
the web interface, see [the plugin documentation](https://xen-orchestra.com/docs/plugins.html).
|
||||
|
||||
## Development
|
||||
|
||||
### `Xo#sendPassiveCheck( { status, message }) `
|
||||
|
||||
This xo method is called to send a passive check to nagios and change the status of a service.
|
||||
It has two parameters:
|
||||
- status: it's the service status in Nagios (0: OK | 1: WARNING | 2: CRITICAL).
|
||||
- message: it's the status information in Nagios.
|
||||
|
||||
```
|
||||
# Install dependencies
|
||||
> npm install
|
||||
|
||||
# Run the tests
|
||||
> npm test
|
||||
|
||||
# Continuously compile
|
||||
> npm run dev
|
||||
|
||||
# Continuously run the tests
|
||||
> npm run dev-test
|
||||
|
||||
# Build for production (automatically called by npm install)
|
||||
> npm run build
|
||||
```
|
||||
|
||||
## Contributions
|
||||
|
||||
Contributions are *very* welcomed, either on the documentation or on
|
||||
the code.
|
||||
|
||||
You may:
|
||||
|
||||
- report any [issue](https://github.com/vatesfr/xo-server-transport-nagios/issues)
|
||||
you've encountered;
|
||||
- fork and create a pull request.
|
||||
|
||||
## License
|
||||
|
||||
AGPL3 © [Vates SAS](https://vates.fr)
|
87
packages/xo-server-transport-nagios/package.json
Normal file
87
packages/xo-server-transport-nagios/package.json
Normal file
@ -0,0 +1,87 @@
|
||||
{
|
||||
"name": "xo-server-transport-nagios",
|
||||
"version": "0.1.0",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"keywords": [
|
||||
"nagios",
|
||||
"orchestra",
|
||||
"plugin",
|
||||
"xen",
|
||||
"xen-orchestra",
|
||||
"xo-server"
|
||||
],
|
||||
"homepage": "https://github.com/vatesfr/xo-server-transport-nagios",
|
||||
"bugs": "https://github.com/vatesfr/xo-server-transport-nagios/issues",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/vatesfr/xo-server-transport-nagios.git"
|
||||
},
|
||||
"author": {
|
||||
"name": "Badr Azizbi",
|
||||
"email": "badr.azizbi@vates.fr"
|
||||
},
|
||||
"preferGlobal": false,
|
||||
"main": "dist/",
|
||||
"bin": {},
|
||||
"files": [
|
||||
"dist/"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel-runtime": "^6.20.0",
|
||||
"buffer-crc32": "^0.2.13"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.18.0",
|
||||
"babel-eslint": "^7.1.1",
|
||||
"babel-plugin-transform-runtime": "^6.15.0",
|
||||
"babel-preset-env": "^1.1.4",
|
||||
"babel-preset-stage-3": "^6.16.0",
|
||||
"cross-env": "^3.1.3",
|
||||
"dependency-check": "^2.6.0",
|
||||
"ghooks": "^1.3.2",
|
||||
"rimraf": "^2.5.4",
|
||||
"standard": "^8.6.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "cross-env NODE_ENV=production babel --source-maps --out-dir=dist/ src/",
|
||||
"clean": "rimraf dist/",
|
||||
"depcheck": "dependency-check ./package.json",
|
||||
"dev": "cross-env NODE_ENV=development babel --watch --source-maps --out-dir=dist/ src/",
|
||||
"lint": "standard",
|
||||
"posttest": "npm run lint && npm run depcheck",
|
||||
"prebuild": "npm run clean",
|
||||
"predev": "npm run clean",
|
||||
"prepublish": "npm run build"
|
||||
},
|
||||
"babel": {
|
||||
"plugins": [
|
||||
"transform-runtime"
|
||||
],
|
||||
"presets": [
|
||||
[
|
||||
"env",
|
||||
{
|
||||
"targets": {
|
||||
"node": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"stage-3"
|
||||
]
|
||||
},
|
||||
"config": {
|
||||
"ghooks": {
|
||||
"commit-msg": "npm test"
|
||||
}
|
||||
},
|
||||
"standard": {
|
||||
"ignore": [
|
||||
"dist"
|
||||
],
|
||||
"parser": "babel-eslint"
|
||||
}
|
||||
}
|
169
packages/xo-server-transport-nagios/src/index.js
Normal file
169
packages/xo-server-transport-nagios/src/index.js
Normal file
@ -0,0 +1,169 @@
|
||||
import crc32 from 'buffer-crc32'
|
||||
import net from 'net'
|
||||
import { Buffer } from 'buffer'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
export const configurationSchema = {
|
||||
type: 'object',
|
||||
|
||||
properties: {
|
||||
server: {
|
||||
type: 'string',
|
||||
description: 'The nagios server adress'
|
||||
},
|
||||
port: {
|
||||
type: 'integer',
|
||||
description: 'The NSCA port'
|
||||
},
|
||||
key: {
|
||||
type: 'string',
|
||||
description: 'The encryption key'
|
||||
},
|
||||
host: {
|
||||
type: 'string',
|
||||
description: 'The host name in Nagios'
|
||||
},
|
||||
service: {
|
||||
type: 'string',
|
||||
description: 'The service description in Nagios'
|
||||
}
|
||||
},
|
||||
additionalProperties: false,
|
||||
required: ['server', 'port', 'key', 'host', 'service']
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
||||
const bind = (fn, thisArg) => function __bound__ () {
|
||||
return fn.apply(thisArg, arguments)
|
||||
}
|
||||
|
||||
function nscaPacketBuilder ({
|
||||
host,
|
||||
iv,
|
||||
message,
|
||||
service,
|
||||
status,
|
||||
timestamp
|
||||
}) {
|
||||
// Building NSCA packet
|
||||
const SIZE = 720
|
||||
const packet = new Buffer(SIZE)
|
||||
packet.fill(0)
|
||||
packet.writeInt16BE(VERSION, 0)
|
||||
packet.fill('h', 2, 3)
|
||||
packet.writeUInt32BE(0, 4) // initial 0 for CRC32 value
|
||||
packet.writeUInt32BE(timestamp, 8)
|
||||
packet.writeInt16BE(status, 12)
|
||||
packet.write(host, 14, 77, ENCODING)
|
||||
packet.write(service, 78, 206, ENCODING)
|
||||
packet.write(message, 206, SIZE, ENCODING)
|
||||
packet.writeUInt32BE(crc32.unsigned(packet), 4)
|
||||
return packet
|
||||
}
|
||||
|
||||
function xor (data, mask) {
|
||||
const dataSize = data.length
|
||||
const maskSize = mask.length
|
||||
const result = new Buffer(dataSize)
|
||||
let j = 0
|
||||
for (let i = 0; i < dataSize; i++) {
|
||||
if (j === maskSize) {
|
||||
j = 0
|
||||
}
|
||||
result[i] = data[i] ^ mask[j]
|
||||
j++
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
||||
export const OK = 0
|
||||
export const WARNING = 1
|
||||
export const CRITICAL = 2
|
||||
|
||||
const VERSION = 3
|
||||
const ENCODING = 'binary'
|
||||
|
||||
class XoServerNagios {
|
||||
|
||||
constructor ({ xo }) {
|
||||
this._sendPassiveCheck = bind(this._sendPassiveCheck, this)
|
||||
this._set = bind(xo.defineProperty, xo)
|
||||
this._unset = null
|
||||
|
||||
// Defined in configure().
|
||||
this._conf = null
|
||||
this._key = null
|
||||
}
|
||||
|
||||
configure (configuration) {
|
||||
this._conf = configuration
|
||||
this._key = new Buffer(configuration.key, ENCODING)
|
||||
}
|
||||
|
||||
load () {
|
||||
this._unset = this._set('sendPassiveCheck', this._sendPassiveCheck)
|
||||
}
|
||||
|
||||
unload () {
|
||||
this._unset()
|
||||
}
|
||||
|
||||
test () {
|
||||
return this._sendPassiveCheck({
|
||||
message: 'The server-nagios plugin for Xen Orchestra server seems to be working fine, nicely done :)',
|
||||
status: OK
|
||||
})
|
||||
}
|
||||
|
||||
_sendPassiveCheck ({
|
||||
message,
|
||||
status
|
||||
}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (/\r|\n/.test(message)) {
|
||||
throw new Error('the message must not contain a line break')
|
||||
}
|
||||
|
||||
const client = new net.Socket()
|
||||
|
||||
client.connect(this._conf.port, this._conf.server, () => {
|
||||
console.log('Successful connection')
|
||||
})
|
||||
|
||||
client.on('data', data => {
|
||||
const timestamp = data.readInt32BE(128)
|
||||
const iv = data.slice(0, 128) // initialization vector
|
||||
const packet = nscaPacketBuilder({
|
||||
...this._conf,
|
||||
iv,
|
||||
message,
|
||||
status,
|
||||
timestamp
|
||||
})
|
||||
|
||||
// 1) Using xor between the NSCA packet and the initialization vector
|
||||
// 2) Using xor between the result of the first operation and the encryption key
|
||||
const xorPacketBuffer = xor(
|
||||
xor(
|
||||
packet,
|
||||
iv
|
||||
),
|
||||
this._key
|
||||
)
|
||||
|
||||
client.write(xorPacketBuffer, res => {
|
||||
client.destroy()
|
||||
resolve(res)
|
||||
})
|
||||
})
|
||||
|
||||
client.on('error', reject)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default opts => new XoServerNagios(opts)
|
2554
packages/xo-server-transport-nagios/yarn.lock
Normal file
2554
packages/xo-server-transport-nagios/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user