· last year · Jul 15, 2024, 09:30 AM
1import requests
2from prometheus_client import start_http_server, Gauge
3import time
4
5# API endpoints
6login_url = "http://1.1.1.1/api/login"
7status_url = "http://1.1.1.1/api/wireless/interfaces/status"
8device_status_url = "http://1.1.1.1/api/system/device/status"
9interface_status_url = "http://1.1.1.1/api/interfaces/status
10
11# Login credentials
12login_payload = {
13 "username": "admin",
14 "password": "admin"
15}
16
17# Prometheus metrics
18frequency_gauge = Gauge('wireless_interface_frequency', 'Frequency of wireless interfaces', ['interface'])
19signal_gauge = Gauge('wireless_interface_signal', 'Signal strength of wireless interfaces', ['interface'])
20tx_rate_gauge = Gauge('wireless_interface_tx_rate', 'TX rate of wireless interfaces', ['interface'])
21rx_rate_gauge = Gauge('wireless_interface_rx_rate', 'RX rate of wireless interfaces', ['interface'])
22macaddr_gauge = Gauge('wireless_interface_macaddr', 'MAC address of clients', ['interface', 'macaddr'])
23ipaddr_gauge = Gauge('wireless_device_ipaddr', 'IP address of the device', ['mask', 'ipaddr'])
24
25def get_token():
26 """Perform the login POST request and retrieve the token."""
27 response = requests.post(login_url, json=login_payload)
28 if response.status_code == 200:
29 response_json = response.json()
30 print("Login response JSON:")
31 print(response_json)
32 token = response_json.get('token')
33 if not token:
34 print("Available keys in the response:", response_json.keys())
35 for key in response_json.keys():
36 if isinstance(response_json[key], dict):
37 token = response_json[key].get('token')
38 if token:
39 break
40 print(f"Extracted token: {token}")
41 return token
42 else:
43 print(f"Login failed: {response.status_code}")
44 print(response.text)
45 return None
46
47def fetch_device_status(token):
48 """Fetch the device status and update the IP address metric."""
49 headers = {
50 "Authorization": f"Bearer {token}"
51 }
52 device_status_response = requests.get(device_status_url, headers=headers)
53 if device_status_response.status_code == 200:
54 device_status_data = device_status_response.json()
55 print("Device Status:")
56 print(device_status_data)
57 ipv4_addresses = device_status_data.get('ipv4-address', [])
58 for ipv4 in ipv4_addresses:
59 mask = ipv4.get('mask')
60 address = ipv4.get('address')
61 if mask is not None and address is not None:
62 ipaddr_gauge.labels(mask=mask, ipaddr=address).set(1)
63 else:
64 print(f"Failed to get device status: {device_status_response.status_code}")
65 print(device_status_response.text)
66
67def fetch_and_update_metrics(token):
68 """Fetch wireless interface status and update Prometheus metrics."""
69 headers = {
70 "Authorization": f"Bearer {token}"
71 }
72 status_response = requests.get(status_url, headers=headers)
73 if status_response.status_code == 200:
74 status_data = status_response.json()
75 print("Wireless Interfaces Status:")
76 print(status_data)
77 # Update metrics with the status data
78 interfaces = status_data.get('data', [])
79 for interface in interfaces:
80 interface_name = interface.get('ifname')
81 frequency = interface.get('freq')
82 signal = interface.get('signal')
83
84 # Extract tx_rate, rx_rate, and macaddr for clients
85 clients = interface.get('clients', [])
86 for client in clients:
87 tx_rate = client.get('tx_rate')
88 rx_rate = client.get('rx_rate')
89 macaddr = client.get('macaddr')
90
91 if tx_rate is not None:
92 tx_rate_gauge.labels(interface=interface_name).set(tx_rate)
93 if rx_rate is not None:
94 rx_rate_gauge.labels(interface=interface_name).set(rx_rate)
95 if macaddr is not None:
96 macaddr_gauge.labels(interface=interface_name, macaddr=macaddr).set(1) # Setting it to 1 to indicate presence
97
98 if interface_name == "wlan0-1":
99 if frequency is not None:
100 frequency_gauge.labels(interface=interface_name).set(frequency)
101 if signal is not None:
102 signal_gauge.labels(interface=interface_name).set(signal)
103 else:
104 print(f"Failed to get status: {status_response.status_code}")
105 print(status_response.text)
106
107def main():
108 # Start the Prometheus exporter
109 start_http_server(8000)
110 print("Prometheus exporter started on port 8000")
111
112 while True:
113 token = get_token()
114 if token:
115 fetch_and_update_metrics(token)
116 fetch_device_status(token)
117 # Sleep for 60 seconds before fetching the data again
118 time.sleep(60)
119
120if __name__ == "__main__":
121 main()
122