公司服务器多,坏的也多,经常会有硬盘只读的,因为服务器太多,有的坏了很久也发现不了,因此写个程序,扫描只读的硬盘,因为这个不是发现故障就必须紧急处理的,所以只是定时执行一遍,扫描入库即可,这个程序可以用来做多种批量操作,只需要改一下执行的命令即可,非常好用
程序代码:
#!/usr/local/bin/python2.7
#-*- coding: utf-8 -*-
import paramiko
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
import MySQLdb
import time
import datetime
import getopt
import json
import collections
import re
from multiprocessing import Pool
user='root'
PORT='******'
H3303='*****com.cn'
H3304m='*****com.cn'
P3303=3303
P3304=3304
dp_admin='dp_admin'
HOST_PORT='3303'
HOST_USER = 'mysqlha'
HOST_PASSED = '*******'
db='test'
ctime_min = (int(time.strftime('%M',time.localtime(time.time())))/5*5)
ctime=time.strftime('%Y-%m-%d %H:',time.localtime(time.time()))
ctime=ctime + str(ctime_min) + ':00'
print ctime
def sql_select(sql, port=3304, domain='*****.com.cn', db='swordfish'):
port = int(port)
array = []
try:
db = MySQLdb.connect(host=domain,user=HOST_USER,port=port,passwd=HOST_PASSED,db=db,connect_timeout=3,charset="utf8")
cursor = db.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
array.append(row)
return array
except Exception,e:
print str(e)
return array
def sql_insert(sql, port=3304, domain='******com.cn', db='swordfish'):
try:
db = MySQLdb.connect(host=domain,user=HOST_USER,port=port,passwd=HOST_PASSED,db=db,connect_timeout=3,charset="utf8")
cursor = db.cursor()
cursor.execute(sql)
db.commit()
db.close()
except Exception,e:
print str(e)
db.rollback()
db.close()
def get_all_ip():
sql_all = """
select distinct ip_in from node \
where depid=1 \
and ip_in not in \
(select node.ip_in from node, node2module where node.id=node2module.nid \
and node2module.mname in ('dbstorage') )
"""
return sql_select(sql_all, port=3303, domain='*****.com.cn', db='dp_admin')
def remote_cmd(host):
begintime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
begintime = time.strptime(begintime, '%Y-%m-%d %H:%M:%S')
begintime = datetime.datetime(*begintime[:6])
#print 'process host %s'%(host)
#标志位,表示是否ssh ok
flag=0
host_info=[host,flag]
try:
cmd1=''' for i in `df -l | egrep -v 'Mounted|shm' | awk '{print $6}'`; do cd $i; [[ -d $i ]] && (cd $i; touch 'read_only_test_file' 2>/dev/null; [[ $? -ne 0 ]] && echo $i | tr '\n' ',' || (rm -fr read_only_test_file)) ; done '''
cmds=[cmd1]
for cmd in cmds:
paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname=host,username=user,port=int(PORT),timeout=5)
s0,s1,s2 = s.exec_command(cmd,timeout=20)
info = s1.read().strip()
host_info.append(info)
s.close()
except Exception,e:
#根据第二个标志位确定ssh是否通
host_info[1]=-1
if host_info[1] == -1:
#bad_ssh="%s\n" % (host_info[0])
#print 'bad ssh 1 %s' % (host_info[0])
return []
else:
##部分机器出现这种错误:Unable to get valid context for root,目前只能通过匹配字符串排除异常
if host_info[2].find("Unable to get valid context for root")==0:
#bad_ssh = "%s\n"%(host_info[0])
#print 'bad ssh 2 %s' % (host_info[0])
return []
else:
if host_info[2]:
return [host_info[0], host_info[2]]
else:
#执行结果为空的
return [host,]
if __name__=='__main__':
begintime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
begintime = time.strptime(begintime, '%Y-%m-%d %H:%M:%S')
begintime = datetime.datetime(*begintime[:6])
hosts = get_all_ip()
host_list = []
for host in hosts:
host_list.append(host[0])
pool = Pool(8)
data = pool.map(remote_cmd, host_list)
pool.close()
pool.join()
for d in data:
if d:
if len(d) == 2:
ip = d[0]
raid_mon_info = d[1]
raid_mon_value = 'BAD'
raid_mon_info = ip + '#NULL#NULL#NULL#NULL#' + raid_mon_info
else:
ip = d[0]
raid_mon_value = 'OK'
raid_mon_info = ip + '#NULL#NULL#NULL#NULL#' + 'OK'
sql = "insert into disk_readonly (ip_in, disk_readonly_value, disk_readonly_info, ctime) values ('%s', '%s', '%s', '%s')" % (ip, raid_mon_value, raid_mon_info, ctime)
#print sql
sql_insert(sql)
endtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
endtime = time.strptime(endtime, '%Y-%m-%d %H:%M:%S')
endtime = datetime.datetime(*endtime[:6])
time_dvalue = (endtime - begintime).seconds
print '总执行时间:%s sec' % (time_dvalue)
print '统计的机器数 %s' % (len(hosts))
转载于:https://blog.51cto.com/6252961/1711004