-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_out.py
73 lines (60 loc) · 2.05 KB
/
list_out.py
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
import subprocess as sp , sys
from datetime import datetime
import requests
import time
# Get the all arguments
username=sys.argv[1]
password=sys.argv[2]
tenantid=sys.argv[3]
key=sys.argv[4]
val=sys.argv[5]
webhooks_url=sys.argv[6]
q1="[?tags.{}==\`{}\`].name".format(key,val)
q2="[].name"
main_output=('Azure machines list\n\n')
main_output+=(f"VM Name \t Resource Group \t Age \n")
def auth():
authoutput=sp.getstatusoutput("az login --service-principal -u {} -p {} --tenant {}".format(username,password,tenantid))
return authoutput
def filter():
str=sp.getoutput("az vm list --query {} -o tsv".format(q1))
l=list(str.split())
return l
def list_all_vm():
str=sp.getoutput("az vm list --query {} -o tsv".format(q2))
l=list(str.split())
return l
authout=auth()
time.sleep(20)
vm_list=list_all_vm()
l=filter()
filter_array= [item for item in vm_list if item not in l]
def time_format(create_timestamp):
print(type(create_timestamp))
current_time=datetime.now()
#print(current_time)
print(create_timestamp,current_time)
right_format=current_time-(datetime.strptime(create_timestamp[:19],'%Y-%m-%dT%H:%M:%S'))
return right_format
if(authout[0]==0):
print("Authentication is successed")
time.sleep(30)
print("List of All Vm \n")
for i in range(len(filter_array)):
q3="[?name==\`{}\`].resourceGroup".format(filter_array[i])
q4="[?name==\`{}\`].timeCreated".format(filter_array[i])
rg=sp.getoutput("az vm list --query {} -o tsv".format(q3))
tm=sp.getoutput("az vm list --query {} -o tsv".format(q4))
time=time_format(tm)
print(f"Resoure Group of {filter_array[i]} is :{rg}")
print(f"Time created : {time}")
main_output+=(f'{filter_array[i]} : \t {rg} : \t {time}\n\n')
print(main_output)
resp = requests.post(
webhooks_url,
data={
'content':"```\n"+main_output+"\n```",
},
)
else:
print("Authentication failed",authout[1])