@@ -19,16 +19,18 @@ import lib.base # pylint: disable=C0413
19
19
import lib .human # pylint: disable=C0413
20
20
import lib .shell # pylint: disable=C0413
21
21
import lib .lftest # pylint: disable=C0413
22
- from lib .globals import ( STATE_OK , STATE_UNKNOWN , # pylint: disable=C0413
23
- STATE_WARN )
22
+ import lib .txt # pylint: disable=C0413
23
+ from lib . globals import ( STATE_OK , STATE_UNKNOWN )
24
24
25
25
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
26
- __version__ = '2025021501 '
26
+ __version__ = '2025071701 '
27
27
28
28
DESCRIPTION = """Checks the current disk usage of all journal files of the systemd journal
29
29
(in fact the sum of the disk usage of all archived and active journal files)."""
30
30
31
31
DEFAULT_WARN = 6 # GiB; journald: "capped to 4G" per default
32
+ SYSTEMD_DEFAULT_MAX_USE = '4G'
33
+ SYSTEMD_DEFAULT_KEEP_FREE = '1M'
32
34
33
35
34
36
def parse_args ():
@@ -39,7 +41,7 @@ def parse_args():
39
41
parser .add_argument (
40
42
'-V' , '--version' ,
41
43
action = 'version' ,
42
- version = '%(prog)s: v{} by {}' . format ( __version__ , __author__ )
44
+ version = f '%(prog)s: v{ __version__ } by { __author__ } '
43
45
)
44
46
45
47
parser .add_argument (
@@ -59,7 +61,8 @@ def parse_args():
59
61
60
62
parser .add_argument (
61
63
'-w' , '--warning' ,
62
- help = 'Set the WARN threshold in GiB. Default: >= %(default)s' ,
64
+ help = 'Set the WARN threshold in GiB. '
65
+ 'Default: >= %(default)s' ,
63
66
dest = 'WARN' ,
64
67
type = int ,
65
68
default = DEFAULT_WARN ,
@@ -68,6 +71,27 @@ def parse_args():
68
71
return parser .parse_args ()
69
72
70
73
74
+ def extract_config (output ):
75
+ """
76
+ Extracts the last occurrences of SystemMaxUse and SystemKeepFree.
77
+ Returns a tuple: (SystemMaxUse_value, SystemKeepFree_value).
78
+ """
79
+ max_use = None
80
+ keep_free = None
81
+ for line in reversed (output .splitlines ()):
82
+ if max_use is None and line .startswith ('SystemMaxUse=' ):
83
+ max_use = line .split ('=' , 1 )[1 ].strip ()
84
+ if keep_free is None and line .startswith ('SystemKeepFree=' ):
85
+ keep_free = line .split ('=' , 1 )[1 ].strip ()
86
+ if max_use is not None and keep_free is not None :
87
+ break
88
+ if max_use is None :
89
+ max_use = SYSTEMD_DEFAULT_MAX_USE
90
+ if keep_free is None :
91
+ keep_free = SYSTEMD_DEFAULT_KEEP_FREE
92
+ return max_use , keep_free
93
+
94
+
71
95
def main ():
72
96
"""The main function. Hier spielt die Musik.
73
97
"""
@@ -81,12 +105,20 @@ def main():
81
105
# fetch data
82
106
if args .TEST is None :
83
107
cmd = 'journalctl --disk-usage'
84
- stdout , stderr , retc = lib .base .coe (lib .shell .shell_exec (cmd )) # pylint: disable=W0612
108
+ stdout , stderr , _ = lib .base .coe (lib .shell .shell_exec (cmd )) # pylint: disable=W0612
85
109
if stderr :
86
110
lib .base .cu (stderr )
111
+
112
+ # get journald's thresholds
113
+ cmd = 'systemd-analyze cat-config systemd/journald.conf'
114
+ config , stderr , _ = lib .base .coe (lib .shell .shell_exec (cmd )) # pylint: disable=W0612
115
+ if stderr :
116
+ lib .base .cu (stderr )
117
+ max_use , keep_free = extract_config (config )
87
118
else :
88
119
# do not call the command, put in test data
89
- stdout , stderr , retc = lib .lftest .test (args .TEST )
120
+ stdout , stderr , _ = lib .lftest .test (args .TEST )
121
+ max_use , keep_free = SYSTEMD_DEFAULT_MAX_USE , SYSTEMD_DEFAULT_KEEP_FREE
90
122
91
123
# init some vars
92
124
msg = ''
@@ -96,22 +128,28 @@ def main():
96
128
# Archived and active journals take up %s on disk.\n
97
129
# Archived and active journals take up %s in the file system.\n
98
130
# Journals take up %s on disk.\n
99
- # currently no need for re module, let's do simple string matching
100
- pos1 = stdout .find (' take up ' ) + len (' take up ' )
101
- pos2 = stdout .find (' ' , pos1 )
102
- value = stdout [pos1 :pos2 ]
131
+ value = lib .txt .extract_str (stdout , ' take up ' , ' ' , include_fromto = False , be_tolerant = False )
103
132
value = lib .human .human2bytes (value )
104
133
105
134
# build the message
106
135
state = lib .base .get_state (value , args .WARN * 1024 * 1024 * 1024 , None )
107
- msg += '{} used{} (sum of all archived and active journal files)' .format (
108
- lib .human .bytes2human (value ),
109
- lib .base .state2str (state , prefix = ' ' ),
110
- )
136
+ msg += f'{ lib .human .bytes2human (value )} used{ lib .base .state2str (state , prefix = " " )} ' \
137
+ '(sum of all archived and active journal files; ' \
138
+ f'SystemMaxUse={ max_use } SystemKeepFree={ keep_free } )'
111
139
if state != STATE_OK :
112
- msg += '. Remove the oldest archived journal files by using `journalctl --vacuum-size=`, ' \
113
- '`--vacuum-time=` and/or `--vacuum-files=`.'
114
- perfdata += lib .base .get_perfdata ('journald-usage' , value , 'B' , args .WARN * 1024 * 1024 * 1024 , None , 0 , None )
140
+ msg += '. Configure `SystemMaxUse` and `SystemKeepFree` in ' \
141
+ '`/etc/systemd/journald.conf/`, ' \
142
+ 'or remove the oldest archived journal files by using ' \
143
+ '`journalctl --vacuum-size=`, `--vacuum-time=` and/or `--vacuum-files=`.'
144
+ perfdata += lib .base .get_perfdata (
145
+ 'journald-usage' ,
146
+ value ,
147
+ uom = 'B' ,
148
+ warn = args .WARN * 1024 * 1024 * 1024 ,
149
+ crit = None ,
150
+ _min = 0 ,
151
+ _max = None ,
152
+ )
115
153
116
154
# over and out
117
155
lib .base .oao (msg , state , perfdata , always_ok = args .ALWAYS_OK )
0 commit comments