diff options
author | Ilya Mashchenko <ilya@netdata.cloud> | 2020-11-23 15:15:17 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-23 15:15:17 +0300 |
commit | 4de1c2e6cca67d8fcd87ebd88879293042aeda8c (patch) | |
tree | 62a676531296eb1e18c796b6db948450a15921cb /collectors | |
parent | 3f8e44d1a6c501feeeacab3b3bf86ec0e401d4a3 (diff) |
python.d/nvidia_smi: use `pwd` lib to get username if not inside a container (#10268)
Diffstat (limited to 'collectors')
-rw-r--r-- | collectors/python.d.plugin/nvidia_smi/nvidia_smi.chart.py | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/collectors/python.d.plugin/nvidia_smi/nvidia_smi.chart.py b/collectors/python.d.plugin/nvidia_smi/nvidia_smi.chart.py index 68eb49966f..506bf1175c 100644 --- a/collectors/python.d.plugin/nvidia_smi/nvidia_smi.chart.py +++ b/collectors/python.d.plugin/nvidia_smi/nvidia_smi.chart.py @@ -7,6 +7,7 @@ import subprocess import threading import os +import pwd import xml.etree.ElementTree as et @@ -247,9 +248,12 @@ HOST_PREFIX = os.getenv('NETDATA_HOST_PREFIX') ETC_PASSWD_PATH = '/etc/passwd' PROC_PATH = '/proc' +IS_INSIDE_DOCKER = False + if HOST_PREFIX: ETC_PASSWD_PATH = os.path.join(HOST_PREFIX, ETC_PASSWD_PATH[1:]) PROC_PATH = os.path.join(HOST_PREFIX, PROC_PATH[1:]) + IS_INSIDE_DOCKER = True def read_passwd_file(): @@ -271,21 +275,25 @@ def read_passwd_file(): def read_passwd_file_safe(): try: - return read_passwd_file() + if IS_INSIDE_DOCKER: + return read_passwd_file() + return dict((k[2], k) for k in pwd.getpwall()) except (OSError, IOError): return dict() def get_username_by_pid_safe(pid, passwd_file): - if not passwd_file: - return '' path = os.path.join(PROC_PATH, pid) try: uid = os.stat(path).st_uid - return passwd_file[uid][0] - except (OSError, IOError, KeyError): + except (OSError, IOError): return '' + try: + return passwd_file[uid][0] + except KeyError: + return str(uid) + class GPU: def __init__(self, num, root, exclude_zero_memory_users=False): |