update: health check info

This commit is contained in:
Ben Allfree 2024-01-23 12:02:34 +00:00
parent a3e4a52c53
commit cb68bc3251

View File

@ -76,6 +76,7 @@ type Check = {
port?: number port?: number
ago?: string ago?: string
mem?: string mem?: string
created?: Date
} }
const containers = _exec(`docker ps --format '{{json .}}'`) const containers = _exec(`docker ps --format '{{json .}}'`)
@ -87,18 +88,19 @@ const containers = _exec(`docker ps --format '{{json .}}'`)
.map((line) => JSON.parse(line) as DockerPs) .map((line) => JSON.parse(line) as DockerPs)
.map<Check>((rec) => { .map<Check>((rec) => {
const name = rec.Names.replace(/-\d+/, '') const name = rec.Names.replace(/-\d+/, '')
const port = parseInt(rec.Ports.match(/:(\d+)/)?.[1] || '0', 10) const port = parseInt(rec.Ports.match(/:(\d+)/)?.[1] || '0', 10)
const mem = rec.Size.match(/(\d+MB)/)?.[1] || '0MB' const mem = rec.Size.match(/(\d+MB)/)?.[1] || '0MB'
const created = new Date(rec.CreatedAt)
return { return {
name, name,
priority: 0, priority: 0,
emoji: ':guitar:', emoji: ':octopus:',
port, port,
isHealthy: false, isHealthy: false,
url: `http://localhost:${port}/api/health`, url: `http://localhost:${port}/api/health`,
ago: rec.RunningFor, ago: rec.RunningFor,
mem, mem,
created,
} }
}) })
@ -150,29 +152,67 @@ function getFreeMemoryInGB(): string {
return freeMemoryGB.toFixed(2) // Rounds to 2 decimal places return freeMemoryGB.toFixed(2) // Rounds to 2 decimal places
} }
await fetch(DISCORD_URL, { const content = [
method: 'POST', `===================`,
body: JSON.stringify({ `Server: SFO-1`,
content: [ `${new Date()}`,
`===================`, `Free RAM: ${getFreeMemoryInGB()}`,
`Server: SFO-1`, `Free disk: ${freeSpace}`,
`${new Date()}`, `${checks.length} containers running and ${openFiles.length} open files.`,
`Free RAM: ${getFreeMemoryInGB()}`, ...checks
`Free disk: ${freeSpace}`, .sort((a, b) => {
`${checks.length} containers running and ${openFiles.length} open files.`, if (a.priority > b.priority) return -1
...checks if (a.priority < b.priority) return 1
.sort((a, b) => { const now = new Date()
if (a.priority > b.priority) return -1 return +(b.created || now) - +(a.created || now)
if (a.priority < b.priority) return 1 return a.name.localeCompare(b.name)
return a.name.localeCompare(b.name) })
}) .map(({ name, isHealthy, emoji, mem, ago }) => {
.map( const isInstance = !!mem
({ name, isHealthy, emoji, mem, ago }) => if (isInstance) {
`${ return `${
isHealthy ? ':white_check_mark:' : ':face_vomiting: ' isHealthy ? ':white_check_mark:' : ':face_vomiting: '
} ${emoji} ${name} ${mem || ''} ${ago || ''}`, } ${emoji} \`${name.padStart(20)} ${(mem || '').padStart(10)} ${
), ago || ''
].join(`\n`), }\``
}
return `${
isHealthy ? ':white_check_mark:' : ':face_vomiting: '
} ${emoji} ${name}`
}),
]
function splitIntoChunks(lines: string[], maxChars: number = 2000): string[] {
const chunks: string[] = []
let currentChunk: string = ''
lines.forEach((line) => {
// Check if adding the next line exceeds the maxChars limit
if (currentChunk.length + line.length + 1 > maxChars) {
chunks.push(currentChunk)
currentChunk = ''
}
currentChunk += line + '\n' // Add the line and a newline character
})
// Add the last chunk if it's not empty
if (currentChunk) {
chunks.push(currentChunk)
}
return chunks
}
dbg(content)
await Promise.all(
splitIntoChunks(content).map(async (content) => {
await fetch(DISCORD_URL, {
method: 'POST',
body: JSON.stringify({
content,
}),
headers: { 'content-type': 'application/json' },
})
}), }),
headers: { 'content-type': 'application/json' }, )
})