stranck-dev #18
14
app.py
14
app.py
|
@ -16,6 +16,9 @@ import requests
|
||||||
import sys
|
import sys
|
||||||
from sanic.log import logger, logging
|
from sanic.log import logger, logging
|
||||||
|
|
||||||
|
if METRICS:
|
||||||
|
from sanic_prometheus import monitor
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
app.static("/res", "res/")
|
app.static("/res", "res/")
|
||||||
|
|
||||||
|
@ -36,6 +39,8 @@ app.blueprint([room_bp, karaoke_bp, propic_bp, export_bp, stats_bp, api_bp, carp
|
||||||
|
|
||||||
@app.exception(exceptions.SanicException)
|
@app.exception(exceptions.SanicException)
|
||||||
async def clear_session(request, exception):
|
async def clear_session(request, exception):
|
||||||
|
print(exception)
|
||||||
|
print(request)
|
||||||
tpl = app.ctx.tpl.get_template('error.html')
|
tpl = app.ctx.tpl.get_template('error.html')
|
||||||
r = html(tpl.render(exception=exception))
|
r = html(tpl.render(exception=exception))
|
||||||
|
|
||||||
|
@ -207,4 +212,13 @@ if __name__ == "__main__":
|
||||||
pass
|
pass
|
||||||
sleep(5)
|
sleep(5)
|
||||||
print("Connected to pretix!", file=sys.stderr)
|
print("Connected to pretix!", file=sys.stderr)
|
||||||
|
|
||||||
|
if(METRICS):
|
||||||
|
if(METRICS_USE_ANOTHER_SOCKET):
|
||||||
|
print(f"Startin metrics server on {METRICS_IP}:{METRICS_PORT} on path '{METRICS_PATH}'")
|
||||||
|
monitor(app, metrics_path=METRICS_PATH).start_server(addr=METRICS_IP, port=METRICS_PORT)
|
||||||
|
else:
|
||||||
|
print(f"Startin metrics server on path '{METRICS_PATH}'")
|
||||||
|
monitor(app, metrics_path=METRICS_PATH).expose_endpoint()
|
||||||
|
|
||||||
app.run(host="127.0.0.1", port=8188, dev=DEV_MODE, access_log=ACCESS_LOG)
|
app.run(host="127.0.0.1", port=8188, dev=DEV_MODE, access_log=ACCESS_LOG)
|
||||||
|
|
|
@ -40,6 +40,12 @@ DEV_MODE = True
|
||||||
ACCESS_LOG = True
|
ACCESS_LOG = True
|
||||||
EXTRA_PRINTS = True
|
EXTRA_PRINTS = True
|
||||||
|
|
||||||
|
METRICS = True
|
||||||
|
METRICS_USE_ANOTHER_SOCKET = True
|
||||||
|
METRICS_IP = "172.17.0.1"
|
||||||
|
METRICS_PORT = "1211"
|
||||||
|
METRICS_PATH = "/metrics"
|
||||||
|
|
||||||
# Additional configured locales.
|
# Additional configured locales.
|
||||||
# If an order has a country that's not listed here,
|
# If an order has a country that's not listed here,
|
||||||
# Will default to an english preference.
|
# Will default to an english preference.
|
||||||
|
|
|
@ -4,4 +4,5 @@ httpx
|
||||||
Pillow
|
Pillow
|
||||||
aztec_code_generator
|
aztec_code_generator
|
||||||
Jinja2
|
Jinja2
|
||||||
Requests
|
Requests
|
||||||
|
sanic_prometheus
|
|
@ -0,0 +1,91 @@
|
||||||
|
import psutil
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from sanic import Sanic
|
||||||
|
from sanic import response as res
|
||||||
|
|
||||||
|
DEV_MODE = False
|
||||||
|
ACCESS_LOG = False
|
||||||
|
|
||||||
|
app = Sanic(__name__)
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
async def main(req):
|
||||||
|
return res.text("I\'m a teapot", status=418)
|
||||||
|
|
||||||
|
@app.route("/metrics")
|
||||||
|
async def metrics(req):
|
||||||
|
out = []
|
||||||
|
|
||||||
|
cpus = psutil.cpu_percent(percpu=True)
|
||||||
|
totalCpu = 0
|
||||||
|
for i in range(len(cpus)):
|
||||||
|
out.append(f'monitor_cpu_core_usage_percent{{core="{i}"}} {cpus[i]}')
|
||||||
|
totalCpu += cpus[i]
|
||||||
|
out.append(f'monitor_cpu_core_usage_percent{{core="avg"}} {"%.2f" % (totalCpu / len(cpus))}')
|
||||||
|
|
||||||
|
diskIo = psutil.disk_io_counters(nowrap=True)
|
||||||
|
out.append(f'monitor_diskio{{value="read_count"}} {diskIo.read_count}')
|
||||||
|
out.append(f'monitor_diskio{{value="write_count"}} {diskIo.write_count}')
|
||||||
|
out.append(f'monitor_diskio{{value="read_bytes"}} {diskIo.read_bytes}')
|
||||||
|
out.append(f'monitor_diskio{{value="write_bytes"}} {diskIo.write_bytes}')
|
||||||
|
out.append(f'monitor_diskio{{value="read_time"}} {diskIo.read_time}')
|
||||||
|
out.append(f'monitor_diskio{{value="write_time"}} {diskIo.write_time}')
|
||||||
|
|
||||||
|
disks = psutil.disk_partitions()
|
||||||
|
for disk in disks:
|
||||||
|
try:
|
||||||
|
dUsage = psutil.disk_usage(disk.mountpoint)
|
||||||
|
out.append(f'monitor_disk_usage_percent{{partition="{disk.mountpoint}"}} {dUsage.percent}')
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
mem = psutil.virtual_memory()
|
||||||
|
out.append(f'monitor_memory{{value="total"}} {mem.total}')
|
||||||
|
out.append(f'monitor_memory{{value="available"}} {mem.available}')
|
||||||
|
out.append(f'monitor_memory{{value="percent"}} {mem.percent}')
|
||||||
|
out.append(f'monitor_memory{{value="used"}} {mem.used}')
|
||||||
|
out.append(f'monitor_memory{{value="free"}} {mem.free}')
|
||||||
|
|
||||||
|
swap = psutil.swap_memory()
|
||||||
|
out.append(f'monitor_swap{{value="total"}} {swap.total}')
|
||||||
|
out.append(f'monitor_swap{{value="used"}} {swap.used}')
|
||||||
|
out.append(f'monitor_swap{{value="free"}} {swap.free}')
|
||||||
|
out.append(f'monitor_swap{{value="percent"}} {swap.percent}')
|
||||||
|
out.append(f'monitor_swap{{value="sin"}} {swap.sin}')
|
||||||
|
out.append(f'monitor_swap{{value="sout"}} {swap.sout}')
|
||||||
|
|
||||||
|
bootTime = psutil.boot_time()
|
||||||
|
out.append(f'monitor_boot_time{{}} {int(time.time() - bootTime)}')
|
||||||
|
|
||||||
|
netioConnections = psutil.net_connections()
|
||||||
|
out.append(f'monitor_netio_connections{{}} {len(netioConnections)}')
|
||||||
|
netioCounters = psutil.net_io_counters(nowrap=True)
|
||||||
|
out.append(f'monitor_netio_counters{{value="bytes_sent"}} {netioCounters.bytes_sent}')
|
||||||
|
out.append(f'monitor_netio_counters{{value="bytes_recv"}} {netioCounters.bytes_recv}')
|
||||||
|
out.append(f'monitor_netio_counters{{value="packets_sent"}} {netioCounters.packets_sent}')
|
||||||
|
out.append(f'monitor_netio_counters{{value="packets_recv"}} {netioCounters.packets_recv}')
|
||||||
|
out.append(f'monitor_netio_counters{{value="errin"}} {netioCounters.errin}')
|
||||||
|
out.append(f'monitor_netio_counters{{value="errout"}} {netioCounters.errout}')
|
||||||
|
out.append(f'monitor_netio_counters{{value="dropin"}} {netioCounters.dropin}')
|
||||||
|
out.append(f'monitor_netio_counters{{value="dropout"}} {netioCounters.dropout}')
|
||||||
|
|
||||||
|
return res.text("\n".join(out), status=200)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ip = "127.0.0.1"
|
||||||
|
port = 2611
|
||||||
|
if(len(sys.argv) > 1):
|
||||||
|
ip = sys.argv[1]
|
||||||
|
if(len(sys.argv) > 2):
|
||||||
|
try:
|
||||||
|
port = int(sys.argv[2])
|
||||||
|
except:
|
||||||
|
print("Port must be a numeric value!")
|
||||||
|
exit(1)
|
||||||
|
if(port < 1 or port > 0xffff):
|
||||||
|
print("Port must be in [1, 65535] range!")
|
||||||
|
exit(1)
|
||||||
|
app.run(host=ip, port=port, dev=DEV_MODE, access_log=ACCESS_LOG)
|
Loading…
Reference in New Issue