first commit

This commit is contained in:
root
2023-10-17 10:03:51 +02:00
commit 73bd0f460c
2265 changed files with 384897 additions and 0 deletions

52
functions/fbackup.py Normal file
View File

@@ -0,0 +1,52 @@
import lxc
from datetime import datetime
from config import *
## LXC
def create_snap_lxc(lxc_name):
cont=lxc.Container(lxc_name)
cont.snapshot()
def get_snap_list_lxc(lxc_name):
dom=lxc.Container(lxc_name)
list_snap_lxc = dom.snapshot_list()
out=[]
for snap in list_snap_lxc:
nom_snap=snap[0]
date_snap=datetime.strptime(snap[2], '%Y:%m:%d %H:%M:%S')
date_snap=date_snap.strftime("%d%m%Y%H%M")
concat_name=dom.name+"_"+date_snap+"_"+nom_snap
out.append(concat_name)
return out
def del_snap_lxc(lxc_name,item):
conn=lxc.Container(lxc_name)
item=item.split('_')[2]
conn.snapshot_destroy(item)
def rest_snap_lxc(lxc_name,item):
conn=lxc.Container(lxc_name)
item=item.split('_')[2]
conn.snapshot_restore(item)
## VM
def create_snap_vm(vm_name):
dom=conn.lookupByName(vm_name)
actual_date=datetime.now()
actual_date=actual_date.strftime("%d%m%Y%H%M")
snapshot_name= dom.name()+"_"+actual_date+"_snap"
SNAPSHOT_XML_TEMPLATE = """<domainsnapshot>
<name>{snapshot_name}</name>
</domainsnapshot>"""
dom.snapshotCreateXML(SNAPSHOT_XML_TEMPLATE.format(snapshot_name=snapshot_name),libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC)
def get_snap_list_vm(vm_name):
dom=conn.lookupByName(vm_name)
list_snap_vm=dom.snapshotListNames()
list_snap_vm.reverse()
out=[]
for snap in list_snap_vm:
out.append(snap)
return out

147
functions/fhost.py Normal file
View File

@@ -0,0 +1,147 @@
from config import *
import re
import os
import psutil
from datetime import datetime
units_map = [
(1<<50, ' PB'),
(1<<40, ' TB'),
(1<<30, ' GB'),
(1<<20, ' MB'),
(1<<10, ' KB'),
(1, (' byte', ' bytes')),
]
def human_size(bytes, units=units_map):
for factor, suffix in units:
if bytes >= factor:
break
amount = int(bytes / factor)
if isinstance(suffix, tuple):
singular, multiple = suffix
if amount == 1:
suffix = singular
else:
suffix = multiple
return str(amount) + suffix
class Host:
def __init__(self):
self.hostname = psutil.os.uname().nodename
self.cpu_number = psutil.cpu_count(logical=False)
self.vcpu = psutil.cpu_count()
self.mem_max = human_size(psutil.virtual_memory().total)
self.boot_time = datetime.fromtimestamp(psutil.boot_time() - 60)
self.cpu_usage = psutil.cpu_percent(interval=0.0, percpu=False)
self.mem_usage = psutil.virtual_memory().percent
self.swap_usage = psutil.swap_memory().percent
class ENV:
def __init__(self):
self.hostname = psutil.os.uname().nodename
self.cpu_number = psutil.cpu_count(logical=False)
self.vcpu = psutil.cpu_count()
self.mem_max = human_size(psutil.virtual_memory().total)
self.boot_time = psutil.boot_time()
class CPU:
def __init__(self):
self.cpu = {}
self.cpu['percent'] = psutil.cpu_percent(interval=0.0, percpu=False)
self.cpu['time_user'] = psutil.cpu_times_percent().user
self.cpu['time_nice'] = psutil.cpu_times_percent().nice
self.cpu['time_system'] = psutil.cpu_times_percent().system
self.cpu['time_idle'] = psutil.cpu_times_percent().idle
self.cpu['time_iowait'] = psutil.cpu_times_percent().iowait
self.cpu['time_irq'] = psutil.cpu_times_percent().irq
self.cpu['time_softirq'] = psutil.cpu_times_percent().softirq
self.cpu['time_steal'] = psutil.cpu_times_percent().steal
self.cpu['time_guest'] = psutil.cpu_times_percent().guest
self.cpu['time_guest_nice'] = psutil.cpu_times_percent().guest_nice
class Memory:
def __init__(self):
self.mem = {}
self.mem['total'] = human_size(psutil.virtual_memory().total)
self.mem['available'] = human_size(psutil.virtual_memory().available)
self.mem['percent'] = psutil.virtual_memory().percent
self.mem['used'] = human_size(psutil.virtual_memory().used)
self.mem['free'] = human_size(psutil.virtual_memory().free)
self.mem['active'] = human_size(psutil.virtual_memory().active)
self.mem['inactive'] = human_size(psutil.virtual_memory().inactive)
self.mem['buffers'] = human_size(psutil.virtual_memory().buffers)
self.mem['cached'] = human_size(psutil.virtual_memory().cached)
self.mem['shared'] = human_size(psutil.virtual_memory().shared)
self.mem['slab'] = human_size(psutil.virtual_memory().slab)
class Swap:
def __init__(self):
self.swap = {}
self.swap['total'] = human_size(psutil.swap_memory().total)
self.swap['used'] = human_size(psutil.swap_memory().used)
self.swap['free'] = human_size(psutil.swap_memory().free)
self.swap['percent'] = psutil.swap_memory().percent
self.swap['sin'] = human_size(psutil.swap_memory().sin)
self.swap['sout'] = human_size(psutil.swap_memory().sout)
class Disks:
def __init__(self):
num=0
for i in psutil.disk_partitions():
exec("self.disk_"+str(num)+" = {}")
exec("self.disk_"+str(num)+"['device'] = '"+str(i.device)+"'")
exec("self.disk_"+str(num)+"['mountpoint'] = '"+str(i.mountpoint)+"'")
exec("self.disk_"+str(num)+"['fstype'] = '"+str(i.fstype)+"'")
exec("self.disk_"+str(num)+"['opts'] = '"+str(i.opts)+"'")
exec("self.disk_"+str(num)+"['maxfile'] = '"+str(i.maxfile)+"'")
exec("self.disk_"+str(num)+"['maxpath'] = '"+str(i.maxpath)+"'")
exec("self.disk_"+str(num)+"['size_total'] = '"+str(human_size(psutil.disk_usage(i.mountpoint).total))+"'")
exec("self.disk_"+str(num)+"['size_used'] = '"+str(human_size(psutil.disk_usage(i.mountpoint).used))+"'")
exec("self.disk_"+str(num)+"['size_free'] = '"+str(human_size(psutil.disk_usage(i.mountpoint).free))+"'")
exec("self.disk_"+str(num)+"['size_percent'] = '"+str(psutil.disk_usage(i.mountpoint).percent)+"'")
num+=1
class Network:
def __init__(self):
num=0
for interface in psutil.net_if_addrs():
exec("self.net_"+str(num)+" = {}")
interface_usage = psutil.net_io_counters(pernic=True)
for detail in psutil.net_if_addrs()[interface]:
exec("self.net_"+str(num)+"['name'] = '"+str(interface)+"'")
exec("self.net_"+str(num)+"['bytes_sent'] = '"+str(human_size(interface_usage[interface].bytes_sent))+"'")
exec("self.net_"+str(num)+"['bytes_recv'] = '"+str(human_size(interface_usage[interface].bytes_recv))+"'")
ip_pattern = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
if ip_pattern.match(detail.address):
exec("self.net_"+str(num)+"['address_v4'] = '"+str(detail.address)+"'")
else:
exec("self.net_"+str(num)+"['address_v6'] = '"+str(detail.address)+"'")
if detail.netmask:
if ip_pattern.match(detail.netmask):
exec("self.net_"+str(num)+"['netmask_v4'] = '"+str(detail.netmask)+"'")
else:
exec("self.net_"+str(num)+"['netmask_v6'] = '"+str(detail.netmask)+"'")
num+=1
class Monit:
def __init__(self):
self.env = ENV()
self.cpu = CPU()
self.mem = Memory()
self.swap = Swap()
self.disks = Disks()
self.network = Network()
def get_host_full():
Localhost = Monit()
host_full = {}
host_full.update(vars(Localhost.env))
host_full.update(vars(Localhost.cpu))
host_full.update(vars(Localhost.mem))
host_full.update(vars(Localhost.swap))
host_full.update(vars(Localhost.disks))
host_full.update(vars(Localhost.network))
return host_full

71
functions/fiso.py Normal file
View File

@@ -0,0 +1,71 @@
import os
from config import *
from xml.dom import minidom
def get_xml(vm_name):
dom=conn.lookupByName(vm_name)
raw_xml = dom.XMLDesc()
xml = minidom.parseString(raw_xml)
return xml
def get_iso_list():
list_iso_path= []
list_iso_size= []
for file in os.listdir(iso_path):
if file.endswith(".iso"):
size=os.path.getsize(iso_path+file)
size=human_size(size)
list_iso_size.append(str(size))
list_iso_path.append(str(file))
list_iso=zip(list_iso_path, list_iso_size)
return list_iso
def get_cdrom_attribut(vm_name):
xml = get_xml(vm_name)
diskTypes = xml.getElementsByTagName('disk')
for disk_xml in diskTypes:
if disk_xml.getAttribute('device') == 'cdrom':
target_tag = disk_xml.getElementsByTagName('target')
address_tag = disk_xml.getElementsByTagName('address')
target_dev = target_tag[0].getAttribute('dev')
target_bus = target_tag[0].getAttribute('bus')
address_type = address_tag[0].getAttribute('type')
address_controller = address_tag[0].getAttribute('controller')
address_bus = address_tag[0].getAttribute('bus')
address_target = address_tag[0].getAttribute('target')
address_unit = address_tag[0].getAttribute('unit')
return target_dev, target_bus, address_type, address_controller, address_bus, address_target, address_unit
def mount_iso(vm_name,iso_name):
dom = conn.lookupByName(vm_name)
target_dev, target_bus, address_type, address_controller, address_bus, address_target, address_unit = get_cdrom_attribut(vm_name)
diskFile = iso_name
diskXML = """ <disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='""" + diskFile + """'/>
<target dev='""" + target_dev + """' bus='""" + target_bus + """'/>
<address type='""" + address_type + """' controller='""" + address_controller + """' bus='""" + address_bus + """' target='""" + address_target + """' unit='""" + address_unit + """'/>
</disk>"""
dom.updateDeviceFlags(diskXML, 0)
def check_iso_is_mounted(vm_name):
dom = conn.lookupByName(vm_name)
raw_xml = dom.XMLDesc()
xml = minidom.parseString(raw_xml)
diskTypes = xml.getElementsByTagName('disk')
for disk_xml in diskTypes:
disk = None
source = disk_xml.getElementsByTagName('source')
if disk_xml.getAttribute('device') == 'cdrom':
try:
gotdata= source[0].getAttribute('file')
if source[0].getAttribute('file'):
state = 1
else:
state = 0
except IndexError:
state = 0
#Return 1 if mounted or 0 if not mounted
return state

63
functions/flxc.py Normal file
View File

@@ -0,0 +1,63 @@
import lxc
from config import *
### LISTS
def get_lxc_list():
full_ct=lxc.list_containers()
full_ct = list(full_ct)
return full_ct
def get_lxc_activ():
activ_ct = []
for ct_name in get_lxc_list():
ct_state=lxc.Container(ct_name)
if ( ct_state.state == "RUNNING"):
activ_ct.append(ct_name)
return activ_ct
def get_lxc_inactiv():
inactiv_ct = []
for ct_name in get_lxc_list():
ct_state=lxc.Container(ct_name)
if ( ct_state.state != "RUNNING"):
inactiv_ct.append(ct_name)
return inactiv_ct
### ACTIONS
def start_lxc(lxc_name):
mount_pts()
container=lxc.Container(lxc_name)
container.start()
container.wait("RUNNING", 3)
def stop_lxc(lxc_name):
mount_pts()
container=lxc.Container(lxc_name)
container.stop()
container.wait("STOPPED", 3)
def get_lxc_ip(lxc_name):
ct_state=lxc.Container(lxc_name)
lxc_ip = str(ct_state.get_ips()).replace('(', '').replace(')', '').replace(',', '').replace('\'', '')
return lxc_ip
## CREATE
def create_lxc(lxc_name,lxc_os):
mount_pts()
container=lxc.Container(lxc_name)
resultats=container.create(lxc_os)
def set_lxc_ip(lxc_ip):
file_object = open('/var/lib/lxc/'+lxc_name+'/config', 'a')
file_object.write('lxc.net.0.ipv4.address = '+lxc_ip+'\n')
file_object.write('lxc.net.0.ipv4.gateway = auto')
file_object.close()
## DESTROY
def destroy_lxc(lxc_name):
container=lxc.Container(lxc_name)
container.destroy()

64
functions/fpool.py Normal file
View File

@@ -0,0 +1,64 @@
from config import *
def refresh_pool():
for refresh_pool in conn.listAllStoragePools():
refresh_pool.refresh(0)
def get_pool_list():
list_pools = []
for pools in conn.listStoragePools():
pool = conn.storagePoolLookupByName(pools)
list_pools.append(pool.name())
return list_pools
def get_full_pool():
refresh_pool()
list_pool_full = []
for pools in get_pool_list():
pool_info = get_pool_info(pools)
pool_info.append(get_pool_volumes(pools))
list_pool_full.append(pool_info)
return list_pool_full
# Name,UUID,Active,Total,Used,Free,Percent
def get_pool_info(pool_name):
refresh_pool()
pool_info = []
pool = conn.storagePoolLookupByName(pool_name)
pool_info.append(pool.name())
pool_info.append(pool.UUIDString())
pool_info.append(str(pool.isActive()))
pool_info.append(str(human_size(pool.info()[1])))
pool_info.append(str(human_size(pool.info()[2])))
pool_info.append(str(human_size(pool.info()[3])))
if pool.info()[1]==0:
pool_info.append(str(0))
else:
pool_info.append(str(round((pool.info()[2]*100)/pool.info()[1],2)))
return pool_info
#Name,Total,Used,Percent
def get_pool_volumes(pool_name):
refresh_pool()
volumes_list = []
pool = conn.storagePoolLookupByName(pool_name)
for volume in pool.listVolumes():
volume_info=[]
vol = pool.storageVolLookupByName(volume)
volume_info.append(volume)
volume_info.append(human_size(vol.info()[1]))
volume_info.append(human_size(vol.info()[2]))
if vol.info()[1]==0:
vol_used=0
else:
vol_used=round((vol.info()[2]*100)/vol.info()[1],2)
volume_info.append(vol_used)
volumes_list.append(volume_info)
return volumes_list
def del_pool_vol(pool_name,volume_name):
refresh_pool()
pools=conn.storagePoolLookupByName(pool_name)
vlm=pools.storageVolLookupByName(volume_name)
vlm.delete()

70
functions/fvm.py Normal file
View File

@@ -0,0 +1,70 @@
from config import *
import time
def get_vm_activ():
list_run_id=conn.listDomainsID()
activ_vm=[]
for id in list_run_id:
dom=conn.lookupByID(id)
activ_vm.append(dom.name())
return activ_vm
def get_vm_inactiv():
inactiv_vm=conn.listDefinedDomains()
return inactiv_vm
def get_vm_list():
full_vm = get_vm_activ() + get_vm_inactiv()
return full_vm
def start_vm(vm_name):
dom = conn.lookupByName(vm_name)
dom.create()
time.sleep(3)
alive=0
while alive < 3:
if dom.isActive():
alive=4
else:
time.sleep(3)
alive+=1
def stop_vm(vm_name):
dom = conn.lookupByName(vm_name)
dom.shutdown()
time.sleep(3)
alive=0
while alive < 5:
if dom.isActive():
time.sleep(3)
alive+=1
else:
alive=6
if dom.isActive():
dom.destroy()
def get_vm_ips(vm_name):
dom=conn.lookupByName(vm_name)
if not dom:
raise SystemExit("Failed to connect to Dom")
try:
ifaces = dom.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT)
except:
ifaces = None
result="--.--.--.--"
if ifaces is None:
result="--.--.--.--"
else:
for (name, val) in ifaces.items():
if val['addrs']:
for addr in val['addrs']:
if addr['addr']:
result=str(addr['addr'])+"/"+str(addr['prefix'])
break
else:
result="-"
return result
def destroy_vm(vm_name):
dom=conn.lookupByName(vm_name)
dom.undefine()

25
functions/fvnc.py Normal file
View File

@@ -0,0 +1,25 @@
from config import *
from xml.etree import ElementTree as ET
from flask import request, Response
import os
def get_vnc_port(vm_name):
dom = conn.lookupByName(vm_name)
vm_xml = dom.XMLDesc(0)
et_xml = ET.fromstring(vm_xml)
graphics = et_xml.find('./devices/graphics')
vnc_port = graphics.get('port')
return vnc_port
def kill_consoles():
os.system('kill -9 $(ps -edf | grep websockify | grep -v grep | awk \'{ print $2 }\')')
os.system('for i in $(ps -edf | grep pyxterm | grep -v grep | awk \'{ print $2 }\');do kill -9 $i;done')
def socket_connect(vm_name):
kill_consoles()
vm_port = get_vnc_port(vm_name)
os.system('websockify -D --web=/usr/share/novnc/ 6080 localhost:'+vm_port)
def pyxterm_connect(lxc_name):
kill_consoles()
os.system('python3 pyxterm.py --command \'lxc-attach\' --cmd-args \''+lxc_name+'\' &')