-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostbckup.py
More file actions
82 lines (82 loc) · 5.25 KB
/
Hostbckup.py
File metadata and controls
82 lines (82 loc) · 5.25 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
#!/usr/bin/python
import time
import sys
import subprocess
from VM_migrate import Migrate
def main():
while True:
print("Host Patroling Starts")
time.sleep(1)
hostfile="/home/node4_a1/hostInfo.txt"
with open(hostfile) as f:
for line in f:
ipindex = line.index("|")
hostipaddr=line[:ipindex]
#Check for CPU
cmdgetcpu="ssh root@"+hostipaddr+" top -bn 2 | sed -n '/%Cpu(s)/p' | sed '2!d' | egrep -o \"[0-9]+(\.[0-9][0-9]?)?\" | sed '1!d'"
hostcpuutil=subprocess.check_output(cmdgetcpu,shell=True)
hostcpuutil = hostcpuutil.decode('utf-8')
#hostcpuidle = 10
if float(hostcpuutil) >= float(20):
print("Host with high CPU"+hostipaddr)
vmfile="/home/node4_a1/VmInfo.txt"
vmtomigrate=""
subprocess.check_call("sh /home/node4_a1/DC/VMKaran1.sh",shell=True)
with open(vmfile) as fvmlist:
for vmlistline in fvmlist:
if hostipaddr in vmlistline:
maxcpu=0
ipindex = vmlistline.index("|")
vmipaddr = vmlistline[:ipindex]
print("Checking VM"+vmipaddr")
vmutilfile="/home/node4_a1/DC/OutputK.txt"
with open(vmutilfile) as fvmutil:
for linevmutil in fvmutil:
if vmipaddr in linevmutil:
words = linevmutil.split("|")
if maxcpu < float(words[1]):
maxcpu=float(words[1])
vmtomigrate=vmipaddr
break
checkvmfile="/home/node4_a1/MigrationExclusion.txt"
if vmtomigrate not in open(checkvmfile).read():
hs = open(checkvmfile,"a")
hs.write("{}\n".format(vmtomigrate))
hs.close()
print("VM to migrate cpu "+vmtomigrate)
Migrate(str(vmtomigrate),"CPU")
#Check for Memory
time.sleep(60)
cmdgetmem="ssh root@"+hostipaddr+" free | grep Mem | awk '{print $3/$2 * 100}'"
hostmemutil=subprocess.check_output(cmdgetmem,shell=True)
hostmemutil = hostmemutil.decode('utf-8')
#print("mem util "+hostmemutil)
#hostmemutil=80
if float(hostmemutil) >= float(75):
vmfile="/home/node4_a1/VmInfo.txt"
vmtomigrate=""
subprocess.check_call("sh /home/node4_a1/DC/VMKaran1.sh",shell=True)
with open(vmfile) as fvmlist:
for vmlistline in fvmlist:
if hostipaddr in vmlistline:
maxmem=0
ipindex = vmlistline.index("|")
vmipaddr = vmlistline[:ipindex]
vmutilfile="/home/node4_a1/DC/OutputK.txt"
with open(vmutilfile) as fvmutil:
for linevmutil in fvmutil:
if vmipaddr in linevmutil:
words = linevmutil.split("|")
if maxmem < float(words[2]):
maxmem=float(words[2])
vmtomigrate=vmipaddr
break
checkvmfile="/home/node4_a1/MigrationExclusion.txt"
if vmtomigrate not in open(checkvmfile).read():
hs = open(checkvmfile,"a")
hs.write("{}\n".format(vmtomigrate))
hs.close()
print("VM to migrate memory"+vmtomigrate)
Migrate(vmtomigrate,"MEM")
if __name__=="__main__":
main()