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 <fabrizio.perez@windriver.com>
Change-Id: I913d6d1282bea346f87f73179f0738c0c17d7446
changes/58/822558/5
fperez 12 months ago
parent 6105f83a85
commit e9ba02ab5c
  1. 10
      fm-common/sources/fmDbUtils.cpp
  2. 26
      fm-common/sources/fm_db_sync_event_suppression.py

@ -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

@ -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()

Loading…
Cancel
Save