diff --git a/app.py b/app.py index 7428093..c3f4585 100644 --- a/app.py +++ b/app.py @@ -41,18 +41,107 @@ def send_webhook(device, status, diagnostics): } requests.post(config['webhook_url'], json=webhook_data) +import requests +from urllib3.exceptions import InsecureRequestWarning +requests.packages.urllib3.disable_warnings(InsecureRequestWarning) + +class UnifiAPI: + def __init__(self, config): + self.base_url = config['unifi']['controller'] + self.headers = { + 'X-API-KEY': config['unifi']['api_key'], + 'Accept': 'application/json' + } + self.site_id = config['unifi']['site_id'] + self.session = requests.Session() + self.session.verify = False + + def get_device_details(self, device_id): + try: + url = f"{self.base_url}/proxy/network/integration/v1/sites/{self.site_id}/devices/{device_id}" + response = self.session.get(url, headers=self.headers, timeout=5) + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + logging.error(f"Failed to get device details: {e}") + return None + + def get_device_diagnostics(self, device): + details = self.get_device_details(device['device_id']) + if not details: + return {'state': 'ERROR', 'error': 'Failed to fetch device details'} + + diagnostics = { + 'state': details['state'], + 'firmware': { + 'version': details['firmwareVersion'], + 'updatable': details.get('firmwareUpdatable', False) + }, + 'network': { + 'ip': details['ipAddress'], + 'mac': details['macAddress'] + }, + 'uptime': { + 'adopted_at': details['adoptedAt'], + 'provisioned_at': details['provisionedAt'] + }, + 'interfaces': {} + } + + # Add interface details + if 'interfaces' in details: + diagnostics['interfaces'] = self._parse_interfaces(details['interfaces']) + + 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" + } + + return result + + + def run_diagnostics(device): - diagnostics = {} + config = load_config() + unifi = UnifiAPI(config) + + diagnostics = unifi.get_device_diagnostics(device) + if device['connection_type'] == 'fiber': - # Add your fiber diagnostic commands here - diagnostics['optical_power'] = subprocess.getoutput('optical-power-check ' + device['ip']) - diagnostics['light_levels'] = subprocess.getoutput('light-level-check ' + device['ip']) - else: - # Add your copper diagnostic commands here - diagnostics['cable_test'] = subprocess.getoutput('ethtool ' + device['ip']) - diagnostics['signal_quality'] = subprocess.getoutput('signal-quality-check ' + device['ip']) + # Add fiber-specific diagnostics + sfp_data = get_sfp_diagnostics(device['ip']) + diagnostics['sfp'] = sfp_data + return diagnostics -@app.route('/') + +@app.route('/api/diagnostics') +def get_diagnostics(): + config = load_config() + diagnostics = {} + for device in config['devices']: + if device['device_id']: + diagnostics[device['name']] = run_diagnostics(device) + return jsonify(diagnostics)@app.route('/') def home(): return render_template('index.html') diff --git a/index.html b/index.html index dd770fa..1644c83 100644 --- a/index.html +++ b/index.html @@ -17,49 +17,15 @@