Syslog host setting as ipv4 or hostname

This commit is contained in:
MichaelDvP
2021-07-26 11:41:23 +02:00
parent 1df427366f
commit 1cf938e16a
6 changed files with 47 additions and 49 deletions

View File

@@ -32,7 +32,7 @@ import {
BlockFormControlLabel BlockFormControlLabel
} from '../components'; } from '../components';
import { isIP, optional } from '../validators'; import { isIPv4, optional, isHostname, or } from '../validators';
import { EMSESPSettings } from './EMSESPtypes'; import { EMSESPSettings } from './EMSESPtypes';
@@ -55,7 +55,10 @@ class EMSESPSettingsForm extends Component<EMSESPSettingsFormProps> {
}; };
componentDidMount() { componentDidMount() {
ValidatorForm.addValidationRule('isOptionalIP', optional(isIP)); ValidatorForm.addValidationRule(
'isOptionalIPorHost',
optional(or(isIPv4, isHostname))
);
} }
changeBoardProfile = (event: React.ChangeEvent<HTMLSelectElement>) => { changeBoardProfile = (event: React.ChangeEvent<HTMLSelectElement>) => {
@@ -538,10 +541,10 @@ class EMSESPSettingsForm extends Component<EMSESPSettingsFormProps> {
> >
<Grid item xs={5}> <Grid item xs={5}>
<TextValidator <TextValidator
validators={['isOptionalIP']} validators={['isOptionalIPorHost']}
errorMessages={['Not a valid IP address']} errorMessages={['Not a valid IPv4 address or hostname']}
name="syslog_host" name="syslog_host"
label="IP" label="Host"
fullWidth fullWidth
variant="outlined" variant="outlined"
value={data.syslog_host} value={data.syslog_host}

View File

@@ -3,3 +3,4 @@ export { default as isIP } from './isIP';
export { default as optional } from './optional'; export { default as optional } from './optional';
export { default as or } from './or'; export { default as or } from './or';
export { default as isPath } from './isPath'; export { default as isPath } from './isPath';
export { default as isIPv4 } from './isIPv4';

View File

@@ -0,0 +1,5 @@
const ipv4AddressRegexp = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
export default function isIpv4(ipAddress: string) {
return ipv4AddressRegexp.test(ipAddress);
}

View File

@@ -159,45 +159,37 @@ void System::format(uuid::console::Shell & shell) {
} }
void System::syslog_start() { void System::syslog_start() {
bool was_enabled = syslog_enabled_;
EMSESP::webSettingsService.read([&](WebSettings & settings) {
syslog_enabled_ = settings.syslog_enabled;
syslog_level_ = settings.syslog_level;
syslog_mark_interval_ = settings.syslog_mark_interval;
syslog_host_ = settings.syslog_host;
syslog_port_ = settings.syslog_port;
});
#ifndef EMSESP_STANDALONE
if (syslog_enabled_) { if (syslog_enabled_) {
#ifndef EMSESP_STANDALONE // start & configure syslog
syslog_.start(); if (!was_enabled) {
syslog_.start();
EMSESP::logger().info(F("Starting Syslog"));
}
syslog_.log_level((uuid::log::Level)syslog_level_); syslog_.log_level((uuid::log::Level)syslog_level_);
#endif syslog_.mark_interval(syslog_mark_interval_);
EMSESP::logger().info(F("Starting Syslog")); syslog_.destination(syslog_host_.c_str(), syslog_port_);
} syslog_.hostname(hostname().c_str());
} } else if (was_enabled) {
// in case service is still running, this flushes the queue
void System::syslog_init(bool refresh) { // https://github.com/emsesp/EMS-ESP/issues/496
if (refresh) { EMSESP::logger().info(F("Stopping Syslog"));
get_settings();
}
#ifndef EMSESP_STANDALONE
// check for empty or invalid hostname
IPAddress addr;
if (!addr.fromString(syslog_host_.c_str())) {
syslog_enabled_ = false;
}
// in case service is still running, this flushes the queue
// https://github.com/emsesp/EMS-ESP/issues/496
if (!syslog_enabled_) {
syslog_.log_level((uuid::log::Level)-1); syslog_.log_level((uuid::log::Level)-1);
syslog_.mark_interval(0); syslog_.mark_interval(0);
syslog_.destination((IPAddress)((uint32_t)0)); syslog_.destination("");
return;
} }
// start & configure syslog
syslog_.log_level((uuid::log::Level)syslog_level_);
syslog_.mark_interval(syslog_mark_interval_);
syslog_.destination(addr, syslog_port_);
syslog_.hostname(hostname().c_str());
#endif #endif
} }
// read all the settings from the config files and store locally // read all the settings except syslog from the config files and store locally
void System::get_settings() { void System::get_settings() {
EMSESP::webSettingsService.read([&](WebSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
// Button // Button
@@ -209,13 +201,6 @@ void System::get_settings() {
// Sysclock // Sysclock
low_clock_ = settings.low_clock; low_clock_ = settings.low_clock;
// Syslog
syslog_enabled_ = settings.syslog_enabled;
syslog_level_ = settings.syslog_level;
syslog_mark_interval_ = settings.syslog_mark_interval;
syslog_host_ = settings.syslog_host;
syslog_port_ = settings.syslog_port;
// LED // LED
hide_led_ = settings.hide_led; hide_led_ = settings.hide_led;
led_gpio_ = settings.led_gpio; led_gpio_ = settings.led_gpio;
@@ -296,7 +281,7 @@ void System::start(uint32_t heap_start) {
adc_init(false); // analog ADC adc_init(false); // analog ADC
button_init(false); // the special button button_init(false); // the special button
network_init(false); // network network_init(false); // network
syslog_init(false); // init SysLog syslog_start(); // start Syslog
EMSESP::init_uart(); // start UART EMSESP::init_uart(); // start UART
} }
@@ -762,15 +747,17 @@ void System::show_system(uuid::console::Shell & shell) {
if (!syslog_enabled_) { if (!syslog_enabled_) {
shell.printfln(F("Syslog: disabled")); shell.printfln(F("Syslog: disabled"));
} else { } else {
shell.printfln(F("Syslog:")); shell.printfln(F("Syslog: %s"),syslog_.started() ? "started" : "stopped");
shell.print(F(" ")); shell.print(F(" "));
shell.printfln(F_(host_fmt), !syslog_host_.isEmpty() ? syslog_host_.c_str() : uuid::read_flash_string(F_(unset)).c_str()); shell.printfln(F_(host_fmt), !syslog_host_.isEmpty() ? syslog_host_.c_str() : uuid::read_flash_string(F_(unset)).c_str());
shell.printfln(F(" IP: %s"),uuid::printable_to_string(syslog_.ip()).c_str());
shell.print(F(" ")); shell.print(F(" "));
shell.printfln(F_(port_fmt), syslog_port_); shell.printfln(F_(port_fmt), syslog_port_);
shell.print(F(" ")); shell.print(F(" "));
shell.printfln(F_(log_level_fmt), uuid::log::format_level_lowercase(static_cast<uuid::log::Level>(syslog_level_))); shell.printfln(F_(log_level_fmt), uuid::log::format_level_lowercase(static_cast<uuid::log::Level>(syslog_level_)));
shell.print(F(" ")); shell.print(F(" "));
shell.printfln(F_(mark_interval_fmt), syslog_mark_interval_); shell.printfln(F_(mark_interval_fmt), syslog_mark_interval_);
shell.printfln(F(" Queued: %d"),syslog_.queued());
} }
#endif #endif
@@ -939,6 +926,10 @@ bool System::command_info(const char * value, const int8_t id, JsonObject & json
node["dallas reads"] = EMSESP::sensor_reads(); node["dallas reads"] = EMSESP::sensor_reads();
node["dallas fails"] = EMSESP::sensor_fails(); node["dallas fails"] = EMSESP::sensor_fails();
} }
if (EMSESP::system_.syslog_enabled_) {
node["syslog_ip"] = syslog_.ip();
node["syslog_started"] = syslog_.started();
}
} }
JsonArray devices2 = json.createNestedArray("Devices"); JsonArray devices2 = json.createNestedArray("Devices");

View File

@@ -74,7 +74,6 @@ class System {
void send_heartbeat(); void send_heartbeat();
void led_init(bool refresh); void led_init(bool refresh);
void syslog_init(bool refresh);
void adc_init(bool refresh); void adc_init(bool refresh);
void network_init(bool refresh); void network_init(bool refresh);
void button_init(bool refresh); void button_init(bool refresh);
@@ -168,7 +167,7 @@ class System {
std::string hostname_ = "ems-esp"; std::string hostname_ = "ems-esp";
bool hide_led_; bool hide_led_;
uint8_t led_gpio_; uint8_t led_gpio_;
bool syslog_enabled_; bool syslog_enabled_ = false;
bool analog_enabled_; bool analog_enabled_;
bool low_clock_; bool low_clock_;
String board_profile_; String board_profile_;

View File

@@ -231,8 +231,7 @@ void WebSettingsService::onUpdate() {
} }
if (WebSettings::has_flags(WebSettings::ChangeFlags::SYSLOG)) { if (WebSettings::has_flags(WebSettings::ChangeFlags::SYSLOG)) {
EMSESP::system_.syslog_init(true); // reload settings EMSESP::system_.syslog_start(); // re-start (or stop)
EMSESP::system_.syslog_start(); // re-start (or stop)
} }
if (WebSettings::has_flags(WebSettings::ChangeFlags::ADC)) { if (WebSettings::has_flags(WebSettings::ChangeFlags::ADC)) {