+++ class = "post" date = "2017-09-25T10:00:00-04:00" tags = ["diy","homelab","buildlog"] title = "Build a Custom Power Monitoring PDU" description = "Building a custom power monitoring PDU for fun and profit" type = "post" weight = 1 draft = true +++ So you've got a homelab. It runs great, you've got dozens of VMs, and a spiffy storage cluster, and the hardware wasn't cheap. But, unless you live in Québec, you also probably care about another big cost sink: power usage. Here in Ontario I'm paying anywhere from 8¢ to 18¢ per kilowatt-hour (kWh) of energy, and with my rack totaling over 1600W normally, the power usage is substantial. The problem, however, is determining how much each device is using. Sure, the UPS can give me an overall power usage figure, but with 8 servers, 2 routers, 2 switches, and several other devices, knowing exactly how much each machine is using helps enable even more granular control of the power usage. The most obvious solution to this problem is what is usually referred to as a "Metered Per-Port PDU" in marketing-speak. Simply-put, it's a large power bar (Power Distribution Unit, for the acronym-unfriendly) that contains a network port and a series of current monitors on each port, allowing you to collect information about per-port power usage via SNMP or a WebUI. Most of the big PDU vendors make one of these devices (though with different names - marketing-speak, after all), however they are not cheap. The lowest cost on I could find was from Tripp-Lite at $1200 CAD new. That's a pretty penny to pay for power distribution, so instead, in my always-present DIY mindset, I started thinking about building my own. ## The basics of power usage and monitoring Determing the power usage in Watts of a device is done using a very simple formula: ``` P = V * I ``` `P` is your power usage, measured in Watts (W). `V` is the voltage of the circuit in Volts (V), and `I` is the current in Amperes (A). For standard home circuits, the voltage is usually 120v (roughly) so calculating the power usage of a device is as simple as multiplying the current by 120 to get a rough wattage value. So with a current of 1A, the power draw would be 120W at 120V. The actual voltage will vary slighly due to other factors (such as voltage droop with a large current), but for most practical purposes you can use 120V to get a reasonably-accurate value. In my case, my UPS outputs the active voltage across the output circuits so I have an exact value to work with. A/C power complicates this ideal formula slightly. A/C power works on a fluctuating voltage in a sine wave, which alternates at a specific frequency. In North America, this is 60Hz (cycles per second). What this means it that every 1/60th of a second, the voltage peaks (120V for a standard household circuit as mentioned), and then dips down to 0V before repeating. And as you know from the formula above, when the voltage is 0, the power usage will also be 0, so the effective power usage also looks like a sine wave with a frequency of 60Hz. While you usually don't notice this fluctuation in your daily life, when trying to measure the power of a circuit, it becomes very important to take this into account. The next section goes over how to create a current monitoring circuit for A/C power. The last thing to touch on for those unfamilliar is the idea of a Kilowatt-Hour, or kWh. One kWh is equal to 1000W (1 kW) of power used continuously for 1 hour. Since this is a time-based measurement, versus the instantaneous measurement of the `P = V * I` formula, it is the most common unit of billing for electric power. As a concrete example, assumming the previous example load of 1A at 120V, working out to 120W, you simply multiply this out by 1 hour to get the usage of 0.120 kWh of energy usage every hour. Over two hours it would use 0.240 kWh of power, and etcetera for as long as the device is active. When you get your monthly power bill, this is how the total power usage is calculated. ## An A/C monitoring circuit With the theoretical underpinnings of this project out of the way, we can begin looking at devices to measure the current of an A/C circuit. Given the formula above, as long as the voltage is known, the only thing we need to measure is the current in Amperes, and we'll be able to do some quick math to get the wattage value. Luckily, there's a very easy way to measure the current of a circuit. Called the 'Hall effect', it relies on the magnetic field of a powered conductor, and while the effect is far to complicated to explain here ([Wikipedia is a good source](https://en.wikipedia.org/wiki/Hall_effect)), suffice to say that it gives a very accurate measurement of the current through a conductor at any given time. The Hall effect works equally well with both A/C and D/C power. Hall effect sensors come in two main types. The first is a wound core through which the hot wire of the circuit is placed. This is a "non-invasive" Hall sensor, and while it works quite well, it is a lot bulkier than the second type. This second type is an in-line Hall sensor, best exemplified by the ACS712-T sensor based on the Allegro chip. This simple 8-pin chip takes the entire current through four of its pins, and via an internal coil and detector, outputs a variable D/C voltage based on the sensed current. To save space as well as cost, since I'll be building the entire PDU from scratch anyways, I went with these sensor chips instead of the "non-invasive" type. The ACS712-T comes in three different varieties, each rated for a different current load: 5A, 20A, and 30A. Each variety outputs a slightly different D/C voltage per Ampere of through current, which allows for varying sensitivity up to 30A. For my purposes, the 20A model was both the lowest I could go (5A would not be enough for the larger servers), while the 30A would be overkill and its extremely small D/C voltage variation would have made "long"-distance measuring of the voltage more problematic. ## Building the PDU ## Building the monitoring circuits ## The software side ## Conclusion