From def081164d56b0e674da9b19ba27ac14fb291b54 Mon Sep 17 00:00:00 2001 From: ale Date: Wed, 11 Sep 2024 19:26:37 +0200 Subject: [PATCH] initial commit --- README.md | 23 ++++++ config.js | 15 ++++ index.js | 51 +++++++++++++ mapping.json | 204 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 12 +++ 5 files changed, 305 insertions(+) create mode 100644 README.md create mode 100644 config.js create mode 100644 index.js create mode 100644 mapping.json create mode 100644 package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..f2fd9c1 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# elastic-monit + +### Index [monit](https://git.manalejandro.com/ale/docker-monit) metrics into `ElasticSearch` node to do stats and more. + +## Install + +``` +$ npm install or yarn +``` + +## Config + +Edit `config.js` file with your settings. + +## Run + +``` +$ node index.js +``` + +## License + +MIT \ No newline at end of file diff --git a/config.js b/config.js new file mode 100644 index 0000000..863800c --- /dev/null +++ b/config.js @@ -0,0 +1,15 @@ +module.exports = { + elasticnode: 'http://elastic-node:9200', + index: 'monit', + monitnodes: [ + '192.168.2.12', + '192.168.2.3', + '192.168.2.2', + '192.168.2.9', + '192.168.2.11' + ], + username: 'admin', + password: 'monit', + timeout: 4000, + poll: 30000 +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..78f2f5c --- /dev/null +++ b/index.js @@ -0,0 +1,51 @@ +const { XMLParser } = require("fast-xml-parser"), + { Client } = require('@elastic/elasticsearch'), + config = require('./config.js'), + parser = new XMLParser({ ignoreDeclaration: true, ignorePiTags: true }), + client = new Client({ node: config.elasticnode }), + monitnodes = config.monitnodes, + username = config.username, + password = config.password, + request = async uri => { + const ac = new AbortController() + setTimeout(() => { + ac.abort() + }, 5000) + const response = await fetch(uri, { + signal: ac.signal, + keepalive: false, + timeout: config.timeout, + headers: { + Authorization: `Basic ${btoa(`${username}:${password}`)}` + } + }), + xml = await response.text() + setImmediate(() => { ac.abort() }) + return xml + }, + index = async (node, data) => { + await client.index({ + index: config.index, + body: { + data, + node, + timestamp: new Date() + } + }) + }, + start = async () => { + await Promise.all(monitnodes.map(async node => { + try { + const xml = await request(`http://${node}:2812/_status?format=xml`), + json = parser.parse(xml) + await index(node, json) + } catch (e) { + console.error(e) + } + })) + setTimeout(async () => await start(), config.poll) + } + +(async () => { + await start() +})() diff --git a/mapping.json b/mapping.json new file mode 100644 index 0000000..3d620e9 --- /dev/null +++ b/mapping.json @@ -0,0 +1,204 @@ +{ + "mappings": { + "properties": { + "data": { + "properties": { + "monit": { + "properties": { + "platform": { + "properties": { + "cpu": { + "type": "long" + }, + "machine": { + "type": "keyword" + }, + "memory": { + "type": "long" + }, + "name": { + "type": "keyword" + }, + "release": { + "type": "keyword" + }, + "swap": { + "type": "long" + }, + "version": { + "type": "keyword" + } + } + }, + "server": { + "properties": { + "controlfile": { + "type": "keyword" + }, + "httpd": { + "properties": { + "address": { + "type": "keyword" + }, + "port": { + "type": "long" + }, + "ssl": { + "type": "long" + } + } + }, + "id": { + "type": "keyword" + }, + "incarnation": { + "type": "long" + }, + "localhostname": { + "type": "keyword" + }, + "poll": { + "type": "long" + }, + "startdelay": { + "type": "long" + }, + "uptime": { + "type": "long" + }, + "version": { + "type": "keyword" + } + } + }, + "service": { + "properties": { + "boottime": { + "type": "long" + }, + "collected_sec": { + "type": "long" + }, + "collected_usec": { + "type": "long" + }, + "filedescriptors": { + "properties": { + "allocated": { + "type": "long" + }, + "maximum": { + "type": "text" + }, + "unused": { + "type": "long" + } + } + }, + "monitor": { + "type": "long" + }, + "monitormode": { + "type": "long" + }, + "name": { + "type": "keyword" + }, + "onreboot": { + "type": "long" + }, + "pendingaction": { + "type": "long" + }, + "status": { + "type": "long" + }, + "status_hint": { + "type": "long" + }, + "system": { + "properties": { + "cpu": { + "properties": { + "guest": { + "type": "long" + }, + "guestnice": { + "type": "long" + }, + "hardirq": { + "type": "long" + }, + "nice": { + "type": "long" + }, + "softirq": { + "type": "float" + }, + "steal": { + "type": "long" + }, + "system": { + "type": "float" + }, + "user": { + "type": "float" + }, + "wait": { + "type": "float" + } + } + }, + "load": { + "properties": { + "avg01": { + "type": "float" + }, + "avg05": { + "type": "float" + }, + "avg15": { + "type": "float" + } + } + }, + "memory": { + "properties": { + "kilobyte": { + "type": "long" + }, + "percent": { + "type": "float" + } + } + }, + "swap": { + "properties": { + "kilobyte": { + "type": "long" + }, + "percent": { + "type": "float" + } + } + } + } + }, + "uptime": { + "type": "long" + } + } + } + } + } + } + }, + "node": { + "type": "keyword" + }, + "timestamp": { + "type": "date" + } + } + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..7efd0a6 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "elastic-monit", + "version": "1.0.0", + "main": "index.js", + "author": "ale", + "license": "MIT", + "private": true, + "dependencies": { + "@elastic/elasticsearch": "^8.15.0", + "fast-xml-parser": "^4.4.1" + } +}