summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/0041-Revamp-Redfish-Event-Log-Unique-ID-Generation.patch
blob: 8e61673e341142f49fa3b063137e750d9fab2b2f (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
From 1c557e1d8bee8f66d97037b0dc8ae392c6ec45d3 Mon Sep 17 00:00:00 2001
From: P Dheeraj Srujan Kumar <p.dheeraj.srujan.kumar@intel.com>
Date: Fri, 22 Jan 2021 17:00:21 +0530
Subject: [PATCH] Revamp Redfish Event Log Unique ID Generation

The unique ID for Redfish log events was generated using
the Timestamp of the log until seconds. This commit
allows the use of microseconds as well to create unique
Redfish Log ID, thereby improving the uniqueness of each
Log event ID

Tested:
- GET of /redfish/v1/Systems/system/LogServices/EventLog/Entries
  produces unique LogEvent Id's
- Verified Event ID's on Event listener.
- Redfish validator passed

Change-Id: Ie2046a8ee7f9e7f6f14b05071b18a291c4313370
Signed-off-by: P Dheeraj Srujan Kumar <p.dheeraj.srujan.kumar@intel.com>
---
 .../include/event_service_manager.hpp         | 40 +++++-------
 redfish-core/lib/log_services.hpp             | 63 ++++++-------------
 2 files changed, 35 insertions(+), 68 deletions(-)

diff --git a/redfish-core/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp
index afbf799..470636f 100644
--- a/redfish-core/include/event_service_manager.hpp
+++ b/redfish-core/include/event_service_manager.hpp
@@ -142,38 +142,32 @@ static const Message* formatMessage(const std::string_view& messageID)
 
 namespace event_log
 {
-bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
-                      const bool firstEntry = true)
+bool getUniqueEntryID(const std::string& logEntry, std::string& entryID)
 {
-    static time_t prevTs = 0;
-    static int index = 0;
-    if (firstEntry)
-    {
-        prevTs = 0;
-    }
-
     // Get the entry timestamp
-    std::time_t curTs = 0;
     std::tm timeStruct = {};
     std::istringstream entryStream(logEntry);
     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
     {
-        curTs = std::mktime(&timeStruct);
-        if (curTs == -1)
+        time_t seconds = std::mktime(&timeStruct);
+        if (seconds == -1)
         {
             return false;
         }
-    }
-    // If the timestamp isn't unique, increment the index
-    index = (curTs == prevTs) ? index + 1 : 0;
-
-    // Save the timestamp
-    prevTs = curTs;
 
-    entryID = std::to_string(curTs);
-    if (index > 0)
+        size_t dot = logEntry.find_first_of(".");
+        if (dot == std::string::npos)
+        {
+            return false;
+        }
+        // 2015-10-24T06:54:38.383093 => 6 digits for microseconds
+        std::string microSec = logEntry.substr((dot + 1), 6);
+        entryID = std::to_string(seconds) + "_";
+        entryID += microSec;
+    }
+    else
     {
-        entryID += "_" + std::to_string(index);
+        return false;
     }
     return true;
 }
@@ -1131,7 +1125,6 @@ class EventServiceManager
         std::vector<EventLogObjectsType> eventRecords;
 
         bool startLogCollection = false;
-        bool firstEntry = true;
 
         std::string logEntry;
         while (std::getline(logStream, logEntry))
@@ -1146,11 +1139,10 @@ class EventServiceManager
             }
 
             std::string idStr;
-            if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry))
+            if (!event_log::getUniqueEntryID(logEntry, idStr))
             {
                 continue;
             }
-            firstEntry = false;
 
             std::string timestamp;
             std::string messageID;
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index e6a9022..ee064ce 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -306,41 +306,32 @@ static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
     return true;
 }
 
-static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
-                             const bool firstEntry = true)
+static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID)
 {
-    static time_t prevTs = 0;
-    static int index = 0;
-    if (firstEntry)
-    {
-        prevTs = 0;
-    }
-
     // Get the entry timestamp
-    std::time_t curTs = 0;
     std::tm timeStruct = {};
     std::istringstream entryStream(logEntry);
     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
     {
-        curTs = std::mktime(&timeStruct);
-    }
-    // If the timestamp isn't unique, increment the index
-    if (curTs == prevTs)
-    {
-        index++;
+        time_t seconds = std::mktime(&timeStruct);
+        if (seconds == -1)
+        {
+            return false;
+        }
+
+        size_t dot = logEntry.find_first_of(".");
+        if (dot == std::string::npos)
+        {
+            return false;
+        }
+        // 2015-10-24T06:54:38.383093 => 6 digits for microseconds
+        std::string microSec = logEntry.substr((dot + 1), 6);
+        entryID = std::to_string(seconds) + "_";
+        entryID += microSec;
     }
     else
     {
-        // Otherwise, reset it
-        index = 0;
-    }
-    // Save the timestamp
-    prevTs = curTs;
-
-    entryID = std::to_string(curTs);
-    if (index > 0)
-    {
-        entryID += "_" + std::to_string(index);
+        return false;
     }
     return true;
 }
@@ -1265,9 +1256,6 @@ class JournalEventLogEntryCollection : public Node
         uint64_t entryCount = 0;
         std::string logEntry;
 
-        // Reset the unique ID on the first entry
-        bool firstEntry = true;
-
         // Oldest logs are in the last file, so start there and loop backwards
         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
              it++)
@@ -1289,16 +1277,11 @@ class JournalEventLogEntryCollection : public Node
                 }
 
                 std::string idStr;
-                if (!getUniqueEntryID(logEntry, idStr, firstEntry))
+                if (!getUniqueEntryID(logEntry, idStr))
                 {
                     continue;
                 }
 
-                if (firstEntry)
-                {
-                    firstEntry = false;
-                }
-
                 logEntryArray.push_back({});
                 nlohmann::json& bmcLogEntry = logEntryArray.back();
                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
@@ -1354,9 +1337,6 @@ class JournalEventLogEntry : public Node
         getRedfishLogFiles(redfishLogFiles);
         std::string logEntry;
 
-        // Reset the unique ID on the first entry
-        bool firstEntry = true;
-
         // Oldest logs are in the last file, so start there and loop backwards
         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
              it++)
@@ -1370,16 +1350,11 @@ class JournalEventLogEntry : public Node
             while (std::getline(logStream, logEntry))
             {
                 std::string idStr;
-                if (!getUniqueEntryID(logEntry, idStr, firstEntry))
+                if (!getUniqueEntryID(logEntry, idStr))
                 {
                     continue;
                 }
 
-                if (firstEntry)
-                {
-                    firstEntry = false;
-                }
-
                 if (idStr == targetID)
                 {
                     if (fillEventLogEntryJson(idStr, logEntry,
-- 
2.17.1