« Admin/Systemd/Journalctl » : différence entre les versions

De TartareFR
Aller à la navigation Aller à la recherche
(Page créée avec « == Introduction == Depuis Fedora 18, systemd est pleinement implémenté, c'est à dire que la commande pour visualiser les journaux systèmes est... »)
 
Ligne 7 : Ligne 7 :


Cette commande lancée avec le super-utilisateur root, permet de voir tous les logs générés par le système et par les utilisateurs de celui-ci. A contrario, lancée avec un utilisateur basique, seuls les logs lui appartenant seront visibles. C'est une amélioration du traditionnel format de <path>/var/log/messages</path>:
Cette commande lancée avec le super-utilisateur root, permet de voir tous les logs générés par le système et par les utilisateurs de celui-ci. A contrario, lancée avec un utilisateur basique, seuls les logs lui appartenant seront visibles. C'est une amélioration du traditionnel format de <path>/var/log/messages</path>:
* Les lignes de priorité '''{{color|erreur|red}}''' (ou supérieur) sont affiché en '''{{color|rouge|red}}'''
* Les lignes de priorité '''{{color|red|erreur}}''' (ou supérieur) sont affiché en '''{{color|red|rouge}}'''
* Les lignes de priorité '''notice/warning''' sont affiché en '''gras'''
* Les lignes de priorité '''notice/warning''' sont affiché en '''gras'''
* Les timestamps sont convertis en date/heure locale
* Les timestamps sont convertis en date/heure locale
* La sortie est utilise automatiquement un pager ( <app>less</app> par défaut)
* La sortie est utilise automatiquement un pager ( <app>less</app> par défaut)
* Affiche toutes les traces, même celles qui ont subies une rotation de logs.
* Affiche toutes les traces, même celles qui ont subies une rotation de logs.
* Un nouveau démarrage du système est clairement identifié
* Un démarrage du système est clairement identifié


Les logs contiennent les trois sources possibles de messages du daemon:
Les logs contiennent les trois sources possibles de messages du daemon:

Version du 27 septembre 2013 à 09:12

Introduction

Depuis Fedora 18, systemd est pleinement implémenté, c'est à dire que la commande pour visualiser les journaux systèmes est <app>journalctl</app>.

Le lancement de cette commande sans argument affiche tous les logs et peut contenir beaucoup de traces.

# journalctl

Cette commande lancée avec le super-utilisateur root, permet de voir tous les logs générés par le système et par les utilisateurs de celui-ci. A contrario, lancée avec un utilisateur basique, seuls les logs lui appartenant seront visibles. C'est une amélioration du traditionnel format de <path>/var/log/messages</path>:

  • Les lignes de priorité erreur (ou supérieur) sont affiché en rouge
  • Les lignes de priorité notice/warning sont affiché en gras
  • Les timestamps sont convertis en date/heure locale
  • La sortie est utilise automatiquement un pager ( <app>less</app> par défaut)
  • Affiche toutes les traces, même celles qui ont subies une rotation de logs.
  • Un démarrage du système est clairement identifié

Les logs contiennent les trois sources possibles de messages du daemon:

  • Appel depuis la GLibc syslog
  • Appel à l'API native de Journal
  • Écriture sur STDOUT et STDERR

Équivalences

<app>journalctl</app> remplace <app>cat /var/log/messages</app> <app>journalctl -f</app> remplace <app>tail -f /var/log/messages</app> <app>journalctl | grep foobar</app> remplace <app>grep foobar /var/log/messages</app>

Contrôle d'accès

Afin d'autoriser un utilisateur à visualiser les logs du système, sans devoir devenir le super-utilisateur root, il est prévu que les logs soient lisibles par les membres du groupe adm:

usermod -a -G adm didier

Live View

If invoked without parameters journalctl will show me the current log database. Sometimes one needs to watch logs as they grow, where one previously used tail -f /var/log/messages:

journalctl -f

Yes, this does exactly what you expect it to do: it will show you the last ten logs lines and then wait for changes and show them as they take place. Basic Filtering

When invoking journalctl without parameters you'll see the whole set of logs, beginning with the oldest message stored. That of course, can be a lot of data. Much more useful is just viewing the logs of the current boot:

journalctl -b

This will show you only the logs of the current boot, with all the aforementioned gimmicks mentioned. But sometimes even this is way too much data to process. So what about just listing all the real issues to care about: all messages of priority levels ERROR and worse, from the current boot:

journalctl -b -p err

If you reboot only seldom the -b makes little sense, filtering based on time is much more useful:

journalctl --since=yesterday

And there you go, all log messages from the day before at 00:00 in the morning until right now. Awesome! Of course, we can combine this with -p err or a similar match. But humm, we are looking for something that happened on the 15th of October, or was it the 16th?

journalctl --since=2012-10-15 --until="2011-10-16 23:59:59"

Yupp, there we go, we found what we were looking for. But humm, I noticed that some CGI script in Apache was acting up earlier today, let's see what Apache logged at that time:

journalctl -u httpd --since=00:00 --until=9:30

Oh, yeah, there we found it. But hey, wasn't there an issue with that disk /dev/sdc? Let's figure out what was going on there:

journalctl /dev/sdc

OMG, a disk error![2] Hmm, let's quickly replace the disk before we lose data. Done! Next! -- Hmm, didn't I see that the vpnc binary made a booboo? Let's check for that:

journalctl /usr/sbin/vpnc

Hmm, I don't get this, this seems to be some weird interaction with dhclient, let's see both outputs, interleaved:

journalctl /usr/sbin/vpnc /usr/sbin/dhclient

That did it! Found it!

Advanced Filtering

Whew! That was awesome already, but let's turn this up a notch. Internally systemd stores each log entry with a set of implicit meta data. This meta data looks a lot like an environment block, but actually is a bit more powerful: values can take binary, large values (though this is the exception, and usually they just contain UTF-8), and fields can have multiple values assigned (an exception too, usually they only have one value). This implicit meta data is collected for each and every log message, without user intervention. The data will be there, and wait to be used by you. Let's see how this looks:

journalctl -o verbose -n
[...]
Tue, 2012-10-23 23:51:38 CEST [s=ac9e9c423355411d87bf0ba1a9b424e8;i=4301;b=5335e9cf5d954633bb99aefc0ec38c25;m=882ee28d2;t=4ccc0f98326e6;x=f21e8b1b0994d7ee]
        PRIORITY=6
        SYSLOG_FACILITY=3
        _MACHINE_ID=a91663387a90b89f185d4e860000001a
        _HOSTNAME=epsilon
        _TRANSPORT=syslog
        SYSLOG_IDENTIFIER=avahi-daemon
        _COMM=avahi-daemon
        _EXE=/usr/sbin/avahi-daemon
        _SYSTEMD_CGROUP=/system/avahi-daemon.service
        _SYSTEMD_UNIT=avahi-daemon.service
        _SELINUX_CONTEXT=system_u:system_r:avahi_t:s0
        _UID=70
        _GID=70
        _CMDLINE=avahi-daemon: registering [epsilon.local]
        MESSAGE=Joining mDNS multicast group on interface wlan0.IPv4 with address 172.31.0.53.
        _BOOT_ID=5335e9cf5d954633bb99aefc0ec38c25
        _PID=27937
        SYSLOG_PID=27937
        _SOURCE_REALTIME_TIMESTAMP=1351029098747042

(I cut out a lot of noise here, I don't want to make this story overly long. -n without parameter shows you the last 10 log entries, but I cut out all but the last.)

With the -o verbose switch we enabled verbose output. Instead of showing a pixel-perfect copy of classic /var/log/messages that only includes a minimimal subset of what is available we now see all the gory details the journal has about each entry. But it's highly interesting: there is user credential information, SELinux bits, machine information and more. For a full list of common, well-known fields, see the man page.

Now, as it turns out the journal database is indexed by all of these fields, out-of-the-box! Let's try this out:

journalctl _UID=70

And there you go, this will show all log messages logged from Linux user ID 70. As it turns out one can easily combine these matches:

journalctl _UID=70 _UID=71

Specifying two matches for the same field will result in a logical OR combination of the matches. All entries matching either will be shown, i.e. all messages from either UID 70 or 71.

journalctl _HOSTNAME=epsilon _COMM=avahi-daemon

You guessed it, if you specify two matches for different field names, they will be combined with a logical AND. All entries matching both will be shown now, meaning that all messages from processes named avahi-daemon and host epsilon.

But of course, that's not fancy enough for us. We are computer nerds after all, we live off logical expressions. We must go deeper!

journalctl _HOSTNAME=theta _UID=70 + _HOSTNAME=epsilon _COMM=avahi-daemon

The + is an explicit OR you can use in addition to the implied OR when you match the same field twice. The line above hence means: show me everything from host theta with UID 70, or of host epsilon with a process name of avahi-daemon.

And now, it becomes magic!

That was already pretty cool, right? Righ! But heck, who can remember all those values a field can take in the journal, I mean, seriously, who has thaaaat kind of photographic memory? Well, the journal has:

journalctl -F _SYSTEMD_UNIT

This will show us all values the field _SYSTEMD_UNIT takes in the database, or in other words: the names of all systemd services which ever logged into the journal. This makes it super-easy to build nice matches. But wait, turns out this all is actually hooked up with shell completion on bash! This gets even more awesome: as you type your match expression you will get a list of well-known field names, and of the values they can take! Let's figure out how to filter for SELinux labels again. We remember the field name was something with SELINUX in it, let's try that:

journalctl _SE<TAB>

And yupp, it's immediately completed:

journalctl _SELINUX_CONTEXT=

Cool, but what's the label again we wanted to match for?

journalctl _SELINUX_CONTEXT=<TAB><TAB>
kernel                                                       system_u:system_r:local_login_t:s0-s0:c0.c1023               system_u:system_r:udev_t:s0-s0:c0.c1023
system_u:system_r:accountsd_t:s0                             system_u:system_r:lvm_t:s0                                   system_u:system_r:virtd_t:s0-s0:c0.c1023
system_u:system_r:avahi_t:s0                                 system_u:system_r:modemmanager_t:s0-s0:c0.c1023              system_u:system_r:vpnc_t:s0
system_u:system_r:bluetooth_t:s0                             system_u:system_r:NetworkManager_t:s0                        system_u:system_r:xdm_t:s0-s0:c0.c1023
system_u:system_r:chkpwd_t:s0-s0:c0.c1023                    system_u:system_r:policykit_t:s0                             unconfined_u:system_r:rpm_t:s0-s0:c0.c1023
system_u:system_r:chronyd_t:s0                               system_u:system_r:rtkit_daemon_t:s0                          unconfined_u:system_r:unconfined_t:s0-s0:c0.c1023
system_u:system_r:crond_t:s0-s0:c0.c1023                     system_u:system_r:syslogd_t:s0                               unconfined_u:system_r:useradd_t:s0-s0:c0.c1023
system_u:system_r:devicekit_disk_t:s0                        system_u:system_r:system_cronjob_t:s0-s0:c0.c1023            unconfined_u:unconfined_r:unconfined_dbusd_t:s0-s0:c0.c1023
system_u:system_r:dhcpc_t:s0                                 system_u:system_r:system_dbusd_t:s0-s0:c0.c1023              unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
system_u:system_r:dnsmasq_t:s0-s0:c0.c1023                   system_u:system_r:systemd_logind_t:s0
system_u:system_r:init_t:s0                                  system_u:system_r:systemd_tmpfiles_t:s0

Ah! Right! We wanted to see everything logged under PolicyKit's security label:

journalctl _SELINUX_CONTEXT=system_u:system_r:policykit_t:s0

Wow! That was easy! I didn't know anything related to SELinux could be thaaat easy! ;-) Of course this kind of completion works with any field, not just SELinux labels.

So much for now. There's a lot more cool stuff in journalctl(1) than this. For example, it generates JSON output for you! You can match against kernel fields! You can get simple /var/log/messages-like output but with relative timestamps! And so much more!

Anyway, in the next weeks I hope to post more stories about all the cool things the journal can do for you. This is just the beginning, stay tuned.