From 44f4225d4d9f7d97ad229ec88d7c55e830c52123 Mon Sep 17 00:00:00 2001 From: root-ali Date: Wed, 12 Jan 2022 22:45:23 +0330 Subject: [PATCH 1/3] support python v3 --- src/hpilo_exporter/exporter.py | 69 ++++++++++++++++------------------ 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/src/hpilo_exporter/exporter.py b/src/hpilo_exporter/exporter.py index 319e89d..9a24d2b 100644 --- a/src/hpilo_exporter/exporter.py +++ b/src/hpilo_exporter/exporter.py @@ -3,22 +3,20 @@ """ from __future__ import print_function from _socket import gaierror -import sys import hpilo import time -import prometheus_metrics -from BaseHTTPServer import BaseHTTPRequestHandler -from BaseHTTPServer import HTTPServer -from SocketServer import ForkingMixIn +from . import prometheus_metrics +from http.server import BaseHTTPRequestHandler +from http.server import HTTPServer +from socketserver import ForkingMixIn from prometheus_client import generate_latest, Summary -from urlparse import parse_qs -from urlparse import urlparse +from urllib.parse import parse_qs +from urllib.parse import urlparse +import logging -def print_err(*args, **kwargs): - print(*args, file=sys.stderr, **kwargs) - +logging.basicConfig(level=logging.DEBUG, format=f'%(asctime)s %(levelname)s %(name)s: %(message)s') # Create a metric to track time spent and requests made. REQUEST_TIME = Summary( @@ -61,8 +59,9 @@ def do_GET(self): ilo_port = int(query_components['ilo_port'][0]) ilo_user = query_components['ilo_user'][0] ilo_password = query_components['ilo_password'][0] - except KeyError, e: - print_err("missing parameter %s" % e) + logging.info("host and port is %s , %s" , ilo_host , ilo_port) + except ( KeyError ) as e: + logging.error("missing parameter %s" % e) self.return_error() error_detected = True @@ -74,14 +73,15 @@ def do_GET(self): login=ilo_user, password=ilo_password, port=ilo_port, timeout=10) + logging.info(ilo) except hpilo.IloLoginFailed: - print("ILO login failed") + logging.info("ILO login failed") self.return_error() except gaierror: - print("ILO invalid address or port") + logging.info("ILO invalid address or port") self.return_error() - except hpilo.IloCommunicationError, e: - print(e) + except hpilo.IloCommunicationError as e: + logging.error(e) # get product and server name try: @@ -95,7 +95,7 @@ def do_GET(self): server_name = ilo_host except: server_name = ilo_host - + # get health embedded_health = ilo.get_embedded_health() health_at_glance = embedded_health['health_at_a_glance'] @@ -114,22 +114,19 @@ def do_GET(self): else: prometheus_metrics.gauges[gauge].labels(product_name=product_name, server_name=server_name).set(2) - #for iLO3 patch network - if ilo.get_fw_version()["management_processor"] == 'iLO3': - print_err('Unknown iLO nic status') - else: - # get nic information - for nic_name,nic in embedded_health['nic_information'].items(): - try: - value = ['OK','Disabled','Unknown','Link Down'].index(nic['status']) - except ValueError: - value = 4 - print_err('unrecognised nic status: {}'.format(nic['status'])) - - prometheus_metrics.hpilo_nic_status_gauge.labels(product_name=product_name, - server_name=server_name, - nic_name=nic_name, - ip_address=nic['ip_address']).set(value) + + # get nic information + for nic_name,nic in embedded_health['nic_information'].items(): + try: + value = ['OK','Disabled','Unknown','Link Down'].index(nic['status']) + except ValueError: + value = 4 + logging.error('unrecognised nic status: {}'.format(nic['status'])) + + prometheus_metrics.Gauge(hpilo_nic_status_gauge).labels(product_name=product_name, + server_name=server_name, + nic_name=nic_name, + ip_address=nic['mac_address']).set(value) # get firmware version fw_version = ilo.get_fw_version()["firmware_version"] @@ -176,8 +173,8 @@ def __init__(self, address='0.0.0.0', port=8080, endpoint="/metrics"): self.endpoint = endpoint def print_info(self): - print_err("Starting exporter on: http://{}:{}{}".format(self._address, self._port, self.endpoint)) - print_err("Press Ctrl+C to quit") + logging.info("Starting exporter on: http://{}:{}{}".format(self._address, self._port, self.endpoint)) + logging.info("Press Ctrl+C to quit") def run(self): self.print_info() @@ -189,5 +186,5 @@ def run(self): while True: server.handle_request() except KeyboardInterrupt: - print_err("Killing exporter") + logging.error("Killing exporter") server.server_close() From f9901724d1cadd83e31d95db3a248d5fdfaf768e Mon Sep 17 00:00:00 2001 From: root-ali Date: Wed, 12 Jan 2022 22:45:51 +0330 Subject: [PATCH 2/3] change Dockerfile to use python 3 alpine image --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ff6f2cd..21eca2f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:2.7-alpine +FROM python:3.10-alpine ADD . /usr/src/hpilo_exporter RUN pip install -e /usr/src/hpilo_exporter ENTRYPOINT ["hpilo-exporter"] From 8802b3dde3486dd845dc7c388f76f1346fd4c84e Mon Sep 17 00:00:00 2001 From: root-ali Date: Wed, 12 Jan 2022 22:46:22 +0330 Subject: [PATCH 3/3] some fixes on readme docker run command --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0b8a6c7..c1bbece 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ curl 'http://127.0.0.1:9416/metrics?ilo_host=127.0.0.1&ilo_port=443&ilo_user=adm Passing argument to the docker run command ``` -docker run -p 9416:9416 hpilo-exporter:latest --port 9416 --ilo_user my_user --ilo_password my_secret_password +docker run -p 9416:9416 hpilo-exporter:latest --port 9416 ``` ### Docker compose @@ -141,6 +141,6 @@ Assuming: - source_labels: [__param_ilo_host] target_label: ilo_host - target_label: __address__ - replacement: hpilo:8082 # hpilo exporter. + replacement: hpilo:9416 # hpilo exporter. ```