more dynamic
This commit is contained in:
@@ -9,41 +9,16 @@ from flask import Flask, render_template, jsonify
|
|||||||
import requests
|
import requests
|
||||||
from urllib3.exceptions import InsecureRequestWarning
|
from urllib3.exceptions import InsecureRequestWarning
|
||||||
|
|
||||||
# Configure logging
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Disable InsecureRequestWarning
|
|
||||||
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
||||||
|
|
||||||
# Initialize Flask app
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# Global state
|
|
||||||
device_status = {}
|
device_status = {}
|
||||||
|
|
||||||
# Configuration functions
|
|
||||||
def load_config():
|
def load_config():
|
||||||
with open('config.json') as f:
|
with open('config.json') as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
# UniFi API integration
|
|
||||||
class UnifiAPI:
|
class UnifiAPI:
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.base_url = config['unifi']['controller']
|
self.base_url = config['unifi']['controller']
|
||||||
@@ -74,10 +49,15 @@ class UnifiAPI:
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
def get_device_details(self, device_id):
|
def get_device_details(self, device_id):
|
||||||
|
try:
|
||||||
url = f"{self.base_url}/proxy/network/v2/api/site/{self.site_id}/device/{device_id}"
|
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 = self.session.get(url, headers=self.headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()
|
return response.json()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to get device details: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
def get_device_diagnostics(self, device):
|
def get_device_diagnostics(self, device):
|
||||||
details = self.get_device_details(device['device_id'])
|
details = self.get_device_details(device['device_id'])
|
||||||
if not details:
|
if not details:
|
||||||
@@ -99,63 +79,22 @@ class UnifiAPI:
|
|||||||
'ports': {},
|
'ports': {},
|
||||||
'radios': {}
|
'radios': {}
|
||||||
}
|
}
|
||||||
|
for port in interfaces:
|
||||||
for port in interfaces.get('ports', []):
|
result['ports'][f"port_{port['index']}"] = {
|
||||||
result['ports'][f"port_{port['idx']}"] = {
|
'state': port['up'] and 'up' or 'down',
|
||||||
'state': port['state'],
|
|
||||||
'type': port['connector'],
|
|
||||||
'speed': {
|
'speed': {
|
||||||
'current': port['speedMbps'],
|
'current': port.get('speed', 0),
|
||||||
'max': port['maxSpeedMbps']
|
'max': port.get('max_speed', 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for radio in interfaces.get('radios', []):
|
|
||||||
result['radios'][f"{radio['frequencyGHz']}GHz"] = {
|
|
||||||
'standard': radio['wlanStandard'],
|
|
||||||
'channel': radio['channel'],
|
|
||||||
'width': f"{radio['channelWidthMHz']}MHz"
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def run_diagnostics(device):
|
|
||||||
try:
|
|
||||||
config = load_config()
|
|
||||||
unifi = UnifiAPI(config)
|
|
||||||
diagnostics = unifi.get_device_diagnostics(device)
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def home():
|
def home():
|
||||||
config = load_config()
|
config = load_config()
|
||||||
unifi = UnifiAPI(config)
|
unifi = UnifiAPI(config)
|
||||||
devices = unifi.get_devices()
|
devices = unifi.get_devices()
|
||||||
return render_template('index.html', devices=devices)
|
return render_template('index.html', devices=devices)
|
||||||
logger.debug(f"Got diagnostics for {device['name']}: {diagnostics}")
|
|
||||||
return diagnostics
|
|
||||||
except Exception as e:
|
|
||||||
return {"error": str(e)}
|
|
||||||
|
|
||||||
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()
|
|
||||||
devices = config['devices']
|
|
||||||
return render_template('index.html', devices=devices)
|
|
||||||
|
|
||||||
@app.route('/api/status')
|
@app.route('/api/status')
|
||||||
def status():
|
def status():
|
||||||
@@ -163,24 +102,14 @@ def status():
|
|||||||
|
|
||||||
@app.route('/api/diagnostics')
|
@app.route('/api/diagnostics')
|
||||||
def get_diagnostics():
|
def get_diagnostics():
|
||||||
try:
|
|
||||||
config = load_config()
|
config = load_config()
|
||||||
unifi = UnifiAPI(config)
|
unifi = UnifiAPI(config)
|
||||||
devices = unifi.get_all_devices()
|
devices = unifi.get_devices()
|
||||||
|
|
||||||
diagnostics = {}
|
diagnostics = {}
|
||||||
for device in config['devices']:
|
for device in devices:
|
||||||
if device.get('device_id'):
|
diagnostics[device['name']] = unifi.get_device_diagnostics(device)
|
||||||
device_details = unifi.get_device_details(device['device_id'])
|
|
||||||
diagnostics[device['name']] = device_details
|
|
||||||
|
|
||||||
return jsonify(diagnostics)
|
return jsonify(diagnostics)
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Error in diagnostics endpoint: {str(e)}")
|
|
||||||
logger.exception("Full traceback:")
|
|
||||||
return jsonify({"error": str(e)}), 500
|
|
||||||
|
|
||||||
# Application entry point
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
status_thread = threading.Thread(target=update_status, daemon=True)
|
status_thread = threading.Thread(target=update_status, daemon=True)
|
||||||
status_thread.start()
|
status_thread.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user