| const express = require('express'); |
| const os = require('os'); |
| const process = require('process'); |
| const geoip = require('geoip-lite'); |
|
|
| const app = express(); |
| const port = 3000; |
|
|
| |
| app.set('json spaces', 4); |
|
|
| |
| const bytesToBinaryMiB = (bytes) => Math.floor(bytes / (1024 * 1024)) + 'MiB'; |
|
|
| |
| const secondsToDhms = (seconds) => { |
| seconds = Number(seconds); |
| const d = Math.floor(seconds / (3600 * 24)); |
| const h = Math.floor((seconds % (3600 * 24)) / 3600); |
| const m = Math.floor((seconds % 3600) / 60); |
| const s = Math.floor(seconds % 60); |
| return `${d} days ${h} hours ${m} minutes ${s} seconds`; |
| }; |
|
|
| |
| app.get('/', (req, res) => { |
| const runtime = secondsToDhms(process.uptime()); |
| res.setHeader('Content-Type', 'application/json'); |
| res.status(200).send(JSON.stringify({ |
| status: 200, |
| message: `Server is running, uptime: ${runtime}` |
| }, null, 4)); |
| }); |
|
|
| |
| app.get('/specs', (req, res) => { |
| const networkInterfaces = os.networkInterfaces(); |
| const specs = { |
| system: { |
| platform: os.platform(), |
| release: os.release(), |
| arch: os.arch(), |
| uptime: secondsToDhms(os.uptime()), |
| loadavg: os.loadavg() |
| }, |
| memory: { |
| totalmem: bytesToBinaryMiB(os.totalmem()), |
| freemem: bytesToBinaryMiB(os.freemem()) |
| }, |
| cpu: { |
| cpus: os.cpus() |
| }, |
| network: { |
| networkInterfaces: networkInterfaces, |
| ipv4Info: {} |
| }, |
| user: { |
| homedir: os.homedir(), |
| hostname: os.hostname(), |
| userInfo: os.userInfo() |
| }, |
| process: { |
| pid: process.pid, |
| version: process.version, |
| title: process.title, |
| argv: process.argv, |
| memoryUsage: process.memoryUsage(), |
| cwd: process.cwd(), |
| execPath: process.execPath, |
| platform: process.platform, |
| uptime: secondsToDhms(process.uptime()), |
| env: process.env |
| } |
| }; |
|
|
| |
| Object.keys(networkInterfaces).forEach((key) => { |
| specs.network.ipv4Info[key] = networkInterfaces[key] |
| .filter((iface) => iface.family === 'IPv4') |
| .map((iface) => { |
| const geo = geoip.lookup(iface.address); |
| return { |
| address: iface.address, |
| netmask: iface.netmask, |
| mac: iface.mac, |
| internal: iface.internal, |
| country: geo ? geo.country : null, |
| region: geo ? geo.region : null, |
| city: geo ? geo.city : null, |
| ll: geo ? geo.ll : null |
| }; |
| }); |
| }); |
|
|
| res.setHeader('Content-Type', 'application/json'); |
| res.send(specs); |
| }); |
|
|
| |
| app.listen(port, () => { |
| console.log(`Server is running on http://localhost:${port}`); |
| }); |
|
|