From 0b26fb35ee8d6eee80d2b9a6a0703dc229651b2d Mon Sep 17 00:00:00 2001 From: Radek Date: Thu, 22 May 2025 09:29:19 +0100 Subject: [PATCH] customer breakdown n flags --- get_power_room_n_customer.py | 254 +++++++++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 get_power_room_n_customer.py diff --git a/get_power_room_n_customer.py b/get_power_room_n_customer.py new file mode 100644 index 0000000..0ba6445 --- /dev/null +++ b/get_power_room_n_customer.py @@ -0,0 +1,254 @@ +import requests +from collections import defaultdict +import argparse +import sqlite3 +from datetime import datetime +import time + +# Configuration +API_KEY = '' +LIBRENMS_IP = '' +HEADERS = {'X-Auth-Token': API_KEY} + +def create_db_connection(db_file): + """Create a database connection to a SQLite database.""" + conn = None + try: + conn = sqlite3.connect(db_file) + return conn + except sqlite3.Error as e: + print(e) + return conn + +def create_tables(conn): + """Create tables for storing the data.""" + try: + cursor = conn.cursor() + cursor.execute(''' + CREATE TABLE IF NOT EXISTS building_totals ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + total_current REAL, + total_power REAL, + timestamp TEXT + ) + ''') + cursor.execute(''' + CREATE TABLE IF NOT EXISTS room_breakdown ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + room_number TEXT, + current REAL, + power REAL, + timestamp TEXT + ) + ''') + cursor.execute(''' + CREATE TABLE IF NOT EXISTS customer_breakdown ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + customer_name TEXT, + current REAL, + power REAL, + timestamp TEXT + ) + ''') + conn.commit() + except sqlite3.Error as e: + print(e) + +def insert_building_total(conn, total_current, total_power): + """Insert building total data into the database.""" + try: + cursor = conn.cursor() + cursor.execute(''' + INSERT INTO building_totals (total_current, total_power, timestamp) + VALUES (?, ?, ?) + ''', (round(total_current, 3), round(total_power, 3), datetime.now().isoformat())) + conn.commit() + except sqlite3.Error as e: + print(e) + +def insert_room_breakdown(conn, room_number, current, power): + """Insert room breakdown data into the database.""" + try: + cursor = conn.cursor() + cursor.execute(''' + INSERT INTO room_breakdown (room_number, current, power, timestamp) + VALUES (?, ?, ?, ?) + ''', (room_number, round(current, 3), round(power, 3), datetime.now().isoformat())) + conn.commit() + except sqlite3.Error as e: + print(e) + +def insert_customer_breakdown(conn, customer_name, current, power): + """Insert customer breakdown data into the database.""" + try: + cursor = conn.cursor() + cursor.execute(''' + INSERT INTO customer_breakdown (customer_name, current, power, timestamp) + VALUES (?, ?, ?, ?) + ''', (customer_name, round(current, 3), round(power, 3), datetime.now().isoformat())) + conn.commit() + except sqlite3.Error as e: + print(e) + +def get_device_ids(debug=False): + """Fetch all device IDs from the LibreNMS API.""" + response = requests.get(f'http://{LIBRENMS_IP}/api/v0/devices', headers=HEADERS, verify=False) + if response.status_code == 200: + devices = response.json().get('devices', []) + if debug: + print(f"Devices: {devices}") # Debugging statement + power_devices = [device['device_id'] for device in devices if device.get('type') == 'power'] + return power_devices + else: + raise Exception(f"Failed to fetch devices: {response.status_code}") + +def get_device_location(device_id, debug=False): + """Fetch the location for a given device ID.""" + response = requests.get(f'http://{LIBRENMS_IP}/api/v0/devices/{device_id}', headers=HEADERS, verify=False) + if response.status_code == 200: + location = response.json().get('devices', [{}])[0].get('location', '') + if debug: + print(f"Location for device {device_id}: {location}") # Debugging statement + # Extract room number from location + room_number = location.split(',')[-1].strip() if ',' in location else 'Unknown' + return room_number + else: + raise Exception(f"Failed to fetch location for device {device_id}: {response.status_code}") + +def get_customer_name(device_id, debug=False): + """Fetch the customer name for a given device ID.""" + response = requests.get(f'http://{LIBRENMS_IP}/api/v0/devices/{device_id}', headers=HEADERS, verify=False) + if response.status_code == 200: + hostname = response.json().get('devices', [{}])[0].get('hostname', '') + if debug: + print(f"Hostname for device {device_id}: {hostname}") + customer_name = hostname.split('-')[0] if '-' in hostname else 'Unknown' + return customer_name + else: + raise Exception(f"Failed to fetch hostname for device {device_id}: {response.status_code}") + +def get_sensor_ids(device_id, sensor_group, debug=False): + """Fetch sensor IDs for a given device ID and sensor group.""" + response = requests.get(f'http://{LIBRENMS_IP}/api/v0/devices/{device_id}/health/{sensor_group}', headers=HEADERS, verify=False) + if response.status_code == 200: + graphs = response.json().get('graphs', []) + if debug: + print(f"Graphs for device {device_id}: {graphs}") # Debugging statement + # Filter sensors based on descriptions + if sensor_group == 'device_current': + relevant_sensors = [ + sensor['sensor_id'] for sensor in graphs + if sensor['desc'] in ["Input Phase 1.1", "Input Phase 1.2", "Input Phase 1.3", "Phase 1"] + ] + elif sensor_group == 'device_power': + relevant_sensors = [ + sensor['sensor_id'] for sensor in graphs + if sensor['desc'] in ["Active power #1", "Total power", "Power, Total"] + ] + return relevant_sensors + else: + raise Exception(f"Failed to fetch sensors for device {device_id}: {response.status_code}") + +def get_sensor_value(device_id, sensor_id, sensor_group, debug=False): + """Fetch the current value for a given sensor ID and sensor group.""" + response = requests.get(f'http://{LIBRENMS_IP}/api/v0/devices/{device_id}/health/{sensor_group}/{sensor_id}', headers=HEADERS, verify=False) + if response.status_code == 200: + sensor_data = response.json() + if debug: + print(f"Sensor data for device {device_id}, sensor {sensor_id}: {sensor_data}") # Debugging statement + sensor_value = sensor_data['graphs'][0].get('sensor_current', 0) + sensor_desc = sensor_data['graphs'][0].get('sensor_descr', '') + + # Divide by 100 if the sensor is from an nLogic PDU and is a current sensor + if sensor_group == 'device_current' and sensor_desc in ["Input Phase 1.1", "Input Phase 1.2", "Input Phase 1.3"]: + sensor_value /= 100 + + return sensor_value + else: + raise Exception(f"Failed to fetch sensor value for sensor {sensor_id}: {response.status_code}") + +def main(debug=False, update_db=True): + try: + device_ids = get_device_ids(debug) + total_current = 0 + total_power_watts = 0 + room_current = defaultdict(float) + room_power = defaultdict(float) + customer_data = defaultdict(lambda: {'current': 0, 'power_watts': 0}) + + # Create a SQLite database connection if update_db is True + if update_db: + db_file = 'power_data_room_n_customer.db' + conn = create_db_connection(db_file) + create_tables(conn) + else: + conn = None + + for device_id in device_ids: + room_number = get_device_location(device_id, debug) + customer_name = get_customer_name(device_id, debug) + + # Fetch and sum current values + current_sensor_ids = get_sensor_ids(device_id, 'device_current', debug) + for sensor_id in current_sensor_ids: + sensor_value = get_sensor_value(device_id, sensor_id, 'device_current', debug) + total_current += sensor_value + room_current[room_number] += sensor_value + customer_data[customer_name]['current'] += sensor_value + + # Fetch and sum power values + power_sensor_ids = get_sensor_ids(device_id, 'device_power', debug) + for sensor_id in power_sensor_ids: + sensor_value = get_sensor_value(device_id, sensor_id, 'device_power', debug) + total_power_watts += sensor_value + room_power[room_number] += sensor_value + customer_data[customer_name]['power_watts'] += sensor_value + + total_power_kw = total_power_watts / 1000 # Convert watts to kilowatts + print(f"Total Current: {round(total_current, 3)} A") + print(f"Total Power: {round(total_power_kw, 3)} kW") + + # Insert building total data into the database if update_db is True + if update_db: + insert_building_total(conn, total_current, total_power_kw) + + print("\nBreakdown by Room:") + for room, current in room_current.items(): + power_kw = room_power[room] / 1000 # Convert watts to kilowatts + print(f"Room {room}: Current = {round(current, 3)} A, Power = {round(power_kw, 3)} kW") + + # Insert room breakdown data into the database if update_db is True + if update_db: + insert_room_breakdown(conn, room, current, power_kw) + + print("\nCustomer Breakdown:") + for customer_name, data in customer_data.items(): + power_kw = data['power_watts'] / 1000 # Convert watts to kilowatts + print(f"Customer {customer_name} - Current: {round(data['current'], 3)} A, Power: {round(power_kw, 3)} kW") + + # Insert customer breakdown data into the database if update_db is True + if update_db: + insert_customer_breakdown(conn, customer_name, data['current'], power_kw) + + # Close the database connection if it was opened + if update_db: + conn.close() + except Exception as e: + print(str(e)) + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Fetch and display power and current data from LibreNMS.') + parser.add_argument('--debug', action='store_true', help='Enable debug output') + parser.add_argument('--runs', type=int, default=None, help='Limit the number of runs') + parser.add_argument('--no-db-update', action='store_true', help='Disable updates to the database') + args = parser.parse_args() + + runs = 0 + while True: + if args.runs is not None and runs >= args.runs: + break + + main(debug=args.debug, update_db=not args.no_db_update) + runs += 1 + if args.runs is None: + time.sleep(240) # Wait for 5 minutes before the next run