Files
2026-04-21 00:11:59 +02:00

57 lines
2.3 KiB
PHP

<?php
error_reporting(0);
header('Content-Type: application/json');
$config = parse_ini_file('config.ini', true);
$base_url = "https://{$config['proxmox']['host']}:{$config['proxmox']['port']}/api2/json/nodes/{$config['proxmox']['node']}";
$token = "PVEAPIToken={$config['proxmox']['user']}!{$config['proxmox']['token_name']}={$config['proxmox']['token_value']}";
function pve_get($url, $token) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: $token"]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res, true)['data'];
}
$qemu = pve_get("$base_url/qemu", $token) ?? [];
$lxc = pve_get("$base_url/lxc", $token) ?? [];
$results = [];
foreach ($qemu as $node) {
$ips = [];
$agent = pve_get("$base_url/qemu/{$node['vmid']}/agent/network-get-interfaces", $token);
if ($agent && isset($agent['result'])) {
foreach($agent['result'] as $iface) {
foreach($iface['ip-addresses'] as $addr) {
if ($addr['ip-address-type'] == 'ipv4' && $addr['ip-address'] != '127.0.0.1') $ips[] = $addr['ip-address'];
}
}
}
$results[] = [
'vmid' => $node['vmid'], 'name' => $node['name'], 'status' => $node['status'], 'type' => 'qemu',
'ip' => !empty($ips) ? implode(", ", array_unique($ips)) : "N/D",
'cpu' => $node['cpu'], 'mem' => $node['mem'], 'maxmem' => $node['maxmem'],
'disk' => $node['disk'], 'maxdisk' => $node['maxdisk'],
'netin' => $node['netin'], 'netout' => $node['netout']
];
}
foreach ($lxc as $node) {
$ip = "N/D";
if (!empty($node['tags']) && preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $node['tags'], $matches)) $ip = $matches[1];
$results[] = [
'vmid' => $node['vmid'], 'name' => $node['name'], 'status' => $node['status'], 'type' => 'lxc', 'ip' => $ip,
'cpu' => $node['cpu'], 'mem' => $node['mem'], 'maxmem' => $node['maxmem'],
'disk' => $node['disk'], 'maxdisk' => $node['maxdisk'],
'netin' => $node['netin'], 'netout' => $node['netout']
];
}
echo json_encode($results);
?>