Linuxfabrik / monitoring-plugins

220+ check plugins for Icinga and other Nagios-compatible monitoring applications. Each plugin is a standalone command line tool (written in Python) that provides a specific type of check.
https://linuxfabrik.ch
The Unlicense
220 stars 51 forks source link

disk-io: UnboundLocalError: cannot access local variable 'msg' where it is not associated with a value #777

Closed wwuck closed 3 months ago

wwuck commented 3 months ago

This issue respects the following points:

Which variant of the Monitoring Plugins do you use?

Bug description

I'm trying to run disk-io check on lots of VMs and on some VMs it is reporting a traceback error.

Traceback (most recent call last):
  File "disk-io.py", line 422, in 'module'
  File "disk-io.py", line 414, in main
  File "disk-io.py", line 183, in top
UnboundLocalError: cannot access local variable 'msg' where it is not associated with a value

I set a pdb trace on the line above https://github.com/Linuxfabrik/monitoring-plugins/blob/main/check-plugins/disk-io/disk-io#L182 and get the following results:

(Pdb) l
181                 )
182     
183         import pdb
184         pdb.set_trace()
185     
186  ->     return msg
187     
188     
189     def main():
190         """The main function. Hier spielt die Musik.
191         """
(Pdb) print(msg)
*** NameError: name 'msg' is not defined
(Pdb) print(cnt)
[('bash', {'r': 0, 'w': 0}), ('python3', {'r': 0, 'w': 0})]

Steps to reproduce - Plugin call

/usr/lib64/nagios/plugins/disk-io

Steps to reproduce - Data

No response

Environment

uname -a

Linux gitlab-runner-01 6.1.0-23-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.99-1 (2024-07-15) x86_64 GNU/Linux

cat /etc/os-release

PRETTY_NAME="Devuan GNU/Linux 5 (daedalus)" NAME="Devuan GNU/Linux" VERSION_ID="5" VERSION="5 (daedalus)" VERSION_CODENAME="daedalus" ID=devuan ID_LIKE=debian HOME_URL="https://www.devuan.org/" SUPPORT_URL="https://devuan.org/os/community" BUG_REPORT_URL="https://bugs.devuan.org/"

cat /etc/debian_version

12.0

apt-cache policy linuxfabrik-monitoring-plugins

linuxfabrik-monitoring-plugins: Installed: 2024060401-1 Candidate: 2024060401-1 Version table: *** 2024060401-1 500 500 https://repo.linuxfabrik.ch/monitoring-plugins/debian bookworm-release/main amd64 Packages 100 /var/lib/dpkg/status 2023112901-1 500 500 https://repo.linuxfabrik.ch/monitoring-plugins/debian bookworm-release/main amd64 Packages 2023051201-1 500 500 https://repo.linuxfabrik.ch/monitoring-plugins/debian bookworm-release/main amd64 Packages

Plugin Version

disk-io: v2024071701 by Linuxfabrik GmbH, Zurich/Switzerland

Python version

No response

List of Python modules

No response

Additional Information

No response

wwuck commented 3 months ago

I can confirm that the following changes to the method appear to fix this issue:

diff --git a/check-plugins/disk-io/disk-io b/check-plugins/disk-io/disk-io
index a6f2ed9a..02872ca4 100755
--- a/check-plugins/disk-io/disk-io
+++ b/check-plugins/disk-io/disk-io
@@ -153,6 +153,7 @@ def top(count):
     """Get top X processes that generated the most I/O traffic.
     """
     cnt = {}
+    msg = ''
     try:
         for p in [x.as_dict(attrs=['name', 'io_counters']) for x in psutil.process_iter()]:
             if p['io_counters']:
@@ -166,7 +167,7 @@ def top(count):
     #     ('WebKitNetworkProcess', {'r': 1716224, 'w': 0}),
     #     ('ibus-x11', {'r': 0, 'w': 0}), ...
     if not cnt:
-        return ''
+        return msg
     cnt = sorted(cnt.items(), key=lambda x: x[1]['r']+x[1]['w'], reverse=True)
     if cnt[0][1]['r'] + cnt[0][1]['w'] > 0: # don't print "1. any-proc: 0.0B/0.0B (r/w)"
         msg = '\nTop {} processes that generate the most I/O traffic (r/w):\n'.format(count)
markuslf commented 3 months ago

Perfect, thank you!