From 20159f341e44a5ea5c25e010f49ef4b0ea475189 Mon Sep 17 00:00:00 2001 From: ranma Date: Thu, 26 Sep 2024 23:01:06 +0200 Subject: [PATCH] Feature: Add syslog logger (#1267) This implements RFC5424 version of the protocol. Don't use https://github.com/arcao/Syslog since the protocol itself is trivial and most of the libraries functionality is not needed here. The library also doesn't support setting the PROCID field, which is set to a random id to indicate a reboot here. Add UI for syslog configuration to network admin view. --- include/Configuration.h | 8 ++ include/SyslogLogger.h | 34 +++++++ include/WebApi_errors.h | 2 + include/defaults.h | 3 + src/Configuration.cpp | 10 ++ src/MessageOutput.cpp | 3 + src/NetworkSettings.cpp | 5 + src/SyslogLogger.cpp | 138 ++++++++++++++++++++++++++ src/WebApi_network.cpp | 23 +++++ webapp/src/locales/de.json | 5 +- webapp/src/locales/en.json | 5 +- webapp/src/types/NetworkConfig.ts | 3 + webapp/src/views/NetworkAdminView.vue | 23 +++++ 13 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 include/SyslogLogger.h create mode 100644 src/SyslogLogger.cpp diff --git a/include/Configuration.h b/include/Configuration.h index c8ae55e15..3b99c38bb 100644 --- a/include/Configuration.h +++ b/include/Configuration.h @@ -12,6 +12,8 @@ #define WIFI_MAX_PASSWORD_STRLEN 64 #define WIFI_MAX_HOSTNAME_STRLEN 31 +#define SYSLOG_MAX_HOSTNAME_STRLEN 128 + #define NTP_MAX_SERVER_STRLEN 31 #define NTP_MAX_TIMEZONE_STRLEN 50 #define NTP_MAX_TIMEZONEDESCR_STRLEN 50 @@ -173,6 +175,12 @@ struct CONFIG_T { bool Enabled; } Mdns; + struct { + bool Enabled; + char Hostname[SYSLOG_MAX_HOSTNAME_STRLEN + 1]; + uint16_t Port; + } Syslog; + struct { char Server[NTP_MAX_SERVER_STRLEN + 1]; char Timezone[NTP_MAX_TIMEZONE_STRLEN + 1]; diff --git a/include/SyslogLogger.h b/include/SyslogLogger.h new file mode 100644 index 000000000..a6982e7bd --- /dev/null +++ b/include/SyslogLogger.h @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once +#include +#include +#include + +class SyslogLogger { +public: + SyslogLogger(); + void init(Scheduler& scheduler); + void updateSettings(const String&& hostname); + void write(const uint8_t *buffer, size_t size); + +private: + void loop(); + void disable(); + void enable(); + bool resolveAndStart(); + bool isResolved() const { + return _address != INADDR_NONE; + } + + Task _loopTask; + std::mutex _mutex; + WiFiUDP _udp; + IPAddress _address; + String _syslog_hostname; + String _proc_id; + String _header; + uint16_t _port; + bool _enabled; +}; + +extern SyslogLogger Syslog; diff --git a/include/WebApi_errors.h b/include/WebApi_errors.h index 0da8d3d9f..f9dc0e5b3 100644 --- a/include/WebApi_errors.h +++ b/include/WebApi_errors.h @@ -69,6 +69,8 @@ enum WebApiError { NetworkDns1Invalid, NetworkDns2Invalid, NetworkApTimeoutInvalid, + NetworkSyslogHostnameLength, + NetworkSyslogPort, NtpBase = 9000, NtpServerLength, diff --git a/include/defaults.h b/include/defaults.h index f3482cdb8..67b1d122e 100644 --- a/include/defaults.h +++ b/include/defaults.h @@ -22,6 +22,9 @@ #define MDNS_ENABLED false +#define SYSLOG_ENABLED false +#define SYSLOG_PORT 514 + #define NTP_SERVER_OLD "pool.ntp.org" #define NTP_SERVER "opendtu.pool.ntp.org" #define NTP_TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3" diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 50454594b..61ee7758d 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -123,6 +123,11 @@ bool ConfigurationClass::write() JsonObject mdns = doc["mdns"].to(); mdns["enabled"] = config.Mdns.Enabled; + JsonObject syslog = doc["syslog"].to(); + syslog["enabled"] = config.Syslog.Enabled; + syslog["hostname"] = config.Syslog.Hostname; + syslog["port"] = config.Syslog.Port; + JsonObject ntp = doc["ntp"].to(); ntp["server"] = config.Ntp.Server; ntp["timezone"] = config.Ntp.Timezone; @@ -450,6 +455,11 @@ bool ConfigurationClass::read() JsonObject mdns = doc["mdns"]; config.Mdns.Enabled = mdns["enabled"] | MDNS_ENABLED; + JsonObject syslog = doc["syslog"]; + config.Syslog.Enabled = syslog["enabled"] | SYSLOG_ENABLED; + strlcpy(config.Syslog.Hostname, syslog["hostname"] | "", sizeof(config.Syslog.Hostname)); + config.Syslog.Port = syslog["port"] | SYSLOG_PORT; + JsonObject ntp = doc["ntp"]; strlcpy(config.Ntp.Server, ntp["server"] | NTP_SERVER, sizeof(config.Ntp.Server)); strlcpy(config.Ntp.Timezone, ntp["timezone"] | NTP_TIMEZONE, sizeof(config.Ntp.Timezone)); diff --git a/src/MessageOutput.cpp b/src/MessageOutput.cpp index 04e9ddd44..9db788506 100644 --- a/src/MessageOutput.cpp +++ b/src/MessageOutput.cpp @@ -4,6 +4,7 @@ */ #include #include "MessageOutput.h" +#include "SyslogLogger.h" MessageOutputClass MessageOutput; @@ -102,12 +103,14 @@ void MessageOutputClass::loop() if (!_ws) { while (!_lines.empty()) { + Syslog.write(_lines.front().data(), _lines.front().size()); _lines.pop(); // do not hog memory } return; } while (!_lines.empty() && _ws->availableForWriteAll()) { + Syslog.write(_lines.front().data(), _lines.front().size()); _ws->textAll(std::make_shared(std::move(_lines.front()))); _lines.pop(); } diff --git a/src/NetworkSettings.cpp b/src/NetworkSettings.cpp index c104fca2e..a81b907c3 100644 --- a/src/NetworkSettings.cpp +++ b/src/NetworkSettings.cpp @@ -5,6 +5,7 @@ #include "NetworkSettings.h" #include "Configuration.h" #include "MessageOutput.h" +#include "SyslogLogger.h" #include "PinMapping.h" #include "Utils.h" #include "SPIPortManager.h" @@ -53,6 +54,8 @@ void NetworkSettingsClass::init(Scheduler& scheduler) scheduler.addTask(_loopTask); _loopTask.enable(); + + Syslog.init(scheduler); } void NetworkSettingsClass::NetworkEvent(const WiFiEvent_t event, WiFiEventInfo_t info) @@ -294,6 +297,8 @@ void NetworkSettingsClass::applyConfig() } MessageOutput.println("done"); setStaticIp(); + + Syslog.updateSettings(getHostname()); } void NetworkSettingsClass::setHostname() diff --git a/src/SyslogLogger.cpp b/src/SyslogLogger.cpp new file mode 100644 index 000000000..0c7a8c114 --- /dev/null +++ b/src/SyslogLogger.cpp @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2022-2024 Thomas Basler and others + */ +#include +#include +#include "defaults.h" +#include "SyslogLogger.h" +#include "Configuration.h" +#include "MessageOutput.h" +#include "NetworkSettings.h" + +SyslogLogger::SyslogLogger() + : _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&SyslogLogger::loop, this)) +{ +} + +void SyslogLogger::init(Scheduler& scheduler) +{ + // PROCID change indicates a restart. + _proc_id = String(esp_random(), HEX); + + scheduler.addTask(_loopTask); + _loopTask.enable(); +} + +void SyslogLogger::updateSettings(const String&& hostname) +{ + auto& config = Configuration.get().Syslog; + + // Disable logger while it is reconfigured. + disable(); + + if (!config.Enabled) { + MessageOutput.println("[SyslogLogger] Syslog not enabled"); + return; + } + + _port = config.Port; + _syslog_hostname = config.Hostname; + if (_syslog_hostname.isEmpty()) { + MessageOutput.println("[SyslogLogger] Hostname not configured"); + return; + } + + MessageOutput.printf("[SyslogLogger] Logging to %s!\r\n", _syslog_hostname.c_str()); + + _header = "<14>1 - "; // RFC5424: Facility USER, severity INFO, version 1, NIL timestamp. + _header += hostname; + _header += " OpenDTU "; + _header += _proc_id; + // NIL values for message id and structured data + _header += " - - "; + + // Enable logger. + enable(); +} + +void SyslogLogger::write(const uint8_t *buffer, size_t size) +{ + std::lock_guard lock(_mutex); + if (!_enabled || !isResolved()) { + return; + } + for (int i = 0; i < size; i++) { + uint8_t c = buffer[i]; + bool overflow = false; + if (c != '\r' && c != '\n') { + // Replace control and non-ASCII characters with '?'. + overflow = !_udp.write(c >= 0x20 && c < 0x7f ? c : '?'); + } + if (c == '\n' || overflow) { + _udp.endPacket(); + _udp.beginPacket(_address, _port); + _udp.print(_header); + } + } +} + +void SyslogLogger::disable() +{ + MessageOutput.println("[SyslogLogger] Disable"); + std::lock_guard lock(_mutex); + if (_enabled) { + _enabled = false; + _address = INADDR_NONE; + _udp.stop(); + } +} + +void SyslogLogger::enable() +{ + // Bind random source port. + if (!_udp.begin(0)) { + MessageOutput.println("[SyslogLogger] No sockets available"); + return; + } + + std::lock_guard lock(_mutex); + _enabled = true; +} + +bool SyslogLogger::resolveAndStart() +{ + if (Configuration.get().Mdns.Enabled) { + _address = MDNS.queryHost(_syslog_hostname); // INADDR_NONE if failed + } + if (_address != INADDR_NONE) { + if (!_udp.beginPacket(_address, _port)) { + return false; + } + } else { + if (!_udp.beginPacket(_syslog_hostname.c_str(), _port)) { + return false; + } + _address = _udp.remoteIP(); // Store resolved address. + } + _udp.print(_header); + _udp.print("[SyslogLogger] Logging to "); + _udp.print(_syslog_hostname); + _udp.endPacket(); + _udp.beginPacket(_address, _port); + _udp.print(_header); + return true; +} + +void SyslogLogger::loop() +{ + std::lock_guard lock(_mutex); + if (!_enabled || !NetworkSettings.isConnected() || isResolved()) { + return; + } + if (!resolveAndStart()) { + _enabled = false; + } +} + +SyslogLogger Syslog; diff --git a/src/WebApi_network.cpp b/src/WebApi_network.cpp index 75275755f..98ebe23c9 100644 --- a/src/WebApi_network.cpp +++ b/src/WebApi_network.cpp @@ -70,6 +70,9 @@ void WebApiNetworkClass::onNetworkAdminGet(AsyncWebServerRequest* request) root["password"] = config.WiFi.Password; root["aptimeout"] = config.WiFi.ApTimeout; root["mdnsenabled"] = config.Mdns.Enabled; + root["syslogenabled"] = config.Syslog.Enabled; + root["sysloghostname"] = config.Syslog.Hostname; + root["syslogport"] = config.Syslog.Port; WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); } @@ -163,6 +166,23 @@ void WebApiNetworkClass::onNetworkAdminPost(AsyncWebServerRequest* request) WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); return; } + if (root["syslogenabled"].as()) { + if (root["sysloghostname"].as().length() == 0 || root["sysloghostname"].as().length() > SYSLOG_MAX_HOSTNAME_STRLEN) { + retMsg["message"] = "Syslog Server must between 1 and " STR(SYSLOG_MAX_HOSTNAME_STRLEN) " characters long!"; + retMsg["code"] = WebApiError::NetworkSyslogHostnameLength; + retMsg["param"]["max"] = SYSLOG_MAX_HOSTNAME_STRLEN; + WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); + return; + } + + if (root["syslogport"].as() == 0 || root["syslogport"].as() > 65535) { + retMsg["message"] = "Port must be a number between 1 and 65535!"; + retMsg["code"] = WebApiError::NetworkSyslogPort; + WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); + return; + } + + } CONFIG_T& config = Configuration.get(); config.WiFi.Ip[0] = ipaddress[0]; @@ -195,6 +215,9 @@ void WebApiNetworkClass::onNetworkAdminPost(AsyncWebServerRequest* request) } config.WiFi.ApTimeout = root["aptimeout"].as(); config.Mdns.Enabled = root["mdnsenabled"].as(); + config.Syslog.Enabled = root["syslogenabled"].as(); + strlcpy(config.Syslog.Hostname, root["sysloghostname"].as().c_str(), sizeof(config.Syslog.Hostname)); + config.Syslog.Port = root["syslogport"].as(); WebApi.writeConfig(retMsg); diff --git a/webapp/src/locales/de.json b/webapp/src/locales/de.json index 7b68b28ce..6e38c3e6c 100644 --- a/webapp/src/locales/de.json +++ b/webapp/src/locales/de.json @@ -506,7 +506,10 @@ "ApTimeoutHint": "Zeit die der AccessPoint offen gehalten wird. Ein Wert von 0 bedeutet unendlich.", "Minutes": "Minuten", "EnableMdns": "mDNS aktivieren", - "MdnsSettings": "mDNS-Einstellungen" + "MdnsSettings": "mDNS-Einstellungen", + "EnableSyslog": "Syslog aktivieren", + "SyslogSettings": "Syslog-Einstellungen", + "Port": "Port:" }, "mqttadmin": { "MqttSettings": "MQTT-Einstellungen", diff --git a/webapp/src/locales/en.json b/webapp/src/locales/en.json index 2602bba6e..06d03ad89 100644 --- a/webapp/src/locales/en.json +++ b/webapp/src/locales/en.json @@ -508,7 +508,10 @@ "ApTimeoutHint": "Time which the AccessPoint is kept open. A value of 0 means infinite.", "Minutes": "minutes", "EnableMdns": "Enable mDNS", - "MdnsSettings": "mDNS Settings" + "MdnsSettings": "mDNS Settings", + "EnableSyslog": "Enable Syslog", + "SyslogSettings": "Syslog Settings", + "Port": "Port:" }, "mqttadmin": { "MqttSettings": "MQTT Settings", diff --git a/webapp/src/types/NetworkConfig.ts b/webapp/src/types/NetworkConfig.ts index da5ddd44b..b71c6a382 100644 --- a/webapp/src/types/NetworkConfig.ts +++ b/webapp/src/types/NetworkConfig.ts @@ -10,4 +10,7 @@ export interface NetworkConfig { dns2: string; aptimeout: number; mdnsenabled: boolean; + syslogenabled: boolean; + sysloghostname: string; + syslogport: number; } diff --git a/webapp/src/views/NetworkAdminView.vue b/webapp/src/views/NetworkAdminView.vue index aeabec085..8c3da6977 100644 --- a/webapp/src/views/NetworkAdminView.vue +++ b/webapp/src/views/NetworkAdminView.vue @@ -82,6 +82,29 @@ /> + + + + + + + +