-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathserverInfo.ts
More file actions
73 lines (66 loc) · 2.01 KB
/
serverInfo.ts
File metadata and controls
73 lines (66 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { Request, Response } from 'express';
import { extension } from '@heyputer/backend/src/extensions';
import fs from 'fs/promises';
import os from 'os';
export const handleServerInfo = async (
_req: Request,
res: Response,
): Promise<void> => {
const osData = {
platform: os.platform(),
type: os.type(),
release: os.release(),
pretty: `${os.type()} ${os.release()}`,
};
const cpus = os.cpus();
const cpuData = {
model: cpus[0]?.model || 'Unknown',
cores: cpus.length,
};
const ramData = {
total: os.totalmem(),
free: os.freemem(),
totalGB: (os.totalmem() / 1073741824).toFixed(2),
freeGB: (os.freemem() / 1073741824).toFixed(2),
};
const uptimeSeconds = os.uptime();
const uptimeData = {
seconds: uptimeSeconds,
days: Math.floor(uptimeSeconds / 86400),
hours: Math.floor((uptimeSeconds % 86400) / 3600),
minutes: Math.floor((uptimeSeconds % 3600) / 60),
pretty: `${Math.floor(uptimeSeconds / 86400)}d ${Math.floor((uptimeSeconds % 86400) / 3600)}h ${Math.floor((uptimeSeconds % 3600) / 60)}m`,
};
let diskData: Record<string, string> = {
total: 'N/A',
free: 'N/A',
used: 'N/A',
};
try {
const stats = await fs.statfs('/');
const totalGB = (stats.blocks * stats.bsize) / 1073741824;
const freeGB = (stats.bfree * stats.bsize) / 1073741824;
const usedGB = (totalGB - freeGB).toFixed(2);
diskData = {
total: totalGB.toFixed(2),
free: freeGB.toFixed(2),
used: usedGB,
};
} catch (err) {
console.error('Disk stats error:', err);
}
res.json({
os: osData,
cpu: cpuData,
ram: ramData,
uptime: uptimeData,
disk: diskData,
loadavg: os.loadavg(),
hostname: os.hostname(),
});
};
extension.get(
'/serverInfo',
{ subdomain: 'api', adminOnly: true },
handleServerInfo,
);