One of the services on my system is spamming the log and generating a lot of data. I don't know which service it is.
With regular log files, I can use ls -lha to print the size of all the files in the /var/log directory, which will point me to the culprit. But with systemd, all the logs are in one 'journal' file.
How do I generate a list of units with their corresponding log size? I'd like something like so;
+----------------------+-------+
| UNIT | SIZE |
+----------------------+-------+
| system.slice | 35.2M |
| cron.service | 17.0M |
| zabbix-agent.service | 8.9M |
| ... | ... |
By adapting the answer here; https://unix.stackexchange.com/questions/727066/how-to-get-only-the-unit-names-using-systemctl I created this rather slow method, which by the way also has an RCE vuln on the names of the units;
systemctl show '*' --state=loaded --property=Id --value --no-pager | grep . | sort | uniq | xargs -i sh -c ' journalctl -u {} | wc -c | tr -d "\n" && echo -n " " && echo {}' | sort -n
Breaking this down;
systemctl show '*'## show all units
--state=loaded ## Only active units
--property=Id ## Show the name.
--value ## Display the value
--no-pager ## Don't invoke 'more'
| grep . ## Get all results
| sort ## Sort by name
| uniq ## If a name occurs more than once, remove it.
| xargs -i sh -c '## Invoke, for each result, the following, and open (1);
journalctl -u {} ## Run journalctl to print the journal.
| wc -c ## Count the number of bytes in the output.
| tr -d "\n" ## Strip the trailing newline from the output of 'wc'.
&& echo -n " " ## Add a space for 'sort' later.
&& echo {}' ## Add the name of the unit. Close (1)
| sort -n ## Sort the whole output from the commands above by size, asc.
It 'works', but is obviously very, very slow, having to crawl through the entire journal. If the journal is big, this could take a long time. I wonder if there is a far better way of doing this?