modif du jour

This commit is contained in:
Tenzing Kandang 2026-02-10 16:45:12 +01:00
parent 3da68b1079
commit f5a4b58668
5 changed files with 49 additions and 110 deletions

7
README.md Normal file
View File

@ -0,0 +1,7 @@
#GRABBER
Grabber is a script that "grabs" informations about the computer and puts them in a folder for ease of access.
##How to run a server
`uvicorn app:app --reload --host 0.0.0.0 --port 8000`

59
app.py
View File

@ -2,36 +2,36 @@ from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from sqlmodel import Field, Session, SQLModel, create_engine, select
from sqlmodel import SQLModel, Session, create_engine, select
import json import json
from models import ordinateur from models import ordinateur
app = FastAPI() app = FastAPI()
#ordi1 = ordinateur()
ordi1 = ordinateur()
app.mount("/static", StaticFiles(directory="static"), name="static") app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory="templates")
# Code above omitted #sqlitemodel
sqlite_file_name = "mydb.db"
sqlite_url = f"sqlite:///mydb.db"
sqlite_file_name = "database.db" engine = create_engine(sqlite_url)
sqlite_url = f"sqlite:///{sqlite_file_name}"
connect_args = {"check_same_thread": False} SQLModel.metadata.create_all(engine)
engine = create_engine(sqlite_url, connect_args=connect_args)
# Code below omitted @app.get("/ordi/{ordi_id}", response_class=HTMLResponse)
async def get_ordi1_info(request: Request, ordi_id: int):
@app.get("/ordi1", response_class=HTMLResponse) with Session(engine) as session:
async def get_ordi1_info(request: Request): statement= select(ordinateur).where(ordinateur.id == ordi_id)
this_ordi = session.exec(statement).first()
return templates.TemplateResponse( return templates.TemplateResponse(
request=request, name="item.html", context={"ordi": ordi1} request=request, name="item.html", context={"ordi": this_ordi}
) )
@app.post("/endpoint") @app.post("/endpoint")
async def receive_info(request: Request): async def receive_info(request: Request):
# Lire le body brut # Lire le body brut
@ -46,11 +46,19 @@ async def receive_info(request: Request):
raise HTTPException(status_code=400, detail="Invalid JSON") raise HTTPException(status_code=400, detail="Invalid JSON")
# Debug # Debug
hardware = data.get('HARDWARE',{}) hardware = data.get('HARDWARE',{})
software = data.get('SOFTWARE',{}) software = data.get('SOFTWARE',{})
with Session(engine) as session:
statement = select(ordinateur).where(ordinateur.mac_address == hardware.get("mac_address", ""))
ordi1 = session.exec(statement).first()
is_new = False
if not ordi1:
ordi1 = ordinateur()
is_new = True
print("Infos reçues :", data) print("Infos reçues :", data)
ordi1.mb_serial = hardware.get("mb_serial", "") ordi1.mb_serial = hardware.get("mb_serial", "")
ordi1.hostname = hardware.get("hostname", "") ordi1.hostname = hardware.get("hostname", "")
@ -69,7 +77,7 @@ async def receive_info(request: Request):
ordi1.cpu_threads_nb = hardware.get("cpu_threads_nb", "") ordi1.cpu_threads_nb = hardware.get("cpu_threads_nb", "")
ordi1.ram_number = hardware.get("ram_number", "") ordi1.ram_number = hardware.get("ram_number", "")
ordi1.ram_slots_nb = hardware.get("ram_slots_nb", "") ordi1.ram_slots_nb = hardware.get("ram_slots_nb", "")
ordi1.mac_adresse = hardware.get("mac_adress", "") ordi1.mac_address = hardware.get("mac_address", "")
ordi1.os = software.get("os", "") ordi1.os = software.get("os", "")
ordi1.arch = software.get("arch", "") ordi1.arch = software.get("arch", "")
@ -77,16 +85,9 @@ async def receive_info(request: Request):
ordi1.wm = software.get("wm", "") ordi1.wm = software.get("wm", "")
ordi1.kernel = software.get("kernel", "") ordi1.kernel = software.get("kernel", "")
#Test if is_new:
print(f"Le serial de la mb est {ordi1.mb_serial}") session.add(ordi1)
print(f"Hostname est {ordi1.hostname}") session.commit()
print(f"Your cpu is {ordi1.cpu}") #session.close()
print(f"Le id de ce cpu est {ordi1.cpu_id}")
print(f"le memory en megabytes {ordi1.memory_mb}")
print(f"Voici numéro de ram {ordi1.ram_number}")
return ({"status": "ok"}) return ({"status": "ok"})
#@app.get("/ordi1")
#async def get_ordi1_info():
# return ordi1

View File

@ -4,16 +4,6 @@ SUCCESS_LOG=/var/log/grabber/grabber-success.log
ERROR_LOG=/var/log/grabber/grabber-error.log ERROR_LOG=/var/log/grabber/grabber-error.log
SUM="$DIR/summary.txt" SUM="$DIR/summary.txt"
#lspci -nn > $DIR/lspci.cmd
#lsusb > $DIR/lsusb.cmd
#apt list --installed > $DIR/apt.cmd
#systemd-analyze > $DIR/systemd-analyze.cmd
#systemd-analyze blame | head -n 10 > $DIR/systemd-blame.cmd
#lscpu > $DIR/lscpu.cmd
#lshw-gtk > $DIR/lshw-gtk.cmd
#inxi > $DIR/inxi.cmd
#lsmem > $DIR/lsmem.cmd
HOSTNAME=$(hostname) HOSTNAME=$(hostname)
CPU=$(lscpu | grep 'Nom de modèle' | awk -F: '{print $2}' | xargs) CPU=$(lscpu | grep 'Nom de modèle' | awk -F: '{print $2}' | xargs)
MEM=$(free -m | awk '/Mem:/ {print $2}') MEM=$(free -m | awk '/Mem:/ {print $2}')
@ -36,8 +26,7 @@ ARCH=$(uname -m)
DESKTOP=$XDG_CURRENT_DESKTOP DESKTOP=$XDG_CURRENT_DESKTOP
WM=$XDG_SESSION_TYPE WM=$XDG_SESSION_TYPE
KERNEL=$(uname -r) KERNEL=$(uname -r)
MAC_ADRESSE=$(ip link | awk '/ether/ {print $2; exit}') MAC_ADDRESS=$(cat /sys/class/net/$(ls /sys/class/net | grep -vE '^(lo|docker|veth|br)' | head -n 1)/address)
##########SIMPLELOCAL####################
# Construire le JSON # Construire le JSON
json_data=$(jq -n \ json_data=$(jq -n \
@ -63,7 +52,7 @@ json_data=$(jq -n \
--arg desktop "$DESKTOP" \ --arg desktop "$DESKTOP" \
--arg wm "$WM" \ --arg wm "$WM" \
--arg kernel "$KERNEL" \ --arg kernel "$KERNEL" \
--arg mac_adresse "$MAC_ADRESSE" \ --arg mac_address "$MAC_ADDRESS" \
'{ '{
HARDWARE: { HARDWARE: {
hostname:$host, hostname:$host,
@ -83,7 +72,7 @@ json_data=$(jq -n \
cpu_threads_nb:$cpu_threads_nb, cpu_threads_nb:$cpu_threads_nb,
ram_number:$ram_number, ram_number:$ram_number,
ram_slots_nb:$ram_slots_nb, ram_slots_nb:$ram_slots_nb,
mac_adresse:$mac_adresse, mac_address:$mac_address,
}, },
SOFTWARE: { SOFTWARE: {
@ -101,7 +90,6 @@ curl -X POST http://$1:8000/endpoint \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "$json_data" -d "$json_data"
########################################
# Affichage du texte de démarrage # Affichage du texte de démarrage
echo " " echo " "
@ -201,15 +189,7 @@ hardware() {
echo "CPU_THREADS_NUMBER=$CPU_THREADS_NUMBER" echo "CPU_THREADS_NUMBER=$CPU_THREADS_NUMBER"
echo "RAM_NUMBER=$RAM_NUMBER" echo "RAM_NUMBER=$RAM_NUMBER"
echo "RAM_SLOTS_NUMBER=$RAM_SLOTS_NUMBER" echo "RAM_SLOTS_NUMBER=$RAM_SLOTS_NUMBER"
echo "MAC_ADRESSE=$MAC_ADRESSE" echo "MAC_ADDRESS=$MAC_ADDRESS"
sizes=$(lsblk -dnb |grep -v loop |grep -v boot |tr -s " " |cut -d \ -f4)
STOCKAGE_TOTAL=0
for SIZE in ${sizes[@]};do
STOCKAGE_TOTAL+=$SIZE
done
STOCKAGE_TOTAL=$(numfmt --to iec $STOCKAGE_TOTAL)
echo "STOCKAGE_TOTAL=$STOCKAGE_TOTAL"
} >$SUM } >$SUM
@ -221,7 +201,6 @@ software() {
echo "DESKTOP=$XDG_CURRENT_DESKTOP" echo "DESKTOP=$XDG_CURRENT_DESKTOP"
echo "WM=$XDG_SESSION_TYPE" echo "WM=$XDG_SESSION_TYPE"
echo "KERNEL=$(uname -r)" echo "KERNEL=$(uname -r)"
echo "MAC_ADRESSE=$MAC_ADRESSE"
} >>$SUM } >>$SUM
hardware hardware

BIN
grabberman.db Normal file

Binary file not shown.

View File

@ -1,7 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import configparser import configparser
import requests import requests
from sqlmodel import Field, Session, SQLModel, create_engine, select from sqlmodel import Field, SQLModel
class ordinateur(SQLModel, table=True): class ordinateur(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True) id: int | None = Field(default=None, primary_key=True)
@ -22,65 +22,17 @@ class ordinateur(SQLModel, table=True):
cpu_threads_nb: str = Field(index=True) cpu_threads_nb: str = Field(index=True)
ram_number: str = Field(index=True) ram_number: str = Field(index=True)
ram_slots_nb: str = Field(index=True) ram_slots_nb: str = Field(index=True)
stockage_total: str = Field(index=True) mac_address: str = Field(index=True)
mac_adresse: str = Field(index=True)
os: str = Field(index=True) os: str = Field(index=True)
arch: str = Field(index=True) arch: str = Field(index=True)
desktop: str = Field(index=True) desktop: str = Field(index=True)
wm: str = Field(index=True) wm: str = Field(index=True)
kernel: str = Field(index=True) kernel: str = Field(index=True)
class employee(SQLModel, tablel=True):
''' id: int | None = Field(default=None, primary_key=True)
def reload(self): first_name: str = Field(index=True)
r = requests.get("http://localhost:8000/summary.txt") family_name: str = Field(index=True)
r.raise_for_status()
print(type(r.content.decode("utf-8")))
sum=configparser.ConfigParser()
sum.read_string(r.content.decode("utf-8"))
#sum.read("/opt/grabber/summary.txt")
if "CPU" in sum['HARDWARE']:
self.cpu = sum['HARDWARE']['CPU']
if "CPU_SERIAL" in sum['HARDWARE']:
self.cpu_serial = sum['HARDWARE']['CPU_SERIAL']
if "CPU_FREQ" in sum['HARDWARE']:
self.cpu_freq = sum['HARDWARE']['CPU_FREQ']
if "CPU_ID" in sum['HARDWARE']:
self.cpu_id = sum['HARDWARE']['CPU_ID']
if "RAM" in sum['HARDWARE']:
self.ram = sum['HARDWARE']['RAM']
if "RAM_GEN" in sum['HARDWARE']:
self.ram_gen = sum['HARDWARE']['RAM_GEN']
if "STOCKAGE_TOTAL" in sum['HARDWARE']:
self.stockage_total = sum['HARDWARE']['STOCKAGE_TOTAL']
if "OS" in sum['SOFTWARE']:
self.os = sum['SOFTWARE']['OS']
if "ARCH" in sum['SOFTWARE']:
self.arch = sum['SOFTWARE']['ARCH']
if "DESKTOP" in sum['SOFTWARE']:
self.desktop = sum['SOFTWARE']['DESKTOP']
if "WM" in sum['SOFTWARE']:
self.wm = sum['SOFTWARE']['WM']
if "KERNEL" in sum['SOFTWARE']:
self.kernel = sum['SOFTWARE']['KERNEL']
def __init__(self):
self.reload()
'''
def shutdown(): def shutdown():
return return