knot_exporter icon indicating copy to clipboard operation
knot_exporter copied to clipboard

AttributeError: 'int' object has no attribute 'items'

Open rhclayto opened this issue 3 years ago • 7 comments

I get the error in the title when I run the command:

/opt/prometheus/exporters/knot/knot_exporter --web-listen-port 9433 --knot-socket-path /run/knot/knot.sock --knot-library-path /usr/lib/x86_64-linux-gnu/libknot.so

Here is the stack trace:

Traceback (most recent call last):
  File "/opt/prometheus/exporters/knot/knot_exporter", line 496, in <module>
    REGISTRY.register(KnotCollector(
  File "/usr/lib/python3/dist-packages/prometheus_client/registry.py", line 24, in register
    names = self._get_names(collector)
  File "/usr/lib/python3/dist-packages/prometheus_client/registry.py", line 64, in _get_names
    for metric in desc_func():
  File "/opt/prometheus/exporters/knot/knot_exporter", line 447, in collect
    for kind, kind_data in item_data.items():
AttributeError: 'int' object has no attribute 'items'

Any idea what's wrong?

rhclayto avatar Feb 08 '22 04:02 rhclayto

@rhclayto Could you try a forked version with some fixes? https://github.com/salzmdan/knot_exporter

salzmdan avatar Feb 12 '22 19:02 salzmdan

@rhclayto Could you try a forked version with some fixes? https://github.com/salzmdan/knot_exporter

Yes, thanks so much, it worked for me. I did however have to make one change:

REGISTRY.register(KnotCollector(
        args.knot_library_path,
        args.knot_socket_path,
        args.knot_socket_timeout,
        args.no_meminfo,
        args.no_global_stats,
        args.no_zone_stats,
        args.no_zone_status,
        args.no_zone_read,
    ))

to

REGISTRY.register(KnotCollector(
        args.knot_library_path,
        args.knot_socket_path,
        args.knot_socket_timeout,
        args.no_meminfo,
        args.no_global_stats,
        args.no_zone_stats,
        args.no_zone_status,
        args.no_zone_timers,
    ))

args.no_zone_read, -> args.no_zone_timers,

Without this change I was getting AttributeError: 'Namespace' object has no attribute args.no_zone_read.

rhclayto avatar Feb 13 '22 22:02 rhclayto

Hi,

I am also affected by this issue, so I tried the fork as well. I needed to manually install libknot, pidof and psutil. However it fails anyway:

Traceback (most recent call last):
  File "/usr/local/bin/knotexporter", line 217, in <module>
    REGISTRY.register(KnotCollector(
  File "/usr/local/lib/python3.9/site-packages/prometheus_client/registry.py", line 40, in register
    names = self._get_names(collector)
  File "/usr/local/lib/python3.9/site-packages/prometheus_client/registry.py", line 80, in _get_names
    for metric in desc_func():
  File "/usr/local/bin/knotexporter", line 76, in collect
    for pid, usage in memory_usage().items():
  File "/usr/local/bin/knotexporter", line 24, in memory_usage
    key = int(pid)
ValueError: invalid literal for int() with base 10: b'\n'

I am using knot 3.3.0 on FreeBSD 13.2-p1

TLINDEN avatar Sep 11 '23 09:09 TLINDEN

Here's the fix:

diff --git a/knot_exporter.py b/knot_exporter.py
index 8b1ce4e..3d16b27 100755
--- a/knot_exporter.py
+++ b/knot_exporter.py
@@ -19,6 +19,7 @@ def memory_usage():
     out = dict()
     pids = subprocess.check_output(['pidof', 'knotd']).split(b' ')
     for pid in pids:
+        pid = pid.rstrip()
         if not pid:
             continue
         key = int(pid)

Without it, one pid contained a newline, so the empty string test failed and it tried to convert that into an integer.

TLINDEN avatar Sep 11 '23 10:09 TLINDEN

Here's the fix:

diff --git a/knot_exporter.py b/knot_exporter.py
index 8b1ce4e..3d16b27 100755
--- a/knot_exporter.py
+++ b/knot_exporter.py
@@ -19,6 +19,7 @@ def memory_usage():
     out = dict()
     pids = subprocess.check_output(['pidof', 'knotd']).split(b' ')
     for pid in pids:
+        pid = pid.rstrip()
         if not pid:
             continue
         key = int(pid)

Without it, one pid contained a newline, so the empty string test failed and it tried to convert that into an integer.

Does this patch work https://gitlab.nic.cz/knot/knot-dns/-/commit/7e293321a4cf895078e6fa83bbb5b9cab181eb66.patch ?

salzmdan avatar Sep 11 '23 12:09 salzmdan

Yes it works!

Do I see this right, knot_exporter.py is going to be part of the official distribution? Great!

kind regards, Tom

TLINDEN avatar Sep 12 '23 07:09 TLINDEN

Fixed by https://gitlab.nic.cz/knot/knot-dns/-/commit/07f5c3970fffce23b625d9a8cae947e1699fa722

Yes, knot-exporter has been included in knot. Also you can find it as https://pypi.org/project/knot-exporter/

salzmdan avatar Sep 13 '23 09:09 salzmdan