-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsi.py
executable file
·183 lines (156 loc) · 4.45 KB
/
rsi.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
###############################################################
#
# Contrail rsi tool for collecting a node information
# Usage:
# ./rsi.py <node ip>
# Ex:
# ./rsi.py 10.219.90.82
# Output:
# collects specified commands( see scripts/ folder within)
# as a compressed tar file
# saves it locally rsi-<ddMMMyy-hhmmss>.tgz
# Ex: rsi-20Mar18-121953.tgz
###############################################################
import os
import time
import sys
import getpass
import os.path
import paramiko
from paramiko import SSHClient
from scp import SCPClient
rsiVersion=0.1
archivedir='scripts'
archive='scripts.tar'
#TBD: traverse all tenants
#for now, specific tenant information is captured
tenantcred='tenant.cred'
def checkPing(host):
response = os.system("ping -c 1 -t2 " + host + "> /dev/null")
# and then check the response...
if response == 0:
pingstatus = True
else:
pingstatus = False
return pingstatus
def checkServer(server, user, pwd, chkcmd):
if not checkPing(server):
print server, 'not reachable?'
print 'exiting'
exit(0)
proxy = None
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(server, username=user, password=pwd, timeout=2)
stdin, stdout, stderr = ssh.exec_command(chkcmd)
for line in stderr:
if 'not' in line:
print server, 'is not a contrail node?'
ssh.close()
return False
c=0
for line in stdout:
c+=1
if not c:
print server, 'is not running contrail services?'
ssh.close()
return False
print server, 'is a contrail node'
return True
return True
except paramiko.AuthenticationException:
print("Authentication failed?")
return False
except:
print("unknown error")
print server, 'is a contrail node?'
return False
ssh.close()
def cleanup():
#remove local scripts tarball
os.system('rm -rf scripts.tar')
os.system('rm -rf /root/rsi/scripts')
def runRsi(server, file, user, pwd):
global tenantcred
proxy = None
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server, username=user, password=pwd)
# SCPClient takes a paramiko transport as its only argument
scp = SCPClient(ssh.get_transport())
out = ssh.exec_command('mkdir -p rsi 2>>/tmp/err')
out = ssh.exec_command('hostname')[1]
hostname = out.readlines()[0].rstrip()
targetfile='/root/rsi/'+file
scp.put(file, targetfile)
rsiFolderNamw = hostname+'-'+str(int(time.time()))
if os.path.isfile(tenantcred):
scp.put(file, tenantcred)
stdout = ssh.exec_command('rm /root/rsi/Finished')
stdin,stdout,stderr = ssh.exec_command('tar xf ' + targetfile + ' -C rsi/')
stdout = ssh.exec_command('rsi/scripts/rsi.sh ' + rsiFolderNamw)[1]
rsiFileName = rsiFolderNamw+'.tgz'
remFileName= '/root/rsi/' + rsiFileName
stdoutlen = 0
waitTime = 60
#while not stdoutlen and retries < 20:
done = False
while waitTime:
try:
stdin, stdout, stderr = ssh.exec_command('ls -l ' + '/root/rsi/Finished')
stdout.readlines()[0]
except IndexError:
waitTime -=2
time.sleep(2)
else:
scp.get('rsi/'+rsiFileName, './'+rsiFileName)
print 'RSI saved to: ', rsiFileName
out = ssh.exec_command('rm /root/rsi/Finished')
out = ssh.exec_command('rm -rf /root/rsi/' + rsiFolderNamw)
out = ssh.exec_command('rm -rf /root/rsi/scripts.tar')
out = ssh.exec_command('rm -rf /root/rsi/scripts')
return True
print 'RSI session hung?'
return False
#stderr = ssh.exec_command('bash test.sh')[2]
def checkScripts():
#print 'checking'
if not os.path.isdir(archivedir):
return False
#print archivedir, 'is found..'
return True
if not checkScripts():
print archivedir, 'is not found!'
exit(0)
user='root'
server=''
print 'contrail-rsi version:', rsiVersion
print
if len(sys.argv) < 2:
print 'Enter Node FQDN/Address:',
server = raw_input()
else:
server = sys.argv[1]
targetfile=int(time.time() )
user = 'root'
pwd = ''
#Comment this line
#pwd = 'Juniper'
if not pwd:
pwd = getpass.getpass()
else:
print 'Warning: password is hardcoded! use getpass()'
print 'logging to', server
os.system('tar cf ' + archive + ' scripts/')
if checkServer(server, user, pwd, 'contrail-status'):
print "OK"
status = False
while not status:
status = runRsi(server, archive, user, pwd)
else:
print('exiting');
cleanup()