DataNote / notes /553eb83e-d0db-4f98-9916-6a4e0801d7d2.json
marriedtermiteblyi's picture
put notes/553eb83e-d0db-4f98-9916-6a4e0801d7d2.json
250f58d verified
{
"id": "553eb83e-d0db-4f98-9916-6a4e0801d7d2",
"title": "upt.js",
"content": "const os = require('os');\r\nconst fs = require('fs').promises;\r\nconst path = require('path');\r\nconst { execSync } = require('child_process');\r\nconst moment = require('moment-timezone');\r\nconst pidusage = require('pidusage');\r\n\r\nconst formatBytes = (bytes) => {\r\n const units = ['B', 'KB', 'MB', 'GB', 'TB'];\r\n if (bytes === 0) return '0 B';\r\n const i = Math.floor(Math.log(bytes) / Math.log(1024));\r\n return (bytes / 1024 ** i).toFixed(1) + ' ' + units[i];\r\n};\r\n\r\nconst makeBar = (percent, total = 13) => {\r\n const full = Math.round(percent * total);\r\n return '█'.repeat(full) + '░'.repeat(total - full);\r\n};\r\n\r\nconst getFolderSize = async (dir) => {\r\n let size = 0;\r\n const stack = [dir];\r\n const ignore = ['node_modules', '.git'];\r\n while (stack.length) {\r\n const cur = stack.pop();\r\n try {\r\n const stat = await fs.stat(cur);\r\n if (stat.isFile()) size += stat.size;\r\n else if (stat.isDirectory() && !ignore.includes(path.basename(cur))) {\r\n const items = await fs.readdir(cur);\r\n for (const i of items) stack.push(path.join(cur, i));\r\n }\r\n } catch {}\r\n }\r\n return size;\r\n};\r\n\r\nconst getDiskInfo = () => {\r\n try {\r\n const platform = os.platform();\r\n if (platform === 'win32') {\r\n const output = execSync('wmic logicaldisk get size,freespace,caption').toString().split('\\n');\r\n const line = output.find(l => l.trim().startsWith('C:'));\r\n if (!line) return null;\r\n const parts = line.trim().split(/\\s+/);\r\n const free = parseInt(parts[1]);\r\n const total = parseInt(parts[2]);\r\n const used = total - free;\r\n return {\r\n total: formatBytes(total),\r\n used: formatBytes(used),\r\n free: formatBytes(free),\r\n usage: `${makeBar(used / total)} ${Math.round((used / total) * 100)}%`\r\n };\r\n } else {\r\n const out = execSync('df -k --output=size,used,avail,target -x tmpfs -x devtmpfs').toString().split('\\n')[1];\r\n const [size, used, avail] = out.trim().split(/\\s+/).map(v => parseInt(v));\r\n return {\r\n total: formatBytes(size * 1024),\r\n used: formatBytes(used * 1024),\r\n free: formatBytes(avail * 1024),\r\n usage: `${makeBar(used / size)} ${Math.round((used / size) * 100)}%`\r\n };\r\n }\r\n } catch {\r\n return null;\r\n }\r\n};\r\n\r\nmodule.exports = {\r\n config: {\r\n name: \"upt\",\r\n version: \"2.0\",\r\n hasPermission: 1,\r\n credits: \"vutuan + gpt\",\r\n description: \"Giám sát toàn bộ hệ thống bot\",\r\n commandCategory: \"Admin\",\r\n usages: \"\",\r\n cooldowns: 5\r\n },\r\n\r\n run: async ({ api, event, Users }) => {\r\n const name = await Users?.getNameUser(event.senderID) || 'Admin';\r\n const now = moment().tz('Asia/Ho_Chi_Minh');\r\n const nodeVer = process.version;\r\n const pid = process.pid;\r\n\r\n const mem = process.memoryUsage();\r\n const totalMem = os.totalmem();\r\n const freeMem = os.freemem();\r\n const usedMem = totalMem - freeMem;\r\n\r\n const cpus = os.cpus();\r\n const load = os.loadavg();\r\n const uptime = process.uptime();\r\n const sysUp = os.uptime();\r\n\r\n const [d, h, m, s] = [\r\n Math.floor(uptime / 86400),\r\n Math.floor(uptime / 3600) % 24,\r\n Math.floor(uptime / 60) % 60,\r\n Math.floor(uptime) % 60\r\n ];\r\n\r\n const folderSize = await getFolderSize('./');\r\n const pkgCount = (() => {\r\n try {\r\n const json = require(path.resolve('package.json'));\r\n return Object.keys(json.dependencies || {}).length;\r\n } catch {\r\n return 0;\r\n }\r\n })();\r\n\r\n const disk = getDiskInfo();\r\n const start = Date.now();\r\n await new Promise(r => setTimeout(r, 5));\r\n const ping = Date.now() - start;\r\n\r\n const cpuUsage = await pidusage(pid);\r\n\r\n const netInterfaces = os.networkInterfaces();\r\n const netCount = Object.keys(netInterfaces).length;\r\n\r\n const msg = [\r\n `=== GIÁM SÁT HỆ THỐNG ===`,\r\n `🕒 Thời gian : ${now.format(\"DD/MM/YYYY HH:mm:ss\")}`,\r\n `👤 Người gọi : ${name}`,\r\n ``,\r\n `--- Thời gian ---`,\r\n `• Uptime bot : ${d}d ${h}h ${m}m ${s}s`,\r\n `• Uptime máy : ${Math.floor(sysUp / 3600)}h`,\r\n ``,\r\n `--- Phiên bản ---`,\r\n `• Node.js : ${nodeVer}`,\r\n `• PID : ${pid}`,\r\n `• Packages : ${pkgCount}`,\r\n `• Dung lượng bot: ${formatBytes(folderSize)}`,\r\n `• Thư mục chạy : ${process.cwd()}`,\r\n ``,\r\n `--- RAM ---`,\r\n `• Tổng RAM : ${formatBytes(totalMem)}`,\r\n `• Đã dùng : ${formatBytes(usedMem)} (${makeBar(usedMem / totalMem)} ${Math.round((usedMem / totalMem) * 100)}%)`,\r\n `• RSS : ${formatBytes(mem.rss)}`,\r\n `• Heap : ${formatBytes(mem.heapUsed)} / ${formatBytes(mem.heapTotal)}`,\r\n `• External : ${formatBytes(mem.external)}`,\r\n `• ArrayBuffers : ${formatBytes(mem.arrayBuffers)}`,\r\n ``,\r\n `--- CPU ---`,\r\n `• Model : ${cpus[0].model}`,\r\n `• Số lõi : ${cpus.length}`,\r\n `• Tốc độ : ${cpus[0].speed} MHz`,\r\n `• Load avg : ${load.map(x => x.toFixed(2)).join(' | ')}`,\r\n `• CPU Bot : ${cpuUsage.cpu.toFixed(1)}%`,\r\n ``,\r\n `--- Ổ ĐĨA ---`,\r\n disk ? [\r\n `• Tổng : ${disk.total}`,\r\n `• Đã dùng : ${disk.used}`,\r\n `• Còn trống : ${disk.free}`,\r\n `• Sử dụng : ${disk.usage}`\r\n ].join('\\n') : 'Không lấy được thông tin ổ đĩa',\r\n ``,\r\n `--- Hệ điều hành ---`,\r\n `• Tên HĐH : ${os.type()} ${os.release()} (${os.arch()})`,\r\n `• Nền tảng : ${os.platform()}`,\r\n `• Người dùng : ${process.env.USER || process.env.USERNAME}`,\r\n `• Network : ${netCount} adapter(s)`,\r\n ``,\r\n `--- Ping ---`,\r\n `• Độ trễ : -9${ping}ms`\r\n ].join('\\n');\r\n\r\n api.sendMessage(msg, event.threadID, event.messageID);\r\n }\r\n};",
"language": "javascript",
"createdAt": 1757125411392,
"updatedAt": 1757125456799
}