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:
parent
6091d6fd9e
commit
8e74a0ee1e
@ -1,8 +1,8 @@
|
|||||||
sphinx>=2.0.0,!=2.1.0 # BSD
|
sphinx>=1.6.2 # BSD
|
||||||
openstackdocstheme>=2.2.1 # Apache-2.0
|
openstackdocstheme>=1.29.2 # Apache-2.0
|
||||||
|
|
||||||
# Release Notes documentation
|
# Release Notes documentation
|
||||||
reno>=3.1.0 # Apache-2.0
|
reno>=2.11.2 # Apache-2.0
|
||||||
|
|
||||||
# Api Ref documentation
|
# Api Ref documentation
|
||||||
os-api-ref>=1.4.0 # Apache-2.0
|
os-api-ref>=1.4.0 # Apache-2.0
|
||||||
|
@ -19,6 +19,8 @@ BuildRequires: python-devel
|
|||||||
BuildRequires: python-setuptools
|
BuildRequires: python-setuptools
|
||||||
BuildRequires: python2-pip
|
BuildRequires: python2-pip
|
||||||
BuildRequires: python2-wheel
|
BuildRequires: python2-wheel
|
||||||
|
Buildrequires: json-c-devel
|
||||||
|
Requires: json-c
|
||||||
|
|
||||||
%package -n fm-common-dev
|
%package -n fm-common-dev
|
||||||
Summary: CGTS Platform Fault Management Common Package - Development files
|
Summary: CGTS Platform Fault Management Common Package - Development files
|
||||||
|
@ -4,7 +4,7 @@ SRCS = fmAPI.cpp fmFile.cpp fmLog.cpp fmMsgServer.cpp fmMutex.cpp fmSocket.cpp f
|
|||||||
CLI_SRCS = fm_cli.cpp
|
CLI_SRCS = fm_cli.cpp
|
||||||
OBJS = $(SRCS:.cpp=.o)
|
OBJS = $(SRCS:.cpp=.o)
|
||||||
CLI_OBJS = fm_cli.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)
|
INCLUDES = -I./ -I$(shell pg_config --includedir)
|
||||||
CCFLAGS = -g -O2 -Wall -Werror -fPIC
|
CCFLAGS = -g -O2 -Wall -Werror -fPIC
|
||||||
EXTRACCFLAGS= -Wformat -Wformat-security
|
EXTRACCFLAGS= -Wformat -Wformat-security
|
||||||
|
@ -94,6 +94,9 @@
|
|||||||
#define FM_STRING_TRUE "True"
|
#define FM_STRING_TRUE "True"
|
||||||
#define FM_CONF_PASSWORD "password"
|
#define FM_CONF_PASSWORD "password"
|
||||||
#define FM_CONF_CONNECTION "connection"
|
#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"
|
#define CLEAR_ALL_REASON_TEXT "System initiated hierarchical alarm clear"
|
||||||
|
|
||||||
|
@ -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
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
#include <map>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <json-c/json.h>
|
||||||
|
#include <map>
|
||||||
|
#include <netdb.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string>
|
||||||
|
#include <unistd.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#include "fmDbAPI.h"
|
|
||||||
#include "fmFile.h"
|
|
||||||
#include "fmAPI.h"
|
#include "fmAPI.h"
|
||||||
#include "fmMsg.h"
|
#include "fmConfig.h"
|
||||||
#include "fmLog.h"
|
#include "fmDbAPI.h"
|
||||||
#include "fmDb.h"
|
#include "fmDb.h"
|
||||||
#include "fmDbUtils.h"
|
#include "fmDbUtils.h"
|
||||||
|
#include "fmFile.h"
|
||||||
|
#include "fmLog.h"
|
||||||
|
#include "fmMsg.h"
|
||||||
#include "fmSnmpConstants.h"
|
#include "fmSnmpConstants.h"
|
||||||
#include "fmSnmpUtils.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;
|
typedef std::map<int,std::string> int_to_objtype;
|
||||||
|
|
||||||
@ -31,260 +39,405 @@ static pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
|||||||
|
|
||||||
|
|
||||||
fm_db_result_t &getTrapDestList(){
|
fm_db_result_t &getTrapDestList(){
|
||||||
static fm_db_result_t trap_dest_list;
|
static fm_db_result_t trap_dest_list;
|
||||||
return trap_dest_list;
|
return trap_dest_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_to_table(int t, std::string objtype, int_to_objtype &tbl) {
|
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() {
|
static void init_objtype_table() {
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
static bool has_inited=false;
|
static bool has_inited=false;
|
||||||
while (!has_inited){
|
while (!has_inited){
|
||||||
add_to_table(FM_ALARM_SEVERITY_CLEAR, ALARM_MSG, objtype_map);
|
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_WARNING, ALARM_WARNING, objtype_map);
|
||||||
add_to_table(FM_ALARM_SEVERITY_MINOR, ALARM_MINOR, 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_MAJOR, ALARM_MAJOR, objtype_map);
|
||||||
add_to_table(FM_ALARM_SEVERITY_CRITICAL, ALARM_CRITICAL, 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_CLEAR, ALARM_CLEAR, objtype_map);
|
||||||
add_to_table(FM_ALARM_HIERARCHICAL_CLEAR, ALARM_HIERARCHICAL_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_ALARM_MESSAGE, ALARM_MSG, objtype_map);
|
||||||
add_to_table(FM_WARM_START, WARM_START, objtype_map);
|
add_to_table(FM_WARM_START, WARM_START, objtype_map);
|
||||||
has_inited=true;
|
has_inited=true;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string add_time_val(std::string &str,
|
/**
|
||||||
const std::string &objtype, FMTimeT time){
|
* This method creates a json trap with the operation type attribute.
|
||||||
std::string time_str;
|
|
||||||
fm_db_util_make_timestamp_string(time_str, time, true);
|
{
|
||||||
return str + objtype + STR_TYPE + time_str + SEP;
|
"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){
|
static std::string get_trap_objtype(int type){
|
||||||
init_objtype_table();
|
init_objtype_table();
|
||||||
return objtype_map[type];
|
return objtype_map[type];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_to_list(std::vector<std::string> &trap_strings) {
|
static void add_to_list(std::vector<std::string> &trap_strings) {
|
||||||
std::string delimiter = " ";
|
std::string delimiter = " ";
|
||||||
|
|
||||||
std::vector<std::string>::iterator it = trap_strings.begin();
|
std::vector<std::string>::iterator it = trap_strings.begin();
|
||||||
std::vector<std::string>::iterator end = trap_strings.end();
|
std::vector<std::string>::iterator end = trap_strings.end();
|
||||||
getTrapDestList().clear();
|
getTrapDestList().clear();
|
||||||
for (; it != end; ++it){
|
for (; it != end; ++it){
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
fm_db_single_result_t entry;
|
fm_db_single_result_t entry;
|
||||||
pos = (*it).find(delimiter);
|
pos = (*it).find(delimiter);
|
||||||
entry[FM_TRAPDEST_IP] = (*it).substr(0, pos);
|
entry[FM_TRAPDEST_IP] = (*it).substr(0, pos);
|
||||||
entry[FM_TRAPDEST_COMM] = (*it).erase(0, pos + delimiter.length());
|
entry[FM_TRAPDEST_COMM] = (*it).erase(0, pos + delimiter.length());
|
||||||
getTrapDestList().push_back(entry);
|
getTrapDestList().push_back(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_trap_dest_list(std::string value){
|
void set_trap_dest_list(std::string value){
|
||||||
|
|
||||||
std::vector<std::string> entries;
|
std::vector<std::string> entries;
|
||||||
std::istringstream f(value);
|
std::istringstream f(value);
|
||||||
std::string s;
|
std::string s;
|
||||||
while (getline(f, s, ',')) {
|
while (getline(f, s, ',')) {
|
||||||
std::cout << s << std::endl;
|
std::cout << s << std::endl;
|
||||||
FM_INFO_LOG("Add entry: (%s)", s.c_str());
|
FM_INFO_LOG("Add entry: (%s)", s.c_str());
|
||||||
entries.push_back(s);
|
entries.push_back(s);
|
||||||
}
|
}
|
||||||
add_to_list(entries);
|
add_to_list(entries);
|
||||||
FM_INFO_LOG("Set trap entries: (%d)", getTrapDestList().size());
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
|
||||||
|
*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 fm_snmp_util_gen_trap(int type, SFmAlarmDataT &data) {
|
||||||
|
|
||||||
bool rc = true;
|
bool send_json_success = false;
|
||||||
fm_buff_t cmdbuff;
|
std::string eid = "";
|
||||||
fm_db_result_t res;
|
std::string trap_server_ip = "";
|
||||||
std::string cmd, eid;
|
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) {
|
if (!fm_get_config_key(key_ip, trap_server_ip)) {
|
||||||
eid.assign(data.entity_instance_id);
|
FM_ERROR_LOG("Fail to get config value for (%s)\n", key_ip.c_str());
|
||||||
std::string region_name = fm_db_util_get_region_name();
|
return false;
|
||||||
std::string sys_name = fm_db_util_get_system_name();
|
};
|
||||||
if (sys_name.length() != 0){
|
if (!fm_get_config_key(key_port, trap_server_port)){
|
||||||
eid = sys_name + "."+ eid;
|
FM_ERROR_LOG("Fail to get config value for (%s)\n", key_port.c_str());
|
||||||
}
|
return false;
|
||||||
if (region_name.length() != 0){
|
};
|
||||||
eid = region_name + "."+ eid;
|
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());
|
||||||
strncpy(data.entity_instance_id, eid.c_str(),
|
return false;
|
||||||
sizeof(data.entity_instance_id)-1);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
fm_db_result_t::iterator it = res.begin();
|
if (trap_server_snmp_enabled == "1"){
|
||||||
fm_db_result_t::iterator end = res.end();
|
json_trap = format_trap_json(type, data);
|
||||||
|
|
||||||
for (; it != end; ++it){
|
if(json_trap.empty()){
|
||||||
memset(&(cmdbuff[0]), 0, cmdbuff.size());
|
FM_ERROR_LOG("ERROR creating SNMP trap with type: %d", type);
|
||||||
cmd.clear();
|
return false;
|
||||||
std::string ip = (*it)[FM_TRAPDEST_IP];
|
}
|
||||||
std::string comm = (*it)[FM_TRAPDEST_COMM];
|
|
||||||
cmd = format_trap_cmd(type, data, ip, comm);
|
|
||||||
|
|
||||||
//FM_INFO_LOG("run cmd: %s\n", cmd.c_str());
|
send_json_success = send_data(trap_server_ip.c_str(),
|
||||||
char *pline = &(cmdbuff[0]);
|
atoi(trap_server_port.c_str()), json_trap.c_str(),
|
||||||
FILE *op = popen(cmd.c_str(),"r");
|
json_trap.length());
|
||||||
if (op==NULL) {
|
|
||||||
FM_ERROR_LOG("popen() failed, errno: (%d) (%s)\n",
|
if(send_json_success){
|
||||||
errno, strerror(errno));
|
FM_INFO_LOG("SNMP trap metadata sent succesfully %s %d",
|
||||||
rc = false;
|
json_trap.c_str(), json_trap.length());
|
||||||
}
|
} else {
|
||||||
while (fgets(pline,cmdbuff.size(),op)!=NULL) {
|
FM_ERROR_LOG("ERROR failed to send SNMP trap metadata %s %d",
|
||||||
FM_ERROR_LOG("Trap error message: (%s)\n", pline);
|
json_trap.c_str(), json_trap.length());
|
||||||
}
|
}
|
||||||
fclose(op);
|
}else{
|
||||||
}
|
FM_INFO_LOG("Fail to send SNMP trap metadata because snmp_trap_enabled"
|
||||||
return rc;
|
" 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){
|
static bool fm_snmp_get_db_connection(std::string &connection){
|
||||||
const char *fn = "/etc/fm/fm.conf";
|
const char *fn = "/etc/fm/fm.conf";
|
||||||
std::string key = FM_SQL_CONNECTION;
|
std::string key = FM_SQL_CONNECTION;
|
||||||
|
|
||||||
fm_conf_set_file(fn);
|
fm_conf_set_file(fn);
|
||||||
return fm_get_config_key(key, connection);
|
return fm_get_config_key(key, connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
bool fm_snmp_util_create_session(TFmAlarmSessionT *handle, const char* db_conn){
|
bool fm_snmp_util_create_session(TFmAlarmSessionT *handle, const char* db_conn){
|
||||||
|
|
||||||
std::string conn;
|
std::string conn;
|
||||||
CFmDBSession *sess = new CFmDBSession;
|
CFmDBSession *sess = new CFmDBSession;
|
||||||
if (sess==NULL) return false;;
|
if (sess==NULL) return false;;
|
||||||
|
|
||||||
if (db_conn == NULL){
|
if (db_conn == NULL){
|
||||||
if (fm_snmp_get_db_connection(conn) != true){
|
if (fm_snmp_get_db_connection(conn) != true){
|
||||||
FM_ERROR_LOG("Fail to get db connection uri\n");
|
FM_ERROR_LOG("Fail to get db connection uri\n");
|
||||||
delete sess;
|
delete sess;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
db_conn = conn.c_str();
|
db_conn = conn.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sess->connect(db_conn) != true){
|
if (sess->connect(db_conn) != true){
|
||||||
FM_ERROR_LOG("Fail to connect to (%s)\n", db_conn);
|
FM_ERROR_LOG("Fail to connect to (%s)\n", db_conn);
|
||||||
delete sess;
|
delete sess;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
*handle = sess;
|
*handle = sess;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fm_snmp_util_destroy_session(TFmAlarmSessionT handle) {
|
void fm_snmp_util_destroy_session(TFmAlarmSessionT handle) {
|
||||||
CFmDBSession *sess = (CFmDBSession *)handle;
|
CFmDBSession *sess = (CFmDBSession *)handle;
|
||||||
|
|
||||||
if (sess != NULL){
|
if (sess != NULL){
|
||||||
delete sess;
|
delete sess;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fm_snmp_util_get_all_alarms(TFmAlarmSessionT handle,
|
bool fm_snmp_util_get_all_alarms(TFmAlarmSessionT handle, SFmAlarmQueryT *query) {
|
||||||
SFmAlarmQueryT *query) {
|
|
||||||
|
|
||||||
assert(handle!=NULL);
|
assert(handle!=NULL);
|
||||||
|
|
||||||
CFmDbAlarmOperation op;
|
CFmDbAlarmOperation op;
|
||||||
fm_db_result_t res;
|
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,
|
bool fm_snmp_util_get_all_event_logs(TFmAlarmSessionT handle, SFmAlarmQueryT *query) {
|
||||||
SFmAlarmQueryT *query) {
|
|
||||||
|
|
||||||
assert(handle!=NULL);
|
assert(handle!=NULL);
|
||||||
|
|
||||||
CFmDbEventLogOperation op;
|
CFmDbEventLogOperation op;
|
||||||
fm_db_result_t res;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,4 +4,4 @@ mock
|
|||||||
PyYAML >= 3.1.0
|
PyYAML >= 3.1.0
|
||||||
yamllint >= 0.5.2
|
yamllint >= 0.5.2
|
||||||
#spec_cleaner>=1.0.9
|
#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
14
tox.ini
@ -73,6 +73,8 @@ commands = pylint {posargs} --rcfile=./pylint.rc \
|
|||||||
basepython = python3
|
basepython = python3
|
||||||
description =
|
description =
|
||||||
Run style checks.
|
Run style checks.
|
||||||
|
deps =
|
||||||
|
hacking
|
||||||
commands =
|
commands =
|
||||||
flake8
|
flake8
|
||||||
|
|
||||||
@ -93,9 +95,19 @@ commands =
|
|||||||
# E123, E125 skipped as they are invalid PEP-8.
|
# E123, E125 skipped as they are invalid PEP-8.
|
||||||
# E501 skipped because some of the code files include templates
|
# E501 skipped because some of the code files include templates
|
||||||
# that end up quite wide
|
# 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
|
show-source = True
|
||||||
ignore = H102,H104,H105,H301,H306,H401,H403,H404,H405,H702,H903,
|
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-*
|
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).
|
# 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).
|
# H203 Use assertIs(Not)None to check for None (off by default).
|
||||||
|
Loading…
Reference in New Issue
Block a user