70 lines
1.6 KiB
Python
Executable File
70 lines
1.6 KiB
Python
Executable File
#!/usr/bin/python
|
|
|
|
#
|
|
# Imports
|
|
#
|
|
import sys
|
|
import time
|
|
import argparse
|
|
import pylikwid
|
|
|
|
#
|
|
# Configuration
|
|
#
|
|
hostname = 'server'
|
|
cpuid = 0
|
|
pinfo = pylikwid.getpowerinfo()
|
|
domainid = pinfo.get('domains').get('PKG').get('ID')
|
|
measurement_duration = 10
|
|
measurement_interval = 60
|
|
dinfo = pinfo.get('domains')
|
|
domain_names = dinfo.keys()
|
|
domain_ids = [domain['ID'] for domain in dinfo.values()]
|
|
|
|
#
|
|
# Command line arguments
|
|
#
|
|
parser = argparse.ArgumentParser(description='Get CPU power consumption')
|
|
parser.add_argument('-s', action='store_true', help='print in human readable format')
|
|
args = parser.parse_args()
|
|
human_readable = args.s
|
|
|
|
#
|
|
# Methods
|
|
#
|
|
def get_power():
|
|
#print dict(zip(domain_names, domain_ids))
|
|
start = list()
|
|
end = list()
|
|
power = list()
|
|
for domain_id in domain_ids:
|
|
e_start = pylikwid.startpower(cpuid, domain_id)
|
|
start.append(e_start)
|
|
time.sleep(measurement_duration)
|
|
for domain_id in domain_ids:
|
|
e_stop = pylikwid.stoppower(cpuid, domain_id)
|
|
end.append(e_stop)
|
|
for events in zip(start, end, domain_ids):
|
|
power.append(pylikwid.getpower(events[0], events[1], events[2]))
|
|
|
|
return dict(zip(domain_names, power))
|
|
|
|
def print_rrd(measurements):
|
|
timestamp = int(time.time())
|
|
for measurement in measurements.items():
|
|
name = measurement[0].lower()
|
|
power = measurement[1]
|
|
print('PUTVAL {}/exec-power/gauge-{} {}:{:.1f}'.format(hostname, name, timestamp, power))
|
|
|
|
#
|
|
# Main
|
|
#
|
|
if (human_readable):
|
|
print get_power()
|
|
else:
|
|
while True:
|
|
power = get_power()
|
|
print_rrd(power)
|
|
sys.stdout.flush()
|
|
time.sleep(measurement_interval)
|