├── 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 |CPU Usage: %
14 |RAM Usage: %
15 |Network Upload: MB/s
16 |Network Download: MB/s
17 |CPU Usage: %
21 |RAM Usage: %
22 |Network Upload: MB/s
23 |Network Download: MB/s
24 |CPU Usage: %
28 |RAM Usage: %
29 |Network Upload: MB/s
30 |Network Download: MB/s
31 |CPU Usage: %
35 |RAM Usage: %
36 |Network Upload: MB/s
37 |Network Download: MB/s
38 |