"""This script save the power values obtained periodically from the Raritan PDU to a CSV file"""
import argparse
import csv
import datetime
import time
import sys
from pdu_python_api.raritan.rpc import Agent
from pdu_python_api.raritan.rpc import pdumodel
def get_args():
"""Manage the command line arguments"""
parser = argparse.ArgumentParser(description='PowerMeter Test Demo.')
parser.add_argument('--ipAddress', metavar='ipAddress', type=str, help='Raritan IP Address',
default='10.186.169.201')
parser.add_argument('--username', metavar='username', type=str, help='Raritan User',
default='admin')
parser.add_argument('--password', metavar='password', type=str, help='Raritan Password',
default='pap3rin0')
parser.add_argument('--interval', metavar='interval', type=float,
help='Interval to get the power values (in seconds)', default=1)
parser.add_argument('--duration', metavar='duration', type=float,
help='Duration to get the power values (in seconds, 0 means infinite)',
default=0)
parser.add_argument('--csvOutput', metavar='csvOutput', type=str,
help='CSV Output filename', required=True)
parser.add_argument('--append', dest='append', help='Append the obtained data',
action='store_true')
parser.add_argument('--messageInterval', metavar='messageInterval', type=int,
help='Interval to display the status of writing (in seconds)', default=-1)
args = parser.parse_args()
print('Arguments:')
print("\tIP Address: %s" % (args.ipAddress))
print("\tUsername: %s" % (args.username))
print("\tPassword: %s" % (args.password))
print("\tInterval: %ds" % (args.interval))
if args.duration != 0:
print("\tDuration: %ds" % (args.duration))
print("\tCSV Output file: %s" % (args.csvOutput))
print("\tAppend: %s" % (args.append))
if args.messageInterval != -1:
print("\tMessage Interval: %ds" % (args.messageInterval))
return args
def get_power_sensor(args):
"""Get the power sensors"""
agent = Agent("https", args.ipAddress, args.username, args.password,
disable_certificate_verification=True)
pdu = pdumodel.Pdu("/model/pdu/0", agent)
outlets = pdu.getOutlets()
outlet = outlets[0]
outlet_sensors = outlet.getSensors()
return outlet_sensors.activePower
def get_power_value(power_sensor):
"""Get the power value"""
sensor_reading = power_sensor.getReading()
timestamp = "%s" % (sensor_reading.timestamp.strftime('%y%m%d_%H%M%S')
if sensor_reading.valid else 'n/a')
value = "%d" % (sensor_reading.value if sensor_reading.valid else 'n/a')
return [timestamp, value]
def main():
"""Read the power value periodically and save to CSV"""
args = get_args()
power_sensor = get_power_sensor(args)
with open(args.csvOutput, 'ab' if args.append else 'wb') as csv_file:
writer = csv.writer(csv_file, delimiter=";", quotechar='"', quoting=csv.QUOTE_MINIMAL)
if not args.append:
writer.writerow(['DateTime', 'Time from Epoch [s]', 'Power Value [W]'])
duration = 0
num_write = 0
total_num_write = 0
last_write = 0
if args.messageInterval != -1:
print("Writing:")
while duration <= args.duration or args.duration == 0:
time.sleep(args.interval)
duration = duration + args.interval
writer.writerow(get_power_value(power_sensor))
num_write = num_write + 1
total_num_write = total_num_write + 1
if args.messageInterval != -1 and duration % args.messageInterval == 0:
print("\t%d values in %s seconds [total: %d]" % (num_write, duration - last_write, total_num_write))
num_write = 0
last_write = duration
csv_file.flush()
main()
Comments