79

I've been asked to estimate the power consumption of the servers I run for my laboratory. I thought that I'd ask if there was some handy Linux commandline to get the power consumption of the server. It looks like powertop is useful for minimizing power consumption but it does not seem to show information that server A is using B watts.

Is there something buried in the /proc system that would help me out?

Carolus
  • 212
  • 1
  • 10
  • Do you mean powertop shows no ACPI power usage estimate available? – Mikel Apr 01 '11 at 03:00
  • It may be worth mentioning that there are instruments available for measuring current/voltage. A current clamp around a power cord can measure current, then multiplied by the mains voltage (120V?). For an estimate, you could even turn the system on and see how much the power meter speeds up. – mbowden May 10 '18 at 14:07
  • For some processors, package energy is available via MSR. turbostat (linux-tools-common). Its not about the whole server anyway hope it helps. – Alex May 27 '22 at 00:25

4 Answers4

49

If your computer actually keeps track of power (e.g. notebook), than on kernel 3.8.11 you can use the command below. It returns power measured in microwatts.

cat /sys/class/power_supply/BAT0/power_now

This works on kernel 3.8.11 (Ubuntu Quantal mainline generic).

  • 16
    It actual is in µW and can be printed like this in terms of watts: awk '{print $1*10^-6 " W"}' /sys/class/power_supply/BAT0/power_now – Matt Jul 27 '16 at 00:34
  • @Matt Yes, you are right. Answer corrected. Thank you. – Adam Ryczkowski Jul 27 '16 at 15:02
  • 4
    If your system does not have the file power_now in /sys/class/power_supply/BAT0/, you can multiply the values in the files current_now and voltage_now from that directory to get power_now (don't forget to convert units accordingly, both files use µ units). – Tomáš Peitl May 12 '18 at 17:50
  • 1
    On my ThinkPad this command shows the power draw only while charging/discharging. From the moment battery is fully charged, it always shows 0. – anatolii Sep 02 '19 at 20:52
  • 3
    My /sys/class/power_supply directory @ ArchLinux Dell workstation is empty. Are there any other alternatives? – mabalenk Jun 18 '20 at 13:51
  • 1
    I'm on ubuntu 16.04. I can't see anything under /sys/class/power_supply. Does anybody know where I should look at? – Chan Kim Dec 11 '20 at 05:19
  • 1
    On my rooted Android 11 phone, the same file can be found here : /sys/class/power_supply/bms/power_now while there is no power information in /sys/class/power_supply/battery/ (but current and voltage) – TheBigBadBoy Jul 19 '22 at 21:54
27

Another KISS solution, completing Adam's suggestion. This is for people who don't have a power_now file. (Arch)

echo - | awk "{printf \"%.1f\", \
$(( \
  $(cat /sys/class/power_supply/BAT1/current_now) * \
  $(cat /sys/class/power_supply/BAT1/voltage_now) \
)) / 1000000000000 }" ; echo " W "

Reports the actual power draw in Watts with one decimal place.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 11
    only works if it has a battery. – KFL Aug 27 '18 at 07:07
  • 1
    When plugged in reports 0.0 W when unplugged reports 44.3 W. Works for me. – WinEunuuchs2Unix Oct 25 '18 at 23:11
  • 2
    If you get syntax error: operand expected (error token is "* "), then it's possible the path in the cat command is non-existent. For me on Ubuntu 16.04, changing the 2 instance of BAT1 to BAT* fixed that. – Garrett Nov 12 '18 at 21:54
  • 2
    Another version of the same command, handling the charging situation: path="/sys/class/power_supply/BAT0"; value=$(echo "scale=1; $(cat ${path}/current_now) * $(cat ${path}/voltage_now) / 10^12" | bc); [[ $value == "0" ]] && echo "" || echo "${value}W" I used this for showing power consumption on panel in Linux Mint/Cinnamon, using the Bash Sensors applet. It shows either the wattage or nothing. – radeklat Feb 25 '20 at 16:46
  • #~/bin/bash

    echo - | awk "{printf "%.1f",
    $((
    $(cat /sys/class/power_supply/BAT0/current_now) *
    $(cat /sys/class/power_supply/BAT0/voltage_now)
    )) / 1000000000000 }" ; echo " W battery"

    echo - | awk "{printf "%.1f",
    $((
    $(cat /sys/class/power_supply/BAT0/charge_now) *
    $(cat /sys/class/power_supply/BAT0/voltage_now)
    )) / 1000000000000 }" ; echo " W charging"

    Both charging and battery wattage

    – vitperov Jan 15 '21 at 17:40
13

Computers generally don't track the current they are drawing. It is common that there are voltage sensors that are readable.

Power consumption can vary widely over time depending on workload. CPUs now throttle back when load is low. Disks will shutdown when idle. Tools like powertop will track processes which trigger increases in power consumption. Other tools will aggressively tune down power consumption.

UPSs care about power consumption and often have accessible data.

The manufacturers specs will give you some idea of power consumption, but reconfiguration of hardware can change power consumption. This is probably a good place to start and estimate. Adding new devices or replacing existing ones may alter the power load.

There are devices like Kill-A-Watt that can be used to measure power consumption. However, they require disconnecting the device so that the power runs through the device.

You may want to invest in or borrow a current meter which works by clipping around the wire. This would allow you to calculate volt-amps. This is typically different (higher) than the wattage for servers.

BillThor
  • 8,965
  • 1
    It's also worth noting that UPSes care both about peak as well as sustained power draw. Peak load will happen during the initial phase of power-on when everything (particularly everything mechanical like fans and spinning platter drives) starts up, and whenever the system is working really hard for some reason. Sustained and average load will almost certainly be lower for many of the reasons you mention in your answer. – user Jun 06 '13 at 11:57
  • 2
    Startup load can be significantly above the running load. On a previous project we would blow the fuses in the breaker box during startup from a full shutdown. During normal usage our load was about 30% of the fuse rating. – BillThor Jun 07 '13 at 00:23
5

I don't think most servers introspect their power consumption, at least not in a form that Linux can access. You might have better luck querying the servers' LOM modules (if any), but I usually get this kind of information from remote power strips.

geekosaur
  • 32,047