forked from waue0920/dtn_demo_check_sc17
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtn_demo_check_sc17_v2.py
executable file
·258 lines (205 loc) · 6.09 KB
/
dtn_demo_check_sc17_v2.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
### readme
### run the script in sudoer priviledge ! ( or get exception)
#
# json_str = json.dumps(data)
#
# print(json_str)
#
# with open('data.json', 'w') as f :
# json.dump(data,f)
#
# with open('data.json', 'r') as f :
# data1 = json.load(f)
#
# from pprint import pprint
# pprint(data1)
import json
import multiprocessing
import pprint
# import queue
import os
import queue
import subprocess
import re
import shutil
import socket
### Using os.system(..) to execute cmd and return 0 or 1
### Using subprocess.Popen(..) to get the execution result
import threading
import time
def return_command(cmd):
# return execution result as output -> str
process = subprocess.Popen([cmd], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=1)
return process.stdout.read().decode('utf8')
def check_command_old(cmd):
# return 0 if success, or return > 0 -> int
return os.system(cmd)
def check_command(cmd):
# return 0 if success, or return > 0 -> int
process = subprocess.run(cmd)
return process.returncode
def checkFirewall():
# check iptables is existed, if without iptables, pass the check
if shutil.which("iptables") is None:
return 1
ret = return_command("head -n 1 /etc/os-release")
# check Centos or Ubuntu, only Centos iptables filter 8000 ..
# if re.search("CentOS Linux", ret) == None: # easily failed
if "CentOS" in ret:
# if Centos , check the rule for opening 8000 port for jupyter
ret = return_command("iptables -nvL |grep 8000 |wc -l")
if int(ret) < 1:
return 0
return 1
def checkVlan():
vlan63 = "192.168.63.59"
vlan61 = "192.168.61.57"
# check vlan 61 and 63
if int(check_command("ping -c 1 " + vlan63 + ">/dev/null")) != 0:
# vlan 63 can't connect, check 61
if int(check_command("ping -c 1 " + vlan61 + ">/dev/null")) != 0:
# neither 61 nor 63 is failed to ping, return 0
return 0
else:
return 61
else:
# vlan 63 ok , check 61
if int(check_command("ping -c 1 " + vlan61 + ">/dev/null")) != 0:
# vlan 63 ok, check 61 failed
return 63
else:
# both 61 nor 63 is ok to ping
return 6163
def checkJupyter():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# check the jupyter port is open, because system reboot will reset iptables rules
ret = sock.connect_ex(('0.0.0.0', 8000))
if ret != 0:
return 0
return 1
def checkNvme():
ret = int(return_command("df |grep nvme |wc -l "))
if type(ret) == int:
return int(ret)
else:
return 0
def checkFileExist():
### this will occur file not found exection
# ret = return_command("ls /data/disk*/sc17/fftest | wc -l ")
# if int(ret) < 8:
# return 0
# return 1
count = 0
dirlist = [
"/data/disk0/sc17/fftest",
"/data/disk1/sc17/fftest",
"/data/disk2/sc17/fftest",
"/data/disk3/sc17/fftest",
"/data/disk4/sc17/fftest",
"/data/disk5/sc17/fftest",
"/data/disk6/sc17/fftest",
"/data/disk7/sc17/fftest",
]
for fd in dirlist:
if os.access(fd, os.R_OK) is False:
continue
count += 1
return count
def checkDirPermission():
count = 0
dirlist = [
"/data/disk0/sc17/",
"/data/disk1/sc17/",
"/data/disk2/sc17/",
"/data/disk3/sc17/",
"/data/disk4/sc17/",
"/data/disk5/sc17/",
"/data/disk6/sc17/",
"/data/disk7/sc17/",
]
for fd in dirlist:
if os.access(fd, os.W_OK) is False:
continue
count += 1
return count
checklist = {}
# checklist = {
# 'firewall_check': 1,
# 'vlan_check': 1,
# 'jupyter_check': 1,
# 'nvme_check': 1,
# 'directory_check': 1,
# 'permission_check': 1,
# }
def checkSudoer():
if os.getuid() != 0:
return False
else:
return True
def pingServer(server, qu):
vlan_name = server[0]
server_name = server[1]
if int(check_command(["ping", "-c 1", server_name + " >/dev/null"])) == 0:
# result = subprocess.run(["/sbin/ping", "-c 1", server])
# if result.returncode == 0 :
# success
qu.put((vlan_name, 1))
else:
# failed
qu.put((vlan_name, 0))
'''
result = subprocess.run(["/sbin/ping" , "-c 1", server])
if result.returncode == 0:
qu.put((server, True))
else:
qu.put((server, False))
'''
def checkIndVlan():
serverlist = [
(3060, "192.168.60.57"),
(3061, "192.168.61.57"),
(3062, "192.168.62.59"),
(3063, "192.168.63.59"),
(1038, "10.250.38.50"),
]
ret = {}
qu = queue.Queue()
# threads = [None,None,None,None,None]
threads = []
for i in range(len(serverlist)):
server = serverlist[i]
# threads[i] = threading.Thread(target=pingServer, args=(server,qu))
# threads[i].start()
th = threading.Thread(target=pingServer, args=(server, qu))
th.start()
threads.append(th)
# for i in range(len(serverlist)):
# threads[i].join()
while (len(threads) != 0):
th = threads.pop()
th.join()
while (qu.empty() is False):
vlan_ret = qu.get()
# vlan_ret[0] = 3060, ...
# vlan_ret[1] = 1 or 0
ret[vlan_ret[0]] = vlan_ret[1]
return ret
def main():
checklist["firewall_check"] = checkFirewall()
## checklist["vlan_check"] = checkVlan() ## deprecated
checklist["vlan_check"] = checkIndVlan()
checklist["jupyter_check"] = checkJupyter()
checklist["nvme_check"] = checkNvme()
checklist["testfile_check"] = checkFileExist()
checklist["permission_check"] = checkDirPermission()
json_str = json.dumps(checklist, indent=4)
print(json_str)
with open("./dtn_demo_check_sc17.json", "w") as f:
f.write(json_str)
def usage():
print("{msg=\"You should run this in sudo priviledge !\"}")
exit(1)
if __name__ == "__main__":
#main()
usage() if checkSudoer() is False else main()