Repository navigation
Expand file tree
/
Copy pathInfoSystem.py
More file actions
116 lines (100 loc) · 3.99 KB
/
Copy pathInfoSystem.py
File metadata and controls
116 lines (100 loc) · 3.99 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
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
import os
import psutil
import time
import datetime
#after execute this script we can save all output information on external file
# example now we have a crontab task like this
# 0 20 * * * root python3 rutaScript > rutaDestino/dato_$(date -d"-0 days" +\%Y\%m\%d).log
# ---------------------------------------------------------------------------------------------
# Looks CPU info and CPU times
if __name__ == '__main__':
for i in range(5):
perc_cpu = psutil.cpu_percent(interval=0.5)
time_cpu = psutil.cpu_times(percpu=False)
systemTime = str(datetime.timedelta(seconds=time_cpu.system))
IdleTime = str(datetime.timedelta(seconds=time_cpu.idle))
print(f'''CPU estado:
Uso de CPU: {perc_cpu}%
Tiempos:
system: {systemTime.split(".")[0]}
idle: {IdleTime.split(".")[0]}
''')
print('''-------------------------------------------------------------''')
time.sleep(4)
# Look RAM memori
for i in range(3):
# save all infomation about used memori
mem_virt = float(psutil.virtual_memory().used / (1024 ** 3))
# save all info about available memori
avail_mem = float(psutil.virtual_memory().available *
100 / psutil.virtual_memory().total)
mem_total = float(psutil.virtual_memory().total / (1024 ** 3))
# print in cosole all text saved in varibles
print(f'''Informacion de Memoria RAM:
Memoria Total: {"{:.2f}".format(mem_total)}GB
Memoria En Uso: {"{:.2f}".format(mem_virt)}GB
Memoria Disponible: {"{:.2f}".format(avail_mem)}%
''')
print('''-------------------------------------------------------------''')
time.sleep(2)
# Look DisK
rName = []
rSize = []
rRuta = []
Dname = os.popen("lsblk -a -p -o NAME").readlines()
Dsize = os.popen("lsblk -a -p -o SIZE").readlines()
Druta = os.popen("lsblk -a -p -o MOUNTPOINT").readlines()
for line in Dname:
line = line.strip()
line = line.lstrip("└─├─")
line = line.rstrip("\n")
rName.append(line)
for line in Dsize:
line = line.strip()
line = line.rstrip("\n")
rSize.append(line)
for line in Druta:
line = line.rstrip("\n")
rRuta.append(line)
for i in range(len(rName)):
if i != 0 :
ruta = rRuta[i]
if ruta != '' and ruta != "[SWAP]":
infoS = "{:.2f}".format(psutil.disk_usage(ruta).used / (1000**3))
print(f'''Disco/Particion {rName[i]} : {infoS}/{rSize[i]} Total''')
else :
if ruta != "[SWAP]":
print(f'''Disco/Particion {rName[i]} : {rSize[i]} Total''')
else:
print(f'''SWAP {rName[i]} : {rSize[i]} Total''')
print('''-------------------------------------------------------------''')
time.sleep(2)
# Look all proces
def checkIfProcessRunning(processName):
#Check if there is any running process that contains the given name processName.
#Iterate over the all the running process
for proc in psutil.process_iter():
try:
# Check if process name contains the given name string.
if processName.lower() in proc.name().lower():
if proc.is_running:
estado = " Activo "
else:
estado = " Inactivo "
print(f'''Process {proc.name()}:
PID: {proc.ppid()}
Estado:{estado}
Conexiones:
''')
for pccon in proc.connections():
print(f''' {pccon}''')
return True
# Catching all excepts
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
if checkIfProcessRunning("NameProcess"):
print('')
else:
print('NameProcess no esta Disponible')
print('''-------------------------------------------------------------''')