Systemctl : 2 instances avec les logs séparés

janvier 5, 2022

Et nous continuons nos modifications concernant weewx sur une Debian et le systemctl

Jusqu’à maintenant , tous les logs sont soit redirigés dans messages soit dans un log sous /var/log weewx.log

 je voulais justement séparé tout ca  : je suis donc reparti de cet article : https://meteo-pontscorff.fr/utilisation-de-weewx-en-1-seule-instance-pour-2-stations/

Mais c’etait un mélange se sytem V et de systemCTL

Alors voici  la version « tout » systemCTL

Pré-requis  : Avoir ses 2 instances Weewx operationnelles

 je suis parti de cette documentation très bien faite :  https://github.com/weewx/weewx/wiki/logging

Modification de chaque instance weewx  

Editer ce fichier  /etc/systemd/system/wx-WMR200.service

[Unit]
Description=weewx WMR200 weather system
Requires=time-sync.target
After=time-sync.target
RequiresMountsFor=/home
# The following two lines are not in the current distribution version, but should be uncommented and used if you
# have changed the [Service] section to automatically restart the weewx service if it crashes. As noted below,
# this can be particularly useful if weewx has an IP connection to the weather station it is monitoring, since
# transient WiFi network problems are quite common, and may cause weewx to crash.
# StartLimitIntervalSec=100
# StartLimitBurst=5

[Service]
ExecStart=/usr/bin/weewxd --daemon --log-label wx-WMR200 --pidfile=/var/run/wx-WMR200.pid /etc/weewx/wx-WMR200.conf
ExecReload=/bin/kill -HUP $MAINPID
Type=forking
PIDFile=/var/run/wx-WMR200.pid

# The following two lines are not in the current distribution version, but may be uncommented and used if you
# want the weewx service to automatically restart if it crashes. This can be particularly useful if weewx
# has an IP connection to the weather station it is monitoring, since transient network problems are quite
# common, and may cause the daemon to crash.
Restart=on-failure
RestartSec=10

# See notes; by default weewx will run with root privileges
#User=weewx
#Group=weewx

[Install]
WantedBy=multi-user.target
ExecStart=/usr/bin/weewxd --daemon --log-label wx-WMR200 --pidfile=/var/run/wx-WMR200.pid /etc/weewx/wx-WMR200.conf

il faut en effet rajouter la partie log-label qui permettra de séparer les logs.

une fois la modification dans les 2 fichiers de service effectuée , il faudra :

systemctl stop wx-WMR200.service
systemctl disable wx-WMR200.service
systemctl enable wx-WMR200.service
systemctl start wx-WMR200.service

Ensuite, il faut modifier le syslog :
Sous /etc/rsyslog.d/weewx.conf


# use this syntax for rsyslog in most modern distributions
#:programname,isequal,"weewx" /var/log/weewx/weewx.log
#:programname,isequal,"weewx" stop
:programname,startswith,"wee_" /var/log/weewx/weewx.log
:programname,startswith,"wee_" stop

# older versions of rsyslog use this syntax
#:programname,isequal,"weewx" /var/log/weewx/weewx.log
#:programname,isequal,"weewx" ~
#:programname,startswith,"wee_" /var/log/weewx/weewx.log
#:programname,startswith,"wee_" ~

# some prefer this dialect of rsyslog
#if $programname == 'weewx' then /var/log/weewx/weewx.log
#if $programname == 'weewx' then ~

:programname,isequal,"wx-WMR200" /var/log/weewx/WMR200.log
:programname,isequal,"wx-WMR200" stop
:programname,isequal,"wx-WMR200" ~

:programname,isequal,"wx-FROGGIT" /var/log/weewx/FROGGIT.log
:programname,isequal,"wx-FROGGIT" stop
:programname,isequal,"wx-FROGGIT" ~

Ne pas oublier de creer le dossier weewx sous /var/log/

Une fois effectué
Redemarrer le service Rsyslog

systemctl stop rsyslog.service
systemctl start rsyslog.service

Il faudra pour faire « propre » , un rotate des logs

Pour ca il faut modifier sous /etc/logrotated.d/weewx

/var/log/weewx/*.log {
  daily
  missingok
  rotate 15
  compress
  delaycompress
  notifempty
}

et redemarrer le Syslog

systemctl stop rsyslog.service
systemctl start rsyslog.service

Ne pas oublier de faire un chown sur le log comme ceci :

# chown syslog.adm "nom_du_fichier.log"