| 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 | { | 
|  | 12 | if (!deviceHasLogging(record)) | 
|  | 13 | { | 
|  | 14 | return; | 
|  | 15 | } | 
|  | 16 | auto findType = record.find("Type"); | 
|  | 17 | auto findAsset = | 
|  | 18 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); | 
|  | 19 |  | 
|  | 20 | std::string model = "Unknown"; | 
|  | 21 | std::string type = "Unknown"; | 
|  | 22 | std::string sn = "Unknown"; | 
|  | 23 | std::string name = "Unknown"; | 
|  | 24 |  | 
|  | 25 | if (findType != record.end()) | 
|  | 26 | { | 
|  | 27 | type = findType->get<std::string>(); | 
|  | 28 | } | 
|  | 29 | if (findAsset != record.end()) | 
|  | 30 | { | 
|  | 31 | auto findModel = findAsset->find("Model"); | 
|  | 32 | auto findSn = findAsset->find("SerialNumber"); | 
|  | 33 | if (findModel != findAsset->end()) | 
|  | 34 | { | 
|  | 35 | model = findModel->get<std::string>(); | 
|  | 36 | } | 
|  | 37 | if (findSn != findAsset->end()) | 
|  | 38 | { | 
|  | 39 | const std::string* getSn = findSn->get_ptr<const std::string*>(); | 
|  | 40 | if (getSn != nullptr) | 
|  | 41 | { | 
|  | 42 | sn = *getSn; | 
|  | 43 | } | 
|  | 44 | else | 
|  | 45 | { | 
|  | 46 | sn = findSn->dump(); | 
|  | 47 | } | 
|  | 48 | } | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | auto findName = record.find("Name"); | 
|  | 52 | if (findName != record.end()) | 
|  | 53 | { | 
|  | 54 | name = findName->get<std::string>(); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i", | 
|  | 58 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", | 
|  | 59 | "OpenBMC.0.1.InventoryAdded", | 
|  | 60 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), | 
|  | 61 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | void logDeviceRemoved(const nlohmann::json& record) | 
|  | 65 | { | 
|  | 66 | if (!deviceHasLogging(record)) | 
|  | 67 | { | 
|  | 68 | return; | 
|  | 69 | } | 
|  | 70 | auto findType = record.find("Type"); | 
|  | 71 | auto findAsset = | 
|  | 72 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); | 
|  | 73 |  | 
|  | 74 | std::string model = "Unknown"; | 
|  | 75 | std::string type = "Unknown"; | 
|  | 76 | std::string sn = "Unknown"; | 
|  | 77 | std::string name = "Unknown"; | 
|  | 78 |  | 
|  | 79 | if (findType != record.end()) | 
|  | 80 | { | 
|  | 81 | type = findType->get<std::string>(); | 
|  | 82 | } | 
|  | 83 | if (findAsset != record.end()) | 
|  | 84 | { | 
|  | 85 | auto findModel = findAsset->find("Model"); | 
|  | 86 | auto findSn = findAsset->find("SerialNumber"); | 
|  | 87 | if (findModel != findAsset->end()) | 
|  | 88 | { | 
|  | 89 | model = findModel->get<std::string>(); | 
|  | 90 | } | 
|  | 91 | if (findSn != findAsset->end()) | 
|  | 92 | { | 
|  | 93 | const std::string* getSn = findSn->get_ptr<const std::string*>(); | 
|  | 94 | if (getSn != nullptr) | 
|  | 95 | { | 
|  | 96 | sn = *getSn; | 
|  | 97 | } | 
|  | 98 | else | 
|  | 99 | { | 
|  | 100 | sn = findSn->dump(); | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | auto findName = record.find("Name"); | 
|  | 106 | if (findName != record.end()) | 
|  | 107 | { | 
|  | 108 | name = findName->get<std::string>(); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(), | 
|  | 112 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", | 
|  | 113 | "OpenBMC.0.1.InventoryRemoved", | 
|  | 114 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), | 
|  | 115 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); | 
|  | 116 | } |