112 lines
2.9 KiB
Django/Jinja
112 lines
2.9 KiB
Django/Jinja
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
|
|
from psutil import Process, NoSuchProcess
|
|
import requests
|
|
|
|
import sudoisinflux
|
|
|
|
def lockfile(path):
|
|
|
|
def inner(func):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
try:
|
|
with open(path, 'r') as f:
|
|
lockfile_pid = int(f.read().strip())
|
|
p = Process(lockfile_pid)
|
|
print(f"pid {lockfile_pid} has status '{p.status()}'")
|
|
except FileNotFoundError as e:
|
|
# no lockfile exists, proceed as normally
|
|
pass
|
|
except NoSuchProcess:
|
|
# lockfile left over from a dead process
|
|
# for now lets try removing it and running
|
|
os.remove(path)
|
|
|
|
# 'x' = raises FileExistsError (errno 17) if file exists
|
|
with open(path, 'x') as f:
|
|
pid = os.getpid()
|
|
f.write(f"{pid}")
|
|
try:
|
|
func(*args, **kwargs)
|
|
finally:
|
|
os.remove(path)
|
|
|
|
return wrapper
|
|
|
|
return inner
|
|
|
|
|
|
@lockfile("/tmp/xmrigmonitor-poolstats.lock")
|
|
def poolstats():
|
|
try:
|
|
r = requests.get("{{ xmrig_pool_url }}",
|
|
params={
|
|
"address": "{{ xmrig_addr }}",
|
|
"longpoll": "false"
|
|
},
|
|
headers={"accept": "application/json"})
|
|
r.raise_for_status()
|
|
except requests.exceptions.RequestException:
|
|
return
|
|
|
|
j = r.json()
|
|
|
|
def xmr(piconero):
|
|
return float(int(piconero) * 10**(-12))
|
|
|
|
tags = {
|
|
'host': '{{ inventory_hostname }}',
|
|
"address": "{{ xmrig_addr }}",
|
|
}
|
|
|
|
s = j['stats']
|
|
fields = {
|
|
'balance': xmr(s['balance']),
|
|
'last_reward': xmr(s['last_reward'])
|
|
}
|
|
|
|
datapoint = sudoisinflux.datapoint('xmrpool', tags, fields)
|
|
sudoisinflux.write([datapoint], precision="m")
|
|
|
|
@lockfile("/tmp/xmrigmonitor.lock")
|
|
def xmrigmonitor():
|
|
try:
|
|
r = requests.get("http://localhost:{{ xmrig_http_port }}/1/summary")
|
|
r.raise_for_status()
|
|
except requests.exceptions.RequestException:
|
|
# for now fail silently, if the http server isnt up,
|
|
# the xmrig isnt running, so we should be alerted that way.
|
|
return
|
|
|
|
|
|
j = r.json()
|
|
tags = {
|
|
'host': '{{ inventory_hostname }}',
|
|
'worker_id': j['worker_id'],
|
|
'algo': j['algo'],
|
|
'pool': j['connection']['pool']
|
|
}
|
|
|
|
|
|
hashrate = j['hashrate']['total'][0]
|
|
fields = {
|
|
'hashrate': float(0) if hashrate is None else hashrate, #uh..
|
|
'hashrate_highest': j['hashrate']['highest'],
|
|
'hugepages': sudoisinflux.int_if_bool(j['hugepages'])
|
|
}
|
|
|
|
datapoint = sudoisinflux.datapoint('xmrig', tags, fields)
|
|
sudoisinflux.write([datapoint])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if "--poolstats" in sys.argv:
|
|
poolstats()
|
|
else:
|
|
xmrigmonitor()
|