blob: 86604166a37dfe6016950469dcbb10219d06994f [file] [log] [blame]
Deepak Kodihallic4966192018-08-23 02:19:58 -05001#pragma once
2
3#include <sdbusplus/bus.hpp>
4#include <sdbusplus/server/object.hpp>
5#include <string>
Deepak Kodihalli9fab2792018-08-28 07:47:11 -05006#include <phosphor-logging/log.hpp>
Deepak Kodihallic4966192018-08-23 02:19:58 -05007#include "xyz/openbmc_project/Network/Client/server.hpp"
8
9namespace phosphor
10{
11namespace rsyslog_config
12{
13
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050014using namespace phosphor::logging;
Deepak Kodihallic4966192018-08-23 02:19:58 -050015using NetworkClient = sdbusplus::xyz::openbmc_project::Network::server::Client;
16using Iface = sdbusplus::server::object::object<NetworkClient>;
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -050017namespace sdbusRule = sdbusplus::bus::match::rules;
Deepak Kodihallic4966192018-08-23 02:19:58 -050018
19/** @class Server
20 * @brief Configuration for rsyslog server
21 * @details A concrete implementation of the
22 * xyz.openbmc_project.Network.Client API, in order to
23 * provide remote rsyslog server's address and port.
24 */
25class Server : public Iface
26{
27 public:
28 Server() = delete;
29 Server(const Server&) = delete;
30 Server& operator=(const Server&) = delete;
31 Server(Server&&) = delete;
32 Server& operator=(Server&&) = delete;
33 virtual ~Server() = default;
34
35 /** @brief Constructor to put object onto bus at a dbus path.
36 * @param[in] bus - Bus to attach to.
37 * @param[in] path - Path to attach at.
38 * @param[in] filePath - rsyslog remote logging config file
39 */
40 Server(sdbusplus::bus::bus& bus,
41 const std::string& path,
42 const char* filePath) :
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050043 Iface(bus, path.c_str(), true),
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -050044 configFilePath(filePath),
45 hostnameChange(
46 bus,
47 sdbusRule::propertiesChanged(
48 "/org/freedesktop/hostname1", "org.freedesktop.hostname1"),
49 std::bind(std::mem_fn(&Server::hostnameChanged),
50 this, std::placeholders::_1))
Deepak Kodihallic4966192018-08-23 02:19:58 -050051 {
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050052 try
53 {
54 restore(configFilePath.c_str());
55 }
56 catch(const std::exception& e)
57 {
58 log<level::ERR>(e.what());
59 }
60
61 emit_object_added();
Deepak Kodihallic4966192018-08-23 02:19:58 -050062 }
63
64 using NetworkClient::address;
65 using NetworkClient::port;
66
67 /** @brief Override that updates rsyslog config file as well
68 * @param[in] value - remote server address
69 * @returns value of changed address
70 */
71 virtual std::string address(std::string value) override;
72
73 /** @brief Override that updates rsyslog config file as well
74 * @param[in] value - remote server port
75 * @returns value of changed port
76 */
77 virtual uint16_t port(uint16_t value) override;
78
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -050079 /** @brief Restart rsyslog's systemd unit
80 */
81 virtual void restart();
82
Deepak Kodihallic4966192018-08-23 02:19:58 -050083 private:
84 /** @brief Update remote server address and port in
85 * rsyslog config file.
86 * @param[in] serverAddress - remote server address
87 * @param[in] serverPort - remote server port
88 * @param[in] filePath - rsyslog config file path
89 */
90 void writeConfig(
91 const std::string& serverAddress,
92 uint16_t serverPort,
93 const char* filePath);
94
Deepak Kodihalli4db81462018-08-27 06:01:46 -050095 /** @brief Checks if input IP address is valid (uses getaddrinfo)
96 * @param[in] address - server address
97 * @returns true if valid, false otherwise
98 */
99 bool addressValid(const std::string& address);
100
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500101 /** @brief Populate existing config into D-Bus properties
102 * @param[in] filePath - rsyslog config file path
103 */
104 void restore(const char* filePath);
105
Deepak Kodihallic4966192018-08-23 02:19:58 -0500106 std::string configFilePath{};
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -0500107
108 /** @brief React to hostname change
109 * @param[in] msg - sdbusplus message
110 */
111 void hostnameChanged(sdbusplus::message::message& msg)
112 {
113 restart();
114 }
115
116 sdbusplus::bus::match_t hostnameChange;
Deepak Kodihallic4966192018-08-23 02:19:58 -0500117};
118
119} // namespace rsyslog_config
120} // namespace phosphor