公司服務器多,壞的也多,常常會有硬盤只讀的,由於服務器太多,有的壞了好久也發現不了,所以寫個程序,掃描只讀的硬盤,由於這個不是發現故障就必須緊急處理的,因此只是定時執行一遍,掃描入庫便可,這個程序能夠用來作多種批量操做,只須要改一下執行的命令便可,很是好用node
程序代碼:python
#!/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))