<?php
header('Content-Type: application/json; charset=utf-8');

$host = isset($_GET['host']) ? trim((string)$_GET['host']) : '';
$community = isset($_GET['community']) ? trim((string)$_GET['community']) : 'public';

$out = [
    'ok' => true,
    'php_version' => PHP_VERSION,
    'snmp_functions' => [
        'snmp2_real_walk' => function_exists('snmp2_real_walk'),
        'snmprealwalk' => function_exists('snmprealwalk'),
        'snmp2_walk' => function_exists('snmp2_walk'),
        'snmpwalk' => function_exists('snmpwalk'),
        'snmp2_get' => function_exists('snmp2_get'),
        'snmpget' => function_exists('snmpget'),
    ],
    'host' => $host,
    'community' => $community,
    'tests' => [],
];

if ($host === '') {
    $out['ok'] = false;
    $out['error'] = 'missing host';
    echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

if (function_exists('snmp_set_quick_print')) { @snmp_set_quick_print(true); }
if (function_exists('snmp_set_enum_print')) { @snmp_set_enum_print(false); }
if (function_exists('snmp_set_oid_numeric_print')) { @snmp_set_oid_numeric_print(true); }

$timeoutUs = 1200000;
$retries = 0;

$tests = [
    ['name' => 'sysDescr', 'oid' => '.1.3.6.1.2.1.1.1.0', 'mode' => 'get'],
    ['name' => 'ifName', 'oid' => '.1.3.6.1.2.1.31.1.1.1.1', 'mode' => 'walk'],
    ['name' => 'ifDescr', 'oid' => '.1.3.6.1.2.1.2.2.1.2', 'mode' => 'walk'],
    ['name' => 'dot1dBasePortIfIndex', 'oid' => '.1.3.6.1.2.1.17.1.4.1.2', 'mode' => 'walk'],
    ['name' => 'dot1dTpFdbPort', 'oid' => '.1.3.6.1.2.1.17.4.3.1.2', 'mode' => 'walk'],
    ['name' => 'dot1dTpFdbAddress', 'oid' => '.1.3.6.1.2.1.17.4.3.1.1', 'mode' => 'walk'],
];

foreach ($tests as $t) {
    $entry = ['name' => $t['name'], 'oid' => $t['oid'], 'mode' => $t['mode'], 'ok' => false];
    try {
        if ($t['mode'] === 'get') {
            if (function_exists('snmp2_get')) {
                $res = @snmp2_get($host, $community, $t['oid'], $timeoutUs, $retries);
            } elseif (function_exists('snmpget')) {
                $res = @snmpget($host, $community, $t['oid'], $timeoutUs, $retries);
            } else {
                $res = false;
            }
            $entry['result'] = $res === false ? false : (string)$res;
            $entry['ok'] = $res !== false;
        } else {
            if (function_exists('snmp2_real_walk')) {
                $res = @snmp2_real_walk($host, $community, $t['oid'], $timeoutUs, $retries);
            } elseif (function_exists('snmprealwalk')) {
                $res = @snmprealwalk($host, $community, $t['oid'], $timeoutUs, $retries);
            } else {
                $res = false;
            }
            $entry['ok'] = is_array($res) && count($res) > 0;
            $entry['count'] = is_array($res) ? count($res) : 0;
            $entry['sample'] = is_array($res) ? array_slice($res, 0, 10, true) : false;
        }
    } catch (Throwable $e) {
        $entry['exception'] = $e->getMessage();
        $entry['ok'] = false;
    }
    $out['tests'][] = $entry;
}

echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
