如何从远程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
CPUnode, then drill down to itsTemperatureschild. We map over all temperature nodes (like CPU Core #1, CPU Package) to collect their names and values. - RAM Usage: Navigate to the
RAMnode, then itsLoadchild, and pull theValuefrom theMemoryentry. - HDD Usage: Similarly, find the
HDDnode, itsLoadchild, and get theValuefromUsed 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

