system_information.js 4.3 KB

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