1 /* 2 // Copyright (c) 2020 Intel Corporation 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 */ 16 #pragma once 17 #include "metric_report.hpp" 18 #include "registries.hpp" 19 #include "registries/base_message_registry.hpp" 20 #include "registries/openbmc_message_registry.hpp" 21 #include "registries/task_event_message_registry.hpp" 22 23 #include <sys/inotify.h> 24 25 #include <boost/asio/io_context.hpp> 26 #include <boost/container/flat_map.hpp> 27 #include <dbus_utility.hpp> 28 #include <error_messages.hpp> 29 #include <event_service_store.hpp> 30 #include <http_client.hpp> 31 #include <persistent_data.hpp> 32 #include <random.hpp> 33 #include <server_sent_events.hpp> 34 #include <utils/json_utils.hpp> 35 36 #include <cstdlib> 37 #include <ctime> 38 #include <fstream> 39 #include <memory> 40 #include <span> 41 42 namespace redfish 43 { 44 45 using ReadingsObjType = 46 std::vector<std::tuple<std::string, std::string, double, int32_t>>; 47 48 static constexpr const char* eventFormatType = "Event"; 49 static constexpr const char* metricReportFormatType = "MetricReport"; 50 51 static constexpr const char* eventServiceFile = 52 "/var/lib/bmcweb/eventservice_config.json"; 53 54 namespace message_registries 55 { 56 inline std::span<const MessageEntry> 57 getRegistryFromPrefix(const std::string& registryName) 58 { 59 if (task_event::header.registryPrefix == registryName) 60 { 61 return {task_event::registry}; 62 } 63 if (openbmc::header.registryPrefix == registryName) 64 { 65 return {openbmc::registry}; 66 } 67 if (base::header.registryPrefix == registryName) 68 { 69 return {base::registry}; 70 } 71 return {openbmc::registry}; 72 } 73 } // namespace message_registries 74 75 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 76 static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 77 static constexpr const char* redfishEventLogDir = "/var/log"; 78 static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 79 static constexpr const size_t iEventSize = sizeof(inotify_event); 80 static int inotifyFd = -1; 81 static int dirWatchDesc = -1; 82 static int fileWatchDesc = -1; 83 84 // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 85 using EventLogObjectsType = 86 std::tuple<std::string, std::string, std::string, std::string, std::string, 87 std::vector<std::string>>; 88 89 namespace message_registries 90 { 91 static const Message* 92 getMsgFromRegistry(const std::string& messageKey, 93 const std::span<const MessageEntry>& registry) 94 { 95 std::span<const MessageEntry>::iterator messageIt = 96 std::find_if(registry.begin(), registry.end(), 97 [&messageKey](const MessageEntry& messageEntry) { 98 return !messageKey.compare(messageEntry.first); 99 }); 100 if (messageIt != registry.end()) 101 { 102 return &messageIt->second; 103 } 104 105 return nullptr; 106 } 107 108 static const Message* formatMessage(const std::string_view& messageID) 109 { 110 // Redfish MessageIds are in the form 111 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 112 // the right Message 113 std::vector<std::string> fields; 114 fields.reserve(4); 115 boost::split(fields, messageID, boost::is_any_of(".")); 116 if (fields.size() != 4) 117 { 118 return nullptr; 119 } 120 std::string& registryName = fields[0]; 121 std::string& messageKey = fields[3]; 122 123 // Find the right registry and check it for the MessageKey 124 return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 125 } 126 } // namespace message_registries 127 128 namespace event_log 129 { 130 inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 131 { 132 static time_t prevTs = 0; 133 static int index = 0; 134 135 // Get the entry timestamp 136 std::time_t curTs = 0; 137 std::tm timeStruct = {}; 138 std::istringstream entryStream(logEntry); 139 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 140 { 141 curTs = std::mktime(&timeStruct); 142 if (curTs == -1) 143 { 144 return false; 145 } 146 } 147 // If the timestamp isn't unique, increment the index 148 index = (curTs == prevTs) ? index + 1 : 0; 149 150 // Save the timestamp 151 prevTs = curTs; 152 153 entryID = std::to_string(curTs); 154 if (index > 0) 155 { 156 entryID += "_" + std::to_string(index); 157 } 158 return true; 159 } 160 161 inline int getEventLogParams(const std::string& logEntry, 162 std::string& timestamp, std::string& messageID, 163 std::vector<std::string>& messageArgs) 164 { 165 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 166 // First get the Timestamp 167 size_t space = logEntry.find_first_of(' '); 168 if (space == std::string::npos) 169 { 170 return -EINVAL; 171 } 172 timestamp = logEntry.substr(0, space); 173 // Then get the log contents 174 size_t entryStart = logEntry.find_first_not_of(' ', space); 175 if (entryStart == std::string::npos) 176 { 177 return -EINVAL; 178 } 179 std::string_view entry(logEntry); 180 entry.remove_prefix(entryStart); 181 // Use split to separate the entry into its fields 182 std::vector<std::string> logEntryFields; 183 boost::split(logEntryFields, entry, boost::is_any_of(","), 184 boost::token_compress_on); 185 // We need at least a MessageId to be valid 186 if (logEntryFields.size() < 1) 187 { 188 return -EINVAL; 189 } 190 messageID = logEntryFields[0]; 191 192 // Get the MessageArgs from the log if there are any 193 if (logEntryFields.size() > 1) 194 { 195 std::string& messageArgsStart = logEntryFields[1]; 196 // If the first string is empty, assume there are no MessageArgs 197 if (!messageArgsStart.empty()) 198 { 199 messageArgs.assign(logEntryFields.begin() + 1, 200 logEntryFields.end()); 201 } 202 } 203 204 return 0; 205 } 206 207 inline void getRegistryAndMessageKey(const std::string& messageID, 208 std::string& registryName, 209 std::string& messageKey) 210 { 211 // Redfish MessageIds are in the form 212 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 213 // the right Message 214 std::vector<std::string> fields; 215 fields.reserve(4); 216 boost::split(fields, messageID, boost::is_any_of(".")); 217 if (fields.size() == 4) 218 { 219 registryName = fields[0]; 220 messageKey = fields[3]; 221 } 222 } 223 224 inline int formatEventLogEntry(const std::string& logEntryID, 225 const std::string& messageID, 226 const std::vector<std::string>& messageArgs, 227 std::string timestamp, 228 const std::string& customText, 229 nlohmann::json& logEntryJson) 230 { 231 // Get the Message from the MessageRegistry 232 const message_registries::Message* message = 233 message_registries::formatMessage(messageID); 234 235 std::string msg; 236 std::string severity; 237 if (message != nullptr) 238 { 239 msg = message->message; 240 severity = message->severity; 241 } 242 243 // Fill the MessageArgs into the Message 244 int i = 0; 245 for (const std::string& messageArg : messageArgs) 246 { 247 std::string argStr = "%" + std::to_string(++i); 248 size_t argPos = msg.find(argStr); 249 if (argPos != std::string::npos) 250 { 251 msg.replace(argPos, argStr.length(), messageArg); 252 } 253 } 254 255 // Get the Created time from the timestamp. The log timestamp is in 256 // RFC3339 format which matches the Redfish format except for the 257 // fractional seconds between the '.' and the '+', so just remove them. 258 std::size_t dot = timestamp.find_first_of('.'); 259 std::size_t plus = timestamp.find_first_of('+'); 260 if (dot != std::string::npos && plus != std::string::npos) 261 { 262 timestamp.erase(dot, plus - dot); 263 } 264 265 // Fill in the log entry with the gathered data 266 logEntryJson = {{"EventId", logEntryID}, 267 {"EventType", "Event"}, 268 {"Severity", std::move(severity)}, 269 {"Message", std::move(msg)}, 270 {"MessageId", messageID}, 271 {"MessageArgs", messageArgs}, 272 {"EventTimestamp", std::move(timestamp)}, 273 {"Context", customText}}; 274 return 0; 275 } 276 277 } // namespace event_log 278 #endif 279 280 inline bool isFilterQuerySpecialChar(char c) 281 { 282 switch (c) 283 { 284 case '(': 285 case ')': 286 case '\'': 287 return true; 288 default: 289 return false; 290 } 291 } 292 293 inline bool 294 readSSEQueryParams(std::string sseFilter, std::string& formatType, 295 std::vector<std::string>& messageIds, 296 std::vector<std::string>& registryPrefixes, 297 std::vector<std::string>& metricReportDefinitions) 298 { 299 sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(), 300 isFilterQuerySpecialChar), 301 sseFilter.end()); 302 303 std::vector<std::string> result; 304 boost::split(result, sseFilter, boost::is_any_of(" "), 305 boost::token_compress_on); 306 307 BMCWEB_LOG_DEBUG << "No of tokens in SEE query: " << result.size(); 308 309 constexpr uint8_t divisor = 4; 310 constexpr uint8_t minTokenSize = 3; 311 if (result.size() % divisor != minTokenSize) 312 { 313 BMCWEB_LOG_ERROR << "Invalid SSE filter specified."; 314 return false; 315 } 316 317 for (std::size_t i = 0; i < result.size(); i += divisor) 318 { 319 std::string& key = result[i]; 320 std::string& op = result[i + 1]; 321 std::string& value = result[i + 2]; 322 323 if ((i + minTokenSize) < result.size()) 324 { 325 std::string& separator = result[i + minTokenSize]; 326 // SSE supports only "or" and "and" in query params. 327 if ((separator != "or") && (separator != "and")) 328 { 329 BMCWEB_LOG_ERROR 330 << "Invalid group operator in SSE query parameters"; 331 return false; 332 } 333 } 334 335 // SSE supports only "eq" as per spec. 336 if (op != "eq") 337 { 338 BMCWEB_LOG_ERROR 339 << "Invalid assignment operator in SSE query parameters"; 340 return false; 341 } 342 343 BMCWEB_LOG_DEBUG << key << " : " << value; 344 if (key == "EventFormatType") 345 { 346 formatType = value; 347 } 348 else if (key == "MessageId") 349 { 350 messageIds.push_back(value); 351 } 352 else if (key == "RegistryPrefix") 353 { 354 registryPrefixes.push_back(value); 355 } 356 else if (key == "MetricReportDefinition") 357 { 358 metricReportDefinitions.push_back(value); 359 } 360 else 361 { 362 BMCWEB_LOG_ERROR << "Invalid property(" << key 363 << ")in SSE filter query."; 364 return false; 365 } 366 } 367 return true; 368 } 369 370 class Subscription : public persistent_data::UserSubscription 371 { 372 public: 373 Subscription(const Subscription&) = delete; 374 Subscription& operator=(const Subscription&) = delete; 375 Subscription(Subscription&&) = delete; 376 Subscription& operator=(Subscription&&) = delete; 377 378 Subscription(const std::string& inHost, const std::string& inPort, 379 const std::string& inPath, const std::string& inUriProto) : 380 eventSeqNum(1), 381 host(inHost), port(inPort), path(inPath), uriProto(inUriProto) 382 { 383 // Subscription constructor 384 } 385 386 Subscription(const std::shared_ptr<boost::beast::tcp_stream>& adaptor) : 387 eventSeqNum(1) 388 { 389 sseConn = std::make_shared<crow::ServerSentEvents>(adaptor); 390 } 391 392 ~Subscription() = default; 393 394 void sendEvent(const std::string& msg) 395 { 396 if (conn == nullptr) 397 { 398 // create the HttpClient connection 399 conn = std::make_shared<crow::HttpClient>( 400 crow::connections::systemBus->get_io_context(), id, host, port, 401 path, httpHeaders); 402 } 403 404 conn->sendData(msg); 405 eventSeqNum++; 406 407 if (sseConn != nullptr) 408 { 409 sseConn->sendData(eventSeqNum, msg); 410 } 411 } 412 413 void sendTestEventLog() 414 { 415 nlohmann::json logEntryArray; 416 logEntryArray.push_back({}); 417 nlohmann::json& logEntryJson = logEntryArray.back(); 418 419 logEntryJson = { 420 {"EventId", "TestID"}, 421 {"EventType", "Event"}, 422 {"Severity", "OK"}, 423 {"Message", "Generated test event"}, 424 {"MessageId", "OpenBMC.0.2.TestEventLog"}, 425 {"MessageArgs", nlohmann::json::array()}, 426 {"EventTimestamp", crow::utility::getDateTimeOffsetNow().first}, 427 {"Context", customText}}; 428 429 nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 430 {"Id", std::to_string(eventSeqNum)}, 431 {"Name", "Event Log"}, 432 {"Events", logEntryArray}}; 433 434 this->sendEvent( 435 msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 436 } 437 438 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 439 void filterAndSendEventLogs( 440 const std::vector<EventLogObjectsType>& eventRecords) 441 { 442 nlohmann::json logEntryArray; 443 for (const EventLogObjectsType& logEntry : eventRecords) 444 { 445 const std::string& idStr = std::get<0>(logEntry); 446 const std::string& timestamp = std::get<1>(logEntry); 447 const std::string& messageID = std::get<2>(logEntry); 448 const std::string& registryName = std::get<3>(logEntry); 449 const std::string& messageKey = std::get<4>(logEntry); 450 const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 451 452 // If registryPrefixes list is empty, don't filter events 453 // send everything. 454 if (registryPrefixes.size()) 455 { 456 auto obj = std::find(registryPrefixes.begin(), 457 registryPrefixes.end(), registryName); 458 if (obj == registryPrefixes.end()) 459 { 460 continue; 461 } 462 } 463 464 // If registryMsgIds list is empty, don't filter events 465 // send everything. 466 if (registryMsgIds.size()) 467 { 468 auto obj = std::find(registryMsgIds.begin(), 469 registryMsgIds.end(), messageKey); 470 if (obj == registryMsgIds.end()) 471 { 472 continue; 473 } 474 } 475 476 logEntryArray.push_back({}); 477 nlohmann::json& bmcLogEntry = logEntryArray.back(); 478 if (event_log::formatEventLogEntry(idStr, messageID, messageArgs, 479 timestamp, customText, 480 bmcLogEntry) != 0) 481 { 482 BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 483 continue; 484 } 485 } 486 487 if (logEntryArray.size() < 1) 488 { 489 BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 490 return; 491 } 492 493 nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 494 {"Id", std::to_string(eventSeqNum)}, 495 {"Name", "Event Log"}, 496 {"Events", logEntryArray}}; 497 498 this->sendEvent( 499 msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 500 } 501 #endif 502 503 void filterAndSendReports(const std::string& reportId, 504 const telemetry::TimestampReadings& var) 505 { 506 std::string mrdUri = telemetry::metricReportDefinitionUri + ("/" + id); 507 508 // Empty list means no filter. Send everything. 509 if (metricReportDefinitions.size()) 510 { 511 if (std::find(metricReportDefinitions.begin(), 512 metricReportDefinitions.end(), 513 mrdUri) == metricReportDefinitions.end()) 514 { 515 return; 516 } 517 } 518 519 nlohmann::json msg; 520 if (!telemetry::fillReport(msg, reportId, var)) 521 { 522 BMCWEB_LOG_ERROR << "Failed to fill the MetricReport for DBus " 523 "Report with id " 524 << reportId; 525 return; 526 } 527 528 this->sendEvent( 529 msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 530 } 531 532 void updateRetryConfig(const uint32_t retryAttempts, 533 const uint32_t retryTimeoutInterval) 534 { 535 if (conn != nullptr) 536 { 537 conn->setRetryConfig(retryAttempts, retryTimeoutInterval); 538 } 539 } 540 541 void updateRetryPolicy() 542 { 543 if (conn != nullptr) 544 { 545 conn->setRetryPolicy(retryPolicy); 546 } 547 } 548 549 uint64_t getEventSeqNum() 550 { 551 return eventSeqNum; 552 } 553 554 private: 555 uint64_t eventSeqNum; 556 std::string host; 557 std::string port; 558 std::string path; 559 std::string uriProto; 560 std::shared_ptr<crow::HttpClient> conn = nullptr; 561 std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr; 562 }; 563 564 class EventServiceManager 565 { 566 private: 567 bool serviceEnabled; 568 uint32_t retryAttempts; 569 uint32_t retryTimeoutInterval; 570 571 EventServiceManager() 572 { 573 // Load config from persist store. 574 initConfig(); 575 } 576 577 std::streampos redfishLogFilePosition{0}; 578 size_t noOfEventLogSubscribers{0}; 579 size_t noOfMetricReportSubscribers{0}; 580 std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor; 581 boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 582 subscriptionsMap; 583 584 uint64_t eventId{1}; 585 586 public: 587 EventServiceManager(const EventServiceManager&) = delete; 588 EventServiceManager& operator=(const EventServiceManager&) = delete; 589 EventServiceManager(EventServiceManager&&) = delete; 590 EventServiceManager& operator=(EventServiceManager&&) = delete; 591 ~EventServiceManager() = default; 592 593 static EventServiceManager& getInstance() 594 { 595 static EventServiceManager handler; 596 return handler; 597 } 598 599 void initConfig() 600 { 601 loadOldBehavior(); 602 603 persistent_data::EventServiceConfig eventServiceConfig = 604 persistent_data::EventServiceStore::getInstance() 605 .getEventServiceConfig(); 606 607 serviceEnabled = eventServiceConfig.enabled; 608 retryAttempts = eventServiceConfig.retryAttempts; 609 retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 610 611 for (const auto& it : persistent_data::EventServiceStore::getInstance() 612 .subscriptionsConfigMap) 613 { 614 std::shared_ptr<persistent_data::UserSubscription> newSub = 615 it.second; 616 617 std::string host; 618 std::string urlProto; 619 std::string port; 620 std::string path; 621 bool status = validateAndSplitUrl(newSub->destinationUrl, urlProto, 622 host, port, path); 623 624 if (!status) 625 { 626 BMCWEB_LOG_ERROR 627 << "Failed to validate and split destination url"; 628 continue; 629 } 630 std::shared_ptr<Subscription> subValue = 631 std::make_shared<Subscription>(host, port, path, urlProto); 632 633 subValue->id = newSub->id; 634 subValue->destinationUrl = newSub->destinationUrl; 635 subValue->protocol = newSub->protocol; 636 subValue->retryPolicy = newSub->retryPolicy; 637 subValue->customText = newSub->customText; 638 subValue->eventFormatType = newSub->eventFormatType; 639 subValue->subscriptionType = newSub->subscriptionType; 640 subValue->registryMsgIds = newSub->registryMsgIds; 641 subValue->registryPrefixes = newSub->registryPrefixes; 642 subValue->resourceTypes = newSub->resourceTypes; 643 subValue->httpHeaders = newSub->httpHeaders; 644 subValue->metricReportDefinitions = newSub->metricReportDefinitions; 645 646 if (subValue->id.empty()) 647 { 648 BMCWEB_LOG_ERROR << "Failed to add subscription"; 649 } 650 subscriptionsMap.insert(std::pair(subValue->id, subValue)); 651 652 updateNoOfSubscribersCount(); 653 654 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 655 656 cacheRedfishLogFile(); 657 658 #endif 659 // Update retry configuration. 660 subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 661 subValue->updateRetryPolicy(); 662 } 663 return; 664 } 665 666 void loadOldBehavior() 667 { 668 std::ifstream eventConfigFile(eventServiceFile); 669 if (!eventConfigFile.good()) 670 { 671 BMCWEB_LOG_DEBUG << "Old eventService config not exist"; 672 return; 673 } 674 auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 675 if (jsonData.is_discarded()) 676 { 677 BMCWEB_LOG_ERROR << "Old eventService config parse error."; 678 return; 679 } 680 681 for (const auto& item : jsonData.items()) 682 { 683 if (item.key() == "Configuration") 684 { 685 persistent_data::EventServiceStore::getInstance() 686 .getEventServiceConfig() 687 .fromJson(item.value()); 688 } 689 else if (item.key() == "Subscriptions") 690 { 691 for (const auto& elem : item.value()) 692 { 693 std::shared_ptr<persistent_data::UserSubscription> 694 newSubscription = 695 persistent_data::UserSubscription::fromJson(elem, 696 true); 697 if (newSubscription == nullptr) 698 { 699 BMCWEB_LOG_ERROR << "Problem reading subscription " 700 "from old persistent store"; 701 continue; 702 } 703 704 std::uniform_int_distribution<uint32_t> dist(0); 705 bmcweb::OpenSSLGenerator gen; 706 707 std::string id; 708 709 int retry = 3; 710 while (retry) 711 { 712 id = std::to_string(dist(gen)); 713 if (gen.error()) 714 { 715 retry = 0; 716 break; 717 } 718 newSubscription->id = id; 719 auto inserted = 720 persistent_data::EventServiceStore::getInstance() 721 .subscriptionsConfigMap.insert( 722 std::pair(id, newSubscription)); 723 if (inserted.second) 724 { 725 break; 726 } 727 --retry; 728 } 729 730 if (retry <= 0) 731 { 732 BMCWEB_LOG_ERROR 733 << "Failed to generate random number from old " 734 "persistent store"; 735 continue; 736 } 737 } 738 } 739 740 persistent_data::getConfig().writeData(); 741 std::remove(eventServiceFile); 742 BMCWEB_LOG_DEBUG << "Remove old eventservice config"; 743 } 744 } 745 746 void updateSubscriptionData() 747 { 748 persistent_data::EventServiceStore::getInstance() 749 .eventServiceConfig.enabled = serviceEnabled; 750 persistent_data::EventServiceStore::getInstance() 751 .eventServiceConfig.retryAttempts = retryAttempts; 752 persistent_data::EventServiceStore::getInstance() 753 .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 754 755 persistent_data::getConfig().writeData(); 756 } 757 758 void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 759 { 760 bool updateConfig = false; 761 bool updateRetryCfg = false; 762 763 if (serviceEnabled != cfg.enabled) 764 { 765 serviceEnabled = cfg.enabled; 766 if (serviceEnabled && noOfMetricReportSubscribers) 767 { 768 registerMetricReportSignal(); 769 } 770 else 771 { 772 unregisterMetricReportSignal(); 773 } 774 updateConfig = true; 775 } 776 777 if (retryAttempts != cfg.retryAttempts) 778 { 779 retryAttempts = cfg.retryAttempts; 780 updateConfig = true; 781 updateRetryCfg = true; 782 } 783 784 if (retryTimeoutInterval != cfg.retryTimeoutInterval) 785 { 786 retryTimeoutInterval = cfg.retryTimeoutInterval; 787 updateConfig = true; 788 updateRetryCfg = true; 789 } 790 791 if (updateConfig) 792 { 793 updateSubscriptionData(); 794 } 795 796 if (updateRetryCfg) 797 { 798 // Update the changed retry config to all subscriptions 799 for (const auto& it : 800 EventServiceManager::getInstance().subscriptionsMap) 801 { 802 std::shared_ptr<Subscription> entry = it.second; 803 entry->updateRetryConfig(retryAttempts, retryTimeoutInterval); 804 } 805 } 806 } 807 808 void updateNoOfSubscribersCount() 809 { 810 size_t eventLogSubCount = 0; 811 size_t metricReportSubCount = 0; 812 for (const auto& it : subscriptionsMap) 813 { 814 std::shared_ptr<Subscription> entry = it.second; 815 if (entry->eventFormatType == eventFormatType) 816 { 817 eventLogSubCount++; 818 } 819 else if (entry->eventFormatType == metricReportFormatType) 820 { 821 metricReportSubCount++; 822 } 823 } 824 825 noOfEventLogSubscribers = eventLogSubCount; 826 if (noOfMetricReportSubscribers != metricReportSubCount) 827 { 828 noOfMetricReportSubscribers = metricReportSubCount; 829 if (noOfMetricReportSubscribers) 830 { 831 registerMetricReportSignal(); 832 } 833 else 834 { 835 unregisterMetricReportSignal(); 836 } 837 } 838 } 839 840 std::shared_ptr<Subscription> getSubscription(const std::string& id) 841 { 842 auto obj = subscriptionsMap.find(id); 843 if (obj == subscriptionsMap.end()) 844 { 845 BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 846 return nullptr; 847 } 848 std::shared_ptr<Subscription> subValue = obj->second; 849 return subValue; 850 } 851 852 std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 853 const bool updateFile = true) 854 { 855 856 std::uniform_int_distribution<uint32_t> dist(0); 857 bmcweb::OpenSSLGenerator gen; 858 859 std::string id; 860 861 int retry = 3; 862 while (retry) 863 { 864 id = std::to_string(dist(gen)); 865 if (gen.error()) 866 { 867 retry = 0; 868 break; 869 } 870 auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 871 if (inserted.second) 872 { 873 break; 874 } 875 --retry; 876 } 877 878 if (retry <= 0) 879 { 880 BMCWEB_LOG_ERROR << "Failed to generate random number"; 881 return ""; 882 } 883 884 std::shared_ptr<persistent_data::UserSubscription> newSub = 885 std::make_shared<persistent_data::UserSubscription>(); 886 newSub->id = id; 887 newSub->destinationUrl = subValue->destinationUrl; 888 newSub->protocol = subValue->protocol; 889 newSub->retryPolicy = subValue->retryPolicy; 890 newSub->customText = subValue->customText; 891 newSub->eventFormatType = subValue->eventFormatType; 892 newSub->subscriptionType = subValue->subscriptionType; 893 newSub->registryMsgIds = subValue->registryMsgIds; 894 newSub->registryPrefixes = subValue->registryPrefixes; 895 newSub->resourceTypes = subValue->resourceTypes; 896 newSub->httpHeaders = subValue->httpHeaders; 897 newSub->metricReportDefinitions = subValue->metricReportDefinitions; 898 persistent_data::EventServiceStore::getInstance() 899 .subscriptionsConfigMap.emplace(newSub->id, newSub); 900 901 updateNoOfSubscribersCount(); 902 903 if (updateFile) 904 { 905 updateSubscriptionData(); 906 } 907 908 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 909 if (redfishLogFilePosition != 0) 910 { 911 cacheRedfishLogFile(); 912 } 913 #endif 914 // Update retry configuration. 915 subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 916 subValue->updateRetryPolicy(); 917 918 return id; 919 } 920 921 bool isSubscriptionExist(const std::string& id) 922 { 923 auto obj = subscriptionsMap.find(id); 924 if (obj == subscriptionsMap.end()) 925 { 926 return false; 927 } 928 return true; 929 } 930 931 void deleteSubscription(const std::string& id) 932 { 933 auto obj = subscriptionsMap.find(id); 934 if (obj != subscriptionsMap.end()) 935 { 936 subscriptionsMap.erase(obj); 937 auto obj2 = persistent_data::EventServiceStore::getInstance() 938 .subscriptionsConfigMap.find(id); 939 persistent_data::EventServiceStore::getInstance() 940 .subscriptionsConfigMap.erase(obj2); 941 updateNoOfSubscribersCount(); 942 updateSubscriptionData(); 943 } 944 } 945 946 size_t getNumberOfSubscriptions() 947 { 948 return subscriptionsMap.size(); 949 } 950 951 std::vector<std::string> getAllIDs() 952 { 953 std::vector<std::string> idList; 954 for (const auto& it : subscriptionsMap) 955 { 956 idList.emplace_back(it.first); 957 } 958 return idList; 959 } 960 961 bool isDestinationExist(const std::string& destUrl) 962 { 963 for (const auto& it : subscriptionsMap) 964 { 965 std::shared_ptr<Subscription> entry = it.second; 966 if (entry->destinationUrl == destUrl) 967 { 968 BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 969 return true; 970 } 971 } 972 return false; 973 } 974 975 void sendTestEventLog() 976 { 977 for (const auto& it : this->subscriptionsMap) 978 { 979 std::shared_ptr<Subscription> entry = it.second; 980 entry->sendTestEventLog(); 981 } 982 } 983 984 void sendEvent(const nlohmann::json& eventMessageIn, 985 const std::string& origin, const std::string& resType) 986 { 987 nlohmann::json eventRecord = nlohmann::json::array(); 988 nlohmann::json eventMessage = eventMessageIn; 989 // MemberId is 0 : since we are sending one event record. 990 uint64_t memberId = 0; 991 992 nlohmann::json event = { 993 {"EventId", eventId}, 994 {"MemberId", memberId}, 995 {"EventTimestamp", crow::utility::getDateTimeOffsetNow().first}, 996 {"OriginOfCondition", origin}}; 997 for (nlohmann::json::iterator it = event.begin(); it != event.end(); 998 ++it) 999 { 1000 eventMessage[it.key()] = it.value(); 1001 } 1002 eventRecord.push_back(eventMessage); 1003 1004 for (const auto& it : this->subscriptionsMap) 1005 { 1006 std::shared_ptr<Subscription> entry = it.second; 1007 bool isSubscribed = false; 1008 // Search the resourceTypes list for the subscription. 1009 // If resourceTypes list is empty, don't filter events 1010 // send everything. 1011 if (entry->resourceTypes.size()) 1012 { 1013 for (const auto& resource : entry->resourceTypes) 1014 { 1015 if (resType == resource) 1016 { 1017 BMCWEB_LOG_INFO << "ResourceType " << resource 1018 << " found in the subscribed list"; 1019 isSubscribed = true; 1020 break; 1021 } 1022 } 1023 } 1024 else // resourceTypes list is empty. 1025 { 1026 isSubscribed = true; 1027 } 1028 if (isSubscribed) 1029 { 1030 nlohmann::json msgJson = { 1031 {"@odata.type", "#Event.v1_4_0.Event"}, 1032 {"Name", "Event Log"}, 1033 {"Id", eventId}, 1034 {"Events", eventRecord}}; 1035 entry->sendEvent(msgJson.dump( 1036 2, ' ', true, nlohmann::json::error_handler_t::replace)); 1037 eventId++; // increament the eventId 1038 } 1039 else 1040 { 1041 BMCWEB_LOG_INFO << "Not subscribed to this resource"; 1042 } 1043 } 1044 } 1045 void sendBroadcastMsg(const std::string& broadcastMsg) 1046 { 1047 for (const auto& it : this->subscriptionsMap) 1048 { 1049 std::shared_ptr<Subscription> entry = it.second; 1050 nlohmann::json msgJson = { 1051 {"Timestamp", crow::utility::getDateTimeOffsetNow().first}, 1052 {"OriginOfCondition", "/ibm/v1/HMC/BroadcastService"}, 1053 {"Name", "Broadcast Message"}, 1054 {"Message", broadcastMsg}}; 1055 entry->sendEvent(msgJson.dump( 1056 2, ' ', true, nlohmann::json::error_handler_t::replace)); 1057 } 1058 } 1059 1060 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 1061 1062 void resetRedfishFilePosition() 1063 { 1064 // Control would be here when Redfish file is created. 1065 // Reset File Position as new file is created 1066 redfishLogFilePosition = 0; 1067 1068 return; 1069 } 1070 1071 void cacheRedfishLogFile() 1072 { 1073 // Open the redfish file and read till the last record. 1074 1075 std::ifstream logStream(redfishEventLogFile); 1076 if (!logStream.good()) 1077 { 1078 BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 1079 return; 1080 } 1081 std::string logEntry; 1082 while (std::getline(logStream, logEntry)) 1083 { 1084 redfishLogFilePosition = logStream.tellg(); 1085 } 1086 1087 BMCWEB_LOG_DEBUG << "Next Log Position : " << redfishLogFilePosition; 1088 } 1089 1090 void readEventLogsFromFile() 1091 { 1092 std::ifstream logStream(redfishEventLogFile); 1093 if (!logStream.good()) 1094 { 1095 BMCWEB_LOG_ERROR << " Redfish log file open failed"; 1096 return; 1097 } 1098 1099 std::vector<EventLogObjectsType> eventRecords; 1100 1101 std::string logEntry; 1102 1103 // Get the read pointer to the next log to be read. 1104 logStream.seekg(redfishLogFilePosition); 1105 1106 while (std::getline(logStream, logEntry)) 1107 { 1108 // Update Pointer position 1109 redfishLogFilePosition = logStream.tellg(); 1110 1111 std::string idStr; 1112 if (!event_log::getUniqueEntryID(logEntry, idStr)) 1113 { 1114 continue; 1115 } 1116 1117 if (!serviceEnabled || !noOfEventLogSubscribers) 1118 { 1119 // If Service is not enabled, no need to compute 1120 // the remaining items below. 1121 // But, Loop must continue to keep track of Timestamp 1122 continue; 1123 } 1124 1125 std::string timestamp; 1126 std::string messageID; 1127 std::vector<std::string> messageArgs; 1128 if (event_log::getEventLogParams(logEntry, timestamp, messageID, 1129 messageArgs) != 0) 1130 { 1131 BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 1132 continue; 1133 } 1134 1135 std::string registryName; 1136 std::string messageKey; 1137 event_log::getRegistryAndMessageKey(messageID, registryName, 1138 messageKey); 1139 if (registryName.empty() || messageKey.empty()) 1140 { 1141 continue; 1142 } 1143 1144 eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 1145 messageKey, messageArgs); 1146 } 1147 1148 if (!serviceEnabled || !noOfEventLogSubscribers) 1149 { 1150 BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 1151 return; 1152 } 1153 1154 if (eventRecords.empty()) 1155 { 1156 // No Records to send 1157 BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 1158 return; 1159 } 1160 1161 for (const auto& it : this->subscriptionsMap) 1162 { 1163 std::shared_ptr<Subscription> entry = it.second; 1164 if (entry->eventFormatType == "Event") 1165 { 1166 entry->filterAndSendEventLogs(eventRecords); 1167 } 1168 } 1169 } 1170 1171 static void watchRedfishEventLogFile() 1172 { 1173 if (!inotifyConn) 1174 { 1175 return; 1176 } 1177 1178 static std::array<char, 1024> readBuffer; 1179 1180 inotifyConn->async_read_some( 1181 boost::asio::buffer(readBuffer), 1182 [&](const boost::system::error_code& ec, 1183 const std::size_t& bytesTransferred) { 1184 if (ec) 1185 { 1186 BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 1187 return; 1188 } 1189 std::size_t index = 0; 1190 while ((index + iEventSize) <= bytesTransferred) 1191 { 1192 struct inotify_event event; 1193 std::memcpy(&event, &readBuffer[index], iEventSize); 1194 if (event.wd == dirWatchDesc) 1195 { 1196 if ((event.len == 0) || 1197 (index + iEventSize + event.len > bytesTransferred)) 1198 { 1199 index += (iEventSize + event.len); 1200 continue; 1201 } 1202 1203 std::string fileName(&readBuffer[index + iEventSize], 1204 event.len); 1205 if (std::strcmp(fileName.c_str(), "redfish") != 0) 1206 { 1207 index += (iEventSize + event.len); 1208 continue; 1209 } 1210 1211 BMCWEB_LOG_DEBUG 1212 << "Redfish log file created/deleted. event.name: " 1213 << fileName; 1214 if (event.mask == IN_CREATE) 1215 { 1216 if (fileWatchDesc != -1) 1217 { 1218 BMCWEB_LOG_DEBUG 1219 << "Remove and Add inotify watcher on " 1220 "redfish event log file"; 1221 // Remove existing inotify watcher and add 1222 // with new redfish event log file. 1223 inotify_rm_watch(inotifyFd, fileWatchDesc); 1224 fileWatchDesc = -1; 1225 } 1226 1227 fileWatchDesc = inotify_add_watch( 1228 inotifyFd, redfishEventLogFile, IN_MODIFY); 1229 if (fileWatchDesc == -1) 1230 { 1231 BMCWEB_LOG_ERROR 1232 << "inotify_add_watch failed for " 1233 "redfish log file."; 1234 return; 1235 } 1236 1237 EventServiceManager::getInstance() 1238 .resetRedfishFilePosition(); 1239 EventServiceManager::getInstance() 1240 .readEventLogsFromFile(); 1241 } 1242 else if ((event.mask == IN_DELETE) || 1243 (event.mask == IN_MOVED_TO)) 1244 { 1245 if (fileWatchDesc != -1) 1246 { 1247 inotify_rm_watch(inotifyFd, fileWatchDesc); 1248 fileWatchDesc = -1; 1249 } 1250 } 1251 } 1252 else if (event.wd == fileWatchDesc) 1253 { 1254 if (event.mask == IN_MODIFY) 1255 { 1256 EventServiceManager::getInstance() 1257 .readEventLogsFromFile(); 1258 } 1259 } 1260 index += (iEventSize + event.len); 1261 } 1262 1263 watchRedfishEventLogFile(); 1264 }); 1265 } 1266 1267 static int startEventLogMonitor(boost::asio::io_context& ioc) 1268 { 1269 inotifyConn.emplace(ioc); 1270 inotifyFd = inotify_init1(IN_NONBLOCK); 1271 if (inotifyFd == -1) 1272 { 1273 BMCWEB_LOG_ERROR << "inotify_init1 failed."; 1274 return -1; 1275 } 1276 1277 // Add watch on directory to handle redfish event log file 1278 // create/delete. 1279 dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1280 IN_CREATE | IN_MOVED_TO | IN_DELETE); 1281 if (dirWatchDesc == -1) 1282 { 1283 BMCWEB_LOG_ERROR 1284 << "inotify_add_watch failed for event log directory."; 1285 return -1; 1286 } 1287 1288 // Watch redfish event log file for modifications. 1289 fileWatchDesc = 1290 inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY); 1291 if (fileWatchDesc == -1) 1292 { 1293 BMCWEB_LOG_ERROR 1294 << "inotify_add_watch failed for redfish log file."; 1295 // Don't return error if file not exist. 1296 // Watch on directory will handle create/delete of file. 1297 } 1298 1299 // monitor redfish event log file 1300 inotifyConn->assign(inotifyFd); 1301 watchRedfishEventLogFile(); 1302 1303 return 0; 1304 } 1305 1306 #endif 1307 void getReadingsForReport(sdbusplus::message::message& msg) 1308 { 1309 sdbusplus::message::object_path path(msg.get_path()); 1310 std::string id = path.filename(); 1311 if (id.empty()) 1312 { 1313 BMCWEB_LOG_ERROR << "Failed to get Id from path"; 1314 return; 1315 } 1316 1317 std::string interface; 1318 std::vector<std::pair<std::string, dbus::utility::DbusVariantType>> 1319 props; 1320 std::vector<std::string> invalidProps; 1321 msg.read(interface, props, invalidProps); 1322 1323 auto found = 1324 std::find_if(props.begin(), props.end(), 1325 [](const auto& x) { return x.first == "Readings"; }); 1326 if (found == props.end()) 1327 { 1328 BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 1329 return; 1330 } 1331 1332 const telemetry::TimestampReadings* readings = 1333 std::get_if<telemetry::TimestampReadings>(&found->second); 1334 if (!readings) 1335 { 1336 BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 1337 return; 1338 } 1339 1340 for (const auto& it : 1341 EventServiceManager::getInstance().subscriptionsMap) 1342 { 1343 Subscription& entry = *it.second.get(); 1344 if (entry.eventFormatType == metricReportFormatType) 1345 { 1346 entry.filterAndSendReports(id, *readings); 1347 } 1348 } 1349 } 1350 1351 void unregisterMetricReportSignal() 1352 { 1353 if (matchTelemetryMonitor) 1354 { 1355 BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1356 matchTelemetryMonitor.reset(); 1357 matchTelemetryMonitor = nullptr; 1358 } 1359 } 1360 1361 void registerMetricReportSignal() 1362 { 1363 if (!serviceEnabled || matchTelemetryMonitor) 1364 { 1365 BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1366 return; 1367 } 1368 1369 BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1370 std::string matchStr = "type='signal',member='PropertiesChanged'," 1371 "interface='org.freedesktop.DBus.Properties'," 1372 "arg0=xyz.openbmc_project.Telemetry.Report"; 1373 1374 matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>( 1375 *crow::connections::systemBus, matchStr, 1376 [this](sdbusplus::message::message& msg) { 1377 if (msg.is_method_error()) 1378 { 1379 BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 1380 return; 1381 } 1382 1383 getReadingsForReport(msg); 1384 }); 1385 } 1386 1387 bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto, 1388 std::string& host, std::string& port, 1389 std::string& path) 1390 { 1391 // Validate URL using regex expression 1392 // Format: <protocol>://<host>:<port>/<path> 1393 // protocol: http/https 1394 const std::regex urlRegex( 1395 "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/" 1396 "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)"); 1397 std::cmatch match; 1398 if (!std::regex_match(destUrl.c_str(), match, urlRegex)) 1399 { 1400 BMCWEB_LOG_INFO << "Dest. url did not match "; 1401 return false; 1402 } 1403 1404 urlProto = std::string(match[1].first, match[1].second); 1405 if (urlProto == "http") 1406 { 1407 #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING 1408 return false; 1409 #endif 1410 } 1411 1412 host = std::string(match[2].first, match[2].second); 1413 port = std::string(match[3].first, match[3].second); 1414 path = std::string(match[4].first, match[4].second); 1415 if (port.empty()) 1416 { 1417 if (urlProto == "http") 1418 { 1419 port = "80"; 1420 } 1421 else 1422 { 1423 port = "443"; 1424 } 1425 } 1426 if (path.empty()) 1427 { 1428 path = "/"; 1429 } 1430 return true; 1431 } 1432 }; 1433 1434 } // namespace redfish 1435