Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python v3 compatible version #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
```

69 changes: 33 additions & 36 deletions src/hpilo_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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']
Expand All @@ -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"]
Expand Down Expand Up @@ -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()
Expand All @@ -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()