-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
234450b
commit f62016f
Showing
14 changed files
with
239 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,31 @@ | ||
Python3 script to report WA hadoop3 key stats | ||
|
||
|
||
CURRENTLY, BELOW systemctl SETUP NOT WORK - ISSUE WITH D-BUS AND monitor USER | ||
|
||
To set up as a systemctl daemon: | ||
* Ensure user systemd directory exists | ||
* mkdir -p ~/.config/systemd/user | ||
|
||
* Create service file as ~/.config/systemd/user/h3exporter.service, chmod 644 | ||
|
||
``` | ||
[Unit] | ||
Description=h3exporter systemctl configuration | ||
After=network.target | ||
|
||
[Service] | ||
Type=simple | ||
ExecStart=/home/monitor/github/ukwa-monitor/h3exporter/run_h3exporter.sh | ||
Restart=always | ||
RestartSec=30 | ||
|
||
[Install] | ||
WantedBy=default.target | ||
``` | ||
|
||
* Reload user systemctl | ||
* systemctl --user daemon-reload | ||
|
||
* Enable and start h3exporter service | ||
* systemctl --user enable --now h3exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,49 @@ | ||
[dev] | ||
logfpfn = logs/h3exporter.log | ||
loglevel = DEBUG | ||
sleep = 10 | ||
|
||
# h3nn settings | ||
#h3nnjmx = http://h3nn.api.wa.bl.uk/jmx | ||
#namenodeinfo = {'qry':'Hadoop:service=NameNode', 'name':'NameNodeInfo'} | ||
#fsnamesystem = {'qry':'Hadoop:service=NameNode', 'name':'FSNamesystem'} | ||
namenodeinfo = http://h3nn.api.wa.bl.uk/jmx?qry=Hadoop:service=NameNode,name=NameNodeInfo | ||
fsnamesystem = http://h3nn.api.wa.bl.uk/jmx?qry=Hadoop:service=NameNode,name=FSNamesystem | ||
|
||
# prometheus settings | ||
pushgtw = http://monitor-pushgateway.dapi.wa.bl.uk | ||
job = h3stats | ||
metric = hadoop3stats | ||
desc = Scrapped hadoop3 stats | ||
|
||
|
||
[beta] | ||
logfpfn = logs/h3exporter.log | ||
loglevel = DEBUG | ||
host = 0.0.0.0 | ||
port = 19120 | ||
loglevel = INFO | ||
sleep = 30 | ||
|
||
# h3nn settings | ||
namenodeinfo = http://h3nn.api.wa.bl.uk/jmx?qry=Hadoop:service=NameNode,name=NameNodeInfo | ||
fsnamesystem = http://h3nn.api.wa.bl.uk/jmx?qry=Hadoop:service=NameNode,name=FSNamesystem | ||
|
||
# prometheus settings | ||
pushgtw = http://monitor-pushgateway.bapi.wa.bl.uk | ||
job = h3stats | ||
metric = hadoop3stats | ||
desc = Scrapped hadoop3 stats | ||
|
||
|
||
[prod] | ||
logfpfn = logs/h3exporter.log | ||
loglevel = INFO | ||
|
||
# h3nn settings | ||
sleep = 55 | ||
namenodeinfo = http://h3nn.api.wa.bl.uk/jmx?qry=Hadoop:service=NameNode,name=NameNodeInfo | ||
fsnamesystem = http://h3nn.api.wa.bl.uk/jmx?qry=Hadoop:service=NameNode,name=FSNamesystem | ||
|
||
# prometheus settings | ||
pushgtw = http://monitor-pushgateway.api.wa.bl.uk | ||
job = h3stats | ||
metric = hadoop3stats | ||
desc = Scrapped hadoop3 stats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os, sys, logging | ||
logger = logging.getLogger(__name__) | ||
import requests | ||
import json | ||
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway | ||
|
||
|
||
# internal functions -------- | ||
def _get_url(url): | ||
''' | ||
Seems that hadoop3/jmx "json" isn't wellformed, at least according to | ||
https://jsononline.net/string-to-json and in testing. | ||
So not using qs as requests parameter | ||
(https://docs.python-requests.org/en/latest/user/quickstart/#passing-parameters-in-urls) | ||
but instead using complete url in in requests.get(). | ||
''' | ||
res = False | ||
logger.debug(f"Getting [{url}]") | ||
try: | ||
res = requests.get(url) | ||
except Exception as e: | ||
logger.error(f"Failed to get [{url}]") | ||
return False | ||
|
||
# check response | ||
if res.status_code != requests.codes.ok: | ||
logger.error(f"Response code not okay for [{url}]") | ||
logger.error(f"Response code: [{res.status_code}]") | ||
return False | ||
else: | ||
return res | ||
|
||
# functions ----------------- | ||
def get_hadoop_stats(settings): | ||
status = 0 | ||
usedPercent = 0 | ||
deadNodes = 0 | ||
underReplicated = 0 | ||
|
||
# get urls, check response, gather values | ||
for url in [ settings['namenodeinfo'], settings['fsnamesystem'] ]: | ||
res = _get_url(url) | ||
if not res: | ||
return 1, False, False, False | ||
|
||
# annoyingly, res.json() doesn't create nested json, just series of key/value strings | ||
# Consequently, Hadoop3 json blobs (like DeadNodes) must be converted again into json | ||
# This block is wrapped in try/except in case the returned dataset is unexpected | ||
try: | ||
resJson = res.json() | ||
if 'beans' in resJson: | ||
beans = resJson['beans'] | ||
for _dbeans in beans: | ||
if 'PercentUsed' in _dbeans: | ||
usedPercent = float(_dbeans['PercentUsed']) | ||
if 'DeadNodes' in _dbeans: | ||
deadNodes = len(json.loads(_dbeans['DeadNodes'])) | ||
if 'UnderReplicatedBlocks' in _dbeans: | ||
underReplicated = int(_dbeans['UnderReplicatedBlocks']) | ||
else: | ||
logger.warning(f"No 'beans' in resJson: {resJson}") | ||
status = 1 | ||
except Exception as e: | ||
logger.warning(f"Failed traversing response json [{e}]") | ||
status = 1 | ||
|
||
logger.debug(f"usedPercent:\t\t [{usedPercent}]") | ||
logger.debug(f"deadNodes:\t\t [{deadNodes}]") | ||
logger.debug(f"underReplicated:\t [{underReplicated}]") | ||
return status, usedPercent, deadNodes, underReplicated | ||
|
||
def send_hadoop_stats(settings, usedPercent, deadNodes, underReplicated): | ||
registry = CollectorRegistry() | ||
g = Gauge(settings['metric'], settings['desc'], labelnames=['instance'], registry=registry) | ||
g.labels(instance='usedPercent').set(usedPercent) | ||
g.labels(instance='deadNodes').set(deadNodes) | ||
g.labels(instance='underReplicatedBlocks').set(underReplicated) | ||
|
||
# push to prometheus | ||
try: | ||
push_to_gateway(settings['pushgtw'], registry=registry, job=settings['job']) | ||
logger.info(f"Pushed to gateway {settings['pushgtw']} {usedPercent}%, deadnodes {deadNodes}, under-rep {underReplicated}") | ||
except Exception as e: | ||
logger.warning(f"Failed push to gateway\nError: [{e}]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests | ||
prometheus_client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env bash | ||
#### Swarm environment also determined in script, from hostname, for settings | ||
|
||
# setup venv | ||
export PYTHONPATH=/home/monitor/github/ukwa-monitor/h3exporter | ||
source ${PYTHONPATH}/venv/bin/activate | ||
|
||
# ensure log directory exists | ||
[[ -d ${PYTHONPATH}/logs/ ]] || mkdir ${PYTHONPATH}/logs | ||
|
||
# ensure python libraries installed | ||
cd ${PYTHONPATH} | ||
pip install -r requirements.txt | ||
|
||
# run h3exporter script | ||
${PYTHONPATH}/h3exporter.py & | ||
exit 0 | ||
|
||
|
||
# ------ | ||
if [[ $(hostname -s) =~ ^(prod|monitor) ]]; then | ||
${PYTHONPATH}/h3exporter.py > /dev/null & # disable generation of large logs over time | ||
else | ||
${PYTHONPATH}/h3exporter.py & | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.