integrated with unifi api
This commit is contained in:
107
app.py
107
app.py
@ -41,18 +41,107 @@ def send_webhook(device, status, diagnostics):
|
|||||||
}
|
}
|
||||||
requests.post(config['webhook_url'], json=webhook_data)
|
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):
|
def run_diagnostics(device):
|
||||||
diagnostics = {}
|
config = load_config()
|
||||||
|
unifi = UnifiAPI(config)
|
||||||
|
|
||||||
|
diagnostics = unifi.get_device_diagnostics(device)
|
||||||
|
|
||||||
if device['connection_type'] == 'fiber':
|
if device['connection_type'] == 'fiber':
|
||||||
# Add your fiber diagnostic commands here
|
# Add fiber-specific diagnostics
|
||||||
diagnostics['optical_power'] = subprocess.getoutput('optical-power-check ' + device['ip'])
|
sfp_data = get_sfp_diagnostics(device['ip'])
|
||||||
diagnostics['light_levels'] = subprocess.getoutput('light-level-check ' + device['ip'])
|
diagnostics['sfp'] = sfp_data
|
||||||
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'])
|
|
||||||
return diagnostics
|
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():
|
def home():
|
||||||
return render_template('index.html')
|
return render_template('index.html')
|
||||||
|
|
||||||
|
|||||||
66
config.json
66
config.json
@ -1,32 +1,74 @@
|
|||||||
{
|
{
|
||||||
|
"unifi": {
|
||||||
|
"controller": "https://10.10.10.1",
|
||||||
|
"api_key": "kyPfIsAVie3hwMD4Bc1MjAu8N7HVPIb8",
|
||||||
|
"site_id": "default"
|
||||||
|
},
|
||||||
"devices": [
|
"devices": [
|
||||||
{
|
{
|
||||||
"name": "UniFi Dream Machine Pro",
|
"name": "UniFi Dream Machine Pro",
|
||||||
"ip": "192.168.1.1",
|
"ip": "10.10.10.1",
|
||||||
"type": "router",
|
"type": "router",
|
||||||
|
"connection_type": "copper",
|
||||||
|
"critical": true,
|
||||||
|
"device_id": "udm-pro-id"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "USP PDU Pro",
|
||||||
|
"ip": "10.10.10.107",
|
||||||
|
"type": "pdu",
|
||||||
|
"connection_type": "copper",
|
||||||
|
"critical": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "USW Aggregation",
|
||||||
|
"ip": "10.10.10.238",
|
||||||
|
"type": "switch",
|
||||||
|
"connection_type": "fiber",
|
||||||
|
"critical": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "USW Flex Mini",
|
||||||
|
"ip": "10.10.10.169",
|
||||||
|
"type": "switch",
|
||||||
"connection_type": "copper"
|
"connection_type": "copper"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "UniFi Switch 24 PoE",
|
"name": "USW Pro 24 PoE",
|
||||||
"ip": "192.168.1.2",
|
"ip": "10.10.10.139",
|
||||||
"type": "switch",
|
"type": "switch",
|
||||||
"connection_type": "fiber"
|
"connection_type": "fiber",
|
||||||
|
"critical": true,
|
||||||
|
"device_id": "usw-24-poe-id"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "AP Living Room",
|
"name": "U6 Lite",
|
||||||
"ip": "192.168.1.10",
|
"ip": "10.10.10.154",
|
||||||
"type": "access_point"
|
"type": "access_point",
|
||||||
|
"connection_type": "copper"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "AP Office",
|
"name": "USP Strip",
|
||||||
"ip": "192.168.1.11",
|
"ip": "10.10.10.243",
|
||||||
"type": "access_point"
|
"type": "pdu",
|
||||||
|
"connection_type": "copper"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"check_interval": 30,
|
"check_interval": 30,
|
||||||
"webhook_url": "https://your-webhook-url",
|
"webhook_url": "https://your-webhook-url",
|
||||||
|
"alert_thresholds": {
|
||||||
|
"fiber": {
|
||||||
|
"optical_power_min": -10,
|
||||||
|
"optical_power_max": 0,
|
||||||
|
"error_rate_threshold": 0.001
|
||||||
|
},
|
||||||
|
"copper": {
|
||||||
|
"signal_quality_min": 70,
|
||||||
|
"max_cable_length": 100
|
||||||
|
}
|
||||||
|
},
|
||||||
"troubleshooting": {
|
"troubleshooting": {
|
||||||
"fiber_tests": ["optical_power", "light_levels", "error_rate"],
|
"fiber_tests": ["optical_power", "light_levels", "error_rate", "sfp_diagnostics"],
|
||||||
"copper_tests": ["cable_length", "crosstalk", "signal_quality"]
|
"copper_tests": ["cable_length", "crosstalk", "signal_quality", "poe_status"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
46
index.html
46
index.html
@ -17,49 +17,15 @@
|
|||||||
<h2>Network Overview</h2>
|
<h2>Network Overview</h2>
|
||||||
<div id="network-health">
|
<div id="network-health">
|
||||||
<div class="device-status">
|
<div class="device-status">
|
||||||
<span class="status-indicator status-up"></span>
|
<span class="status-indicator"></span>
|
||||||
<span>UniFi Dream Machine Pro</span>
|
<div class="device-info">
|
||||||
</div>
|
<span class="device-name">UniFi Dream Machine Pro</span>
|
||||||
<div class="device-status">
|
<span class="device-details">10.10.10.1</span>
|
||||||
<span class="status-indicator status-up"></span>
|
</div>
|
||||||
<span>UniFi Switch 24 PoE</span>
|
|
||||||
</div>
|
</div>
|
||||||
|
<!-- Repeat for other devices -->
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="metric-card">
|
|
||||||
<h2>Network Performance</h2>
|
|
||||||
<div id="latency"></div>
|
|
||||||
<div id="bandwidth"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="metric-card">
|
|
||||||
<h2>Access Points</h2>
|
|
||||||
<div id="ap-status">
|
|
||||||
<div class="device-status">
|
|
||||||
<span class="status-indicator status-up"></span>
|
|
||||||
<span>AP Living Room</span>
|
|
||||||
</div>
|
|
||||||
<div class="device-status">
|
|
||||||
<span class="status-indicator status-up"></span>
|
|
||||||
<span>AP Office</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="metric-card">
|
|
||||||
<h2>Connected Clients</h2>
|
|
||||||
<div id="client-count"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="metric-card">
|
|
||||||
<h2>Diagnostics</h2>
|
|
||||||
<div id="diagnostics-panel">
|
|
||||||
<div class="diagnostics-content"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<script src="{{ url_for('static', filename='app.js') }}"></script>
|
<script src="{{ url_for('static', filename='app.js') }}"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@ -6,18 +6,77 @@ function updateDiagnostics() {
|
|||||||
diagnosticsPanel.innerHTML = '';
|
diagnosticsPanel.innerHTML = '';
|
||||||
|
|
||||||
Object.entries(data).forEach(([device, diagnostics]) => {
|
Object.entries(data).forEach(([device, diagnostics]) => {
|
||||||
const diagElement = document.createElement('div');
|
const diagElement = createDiagnosticElement(device, diagnostics);
|
||||||
diagElement.className = `diagnostic-item ${diagnostics.type}-diagnostic`;
|
|
||||||
diagElement.innerHTML = `
|
|
||||||
<h3>${device}</h3>
|
|
||||||
<pre>${JSON.stringify(diagnostics.results, null, 2)}</pre>
|
|
||||||
`;
|
|
||||||
diagnosticsPanel.appendChild(diagElement);
|
diagnosticsPanel.appendChild(diagElement);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update diagnostics every minute
|
function createDiagnosticElement(device, diagnostics) {
|
||||||
|
const element = document.createElement('div');
|
||||||
|
element.className = `diagnostic-item ${diagnostics.connection_type}-diagnostic`;
|
||||||
|
|
||||||
|
const content = `
|
||||||
|
<h3>${device}</h3>
|
||||||
|
<div class="diagnostic-details">
|
||||||
|
<div class="status-group">
|
||||||
|
<span class="label">Status:</span>
|
||||||
|
<span class="value ${diagnostics.state.toLowerCase()}">${diagnostics.state}</span>
|
||||||
|
</div>
|
||||||
|
<div class="firmware-group">
|
||||||
|
<span class="label">Firmware:</span>
|
||||||
|
<span class="value">${diagnostics.firmware.version}</span>
|
||||||
|
</div>
|
||||||
|
${createInterfaceHTML(diagnostics.interfaces)}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
element.innerHTML = content;
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createInterfaceHTML(interfaces) {
|
||||||
|
let html = '<div class="interfaces-group">';
|
||||||
|
|
||||||
|
// Add port information
|
||||||
|
Object.entries(interfaces.ports || {}).forEach(([portName, port]) => {
|
||||||
|
html += `
|
||||||
|
<div class="interface-item">
|
||||||
|
<span class="label">${portName}:</span>
|
||||||
|
<span class="value">${port.speed.current}/${port.speed.max} Mbps</span>
|
||||||
|
<span class="state ${port.state.toLowerCase()}">${port.state}</span>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add radio information
|
||||||
|
Object.entries(interfaces.radios || {}).forEach(([radioName, radio]) => {
|
||||||
|
html += `
|
||||||
|
<div class="interface-item">
|
||||||
|
<span class="label">${radioName}:</span>
|
||||||
|
<span class="value">${radio.standard} - Ch${radio.channel} (${radio.width})</span>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
|
||||||
|
html += '</div>';
|
||||||
|
return html;
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateDeviceStatus() {
|
||||||
|
fetch('/api/status')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
Object.entries(data).forEach(([deviceName, status]) => {
|
||||||
|
updateDeviceIndicator(deviceName, status);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update intervals
|
||||||
|
setInterval(updateDeviceStatus, 30000);
|
||||||
setInterval(updateDiagnostics, 60000);
|
setInterval(updateDiagnostics, 60000);
|
||||||
// Initial update
|
|
||||||
|
// Initial updates
|
||||||
updateDeviceStatus();
|
updateDeviceStatus();
|
||||||
|
updateDiagnostics();
|
||||||
|
|||||||
64
style.css
64
style.css
@ -86,4 +86,66 @@ body {
|
|||||||
|
|
||||||
.copper-diagnostic {
|
.copper-diagnostic {
|
||||||
border-color: #F59E0B;
|
border-color: #F59E0B;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.device-info {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.device-details {
|
||||||
|
font-size: 0.8em;
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
|
||||||
|
.diagnostic-details {
|
||||||
|
display: grid;
|
||||||
|
gap: 15px;
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-group, .firmware-group, .interfaces-group {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.interface-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.label {
|
||||||
|
font-weight: 500;
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
|
||||||
|
.value {
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.state {
|
||||||
|
padding: 2px 8px;
|
||||||
|
border-radius: 12px;
|
||||||
|
font-size: 0.8em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.state.up {
|
||||||
|
background-color: #10B981;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.state.down {
|
||||||
|
background-color: #EF4444;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.online {
|
||||||
|
color: #10B981;
|
||||||
|
}
|
||||||
|
|
||||||
|
.offline {
|
||||||
|
color: #EF4444;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user