37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
import os
|
|
import importlib.util
|
|
import time
|
|
from prometheus_client import start_http_server
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
def load_stat_modules(path='stats'):
|
|
modules = []
|
|
for filename in os.listdir(path):
|
|
if filename.endswith(".py") and filename != "__init__.py":
|
|
module_name = filename[:-3]
|
|
filepath = os.path.join(path, filename)
|
|
|
|
spec = importlib.util.spec_from_file_location(module_name, filepath)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
|
|
if hasattr(module, 'update'):
|
|
modules.append(module)
|
|
return modules
|
|
|
|
if __name__ == '__main__':
|
|
port = int(os.getenv("EXPORTER_PORT", "8000"))
|
|
print(f"Starting exporter on port {port}...")
|
|
start_http_server(port)
|
|
|
|
modules = load_stat_modules()
|
|
|
|
while True:
|
|
for mod in modules:
|
|
try:
|
|
mod.update()
|
|
except Exception as e:
|
|
print(f"Error in {mod.__name__}: {e}")
|
|
time.sleep(int(os.getenv("SCRAPE_INTERVAL", "30")))
|