Logging to a remote syslog server

How do I setup Vera V1/V2 to log to a remote [tt]syslog[/tt] server?

Some ideas:

[ul][li]Create [tt]/etc/syslog.conf[/tt].[/li]

[li][tt]uci set system.@system[0].log_ip=192.168.1.1
uci commit[/tt][/li]

[li]Modify [tt]/etc/config/system[/tt].[/li]

[li]Create [tt]/etc/rsyslogd.d/openwrt.conf[/tt].[/li][/ul]

I’m using a remote [tt]syslog-ng[/tt] server on a port ~= 514 and would like to use [tt]logger[/tt] to log wattage to a remote server. The solution must not affect the standard logging to [tt]/etc/var/cmh[/tt].

This is how you send syslog to a remote server on OpenWrt. But that’ll only send the system log, and I sense you are actually referring to the Luup log produced by MiOS.

First get remote syslog to work for you. I think it works only on TCP, not UDP, so configure the syslog receiver accordingly.

Then try to copy the Luup log over to syslog, by running something like:

tail -f /var/log/cmh/LuaUPnP.log | logger

Prove that works and we can work at automating it.

Solution for Vera V2 and Vera V3 (see below for V1):

Please note the disclaimer:

  *** Any changes made to the system without ***
  *** guidance from MiOS support will VOID   ***
  *** your future Support requests           ***

Old [tt]/etc/config/system:[/tt]

config 'system'
        option 'timezone' 'PST8PDT,M3.2.0,M11.1.0'
        option 'hostname' 'MiOS_XXXXX'

config 'rdate'
        option 'interface' 'wan'

Now let’s define the remote logging server:

uci set system.@system[0].log_ip=192.168.x.y
uci set system.@system[0].log_port=12345
uci commit

New [tt]/etc/config/system:[/tt]

config 'system'
        option 'timezone' 'PST8PDT,M3.2.0,M11.1.0'
        option 'hostname' 'MiOS_XXXXX'
        option 'log_ip' '192.168.x.y'
        option 'log_port' '12345'

config 'rdate'
        option 'interface' 'wan'

Reboot your Vera.

Test the setup:

logger 'message from Vera'

Result on remote server:

Jul 26 19:39:36 192.168.a.b root: message from Vera

Plugin example:
[tt]http://forum.micasaverde.com/index.php?topic=6752.msg46270#msg46270[/tt]

Remote syslog server used:
[tt]http://forum.synology.com/wiki/index.php/How_to_make_your_NAS_a_SYSLOG_Server[/tt]

Food for your syslog server:
[tt]http://forum.micasaverde.com/index.php?topic=7305.0[/tt]

Solution for Vera V1 (1.1.1245):

Modify [tt]/etc/config/syslog[/tt] manually and reboot.

Solution for Vera V2 ([tt]http://forum.micasaverde.com/index.php/topic,7288.msg46266.html#msg46266[/tt]) works without modifications on Vera V3 (VeraLite@1.5.276).

I was able to stream the contents of LuaUPnP.log to a syslog server using netcat.

For example:
tail -f /var/log/cmh/LuaUPnP.log | nc -u 192.168.0.100 514

[quote=“hazedav, post:7, topic:168780”]I was able to stream the contents of LuaUPnP.log to a syslog server using netcat.

For example:
tail -f /var/log/cmh/LuaUPnP.log | nc -u 192.168.0.100 514[/quote]

Is that working well for you? Do you just do that for short periods or did you put something in place to do that continuously? I used the while true do tail luup | logger trick in putty but it only does that while I am in SSH.

I am currently running an rsyslog server to which my vera 3 is successfully sending its syslogs, as detailed here http://forum.micasaverde.com/index.ph/topic,7288.msg46266.html#msg46266.

I then use the following to forward the LuaUPnP log to the local syslog:

tail -f /tmp/log/cmh/LuaUPnP.log | awk '{print $1 " " $2 " " $3 " - [LuaUPnP.log] - " substr($0, index($0,$4)) }' | logger &
and now it’s in the local syslog it is transferred on to the remote syslog server.

The ‘&’ at the end of the line allows the process to continue running in the background even when the calling terminal is closed (in this case).

This however will not survive a reboot.

Does anyone know how to add this to the config either to send the luaUPnP log directly to a syslog server or to do the above on a more permanent basis?

It probably will not survive a log rotation either.

Could this be called from luup using os execute?

you can because Vera is running as root.
However you should wrap it in a shell script that kills itself if it’s already running … Then launch the command in a loop so it will survive the log rotation.

the reason for killing itself is for when Vera is reloaded, not for when it reboots.

So here’s my script that’s nearly there, I need it to survive a reboot next…

#!/usr/bin/lua

require "socket"

function sleep(sec)
    socket.select(nil, nil, sec)
end

while true do
	os.execute("[[ ! `ps -ef | grep 'tail -F /tmp/log/cmh/LuaUPnP.log' | grep -v grep` ]] && tail -F /tmp/log/cmh/LuaUPnP.log | awk '{print \"- [LuaUPnP.log - \" $2 \" \" $3 \" ] - \" substr($0, index($0,$4)) }' | logger >/dev/null 2>&1 & ")
	sleep(15)
end

Try something like:

#!/bin/sh
MPID=$$
PID=$(cat /tmp/syslog_copy.pid)
if [ $PID -ne $MPID ]; then 
  for cpid in $(ps -o pid --ppid $PID); do
    kill $cpid
  done
  kill $PID
fi  
echo $MPID > /tmp/syslog_copy.pid
while true; do
  tail -f /tmp/log/cmh/LuaUPnP.log
done

Replace the tail command with your actual command.
This kills processes and sub processes from a previously run command … if it exists.
The reason for testing if the last process is equal to the current process … is that on a quick reboot … the process ID can be the same as the processed ID from the previous boot. On a Vera Restart/Reload they will never be the same.

So is this safe to try?

LuaLogger.sh

#!/bin/sh MPID=$$ PID=$(cat /tmp/syslog_copy.pid) if [ $PID -ne $MPID ]; then for cpid in $(ps -o pid --ppid $PID); do kill $cpid done kill $PID fi echo $MPID > /tmp/syslog_copy.pid while true; do os.execute("[[ ! `ps -ef | grep 'tail -F /tmp/log/cmh/LuaUPnP.log' | grep -v grep` ]] && tail -F /tmp/log/cmh/LuaUPnP.log | awk '{print \"- [LuaUPnP.log - \" $2 \" \" $3 \" ] - \" substr($0, index($0,$4)) }' | logger >/dev/null 2>&1 & ") sleep(15) done

Placing the file in /etc and setting permissions:

chmod +x LuaLogger.sh

And then add to the startup lua:

os.execute("/etc/LuaLogger.sh")

I’m a little green at this, so please excuse me if it’s not wise to put this in the startup lua or if I have something in the wrong place here. I already have my syslog going to a Synology so this would make it easy for me to monitor for failures of devices and plugins.

os.execute will NOT work inside the sh script.

Thank you sir, I should have noticed that. I will edit that and test in ssh before calling it from luup.

In case someone else is interested…

add following script to your vera 7.0.x by ( ssh <vera_ip>, then copy paste in the terminal )

cat << 'EOF' > /etc/LuaLogger.sh
#!/bin/sh
MPID=$$
PID=$(cat /tmp/syslog_copy.pid)
if [ $PID -ne $MPID ]; then
  for cpid in $(ps -o pid --ppid $PID); do
    kill $cpid
  done
  kill $PID
fi
echo $MPID > /tmp/syslog_copy.pid
while true; do
  tail -f /tmp/log/cmh/LuaUPnP.log | awk '{print "["$2" "$3"] " substr($0, index($0,$4)) }' | logger -t LuaUPnP >/dev/null 2>&1
done
EOF

Edit your lua startup code and adjust the variables as needed:

-- define syslog remote ip and port
syslog_ip="1.2.3.4"
syslog_port="514"
-- do not edit below unless you know what you are doing
os.execute("uci set system.@system[0].log_ip=" .. syslog_ip)
os.execute("uci set system.@system[0].log_port=" .. syslog_port)
os.execute("uci commit system")
os.execute("/etc/init.d/log restart")
os.execute("test -x /etc/LuaLogger.sh && /etc/LuaLogger.sh &")

/Alex

© 2019 Vera Control Ltd., All Rights Reserved. Terms of Use | Privacy Policy