Add Zabbix API token authentication support
ZABBIX_API_TOKEN env var can now be used instead of ZABBIX_USER + ZABBIX_PASSWORD. When set, the token is passed directly as the auth value in JSON-RPC calls and no login/logout session is created. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,8 @@ YODECK_POLL_INTERVAL_MINUTES = int(os.environ.get('YODECK_POLL_INTERVAL_MINUTES'
|
|||||||
|
|
||||||
# Zabbix (all optional — leave ZABBIX_URL empty to disable)
|
# Zabbix (all optional — leave ZABBIX_URL empty to disable)
|
||||||
ZABBIX_URL = os.environ.get('ZABBIX_URL', '')
|
ZABBIX_URL = os.environ.get('ZABBIX_URL', '')
|
||||||
|
# Authentication: set ZABBIX_API_TOKEN (preferred) OR ZABBIX_USER + ZABBIX_PASSWORD
|
||||||
|
ZABBIX_API_TOKEN = os.environ.get('ZABBIX_API_TOKEN', '')
|
||||||
ZABBIX_USER = os.environ.get('ZABBIX_USER', 'Admin')
|
ZABBIX_USER = os.environ.get('ZABBIX_USER', 'Admin')
|
||||||
ZABBIX_PASSWORD = os.environ.get('ZABBIX_PASSWORD', '')
|
ZABBIX_PASSWORD = os.environ.get('ZABBIX_PASSWORD', '')
|
||||||
ZABBIX_HOST_GROUP = os.environ.get('ZABBIX_HOST_GROUP', 'Yodeck Players')
|
ZABBIX_HOST_GROUP = os.environ.get('ZABBIX_HOST_GROUP', 'Yodeck Players')
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ Each Yodeck player becomes a Zabbix host with:
|
|||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
from app.config import (
|
from app.config import (
|
||||||
ZABBIX_URL, ZABBIX_USER, ZABBIX_PASSWORD,
|
ZABBIX_URL, ZABBIX_API_TOKEN, ZABBIX_USER, ZABBIX_PASSWORD,
|
||||||
ZABBIX_HOST_GROUP, ZABBIX_SNMP_COMMUNITY,
|
ZABBIX_HOST_GROUP, ZABBIX_SNMP_COMMUNITY,
|
||||||
APP_HOST, ENTERPRISE_OID,
|
APP_HOST, ENTERPRISE_OID,
|
||||||
)
|
)
|
||||||
@@ -47,9 +47,14 @@ class ZabbixClient:
|
|||||||
return body['result']
|
return body['result']
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
self._auth = self._call('user.login', {'user': ZABBIX_USER, 'password': ZABBIX_PASSWORD})
|
if ZABBIX_API_TOKEN:
|
||||||
|
self._auth = ZABBIX_API_TOKEN # API token used directly — no session needed
|
||||||
|
else:
|
||||||
|
self._auth = self._call('user.login', {'user': ZABBIX_USER, 'password': ZABBIX_PASSWORD})
|
||||||
|
|
||||||
def logout(self):
|
def logout(self):
|
||||||
|
if ZABBIX_API_TOKEN:
|
||||||
|
return # API tokens have no session to invalidate
|
||||||
try:
|
try:
|
||||||
self._call('user.logout', [])
|
self._call('user.logout', [])
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@@ -28,8 +28,11 @@ services:
|
|||||||
|
|
||||||
# ── Zabbix API (optional) ─────────────────────────────────────────────
|
# ── Zabbix API (optional) ─────────────────────────────────────────────
|
||||||
# Leave ZABBIX_URL empty to disable automatic host management.
|
# Leave ZABBIX_URL empty to disable automatic host management.
|
||||||
# Requires Zabbix 6.0+.
|
# Requires Zabbix 5.4+.
|
||||||
ZABBIX_URL: "https://monitor.stranto.com" # e.g. "http://zabbix.example.com"
|
ZABBIX_URL: "https://monitor.stranto.com"
|
||||||
|
# Use an API token (preferred) OR user + password — not both.
|
||||||
|
# Create token in Zabbix: Administration → API tokens → Create API token
|
||||||
|
ZABBIX_API_TOKEN: ""
|
||||||
ZABBIX_USER: "odmon"
|
ZABBIX_USER: "odmon"
|
||||||
ZABBIX_PASSWORD: "lk09u834ojnaslvnjh09u34g"
|
ZABBIX_PASSWORD: "lk09u834ojnaslvnjh09u34g"
|
||||||
ZABBIX_HOST_GROUP: "Yodeck Players"
|
ZABBIX_HOST_GROUP: "Yodeck Players"
|
||||||
|
|||||||
Reference in New Issue
Block a user