The checkAllSitesAvailability() function did HEAD requests from EasyPanel to check sites. Many WordPress sites block HEAD or return errors, causing all sites to show as DOWN while keeping valid response times from the CWP collector. The CWP collector (collect-sites.sh) is the single source of truth for site status. Removed: - checkSiteAvailability() and checkAllSitesAvailability() from monitoring service - POST /api/monitor/check-sites endpoint - api/scripts/check-sites.ts cron script Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
/**
|
|
* Monitoring Queries Service
|
|
* @author Descomplicar® | @link descomplicar.pt | @copyright 2026
|
|
*/
|
|
import db from '../db.js'
|
|
import type { RowDataPacket } from 'mysql2'
|
|
|
|
interface MonitoringItem {
|
|
id: number
|
|
name: string
|
|
category: string
|
|
status: string
|
|
details: any
|
|
last_check: string
|
|
}
|
|
|
|
interface CategorySummary {
|
|
category: string
|
|
total: number
|
|
ok: number
|
|
warning: number
|
|
critical: number
|
|
}
|
|
|
|
export async function getMonitoringData() {
|
|
// Get all items
|
|
const [items] = await db.query<RowDataPacket[]>(`
|
|
SELECT * FROM tbl_eal_monitoring
|
|
ORDER BY category, name
|
|
`)
|
|
|
|
// Get summary by category
|
|
const [summary] = await db.query<RowDataPacket[]>(`
|
|
SELECT
|
|
category,
|
|
COUNT(*) as total,
|
|
SUM(CASE WHEN status IN ('ok','up') THEN 1 ELSE 0 END) as ok,
|
|
SUM(CASE WHEN status = 'warning' THEN 1 ELSE 0 END) as warning,
|
|
SUM(CASE WHEN status IN ('failed','down') THEN 1 ELSE 0 END) as critical
|
|
FROM tbl_eal_monitoring
|
|
GROUP BY category
|
|
`)
|
|
|
|
// Parse details JSON and cast to MonitoringItem
|
|
const itemsParsed: MonitoringItem[] = items.map(item => ({
|
|
...item,
|
|
details: typeof item.details === 'string' ? JSON.parse(item.details) : item.details
|
|
} as MonitoringItem))
|
|
|
|
// Organize by category
|
|
const data: Record<string, MonitoringItem[]> = {}
|
|
for (const item of itemsParsed) {
|
|
if (!data[item.category]) {
|
|
data[item.category] = []
|
|
}
|
|
data[item.category].push(item)
|
|
}
|
|
|
|
// Calculate overall status
|
|
let overall: 'ok' | 'warning' | 'critical' = 'ok'
|
|
let total_critical = 0
|
|
let total_warning = 0
|
|
let total_ok = 0
|
|
|
|
for (const s of summary as CategorySummary[]) {
|
|
// MySQL pode retornar strings, converter para número
|
|
total_critical += Number(s.critical) || 0
|
|
total_warning += Number(s.warning) || 0
|
|
total_ok += Number(s.ok) || 0
|
|
}
|
|
|
|
if (total_critical > 0) overall = 'critical'
|
|
else if (total_warning > 0) overall = 'warning'
|
|
|
|
return {
|
|
items: data,
|
|
summary,
|
|
overall,
|
|
stats: {
|
|
total_critical,
|
|
total_warning,
|
|
total_ok
|
|
}
|
|
}
|
|
}
|