Skip to content

Commit 3b0705e

Browse files
committed
update client linux and psutil
1 parent 2b799a5 commit 3b0705e

2 files changed

Lines changed: 73 additions & 50 deletions

File tree

‎clients/client-linux.py‎

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,36 +150,55 @@ def is_generic_cpu_model(value):
150150
v = normalize_cpu_model(value).lower().replace('-', '').replace('_', '').replace(' ', '')
151151
return v in ('', 'unknown', 'x8664', 'amd64', 'i386', 'i686', 'aarch64', 'arm64') or v.startswith('armv')
152152

153-
def get_cpu_model():
153+
def get_lscpu_info():
154+
result = {}
155+
try:
156+
output = subprocess.check_output(['lscpu'], stderr=subprocess.DEVNULL, timeout=2).decode(errors='ignore')
157+
for line in output.splitlines():
158+
if ':' not in line:
159+
continue
160+
key, value = line.split(':', 1)
161+
key = key.strip().lower()
162+
value = normalize_cpu_model(value)
163+
if value and key not in result:
164+
result[key] = value
165+
except Exception:
166+
pass
167+
return result
168+
169+
def get_cpuinfo_values():
170+
result = {}
154171
try:
155-
fallback = ""
156172
with open('/proc/cpuinfo') as f:
157173
for line in f:
158174
if ':' not in line:
159175
continue
160176
key, value = line.split(':', 1)
161177
key = key.strip().lower()
162178
value = normalize_cpu_model(value)
163-
if not value:
164-
continue
165-
if key == 'model name':
166-
return value
167-
if key in ('hardware', 'processor') and not value.isdigit() and not fallback:
168-
fallback = value
169-
if fallback:
170-
return fallback
179+
if value and key not in result:
180+
result[key] = value
171181
except Exception:
172182
pass
173-
for cmd in ('sysctl -n machdep.cpu.brand_string', 'sysctl -n hw.model'):
174-
try:
175-
value = subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, timeout=2).decode().strip()
176-
value = normalize_cpu_model(value)
177-
if value:
178-
return value
179-
except Exception:
180-
pass
181-
value = normalize_cpu_model(platform.processor())
182-
return "" if is_generic_cpu_model(value) else value
183+
return result
184+
185+
def get_cpu_model():
186+
cpuinfo = get_cpuinfo_values()
187+
lscpu = get_lscpu_info()
188+
for value in (
189+
cpuinfo.get('model name'),
190+
lscpu.get('model name'),
191+
cpuinfo.get('hardware'),
192+
cpuinfo.get('processor'),
193+
platform.processor(),
194+
):
195+
value = normalize_cpu_model(value)
196+
if value and not value.isdigit() and not is_generic_cpu_model(value):
197+
return value
198+
vendor = normalize_cpu_model(lscpu.get('vendor id') or cpuinfo.get('vendor_id'))
199+
if vendor:
200+
return vendor
201+
return normalize_cpu_model(lscpu.get('architecture') or platform.machine() or platform.processor())
183202

184203
def liuliang():
185204
NET_IN = 0

‎clients/client-psutil.py‎

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import json
2929
import errno
3030
import psutil
31-
import subprocess
3231
import threading
3332
import platform
3433
from queue import Queue
@@ -103,36 +102,41 @@ def is_generic_cpu_model(value):
103102
v = normalize_cpu_model(value).lower().replace('-', '').replace('_', '').replace(' ', '')
104103
return v in ('', 'unknown', 'x8664', 'amd64', 'i386', 'i686', 'aarch64', 'arm64') or v.startswith('armv')
105104

105+
def get_platform_cpu_vendor():
106+
values = [
107+
platform.processor(),
108+
getattr(platform.uname(), 'processor', ''),
109+
platform.machine(),
110+
getattr(platform.uname(), 'machine', ''),
111+
platform.platform(),
112+
]
113+
text = " ".join(normalize_cpu_model(v).lower() for v in values)
114+
if 'genuineintel' in text:
115+
return 'GenuineIntel'
116+
if 'authenticamd' in text:
117+
return 'AuthenticAMD'
118+
if 'intel' in text:
119+
return 'Intel'
120+
if 'amd' in text:
121+
return 'AMD'
122+
if sys.platform.startswith('darwin') and platform.machine().lower() in ('arm64', 'aarch64'):
123+
return 'Apple'
124+
if any(token in text for token in ('aarch64', 'arm64', 'armv7', 'armv8', ' arm ')):
125+
return 'ARM'
126+
return ''
127+
128+
def get_platform_cpu_arch():
129+
return normalize_cpu_model(platform.machine() or platform.processor() or platform.architecture()[0])
130+
106131
def get_cpu_model():
107-
try:
108-
fallback = ""
109-
with open('/proc/cpuinfo') as f:
110-
for line in f:
111-
if ':' not in line:
112-
continue
113-
key, value = line.split(':', 1)
114-
key = key.strip().lower()
115-
value = normalize_cpu_model(value)
116-
if not value:
117-
continue
118-
if key == 'model name':
119-
return value
120-
if key in ('hardware', 'processor') and not value.isdigit() and not fallback:
121-
fallback = value
122-
if fallback:
123-
return fallback
124-
except Exception:
125-
pass
126-
for cmd in ('sysctl -n machdep.cpu.brand_string', 'sysctl -n hw.model'):
127-
try:
128-
value = subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, timeout=2).decode().strip()
129-
value = normalize_cpu_model(value)
130-
if value:
131-
return value
132-
except Exception:
133-
pass
134-
value = normalize_cpu_model(platform.processor())
135-
return "" if is_generic_cpu_model(value) else value
132+
for value in (platform.processor(), getattr(platform.uname(), 'processor', '')):
133+
value = normalize_cpu_model(value)
134+
if value and not is_generic_cpu_model(value):
135+
return value
136+
vendor = normalize_cpu_model(get_platform_cpu_vendor())
137+
if vendor:
138+
return vendor
139+
return get_platform_cpu_arch()
136140

137141
def liuliang():
138142
NET_IN = 0

0 commit comments

Comments
 (0)