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
|
|
|
# Configure logging
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
# Disable InsecureRequestWarning
|
|
|
|
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
|
|
|
|
|
|
|
|
|
# Initialize Flask app
|
2025-01-04 01:42:16 -05:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
2025-02-07 21:03:31 -05:00
|
|
|
# Global state
|
|
|
|
|
device_status = {}
|
2025-01-04 01:42:16 -05:00
|
|
|
|
2025-02-07 21:03:31 -05:00
|
|
|
# Configuration functions
|
2025-01-04 01:42:16 -05:00
|
|
|
def load_config():
|
|
|
|
|
with open('config.json') as f:
|
|
|
|
|
return json.load(f)
|
|
|
|
|
|
2025-02-07 21:03:31 -05:00
|
|
|
# Network utility functions
|
|
|
|
|
def ping(host):
|
|
|
|
|
param = '-n' if platform.system().lower() == 'windows' else '-c'
|
|
|
|
|
command = ['ping', param, '1', host]
|
|
|
|
|
return subprocess.call(command, stdout=subprocess.DEVNULL) == 0
|
2025-01-04 01:42:16 -05:00
|
|
|
|
2025-02-07 20:31:56 -05:00
|
|
|
def send_webhook(device, status, diagnostics):
|
|
|
|
|
config = load_config()
|
|
|
|
|
webhook_data = {
|
|
|
|
|
"device": device,
|
|
|
|
|
"status": status,
|
|
|
|
|
"timestamp": datetime.now().isoformat(),
|
|
|
|
|
"diagnostics": diagnostics
|
|
|
|
|
}
|
|
|
|
|
requests.post(config['webhook_url'], json=webhook_data)
|
|
|
|
|
|
2025-02-07 21:03:31 -05:00
|
|
|
# UniFi API integration
|
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:19:50 -05:00
|
|
|
'Authorization': f'Bearer {config["unifi"]["api_key"]}',
|
2025-02-07 23:17:12 -05:00
|
|
|
'Accept': 'application/json',
|
|
|
|
|
'Content-Type': 'application/json'
|
2025-02-07 22:17:27 -05:00
|
|
|
}
|
2025-02-07 23:06:49 -05:00
|
|
|
self.site_id = self.get_site_id()
|
|
|
|
|
|
|
|
|
|
def get_site_id(self):
|
|
|
|
|
url = f"{self.base_url}/proxy/network/api/self/sites"
|
|
|
|
|
response = self.session.get(url, headers=self.headers)
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
sites = response.json()['data']
|
|
|
|
|
return sites[0]['_id']
|
2025-02-07 23:17:12 -05:00
|
|
|
|
2025-02-07 21:53:12 -05:00
|
|
|
def get_all_devices(self):
|
2025-02-07 23:09:11 -05:00
|
|
|
url = f"{self.base_url}/proxy/network/api/s/{self.site_id}/stat/device"
|
2025-02-07 22:20:20 -05:00
|
|
|
response = self.session.get(url, headers=self.headers)
|
2025-02-07 22:21:34 -05:00
|
|
|
response.raise_for_status()
|
2025-02-07 23:20:14 -05:00
|
|
|
return response.json()['data']
|
|
|
|
|
|
|
|
|
|
def get_device_details(self, device_id):
|
2025-02-07 22:50:52 -05:00
|
|
|
url = f"{self.base_url}/proxy/network/integration/v1/sites/{self.site_id}/devices/{device_id}"
|
2025-02-07 22:14:57 -05:00
|
|
|
response = self.session.get(url, headers=self.headers)
|
2025-02-07 22:21:34 -05:00
|
|
|
response.raise_for_status()
|
2025-02-07 22:50:52 -05:00
|
|
|
logger.debug(f"Device details response: {response.text}")
|
2025-02-07 22:47:04 -05:00
|
|
|
data = response.json()
|
2025-02-07 22:44:45 -05:00
|
|
|
return {
|
|
|
|
|
'state': data['state'],
|
|
|
|
|
'firmware': {
|
2025-02-07 22:47:04 -05:00
|
|
|
'version': data['firmwareVersion'],
|
|
|
|
|
'updatable': data.get('firmwareUpdatable', False)
|
2025-02-07 22:44:45 -05:00
|
|
|
},
|
|
|
|
|
'network': {
|
2025-02-07 22:47:04 -05:00
|
|
|
'ip': data['ipAddress'],
|
|
|
|
|
'mac': data['macAddress']
|
|
|
|
|
}
|
2025-02-07 22:44:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-07 20:55:38 -05:00
|
|
|
def get_device_diagnostics(self, device):
|
2025-02-07 21:53:12 -05:00
|
|
|
details = self.get_device_details(device['ip'])
|
2025-02-07 20:55:38 -05:00
|
|
|
if not details:
|
|
|
|
|
return {'state': 'ERROR', 'error': 'Failed to fetch device details'}
|
|
|
|
|
|
|
|
|
|
diagnostics = {
|
|
|
|
|
'state': details['state'],
|
2025-02-07 22:21:34 -05:00
|
|
|
'firmware': details['firmware'],
|
|
|
|
|
'network': details['network'],
|
2025-02-07 20:55:38 -05:00
|
|
|
'uptime': {
|
2025-02-07 22:21:34 -05:00
|
|
|
'adopted_at': details.get('adoptedAt'),
|
|
|
|
|
'provisioned_at': details.get('provisionedAt')
|
2025-02-07 20:55:38 -05:00
|
|
|
},
|
2025-02-07 22:21:34 -05:00
|
|
|
'interfaces': details['interfaces']
|
2025-02-07 20:55:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return diagnostics
|
|
|
|
|
|
|
|
|
|
def _parse_interfaces(self, interfaces):
|
|
|
|
|
result = {
|
|
|
|
|
'ports': {},
|
|
|
|
|
'radios': {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for port in interfaces.get('ports', []):
|
|
|
|
|
result['ports'][f"port_{port['idx']}"] = {
|
|
|
|
|
'state': port['state'],
|
|
|
|
|
'type': port['connector'],
|
|
|
|
|
'speed': {
|
|
|
|
|
'current': port['speedMbps'],
|
|
|
|
|
'max': port['maxSpeedMbps']
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for radio in interfaces.get('radios', []):
|
|
|
|
|
result['radios'][f"{radio['frequencyGHz']}GHz"] = {
|
|
|
|
|
'standard': radio['wlanStandard'],
|
|
|
|
|
'channel': radio['channel'],
|
|
|
|
|
'width': f"{radio['channelWidthMHz']}MHz"
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 22:47:04 -05:00
|
|
|
return result
|
2025-02-07 20:31:56 -05:00
|
|
|
def run_diagnostics(device):
|
2025-02-07 21:36:44 -05:00
|
|
|
try:
|
|
|
|
|
config = load_config()
|
|
|
|
|
unifi = UnifiAPI(config)
|
|
|
|
|
diagnostics = unifi.get_device_diagnostics(device)
|
|
|
|
|
logger.debug(f"Got diagnostics for {device['name']}: {diagnostics}")
|
|
|
|
|
return diagnostics
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error getting diagnostics for {device['name']}: {str(e)}")
|
|
|
|
|
return {"error": str(e)}
|
2025-02-07 20:55:38 -05:00
|
|
|
|
2025-02-07 21:03:31 -05:00
|
|
|
def update_status():
|
|
|
|
|
while True:
|
|
|
|
|
config = load_config()
|
|
|
|
|
for device in config['devices']:
|
|
|
|
|
current_status = ping(device['ip'])
|
|
|
|
|
previous_status = device_status.get(device['name'], True)
|
|
|
|
|
|
|
|
|
|
if current_status != previous_status:
|
|
|
|
|
diagnostics = run_diagnostics(device)
|
|
|
|
|
send_webhook(device, current_status, diagnostics)
|
|
|
|
|
|
|
|
|
|
device_status[device['name']] = current_status
|
|
|
|
|
time.sleep(config['check_interval'])
|
|
|
|
|
|
|
|
|
|
# Flask routes
|
|
|
|
|
@app.route('/')
|
|
|
|
|
def home():
|
|
|
|
|
config = load_config()
|
|
|
|
|
logger.debug(f"Loaded devices: {config['devices']}")
|
2025-02-07 21:22:43 -05:00
|
|
|
devices = config['devices']
|
|
|
|
|
logger.debug(f"Rendering template with devices: {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-07 21:36:44 -05:00
|
|
|
try:
|
|
|
|
|
config = load_config()
|
2025-02-07 22:03:19 -05:00
|
|
|
unifi = UnifiAPI(config)
|
2025-02-07 22:48:55 -05:00
|
|
|
logger.debug("Getting all devices...")
|
2025-02-07 22:03:19 -05:00
|
|
|
devices = unifi.get_all_devices()
|
2025-02-07 22:48:55 -05:00
|
|
|
logger.debug(f"Raw devices response: {devices}")
|
|
|
|
|
|
2025-02-07 21:36:44 -05:00
|
|
|
diagnostics = {}
|
|
|
|
|
for device in config['devices']:
|
2025-02-07 22:48:55 -05:00
|
|
|
if device.get('device_id'):
|
|
|
|
|
logger.debug(f"Getting diagnostics for {device['name']}")
|
|
|
|
|
device_details = unifi.get_device_details(device['device_id'])
|
|
|
|
|
logger.debug(f"Raw device details: {device_details}")
|
2025-02-07 22:03:19 -05:00
|
|
|
diagnostics[device['name']] = device_details
|
2025-02-07 22:48:55 -05:00
|
|
|
|
2025-02-07 21:36:44 -05:00
|
|
|
return jsonify(diagnostics)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error in diagnostics endpoint: {str(e)}")
|
2025-02-07 22:48:55 -05:00
|
|
|
logger.exception("Full traceback:")
|
|
|
|
|
return jsonify({"error": str(e)}), 500
|
2025-01-04 01:42:16 -05:00
|
|
|
|
2025-02-07 21:03:31 -05:00
|
|
|
# Application entry point
|
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)
|