Skip to content

Commit 9d515cd

Browse files
committed
refactor: convert .format() string formatting to f-strings across all plugins
1 parent 4fc9899 commit 9d515cd

203 files changed

Lines changed: 3244 additions & 3045 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Grafana:
7575

7676
Monitoring Plugins:
7777

78+
* all plugins: convert `.format()` string formatting to f-strings
7879
* all plugins: ignore unknown arguments instead of generating an error (this helps with updating Icinga and Nagios service definitions considerably)
7980
* by-ssh, by-winrm, disk-usage, example, file-ownership, fs-ro, infomaniak-events, journald-query, logfile, matomo-reporting, mysql-logfile, php-status, pip-updates, systemd-unit: fix `append` parameters so that user-specified values replace defaults instead of being appended to them ([#540](https://github.com/Linuxfabrik/monitoring-plugins/issues/540))
8081
* cpu-usage: remove `--top` parameter (the top N processes by CPU time are now reported by the procs check via `--top`)

check-plugins/about-me/about-me

Lines changed: 111 additions & 111 deletions
Large diffs are not rendered by default.

check-plugins/apache-httpd-status/apache-httpd-status

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
2424
STATE_UNKNOWN, STATE_WARN)
2525

2626
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
27-
__version__ = '2025100601'
27+
__version__ = '2026040801'
2828

2929
DESCRIPTION = 'Checks how well an Apache httpd server is performing.'
3030

@@ -44,7 +44,7 @@ def parse_args():
4444
parser.add_argument(
4545
'-V', '--version',
4646
action='version',
47-
version='%(prog)s: v{} by {}'.format(__version__, __author__)
47+
version=f'%(prog)s: v{__version__} by {__author__}'
4848
)
4949

5050
parser.add_argument(
@@ -242,14 +242,14 @@ def main():
242242
# If ExtendedStatus is off:
243243
if 'Uptime' not in apache:
244244
# "ExtendedStatus off" just reports BusyWorkers, IdleWorkers and Scoreboard
245-
msg = '{}/{} workers busy ({}%{}; {} "G"), {} idle, {} free'.format(
246-
apache['BusyWorkers'],
247-
apache['TotalWorkers'],
248-
apache['WorkerUsagePercentage'],
249-
lib.base.state2str(state, prefix=' '),
250-
workers['finishing'],
251-
apache['IdleWorkers'],
252-
workers['free'],
245+
msg = (
246+
f'{apache["BusyWorkers"]}/{apache["TotalWorkers"]}'
247+
f' workers busy'
248+
f' ({apache["WorkerUsagePercentage"]}%'
249+
f'{lib.base.state2str(state, prefix=" ")}'
250+
f'; {workers["finishing"]} "G")'
251+
f', {apache["IdleWorkers"]} idle'
252+
f', {workers["free"]} free'
253253
)
254254

255255
# over and out
@@ -262,31 +262,31 @@ def main():
262262

263263
# get previous values
264264
prev_uptime = lib.cache.get(
265-
'apache-httpd-status-{}-uptime'.format(args.URL),
265+
f'apache-httpd-status-{args.URL}-uptime',
266266
filename='linuxfabrik-monitoring-plugins-apache-httpd-status.db',
267267
)
268268
prev_count = lib.cache.get(
269-
'apache-httpd-status-{}-total-accesses'.format(args.URL),
269+
f'apache-httpd-status-{args.URL}-total-accesses',
270270
filename='linuxfabrik-monitoring-plugins-apache-httpd-status.db',
271271
)
272272
prev_bcount = lib.cache.get(
273-
'apache-httpd-status-{}-total-bytes'.format(args.URL),
273+
f'apache-httpd-status-{args.URL}-total-bytes',
274274
filename='linuxfabrik-monitoring-plugins-apache-httpd-status.db',
275275
)
276276

277277
# save current values
278278
lib.cache.set(
279-
'apache-httpd-status-{}-uptime'.format(args.URL),
279+
f'apache-httpd-status-{args.URL}-uptime',
280280
apache['Uptime'],
281281
filename='linuxfabrik-monitoring-plugins-apache-httpd-status.db',
282282
)
283283
lib.cache.set(
284-
'apache-httpd-status-{}-total-accesses'.format(args.URL),
284+
f'apache-httpd-status-{args.URL}-total-accesses',
285285
apache['Total Accesses'],
286286
filename='linuxfabrik-monitoring-plugins-apache-httpd-status.db',
287287
)
288288
lib.cache.set(
289-
'apache-httpd-status-{}-total-bytes'.format(args.URL),
289+
f'apache-httpd-status-{args.URL}-total-bytes',
290290
apache['Total Bytes'],
291291
filename='linuxfabrik-monitoring-plugins-apache-httpd-status.db',
292292
)
@@ -306,17 +306,17 @@ def main():
306306
perfdata += lib.base.get_perfdata('Uptime', apache['Uptime'], 's', None, None, 0, None) # pylint: disable=C0301
307307

308308
if 'Total Duration' not in apache:
309-
msg = '{}/{} workers busy ({}%{}; {} "G"), {} idle, {} free; {} accesses, {} traffic; Up {}'.format(
310-
apache['BusyWorkers'],
311-
apache['TotalWorkers'],
312-
apache['WorkerUsagePercentage'],
313-
lib.base.state2str(state, prefix=' '),
314-
workers['finishing'],
315-
apache['IdleWorkers'],
316-
workers['free'],
317-
lib.human.number2human(apache['Total Accesses']),
318-
lib.human.bytes2human(apache['Total Bytes']),
319-
lib.human.seconds2human(apache['Uptime']),
309+
msg = (
310+
f'{apache["BusyWorkers"]}/{apache["TotalWorkers"]}'
311+
f' workers busy'
312+
f' ({apache["WorkerUsagePercentage"]}%'
313+
f'{lib.base.state2str(state, prefix=" ")}'
314+
f'; {workers["finishing"]} "G")'
315+
f', {apache["IdleWorkers"]} idle'
316+
f', {workers["free"]} free'
317+
f'; {lib.human.number2human(apache["Total Accesses"])} accesses'
318+
f', {lib.human.bytes2human(apache["Total Bytes"])} traffic'
319+
f'; Up {lib.human.seconds2human(apache["Uptime"])}'
320320
)
321321

322322
# over and out
@@ -327,11 +327,11 @@ def main():
327327
# Some more variables in newer versions of mod_status
328328

329329
prev_duration_global = lib.cache.get(
330-
'apache-httpd-status-{}-total-duration'.format(args.URL),
330+
f'apache-httpd-status-{args.URL}-total-duration',
331331
filename='linuxfabrik-monitoring-plugins-apache-httpd-status.db',
332332
)
333333
lib.cache.set(
334-
'apache-httpd-status-{}-total-duration'.format(args.URL),
334+
f'apache-httpd-status-{args.URL}-total-duration',
335335
apache['Total Duration'],
336336
filename='linuxfabrik-monitoring-plugins-apache-httpd-status.db',
337337
)
@@ -356,18 +356,18 @@ def main():
356356
perfdata += lib.base.get_perfdata('Stopping', apache['Stopping'], None, None, None, 0, None) if 'Stopping' in apache else '' # pylint: disable=C0301
357357
perfdata += lib.base.get_perfdata('Total Duration', duration_diff, 's', None, None, 0, None) if 'Total Duration' in apache else '' # pylint: disable=C0301
358358

359-
msg = '{}: {}/{} workers busy ({}%{}; {} "G"), {} idle, {} free; {} accesses, {} traffic; Up {}'.format(
360-
apache['ServerName'],
361-
apache['BusyWorkers'],
362-
apache['TotalWorkers'],
363-
apache['WorkerUsagePercentage'],
364-
lib.base.state2str(state, prefix=' '),
365-
workers['finishing'],
366-
apache['IdleWorkers'],
367-
workers['free'],
368-
lib.human.number2human(apache['Total Accesses']),
369-
lib.human.bytes2human(apache['Total Bytes']),
370-
lib.human.seconds2human(apache['Uptime']),
359+
msg = (
360+
f'{apache["ServerName"]}:'
361+
f' {apache["BusyWorkers"]}/{apache["TotalWorkers"]}'
362+
f' workers busy'
363+
f' ({apache["WorkerUsagePercentage"]}%'
364+
f'{lib.base.state2str(state, prefix=" ")}'
365+
f'; {workers["finishing"]} "G")'
366+
f', {apache["IdleWorkers"]} idle'
367+
f', {workers["free"]} free'
368+
f'; {lib.human.number2human(apache["Total Accesses"])} accesses'
369+
f', {lib.human.bytes2human(apache["Total Bytes"])} traffic'
370+
f'; Up {lib.human.seconds2human(apache["Uptime"])}'
371371
)
372372

373373
table_values = []

check-plugins/apache-httpd-version/apache-httpd-version

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import lib.version # pylint: disable=C0413
2121
from lib.globals import (STATE_UNKNOWN) # pylint: disable=C0413
2222

2323
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
24-
__version__ = '2025100601'
24+
__version__ = '2026040801'
2525

2626
DESCRIPTION = 'Tracks if Apache httpd is EOL.'
2727

@@ -42,7 +42,7 @@ def parse_args():
4242
parser.add_argument(
4343
'-V', '--version',
4444
action='version',
45-
version='%(prog)s: v{} by {}'.format(__version__, __author__)
45+
version=f'%(prog)s: v{__version__} by {__author__}'
4646
)
4747

4848
parser.add_argument(
@@ -170,7 +170,7 @@ def main():
170170

171171
# over and out
172172
lib.base.oao(
173-
'Apache httpd v{} ({})'.format(installed_version, msg),
173+
f'Apache httpd v{installed_version} ({msg})',
174174
state,
175175
lib.base.get_perfdata(
176176
'apache-httpd-version',

check-plugins/apache-solr-version/apache-solr-version

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import lib.version # pylint: disable=C0413
2121
from lib.globals import (STATE_UNKNOWN) # pylint: disable=C0413
2222

2323
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
24-
__version__ = '2025100601'
24+
__version__ = '2026040801'
2525

2626
DESCRIPTION = 'Tracks if Apache Solr is EOL.'
2727

@@ -43,7 +43,7 @@ def parse_args():
4343
parser.add_argument(
4444
'-V', '--version',
4545
action='version',
46-
version='%(prog)s: v{} by {}'.format(__version__, __author__)
46+
version=f'%(prog)s: v{__version__} by {__author__}'
4747
)
4848

4949
parser.add_argument(
@@ -136,7 +136,7 @@ def parse_args():
136136

137137

138138
def get_installed_version(path):
139-
success, result = lib.shell.shell_exec('{} version'.format(path))
139+
success, result = lib.shell.shell_exec(f'{path} version')
140140
if not success:
141141
return ''
142142
stdout = result[0].strip()
@@ -164,7 +164,7 @@ def main():
164164
# fetch data
165165
installed_version = get_installed_version(args.PATH)
166166
if not installed_version:
167-
lib.base.cu('Apache Solr `{}` not found.'.format(args.PATH))
167+
lib.base.cu(f'Apache Solr `{args.PATH}` not found.')
168168
state, msg = lib.version.check_eol(
169169
'https://endoflife.date/api/solr.json',
170170
installed_version,
@@ -179,7 +179,7 @@ def main():
179179

180180
# over and out
181181
lib.base.oao(
182-
'Apache Solr v{} ({})'.format(installed_version, msg),
182+
f'Apache Solr v{installed_version} ({msg})',
183183
state,
184184
lib.base.get_perfdata(
185185
'apache-solr-version',

check-plugins/axenita-stats/axenita-stats

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
2323
STATE_UNKNOWN, STATE_WARN)
2424

2525
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
26-
__version__ = '2025100601'
26+
__version__ = '2026040801'
2727

2828
DESCRIPTION = 'With this plugin you can track some values of the Axenita application.'
2929

@@ -39,7 +39,7 @@ def parse_args():
3939
parser.add_argument(
4040
'-V', '--version',
4141
action='version',
42-
version='%(prog)s: v{} by {}'.format(__version__, __author__)
42+
version=f'%(prog)s: v{__version__} by {__author__}'
4343
)
4444

4545
parser.add_argument(
@@ -165,45 +165,45 @@ def main():
165165
if achilles_maintenance:
166166
if achilles_maintenance['state'].upper() != 'SUCCESS':
167167
state = lib.base.get_worst(STATE_WARN, state)
168-
msg = '{}/api/login/maintenance-state-active, state: {}{}'.format(args.URL, achilles_maintenance['state'], lib.base.state2str(state))
168+
msg = f"{args.URL}/api/login/maintenance-state-active, state: {achilles_maintenance['state']}{lib.base.state2str(state)}"
169169
lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)
170170
if achilles_maintenance['data'] is not False:
171171
state = lib.base.get_worst(STATE_WARN, state)
172-
msg += 'Axenita/Achilles is in maintenance mode {}, '.format(lib.base.state2str(state))
172+
msg += f'Axenita/Achilles is in maintenance mode {lib.base.state2str(state)}, '
173173
perfdata += lib.base.get_perfdata('maintenance', 1, None, None, None, 0, 1)
174174
else:
175175
perfdata += lib.base.get_perfdata('maintenance', 0, None, None, None, 0, 1)
176176

177177
if achilles_readmodel:
178178
if achilles_readmodel['state'].upper() != 'SUCCESS':
179179
state = lib.base.get_worst(STATE_WARN, state)
180-
msg = '{}/api/admin/readmodel/state, state: {}{}'.format(args.URL, achilles_readmodel['state'], lib.base.state2str(state))
180+
msg = f"{args.URL}/api/admin/readmodel/state, state: {achilles_readmodel['state']}{lib.base.state2str(state)}"
181181
lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)
182182
if achilles_readmodel['data']['readModelState'].upper() != 'DONE':
183183
state = lib.base.get_worst(STATE_WARN, state)
184-
msg = '{}/api/admin/readmodel/state, data.readModelState: {}{}'.format(args.URL, achilles_readmodel['data']['readModelState'], lib.base.state2str(state))
184+
msg = f"{args.URL}/api/admin/readmodel/state, data.readModelState: {achilles_readmodel['data']['readModelState']}{lib.base.state2str(state)}"
185185
lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)
186-
msg += 'ReadModel: {}, '.format(achilles_readmodel['data']['message'])
186+
msg += f"ReadModel: {achilles_readmodel['data']['message']}, "
187187
perfdata += lib.base.get_perfdata('currentInitRmStep', achilles_readmodel['data']['currentInitRmStep'], None, None, None, 0, None)
188188
perfdata += lib.base.get_perfdata('totalInitRmSteps', achilles_readmodel['data']['totalInitRmSteps'], None, None, None, 0, None)
189189
perfdata += lib.base.get_perfdata('totalDurationInitRm', achilles_readmodel['data']['totalDurationInitRm'], None, None, None, 0, None)
190190

191191
if achilles_userinfo:
192192
if achilles_userinfo['state'].upper() != 'SUCCESS':
193193
state = lib.base.get_worst(STATE_WARN, state)
194-
msg = '{}/api/admin/user-info/number-of-current-sessions, state: {}{}'.format(args.URL, achilles_userinfo['state'], lib.base.state2str(state))
194+
msg = f"{args.URL}/api/admin/user-info/number-of-current-sessions, state: {achilles_userinfo['state']}{lib.base.state2str(state)}"
195195
lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)
196-
msg += '{} users logged in, {} active sessions, '.format(achilles_userinfo['data']['loggedInUsers'], achilles_userinfo['data']['currentActiveSessions'])
196+
msg += f"{achilles_userinfo['data']['loggedInUsers']} users logged in, {achilles_userinfo['data']['currentActiveSessions']} active sessions, "
197197
perfdata += lib.base.get_perfdata('loggedInUsers', achilles_userinfo['data']['loggedInUsers'], None, None, None, 0, None)
198198
perfdata += lib.base.get_perfdata('currentActiveSessions', achilles_userinfo['data']['currentActiveSessions'], None, None, None, 0, None)
199199

200200
if achilles_buildinfo:
201201
if achilles_buildinfo['state'].upper() != 'SUCCESS':
202202
state = lib.base.get_worst(STATE_WARN, state)
203-
msg = '{}/api/build-info, state: {}{}'.format(args.URL, achilles_buildinfo['state'], lib.base.state2str(state))
203+
msg = f"{args.URL}/api/build-info, state: {achilles_buildinfo['state']}{lib.base.state2str(state)}"
204204
lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)
205205
axenita_ver = achilles_buildinfo['data']['version']
206-
msg += '{} (timestamp {}), '.format(axenita_ver, achilles_buildinfo['data']['timestamp'])
206+
msg += f"{axenita_ver} (timestamp {achilles_buildinfo['data']['timestamp']}), "
207207
axenita_ver = axenita_ver[8:8+axenita_ver.find('-')-1].replace('.', '')
208208
perfdata += lib.base.get_perfdata('axenita-version', axenita_ver, None, None, None, 0, None)
209209

check-plugins/borgbackup/borgbackup

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
2222
STATE_UNKNOWN, STATE_WARN)
2323

2424
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
25-
__version__ = '2025100601'
25+
__version__ = '2026040801'
2626

2727
DESCRIPTION = 'Checks the date and return code of the last borgbackup, according to the logfile.'
2828

@@ -38,7 +38,7 @@ def parse_args():
3838
parser.add_argument(
3939
'-V', '--version',
4040
action='version',
41-
version='{0}: v{1} by {2}'.format('%(prog)s', __version__, __author__)
41+
version=f'%(prog)s: v{__version__} by {__author__}'
4242
)
4343

4444
parser.add_argument(
@@ -85,7 +85,7 @@ def main():
8585
# end: 2020-04-08 23:00:13
8686

8787
if os.path.exists(path) <= 0 and not os.path.isfile(path):
88-
lib.base.cu('Logfile {} not found or empty.'.format(path))
88+
lib.base.cu(f'Logfile {path} not found or empty.')
8989

9090
mount_lines = ''
9191
with open('/proc/mounts') as mountlist:
@@ -94,7 +94,7 @@ def main():
9494
mount_lines += '* ' + line
9595

9696
if mount_lines:
97-
lib.base.oao('There are active borg mounts.\n\n{}'.format(mount_lines), STATE_WARN)
97+
lib.base.oao(f'There are active borg mounts.\n\n{mount_lines}', STATE_WARN)
9898

9999

100100
logfile = open(path, 'r')
@@ -140,14 +140,14 @@ def main():
140140
duration = lib.time.timestrdiff(endtime, starttime) # in seconds
141141
perfdata += lib.base.get_perfdata('duration', duration, 's', None, None, 0, None)
142142

143-
msg = 'Last Backup started {}, ended {}, took {}.\n'.format(starttime, endtime, lib.human.seconds2human(duration))
143+
msg = f'Last Backup started {starttime}, ended {endtime}, took {lib.human.seconds2human(duration)}.\n'
144144
if state != STATE_OK:
145145
if last_starttime_state != STATE_OK:
146146
msg = 'Last backup is too long ago. ' + msg
147147
else:
148148
msg = 'One or more errors. ' + msg
149-
msg += '* Create retc: {}, State: {}\n'.format(create_retc, lib.base.state2str(create_retc_state))
150-
msg += '* Prune retc: {}, State: {}'.format(prune_retc, lib.base.state2str(prune_retc_state))
149+
msg += f'* Create retc: {create_retc}, State: {lib.base.state2str(create_retc_state)}\n'
150+
msg += f'* Prune retc: {prune_retc}, State: {lib.base.state2str(prune_retc_state)}'
151151

152152
# over and out
153153
lib.base.oao(msg, state, perfdata)

0 commit comments

Comments
 (0)