-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoteur_linux.py
More file actions
78 lines (65 loc) · 2.94 KB
/
moteur_linux.py
File metadata and controls
78 lines (65 loc) · 2.94 KB
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
#!/usr/bin/env python3
import subprocess
from moteur_base import MoteurBase
class MoteurLinux(MoteurBase):
"""
Moteur de collecte de données pour les systèmes Linux (Debian, Ubuntu, etc.).
Utilise /proc, /etc et les commandes GNU/Linux.
"""
def obtenir_hostname(self) -> str:
"""Lit le hostname depuis /etc/hostname (spécifique Linux)."""
with open("/etc/hostname") as f:
return f.read().strip()
def obtenir_ram(self) -> str:
"""Lit la RAM depuis /proc/meminfo (spécifique Linux)."""
with open("/proc/meminfo") as f:
for ligne in f:
if "MemTotal" in ligne:
return ligne.strip()
return "Inconnu"
def obtenir_cpu(self) -> str:
"""Lit le modèle CPU depuis /proc/cpuinfo (spécifique Linux)."""
with open("/proc/cpuinfo", "r") as f:
for ligne in f:
if "model name" in ligne:
return ligne.split(":")[1].strip()
return "Inconnu"
def obtenir_processus(self) -> str:
"""Liste les processus avec ps aux (commande POSIX, fonctionne sur Linux)."""
cmd = subprocess.run(["ps", "aux"], capture_output=True, text=True)
return cmd.stdout
def obtenir_arborescence(self, chemin: str, profondeur: int = 2) -> str:
"""Affiche l'arborescence avec tree (paquet à installer sur Linux)."""
cmd = subprocess.run(
["tree", "-L", str(profondeur), chemin],
capture_output=True,
text=True
)
return cmd.stdout
def obtenir_ports_ouverts(self) -> str:
"""Liste les ports ouverts avec ss (remplaçant de netstat sur Linux)."""
cmd = subprocess.run(["ss", "-tuln"], capture_output=True, text=True)
return cmd.stdout
def obtenir_sudoers(self) -> str:
"""Lit le groupe sudo depuis /etc/group (spécifique Linux)."""
with open("/etc/group", "r") as f:
for ligne in f:
if ligne.startswith("sudo:"):
return ligne.strip()
return "Aucun"
def obtenir_stockage(self) -> str:
"""Affiche l'espace disque avec df -h (commande POSIX)."""
cmd = subprocess.run(["df", "-h"], capture_output=True, text=True)
return cmd.stdout
def obtenir_uptime(self) -> str:
"""Récupère l'uptime via la commande uptime."""
cmd = subprocess.run(["uptime"], capture_output=True, text=True)
uptime_raw = cmd.stdout.strip()
parties = uptime_raw.split("load average:")
return parties[0].strip().rstrip(",") if len(parties) > 0 else "Inconnu"
def obtenir_load_average(self) -> str:
"""Récupère le load average via la commande uptime."""
cmd = subprocess.run(["uptime"], capture_output=True, text=True)
uptime_raw = cmd.stdout.strip()
parties = uptime_raw.split("load average:")
return parties[1].strip() if len(parties) > 1 else "Inconnu"