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 <pablo.bovina@windriver.com>
This commit is contained in:
Pablo Bovina 2020-10-23 10:21:44 -04:00 committed by pablo bovina
parent 6091d6fd9e
commit 8e74a0ee1e
7 changed files with 378 additions and 208 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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 <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <assert.h>
#include <iostream>
#include <json-c/json.h>
#include <map>
#include <netdb.h>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <vector>
#include <arpa/inet.h>
#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,std::string> int_to_objtype;
@ -57,22 +65,109 @@ static void init_objtype_table() {
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){
@ -110,69 +205,115 @@ void set_trap_dest_list(std::string value){
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);
*This method creates a JSON string representing a trap from the attributes
type and data.
cmd = TRAP_CMD + OPTION_COMM + comm + SEP + ip + SEP + s + objtype + SEP;
std::string operation_type =get_trap_objtype(type);
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){
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);
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){
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());
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){
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);
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 {
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);
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));
}
return cmd;
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;
res = getTrapDestList();
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 = "";
if (get_trap_objtype(type) != WARM_START) {
eid.assign(data.entity_instance_id);
@ -188,30 +329,45 @@ bool fm_snmp_util_gen_trap(int type, SFmAlarmDataT &data) {
sizeof(data.entity_instance_id)-1);
}
fm_db_result_t::iterator it = res.begin();
fm_db_result_t::iterator end = res.end();
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;
};
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 (trap_server_snmp_enabled == "1"){
json_trap = format_trap_json(type, data);
//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;
if(json_trap.empty()){
FM_ERROR_LOG("ERROR creating SNMP trap with type: %d", type);
return false;
}
while (fgets(pline,cmdbuff.size(),op)!=NULL) {
FM_ERROR_LOG("Trap error message: (%s)\n", pline);
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());
}
fclose(op);
}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 rc;
return send_json_success;
}
static bool fm_snmp_get_db_connection(std::string &connection){
@ -256,8 +412,7 @@ void fm_snmp_util_destroy_session(TFmAlarmSessionT handle) {
}
}
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);
@ -271,8 +426,7 @@ bool fm_snmp_util_get_all_alarms(TFmAlarmSessionT handle,
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);
@ -287,4 +441,3 @@ bool fm_snmp_util_get_all_event_logs(TFmAlarmSessionT handle,
}
}

View File

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

14
tox.ini
View File

@ -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 Dont put vim configuration in source files (off by default).
# H203 Use assertIs(Not)None to check for None (off by default).