system_information.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const { currentLoad, mem, networkStats, fsSize, dockerContainerStats } = require('systeminformation');
  2. var Docker = require('dockerode');
  3. var docker = new Docker({ socketPath: '/var/run/docker.sock' });
  4. const { dashCard } = require('../components/dashCard');
  5. module.exports.serverStats = async function () {
  6. const cpuUsage = await currentLoad();
  7. const ramUsage = await mem();
  8. const netUsage = await networkStats();
  9. const diskUsage = await fsSize();
  10. const info = {
  11. cpu: Math.round(cpuUsage.currentLoad),
  12. ram: Math.round((ramUsage.active / ramUsage.total) * 100),
  13. tx: netUsage[0].tx_bytes,
  14. rx: netUsage[0].rx_bytes,
  15. disk: diskUsage[0].use,
  16. };
  17. return info;
  18. }
  19. module.exports.containerList = async function () {
  20. let card_list = '';
  21. const data = await docker.listContainers({ all: true });
  22. for (const container of data) {
  23. let imageVersion = container.Image.split('/');
  24. let service = imageVersion[imageVersion.length - 1].split(':')[0];
  25. let containerId = docker.getContainer(container.Id);
  26. let containerInfo = await containerId.inspect();
  27. let open_ports = [];
  28. let external_port = 0;
  29. let internal_port = 0;
  30. for (const [key, value] of Object.entries(containerInfo.HostConfig.PortBindings)) {
  31. open_ports.push(`${value[0].HostPort}`);
  32. external_port = value[0].HostPort;
  33. internal_port = key;
  34. if ((external_port == undefined) || (internal_port == undefined)) {
  35. external_port = 0;
  36. internal_port = 0;
  37. }
  38. }
  39. let volumes = [];
  40. for (const [key, value] of Object.entries(containerInfo.Mounts)) {
  41. volumes.push(`${value.Source}: ${value.Destination}: ${value.RW}`);
  42. }
  43. let environment_variables = [];
  44. for (const [key, value] of Object.entries(containerInfo.Config.Env)) {
  45. environment_variables.push(`${key}: ${value}`);
  46. }
  47. let labels = [];
  48. for (const [key, value] of Object.entries(containerInfo.Config.Labels)) {
  49. labels.push(`${key}: ${value}`);
  50. }
  51. let container_info = {
  52. name: container.Names[0].slice(1),
  53. service: service,
  54. id: container.Id,
  55. state: container.State,
  56. image: container.Image,
  57. external_port: external_port,
  58. internal_port: internal_port
  59. }
  60. let dockerCard = dashCard(container_info);
  61. card_list += dockerCard;
  62. }
  63. return card_list;
  64. }
  65. module.exports.containerStats = async function () {
  66. let container_stats = [];
  67. const data = await docker.listContainers({ all: true });
  68. for (const container of data) {
  69. const stats = await dockerContainerStats(container.Id);
  70. let container_stat = {
  71. name: container.Names[0].slice(1),
  72. cpu: Math.round(stats[0].cpuPercent),
  73. ram: Math.round(stats[0].memPercent)
  74. }
  75. //push stats to an array
  76. container_stats.push(container_stat);
  77. }
  78. return container_stats;
  79. }
  80. module.exports.containerAction = async function (data) {
  81. let { user, role, action, container, state } = data;
  82. console.log(`${user} wants to: ${action} ${container}`);
  83. if (role == 'admin') {
  84. var containerName = docker.getContainer(container);
  85. if ((action == 'start') && (state == 'stopped')) {
  86. containerName.start();
  87. } else if ((action == 'start') && (state == 'paused')) {
  88. containerName.unpause();
  89. } else if ((action == 'stop') && (state != 'stopped')) {
  90. containerName.stop();
  91. } else if ((action == 'pause') && (state == 'running')) {
  92. containerName.pause();
  93. } else if ((action == 'pause') && (state == 'paused')) {
  94. containerName.unpause();
  95. } else if (action == 'restart') {
  96. containerName.restart();
  97. }
  98. } else {
  99. console.log('User is not an admin');
  100. }
  101. }