Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- 'use strict';
- const config = {
- host: '123.45.67.89',
- port: 28016,
- pass: 'CHANGEME',
- pollinterval: 500
- };
- const Websocket = require('ws');
- const async = require('async');
- const Influx = require('influx');
- const once = require('once');
- const influx = new Influx.InfluxDB({
- database: 'influxdb',
- host: 'localhost',
- port: 8086,
- username: 'username',
- password: 'password',
- schema: [{
- measurement: 'rust',
- tags: [
- 'Hostname',
- 'Map'
- ],
- fields: {
- MaxPlayers: Influx.FieldType.INTEGER,
- Players: Influx.FieldType.INTEGER,
- Queued: Influx.FieldType.INTEGER,
- Joining: Influx.FieldType.INTEGER,
- EntityCount: Influx.FieldType.INTEGER,
- Uptime: Influx.FieldType.INTEGER,
- Framerate: Influx.FieldType.INTEGER,
- Memory: Influx.FieldType.INTEGER,
- Collections: Influx.FieldType.INTEGER,
- NetworkIn: Influx.FieldType.INTEGER,
- NetworkOut: Influx.FieldType.INTEGER,
- Restarting: Influx.FieldType.BOOLEAN
- }
- }]
- });
- const cargo = async.cargo((buffer, cb) => influx.writePoints(buffer).then(cb).catch(cb));
- const poll = () => {
- const reopen = once(poll.bind(null));
- const ws = new Websocket(`ws://${config.host}:${config.port}/${config.pass}`);
- let timer = null;
- ws.on('close', () => {
- clearInterval(timer);
- timer = null;
- console.log('Websocket was closed');
- reopen();
- });
- ws.on('error', e => {
- clearInterval(timer);
- timer = null;
- console.log('Error emitted on Websocket:', e);
- reopen();
- });
- ws.on('message', msg => {
- let data = null;
- try {
- data = JSON.parse(JSON.parse(msg).Message);
- } catch(e) {
- // do nothing
- }
- if (!data || !data.Hostname || !data.Map) return;
- cargo.push({
- measurement: 'rust',
- tags: {
- Hostname: data.Hostname,
- Map: data.Map
- }, fields: {
- MaxPlayers: data.MaxPlayers,
- Players: data.Players,
- Queued: data.Queued,
- Joining: data.Joining,
- EntityCount: data.EntityCount,
- Uptime: data.Uptime,
- Framerate: data.Framerate,
- Memory: data.Memory,
- Collections: data.Collections,
- NetworkIn: data.NetworkIn,
- NetworkOut: data.NetworkOut,
- Restarting: data.Restarting
- },
- timestamp: new Date()
- });
- });
- ws.on('open', () => {
- timer = setInterval(() => {
- ws.send(JSON.stringify({
- Message: 'serverinfo',
- Name: 'InfluxDB'
- }));
- }, config.pollinterval);
- });
- };
- poll();
Add Comment
Please, Sign In to add comment