blob: 979f84535caaacd849a2381d02d2e18e11aa05b9 [file] [log] [blame]
Deepak Kodihallic4966192018-08-23 02:19:58 -05001#pragma once
2
Patrick Venturef18bf832018-10-26 18:14:00 -07003#include "xyz/openbmc_project/Network/Client/server.hpp"
4
5#include <phosphor-logging/log.hpp>
Deepak Kodihallic4966192018-08-23 02:19:58 -05006#include <sdbusplus/bus.hpp>
7#include <sdbusplus/server/object.hpp>
8#include <string>
Deepak Kodihallic4966192018-08-23 02:19:58 -05009
10namespace phosphor
11{
12namespace rsyslog_config
13{
14
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050015using namespace phosphor::logging;
Deepak Kodihallic4966192018-08-23 02:19:58 -050016using NetworkClient = sdbusplus::xyz::openbmc_project::Network::server::Client;
17using Iface = sdbusplus::server::object::object<NetworkClient>;
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -050018namespace sdbusRule = sdbusplus::bus::match::rules;
Deepak Kodihallic4966192018-08-23 02:19:58 -050019
20/** @class Server
21 * @brief Configuration for rsyslog server
22 * @details A concrete implementation of the
23 * xyz.openbmc_project.Network.Client API, in order to
24 * provide remote rsyslog server's address and port.
25 */
26class Server : public Iface
27{
Patrick Venturef18bf832018-10-26 18:14:00 -070028 public:
29 Server() = delete;
30 Server(const Server&) = delete;
31 Server& operator=(const Server&) = delete;
32 Server(Server&&) = delete;
33 Server& operator=(Server&&) = delete;
34 virtual ~Server() = default;
Deepak Kodihallic4966192018-08-23 02:19:58 -050035
Patrick Venturef18bf832018-10-26 18:14:00 -070036 /** @brief Constructor to put object onto bus at a dbus path.
37 * @param[in] bus - Bus to attach to.
38 * @param[in] path - Path to attach at.
39 * @param[in] filePath - rsyslog remote logging config file
40 */
41 Server(sdbusplus::bus::bus& bus, const std::string& path,
42 const char* filePath) :
43 Iface(bus, path.c_str(), true),
44 configFilePath(filePath),
45 hostnameChange(
46 bus,
47 sdbusRule::propertiesChanged("/org/freedesktop/hostname1",
48 "org.freedesktop.hostname1"),
49 std::bind(std::mem_fn(&Server::hostnameChanged), this,
50 std::placeholders::_1))
51 {
52 try
Deepak Kodihallic4966192018-08-23 02:19:58 -050053 {
Patrick Venturef18bf832018-10-26 18:14:00 -070054 restore(configFilePath.c_str());
55 }
56 catch (const std::exception& e)
57 {
58 log<level::ERR>(e.what());
Deepak Kodihallic4966192018-08-23 02:19:58 -050059 }
60
Patrick Venturef18bf832018-10-26 18:14:00 -070061 emit_object_added();
62 }
Deepak Kodihallic4966192018-08-23 02:19:58 -050063
Patrick Venturef18bf832018-10-26 18:14:00 -070064 using NetworkClient::address;
65 using NetworkClient::port;
Deepak Kodihallic4966192018-08-23 02:19:58 -050066
Patrick Venturef18bf832018-10-26 18:14:00 -070067 /** @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;
Deepak Kodihallic4966192018-08-23 02:19:58 -050072
Patrick Venturef18bf832018-10-26 18:14:00 -070073 /** @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;
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -050078
Patrick Venturef18bf832018-10-26 18:14:00 -070079 /** @brief Restart rsyslog's systemd unit
80 */
81 virtual void restart();
Deepak Kodihallic4966192018-08-23 02:19:58 -050082
Patrick Venturef18bf832018-10-26 18:14:00 -070083 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(const std::string& serverAddress, uint16_t serverPort,
91 const char* filePath);
Deepak Kodihalli4db81462018-08-27 06:01:46 -050092
Patrick Venturef18bf832018-10-26 18:14:00 -070093 /** @brief Checks if input IP address is valid (uses getaddrinfo)
94 * @param[in] address - server address
95 * @returns true if valid, false otherwise
96 */
97 bool addressValid(const std::string& address);
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050098
Patrick Venturef18bf832018-10-26 18:14:00 -070099 /** @brief Populate existing config into D-Bus properties
100 * @param[in] filePath - rsyslog config file path
101 */
102 void restore(const char* filePath);
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -0500103
Patrick Venturef18bf832018-10-26 18:14:00 -0700104 std::string configFilePath{};
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -0500105
Patrick Venturef18bf832018-10-26 18:14:00 -0700106 /** @brief React to hostname change
107 * @param[in] msg - sdbusplus message
108 */
Patrick Williams0e734df2021-04-16 20:09:03 -0500109 void hostnameChanged(sdbusplus::message::message& /*msg*/)
Patrick Venturef18bf832018-10-26 18:14:00 -0700110 {
111 restart();
112 }
113
114 sdbusplus::bus::match_t hostnameChange;
Deepak Kodihallic4966192018-08-23 02:19:58 -0500115};
116
117} // namespace rsyslog_config
118} // namespace phosphor