#!/usr/bin/php
<?php

require_once __DIR__ . '/config/init.php';

$jsonFile = __DIR__ . '/web_check.json';

if (!file_exists($jsonFile)) {
    echo "CRITICAL - JSON file not found\n";
    exit(2);
}

$data = json_decode(file_get_contents($jsonFile), true);
if (!$data) {
    echo "CRITICAL - Invalid JSON\n";
    exit(2);
}

$problems = [];

foreach ($data as $site) {
    if ($site['sslcheck_ok'] != 1) {
        $problems[] = $site['domain'];
    }
}

if (!empty($problems)) {
    echo "CRITICAL - SSL problems on: " . implode(', ', $problems) . "\n";
    exit(2);
}

echo "OK - All SSL certificates valid\n";
exit(0);
