├── README.md ├── api.js ├── index.html ├── main.css └── script.js /README.md: -------------------------------------------------------------------------------- 1 | ## System Monitor using Node.js API 2 | 3 | This project enables you to monitor system metrics using API in Node.js. The system metrics include CPU usage, memory usage, disk usage, network usage, and system uptime. 4 | 5 | ### Installation 6 | 7 | To get started, run the following commands in your terminal: 8 | 9 | ```shell 10 | curl -sL https://deb.nodesource.com/setup_16.x | sudo bash - 11 | sudo apt -y install nodejs 12 | npm i express os-utils systeminformation 13 | ``` 14 | 15 | After installing the necessary packages, open 'api.js' and replace 'eth0' with your network adapter. 16 | 17 | Then, open 'script.js' and replace 'https://api.yourdomain.com/server1' with your server APIs. 18 | 19 | ### Reverse Proxy 20 | 21 | We recommend making a reverse proxy using Nginx to hide your backend server IPs: 22 | 23 | ```conf 24 | server { 25 | listen 80; 26 | server_name api.yourdomain.com; 27 | location /server1 { 28 | add_header Access-Control-Allow-Origin *; 29 | proxy_pass http://backend:3000/metrics; 30 | } 31 | 32 | location /server2 { 33 | add_header Access-Control-Allow-Origin *; 34 | proxy_pass http://backend:3000/metrics; 35 | } 36 | 37 | location /server3 { 38 | add_header Access-Control-Allow-Origin *; 39 | proxy_pass http://backend:3000/metrics; 40 | } 41 | 42 | location /server4 { 43 | add_header Access-Control-Allow-Origin *; 44 | proxy_pass http://backend:3000/metrics; 45 | } 46 | } 47 | ``` 48 | 49 | 50 | Replace `'http://backend:3000/metrics'` with your own backend server URL. 51 | 52 | ### Running the Server 53 | 54 | Once you've completed these steps, you can run 'node api.js' to start the server and begin monitoring your system metrics. 55 | 56 | -------------------------------------------------------------------------------- /api.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const os = require('os'); 3 | const osUtils = require('os-utils'); 4 | const si = require('systeminformation'); 5 | 6 | //settings 7 | const port = 3000; 8 | const nic = 'eth0'; 9 | 10 | const app = express(); 11 | app.use(express.json()); 12 | 13 | app.post(`/metrics`, async (req, res) => { 14 | try { 15 | const memoryUsage = 100 - (os.freemem() / os.totalmem() * 100); 16 | const cpu = await getCpuUsage(); 17 | const network = await getNetworkUsage(nic); 18 | res.json({ 19 | status: "success", 20 | memoryUsage: parseFloat(memoryUsage.toFixed(2)), 21 | cpuUsage: parseFloat((cpu * 100).toFixed(2)), 22 | networkUpload: parseFloat((network.tx_sec / 125000).toFixed(2)), 23 | networkDownload: parseFloat((network.rx_sec / 125000).toFixed(2)) 24 | }); 25 | } catch (err) { 26 | console.error(err); 27 | res.json({ 28 | status: "failed" 29 | }); 30 | } 31 | }); 32 | 33 | app.listen(port, () => console.log(`System/Analytics API started successfully on port ${port}`)); 34 | 35 | function getCpuUsage() { 36 | return new Promise((resolve, reject) => { 37 | osUtils.cpuUsage((value) => { 38 | resolve(value); 39 | }); 40 | }); 41 | } 42 | 43 | async function getNetworkUsage(name) { 44 | const stats = await si.networkStats(name); 45 | return { 46 | rx_sec: stats[0].rx_sec, 47 | tx_sec: stats[0].tx_sec 48 | }; 49 | } 50 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Server Load Status 5 | 6 | 7 | 8 |

Server Load Status

9 |
10 |
11 |
12 |

Server 1

13 |

CPU Usage: %

14 |

RAM Usage: %

15 |

Network Upload: MB/s

16 |

Network Download: MB/s

17 |
18 |
19 |

Server 2

20 |

CPU Usage: %

21 |

RAM Usage: %

22 |

Network Upload: MB/s

23 |

Network Download: MB/s

24 |
25 |
26 |

Server 3

27 |

CPU Usage: %

28 |

RAM Usage: %

29 |

Network Upload: MB/s

30 |

Network Download: MB/s

31 |
32 |
33 |

Server 4

34 |

CPU Usage: %

35 |

RAM Usage: %

36 |

Network Upload: MB/s

37 |

Network Download: MB/s

38 |
39 |
40 | 41 | 42 | -------------------------------------------------------------------------------- /main.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #1a1a1a; 3 | color: white; 4 | font-family: Arial, sans-serif; 5 | } 6 | 7 | .container { 8 | display: flex; 9 | flex-wrap: wrap; 10 | justify-content: space-between; 11 | } 12 | 13 | .box { 14 | width: 20%; 15 | margin-bottom: 20px; 16 | padding: 20px; 17 | border-radius: 10px; 18 | box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.1); 19 | background-color: #333; 20 | } 21 | 22 | .box h2 { 23 | font-size: 20px; 24 | margin-top: 0; 25 | } 26 | 27 | .box p { 28 | font-size: 16px; 29 | margin: 10px 0; 30 | } -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | const url1 = "https://api.yourdomain.com/server1"; 2 | const url2 = "https://api.yourdomain.com/server2"; 3 | const url3 = "https://api.yourdomain.com/server3"; 4 | const url4 = "https://api.yourdomain.com/server4"; 5 | 6 | function fetchData() { 7 | fetch(url1, { 8 | method: "POST" 9 | }) 10 | .then((response) => response.json()) 11 | .then((data) => updateBoxData("server1", data)); 12 | fetch(url2, { 13 | method: "POST" 14 | }) 15 | .then((response) => response.json()) 16 | .then((data) => updateBoxData("server2", data)); 17 | fetch(url3, { 18 | method: "POST" 19 | }) 20 | .then((response) => response.json()) 21 | .then((data) => updateBoxData("server3", data)); 22 | fetch(url4, { 23 | method: "POST" 24 | }) 25 | .then((response) => response.json()) 26 | .then((data) => updateBoxData("server4", data)); 27 | } 28 | 29 | function updateBoxData(boxId, data) { 30 | const box = document.getElementById(boxId); 31 | const cpuElem = box.querySelector(`#${boxId}-cpu`); 32 | const ramElem = box.querySelector(`#${boxId}-ram`); 33 | const uploadElem = box.querySelector(`#${boxId}-upload`); 34 | const downloadElem = box.querySelector(`#${boxId}-download`); 35 | 36 | cpuElem.innerText = data.cpuUsage; 37 | ramElem.innerText = data.memoryUsage; 38 | uploadElem.innerText = data.networkUpload; 39 | downloadElem.innerText = data.networkDownload; 40 | } 41 | 42 | fetchData(); 43 | setInterval(fetchData, 2000); --------------------------------------------------------------------------------