Skip to content

Commit

Permalink
Add changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jul 22, 2022
1 parent f114b91 commit b8300f3
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 87 deletions.
15 changes: 2 additions & 13 deletions src/sonic-eventd/rsyslog_plugin/rsyslog_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ bool RsyslogPlugin::onMessage(string msg, lua_State* luaState) {
SWSS_LOG_DEBUG("%s was not able to be parsed into a structured event\n", msg.c_str());
return false;
} else {
string timestamp = paramDict["timestamp"];
string formattedTimestamp = m_timestampFormatter->changeTimestampFormat(paramDict["timestamp"]);
if(timestamp.empty()) {
SWSS_LOG_ERROR("Timestamp Formatter was unable to format %s.\n", timestamp.c_str());
}
paramDict["timestamp"] = formattedTimestamp;
int returnCode = event_publish(m_eventHandle, tag, &paramDict);
if(returnCode != 0) {
SWSS_LOG_ERROR("rsyslog_plugin was not able to publish event for %s.\n", tag.c_str());
Expand All @@ -45,17 +39,14 @@ bool RsyslogPlugin::createRegexList() {
return false;
}

string regexString;
regex expression;

for(long unsigned int i = 0; i < m_parser->m_regexList.size(); i++) {
string regexString = "([a-zA-Z]{3} [0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6}) ";
try {
string givenRegex = m_parser->m_regexList[i]["regex"];
regexString += givenRegex;
regexString = m_parser->m_regexList[i]["regex"];
string tag = m_parser->m_regexList[i]["tag"];
vector<string> params = m_parser->m_regexList[i]["params"];
params.insert(params.begin(), "timestamp"); // each event will have timestamp so inserting it
m_parser->m_regexList[i]["params"] = params;
regex expr(regexString);
expression = expr;
} catch (domain_error& deException) {
Expand Down Expand Up @@ -103,9 +94,7 @@ int RsyslogPlugin::onInit() {
}

RsyslogPlugin::RsyslogPlugin(string moduleName, string regexPath) {
const string timestampFormatRegex = "([a-zA-Z]{3}) ([0-9]{1,2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6})";
m_parser = unique_ptr<SyslogParser>(new SyslogParser());
m_timestampFormatter = unique_ptr<TimestampFormatter>(new TimestampFormatter(timestampFormatRegex));
m_moduleName = moduleName;
m_regexPath = regexPath;
}
2 changes: 0 additions & 2 deletions src/sonic-eventd/rsyslog_plugin/rsyslog_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ extern "C"
#include <string>
#include <memory>
#include "syslog_parser.h"
#include "timestamp_formatter.h"
#include "events.h"
#include "logger.h"

Expand All @@ -31,7 +30,6 @@ class RsyslogPlugin {
RsyslogPlugin(string moduleName, string regexPath);
private:
unique_ptr<SyslogParser> m_parser;
unique_ptr<TimestampFormatter> m_timestampFormatter;
event_handle_t m_eventHandle;
string m_regexPath;
string m_moduleName;
Expand Down
63 changes: 40 additions & 23 deletions src/sonic-eventd/rsyslog_plugin/syslog_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <ctime>
#include "syslog_parser.h"
#include "logger.h"

Expand All @@ -10,6 +11,15 @@
*
*/

void SyslogParser::addTimestamp(string message, event_params_t& paramMap) {
string formattedTimestamp = m_timestampFormatter->changeTimestampFormat(message);
if(formattedTimestamp.empty()) {
SWSS_LOG_ERROR("Message does not contain valid timestamp and cannot be formatted: %s.\n", message.c_str());
return;
}
paramMap["timestamp"] = formattedTimestamp;
}

bool SyslogParser::parseMessage(string message, string& eventTag, event_params_t& paramMap, lua_State* luaState) {
for(long unsigned int i = 0; i < m_regexList.size(); i++) {
smatch matchResults;
Expand All @@ -23,32 +33,39 @@ bool SyslogParser::parseMessage(string message, string& eventTag, event_params_t
for(long unsigned int j = 0; j < params.size(); j++) {
auto delimPos = params[j].find(':');
string resultValue = matchResults[j + 1].str();
if(delimPos != string::npos) { // have to execute lua script
string param = params[j].substr(0, delimPos);
string luaString = params[j].substr(delimPos + 1);
if(luaString.empty()) { // empty lua code
SWSS_LOG_INFO("Lua code missing after :, skipping operation");
paramMap[param] = resultValue;
continue;
}
const char* luaCode = luaString.c_str();
lua_pushstring(luaState, resultValue.c_str());
lua_setglobal(luaState, "arg");
if(luaL_dostring(luaState, luaCode) == 0) {
lua_pop(luaState, lua_gettop(luaState));
} else {
SWSS_LOG_ERROR("Invalid lua code, unable to do operation.\n");
paramMap[param] = resultValue;
continue;
}
lua_getglobal(luaState, "ret");
paramMap[param] = lua_tostring(luaState, -1);
lua_pop(luaState, 1);
} else {
paramMap[params[j]] = resultValue;
if(delimPos == string::npos) { // no lua code
paramMap[params[j]] = resultValue;
continue;
}
// have to execute lua script
string param = params[j].substr(0, delimPos);
string luaString = params[j].substr(delimPos + 1);
if(luaString.empty()) { // empty lua code
SWSS_LOG_INFO("Lua code missing after :, skipping operation");
paramMap[param] = resultValue;
continue;
}
const char* luaCode = luaString.c_str();
lua_pushstring(luaState, resultValue.c_str());
lua_setglobal(luaState, "arg");
if(luaL_dostring(luaState, luaCode) == 0) {
lua_pop(luaState, lua_gettop(luaState));
} else {
SWSS_LOG_ERROR("Invalid lua code, unable to do operation.\n");
paramMap[param] = resultValue;
continue;
}
lua_getglobal(luaState, "ret");
paramMap[param] = lua_tostring(luaState, -1);
lua_pop(luaState, 1);
}
addTimestamp(message, paramMap);
return true;
}
return false;
}

SyslogParser::SyslogParser() {
string timestampFormatRegex = "([a-zA-Z]{3}) ([0-9]{1,2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6}).*";
m_timestampFormatter = unique_ptr<TimestampFormatter>(new TimestampFormatter(timestampFormatRegex));
}
4 changes: 4 additions & 0 deletions src/sonic-eventd/rsyslog_plugin/syslog_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C"
#include <regex>
#include "json.hpp"
#include "events.h"
#include "timestamp_formatter.h"

using namespace std;
using json = nlohmann::json;
Expand All @@ -25,9 +26,12 @@ using json = nlohmann::json;

class SyslogParser {
public:
unique_ptr<TimestampFormatter> m_timestampFormatter;
vector<regex> m_expressions;
json m_regexList = json::array();
void addTimestamp(string message, event_params_t& paramDict);
bool parseMessage(string message, string& tag, event_params_t& paramDict, lua_State* luaState);
SyslogParser();
};

#endif
14 changes: 8 additions & 6 deletions src/sonic-eventd/rsyslog_plugin/timestamp_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ string TimestampFormatter::getYear(string timestamp) {
return year;
}

string TimestampFormatter::changeTimestampFormat(string timestamp) {
string TimestampFormatter::changeTimestampFormat(string message) {
smatch dateComponents;
string formattedTimestamp; // need to change format of Mmm dd hh:mm:ss.SSSSSS to YYYY-mm-ddThh:mm:ss.SSSSSSZ
if(!regex_search(timestamp, dateComponents, m_expression) || dateComponents.size() != 4) {
SWSS_LOG_ERROR("Timestamp unable to be broken down into components.\n");
if(!regex_search(message, dateComponents, m_expression) || dateComponents.size() != 4) { //whole,month,day,time
printf("Timestamp unable to be broken down into components.\n");
return ""; // empty string is error
}

string month;
auto it = g_monthDict.find(dateComponents[1].str());
if(it != g_monthDict.end()) {
Expand All @@ -63,16 +62,19 @@ string TimestampFormatter::changeTimestampFormat(string timestamp) {
SWSS_LOG_ERROR("Timestamp month was given in wrong format.\n");
return "";
}

string day = dateComponents[2].str();
if(day.size() == 1) { // convert 1 -> 01
day.insert(day.begin(), '0');
}

string time = dateComponents[3].str();
string currentTimestamp = month + day + time;
string year = getYear(currentTimestamp);

formattedTimestamp = year + "-" + month + "-" + day + "T" + time + "Z";
return formattedTimestamp;
}

TimestampFormatter::TimestampFormatter(string timestampFormatRegex) {
regex expr(timestampFormatRegex);
m_expression = expr;
}
9 changes: 3 additions & 6 deletions src/sonic-eventd/rsyslog_plugin/timestamp_formatter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef TIMESTAMP_FORMATTER_H
#define TIMESTAMP_FORMATTER_H

#include <iostream>
#include <string>
#include <regex>
#include <ctime>
Expand All @@ -13,11 +13,8 @@

class TimestampFormatter {
public:
std::string changeTimestampFormat(std::string timestamp);
TimestampFormatter(std::string regexFormatString) {
std::regex expr(regexFormatString);
m_expression = expr;
}
std::string changeTimestampFormat(std::string message);
TimestampFormatter(std::string timestampFormatRegex);
std::string m_storedTimestamp;
std::string m_storedYear;
private:
Expand Down
Loading

0 comments on commit b8300f3

Please sign in to comment.