Skip to content

Commit

Permalink
Change log statement levels, change default level to INFO (#85)
Browse files Browse the repository at this point in the history
* Change log statement levels, remove unused

* Refactor log level validator and serializer

* Add pyright config

* Change default log level
  • Loading branch information
pederhan authored Sep 4, 2024
1 parent 5effbd1 commit 790cbc9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ host_modifier_dir = "path/to/host_modifier_dir/"
db_uri = "dbname='zac' user='zabbix' host='localhost' password='secret' port=5432 connect_timeout=2"

# Log level for the application.
log_level = "DEBUG"
log_level = "INFO"

# Health status for each ZAC process.
health_file = "/tmp/zac_health.json"
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ extend-select = [
force-single-line = true
# Add annotations import to every file
required-imports = ["from __future__ import annotations"]

[tool.pyright]
pythonVersion = "3.8"
2 changes: 1 addition & 1 deletion zabbix_auto_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def main() -> None:

time.sleep(1)

logging.debug(
logging.info(
"Queues: %s",
", ".join([str(queue.qsize()) for queue in source_hosts_queues]),
)
Expand Down
14 changes: 10 additions & 4 deletions zabbix_auto_config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ZacSettings(ConfigBaseModel):
source_collector_dir: str
host_modifier_dir: str
db_uri: str
log_level: int = Field(logging.DEBUG, description="The log level to use.")
log_level: int = Field(logging.INFO, description="The log level to use.")
health_file: Optional[Path] = None
failsafe_file: Optional[Path] = None
failsafe_ok_file: Optional[Path] = None
Expand All @@ -164,7 +164,7 @@ def _validate_file_path(
return v

@field_serializer("log_level")
def _serialize_log_level(self, v: str) -> str:
def _serialize_log_level(self, v: int) -> str:
"""Serializes the log level as a string.
Ensures consistent semantics between loading/storing log level in config.
E.g. we dump `"INFO"` instead of `20`.
Expand All @@ -176,6 +176,13 @@ def _serialize_log_level(self, v: str) -> str:
def _validate_log_level(cls, v: Any) -> int:
"""Validates the log level and converts it to an integer.
The log level can be specified as an integer or a string."""
# NOTE: this is basically an overcomplicated version of
# `logging.getLevelName(v)`, but it's necessary for 2 reasons:
# 1. We want to validate that the level is a valid log level.
# `logging.getLevelName(v)` doesn't raise an error if `v` is invalid.
# It just returns `Level <v>`, which is not helpful.
# 2. `logging.getLevelName(v)` with string arguments
# is deprecated in Python 3.10.
if isinstance(v, int):
if v not in logging._levelToName:
raise ValueError(
Expand All @@ -184,8 +191,7 @@ def _validate_log_level(cls, v: Any) -> int:
return v
elif isinstance(v, str):
v = v.upper()
level_int = logging._nameToLevel.get(v, None)
if level_int is None:
if (level_int := logging._nameToLevel.get(v)) is None:
raise ValueError(
f"Invalid log level: {v} is not a valid log level name."
)
Expand Down
43 changes: 19 additions & 24 deletions zabbix_auto_config/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def run(self) -> None:
break

if self.next_update > datetime.datetime.now():
# logging.debug(f"Waiting for next update {self.next_update.isoformat()}")
time.sleep(1)
continue

Expand Down Expand Up @@ -330,7 +329,7 @@ def work(self) -> None:
source = source_hosts["source"]
hosts = source_hosts["hosts"]

logging.debug(
logging.info(
"Handling %d hosts from source, '%s', from queue. Current queue size: %d",
len(source_hosts["hosts"]),
source,
Expand All @@ -350,14 +349,12 @@ def handle_source_host(
if current_host == host:
return HostAction.NO_CHANGE
else:
# logging.debug(f"Replaced host <{host['hostname']}> from source <{source}>")
cursor.execute(
f"UPDATE {self.db_source_table} SET data = %s WHERE data->>'hostname' = %s AND data->'sources' ? %s",
[host.model_dump_json(), host.hostname, source],
)
return HostAction.UPDATE
else:
# logging.debug(f"Inserted host <{host['hostname']}> from source <{source}>")
cursor.execute(
f"INSERT INTO {self.db_source_table} (data) VALUES (%s)",
[host.model_dump_json()],
Expand Down Expand Up @@ -507,17 +504,14 @@ def handle_host(

if current_host:
if current_host == host:
# logging.debug(f"Host <{host['hostname']}> from source <{source}> is equal to current host")
return HostAction.NO_CHANGE
else:
# logging.debug(f"Replaced host <{host['hostname']}> from source <{source}>")
cursor.execute(
f"UPDATE {self.db_hosts_table} SET data = %s WHERE data->>'hostname' = %s",
[host.model_dump_json(), host.hostname],
)
return HostAction.UPDATE
else:
# logging.debug(f"Inserted host <{host['hostname']}> from source <{source}>")
cursor.execute(
f"INSERT INTO {self.db_hosts_table} (data) VALUES (%s)",
[host.model_dump_json()],
Expand Down Expand Up @@ -671,6 +665,7 @@ def __init__(

ver = self.api.apiinfo.version()
self.zabbix_version = Version(ver)
logging.info("Connected to Zabbix API version: %s", ver)

def work(self) -> None:
start_time = time.time()
Expand Down Expand Up @@ -1209,18 +1204,18 @@ def do_update(self) -> None:
db_hostnames.intersection(zabbix_manual_hostnames)
)

logging.debug("Total in zabbix: %d", len(zabbix_hostnames))
logging.debug("Total in db: %d", len(db_hostnames))
logging.debug("Manual in zabbix: %d", len(zabbix_manual_hostnames))
logging.debug("Manual and in source: %d", len(hostnames_in_manual_and_source))
logging.debug(
logging.info("Total in zabbix: %d", len(zabbix_hostnames))
logging.info("Total in db: %d", len(db_hostnames))
logging.info("Manual in zabbix: %d", len(zabbix_manual_hostnames))
logging.info("Manual and in source: %d", len(hostnames_in_manual_and_source))
logging.info(
"Manual and in source: %s", " ".join(hostnames_in_manual_and_source[:10])
)
logging.debug("Only in zabbix: %d", len(hostnames_to_remove))
logging.debug("Only in zabbix: %s", " ".join(hostnames_to_remove[:10]))
logging.debug("Only in db: %d", len(hostnames_to_add))
logging.debug("Only in db: %s", " ".join(hostnames_to_add[:10]))
logging.debug("In both: %d", len(hostnames_in_both))
logging.info("Only in zabbix: %d", len(hostnames_to_remove))
logging.info("Only in zabbix: %s", " ".join(hostnames_to_remove[:10]))
logging.info("Only in db: %d", len(hostnames_to_add))
logging.info("Only in db: %s", " ".join(hostnames_to_add[:10]))
logging.info("In both: %d", len(hostnames_in_both))

# Check if we have too many hosts to add/remove
check_failsafe(self.settings, hostnames_to_add, hostnames_to_remove)
Expand Down Expand Up @@ -1315,7 +1310,7 @@ def do_update(self) -> None:
or zabbix_interface.port != interface.port
or zabbix_interface.useip != useip
):
logging.debug(
logging.info(
"DNS interface of type %s for host '%s' is configured wrong",
interface.type,
db_host.hostname,
Expand All @@ -1336,7 +1331,7 @@ def do_update(self) -> None:
str(details_dict.get(k)) == str(v)
for k, v in interface.details.items()
):
logging.debug(
logging.info(
"SNMP interface for host '%s' differs from source data. Fixing.",
db_host.hostname,
)
Expand Down Expand Up @@ -1446,7 +1441,7 @@ def __init__(

def clear_templates(self, templates: List[Template], host: Host) -> None:
if self.config.dryrun:
logging.debug(
logging.info(
"DRYRUN: Clearing templates %s on host: %s",
", ".join(t.host for t in templates),
host,
Expand All @@ -1469,7 +1464,7 @@ def set_templates(self, templates: List[Template], host: Host) -> None:
to_add = ", ".join(f"{t.host!r}" for t in templates)

if self.config.dryrun:
logging.debug("DRYRUN: Setting templates %s on host: %s", to_add, host)
logging.info("DRYRUN: Setting templates %s on host: %s", to_add, host)
return

try:
Expand Down Expand Up @@ -1595,7 +1590,7 @@ def set_hostgroups(self, host: Host, hostgroups: List[HostGroup]) -> None:
"""Set host groups on a host given a list of host groups."""
to_add = ", ".join(f"{hg.name!r}" for hg in hostgroups)
if self.config.dryrun:
logging.debug("DRYRUN: Setting hostgroups %s on host: %s", to_add, host)
logging.info("DRYRUN: Setting hostgroups %s on host: %s", to_add, host)
return
try:
self.api.set_host_hostgroups(host, hostgroups)
Expand All @@ -1606,7 +1601,7 @@ def set_hostgroups(self, host: Host, hostgroups: List[HostGroup]) -> None:

def create_hostgroup(self, hostgroup_name: str) -> Optional[str]:
if self.config.dryrun:
logging.debug("DRYRUN: Creating hostgroup: '%s'", hostgroup_name)
logging.info("DRYRUN: Creating hostgroup: '%s'", hostgroup_name)
return None

logging.debug("Creating hostgroup: '%s'", hostgroup_name)
Expand Down Expand Up @@ -1637,7 +1632,7 @@ def create_extra_hostgroups(self, existing_hostgroups: List[HostGroup]) -> None:

def create_templategroup(self, templategroup_name: str) -> Optional[str]:
if self.config.dryrun:
logging.debug("DRYRUN: Creating template group: '%s'", templategroup_name)
logging.info("DRYRUN: Creating template group: '%s'", templategroup_name)
return None

logging.debug("Creating template group: '%s'", templategroup_name)
Expand Down
6 changes: 6 additions & 0 deletions zabbix_auto_config/pyzabbix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ def do_request(
try:
response = self.session.post(self.url, json=request_json)
except Exception as e:
logging.error(
"Failed to send request to %s (%s) with params %s",
self.url,
method,
params,
)
raise ZabbixAPIRequestError(
f"Failed to send request to {self.url} ({method}) with params {params}",
params=params,
Expand Down

0 comments on commit 790cbc9

Please sign in to comment.