Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 1 | #include <fstream> |
| 2 | #include <iostream> |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 3 | #include <chrono> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 4 | #include <cstdio> |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 5 | #include <set> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 8 | #include <sdbusplus/vtable.hpp> |
| 9 | #include <systemd/sd-bus.h> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 10 | #include <systemd/sd-journal.h> |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 11 | #include "config.h" |
| 12 | #include "elog_entry.hpp" |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 13 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 14 | #include "log_manager.hpp" |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 15 | #include "elog_meta.hpp" |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 16 | #include "elog_serialize.hpp" |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 17 | |
| 18 | using namespace phosphor::logging; |
| 19 | extern const std::map<metadata::Metadata, |
| 20 | std::function<metadata::associations::Type>> meta; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 21 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 22 | namespace phosphor |
| 23 | { |
| 24 | namespace logging |
| 25 | { |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 26 | namespace internal |
| 27 | { |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 28 | void Manager::commit(uint64_t transactionId, std::string errMsg) |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 29 | { |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 30 | auto reqLevel = level::ERR; // Default to ERR |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 31 | auto levelmap = g_errLevelMap.find(errMsg); |
| 32 | |
| 33 | if (levelmap != g_errLevelMap.end()) |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 34 | { |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 35 | reqLevel = levelmap->second; |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 38 | if (static_cast<Entry::Level>(reqLevel) < Entry::sevLowerLimit) |
| 39 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 40 | if (realErrors.size() >= ERROR_CAP) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 41 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 42 | erase(realErrors.front()); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | if (infoErrors.size() >= ERROR_INFO_CAP) |
| 48 | { |
| 49 | erase(infoErrors.front()); |
| 50 | } |
| 51 | } |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 52 | constexpr const auto transactionIdVar = "TRANSACTION_ID"; |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 53 | // Length of 'TRANSACTION_ID' string. |
Adriana Kobylak | 6721899 | 2017-02-28 12:53:37 -0600 | [diff] [blame] | 54 | constexpr const auto transactionIdVarSize = strlen(transactionIdVar); |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 55 | // Length of 'TRANSACTION_ID=' string. |
| 56 | constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 57 | |
Adriana Kobylak | cfd9a7d | 2017-06-07 11:57:31 -0500 | [diff] [blame] | 58 | // Flush all the pending log messages into the journal via Synchronize |
| 59 | constexpr auto JOURNAL_BUSNAME = "org.freedesktop.journal1"; |
| 60 | constexpr auto JOURNAL_PATH = "/org/freedesktop/journal1"; |
| 61 | constexpr auto JOURNAL_INTERFACE = "org.freedesktop.journal1"; |
| 62 | auto bus = sdbusplus::bus::new_default(); |
| 63 | auto method = bus.new_method_call(JOURNAL_BUSNAME, JOURNAL_PATH, |
| 64 | JOURNAL_INTERFACE, "Synchronize"); |
| 65 | bus.call_noreply(method); |
| 66 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 67 | sd_journal *j = nullptr; |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 68 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 69 | if (rc < 0) |
| 70 | { |
| 71 | logging::log<logging::level::ERR>("Failed to open journal", |
| 72 | logging::entry("DESCRIPTION=%s", strerror(-rc))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 73 | return; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 76 | std::string transactionIdStr = std::to_string(transactionId); |
Adriana Kobylak | d722b3a | 2017-02-28 12:10:44 -0600 | [diff] [blame] | 77 | std::set<std::string> metalist; |
| 78 | auto metamap = g_errMetaMap.find(errMsg); |
| 79 | if (metamap != g_errMetaMap.end()) |
| 80 | { |
| 81 | metalist.insert(metamap->second.begin(), metamap->second.end()); |
| 82 | } |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 83 | |
Jayanth Othayoth | db18ebe | 2017-09-04 00:48:02 -0500 | [diff] [blame] | 84 | //Add _PID field information in AdditionalData. |
| 85 | metalist.insert("_PID"); |
| 86 | |
Tom Joseph | 7a33ee4 | 2017-07-25 00:04:20 +0530 | [diff] [blame] | 87 | std::vector<std::string> additionalData; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 88 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 89 | // Read the journal from the end to get the most recent entry first. |
| 90 | // The result from the sd_journal_get_data() is of the form VARIABLE=value. |
| 91 | SD_JOURNAL_FOREACH_BACKWARDS(j) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 92 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 93 | const char *data = nullptr; |
| 94 | size_t length = 0; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 95 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 96 | // Look for the transaction id metadata variable |
| 97 | rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data, |
| 98 | &length); |
| 99 | if (rc < 0) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 100 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 101 | // This journal entry does not have the TRANSACTION_ID |
| 102 | // metadata variable. |
| 103 | continue; |
| 104 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 105 | |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 106 | // journald does not guarantee that sd_journal_get_data() returns NULL |
| 107 | // terminated strings, so need to specify the size to use to compare, |
| 108 | // use the returned length instead of anything that relies on NULL |
| 109 | // terminators like strlen(). |
| 110 | // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove |
| 111 | // the TRANSACTION_ID characters plus the (=) sign to do the comparison. |
| 112 | // 'data + transactionIdVarOffset' will be in the form of '1234'. |
| 113 | // 'length - transactionIdVarOffset' will be the length of '1234'. |
| 114 | if ((length <= (transactionIdVarOffset)) || |
| 115 | (transactionIdStr.compare(0, |
| 116 | transactionIdStr.size(), |
| 117 | data + transactionIdVarOffset, |
| 118 | length - transactionIdVarOffset) != 0)) |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 119 | { |
| 120 | // The value of the TRANSACTION_ID metadata is not the requested |
| 121 | // transaction id number. |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | // Search for all metadata variables in the current journal entry. |
| 126 | for (auto i = metalist.cbegin(); i != metalist.cend();) |
| 127 | { |
| 128 | rc = sd_journal_get_data(j, (*i).c_str(), |
| 129 | (const void **)&data, &length); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 130 | if (rc < 0) |
| 131 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 132 | // Metadata variable not found, check next metadata variable. |
| 133 | i++; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 134 | continue; |
| 135 | } |
| 136 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 137 | // Metadata variable found, save it and remove it from the set. |
| 138 | additionalData.emplace_back(data, length); |
| 139 | i = metalist.erase(i); |
| 140 | } |
| 141 | if (metalist.empty()) |
| 142 | { |
| 143 | // All metadata variables found, break out of journal loop. |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 144 | break; |
| 145 | } |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 146 | } |
| 147 | if (!metalist.empty()) |
| 148 | { |
| 149 | // Not all the metadata variables were found in the journal. |
| 150 | for (auto& metaVarStr : metalist) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 151 | { |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 152 | logging::log<logging::level::INFO>("Failed to find metadata", |
| 153 | logging::entry("META_FIELD=%s", metaVarStr.c_str())); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 157 | sd_journal_close(j); |
| 158 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 159 | // Create error Entry dbus object |
| 160 | entryId++; |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 161 | if (static_cast<Entry::Level>(reqLevel) >= Entry::sevLowerLimit) |
| 162 | { |
| 163 | infoErrors.push_back(entryId); |
| 164 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 165 | else |
| 166 | { |
| 167 | realErrors.push_back(entryId); |
| 168 | } |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 169 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 170 | std::chrono::system_clock::now().time_since_epoch()).count(); |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 171 | auto objPath = std::string(OBJ_ENTRY) + '/' + |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 172 | std::to_string(entryId); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 173 | |
Deepak Kodihalli | 35b4637 | 2017-02-27 04:58:18 -0600 | [diff] [blame] | 174 | AssociationList objects {}; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 175 | processMetadata(errMsg, additionalData, objects); |
| 176 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 177 | auto e = std::make_unique<Entry>( |
| 178 | busLog, |
| 179 | objPath, |
| 180 | entryId, |
| 181 | ms, // Milliseconds since 1970 |
| 182 | static_cast<Entry::Level>(reqLevel), |
| 183 | std::move(errMsg), |
| 184 | std::move(additionalData), |
| 185 | std::move(objects), |
| 186 | *this); |
| 187 | serialize(*e); |
| 188 | entries.insert(std::make_pair(entryId, std::move(e))); |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 191 | void Manager::processMetadata(const std::string& errorName, |
| 192 | const std::vector<std::string>& additionalData, |
| 193 | AssociationList& objects) const |
| 194 | { |
| 195 | // additionalData is a list of "metadata=value" |
| 196 | constexpr auto separator = '='; |
| 197 | for(const auto& entry: additionalData) |
| 198 | { |
| 199 | auto found = entry.find(separator); |
| 200 | if(std::string::npos != found) |
| 201 | { |
| 202 | auto metadata = entry.substr(0, found); |
| 203 | auto iter = meta.find(metadata); |
| 204 | if(meta.end() != iter) |
| 205 | { |
| 206 | (iter->second)(metadata, additionalData, objects); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 212 | void Manager::erase(uint32_t entryId) |
| 213 | { |
| 214 | auto entry = entries.find(entryId); |
| 215 | if(entries.end() != entry) |
| 216 | { |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 217 | // Delete the persistent representation of this error. |
| 218 | fs::path errorPath(ERRLOG_PERSIST_PATH); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 219 | errorPath /= std::to_string(entryId); |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 220 | fs::remove(errorPath); |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 221 | |
| 222 | auto removeId = [](std::list<uint32_t>& ids , uint32_t id) |
| 223 | { |
| 224 | auto it = std::find(ids.begin(), ids.end(), id); |
| 225 | if (it != ids.end()) |
| 226 | { |
| 227 | ids.erase(it); |
| 228 | } |
| 229 | }; |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 230 | if (entry->second->severity() >= Entry::sevLowerLimit) |
| 231 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 232 | removeId(infoErrors, entryId); |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | removeId(realErrors, entryId); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 237 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 238 | entries.erase(entry); |
| 239 | } |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 240 | else |
| 241 | { |
| 242 | logging::log<level::ERR>("Invalid entry ID to delete", |
| 243 | logging::entry("ID=%d", entryId)); |
| 244 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 247 | void Manager::restore() |
| 248 | { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 249 | auto sanity = [](const auto& id, const auto& restoredId) |
| 250 | { |
| 251 | return id == restoredId; |
| 252 | }; |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 253 | std::vector<uint32_t> errorIds; |
| 254 | |
| 255 | fs::path dir(ERRLOG_PERSIST_PATH); |
| 256 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 257 | { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | for(auto& file: fs::directory_iterator(dir)) |
| 262 | { |
| 263 | auto id = file.path().filename().c_str(); |
| 264 | auto idNum = std::stol(id); |
| 265 | auto e = std::make_unique<Entry>( |
| 266 | busLog, |
| 267 | std::string(OBJ_ENTRY) + '/' + id, |
| 268 | idNum, |
| 269 | *this); |
| 270 | if (deserialize(file.path(), *e)) |
| 271 | { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 272 | //validate the restored error entry id |
| 273 | if (sanity(static_cast<uint32_t>(idNum), e->id())) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 274 | { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 275 | e->emit_object_added(); |
| 276 | if (e->severity() >= Entry::sevLowerLimit) |
| 277 | { |
| 278 | infoErrors.push_back(idNum); |
| 279 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 280 | else |
| 281 | { |
| 282 | realErrors.push_back(idNum); |
| 283 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 284 | |
| 285 | entries.insert(std::make_pair(idNum, std::move(e))); |
| 286 | errorIds.push_back(idNum); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 287 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 288 | else |
| 289 | { |
| 290 | logging::log<logging::level::ERR>( |
| 291 | "Failed in sanity check while restoring error entry. " |
| 292 | "Ignoring error entry", |
| 293 | logging::entry("ID_NUM=%d", idNum), |
| 294 | logging::entry("ENTRY_ID=%d", e->id())); |
| 295 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 299 | if (!errorIds.empty()) |
| 300 | { |
| 301 | entryId = *(std::max_element(errorIds.begin(), errorIds.end())); |
| 302 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 305 | } // namespace internal |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 306 | } // namespace logging |
| 307 | } // namepsace phosphor |