From 8e74a0ee1ee679caabc4686a985010dd3e1f523c Mon Sep 17 00:00:00 2001 From: Pablo Bovina Date: Fri, 23 Oct 2020 10:21:44 -0400 Subject: [PATCH] Reimplementation logic for trap generation In the current implementation "Fault" is responsible for the generation and dispatch of traps. This logic is removed from it, leaving only the responsibility of sending the metadata of a trap to a service dedicated to generating and sending them. The fm.conf file will contain the IP and port values of the service. Story: 2008132 Task: 40867 Depends-On: https://review.opendev.org/761217 Change-Id: I575dfa2329f11821404ca4cb1d539e5189444b4e Signed-off-by: Pablo Bovina --- doc/requirements.txt | 6 +- fm-common/centos/fm-common.spec | 2 + fm-common/sources/Makefile | 2 +- fm-common/sources/fmConstants.h | 3 + fm-common/sources/fmSnmpUtils.cpp | 557 +++++++++++++++++++----------- test-requirements.txt | 2 +- tox.ini | 14 +- 7 files changed, 378 insertions(+), 208 deletions(-) diff --git a/doc/requirements.txt b/doc/requirements.txt index 5f5e4377..dde36d52 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -1,8 +1,8 @@ -sphinx>=2.0.0,!=2.1.0 # BSD -openstackdocstheme>=2.2.1 # Apache-2.0 +sphinx>=1.6.2 # BSD +openstackdocstheme>=1.29.2 # Apache-2.0 # Release Notes documentation -reno>=3.1.0 # Apache-2.0 +reno>=2.11.2 # Apache-2.0 # Api Ref documentation os-api-ref>=1.4.0 # Apache-2.0 diff --git a/fm-common/centos/fm-common.spec b/fm-common/centos/fm-common.spec index 324fb304..a5c36188 100644 --- a/fm-common/centos/fm-common.spec +++ b/fm-common/centos/fm-common.spec @@ -19,6 +19,8 @@ BuildRequires: python-devel BuildRequires: python-setuptools BuildRequires: python2-pip BuildRequires: python2-wheel +Buildrequires: json-c-devel +Requires: json-c %package -n fm-common-dev Summary: CGTS Platform Fault Management Common Package - Development files diff --git a/fm-common/sources/Makefile b/fm-common/sources/Makefile index 7b1ea031..ad7f871f 100755 --- a/fm-common/sources/Makefile +++ b/fm-common/sources/Makefile @@ -4,7 +4,7 @@ SRCS = fmAPI.cpp fmFile.cpp fmLog.cpp fmMsgServer.cpp fmMutex.cpp fmSocket.cpp f CLI_SRCS = fm_cli.cpp OBJS = $(SRCS:.cpp=.o) CLI_OBJS = fm_cli.o -LDLIBS = -lstdc++ -lrt -luuid -lpq -lpthread -lpython2.7 +LDLIBS = -lstdc++ -lrt -luuid -lpq -lpthread -lpython2.7 -ljson-c INCLUDES = -I./ -I$(shell pg_config --includedir) CCFLAGS = -g -O2 -Wall -Werror -fPIC EXTRACCFLAGS= -Wformat -Wformat-security diff --git a/fm-common/sources/fmConstants.h b/fm-common/sources/fmConstants.h index 954c28af..7b649b06 100644 --- a/fm-common/sources/fmConstants.h +++ b/fm-common/sources/fmConstants.h @@ -94,6 +94,9 @@ #define FM_STRING_TRUE "True" #define FM_CONF_PASSWORD "password" #define FM_CONF_CONNECTION "connection" +#define FM_TRAP_SERVER_IP "trap_server_ip" +#define FM_TRAP_SERVER_PORT "trap_server_port" +#define FM_TRAP_SNMP_ENABLED "snmp_enabled" #define CLEAR_ALL_REASON_TEXT "System initiated hierarchical alarm clear" diff --git a/fm-common/sources/fmSnmpUtils.cpp b/fm-common/sources/fmSnmpUtils.cpp index 9177a028..7e63963b 100644 --- a/fm-common/sources/fmSnmpUtils.cpp +++ b/fm-common/sources/fmSnmpUtils.cpp @@ -1,28 +1,36 @@ // -// Copyright (c) 2014-2018 Wind River Systems, Inc. +// Copyright (c) 2014-2020 Wind River Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // -#include -#include -#include -#include -#include #include +#include +#include +#include +#include #include +#include +#include +#include #include +#include -#include "fmDbAPI.h" -#include "fmFile.h" #include "fmAPI.h" -#include "fmMsg.h" -#include "fmLog.h" +#include "fmConfig.h" +#include "fmDbAPI.h" #include "fmDb.h" #include "fmDbUtils.h" +#include "fmFile.h" +#include "fmLog.h" +#include "fmMsg.h" #include "fmSnmpConstants.h" #include "fmSnmpUtils.h" -#include "fmConfig.h" +#include "fmSocket.h" + +#define JSON_TRAP_TAG_ALARM "alarm" +#define JSON_TRAP_TAG_OP_TYPE "operation_type" +#define JSON_TRAP_EMPTY "" typedef std::map int_to_objtype; @@ -31,260 +39,405 @@ static pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; fm_db_result_t &getTrapDestList(){ - static fm_db_result_t trap_dest_list; - return trap_dest_list; + static fm_db_result_t trap_dest_list; + return trap_dest_list; } static void add_to_table(int t, std::string objtype, int_to_objtype &tbl) { - tbl[t]=objtype; + tbl[t]=objtype; } static void init_objtype_table() { - pthread_mutex_lock(&mutex); - static bool has_inited=false; - while (!has_inited){ - add_to_table(FM_ALARM_SEVERITY_CLEAR, ALARM_MSG, objtype_map); - add_to_table(FM_ALARM_SEVERITY_WARNING, ALARM_WARNING, objtype_map); - add_to_table(FM_ALARM_SEVERITY_MINOR, ALARM_MINOR, objtype_map); - add_to_table(FM_ALARM_SEVERITY_MAJOR, ALARM_MAJOR, objtype_map); - add_to_table(FM_ALARM_SEVERITY_CRITICAL, ALARM_CRITICAL, objtype_map); - add_to_table(FM_ALARM_CLEAR, ALARM_CLEAR, objtype_map); - add_to_table(FM_ALARM_HIERARCHICAL_CLEAR, ALARM_HIERARCHICAL_CLEAR, objtype_map); - add_to_table(FM_ALARM_MESSAGE, ALARM_MSG, objtype_map); - add_to_table(FM_WARM_START, WARM_START, objtype_map); - has_inited=true; - } - pthread_mutex_unlock(&mutex); + pthread_mutex_lock(&mutex); + static bool has_inited=false; + while (!has_inited){ + add_to_table(FM_ALARM_SEVERITY_CLEAR, ALARM_MSG, objtype_map); + add_to_table(FM_ALARM_SEVERITY_WARNING, ALARM_WARNING, objtype_map); + add_to_table(FM_ALARM_SEVERITY_MINOR, ALARM_MINOR, objtype_map); + add_to_table(FM_ALARM_SEVERITY_MAJOR, ALARM_MAJOR, objtype_map); + add_to_table(FM_ALARM_SEVERITY_CRITICAL, ALARM_CRITICAL, objtype_map); + add_to_table(FM_ALARM_CLEAR, ALARM_CLEAR, objtype_map); + add_to_table(FM_ALARM_HIERARCHICAL_CLEAR, ALARM_HIERARCHICAL_CLEAR, objtype_map); + add_to_table(FM_ALARM_MESSAGE, ALARM_MSG, objtype_map); + add_to_table(FM_WARM_START, WARM_START, objtype_map); + has_inited=true; + } + pthread_mutex_unlock(&mutex); } -static std::string add_time_val(std::string &str, - const std::string &objtype, FMTimeT time){ - std::string time_str; - fm_db_util_make_timestamp_string(time_str, time, true); - return str + objtype + STR_TYPE + time_str + SEP; +/** +* This method creates a json trap with the operation type attribute. + + { + "operation_type": "your_value", + "alarm" : { + } + } + +* Returns the json object representing the new trap. +*/ +struct json_object* init_json_trap(std::string op_type){ + struct json_object *json_trap = json_object_new_object(); + struct json_object *json_data_operation_type = + json_object_new_string(op_type.c_str()); + json_object_object_add(json_trap, JSON_TRAP_TAG_OP_TYPE, + json_data_operation_type ); + struct json_object *alarm_values = json_object_new_object(); + json_object_object_add(json_trap, JSON_TRAP_TAG_ALARM, alarm_values); + return json_trap; } -static std::string add_str_val(std::string &str, - const std::string &objtype, const char *value){ - std::string val(value); - return str + objtype + STR_TYPE + '"' + val + '"' + SEP; + +/** + +* This method adds new metadata given an json object that represents a trap. + +ie: Given the attributes object_type "v1", value "v2" +the result will be this: + + { + "operation_type": "alarm", + "alarm" : { ... + "v1": "v2" + } + } + +* The json object provided has the added metadata after the operation. + +*/ +void add_value_json_trap(struct json_object* json_trap, std::string obj_type, + std::string value){ + + struct json_object *json_alarm_values = NULL; + json_object_object_get_ex(json_trap, JSON_TRAP_TAG_ALARM, &json_alarm_values); + + struct json_object *json_value = json_object_new_string(value.c_str()); + json_object_object_add(json_alarm_values, obj_type.c_str(), json_value); + + return; } -static std::string add_int_val(std::string &str, - const std::string &objtype, int value){ - return str + objtype + INT_TYPE + fm_db_util_int_to_string(value) + SEP; + +/** +This method opens an socket and writes a message given a server name, +port number and the number of bytes of the message. + +Returns True if message is write succesfully else returns False. + +*/ +bool send_data(const char * server_name, int portno, const void * message, + int message_len){ + + char addr[INET6_ADDRSTRLEN]; + static bool m_connected = false; + static CFmSocket m_client; + struct addrinfo hints; + struct addrinfo *res = NULL; + memset(&hints,0,sizeof(hints)); + hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + hints.ai_socktype = SOCK_STREAM; /* Datagram socket */ + hints.ai_flags = 0; /* For wildcard IP address */ + hints.ai_protocol = 0; /* Any protocol */ + hints.ai_canonname = NULL; + hints.ai_addr = NULL; + hints.ai_next = NULL; + bool result = false; + + int rc = getaddrinfo(server_name, NULL, &hints, &res); + if (rc != 0) { + FM_ERROR_LOG("ERROR failed to get SNMP trap server address info :%d", errno); + } else { + if (res->ai_family == AF_INET || res->ai_family==AF_INET6) { + if(res->ai_family == AF_INET) { + inet_ntop(AF_INET, &(((sockaddr_in*)res->ai_addr)->sin_addr), + addr, sizeof(addr)); + } else if (res->ai_family == AF_INET6) { + inet_ntop(AF_INET6, &(((sockaddr_in6*)res->ai_addr)->sin6_addr), + addr, sizeof(addr)); + } + m_connected = m_client.connect(addr, portno, res->ai_family); + if (m_connected == true) { + result = m_client.write_packet(message, message_len); + if (result){ + FM_INFO_LOG("SNMP sent data successfully"); + } + } else { + FM_ERROR_LOG("ERROR failed to connect with SNMP trap server: %d", errno); + } + } + } + freeaddrinfo(res); + return result; } static std::string get_trap_objtype(int type){ - init_objtype_table(); - return objtype_map[type]; + init_objtype_table(); + return objtype_map[type]; } static void add_to_list(std::vector &trap_strings) { - std::string delimiter = " "; + std::string delimiter = " "; - std::vector::iterator it = trap_strings.begin(); - std::vector::iterator end = trap_strings.end(); - getTrapDestList().clear(); - for (; it != end; ++it){ - size_t pos = 0; - fm_db_single_result_t entry; - pos = (*it).find(delimiter); - entry[FM_TRAPDEST_IP] = (*it).substr(0, pos); - entry[FM_TRAPDEST_COMM] = (*it).erase(0, pos + delimiter.length()); - getTrapDestList().push_back(entry); - } + std::vector::iterator it = trap_strings.begin(); + std::vector::iterator end = trap_strings.end(); + getTrapDestList().clear(); + for (; it != end; ++it){ + size_t pos = 0; + fm_db_single_result_t entry; + pos = (*it).find(delimiter); + entry[FM_TRAPDEST_IP] = (*it).substr(0, pos); + entry[FM_TRAPDEST_COMM] = (*it).erase(0, pos + delimiter.length()); + getTrapDestList().push_back(entry); + } } void set_trap_dest_list(std::string value){ - std::vector entries; - std::istringstream f(value); - std::string s; - while (getline(f, s, ',')) { - std::cout << s << std::endl; - FM_INFO_LOG("Add entry: (%s)", s.c_str()); - entries.push_back(s); - } - add_to_list(entries); - FM_INFO_LOG("Set trap entries: (%d)", getTrapDestList().size()); -} - -static std::string format_trap_cmd(int type, SFmAlarmDataT &data, - std::string &ip, std::string &comm){ - std::string cmd; - std::string objtype; - std::string mib; - std::string s = "\"\" "; - std::string env; - - if (get_trap_objtype(type) == WARM_START) - mib = SNMPv2_MIB; - else - mib = WRS_ALARM_MIB; - - objtype = mib + SCOPE + get_trap_objtype(type); - - cmd = TRAP_CMD + OPTION_COMM + comm + SEP + ip + SEP + s + objtype + SEP; - std::string operation_type =get_trap_objtype(type); - - if (operation_type == ALARM_CLEAR){ - cmd = add_str_val(cmd,ALARM_ID, data.alarm_id); - cmd = add_str_val(cmd, ALARM_INSTANCE_ID, data.entity_instance_id); - cmd = add_time_val(cmd, ALARM_DATE_TIME, data.timestamp); - cmd = add_str_val(cmd, ALARM_REASON_TEXT, data.reason_text); - } else if (operation_type == ALARM_HIERARCHICAL_CLEAR){ - cmd = add_str_val(cmd, ALARM_INSTANCE_ID, data.entity_instance_id); - cmd = add_time_val(cmd, ALARM_DATE_TIME, 0); - cmd = add_str_val(cmd, ALARM_REASON_TEXT, CLEAR_REASON_TEXT.c_str()); - } else if (operation_type == ALARM_MSG){ - cmd = add_str_val(cmd, EVENT_ID, data.alarm_id); - cmd = add_str_val(cmd, EVENT_INSTANCE_ID, data.entity_instance_id); - cmd = add_time_val(cmd, EVENT_DATE_TIME, data.timestamp); - cmd = add_int_val(cmd, EVENT_SEVERITY, data.severity); - cmd = add_str_val(cmd, EVENT_REASON_TEXT, data.reason_text); - cmd = add_int_val(cmd, EVENT_EVENT_TYPE, data.alarm_type); - cmd = add_int_val(cmd, EVENT_CAUSE, data.probable_cause); - cmd = add_int_val(cmd, EVENT_SERVICE_AFFECTING, data.service_affecting); - } else if (operation_type == WARM_START){ - // nothing to add to cmd - } else { - cmd = add_str_val(cmd, ALARM_ID, data.alarm_id); - cmd = add_str_val(cmd, ALARM_INSTANCE_ID, data.entity_instance_id); - cmd = add_time_val(cmd, ALARM_DATE_TIME, data.timestamp); - cmd = add_int_val(cmd, ALARM_SEVERITY, data.severity); - cmd = add_str_val(cmd, ALARM_REASON_TEXT, data.reason_text); - cmd = add_int_val(cmd, ALARM_EVENT_TYPE, data.alarm_type); - cmd = add_int_val(cmd, ALARM_CAUSE, data.probable_cause); - cmd = add_str_val(cmd, ALARM_REPAIR_ACTION, data.proposed_repair_action); - cmd = add_int_val(cmd, ALARM_SERVICE_AFFECTING, data.service_affecting); - cmd = add_int_val(cmd, ALARM_SUPPRESSION, data.suppression); - } - - return cmd; + std::vector entries; + std::istringstream f(value); + std::string s; + while (getline(f, s, ',')) { + std::cout << s << std::endl; + FM_INFO_LOG("Add entry: (%s)", s.c_str()); + entries.push_back(s); + } + add_to_list(entries); + FM_INFO_LOG("Set trap entries: (%d)", getTrapDestList().size()); } +/** + +*This method creates a JSON string representing a trap from the attributes +type and data. + +an example of a JSON string: + + { + "operation_type": "alarm_type_from_type_arg", + "alarm": { + ... + "obj_type1":"value1" + "obj_type2":"value2" + ... + } + } + +* Returns the JSON string created representing the trap. + +*/ +static std::string format_trap_json(int type, SFmAlarmDataT &data){ + + std::string operation_type = get_trap_objtype(type); + struct json_object *result = init_json_trap(operation_type); + std::string result_json; + std::string time_str; + + if(operation_type.empty() || result == NULL){ + return JSON_TRAP_EMPTY; + } + + if (operation_type == ALARM_CLEAR){ + add_value_json_trap(result, ALARM_ID, data.alarm_id); + add_value_json_trap(result, ALARM_INSTANCE_ID, + data.entity_instance_id); + fm_db_util_make_timestamp_string(time_str, data.timestamp, true); + add_value_json_trap(result, ALARM_DATE_TIME, time_str); + add_value_json_trap(result, ALARM_REASON_TEXT, data.reason_text); + } else if (operation_type == ALARM_HIERARCHICAL_CLEAR){ + add_value_json_trap(result, ALARM_INSTANCE_ID, + data.entity_instance_id); + fm_db_util_make_timestamp_string(time_str, 0, true); + add_value_json_trap(result, ALARM_DATE_TIME, time_str); + add_value_json_trap(result, ALARM_REASON_TEXT, CLEAR_REASON_TEXT); + } else if (operation_type == ALARM_MSG){ + add_value_json_trap(result, EVENT_ID, data.alarm_id); + add_value_json_trap(result, EVENT_INSTANCE_ID, + data.entity_instance_id); + fm_db_util_make_timestamp_string(time_str, data.timestamp, true); + add_value_json_trap(result, EVENT_DATE_TIME, time_str); + add_value_json_trap(result, EVENT_SEVERITY, + fm_db_util_int_to_string(data.severity)); + add_value_json_trap(result, EVENT_REASON_TEXT, data.reason_text); + add_value_json_trap(result, EVENT_EVENT_TYPE, + fm_db_util_int_to_string(data.alarm_type)); + add_value_json_trap(result, EVENT_CAUSE, + fm_db_util_int_to_string(data.probable_cause)); + add_value_json_trap(result, EVENT_SERVICE_AFFECTING, + fm_db_util_int_to_string(data.service_affecting)); + } else if (operation_type == WARM_START){ + // nothing to add to cmd + } else { + add_value_json_trap(result, ALARM_ID, data.alarm_id); + add_value_json_trap(result, ALARM_INSTANCE_ID, + data.entity_instance_id ); + fm_db_util_make_timestamp_string(time_str, data.timestamp, true); + add_value_json_trap(result, ALARM_DATE_TIME, time_str); + add_value_json_trap(result, ALARM_SEVERITY, + fm_db_util_int_to_string(data.severity)); + add_value_json_trap(result, ALARM_REASON_TEXT, data.reason_text); + add_value_json_trap(result, ALARM_EVENT_TYPE, + fm_db_util_int_to_string(data.alarm_type)); + add_value_json_trap(result, ALARM_CAUSE, + fm_db_util_int_to_string(data.probable_cause)); + add_value_json_trap(result, ALARM_REPAIR_ACTION, + data.proposed_repair_action); + add_value_json_trap(result, ALARM_SERVICE_AFFECTING, + fm_db_util_int_to_string(data.service_affecting)); + add_value_json_trap(result, ALARM_SUPPRESSION, + fm_db_util_int_to_string(data.suppression)); + } + result_json = std::string(json_object_to_json_string_ext(result, + JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY)); + int freed_json = json_object_put(result); + FM_DEBUG_LOG("JSON freed succesfully: %d", freed_json); + return result_json; +} + + +/** + +This method sends a JSON string representing the trap +to a trap server listening in a specific port. + +The server name and port are readed from fm.conf file. + +*/ bool fm_snmp_util_gen_trap(int type, SFmAlarmDataT &data) { - bool rc = true; - fm_buff_t cmdbuff; - fm_db_result_t res; - std::string cmd, eid; + bool send_json_success = false; + std::string eid = ""; + std::string trap_server_ip = ""; + std::string trap_server_port = ""; + std::string trap_server_snmp_enabled = ""; + std::string key_ip = FM_TRAP_SERVER_IP; + std::string key_port = FM_TRAP_SERVER_PORT; + std::string key_enabled = FM_TRAP_SNMP_ENABLED; + std::string json_trap = ""; - res = getTrapDestList(); + if (get_trap_objtype(type) != WARM_START) { + eid.assign(data.entity_instance_id); + std::string region_name = fm_db_util_get_region_name(); + std::string sys_name = fm_db_util_get_system_name(); + if (sys_name.length() != 0){ + eid = sys_name + "."+ eid; + } + if (region_name.length() != 0){ + eid = region_name + "."+ eid; + } + strncpy(data.entity_instance_id, eid.c_str(), + sizeof(data.entity_instance_id)-1); + } - if (get_trap_objtype(type) != WARM_START) { - eid.assign(data.entity_instance_id); - std::string region_name = fm_db_util_get_region_name(); - std::string sys_name = fm_db_util_get_system_name(); - if (sys_name.length() != 0){ - eid = sys_name + "."+ eid; - } - if (region_name.length() != 0){ - eid = region_name + "."+ eid; - } - strncpy(data.entity_instance_id, eid.c_str(), - sizeof(data.entity_instance_id)-1); - } + if (!fm_get_config_key(key_ip, trap_server_ip)) { + FM_ERROR_LOG("Fail to get config value for (%s)\n", key_ip.c_str()); + return false; + }; + if (!fm_get_config_key(key_port, trap_server_port)){ + FM_ERROR_LOG("Fail to get config value for (%s)\n", key_port.c_str()); + return false; + }; + if (!fm_get_config_key(key_enabled, trap_server_snmp_enabled)){ + FM_ERROR_LOG("Fail to get config value for (%s)\n", key_enabled.c_str()); + return false; + }; - fm_db_result_t::iterator it = res.begin(); - fm_db_result_t::iterator end = res.end(); + if (trap_server_snmp_enabled == "1"){ + json_trap = format_trap_json(type, data); - for (; it != end; ++it){ - memset(&(cmdbuff[0]), 0, cmdbuff.size()); - cmd.clear(); - std::string ip = (*it)[FM_TRAPDEST_IP]; - std::string comm = (*it)[FM_TRAPDEST_COMM]; - cmd = format_trap_cmd(type, data, ip, comm); + if(json_trap.empty()){ + FM_ERROR_LOG("ERROR creating SNMP trap with type: %d", type); + return false; + } - //FM_INFO_LOG("run cmd: %s\n", cmd.c_str()); - char *pline = &(cmdbuff[0]); - FILE *op = popen(cmd.c_str(),"r"); - if (op==NULL) { - FM_ERROR_LOG("popen() failed, errno: (%d) (%s)\n", - errno, strerror(errno)); - rc = false; - } - while (fgets(pline,cmdbuff.size(),op)!=NULL) { - FM_ERROR_LOG("Trap error message: (%s)\n", pline); - } - fclose(op); - } - return rc; + send_json_success = send_data(trap_server_ip.c_str(), + atoi(trap_server_port.c_str()), json_trap.c_str(), + json_trap.length()); + + if(send_json_success){ + FM_INFO_LOG("SNMP trap metadata sent succesfully %s %d", + json_trap.c_str(), json_trap.length()); + } else { + FM_ERROR_LOG("ERROR failed to send SNMP trap metadata %s %d", + json_trap.c_str(), json_trap.length()); + } + }else{ + FM_INFO_LOG("Fail to send SNMP trap metadata because snmp_trap_enabled" + " is not setted as 1, actual value: %s \n", + trap_server_snmp_enabled.c_str()); + return false; + } + return send_json_success; } static bool fm_snmp_get_db_connection(std::string &connection){ - const char *fn = "/etc/fm/fm.conf"; - std::string key = FM_SQL_CONNECTION; + const char *fn = "/etc/fm/fm.conf"; + std::string key = FM_SQL_CONNECTION; - fm_conf_set_file(fn); - return fm_get_config_key(key, connection); + fm_conf_set_file(fn); + return fm_get_config_key(key, connection); } extern "C" { bool fm_snmp_util_create_session(TFmAlarmSessionT *handle, const char* db_conn){ - std::string conn; - CFmDBSession *sess = new CFmDBSession; - if (sess==NULL) return false;; + std::string conn; + CFmDBSession *sess = new CFmDBSession; + if (sess==NULL) return false;; - if (db_conn == NULL){ - if (fm_snmp_get_db_connection(conn) != true){ - FM_ERROR_LOG("Fail to get db connection uri\n"); - delete sess; - return false; - } - db_conn = conn.c_str(); - } + if (db_conn == NULL){ + if (fm_snmp_get_db_connection(conn) != true){ + FM_ERROR_LOG("Fail to get db connection uri\n"); + delete sess; + return false; + } + db_conn = conn.c_str(); + } - if (sess->connect(db_conn) != true){ - FM_ERROR_LOG("Fail to connect to (%s)\n", db_conn); - delete sess; - return false; - } - *handle = sess; - return true; + if (sess->connect(db_conn) != true){ + FM_ERROR_LOG("Fail to connect to (%s)\n", db_conn); + delete sess; + return false; + } + *handle = sess; + return true; } void fm_snmp_util_destroy_session(TFmAlarmSessionT handle) { - CFmDBSession *sess = (CFmDBSession *)handle; + CFmDBSession *sess = (CFmDBSession *)handle; - if (sess != NULL){ - delete sess; - } + if (sess != NULL){ + delete sess; + } } -bool fm_snmp_util_get_all_alarms(TFmAlarmSessionT handle, - SFmAlarmQueryT *query) { +bool fm_snmp_util_get_all_alarms(TFmAlarmSessionT handle, SFmAlarmQueryT *query) { - assert(handle!=NULL); + assert(handle!=NULL); - CFmDbAlarmOperation op; - fm_db_result_t res; + CFmDbAlarmOperation op; + fm_db_result_t res; - CFmDBSession &sess = *((CFmDBSession*)handle); + CFmDBSession &sess = *((CFmDBSession*)handle); - if (!op.get_all_alarms(sess, &(query->alarm), &(query->num))) return false; + if (!op.get_all_alarms(sess, &(query->alarm), &(query->num))) return false; - return true; + return true; } -bool fm_snmp_util_get_all_event_logs(TFmAlarmSessionT handle, - SFmAlarmQueryT *query) { +bool fm_snmp_util_get_all_event_logs(TFmAlarmSessionT handle, SFmAlarmQueryT *query) { - assert(handle!=NULL); + assert(handle!=NULL); - CFmDbEventLogOperation op; - fm_db_result_t res; + CFmDbEventLogOperation op; + fm_db_result_t res; - CFmDBSession &sess = *((CFmDBSession*)handle); + CFmDBSession &sess = *((CFmDBSession*)handle); - if (!op.get_all_event_logs(sess, &(query->alarm), &(query->num))) return false; + if (!op.get_all_event_logs(sess, &(query->alarm), &(query->num))) return false; - return true; + return true; } } - diff --git a/test-requirements.txt b/test-requirements.txt index cbbb8bfc..2d6552d7 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,4 +4,4 @@ mock PyYAML >= 3.1.0 yamllint >= 0.5.2 #spec_cleaner>=1.0.9 -bandit!=1.6.0,>=1.1.0,<2.0.0 +bandit>=1.1.0,<=1.6.2 diff --git a/tox.ini b/tox.ini index 32bf0038..e9a504dc 100644 --- a/tox.ini +++ b/tox.ini @@ -73,6 +73,8 @@ commands = pylint {posargs} --rcfile=./pylint.rc \ basepython = python3 description = Run style checks. +deps = + hacking commands = flake8 @@ -93,9 +95,19 @@ commands = # E123, E125 skipped as they are invalid PEP-8. # E501 skipped because some of the code files include templates # that end up quite wide + +# E402,module level import not at top of file +# W504,line break after binary operator +# W605,invalid escape sequence '\w' +# E117,over-indented +# F633,use of >> is invalid with print function +# F841,local variable 'e' is assigned to but never used +# E741,ambiguous variable name 'l' +# E117,over-indented +# F632,use ==/!= to compare constant literals (str, bytes, int, float, tuple) show-source = True ignore = H102,H104,H105,H301,H306,H401,H403,H404,H405,H702,H903, - W504,W605,E123,E125,E501 + W504,W605,E123,E125,E501,E402,W504,W605,E117,F633,F841,E741,E117,F632 exclude = .venv,.git,.tox,dist,doc,*lib/python*,*egg,build,release-tag-* # TODO: H106 Don’t put vim configuration in source files (off by default). # H203 Use assertIs(Not)None to check for None (off by default).