Guest User

Untitled

a guest
Nov 29th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. 'use strict';
  4.  
  5. const config = {
  6. host: '123.45.67.89',
  7. port: 28016,
  8. pass: 'CHANGEME',
  9. pollinterval: 500
  10. };
  11.  
  12. const Websocket = require('ws');
  13. const async = require('async');
  14. const Influx = require('influx');
  15. const once = require('once');
  16.  
  17. const influx = new Influx.InfluxDB({
  18. database: 'influxdb',
  19. host: 'localhost',
  20. port: 8086,
  21. username: 'username',
  22. password: 'password',
  23. schema: [{
  24. measurement: 'rust',
  25. tags: [
  26. 'Hostname',
  27. 'Map'
  28. ],
  29. fields: {
  30. MaxPlayers: Influx.FieldType.INTEGER,
  31. Players: Influx.FieldType.INTEGER,
  32. Queued: Influx.FieldType.INTEGER,
  33. Joining: Influx.FieldType.INTEGER,
  34. EntityCount: Influx.FieldType.INTEGER,
  35. Uptime: Influx.FieldType.INTEGER,
  36. Framerate: Influx.FieldType.INTEGER,
  37. Memory: Influx.FieldType.INTEGER,
  38. Collections: Influx.FieldType.INTEGER,
  39. NetworkIn: Influx.FieldType.INTEGER,
  40. NetworkOut: Influx.FieldType.INTEGER,
  41. Restarting: Influx.FieldType.BOOLEAN
  42. }
  43. }]
  44. });
  45.  
  46. const cargo = async.cargo((buffer, cb) => influx.writePoints(buffer).then(cb).catch(cb));
  47.  
  48. const poll = () => {
  49. const reopen = once(poll.bind(null));
  50. const ws = new Websocket(`ws://${config.host}:${config.port}/${config.pass}`);
  51. let timer = null;
  52. ws.on('close', () => {
  53. clearInterval(timer);
  54. timer = null;
  55. console.log('Websocket was closed');
  56. reopen();
  57. });
  58. ws.on('error', e => {
  59. clearInterval(timer);
  60. timer = null;
  61. console.log('Error emitted on Websocket:', e);
  62. reopen();
  63. });
  64. ws.on('message', msg => {
  65. let data = null;
  66. try {
  67. data = JSON.parse(JSON.parse(msg).Message);
  68. } catch(e) {
  69. // do nothing
  70. }
  71. if (!data || !data.Hostname || !data.Map) return;
  72. cargo.push({
  73. measurement: 'rust',
  74. tags: {
  75. Hostname: data.Hostname,
  76. Map: data.Map
  77. }, fields: {
  78. MaxPlayers: data.MaxPlayers,
  79. Players: data.Players,
  80. Queued: data.Queued,
  81. Joining: data.Joining,
  82. EntityCount: data.EntityCount,
  83. Uptime: data.Uptime,
  84. Framerate: data.Framerate,
  85. Memory: data.Memory,
  86. Collections: data.Collections,
  87. NetworkIn: data.NetworkIn,
  88. NetworkOut: data.NetworkOut,
  89. Restarting: data.Restarting
  90. },
  91. timestamp: new Date()
  92. });
  93. });
  94. ws.on('open', () => {
  95. timer = setInterval(() => {
  96. ws.send(JSON.stringify({
  97. Message: 'serverinfo',
  98. Name: 'InfluxDB'
  99. }));
  100. }, config.pollinterval);
  101. });
  102. };
  103.  
  104. poll();
Add Comment
Please, Sign In to add comment