-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunshine_get_data.py
executable file
·139 lines (114 loc) · 5.59 KB
/
sunshine_get_data.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
#!/usr/bin/env python
import paramiko
from datetime import date
import os
import multiprocessing
import socket
import logging
import commands
curr_dir = os.path.abspath(os.path.dirname(__file__))
mytuning_path = os.sep.join((curr_dir, 'mytuning.pl'))
dest_file_path = os.sep.join((curr_dir, 'result', date.isoformat(date.today())))
host_list_path = os.sep.join((curr_dir, 'host_list'))
host_list_dir = os.sep.join((curr_dir, 'host_dir'))
scripts_file_path = os.sep.join((curr_dir, 'scripts'))
source_file = './xunjian/{0}.txt'
def make_related_dirs():
if os.path.exists(host_list_dir):
for host_file in os.listdir(host_list_dir):
os.remove(os.sep.join((host_list_dir, host_file)))
for item_path in (dest_file_path, host_list_dir, scripts_file_path):
if not os.path.exists(item_path):
os.makedirs(item_path)
temp_path = os.path.join(scripts_file_path, 'check_path')
if not os.path.exists(temp_path):
os.mkdir(temp_path)
if date.isoformat(date.today()) not in os.listdir(os.sep.join([curr_dir, 'result'])):
os.mkdir(dest_file_path)
def make_running_script(hosts):
for item in hosts:
check_file = open(os.sep.join((scripts_file_path, 'check_path', item[0])), 'w')
check_file.write('#!/bin/sh\n')
check_file.write('top -b -d 2 -n 2 > ./xunjian/{0}.txt\n'.format(item[0]))
check_file.write('free -m >> ./xunjian/{0}.txt\n'.format(item[0]))
check_file.write('source /etc/profile && perl ./xunjian/mytuning.pl --user={0} --pass={1} --socket={3} >> ./xunjian/{2}.txt\n'.format(item[3], item[4], item[0], item[5]))
check_file.write('source /etc/profile && mysql -u{0} -p{1} --socket={3} -e "show slave status\G" >> ./xunjian/{2}.txt\n'.format(item[3], item[4], item[0], item[5]))
check_file.close()
def check(hosts, logger):
for host in hosts:
try:
hostname, username, password, mysql_user, mysql_pass, mysql_socket = host
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect(hostname, port=22, username=username, password=password, timeout=5)
sftp = client.open_sftp()
stdin, stdout, stderr = client.exec_command('ls -d xunjian')
if stderr.read().find('No such file or directory') > 0:
stdin, stdout, stderr = client.exec_command('mkdir xunjian')
stderr.read()
stdout.read()
stdin, stdout, stderr = client.exec_command('ls -l ./xunjian/mytuning.pl')
if stderr.read().find('No such file or directory') > 0:
sftp.put(mytuning_path, './xunjian/mytuning.pl')
sftp.put(os.sep.join((scripts_file_path, 'check_path', hostname)), './xunjian/xunjian.sh')
client.exec_command('chmod a+x ./xunjian/mytuning.pl')
stdin, stdout, stderr = client.exec_command('chmod a+x ./xunjian/xunjian.sh')
stdout.read()
stderr.read()
stdin, stdout, stderr = client.exec_command('./xunjian/xunjian.sh', timeout=30)
temp_dest_file = dest_file_path + '/{0}.txt'.format(hostname)
print source_file.format(hostname), temp_dest_file
stdout.read()
stderr.read()
sftp.get(source_file.format(hostname), dest_file_path + '/{0}.txt'.format(hostname))
sftp.close()
client.close()
with open(temp_dest_file, 'r') as temp_file:
temp_flag = 0
for line in temp_file:
if line.find('but they were invalid') > 0:
temp_flag = 1
if temp_flag == 1:
logger.warn(hostname + ' mytuning.pl may execute failed,please check!')
else:
logger.info(hostname + ' check successfully!')
except socket.timeout:
logger.error(hostname + ' could not be reached,or execute command timeout!')
except IOError:
logger.error(hostname + ' has no Permission')
except paramiko.ssh_exception.AuthenticationException:
logger.error(hostname + ' authentication failure!')
except paramiko.ssh_exception.SSHException:
logger.error(hostname + ' No existing session!')
finally:
client.close()
def multi_check(hosts_list):
logger = multiprocessing.get_logger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')
file_handler = logging.FileHandler('check.log', 'w')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
processes = []
for host_filename, hosts in hosts_list.items():
p = multiprocessing.Process(target=check, args=(hosts, logger, ))
processes.append(p)
p.start()
if __name__ == "__main__":
make_related_dirs()
with open(host_list_path, 'r') as f:
lines = f.readlines()
file_len = len(lines)
commands.getoutput('split -l ' + ' '.join([str(file_len / 4), host_list_path]))
commands.getoutput('mv xa* ' + host_list_dir)
host_list = {}
for host_file in os.listdir(host_list_dir):
with open(os.sep.join((host_list_dir, host_file)), 'r') as f:
host_list[host_file] = []
for line in f:
host_list[host_file].append(line.replace('\r\n', '').replace('\n', '').split())
make_running_script(host_list[host_file])
multi_check(host_list)
for host_file in os.listdir(host_list_dir):
os.remove(os.sep.join((host_list_dir, host_file)))