diff --git a/api/routes/monitor.ts b/api/routes/monitor.ts index 8eba688..76a12fd 100644 --- a/api/routes/monitor.ts +++ b/api/routes/monitor.ts @@ -1,7 +1,7 @@ /** * Monitor API Route * GET /api/monitor - Get all monitoring data - * POST /api/monitor/check-sites - Trigger site availability check + * Site status comes from CWP collector (collect-sites.sh) via sync-to-db.sh * @author Descomplicar® | @link descomplicar.pt | @copyright 2026 */ import { Router } from 'express' @@ -21,21 +21,4 @@ router.get('/', async (_req: Request, res: Response) => { } }) -// Trigger site availability check -router.post('/check-sites', async (_req: Request, res: Response) => { - try { - console.log('[Monitor] Manual site check triggered') - const result = await monitoringService.checkAllSitesAvailability() - res.json({ - success: true, - message: 'Site check completed', - ...result, - timestamp: new Date().toISOString() - }) - } catch (error) { - console.error('Site check error:', error) - res.status(500).json({ error: 'Internal server error', message: (error as Error).message }) - } -}) - export default router diff --git a/api/scripts/check-sites.ts b/api/scripts/check-sites.ts deleted file mode 100644 index 4503931..0000000 --- a/api/scripts/check-sites.ts +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env npx tsx -/** - * Site Availability Checker Script - * Run via cron every 5-15 minutes to check if sites are online - * - * Usage: - * npx tsx api/scripts/check-sites.ts - * - * Cron example (every 10 minutes): - * 0,10,20,30,40,50 * * * * cd /path/to/DashDescomplicar && npx tsx api/scripts/check-sites.ts - * - * @author Descomplicar | @link descomplicar.pt | @copyright 2026 - */ -import 'dotenv/config' -import { checkAllSitesAvailability } from '../services/monitoring.js' - -async function main() { - const startTime = Date.now() - console.log(`[${new Date().toISOString()}] Starting site availability check...`) - - try { - const result = await checkAllSitesAvailability() - - console.log(`[${new Date().toISOString()}] Check completed:`) - console.log(` - Sites checked: ${result.checked}`) - console.log(` - Up: ${result.up}`) - console.log(` - Down: ${result.down}`) - - // Log any down sites - const downSites = result.results.filter(r => !r.available) - if (downSites.length > 0) { - console.log(` - Down sites:`) - for (const site of downSites) { - console.log(` - ${site.name}: ${site.error || 'No response'}`) - } - } - - const elapsed = Date.now() - startTime - console.log(`[${new Date().toISOString()}] Done in ${elapsed}ms`) - - // Exit with code 1 if any sites are down (useful for alerting) - process.exit(downSites.length > 0 ? 1 : 0) - - } catch (error) { - console.error(`[${new Date().toISOString()}] Error:`, error) - process.exit(2) - } -} - -main() diff --git a/api/services/monitoring.ts b/api/services/monitoring.ts index 6a38bef..1dbb90f 100644 --- a/api/services/monitoring.ts +++ b/api/services/monitoring.ts @@ -9,12 +9,9 @@ interface MonitoringItem { id: number name: string category: string - type: string status: string details: any last_check: string - created_at: string - updated_at: string } interface CategorySummary { @@ -25,105 +22,6 @@ interface CategorySummary { critical: number } -/** - * Check if a URL is accessible (HTTP HEAD request) - */ -export async function checkSiteAvailability(url: string, timeout = 10000): Promise<{ - available: boolean - statusCode?: number - responseTime?: number - error?: string -}> { - const startTime = Date.now() - const controller = new AbortController() - const timeoutId = setTimeout(() => controller.abort(), timeout) - - try { - const response = await fetch(url, { - method: 'HEAD', - signal: controller.signal, - headers: { - 'User-Agent': 'Descomplicar-Monitor/1.0' - } - }) - - clearTimeout(timeoutId) - const responseTime = Date.now() - startTime - - return { - available: response.ok || response.status < 500, - statusCode: response.status, - responseTime - } - } catch (error) { - clearTimeout(timeoutId) - return { - available: false, - error: (error as Error).message, - responseTime: Date.now() - startTime - } - } -} - -/** - * Check all sites and update their availability status - */ -export async function checkAllSitesAvailability(): Promise<{ - checked: number - up: number - down: number - results: any[] -}> { - // Get all sites from monitoring table - const [sites] = await db.query(` - SELECT id, name, details FROM tbl_eal_monitoring - WHERE category = 'site' - `) - - const results: any[] = [] - let up = 0 - let down = 0 - - for (const site of sites) { - const details = typeof site.details === 'string' ? JSON.parse(site.details) : site.details - const siteUrl = details?.site_url || `https://${site.name}` - - const check = await checkSiteAvailability(siteUrl) - - // Update status if site is down - if (!check.available) { - await db.query( - 'UPDATE tbl_eal_monitoring SET status = ?, last_check = NOW() WHERE id = ?', - ['down', site.id] - ) - down++ - } else { - // If was down and now is up, set to 'up' (will be replaced by plugin data later) - const currentStatus = details?.health?.status || 'ok' - if (currentStatus === 'down') { - await db.query( - 'UPDATE tbl_eal_monitoring SET status = ?, last_check = NOW() WHERE id = ?', - ['up', site.id] - ) - } - up++ - } - - results.push({ - name: site.name, - url: siteUrl, - ...check - }) - } - - return { - checked: sites.length, - up, - down, - results - } -} - export async function getMonitoringData() { // Get all items const [items] = await db.query(`