From e9ba02ab5cb03e94814b15bfacd6397609e3c57a Mon Sep 17 00:00:00 2001 From: fperez Date: Tue, 21 Dec 2021 17:23:33 -0300 Subject: [PATCH] Fixing logging for python scripts - FM This commit complements the previous commit with same topic: https://review.opendev.org/c/starlingx/fault/+/815381 This particular commit improves the log inside the python script, considering others possible fails. Also, some verifications are added in fmDbUtils class wich calls the script. Test Plan: Log in (/var/log/platform.log): PASS: Log arguments error calling script. PASS: Log new database connection problems. PASS: Log Session commit problems. PASS: Log problems opening "/etc/fm/events.yaml" file. Log (in /var/log/fm-manager.log): PASS: Log Problems opening fm_db_sync_event_suppression.py file. PASS: Log problems running fm_db_sync_event_suppression.py. PASS: build and install package. Closes-bug: 1932324 Signed-off-by: fperez Change-Id: I913d6d1282bea346f87f73179f0738c0c17d7446 --- fm-common/sources/fmDbUtils.cpp | 10 ++++++- .../sources/fm_db_sync_event_suppression.py | 26 ++++++++++++------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/fm-common/sources/fmDbUtils.cpp b/fm-common/sources/fmDbUtils.cpp index 6bc0cdfa..dc917498 100644 --- a/fm-common/sources/fmDbUtils.cpp +++ b/fm-common/sources/fmDbUtils.cpp @@ -644,7 +644,15 @@ bool fm_db_util_sync_event_suppression(void){ PySys_SetArgv(argc, argv); #endif file = fopen(FM_DB_SYNC_EVENT_SUPPRESSION,"r"); - PyRun_SimpleFile(file, FM_DB_SYNC_EVENT_SUPPRESSION); + if(file != NULL){ + int rc = PyRun_SimpleFile(file, FM_DB_SYNC_EVENT_SUPPRESSION); + FM_DEBUG_LOG("return code from PyRun_SimpleFile: %d",rc); + if(rc != 0) { + FM_ERROR_LOG("Error running script %s", FM_DB_SYNC_EVENT_SUPPRESSION); + } + }else { + FM_ERROR_LOG("Error opening file %s", FM_DB_SYNC_EVENT_SUPPRESSION); + } fclose(file); Py_Finalize(); #if PY_MAJOR_VERSION >= 3 diff --git a/fm-common/sources/fm_db_sync_event_suppression.py b/fm-common/sources/fm_db_sync_event_suppression.py index affe248e..2ce7ec78 100755 --- a/fm-common/sources/fm_db_sync_event_suppression.py +++ b/fm-common/sources/fm_db_sync_event_suppression.py @@ -8,6 +8,7 @@ import sys import os import json import datetime +import errno import uuid as uuid_gen import yaml @@ -75,7 +76,7 @@ def get_events_yaml_filename(): if len(sys.argv) < 2: msg = 'Postgres credentials required as argument.' LOG.error(msg) - sys.exit(msg) + raise ValueError(msg) postgresql_credentials = str(sys.argv[1]) @@ -88,23 +89,28 @@ try: meta = sqlalchemy.MetaData() engine = sqlalchemy.create_engine(postgresql_credentials) meta.bind = engine + Session = sessionmaker(bind=engine) + session = Session() except exc.SQLAlchemyError as exp: LOG.error(exp) - sys.exit(exp) - -Session = sessionmaker(bind=engine) -session = Session() + raise RuntimeError(exp) # Convert events.yaml to dict: LOG.info("Converting events.yaml to dict: ") EVENT_TYPES_FILE = get_events_yaml_filename() if not os.path.isfile(EVENT_TYPES_FILE): - LOG.error("file %s doesn't exist. Finishing" % (EVENT_TYPES_FILE)) - exit(-1) + LOG.error("file %s doesn't exist. Ending execution" % (EVENT_TYPES_FILE)) + raise OSError( + errno.ENOENT, os.strerror(errno.ENOENT), EVENT_TYPES_FILE + ) -with open(EVENT_TYPES_FILE, 'r') as stream: - event_types = yaml.load(stream) +try: + with open(EVENT_TYPES_FILE, 'r') as stream: + event_types = yaml.load(stream) +except Exception as exp: + LOG.error(exp) + raise RuntimeError(exp) for alarm_id in list(event_types.keys()): if isinstance(alarm_id, float): @@ -172,6 +178,7 @@ for event_type in event_types: session.commit() except exc.SQLAlchemyError as exp: LOG.error(exp) + raise RuntimeError(exp) event_supp = session.query(EventSuppression) alarms = session.query(ialarm) @@ -201,6 +208,7 @@ for event_type in event_supp: session.commit() except exc.SQLAlchemyError as exp: LOG.error(exp) + raise RuntimeError(exp) session.close()