Skip to content

Instantly share code, notes, and snippets.

@arvati
Forked from mbernson/README.md
Last active February 26, 2022 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arvati/1e90caa751bb338a07106566c2cb5117 to your computer and use it in GitHub Desktop.
Save arvati/1e90caa751bb338a07106566c2cb5117 to your computer and use it in GitHub Desktop.
Publish openwrt log contents to MQTT

Router logging to MQTT

This configuration sends all (or part of) your OpenWRT logs to a MQTT broker. I'm writing it up here for future reference.

I made this so I can keep track of wireless clients as they associate and disassociate with my home network. This way, my home automation setup can make decisions based on that information. :)

Installation

# We need syslog-ng first
opkg update
opkg install syslog-ng # new package = opkg install syslog-ng3

# Edit your configuration according to the other files in this Gist...

# Disable the regular logger
/etc/init.d/log disable
/etc/init.d/log stop

# Enable syslog-ng on boot and start it
/etc/init.d/syslog-ng enable
/etc/init.d/syslog-ng start
nano /usr/bin/log_mqtt_publish
chmod +x /usr/bin/log_mqtt_publish

nano /etc/syslog-ng.conf
nano /etc/syslog-ng.d/mosquitto.conf
/etc/init.d/syslog-ng restart
#!/bin/sh
#/usr/bin/log_mqtt_publish
# OPENWRT mosquitto
thishostname="`uci get system.@system[0].hostname`"
thisdomain="`uci get dhcp.@dnsmasq[0].domain`"
while read line; do
#echo "$line";
mosquitto_pub -h localhost -p 1883 -t "openwrt/$thisdomain/$thishostname/syslog" -m "${line}" -q 1
done
@version:3.9
#/etc/syslog-ng.d/mosquitto.conf
# Define some filters if you want to (untested)
filter f_associated {
match(".*IEEE 802.11.*", value("MESSAGE"));
};
destination d_mqtt {
# The first argument is a program (or script) that gets spawned when syslog-ng starts, and keeps running until it stops again.
# The log messages are sent to stdin on that program.
program("ash /usr/bin/log_mqtt_publish" template("${MESSAGE}\n") );
};
# Log to MQTT
log {
source(src);
source(net);
source(kernel);
# Apply any filters here
# filter(f_associated);
destination(d_mqtt);
};
@version:3.9
options {
chain_hostnames(no);
create_dirs(yes);
flush_lines(0);
keep_hostname(yes);
log_fifo_size(256);
log_msg_size(1024);
stats_freq(0);
flush_lines(0);
use_fqdn(no);
};
source src {
internal();
#unix-stream("/dev/log");
unix-dgram("/dev/log");
};
source net {
udp(ip(0.0.0.0) port(514));
};
source kernel {
file("/proc/kmsg" program_override("kernel"));
};
destination messages {
#file("/var/log/messages" log_fifo_size(256));
file("/var/log/messages");
};
# Log to /var/log/messages
log {
source(src);
source(net);
source(kernel);
destination(messages);
};
# put any customization files in this directory
@include "/etc/syslog-ng.d/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment