implementation (#1)
This commit is contained in:
parent
e2ba1fa7f8
commit
0c497900a2
@ -12,11 +12,18 @@ Installation of the [npm package](https://npmjs.org/package/xo-server-nagios):
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
**TODO**
|
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
|
## 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
|
# Install dependencies
|
||||||
> npm install
|
> npm install
|
||||||
|
|
||||||
|
@ -31,10 +31,14 @@
|
|||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=4"
|
"node": ">=4"
|
||||||
},
|
},
|
||||||
"dependencies": {},
|
"dependencies": {
|
||||||
|
"babel-runtime": "^6.18.0",
|
||||||
|
"buffer-crc32": "^0.2.13"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel-cli": "^6.18.0",
|
"babel-cli": "^6.18.0",
|
||||||
"babel-eslint": "^7.1.0",
|
"babel-eslint": "^7.1.0",
|
||||||
|
"babel-plugin-transform-runtime": "^6.15.0",
|
||||||
"babel-preset-latest": "^6.16.0",
|
"babel-preset-latest": "^6.16.0",
|
||||||
"babel-preset-stage-0": "^6.16.0",
|
"babel-preset-stage-0": "^6.16.0",
|
||||||
"cross-env": "^3.1.3",
|
"cross-env": "^3.1.3",
|
||||||
@ -55,6 +59,9 @@
|
|||||||
"prepublish": "npm run build"
|
"prepublish": "npm run build"
|
||||||
},
|
},
|
||||||
"babel": {
|
"babel": {
|
||||||
|
"plugins": [
|
||||||
|
"transform-runtime"
|
||||||
|
],
|
||||||
"presets": [
|
"presets": [
|
||||||
"latest",
|
"latest",
|
||||||
"stage-0"
|
"stage-0"
|
||||||
|
@ -1,26 +1,164 @@
|
|||||||
|
import crc32 from 'buffer-crc32'
|
||||||
|
import net from 'net'
|
||||||
|
import { Buffer } from 'buffer'
|
||||||
|
|
||||||
|
// ===================================================================
|
||||||
|
|
||||||
export const configurationSchema = {
|
export const configurationSchema = {
|
||||||
foo: {
|
type: 'object',
|
||||||
required: true,
|
|
||||||
title: 'Foo',
|
properties: {
|
||||||
type: 'string'
|
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']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ===================================================================
|
||||||
|
|
||||||
|
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 {
|
class XoServerNagios {
|
||||||
|
|
||||||
constructor ({ xo }) {
|
constructor ({ xo }) {
|
||||||
this._xo = xo
|
this._sendPassiveCheck = ::this._sendPassiveCheck
|
||||||
|
this._set = ::xo.defineProperty
|
||||||
|
this._unset = null
|
||||||
|
|
||||||
|
// Defined in configure().
|
||||||
|
this._conf = null
|
||||||
|
this._key = null
|
||||||
}
|
}
|
||||||
|
|
||||||
configure (configuration) {
|
configure (configuration) {
|
||||||
throw new Error('not implemented')
|
this._conf = configuration
|
||||||
|
this._key = new Buffer(configuration.key, ENCODING)
|
||||||
}
|
}
|
||||||
|
|
||||||
load () {
|
load () {
|
||||||
throw new Error('not implemented')
|
this._unset = this._set('sendPassiveCheck', this._sendPassiveCheck)
|
||||||
}
|
}
|
||||||
|
|
||||||
unload () {
|
unload () {
|
||||||
throw new Error('not implemented')
|
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)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user