52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import { Router } from 'express'
|
|
import axios from 'axios'
|
|
import https from 'https'
|
|
|
|
const router = Router()
|
|
const agent = new https.Agent({ rejectUnauthorized: false })
|
|
|
|
router.get('/status', async (_req, res) => {
|
|
try {
|
|
const host = process.env.PROXMOX_HOST
|
|
const token = process.env.PROXMOX_TOKEN
|
|
if (!host || !token) {
|
|
res.status(503).json({ error: 'PROXMOX_HOST / PROXMOX_TOKEN not configured' })
|
|
return
|
|
}
|
|
|
|
const headers = { Authorization: token }
|
|
|
|
const nodesRes = await axios.get(`${host}/api2/json/nodes`, { headers, httpsAgent: agent })
|
|
const node: string = nodesRes.data.data[0].node
|
|
|
|
const [statusRes, lxcRes, qemuRes, storageRes] = await Promise.all([
|
|
axios.get(`${host}/api2/json/nodes/${node}/status`, { headers, httpsAgent: agent }),
|
|
axios.get(`${host}/api2/json/nodes/${node}/lxc`, { headers, httpsAgent: agent }),
|
|
axios.get(`${host}/api2/json/nodes/${node}/qemu`, { headers, httpsAgent: agent }),
|
|
axios.get(`${host}/api2/json/nodes/${node}/storage`, { headers, httpsAgent: agent }),
|
|
])
|
|
|
|
const s = statusRes.data.data
|
|
|
|
type StorageEntry = { storage: string; type: string; active: number; enabled: number; used: number; total: number }
|
|
const storages = (storageRes.data.data as StorageEntry[])
|
|
.filter(st => st.active && st.enabled && st.total > 0 && st.storage !== 'nas')
|
|
.map(st => ({ name: st.storage, type: st.type, used: st.used, total: st.total }))
|
|
|
|
res.json({
|
|
node,
|
|
uptime: s.uptime,
|
|
cpu: s.cpu,
|
|
memory: { used: s.memory.used, total: s.memory.total },
|
|
storages,
|
|
lxcCount: (lxcRes.data.data as { status: string }[]).filter(c => c.status === 'running').length,
|
|
vmCount: (qemuRes.data.data as { status: string }[]).filter(v => v.status === 'running').length,
|
|
})
|
|
} catch (err: unknown) {
|
|
const msg = err instanceof Error ? err.message : 'Unknown error'
|
|
res.status(500).json({ error: msg })
|
|
}
|
|
})
|
|
|
|
export default router
|