blob: 74ba827c295be533a0ffbb3587783750be1f15ec [file] [log] [blame]
James Feist1df06a42019-04-11 14:23:04 -07001/*
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 Feist1a996582019-05-14 15:10:06 -070022#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070023#include <string>
24
James Feist1a996582019-05-14 15:10:06 -070025inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070026{
27
James Feist1a996582019-05-14 15:10:06 -070028 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 Feist1df06a42019-04-11 14:23:04 -070054 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
55 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -070056 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
57 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -070058}
59
James Feist1a996582019-05-14 15:10:06 -070060inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070061{
James Feist1a996582019-05-14 15:10:06 -070062 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 Feist1df06a42019-04-11 14:23:04 -070087
88 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
89 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -070090 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
91 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -070092}