system_information.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if ((container.Names[0].slice(1) != 'DweebUI') && (container.Names[0].slice(1) != 'DweebCache')) {
  26. let imageVersion = container.Image.split('/');
  27. let service = imageVersion[imageVersion.length - 1].split(':')[0];
  28. let containerId = docker.getContainer(container.Id);
  29. let containerInfo = await containerId.inspect();
  30. // Get ports //////////////////////////
  31. let ports_list = [];
  32. for (const [key, value] of Object.entries(containerInfo.HostConfig.PortBindings)) {
  33. let ports = {
  34. check : 'checked',
  35. external: value[0].HostPort,
  36. internal: key.split('/')[0],
  37. protocol: key.split('/')[1]
  38. }
  39. ports_list.push(ports);
  40. }
  41. for (let i = 0; i < 12; i++) {
  42. if (ports_list[i] == undefined) {
  43. let ports = {
  44. check : '',
  45. external: '',
  46. internal: '',
  47. protocol: ''
  48. }
  49. ports_list[i] = ports;
  50. }
  51. } /////////////////////////////////////
  52. // Get volumes ////////////////////////
  53. let volumes_list = [];
  54. for (const [key, value] of Object.entries(containerInfo.HostConfig.Binds)) {
  55. let volumes = {
  56. check : 'checked',
  57. bind: value.split(':')[0],
  58. container: value.split(':')[1],
  59. readwrite: value.split(':')[2]
  60. }
  61. volumes_list.push(volumes);
  62. }
  63. for (let i = 0; i < 12; i++) {
  64. if (volumes_list[i] == undefined) {
  65. let volumes = {
  66. check : '',
  67. bind: '',
  68. container: '',
  69. readwrite: ''
  70. }
  71. volumes_list[i] = volumes;
  72. }
  73. } /////////////////////////////////////
  74. // Get environment variables.
  75. let environment_variables = [];
  76. for (const [key, value] of Object.entries(containerInfo.Config.Env)) {
  77. let env = {
  78. check : 'checked',
  79. name: value.split('=')[0],
  80. default: value.split('=')[1]
  81. }
  82. environment_variables.push(env);
  83. }
  84. for (let i = 0; i < 12; i++) {
  85. if (environment_variables[i] == undefined) {
  86. let env = {
  87. check : '',
  88. name: '',
  89. default: ''
  90. }
  91. environment_variables[i] = env;
  92. }
  93. }
  94. // Get labels.
  95. let labels = [];
  96. for (const [key, value] of Object.entries(containerInfo.Config.Labels)) {
  97. let label = {
  98. check : 'checked',
  99. name: key,
  100. value: value
  101. }
  102. labels.push(label);
  103. }
  104. for (let i = 0; i < 12; i++) {
  105. if (labels[i] == undefined) {
  106. let label = {
  107. check : '',
  108. name: '',
  109. value: ''
  110. }
  111. labels[i] = label;
  112. }
  113. }
  114. let container_info = {
  115. name: container.Names[0].slice(1),
  116. service: service,
  117. id: container.Id,
  118. state: container.State,
  119. image: container.Image,
  120. external_port: ports_list[0].external || 0,
  121. internal_port: ports_list[0].internal || 0,
  122. ports: ports_list,
  123. volumes: volumes_list,
  124. environment_variables: environment_variables,
  125. labels: labels,
  126. }
  127. let dockerCard = dashCard(container_info);
  128. card_list += dockerCard;
  129. }
  130. }
  131. return card_list;
  132. }
  133. module.exports.containerStats = async function () {
  134. let container_stats = [];
  135. const data = await docker.listContainers({ all: true });
  136. for (const container of data) {
  137. if ((container.Names[0].slice(1) != 'DweebUI') && (container.Names[0].slice(1) != 'DweebCache')) {
  138. const stats = await dockerContainerStats(container.Id);
  139. let container_stat = {
  140. name: container.Names[0].slice(1),
  141. cpu: Math.round(stats[0].cpuPercent),
  142. ram: Math.round(stats[0].memPercent)
  143. }
  144. //push stats to an array
  145. container_stats.push(container_stat);
  146. }
  147. }
  148. return container_stats;
  149. }
  150. module.exports.containerAction = async function (data) {
  151. let { user, role, action, container, state } = data;
  152. console.log(`${user} wants to: ${action} ${container}`);
  153. if (role == 'admin') {
  154. var containerName = docker.getContainer(container);
  155. if ((action == 'start') && (state == 'stopped')) {
  156. containerName.start();
  157. } else if ((action == 'start') && (state == 'paused')) {
  158. containerName.unpause();
  159. } else if ((action == 'stop') && (state != 'stopped')) {
  160. containerName.stop();
  161. } else if ((action == 'pause') && (state == 'running')) {
  162. containerName.pause();
  163. } else if ((action == 'pause') && (state == 'paused')) {
  164. containerName.unpause();
  165. } else if (action == 'restart') {
  166. containerName.restart();
  167. }
  168. } else {
  169. console.log('User is not an admin');
  170. }
  171. }