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

如何从远程JSON提取指定Open Hardware Monitor硬件监控数据?

Hey there! I've worked with Open Hardware Monitor's JSON output before, so let's get your script pulling exactly the metrics you need. Here's a complete, modified solution that extracts CPU/GPU temperatures, RAM usage, and HDD usage, plus a breakdown of how it works.


Full Modified Code

HTML

<div id="display-resources">
  <p>Click below to retrieve hardware metrics from your Open Hardware Monitor instance.</p>
</div>
<a href="javascript:void(0)" class="btn btn-primary" id="retrieve-resources">Retrieve Metrics</a>

JavaScript (jQuery)

<script>
$(document).ready(function() {
  $("#retrieve-resources").click(function() {
    const displayResources = $("#display-resources");
    displayResources.text("Loading hardware data...");

    // Helper function to find a child node by its Text property
    function findChildByText(parentNode, targetText) {
      if (!parentNode?.Children) return null;
      return parentNode.Children.find(child => child.Text === targetText);
    }

    $.ajax({
      type: "GET",
      url: "http://theirip:8085/data.json", // Replace with your target IP
      dataType: "json",
      success: function(result) {
        console.log("Raw JSON data:", result);
        
        // Navigate to the main hardware components (skip root and computer name nodes)
        const rootChildren = result.Children[0]?.Children;
        if (!rootChildren) {
          displayResources.text("Failed to locate hardware components in JSON.");
          return;
        }

        // Extract CPU Temperatures
        const cpuNode = findChildByText({ Children: rootChildren }, "CPU");
        const cpuTempNode = findChildByText(cpuNode, "Temperatures");
        const cpuTemps = cpuTempNode?.Children.map(temp => ({
          name: temp.Text,
          value: temp.Value
        })) || [];

        // Extract RAM Usage
        const ramNode = findChildByText({ Children: rootChildren }, "RAM");
        const ramLoadNode = findChildByText(ramNode, "Load");
        const ramUsage = findChildByText(ramLoadNode, "Memory")?.Value || "N/A";

        // Extract HDD Usage
        const hddNode = findChildByText({ Children: rootChildren }, "HDD");
        const hddLoadNode = findChildByText(hddNode, "Load");
        const hddUsage = findChildByText(hddLoadNode, "Used Space")?.Value || "N/A";

        // Build output HTML
        let output = "<table class='table'><thead><tr><th>Metric</th><th>Value</th></tr></thead><tbody>";
        
        // Add CPU temps
        cpuTemps.forEach(temp => {
          output += `<tr><td>${temp.name}</td><td>${temp.value}</td></tr>`;
        });
        
        // Add RAM and HDD
        output += `<tr><td>RAM Usage</td><td>${ramUsage}</td></tr>`;
        output += `<tr><td>HDD Usage</td><td>${hddUsage}</td></tr>`;
        
        output += "</tbody></table>";
        displayResources.html(output);
      },
      error: function(xhr, status, error) {
        displayResources.text(`Error loading data: ${error}`);
        console.error("AJAX Error:", status, error);
      }
    });
  });
});
</script>

How It Works

1. Helper Function for Nested JSON Navigation

The findChildByText function is critical here. Open Hardware Monitor's JSON uses a nested tree structure where IDs can vary between systems, but your renamed component names (CPU, RAM, HDD) are consistent. This function searches a parent node's children for a specific Text value, making your code robust across different machines.

2. Step-by-Step Metric Extraction

  • CPU Temperatures: We first locate the CPU node, then drill down to its Temperatures child. We map over all temperature nodes (like CPU Core #1, CPU Package) to collect their names and values.
  • RAM Usage: Navigate to the RAM node, then its Load child, and pull the Value from the Memory entry.
  • HDD Usage: Similarly, find the HDD node, its Load child, and get the Value from Used Space.

3. Error Handling

The code includes checks for missing nodes (e.g., if a component isn't detected) and will show "N/A" instead of breaking. It also has an AJAX error handler to catch network issues or invalid JSON.


Quick Tip for Your 50-Computer Setup

If you plan to pull data from all 50 machines, you could:

  • Create an array of IP addresses and loop through them
  • Add a column for the machine IP/name in the output table
  • Implement rate limiting to avoid overwhelming your network

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:18:08