2025-02-07 21:03:31 -05:00
|
|
|
import logging
|
2025-01-04 01:42:16 -05:00
|
|
|
import json
|
2025-02-07 21:03:31 -05:00
|
|
|
import platform
|
|
|
|
|
import subprocess
|
2025-01-04 01:42:16 -05:00
|
|
|
import threading
|
|
|
|
|
import time
|
2025-02-07 21:03:31 -05:00
|
|
|
from datetime import datetime
|
|
|
|
|
from flask import Flask, render_template, jsonify
|
|
|
|
|
import requests
|
|
|
|
|
from urllib3.exceptions import InsecureRequestWarning
|
2025-01-04 01:42:16 -05:00
|
|
|
|
2025-02-07 21:03:31 -05:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
2025-01-04 01:42:16 -05:00
|
|
|
app = Flask(__name__)
|
2025-02-07 21:03:31 -05:00
|
|
|
device_status = {}
|
2025-01-04 01:42:16 -05:00
|
|
|
|
|
|
|
|
def load_config():
|
|
|
|
|
with open('config.json') as f:
|
|
|
|
|
return json.load(f)
|
|
|
|
|
|
2025-02-07 20:55:38 -05:00
|
|
|
class UnifiAPI:
|
|
|
|
|
def __init__(self, config):
|
|
|
|
|
self.base_url = config['unifi']['controller']
|
2025-02-07 22:31:07 -05:00
|
|
|
self.session = requests.Session()
|
|
|
|
|
self.session.verify = False
|
2025-02-07 22:17:27 -05:00
|
|
|
self.headers = {
|
2025-02-07 23:24:36 -05:00
|
|
|
'X-API-KEY': config['unifi']['api_key'],
|
|
|
|
|
'Accept': 'application/json'
|
2025-02-07 22:17:27 -05:00
|
|
|
}
|
2025-02-07 23:26:41 -05:00
|
|
|
self.site_id = "default"
|
2025-02-08 00:03:01 -05:00
|
|
|
|
|
|
|
|
def get_devices(self):
|
2025-02-08 00:04:42 -05:00
|
|
|
try:
|
|
|
|
|
url = f"{self.base_url}/proxy/network/v2/api/site/{self.site_id}/device"
|
|
|
|
|
response = self.session.get(url, headers=self.headers)
|
|
|
|
|
response.raise_for_status()
|
2025-02-08 00:16:06 -05:00
|
|
|
|
|
|
|
|
# Log raw response
|
|
|
|
|
logger.debug(f"Response status: {response.status_code}")
|
|
|
|
|
logger.debug(f"Response headers: {response.headers}")
|
|
|
|
|
logger.debug(f"Raw response text: {response.text}")
|
|
|
|
|
|
2025-02-08 00:13:06 -05:00
|
|
|
devices_data = response.json()
|
2025-02-08 00:16:06 -05:00
|
|
|
logger.debug(f"Parsed JSON: {devices_data}")
|
2025-02-08 00:13:06 -05:00
|
|
|
|
2025-02-08 00:16:06 -05:00
|
|
|
# Extract network_devices from the response
|
|
|
|
|
network_devices = devices_data.get('network_devices', [])
|
2025-02-08 00:13:06 -05:00
|
|
|
|
|
|
|
|
devices = []
|
2025-02-08 00:16:06 -05:00
|
|
|
for device in network_devices:
|
2025-02-08 00:13:06 -05:00
|
|
|
devices.append({
|
|
|
|
|
'name': device.get('name', 'Unknown'),
|
|
|
|
|
'ip': device.get('ip', '0.0.0.0'),
|
|
|
|
|
'type': device.get('type', 'unknown'),
|
|
|
|
|
'connection_type': 'fiber' if device.get('uplink', {}).get('media') == 'sfp' else 'copper',
|
2025-02-08 00:16:06 -05:00
|
|
|
'critical': True if device.get('type') in ['udm', 'usw'] else False,
|
2025-02-08 00:13:06 -05:00
|
|
|
'device_id': device.get('mac')
|
|
|
|
|
})
|
2025-02-08 00:16:06 -05:00
|
|
|
|
|
|
|
|
logger.debug(f"Processed devices: {devices}")
|
2025-02-08 00:13:06 -05:00
|
|
|
return devices
|
2025-02-08 00:16:06 -05:00
|
|
|
|
2025-02-08 00:04:42 -05:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error fetching devices: {e}")
|
2025-02-08 00:16:06 -05:00
|
|
|
logger.exception("Full traceback:")
|
2025-02-08 00:16:45 -05:00
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
def get_device_details(self, device_id):
|
2025-02-08 00:11:28 -05:00
|
|
|
try:
|
|
|
|
|
url = f"{self.base_url}/proxy/network/v2/api/site/{self.site_id}/device/{device_id}"
|
|
|
|
|
response = self.session.get(url, headers=self.headers)
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
return response.json()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to get device details: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
2025-02-07 20:55:38 -05:00
|
|
|
def get_device_diagnostics(self, device):
|
2025-02-07 23:57:34 -05:00
|
|
|
details = self.get_device_details(device['device_id'])
|
2025-02-07 20:55:38 -05:00
|
|
|
if not details:
|
|
|
|
|
return {'state': 'ERROR', 'error': 'Failed to fetch device details'}
|
|
|
|
|
|
|
|
|
|
diagnostics = {
|
2025-02-07 23:57:34 -05:00
|
|
|
'state': details.get('state', 'unknown'),
|
2025-02-08 00:32:25 -05:00
|
|
|
'interfaces': {
|
|
|
|
|
'ports': {}
|
|
|
|
|
}
|
2025-02-07 20:55:38 -05:00
|
|
|
}
|
2025-02-08 00:32:25 -05:00
|
|
|
|
|
|
|
|
# Parse port information
|
|
|
|
|
for port in details.get('port_table', []):
|
|
|
|
|
diagnostics['interfaces']['ports'][f"Port {port.get('port_idx')}"] = {
|
|
|
|
|
'state': 'up' if port.get('up') else 'down',
|
|
|
|
|
'speed': {
|
|
|
|
|
'current': port.get('speed', 0),
|
|
|
|
|
'max': port.get('max_speed', 0)
|
|
|
|
|
},
|
|
|
|
|
'poe': port.get('poe_enable', False),
|
|
|
|
|
'media': port.get('media', 'unknown')
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 20:55:38 -05:00
|
|
|
return diagnostics
|
|
|
|
|
|
|
|
|
|
def _parse_interfaces(self, interfaces):
|
|
|
|
|
result = {
|
|
|
|
|
'ports': {},
|
|
|
|
|
'radios': {}
|
|
|
|
|
}
|
2025-02-08 00:11:28 -05:00
|
|
|
for port in interfaces:
|
|
|
|
|
result['ports'][f"port_{port['index']}"] = {
|
|
|
|
|
'state': port['up'] and 'up' or 'down',
|
2025-02-07 20:55:38 -05:00
|
|
|
'speed': {
|
2025-02-08 00:11:28 -05:00
|
|
|
'current': port.get('speed', 0),
|
|
|
|
|
'max': port.get('max_speed', 0)
|
2025-02-07 20:55:38 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-07 22:47:04 -05:00
|
|
|
return result
|
2025-02-08 00:03:01 -05:00
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
|
def home():
|
|
|
|
|
config = load_config()
|
|
|
|
|
unifi = UnifiAPI(config)
|
|
|
|
|
devices = unifi.get_devices()
|
|
|
|
|
return render_template('index.html', devices=devices)
|
2025-02-07 21:03:31 -05:00
|
|
|
|
|
|
|
|
@app.route('/api/status')
|
|
|
|
|
def status():
|
|
|
|
|
return jsonify(device_status)
|
|
|
|
|
|
2025-02-07 20:55:38 -05:00
|
|
|
@app.route('/api/diagnostics')
|
|
|
|
|
def get_diagnostics():
|
2025-02-08 00:11:28 -05:00
|
|
|
config = load_config()
|
|
|
|
|
unifi = UnifiAPI(config)
|
|
|
|
|
devices = unifi.get_devices()
|
|
|
|
|
diagnostics = {}
|
|
|
|
|
for device in devices:
|
|
|
|
|
diagnostics[device['name']] = unifi.get_device_diagnostics(device)
|
|
|
|
|
return jsonify(diagnostics)
|
2025-01-04 01:42:16 -05:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
status_thread = threading.Thread(target=update_status, daemon=True)
|
|
|
|
|
status_thread.start()
|
2025-02-07 21:03:31 -05:00
|
|
|
app.run(debug=True)
|