La source des données
Bien sur, c’est à l’ordinateur des fournirs, j’ai un programme que j’utilise qui couvre tout les informations essentiels, les avantages est qu’il est gratuit et que les données sont disponibles rapidement et simplement. Les données sont enregistré dans la base de registre, alors je n’ai besoin que de lire la base du registre pour obtenir mes informations.
Après il faut les communiquer au Raspberry. J’utilise dont un principe simple, un serveur Web. Comme ça les donnés sont disponibles et le Raspberry viendra les chercher de lui-même. Accessoirement, le serveur Web peut fournir des fichiers depuis le dossier pcstats/pc/web/
.
pcstats/pc/pcstats.py
#!/usr/bin/python3
import urllib.parse as urlparse
import http.server
import socketserver
import os
import subprocess
import configparser
import json
import datetime
import time
from threading import Timer
from urllib.parse import parse_qs
from os import path
from datetime import datetime as dt
from os import listdir
from os.path import isfile, join, isdir
import winreg
config = configparser.ConfigParser()
config.read('config.ini')
WEBPATH=config['system']['path']+"web"
os.chdir(WEBPATH)
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
pathSplit = self.path.split("?")
pathSection = pathSplit[0].split("/")
if self.path == '/':
self.path = '/index.html'
print(self.path);
return http.server.SimpleHTTPRequestHandler.do_GET(self)
elif path.exists(WEBPATH+pathSplit[0]) is True:
self.path = pathSplit[0]
return http.server.SimpleHTTPRequestHandler.do_GET(self)
elif pathSection[1] == "api":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
wanted=pathSection[2]
outputJson={"result":str(self.getRegistry(config['monitors'][wanted+"_type"],config['monitors'][wanted]))}
return self.wfile.write(bytes(json.dumps(outputJson), "utf-8"))
elif pathSection[1] == "stats":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
outputJson={
"pcname":str(self.getRegistry(config['monitors']["pcname_type"],config['monitors']["pcname"])),
"cpuname":str(self.getRegistry(config['monitors']["cpuname_type"],config['monitors']["cpuname"])),
"cpuspeed":str(self.getRegistry(config['monitors']["cpuspeed_type"],config['monitors']["cpuspeed"])),
"cpubus":str(self.getRegistry(config['monitors']["cpubus_type"],config['monitors']["cpubus"])),
"cpumulti":str(self.getRegistry(config['monitors']["cpumulti_type"],config['monitors']["cpumulti"])),
"cpuload":str(self.getRegistry(config['monitors']["cpuload_type"],config['monitors']["cpuload"])),
"cputemp":str(self.getRegistry(config['monitors']["cputemp_type"],config['monitors']["cputemp"])),
"cpupower":str(self.getRegistry(config['monitors']["cpupower_type"],config['monitors']["cpupower"])),
"fancpu":str(self.getRegistry(config['monitors']["fancpu_type"],config['monitors']["fancpu"])),
"fansys1":str(self.getRegistry(config['monitors']["fansys1_type"],config['monitors']["fansys1"])),
"fansys2":str(self.getRegistry(config['monitors']["fansys2_type"],config['monitors']["fansys2"])),
"fangpu":str(self.getRegistry(config['monitors']["fangpu_type"],config['monitors']["fangpu"])),
"sysname":str(self.getRegistry(config['monitors']["sysname_type"],config['monitors']["sysname"])),
"syspower":str(self.getRegistry(config['monitors']["syspower_type"],config['monitors']["syspower"])),
"systemp1":str(self.getRegistry(config['monitors']["systemp1_type"],config['monitors']["systemp1"])),
"systemp2":str(self.getRegistry(config['monitors']["systemp2_type"],config['monitors']["systemp2"])),
"ramname":str(self.getRegistry(config['monitors']["ramname_type"],config['monitors']["ramname"])),
"ramload":str(self.getRegistry(config['monitors']["ramload_type"],config['monitors']["ramload"])),
"ramuse":str(self.getRegistry(config['monitors']["ramuse_type"],config['monitors']["ramuse"])),
"ramfree":str(self.getRegistry(config['monitors']["ramfree_type"],config['monitors']["ramfree"])),
"ramtotal":str(self.getRegistry(config['monitors']["ramtotal_type"],config['monitors']["ramtotal"])),
"gpuname":str(self.getRegistry(config['monitors']["gpuname_type"],config['monitors']["gpuname"])),
"gpucorespeed":str(self.getRegistry(config['monitors']["gpucorespeed_type"],config['monitors']["gpucorespeed"])),
"gpuvideospeed":str(self.getRegistry(config['monitors']["gpuvideospeed_type"],config['monitors']["gpuvideospeed"])),
"gpupower":str(self.getRegistry(config['monitors']["gpupower_type"],config['monitors']["gpupower"])),
"gpuload":str(self.getRegistry(config['monitors']["gpuload_type"],config['monitors']["gpuload"])),
"gputemp":str(self.getRegistry(config['monitors']["gputemp_type"],config['monitors']["gputemp"])),
"vramname":str(self.getRegistry(config['monitors']["vramname_type"],config['monitors']["vramname"])),
"vramload":str(self.getRegistry(config['monitors']["vramload_type"],config['monitors']["vramload"])),
"vramuse":str(self.getRegistry(config['monitors']["vramuse_type"],config['monitors']["vramuse"])),
"vramfree":str(self.getRegistry(config['monitors']["vramfree_type"],config['monitors']["vramfree"])),
"vramtotal":str(self.getRegistry(config['monitors']["vramtotal_type"],config['monitors']["vramtotal"])),
"vramspeed":str(self.getRegistry(config['monitors']["vramspeed_type"],config['monitors']["vramspeed"]))
}
return self.wfile.write(bytes(json.dumps(outputJson), "utf-8"))
else:
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes('Document requested is not found.', "utf-8"))
return
def getData(self,data,url):
parsed = urlparse.urlparse("http://localhost"+url)
try:
return str(parse_qs(parsed.query)[data]).replace("['","").replace("']","")
except:
return ""
pass
def getRegistry(self,rType,rKey):
keyPath = r"HARDWARE\\SIV\\"+rType
try:
registry_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, keyPath, 0, winreg.KEY_READ)
value, regtype = winreg.QueryValueEx(registry_key, rKey)
winreg.CloseKey(registry_key)
return value
except WindowsError:
return None
pcStats = MyHttpRequestHandler
pcStatsServer = socketserver.TCPServer(("0.0.0.0", int(config['ctrl']['port'])), pcStats)
pcStatsServer.serve_forever()
Il faut aussi un fichier pour la configuration.
pcstats/pc/config.ini
; PCStats Configuration file
;
[system]
;Path where are stored PCStats files, with "\" at the end
path=D:\CODES\PYTHON\pcstats\pc\
[ctrl]
;Password for safety control, only used for reboot/poweroff system over Web UI
pass=42758
;Port for the Web access, 9000 is the default
port=9000
[monitors]
#Require SIV
pcname=Nodename
pcname_type=H
cpuname=CPU-0-NAM
cpuname_type=H
cpuspeed=CPU-0-P
cpuspeed_type=K
cpubus=CPU-0-CLK
cpubus_type=K
cpumulti=CPU-0-MLT
cpumulti_type=K
cpuload=CPU-T-L
cpuload_type=L
cputemp=CPU-0-T
cputemp_type=T
cpupower=CPU-0-PWR
cpupower_type=P
fancpu=M-F1
fancpu_type=C
fansys1=M-F2
fansys1_type=C
fansys2=M-F3
fansys2_type=C
fangpu=GPU-0-F
fangpu_type=C
sysname=Motherboard
sysname_type=H
syspower=SYSPWRUSE
syspower_type=P
systemp1=M-TA
systemp1_type=T
systemp2=M-TB
systemp2_type=T
ramname=SYSMEMSUM
ramname_type=H
ramload=SYSRAMPOT
ramload_type=H
ramuse=SYSRAMUSE
ramuse_type=H
ramfree=SYSRAMFRE
ramfree_type=H
ramtotal=SYSRAMTOT
ramtotal_type=H
gpuname=GPU-0-NAM
gpuname_type=H
gpucorespeed=GPU-0-C
gpucorespeed_type=K
gpuvideospeed=CPU-0-V
gpuvideospeed_type=K
gpupower=GPU-0-PWR
gpupower_type=P
gpuload=GPU-T-L
gpuload_type=L
gputemp=GPU-0-T
gputemp_type=T
vramname=
vramname_type=
vramload=GPU-T-P
vramload_type=L
vramuse=GPU-T-B
vramuse_type=L
vramfree=
vramfree_type=
vramtotal=GPU-T-B
vramtotal_type=L
vramspeed=GPU-0-M
vramspeed_type=K
Il faut l’exécuter, pour ce faire il y a deux fichiers. Le premier est le script de lancement en « .bat », il permet de lancer l’application, mais vu le code Python et les caprices de Windows, vous aurez une console affiché, pratique par contre pour le debug. Inutile de tenter d’utiliser « pythonw.exe »; le script ne ce lance pas.
Le second fichier est un script en « .vbs », il perme de lancer la console en arrière-plan, bref, elle sera caché.
pcstats/pc/run.bat
python.exe pcstats.py
pcstats/pc/run.vbs
Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "run.bat" & Chr(34), 0
Set WinScriptHost = Nothing
Vous avez alors votre service pour afficher vos informations systèmes.