| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 1 | #include "../utils.hpp" | 
|  | 2 |  | 
|  | 3 | #include <systemd/sd-journal.h> | 
|  | 4 |  | 
|  | 5 | #include <boost/container/flat_map.hpp> | 
|  | 6 | #include <nlohmann/json.hpp> | 
|  | 7 |  | 
|  | 8 | #include <string> | 
|  | 9 |  | 
|  | 10 | void logDeviceAdded(const nlohmann::json& record) | 
|  | 11 | { | 
| Alexander Hansen | e665185 | 2025-01-21 16:22:05 +0100 | [diff] [blame] | 12 | if (!EM_CACHE_CONFIGURATION) | 
|  | 13 | { | 
|  | 14 | return; | 
|  | 15 | } | 
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 16 | if (!deviceHasLogging(record)) | 
|  | 17 | { | 
|  | 18 | return; | 
|  | 19 | } | 
|  | 20 | auto findType = record.find("Type"); | 
|  | 21 | auto findAsset = | 
|  | 22 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); | 
|  | 23 |  | 
|  | 24 | std::string model = "Unknown"; | 
|  | 25 | std::string type = "Unknown"; | 
|  | 26 | std::string sn = "Unknown"; | 
|  | 27 | std::string name = "Unknown"; | 
|  | 28 |  | 
|  | 29 | if (findType != record.end()) | 
|  | 30 | { | 
|  | 31 | type = findType->get<std::string>(); | 
|  | 32 | } | 
|  | 33 | if (findAsset != record.end()) | 
|  | 34 | { | 
|  | 35 | auto findModel = findAsset->find("Model"); | 
|  | 36 | auto findSn = findAsset->find("SerialNumber"); | 
|  | 37 | if (findModel != findAsset->end()) | 
|  | 38 | { | 
|  | 39 | model = findModel->get<std::string>(); | 
|  | 40 | } | 
|  | 41 | if (findSn != findAsset->end()) | 
|  | 42 | { | 
|  | 43 | const std::string* getSn = findSn->get_ptr<const std::string*>(); | 
|  | 44 | if (getSn != nullptr) | 
|  | 45 | { | 
|  | 46 | sn = *getSn; | 
|  | 47 | } | 
|  | 48 | else | 
|  | 49 | { | 
|  | 50 | sn = findSn->dump(); | 
|  | 51 | } | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | auto findName = record.find("Name"); | 
|  | 56 | if (findName != record.end()) | 
|  | 57 | { | 
|  | 58 | name = findName->get<std::string>(); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i", | 
|  | 62 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", | 
|  | 63 | "OpenBMC.0.1.InventoryAdded", | 
|  | 64 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), | 
|  | 65 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | void logDeviceRemoved(const nlohmann::json& record) | 
|  | 69 | { | 
|  | 70 | if (!deviceHasLogging(record)) | 
|  | 71 | { | 
|  | 72 | return; | 
|  | 73 | } | 
|  | 74 | auto findType = record.find("Type"); | 
|  | 75 | auto findAsset = | 
|  | 76 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); | 
|  | 77 |  | 
|  | 78 | std::string model = "Unknown"; | 
|  | 79 | std::string type = "Unknown"; | 
|  | 80 | std::string sn = "Unknown"; | 
|  | 81 | std::string name = "Unknown"; | 
|  | 82 |  | 
|  | 83 | if (findType != record.end()) | 
|  | 84 | { | 
|  | 85 | type = findType->get<std::string>(); | 
|  | 86 | } | 
|  | 87 | if (findAsset != record.end()) | 
|  | 88 | { | 
|  | 89 | auto findModel = findAsset->find("Model"); | 
|  | 90 | auto findSn = findAsset->find("SerialNumber"); | 
|  | 91 | if (findModel != findAsset->end()) | 
|  | 92 | { | 
|  | 93 | model = findModel->get<std::string>(); | 
|  | 94 | } | 
|  | 95 | if (findSn != findAsset->end()) | 
|  | 96 | { | 
|  | 97 | const std::string* getSn = findSn->get_ptr<const std::string*>(); | 
|  | 98 | if (getSn != nullptr) | 
|  | 99 | { | 
|  | 100 | sn = *getSn; | 
|  | 101 | } | 
|  | 102 | else | 
|  | 103 | { | 
|  | 104 | sn = findSn->dump(); | 
|  | 105 | } | 
|  | 106 | } | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | auto findName = record.find("Name"); | 
|  | 110 | if (findName != record.end()) | 
|  | 111 | { | 
|  | 112 | name = findName->get<std::string>(); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(), | 
|  | 116 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", | 
|  | 117 | "OpenBMC.0.1.InventoryRemoved", | 
|  | 118 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), | 
|  | 119 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); | 
|  | 120 | } |