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: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-07 23:17:12 -05:00
|
|
|
|
2025-02-07 21:53:12 -05:00
|
|
|
def get_all_devices(self):
|
2025-02-07 23:24:36 -05:00
|
|
|
url = f"{self.base_url}/proxy/network/v2/api/site/{self.site_id}/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:26:41 -05:00
|
|
|
return response.json() # Return the full response
|
2025-02-07 23:24:36 -05:00
|
|
|
|
2025-02-07 23:20:14 -05:00
|
|
|
def get_device_details(self, device_id):
|
2025-02-07 23:24:36 -05:00
|
|
|
url = f"{self.base_url}/proxy/network/v2/api/site/{self.site_id}/device/{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 23:26:41 -05:00
|
|
|
return response.json() # Return the full response
|
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 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'),
|
|
|
|
|
'system': {
|
|
|
|
|
'cpu': details.get('system_stats', {}).get('cpu', 0),
|
|
|
|
|
'memory': details.get('system_stats', {}).get('mem', 0),
|
|
|
|
|
'temperature': details.get('general_temperature', 0)
|
2025-02-07 20:55:38 -05:00
|
|
|
},
|
2025-02-07 23:57:34 -05:00
|
|
|
'interfaces': self._parse_interfaces(details.get('port_table', []))
|
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)
|