initial commit

This commit is contained in:
ale 2024-09-11 19:26:37 +02:00
commit def081164d
5 changed files with 305 additions and 0 deletions

23
README.md Normal file
View File

@ -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

15
config.js Normal file
View File

@ -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
}

51
index.js Normal file
View File

@ -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()
})()

204
mapping.json Normal file
View File

@ -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"
}
}
}
}

12
package.json Normal file
View File

@ -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"
}
}