Fixed authentication variable problems

This commit is contained in:
TheNetworkGuy 2024-06-12 14:57:35 +02:00
parent 7099df93d1
commit ecec3ee46e

View File

@ -6,7 +6,7 @@ import logging
import argparse import argparse
from os import environ, path, sys from os import environ, path, sys
from pynetbox import api from pynetbox import api
from zabbix_utils import ZabbixAPI, APIRequestError from zabbix_utils import ZabbixAPI, APIRequestError, ProcessingError
from modules.device import NetworkDevice from modules.device import NetworkDevice
from modules.tools import convert_recordset, proxy_prepper from modules.tools import convert_recordset, proxy_prepper
from modules.exceptions import EnvironmentVarError, HostgroupError, SyncError from modules.exceptions import EnvironmentVarError, HostgroupError, SyncError
@ -24,7 +24,7 @@ try:
except ModuleNotFoundError: except ModuleNotFoundError:
print("Configuration file config.py not found in main directory." print("Configuration file config.py not found in main directory."
"Please create the file or rename the config.py.example file to config.py.") "Please create the file or rename the config.py.example file to config.py.")
sys.exit(0) sys.exit(1)
# Set logging # Set logging
log_format = logging.Formatter('%(asctime)s - %(name)s - ' log_format = logging.Formatter('%(asctime)s - %(name)s - '
@ -90,17 +90,15 @@ def main(arguments):
raise HostgroupError(e) raise HostgroupError(e)
# Set Zabbix API # Set Zabbix API
try: try:
zabbix = ZabbixAPI(zabbix_host, password=zabbix_pass) if not zabbix_token:
if "ZABBIX_TOKEN" in env_vars: zabbix = ZabbixAPI(zabbix_host, user=zabbix_user, password=zabbix_pass)
zabbix.login(token=zabbix_token)
else: else:
m=("Logging in with Zabbix user and password," zabbix = ZabbixAPI(zabbix_host, token=zabbix_token)
" consider using an API token instead.") zabbix.check_auth()
logger.warning(m) except (APIRequestError, ProcessingError) as e:
zabbix.login(user=zabbix_user, password=zabbix_pass) e = f"Zabbix returned the following error: {str(e)}"
except APIRequestError as e:
e = f"Zabbix returned the following error: {str(e)}."
logger.error(e) logger.error(e)
sys.exit(1)
# Set API parameter mapping based on API version # Set API parameter mapping based on API version
if not str(zabbix.version).startswith('7'): if not str(zabbix.version).startswith('7'):
proxy_name = "host" proxy_name = "host"