forked from tuxedocomputers/tuxedo-systeminfos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysteminfos.py
299 lines (259 loc) · 13.1 KB
/
systeminfos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
serverURI: str = "https://www.tuxedocomputers.com/tuxedosysteminfos/systeminfo.php"
import datetime
import os
import platform
import subprocess
import xml.etree.ElementTree as ET
from xml.dom import minidom
from xml.etree.ElementTree import tostring
import re
import chardet
import distro
import usb.core
from pylspci.parsers import VerboseParser
import psutil
from dmidecode import DMIDecode
def getRAM():
r = {}
RAMs = DMIDecode().get(17)
for RAM in RAMs:
r[RAM["Bank Locator"]]= {}
r[RAM["Bank Locator"]]['Size'] = str(RAM["Size"])
r[RAM["Bank Locator"]]['ConfiguredVoltage'] = str(RAM['Configured Voltage'])
r[RAM["Bank Locator"]]['FormFactor'] = str(RAM['Form Factor'])
r[RAM["Bank Locator"]]['Type'] = str(RAM['Type'])
r[RAM["Bank Locator"]]['TypeDetail'] = str(RAM['Type Detail'])
r[RAM["Bank Locator"]]['PartNumber'] = str(RAM['Part Number'])
return r
def getNetBasics():
NetworkInfo = {}
NetIf = psutil.net_if_addrs()
statsIf = psutil.net_if_stats()
for Interface in NetIf.keys():
NetworkInfo[Interface] = {}
# Up is not the same as connected (have to learn it the hard way)
NetworkInfo[Interface]["Up"] = str(statsIf[Interface].isup)
NetworkInfo[Interface]["duplex"] = str(statsIf[Interface].duplex)
NetworkInfo[Interface]["mtu"] = str(statsIf[Interface].mtu)
NetworkInfo[Interface]["speed"] = str(statsIf[Interface].speed)
return NetworkInfo
def getPCI():
lspci = VerboseParser()
pciData = lspci.run()
return pciData
def secs2hours(secs):
mm, ss = divmod(secs, 60)
hh, mm = divmod(mm, 60)
return "%d:%02d:%02d" % (hh, mm, ss)
def getDisks():
Disks = {}
for mount in psutil.disk_partitions():
Disks[mount.device] = {}
Disks[mount.device]["mountpoints"] = {}
for mount in psutil.disk_partitions():
Disks[mount.device]["fstype"] = mount.fstype
Disks[mount.device]["mountpoints"][mount.mountpoint] = mount.opts
return Disks
def getUSB():
usbDevsList = {}
usbDevs = usb.core.find(find_all=True)
for dev in usbDevs:
usbDevsList[hex(dev.idVendor) + ":" + hex(dev.idProduct)] = {}
usbDevsList[hex(dev.idVendor) + ":" + hex(dev.idProduct)]["Bus"] = str(dev.bus)
usbDevsList[hex(dev.idVendor) + ":" + hex(dev.idProduct)]["AddrOnBus"] = str(dev.address)
usbDevsList[hex(dev.idVendor) + ":" + hex(dev.idProduct)]["serial_number"] = str(dev.serial_number)
usbDevsList[hex(dev.idVendor) + ":" + hex(dev.idProduct)]["product"] = str(dev.product)
usbDevsList[hex(dev.idVendor) + ":" + hex(dev.idProduct)]["manufacturer"] = str(dev.manufacturer)
return usbDevsList
def getDKMS():
DKMSstats = {}
dmksout = subprocess.check_output(['dkms', 'status'])
dmksoutEnc = chardet.detect(dmksout)["encoding"]
dmksout = str(dmksout, dmksoutEnc)
for line in dmksout.splitlines():
line = line.strip()
infos = line.split(",")
DKMSstats[infos[0]] = {}
DKMSstats[infos[0]]["Version"] = infos[1].strip()
DKMSstats[infos[0]]["KernelVersion"] = infos[2].strip()
DKMSstats[infos[0]]["architecture"] = infos[3].split(":")[0].strip()
DKMSstats[infos[0]]["status"] = infos[3].split(":")[1].strip()
return DKMSstats
def getDmesg():
dmesgout = subprocess.check_output(['dmesg', '--ctime'])
dmesgout = str(dmesgout, chardet.detect(dmesgout)["encoding"]).splitlines()
log = {}
for line in dmesgout:
dtsave = line[line.find("[") + 1:line.find("]")]
msgsave = line.split("] ",1)[1]
log[dtsave] = msgsave
return log
def main():
getUSB()
print(
"Bitte beachten Sie, dass wir ohne Ticketnummer, Ihr Anliegen nicht bearbeiten können. / We cannot proceed your inquire without ticket number!")
print(
"Um eine Ticketnummer zu erhalten, schreiben Sie uns eine Mail an tux@tuxedocomputer.com mit Ihrem Anliegen. / To get an ticket number you can contact us by mail to tux@tuxedocomputers.com")
IMNr = input("Wie lautet Ihre Ticketnummer? Mit [ENTER] bestätigen / What is your ticket number?: ")
print("")
print("Okay, we are working with ticket %s!" % IMNr)
MotherBoard = {}
with open('/sys/devices/virtual/dmi/id/board_vendor') as f:
MotherBoard["vendor"] = f.readline().rstrip()
with open('/sys/devices/virtual/dmi/id/board_name') as f:
MotherBoard["name"] = f.readline().rstrip()
with open('/sys/devices/virtual/dmi/id/bios_version') as f:
MotherBoard["bios_version"] = f.readline().rstrip()
with open('/sys/devices/virtual/dmi/id/bios_vendor') as f:
MotherBoard["bios_vendor"] = f.readline().rstrip()
LinuxDistro = distro.linux_distribution()[0]
LinuxDistroVersion = distro.linux_distribution()[1]
Kernel = platform.platform()
pciDevs = getPCI()
usbDevs = getUSB()
installedPKG = {}
PKGMgrCfg = {}
#ToDo: need to be done for other distro
if distro.linux_distribution(full_distribution_name=False)[0] == "arch":
PacOut = subprocess.check_output(['pacman', '-Q', "-e"])
PacOutEnc = chardet.detect(PacOut)["encoding"]
PacOutStr = str(PacOut, PacOutEnc)
for line in PacOutStr.splitlines():
lineParts = line.split()
installedPKG[lineParts[0]] = lineParts[1]
with open("/etc/pacman.conf", "r") as pacmanCFG:
readConfig = ""
for line in pacmanCFG.readlines():
if not line.startswith("#"):
readConfig = readConfig + line
readConfig = re.sub(' +', ' ',readConfig)
readConfig = os.linesep.join([s for s in readConfig.splitlines() if s])
PKGMgrCfg["pacman.conf"] = readConfig
IncludeList = []
for line in readConfig.splitlines():
if line.startswith("Include"):
IncludeList.append(line[line.index("/"):])
IncludeList = list(dict.fromkeys(IncludeList))
for file in IncludeList:
with open(file, "r") as cfgFile:
readConfig = ""
for line in cfgFile.readlines():
if not line.startswith("#"):
readConfig = readConfig + line
readConfig = re.sub(' +', ' ', readConfig)
readConfig = os.linesep.join([s for s in readConfig.splitlines() if s])
PKGMgrCfg[file] = readConfig
TuxReport = ET.Element("TuxReport", TicketID=IMNr)
xml_LinuxDist = ET.SubElement(TuxReport, "LinuxDistro", name=LinuxDistro, version=LinuxDistroVersion)
xml_instSoftware = ET.SubElement(xml_LinuxDist, "InstalledSoftware")
xml_LinuxKernel = ET.SubElement(xml_LinuxDist, "LinuxKernel", VersionString=Kernel)
xml_dmesg = ET.SubElement(xml_LinuxKernel, "dmesg")
for when, what in getDmesg().items():
ET.SubElement(xml_dmesg, "LogLine", DateAndTime=str(when)).text = what
xml_DKMS = ET.SubElement(xml_LinuxKernel, "DKMS")
xml_System = ET.SubElement(TuxReport, "System",boottime=datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
xml_Power = ET.SubElement(xml_System, "Power")
battery = psutil.sensors_battery()
if battery.power_plugged:
xml_AC = ET.SubElement(xml_Power, "AC-Power")
if not battery.percent == None:
if not battery.secsleft == psutil.POWER_TIME_UNLIMITED:
xml_battery = ET.SubElement(xml_Power, "battery-Power", charge=str(battery.percent), timeleft=secs2hours(battery.secsleft))
else:
xml_battery = ET.SubElement(xml_Power, "battery-Power", charge=str(battery.percent))
xml_Network = ET.SubElement(xml_System, "Network")
xmlc_NetworkUp = ET.Comment("for a network interface the status Up does not mean that it is connected to anything. It just means it would accept connections.")
xmlc_NetworkSpeed = ET.Comment("the NIC speed expressed in megabits, if it cannot be determined it will be set to 0.")
xml_Network.insert(0,xmlc_NetworkUp)
xml_Network.insert(1, xmlc_NetworkSpeed)
xml_Disks = ET.SubElement(xml_System, "Disks")
xml_pciBus = ET.SubElement(xml_System, "PCI")
xml_usbBus = ET.SubElement(xml_System, "USB")
xml_PKGMgr = ET.SubElement(xml_LinuxDist, "PKGManager")
for filename, cont in PKGMgrCfg.items():
xml_PKFcfgFile = ET.SubElement(xml_PKGMgr, "cfg-file", filename=filename)
xml_PKFcfgFile.text = cont
for modName, info in getDKMS().items():
xml_DKMSMod = ET.SubElement(xml_DKMS, modName, info)
for CardName, info in getNetBasics().items():
xml_Networkcard = ET.SubElement(xml_Network, CardName, info)
for pkg in installedPKG.items():
ET.SubElement(xml_instSoftware, "pkg", version=pkg[1]).text = pkg[0]
xml_MotherBoard = ET.SubElement(xml_System, "MotherBoard", MotherBoard)
xml_CPU = ET.SubElement(xml_MotherBoard, "CPU", count=str(psutil.cpu_count(logical=False)),
logicalcount=str(psutil.cpu_count(logical=True)), type=DMIDecode().cpu_type())
xml_RAM = ET.SubElement(xml_MotherBoard, "RAM", size=str(psutil.virtual_memory().total), free=str(psutil.virtual_memory().free), available=str(psutil.virtual_memory().available))
for dev in pciDevs:
xml_pciDev = ET.SubElement(xml_pciBus, "dev", id=dev.device.name, slot=str(dev.slot), vendor=str(dev.vendor),
driver=str(dev.driver), name=dev.device.name, revision=str(dev.revision))
for km in dev.kernel_modules:
ET.SubElement(xml_pciDev, "kernel_module").text = km
for devID, dev in usbDevs.items():
localDict = dev
localDict["id"] = devID
xml_usbDev = ET.SubElement(xml_usbBus, "dev", localDict)
cpuUseP = psutil.cpu_percent(interval=2, percpu=True)
cpuTimesP = psutil.cpu_times_percent(interval=2, percpu=True)
xmlc_CPUinfo = ET.Comment("this values are collected by 2 commands. both blocking for 2 seconds. It is not at the same time collected.")
xmlc_CPUinfo2 = ET.Comment("The value ending with P are % ")
xmlc_CPUinfo3 = ET.Comment("The value ending with freq are MHz ")
xml_CPU.insert(0, xmlc_CPUinfo)
xml_CPU.insert(1, xmlc_CPUinfo2)
xml_CPU.insert(2, xmlc_CPUinfo3)
for CPU in range(psutil.cpu_count(logical=True)):
xml_CPUthread = ET.SubElement(xml_CPU, "LCPU", CPUUseP=str(cpuUseP[CPU]), userP=str(cpuTimesP[CPU].user),
idleP=str(cpuTimesP[CPU].idle), systemP=str(cpuTimesP[CPU].system),
iowaitP=str(cpuTimesP[CPU].iowait),currentfreq = str(psutil.cpu_freq(percpu=True)[CPU].current),
minfreq = str(psutil.cpu_freq(percpu=True)[CPU].min),
maxfreq = str(psutil.cpu_freq(percpu=True)[CPU].max))
for Disk, info in getDisks().items():
xml_Part = ET.SubElement(xml_Disks, "Part", path=Disk, fstype=info["fstype"])
for path, opts in info["mountpoints"].items():
xml_Mount = ET.SubElement(xml_Part, "Mount", path=path).text = opts
for Bank, info in getRAM().items():
info["Bank"]= str(Bank)
xml_RAMbank = ET.SubElement(xml_RAM, "stick", info)
tree = ET.ElementTree(TuxReport).getroot()
xmlstr = minidom.parseString(tostring(tree, encoding='utf8')).toprettyxml()
import tempfile, shutil,zipfile, hashlib
tmpdir = tempfile.mkdtemp()
try:
tmparchive = os.path.join(tmpdir, 'TuxReport.zip')
reportfile = os.path.join(tmpdir, 'TuxReport.xml')
text_file = open(reportfile, "w")
text_file.write(xmlstr)
text_file.close()
ziptext = "This file is for ticket %s!" % IMNr
ziptext = ziptext.encode()
with zipfile.ZipFile(tmparchive, "w", zipfile.ZIP_DEFLATED, allowZip64=True) as zf:
zf.comment = ziptext
zf.write(reportfile,"TuxReport.xml")
BLOCK_SIZE = 65536
file_hash = hashlib.sha3_512()
with open(tmparchive, 'rb') as f:
fb = f.read(BLOCK_SIZE)
while len(fb) > 0:
file_hash.update(fb)
fb = f.read(BLOCK_SIZE)
zipchecksum = file_hash.hexdigest()
import pycurl, certifi, urllib.parse
params = {"ticketnumber": IMNr, "SHA3-512": zipchecksum}
curl = pycurl.Curl()
curl.setopt(pycurl.VERBOSE, 0)
curl.setopt(pycurl.WRITEFUNCTION, lambda x: None)
curl.setopt(curl.URL, serverURI + '?' + urllib.parse.urlencode(params))
curl.setopt(curl.CAINFO, certifi.where())
curl.setopt(curl.POST, 1)
curl.setopt(curl.HTTPPOST, [("file", (curl.FORM_FILE, tmparchive))])
curl.setopt(pycurl.HTTPHEADER, ['Accept-Language: en'])
curl.setopt(pycurl.USERAGENT, "TUXEDO/PyHWReporter")
# print(serverURI + '?' + urllib.parse.urlencode(params))
curl.perform()
curl.close()
#The ZIP file is located locally on the hard disk in a temporary folder. For this purpose there is a checksum of the ZIP file in the variable zipchecksum.
#The folder is also deleted after these lines.
finally:
shutil.rmtree(tmpdir)
if __name__ == '__main__':
main()