EmonHub log systemd

Continuing the discussion from KwH per day:

There have been several reports that the EmonHub log seems to have gone AWOL (within the emonhub page) on 9.9.6.

You can get it via journalctl

@TrystanLea, @glyn.hudson could you suggest what has happened to it?

1 Like

I can confirm it is not present. I will look into it.

journalctl -f -u emonhub -n 1000

does work for those needing it in the meantime

1 Like

It looks like this is as a result of moving from /etc/init.d/ service to the systemd service.
I have tried adding a standard output definition but cant seem the get it to log:

StandardOutput=file:/var/log/emonhub.log

Any ideas? @Greebo

We are considering here accessing the log directly from the system log rather than having an emonhub.log

Should possibly be append: (systemd.exec) but only available from systemd 236 or newer apparently (linux - How to redirect output of systemd service to a file - Stack Overflow)

An alternative might be to use rsyslog so it is available via journal and via a file - this answer as well. (linux - How to redirect output of systemd service to a file - Stack Overflow)

1 Like

We’ve got it to work with journalctl. www-data needs to be added to the systemd-journal group:

usermod -a -G systemd-journal www-data

and then the getemonhublog route config/config_controller.php at master · emoncms/config · GitHub changed to:

if ($route->action == 'getemonhublog') {
    $route->format = "text";
    ob_start();
    passthru("journalctl -u emonhub -n 30 --no-pager");
    return trim(ob_get_clean());
}

we are putting this together into an update now.

1 Like

The only downside to this, is that it will be more difficult to extract errors further back or over a longer period.

The changes to emoncms/config mentioned by @TrystanLea are now committed in the stable branch of this module.

However I’m having an issue adding www-data to the systemd-journal via the update scripts.

Executing in a shell

sudo usermod -a -G systemd-journal www-data

works fine but when it’s in the emonhub update script it does’t seem to work, just failes with no error. I think this could be a permission issue, however all other sudo commands e.g sudo apt-get update work fine via the script. Any ideas?

We have also fixed the emonHub restart button to restart the systemd service.

The only thing remaining (apart from fixing the group add issue mentioned above) is to fix the emonHub log Download button. This is currently broken. I propose when a user clicks the download button the journal log is saved to a tmp file which is then downloaded via the browser e.g

journalctl -u emonhub --no-pager > ~/tmp/emonhub.log
rm /tmp/emonhub.log

Have you tried a reboot? permissions - usermod -a -G group user not work - Unix & Linux Stack Exchange

@borpin nailed it… needs a newer version of systemd… I ran into the same issue with the service-runner unit file trying to log to its own log file. (I was sure I’d posted about it somewhere either here or in the discussions about the service-runner PR but I can’t find it now!)

[EDIT]
Debian stretch includes v232, buster includes v240… so the StandardOutput feature will become available when we switch to buster (or later) ref

The stretch-backports version is v239, so that would also provide the feature without having to upgrade from stretch. This may be most appropriate for the emonSD image?

1 Like

A reboot is not required, just a restart of apache2 is fine to apply the group changes. The issue I’m having is I can run the usermod command via sudo in a shell window no problem. However, to be able to roll this out to all emonPi users we need to be able to do it via the emonpi update script. For some reason (I’m not sure why) usemode command does not seem to do anything when ran from a script. Maybe it’s a permission issue? But as I mentioned other sudo commands work fine from the update script.

There are some comments on those links suggesting that you need to log out/login to make the change stick. I wondered if a reboot might do the same.

I think it is to do with a vagary around the user running the script.

Could the command be run as a different user?

Ok, this issue has now been fixed. If a users runs emonPi update the emonhub log should start working again. Please let me know if it does. It may require a reboot for changes to take effect.

You’ve got to tell me how!

Most of the changes are in this commit

passthru("journalctl -u emonhub -n 30 --no-pager");

We just needed to add the www-data users to the systemd-journal group to allow it to read, this has been added to emonPi update and the emonhub config module readme

sudo usermod -a -G systemd-journal www-data

We still need to fix the log download button.

1 Like

@TrystanLea @glyn.hudson I have just submitted a PR to fix the emonhub log download issue. Just used your same passthru solution without the number :smile:

https://github.com/emoncms/config/pull/11

1 Like

Thanks @borpin, merged

1 Like