Skip to content

Commit 23d45e7

Browse files
authored
sq-poller/coalescer: log env info at startup (#857)
* sq-poller/coalescer: log env info at startup Signed-off-by: Claudio Lorina <[email protected]> * Log more detailed info about the cpu Signed-off-by: Claudio Lorina <[email protected]> * Log the history of cpu load Signed-off-by: Claudio Lorina <[email protected]> * Show warning message when min req are not met Signed-off-by: Claudio Lorina <[email protected]> --------- Signed-off-by: Claudio Lorina <[email protected]>
1 parent 9641f68 commit 23d45e7

File tree

6 files changed

+112
-7
lines changed

6 files changed

+112
-7
lines changed

poetry.lock

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ciscoconfparse = "^1.6.21"
4949
notebook = "6.4.12"
5050
urllib3 = "^1.26.12"
5151
packaging = "^21.3"
52+
psutil = "^5.9.4"
5253

5354
[tool.poetry.dev-dependencies]
5455
pylint = "*"

suzieq/poller/sq_poller.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from suzieq.shared.exceptions import InventorySourceError, PollingError, \
1616
SqPollerConfError
1717
from suzieq.shared.utils import (poller_log_params, init_logger,
18-
load_sq_config, print_version)
18+
load_sq_config, print_version,
19+
log_suzieq_info)
1920
from suzieq.poller.controller.utils.inventory_utils import read_inventory
2021

2122

@@ -45,6 +46,8 @@ async def start_controller(user_args: argparse.Namespace, config_data: Dict):
4546
read_inventory(user_args.inventory)
4647
print('Inventory syntax check passed')
4748
return
49+
50+
log_suzieq_info('Poller Controller', logger, show_more=True)
4851
controller = Controller(user_args, config_data)
4952
controller.init()
5053
await controller.run()

suzieq/poller/worker/sq_worker.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
from typing import Dict
99

1010
import uvloop
11+
1112
from suzieq.poller.worker.worker import Worker
1213
from suzieq.poller.worker.writers.output_worker import OutputWorker
1314
from suzieq.shared.exceptions import InventorySourceError, SqPollerConfError
14-
from suzieq.shared.utils import init_logger, load_sq_config, poller_log_params
15+
from suzieq.shared.utils import (init_logger, load_sq_config, log_suzieq_info,
16+
poller_log_params)
1517

1618

1719
async def start_worker(userargs: argparse.Namespace, cfg: Dict):
@@ -29,6 +31,7 @@ async def start_worker(userargs: argparse.Namespace, cfg: Dict):
2931
logger = init_logger('suzieq.poller.worker', logfile,
3032
loglevel, logsize, log_stdout)
3133

34+
log_suzieq_info('Poller Worker', logger)
3235
worker = None
3336
try:
3437
worker = Worker(userargs, cfg)

suzieq/shared/utils.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import logging
66
import os
7+
import platform
78
import re
89
import sys
910
from datetime import datetime
@@ -15,14 +16,16 @@
1516
from os import getenv
1617
from time import time
1718
from typing import Any, Dict, List, Tuple
18-
from tzlocal import get_localzone
1919

2020
import pandas as pd
21+
import psutil
2122
import pyarrow as pa
2223
import yaml
2324
from dateparser import parse
2425
from dateutil.relativedelta import relativedelta
2526
from pytz import all_timezones
27+
from tzlocal import get_localzone
28+
2629
from suzieq.shared.exceptions import SensitiveLoadError
2730
from suzieq.shared.schema import SchemaForTable
2831
from suzieq.version import SUZIEQ_VERSION
@@ -1131,3 +1134,69 @@ def get_default_per_vals() -> Dict:
11311134
pa.list_(pa.int64()): [],
11321135
pa.binary(): b''
11331136
})
1137+
1138+
1139+
def log_suzieq_info(name: str, c_logger: logging.Logger = None,
1140+
show_more=False):
1141+
"""Log the info about the running component. This function changes the
1142+
logging level so that these info are always shown and then sets it back to
1143+
the original value.
1144+
1145+
Args:
1146+
name (str): the name of the running component.
1147+
c_logger (logging.Logger, optional): The logger to use for logging.
1148+
If None the default utils module logger is used. Defaults to None.
1149+
show_more (bool, optional): By defaul the version of SuzieQ and
1150+
of the current Python interpreter are shown. If True, shows
1151+
additional info about the evironment where SuzieQ is running, like
1152+
OS, CPU and memory info. Defaults to False.
1153+
"""
1154+
if not c_logger:
1155+
c_logger = logger
1156+
prev_level = c_logger.level
1157+
if prev_level > logging.INFO:
1158+
c_logger.setLevel(logging.INFO)
1159+
1160+
info_to_show = '\n|-----------------------------------------------------|'
1161+
1162+
info_to_show += f'\nSuzieQ {name} v{SUZIEQ_VERSION} \n' \
1163+
f'Python version: {sys.version}'
1164+
1165+
if show_more:
1166+
# Get processor model. This might not work everywhere, if it doesn't
1167+
# rollback to platform.processor()
1168+
cpu_name = None
1169+
try:
1170+
with open('/proc/cpuinfo', 'r') as ifile:
1171+
model_prefix = 'model name'
1172+
while line := ifile.readline():
1173+
if line.startswith(model_prefix):
1174+
# The line has format: "model name\t: MODEL\n"
1175+
cpu_name = line[len(model_prefix) + 3:-1]
1176+
except Exception:
1177+
pass
1178+
if not cpu_name:
1179+
cpu_name = (platform.processor() or '-')
1180+
1181+
cpu_freq = psutil.cpu_freq()
1182+
cpu_cores = psutil.cpu_count()
1183+
cpu_info = f'{cpu_name} {cpu_cores} cores - '
1184+
cpu_info += f'freq. {cpu_freq.min:.2f}Mhz - {cpu_freq.max:.2f}Mhz'
1185+
cpu_load_history = ', '.join(
1186+
f'{(c / cpu_cores) * 100:.1f}%' for c in psutil.getloadavg())
1187+
mem_info = psutil.virtual_memory()
1188+
info_to_show += f'\nPlatform: {platform.platform()} \n' \
1189+
f'CPU: {cpu_info} \n' \
1190+
f'CPU load 1m, 5m, 15m: {cpu_load_history}\n' \
1191+
f'Memory: total: {mem_info.total}, available: {mem_info.available}'
1192+
1193+
info_to_show += '\n|-----------------------------------------------------|'
1194+
c_logger.info(info_to_show)
1195+
1196+
# Warning if the system has less than 2 cores and 16GB of ram
1197+
if show_more and (cpu_cores < 2 or mem_info.total < 16 * (1024 ** 3)):
1198+
c_logger.warning(
1199+
'Minimum recommended system spec is a modern i7-equivalent or '
1200+
'higher with 4 cores and 16 GB RAM')
1201+
if prev_level > logging.INFO:
1202+
c_logger.setLevel(prev_level)

suzieq/utilities/sq_coalescer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
import pandas as pd
1717

18-
from suzieq.shared.utils import (load_sq_config, init_logger,
19-
ensure_single_instance,
20-
get_log_params, get_sleep_time)
18+
from suzieq.shared.utils import (ensure_single_instance, get_log_params,
19+
get_sleep_time, init_logger, load_sq_config,
20+
log_suzieq_info)
2121
from suzieq.shared.schema import Schema, SchemaForTable
2222
from suzieq.db import do_coalesce, get_sqdb_engine
2323
from suzieq.version import SUZIEQ_VERSION
@@ -75,6 +75,8 @@ def run_coalescer(cfg: dict, tables: List[str], periodstr: str, run_once: bool,
7575
print(f'ERROR: Aborting. Unable to load schema: {str(ex)}')
7676
sys.exit(1)
7777

78+
log_suzieq_info('Coalescer', logger)
79+
7880
coalescer_schema = SchemaForTable('sqCoalescer', schemas)
7981
pqdb = get_sqdb_engine(cfg, 'sqCoalescer', None, logger)
8082

0 commit comments

Comments
 (0)