summaryrefslogtreecommitdiff
path: root/meta-ibs/meta-common/recipes-phosphor/interfaces/bmcweb/0003-IBS-Add-redfish2phosphor-logs.patch
blob: 015aad4c10e78a53422fb077116b4c797402e133 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
From 3fc3a75cb49d59019ac0350388dde06d333b9156 Mon Sep 17 00:00:00 2001
From: "Evgeny Alekseev (IBS Group)" <ealekseev@ibs.sila.ru>
Date: Tue, 6 Sep 2022 14:45:36 +0300
Subject: [PATCH] IBS: Add redfish2phosphor-logs

---
 meson.build                   |  12 ++
 src/redfish2phosphor-logs.cpp | 210 ++++++++++++++++++++++++++++++++++
 2 files changed, 222 insertions(+)
 create mode 100644 src/redfish2phosphor-logs.cpp

diff --git a/meson.build b/meson.build
index f3d1c53d..510a8287 100644
--- a/meson.build
+++ b/meson.build
@@ -252,6 +252,7 @@ language : 'cpp')
 # Find the dependency modules, if not found use meson wrap to get them
 # automatically during the configure step
 bmcweb_dependencies = []
+redfish2phosphor_dependencies = []
 
 pam = cxx.find_library('pam', required: true)
 atomic =  cxx.find_library('atomic', required: true)
@@ -265,6 +266,7 @@ if not sdbusplus.found()
   sdbusplus = sdbusplus.as_system('system')
 endif
 bmcweb_dependencies += sdbusplus
+redfish2phosphor_dependencies += sdbusplus
 
 tinyxml = dependency('tinyxml2',
     default_options: ['tests=false'],
@@ -339,6 +341,7 @@ configure_file(input: 'bmcweb_config.h.in',
 systemd_system_unit_dir = systemd.get_variable(pkgconfig: 'systemdsystemunitdir')
 
 bindir = get_option('prefix') + '/' +get_option('bindir')
+sbindir = get_option('prefix') + '/' +get_option('sbindir')
 
 summary({
           'prefix' : get_option('prefix'),
@@ -390,6 +393,15 @@ executable(
   install_dir:bindir
 )
 
+executable(
+    'redfish2phosphor-logs',
+    'src/redfish2phosphor-logs.cpp',
+    dependencies: redfish2phosphor_dependencies,
+    include_directories : incdir,
+    install: true,
+    install_dir:sbindir
+)
+
 srcfiles_unittest = [
   'http/ut/utility_test.cpp',
   'http/ut/router_test.cpp',
diff --git a/src/redfish2phosphor-logs.cpp b/src/redfish2phosphor-logs.cpp
new file mode 100644
index 00000000..1bd51cac
--- /dev/null
+++ b/src/redfish2phosphor-logs.cpp
@@ -0,0 +1,210 @@
+
+#define BMCWEB_ENABLE_LOGGING 1
+#include <syslog.h>
+#include <systemd/sd-journal.h>
+
+#include <logging.hpp>
+#include <registries/openbmc_message_registry.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/bus/match.hpp>
+#include <sdbusplus/message.hpp>
+
+#include <array>
+#include <iostream>
+#include <map>
+#include <ranges>
+#include <regex>
+#include <span>
+#include <string>
+#include <string_view>
+#include <tuple>
+
+using namespace std::literals;
+
+static constexpr char ARG_DELIMITER = '$';
+static constexpr auto ARG_COUNT = 4;
+enum class level
+{
+    EMERG = LOG_EMERG,
+    ALERT = LOG_ALERT,
+    CRIT = LOG_CRIT,
+    ERR = LOG_ERR,
+    WARNING = LOG_WARNING,
+    NOTICE = LOG_NOTICE,
+    INFO = LOG_INFO,
+    DEBUG = LOG_DEBUG,
+};
+static level string2severity(std::string_view str)
+{
+
+    std::map<std::string_view, level> sev_map = {{"Critical", level::CRIT},
+                                                 {"Warning", level::WARNING},
+                                                 {"OK", level::NOTICE}};
+    if (sev_map.count(str))
+        return sev_map.at(str);
+    return level::NOTICE;
+}
+
+static std::string get_format_str(std::string_view str)
+{
+    std::stringstream ret_str;
+
+    std::regex format_re("%\\d");
+    std::regex_replace(std::ostreambuf_iterator<char>(ret_str), str.begin(),
+                       str.end(), format_re, "%s");
+    return ret_str.str();
+}
+
+std::vector<std::string_view> Split(const std::string_view str,
+                                    const char delim = ',')
+{
+    std::vector<std::string_view> result;
+
+    int indexCommaToLeftOfColumn = 0;
+    int indexCommaToRightOfColumn = -1;
+
+    for (int i = 0; i < static_cast<int>(str.size()); i++)
+    {
+        if (str[static_cast<std::basic_string_view<char>::size_type>(i)] ==
+            delim)
+        {
+            indexCommaToLeftOfColumn = indexCommaToRightOfColumn;
+            indexCommaToRightOfColumn = i;
+            int index = indexCommaToLeftOfColumn + 1;
+            int length = indexCommaToRightOfColumn - index;
+
+            // Bounds checking can be omitted as logically, this code can never
+            // be invoked Try it: put a breakpoint here and run the unit tests.
+            /*if (index + length >= static_cast<int>(str.size()))
+            {
+                length--;
+            }
+            if (length < 0)
+            {
+                length = 0;
+            }*/
+
+            std::string_view column(
+                str.data() +
+                    static_cast<std::basic_string_view<char>::size_type>(index),
+                static_cast<std::basic_string_view<char>::size_type>(length));
+            result.push_back(column);
+        }
+    }
+    const std::string_view finalColumn(
+        str.data() +
+            static_cast<std::basic_string_view<char>::size_type>(
+                indexCommaToRightOfColumn) +
+            1,
+        str.size() -
+            static_cast<std::basic_string_view<char>::size_type>(
+                indexCommaToRightOfColumn) -
+            1);
+    result.push_back(finalColumn);
+    return result;
+}
+
+constexpr auto severity_array = std::to_array(
+    {std::string_view{"xyz.openbmc_project.Logging.Entry.Level.Emergency"},
+     std::string_view{"xyz.openbmc_project.Logging.Entry.Level.Alert"},
+     std::string_view{"xyz.openbmc_project.Logging.Entry.Level.Critical"},
+     std::string_view{"xyz.openbmc_project.Logging.Entry.Level.Error"},
+     std::string_view{"xyz.openbmc_project.Logging.Entry.Level.Warning"},
+     std::string_view{"xyz.openbmc_project.Logging.Entry.Level.Notice"},
+     std::string_view{"xyz.openbmc_project.Logging.Entry.Level.Informational"},
+     std::string_view{"xyz.openbmc_project.Logging.Entry.Level.Debug"}});
+
+static auto& getBus()
+{
+    static auto bus = sdbusplus::bus::new_default();
+    return bus;
+}
+
+template <typename... Args>
+static void dbusCallMethodNoReturn(sdbusplus::bus_t& bus,
+                                   const std::string& busName,
+                                   const std::string& path,
+                                   const std::string& interface,
+                                   const std::string& method, Args&&... args)
+{
+    auto reqMsg = bus.new_method_call(busName.c_str(), path.c_str(),
+                                      interface.c_str(), method.c_str());
+    reqMsg.append(std::forward<Args>(args)...);
+    bus.call_noreply(reqMsg);
+}
+
+int main(int argc, char* argv[])
+{
+    crow::Logger::setLogLevel(crow::LogLevel::Warning);
+    if (argc != 2)
+    {
+        BMCWEB_LOG_CRITICAL << "Wrong number of args";
+        return -1;
+    }
+
+    BMCWEB_LOG_DEBUG << "Args:" << argv[1];
+    std::string_view log_argument(argv[1]);
+    BMCWEB_LOG_DEBUG << "Args string_view:" << log_argument;
+    std::vector<std::string_view> arg_vect = Split(log_argument, ARG_DELIMITER);
+    BMCWEB_LOG_DEBUG << "$ args count: " << arg_vect.size();
+    for (auto arg : arg_vect)
+        BMCWEB_LOG_DEBUG << arg;
+    string2severity(arg_vect[2]);
+
+    std::string_view type_header = "OpenBMC.0.1."sv;
+    if (arg_vect[2].find(type_header) == arg_vect[2].npos)
+    {
+        BMCWEB_LOG_CRITICAL << "Illegal Message Type" << arg_vect[2];
+        return -1;
+    }
+    std::string_view type =
+        std::string_view(arg_vect[2].begin() + arg_vect[2].find(type_header) +
+                             type_header.size(),
+                         arg_vect[2].end());
+    BMCWEB_LOG_DEBUG << "Message type: " << type;
+    const redfish::registries::MessageEntry* message = nullptr;
+    for (const auto& entry : redfish::registries::openbmc::registry)
+    {
+        if (std::string_view(std::get<0>(entry)).find(type) !=
+            std::string_view(std::get<0>(entry)).npos)
+        {
+            BMCWEB_LOG_DEBUG << "Found type";
+            message = &entry;
+            break;
+        }
+
+        get_format_str(std::get<1>(entry).message);
+    }
+    if (!message)
+    {
+        BMCWEB_LOG_CRITICAL << "Couldn't find message type: " << type;
+        return -1;
+    }
+
+    std::string string_msg = std::get<1>(*message).message;
+    auto severity_str = severity_array[static_cast<unsigned int>(
+        string2severity(std::get<1>(*message).messageSeverity))];
+    BMCWEB_LOG_DEBUG << "Severity: " << severity_str;
+
+    auto strsplit_args = Split(arg_vect[3], ',');
+
+    for (auto& m : strsplit_args)
+        BMCWEB_LOG_DEBUG << m;
+    string_msg = redfish::registries::fillMessageArgs(strsplit_args, string_msg);
+    BMCWEB_LOG_DEBUG << string_msg;
+    try
+    {
+        dbusCallMethodNoReturn(
+            getBus(), "xyz.openbmc_project.Logging"s,
+            "/xyz/openbmc_project/logging"s,
+            "xyz.openbmc_project.Logging.Create"s, "Create"s,
+            std::string(string_msg), std::string(severity_str),
+            std::array<std::pair<std::string, std::string>, 0>{});
+    }
+    catch (const sdbusplus::exception_t& ex)
+    {
+        BMCWEB_LOG_CRITICAL << ex.what();
+        return -1;
+    }
+    return 0;
+}
-- 
2.35.1