James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 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 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <systemd/sd-journal.h> |
| 20 | |
| 21 | #include <iostream> |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 22 | #include <nlohmann/json.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 25 | inline void logDeviceAdded(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 26 | { |
| 27 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 28 | auto findType = record.find("Type"); |
| 29 | auto findAsset = |
| 30 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 31 | |
| 32 | std::string model = "Unkown"; |
| 33 | std::string type = "Unknown"; |
| 34 | std::string sn = "Unkown"; |
| 35 | |
| 36 | if (findType != record.end()) |
| 37 | { |
| 38 | type = findType->get<std::string>(); |
| 39 | } |
| 40 | if (findAsset != record.end()) |
| 41 | { |
| 42 | auto findModel = findAsset->find("Model"); |
| 43 | auto findSn = findAsset->find("SerialNumber"); |
| 44 | if (findModel != findAsset->end()) |
| 45 | { |
| 46 | model = findModel->get<std::string>(); |
| 47 | } |
| 48 | if (findSn != findAsset->end()) |
| 49 | { |
| 50 | sn = findSn->get<std::string>(); |
| 51 | } |
| 52 | } |
| 53 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 54 | sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR, |
| 55 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 56 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 57 | type.c_str(), sn.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 58 | } |
| 59 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 60 | inline void logDeviceRemoved(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 61 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 62 | auto findType = record.find("Type"); |
| 63 | auto findAsset = |
| 64 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 65 | |
| 66 | std::string model = "Unkown"; |
| 67 | std::string type = "Unknown"; |
| 68 | std::string sn = "Unkown"; |
| 69 | |
| 70 | if (findType != record.end()) |
| 71 | { |
| 72 | type = findType->get<std::string>(); |
| 73 | } |
| 74 | if (findAsset != record.end()) |
| 75 | { |
| 76 | auto findModel = findAsset->find("Model"); |
| 77 | auto findSn = findAsset->find("SerialNumber"); |
| 78 | if (findModel != findAsset->end()) |
| 79 | { |
| 80 | model = findModel->get<std::string>(); |
| 81 | } |
| 82 | if (findSn != findAsset->end()) |
| 83 | { |
| 84 | sn = findSn->get<std::string>(); |
| 85 | } |
| 86 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 87 | |
| 88 | sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR, |
| 89 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 90 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 91 | type.c_str(), sn.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 92 | } |