<?php
// status_netwatch.php
session_start();
require('routeros_api.class.php');

header('Content-Type: application/json');

if (!isset($_SESSION['ip'], $_SESSION['user'], $_SESSION['pass'])) {
    http_response_code(401);
    echo json_encode(['error' => 'not_authenticated']);
    exit;
}

$API = new RouterosAPI();
$API->debug = false;

$out = ['status' => [], 'ts' => time()];

if ($API->connect($_SESSION['ip'], $_SESSION['user'], $_SESSION['pass'])) {
    $nw = $API->comm('/tool/netwatch/print');
    foreach ($nw as $row) {
        if (isset($row['host'], $row['status'])) {
            $out['status'][$row['host']] = strtolower($row['status']); // up / down
        }
    }
    $API->disconnect();
}

echo json_encode($out);
