Skip to content

Instantly share code, notes, and snippets.

@mbernson
Created December 2, 2015 11:36
Show Gist options
  • Save mbernson/78e53b26d3c68f997493 to your computer and use it in GitHub Desktop.
Save mbernson/78e53b26d3c68f997493 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-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
#!/bin/bash
while read line; do
echo "$line";
mosquitto_pub -h 192.168.0.2 -t home/router -m "$line";
done
@version:3.0
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");
};
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));
};
# 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 /root/mqtt_publish.sh" template("$MESSAGE\n") );
};
# Log to /var/log/messages
log {
source(src);
source(net);
source(kernel);
destination(messages);
};
# Log to MQTT
log {
source(src);
source(net);
source(kernel);
# Apply any filters here
# filter(f_associated);
destination(d_mqtt);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment