-
Notifications
You must be signed in to change notification settings - Fork 85
Description
I'm sure this has come up before. There is a disconnect/conflict between the self rotation of logs by emonhub and the logrotate schemes. There is also inconsistency in log file creation & permissions depending on where it is done.
Lines 321 to 323 in 1215a9f
| loghandler = logging.handlers.RotatingFileHandler(args.logfile.name, | |
| maxBytes=5000 * 1024, | |
| backupCount=1) |
https://github.com/openenergymonitor/EmonScripts/blob/master/defaults/etc/logrotate.d/emonhub
Add to this the service files and logging and the install scripts for emonhub and EmonScripts and emonhub logging is all a bit messed up.
Lines 103 to 110 in 4539b9e
| # Fix emonhub log file permissions | |
| if [ -d /var/log/emonhub ]; then | |
| echo "Setting ownership of /var/log/emonhub to $user" | |
| sudo chown $user:root /var/log/emonhub | |
| fi | |
| if [ -f /var/log/emonhub/emonhub.log ]; then | |
| echo "Setting ownership of /var/log/emonhub/emonhub.log to $user and permissions to 644" |
emonhub/service/emonhub.service
Lines 8 to 16 in 4539b9e
| [Service] | |
| Type=exec | |
| ExecStart=/usr/local/bin/emonhub/emonhub.py --config-file=/etc/emonhub/emonhub.conf --logfile=/var/log/emonhub/emonhub.log | |
| User=pi | |
| Environment='USER=pi' | |
| Environment='LOG_PATH=/var/log/emonhub' | |
| PermissionsStartOnly=true | |
| ExecStartPre=/bin/mkdir -p ${LOG_PATH} | |
| ExecStartPre=/bin/chown ${USER} ${LOG_PATH} |
There was some discussion here openenergymonitor/emonpi#103
Where the internal python rotation happens, the subsequent rotated log files will not be rotated by logrotate. You will end up with a discontinuous log file (which would not help debugging).
As set currently, if the logfile happens to be between 3M and 5M in size when logrotate runs, the file will be rotated and only in this circumstance.
I think there should be a systematic review of the logging for emonhub.
My suggestion
If there is a no log file specified in the emonhub.service file, emonhub logging reverts to stderr, which ends up in rsyslog.log.
This logging can then be directed to a log file using a file /etc/rsyslog.d/10-emonhub.conf. [edit] - updated to latest format.
# https://www.rsyslog.com/doc/v8-stable/configuration/templates.html
template(name="EmonhubMsg" type="list") {
property(name="hostname")
constant(value=" ")
property(name="syslogtag")
property(name="msg" spifno1stsp="on" )
property(name="msg" droplastlf="on" )
constant(value="\n")
}
if ( $programname == 'emonhub' or $programname == 'emonhub.py' ) then {
action(type="omfile" file="/var/log/emonhub/emonhub.log" template="EmonhubMsg" FileCreateMode="0644" dirCreateMode="0744")
stop
}
This would then be correctly rotated by logrotate.
However, with the current logrotate settings mean that logrotate only runs daily, but the rotation settings mean the log is rotated weekly (unless too big).
Moving to hourly rotation can be solved by a logrotate drop-in to change the timer to hourly with minimal impact on resources and then rotate once the size is greater than 5M.
/lib/systemd/system/logrotate.timer.d/00_emonhub.conf
[Timer]
OnCalendar=
OnCalendar=hourly
Finally, the logrotate config needs amending slightly. Assuming logrotate is run hourly, the file will rotate when greater than 5M (but no earlier), but at least 3M, keep 14 files, compress on next rotation.
/var/log/emonhub/emonhub.log {
size 5M
minsize 3M
maxsize 5M
rotate 14
compress
delaycompress
olddir /var/log.old/emonhub
createolddir 775 root root
renamecopy
}
Service file would therefore be simplified as well.
[Unit]
Description=emonHub data multiplexer
# The config file lives in /etc/emonhub/emonhub.conf
# The log file lives in /var/log/emonhub/emonhub.log
Requires=var-log.mount
After=var-log.mount network.target
[Service]
Type=exec
ExecStart=/usr/local/bin/emonhub/emonhub.py --config-file=/etc/emonhub/emonhub.conf
User=pi
Restart=always
RestartSec=5
SyslogIdentifier=emonhub
[Install]
WantedBy=multi-user.target
Currently running this on a NOV22 EmonPi.
** Install and update scripts need considering ** Probably needs removing.
Lines 103 to 115 in 4539b9e
| # Fix emonhub log file permissions | |
| if [ -d /var/log/emonhub ]; then | |
| echo "Setting ownership of /var/log/emonhub to $user" | |
| sudo chown $user:root /var/log/emonhub | |
| fi | |
| if [ -f /var/log/emonhub/emonhub.log ]; then | |
| echo "Setting ownership of /var/log/emonhub/emonhub.log to $user and permissions to 644" | |
| sudo chown $user:$user /var/log/emonhub/emonhub.log | |
| sudo chmod 644 /var/log/emonhub/emonhub.log | |
| fi | |