You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Zabbix的多主机自定义仪表盘:UPS状态动态更新实现咨询

How to Dynamically Pull Zabbix Data for Your UPS Status Dashboard

Hey Nick, sounds like you're halfway there with your static dashboard—let's turn it into a live-updating one that pulls real UPS status from Zabbix. Here's a step-by-step approach tailored to your setup:

1. Use the Zabbix API to Fetch Real-Time Data

Zabbix has a robust API that lets you pull trigger/host statuses programmatically. First, you'll need to authenticate and query the relevant data for your UPS devices.

Here's a PHP snippet to connect to the Zabbix API and get the latest trigger statuses for your UPS group (groupid=10 as seen in your link):

<?php
// Zabbix API credentials and endpoint
$zabbixUrl = 'http://your-zabbix-server/api_jsonrpc.php';
$username = 'your-zabbix-username';
$password = 'your-zabbix-password';

// Step 1: Authenticate to get an auth token
$authPayload = json_encode([
    'jsonrpc' => '2.0',
    'method' => 'user.login',
    'params' => [
        'user' => $username,
        'password' => $password
    ],
    'id' => 1
]);

$ch = curl_init($zabbixUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $authPayload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json-rpc']);
$authResponse = json_decode(curl_exec($ch), true);
$authToken = $authResponse['result'];

// Step 2: Fetch trigger statuses for your UPS group (groupid=10)
$triggerPayload = json_encode([
    'jsonrpc' => '2.0',
    'method' => 'trigger.get',
    'params' => [
        'output' => ['triggerid', 'description', 'status'],
        'groupids' => 10,
        'sortfield' => 'description'
    ],
    'auth' => $authToken,
    'id' => 2
]);

$triggerResponse = json_decode(curl_exec($ch), true);
curl_close($ch);

// Organize results into an array for easy lookup (key by trigger name like "L1")
$upsStatuses = [];
foreach ($triggerResponse['result'] as $trigger) {
    if (str_contains($trigger['description'], 'L1')) {
        $upsStatuses['L1'] = $trigger['status']; // 0 = OK (green), 1 = Problem (red)
    }
    // Add entries for L2, L3, etc., based on your Zabbix triggers
}
?>

2. Replace Static Table Content with Dynamic Data

Update your existing table HTML to use the fetched statuses instead of hardcoded values. Adjust the cell class to show green/red based on the status:

<tbody>
    <tr>
        <td><a href="tr_status.php?filter_set=1&amp;groupid=10&amp;hostid=0&amp;show_triggers=1">L1</a></td>
        <td class="<?php echo isset($upsStatuses['L1']) && $upsStatuses['L1'] == 1 ? 'problem-bg' : 'normal-bg'; ?>">
            <?php echo isset($upsStatuses['L1']) ? ($upsStatuses['L1'] == 1 ? '1' : '0') : '0'; ?>
        </td>
        <!-- Repeat this pattern for other UPS entries (L2, L3, etc.) -->
    </tr>
</tbody>

Note: Ensure your CSS styles .normal-bg with a green background and .problem-bg with red to match your status needs.

To update the dashboard without manual reloads, use JavaScript to fetch statuses via AJAX at intervals:

  1. Create a separate PHP file (e.g., get-ups-status.php) that outputs the UPS statuses as JSON (use the API code from step 1, but replace HTML rendering with echo json_encode($upsStatuses);).

  2. Add this script to your dashboard page:

// Refresh every 30 seconds (adjust interval as needed)
setInterval(function() {
    fetch('get-ups-status.php')
        .then(response => response.json())
        .then(data => {
            // Update L1 status cell
            const l1Cell = document.querySelector('tr td:nth-child(2)');
            if (data.L1 === 1) {
                l1Cell.className = 'problem-bg';
                l1Cell.textContent = '1';
            } else {
                l1Cell.className = 'normal-bg';
                l1Cell.textContent = '0';
            }
            // Add updates for other UPS cells here
        });
}, 30000);

Quick Tips

  • Make sure your Zabbix user has read permissions for the UPS group/hosts to avoid API errors.
  • Test API calls first with tools like Postman to confirm you're getting the correct status values.
  • Adjust the trigger filter in the API call if you need to target specific UPS-related triggers (e.g., filter by description or itemids).

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:25:01