blob: 57c292c30121126db84237ab1cec717679080a49 [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>
Patrick Williams2544b412022-10-04 08:41:06 -05008
Deepak Kodihallic4966192018-08-23 02:19:58 -05009#include <string>
Deepak Kodihallic4966192018-08-23 02:19:58 -050010
11namespace phosphor
12{
13namespace rsyslog_config
14{
15
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050016using namespace phosphor::logging;
Deepak Kodihallic4966192018-08-23 02:19:58 -050017using NetworkClient = sdbusplus::xyz::openbmc_project::Network::server::Client;
Patrick Williams45e83522022-07-22 19:26:52 -050018using Iface = sdbusplus::server::object_t<NetworkClient>;
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -050019namespace sdbusRule = sdbusplus::bus::match::rules;
Deepak Kodihallic4966192018-08-23 02:19:58 -050020
21/** @class Server
22 * @brief Configuration for rsyslog server
23 * @details A concrete implementation of the
24 * xyz.openbmc_project.Network.Client API, in order to
25 * provide remote rsyslog server's address and port.
26 */
27class Server : public Iface
28{
Patrick Venturef18bf832018-10-26 18:14:00 -070029 public:
30 Server() = delete;
31 Server(const Server&) = delete;
32 Server& operator=(const Server&) = delete;
33 Server(Server&&) = delete;
34 Server& operator=(Server&&) = delete;
35 virtual ~Server() = default;
Deepak Kodihallic4966192018-08-23 02:19:58 -050036
Patrick Venturef18bf832018-10-26 18:14:00 -070037 /** @brief Constructor to put object onto bus at a dbus path.
38 * @param[in] bus - Bus to attach to.
39 * @param[in] path - Path to attach at.
40 * @param[in] filePath - rsyslog remote logging config file
41 */
Patrick Williams45e83522022-07-22 19:26:52 -050042 Server(sdbusplus::bus_t& bus, const std::string& path,
Patrick Venturef18bf832018-10-26 18:14:00 -070043 const char* filePath) :
Patrick Williams6ef6b252022-03-30 14:28:27 -050044 Iface(bus, path.c_str(), Iface::action::defer_emit),
Patrick Venturef18bf832018-10-26 18:14:00 -070045 configFilePath(filePath),
46 hostnameChange(
47 bus,
48 sdbusRule::propertiesChanged("/org/freedesktop/hostname1",
49 "org.freedesktop.hostname1"),
50 std::bind(std::mem_fn(&Server::hostnameChanged), this,
51 std::placeholders::_1))
52 {
53 try
Deepak Kodihallic4966192018-08-23 02:19:58 -050054 {
Patrick Venturef18bf832018-10-26 18:14:00 -070055 restore(configFilePath.c_str());
56 }
57 catch (const std::exception& e)
58 {
59 log<level::ERR>(e.what());
Deepak Kodihallic4966192018-08-23 02:19:58 -050060 }
61
Patrick Venturef18bf832018-10-26 18:14:00 -070062 emit_object_added();
63 }
Deepak Kodihallic4966192018-08-23 02:19:58 -050064
Patrick Venturef18bf832018-10-26 18:14:00 -070065 using NetworkClient::address;
66 using NetworkClient::port;
Deepak Kodihallic4966192018-08-23 02:19:58 -050067
Patrick Venturef18bf832018-10-26 18:14:00 -070068 /** @brief Override that updates rsyslog config file as well
69 * @param[in] value - remote server address
70 * @returns value of changed address
71 */
72 virtual std::string address(std::string value) override;
Deepak Kodihallic4966192018-08-23 02:19:58 -050073
Patrick Venturef18bf832018-10-26 18:14:00 -070074 /** @brief Override that updates rsyslog config file as well
75 * @param[in] value - remote server port
76 * @returns value of changed port
77 */
78 virtual uint16_t port(uint16_t value) override;
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -050079
Patrick Venturef18bf832018-10-26 18:14:00 -070080 /** @brief Restart rsyslog's systemd unit
81 */
82 virtual void restart();
Deepak Kodihallic4966192018-08-23 02:19:58 -050083
Patrick Venturef18bf832018-10-26 18:14:00 -070084 private:
85 /** @brief Update remote server address and port in
86 * rsyslog config file.
87 * @param[in] serverAddress - remote server address
88 * @param[in] serverPort - remote server port
89 * @param[in] filePath - rsyslog config file path
90 */
91 void writeConfig(const std::string& serverAddress, uint16_t serverPort,
92 const char* filePath);
Deepak Kodihalli4db81462018-08-27 06:01:46 -050093
Patrick Venturef18bf832018-10-26 18:14:00 -070094 /** @brief Checks if input IP address is valid (uses getaddrinfo)
95 * @param[in] address - server address
96 * @returns true if valid, false otherwise
97 */
98 bool addressValid(const std::string& address);
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050099
Patrick Venturef18bf832018-10-26 18:14:00 -0700100 /** @brief Populate existing config into D-Bus properties
101 * @param[in] filePath - rsyslog config file path
102 */
103 void restore(const char* filePath);
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -0500104
Patrick Venturef18bf832018-10-26 18:14:00 -0700105 std::string configFilePath{};
Deepak Kodihalliaabb92e2018-10-17 05:03:20 -0500106
Patrick Venturef18bf832018-10-26 18:14:00 -0700107 /** @brief React to hostname change
108 * @param[in] msg - sdbusplus message
109 */
Patrick Williams45e83522022-07-22 19:26:52 -0500110 void hostnameChanged(sdbusplus::message_t& /*msg*/)
Patrick Venturef18bf832018-10-26 18:14:00 -0700111 {
112 restart();
113 }
114
115 sdbusplus::bus::match_t hostnameChange;
Deepak Kodihallic4966192018-08-23 02:19:58 -0500116};
117
118} // namespace rsyslog_config
119} // namespace phosphor