This repository has been archived on 2024-10-15. You can view files and clone it, but cannot push or open issues or pull requests.
huawei_lte_exporter/huwawei_lte_exporter

86 lines
3.8 KiB
Plaintext
Raw Permalink Normal View History

2020-04-06 22:27:29 +02:00
#!/usr/bin/env python3
import os
import pprint
2021-06-12 22:25:31 +02:00
import configparser
2020-04-06 22:27:29 +02:00
import http.server as SimpleHTTPServer
import socketserver as SocketServer
from huawei_lte_api.Client import Client
from huawei_lte_api.AuthorizedConnection import AuthorizedConnection
from huawei_lte_api.Connection import Connection
def prom_exporter():
2021-06-12 22:25:31 +02:00
2020-04-06 22:27:29 +02:00
# Auth to router
2021-06-12 22:25:31 +02:00
conn ='http://'+config['DEFAULT']['ROUTER_USER']+':'+config['DEFAULT']['ROUTER_PASS']+'@'+config['DEFAULT']['ROUTER_ADDRESS']+'/'
2020-04-06 22:27:29 +02:00
connection = AuthorizedConnection(conn)
# Initiatise client
client = Client(connection)
if os.environ.get('VERBOSE') is not None:
2020-04-10 12:43:00 +02:00
pprint.pprint(client.device.signal())
pprint.pprint(client.device.information())
# Set common response attributes
band = client.device.signal().get('band')
device = 'deviceName="'+ client.device.information().get('DeviceName')+'",iccid="'+client.device.information().get('Iccid')+'"'
deviceband = device
if band is not None:
deviceband = device+',band="'+band+'"'
# Retrieve attributes
signal = { 'band': { 'help': 'The signal band the LTE connection is using', 'type': 'gauge', 'device': device, 'value': band},
'rsrp': { 'help': 'The average power received from a single Reference signal in dBm', 'type': 'gauge', 'device': deviceband, 'value': client.device.signal().get('rsrp')},
'rsrq': { 'help': 'Indicates quality of the received signal in db', 'type': 'gauge', 'device': deviceband, 'value': client.device.signal().get('rsrq')},
'rssi': { 'help': 'Represents the entire received power including the wanted power from the serving cell as well as all co-channel power and other sources of noise in dBm', 'type': 'gauge', 'device': deviceband, 'value': client.device.signal().get('rssi')},
'rscp': { 'help': 'Denotes the power measured by a receiver on a particular physical communication channel in dBm', 'type': 'gauge', 'device': deviceband, 'value': client.device.signal().get('rscp')},
'sinr': { 'help': 'The signal-to-noise ratio of the given signal in dB', 'type': 'gauge', 'device': deviceband, 'value': client.device.signal().get('sinr')},
'ecio': { 'help': 'The EC/IO is a measure of the quality/cleanliness of the signal from the tower to the modem and indicates the signal-to noise ratio in dB', 'type': 'gauge', 'device': deviceband, 'value': client.device.signal().get('ecio')}
}
if os.environ.get('VERBOSE') is not None:
pprint.pprint(signal)
# Cleanse data
for attribute, info in signal.items():
if info['value'] is not None:
info['value'] = info['value'].replace("dBm", "")
info['value'] = info['value'].replace("dB", "")
2020-04-10 22:37:47 +02:00
info['value'] = info['value'].replace(">=", "")
# Populate response
2020-04-06 22:27:29 +02:00
response=[]
for attribute, info in signal.items():
if attribute is not None and info['value'] is not None:
response.append('#HELP '+attribute+' '+info['help'])
response.append('#TYPE '+attribute+' '+info['type'])
response.append(attribute+'{'+info['device']+'} '+info['value'])
2024-03-28 11:00:54 +01:00
client.user.logout()
2020-04-09 23:05:38 +02:00
return '\n'.join(response)
2020-04-06 22:27:29 +02:00
2020-04-07 20:59:20 +02:00
class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
timeout=5
2020-04-06 22:27:29 +02:00
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(prom_exporter().encode())
2021-06-12 22:25:31 +02:00
#MANDATORY_ENV_VARS = ["ROUTER_ADDRESS", "ROUTER_USER", "ROUTER_PASS", "PROM_PORT"]
#for var in MANDATORY_ENV_VARS:
# if var not in os.environ:
# raise EnvironmentError("Failed because {} is not set.".format(var))
config = configparser.ConfigParser()
2021-06-12 22:34:47 +02:00
config.read('huwawei_lte_exporter.ini')
2021-06-12 22:25:31 +02:00
MANDATORY_ENV_VARS = ["ROUTER_ADDRESS", "ROUTER_USER", "ROUTER_PASS", "PROM_PORT"]
for var in MANDATORY_ENV_VARS:
if var in os.environ:
config['DEFAULT'][var] = os.environ[var]
2020-04-06 22:27:29 +02:00
Handler = GetHandler
2021-06-12 22:25:31 +02:00
httpd = SocketServer.TCPServer(("", int(config['DEFAULT']['PROM_PORT'])), Handler)
2020-04-06 22:27:29 +02:00
httpd.serve_forever()