nfs服务检测与自动恢复

来源:互联网 发布:工作站品牌 知乎 编辑:程序博客网 时间:2024/04/30 13:41

      • 场景分析
      • 检测思路
      • 测试用例验证
      • 实现代码

场景分析

平台目前使用的存储为nfs和gluster网络文件系统,出现的问题一般是C/S端服务异常。nfs服务异常时会导致目录列表(df -h 或者cd)无法输出,主要是因为系统后台一直在尝试挂载,虽然nfs有retry尝试次数选项,但是服务端恢复就没法及时挂载上(目录依旧无法显示)。看来只能自己动手,丰衣足食了。

检测思路

脚本主要包含两个方面,分别对应开机挂载和异常处理两种情形。思路如下:

  1. 使用系统定时任务实现,流程:尝试挂载》异常卸载目录并报错》下次检测继续挂载,超时放弃。

  2. nfs存储目录里都会有.nfs标识文件,判断.nfs是否存在即可,当服务异常时则先卸载目录(针对服务无响应目录无法显示情形),下一次则尝试挂载恢复,超时则放弃。

  3. 尝试挂载部分,针对目录集合遍历循环开启多线程,每个线程检测.nfs文件是否存在。python不支持kill线程,但是有个setDaemon方法,其作用是只要主线程完成了,不管子线程是否完成,都要和主线程一起退出。在超时时间内,能挂载好的就都挂上了,挂不上的话只要主线程结束就停止挂载了。

测试用例验证

  1. 开机启动是否能够自动挂载

  2. 本机对外提供存储,存储目录是否被挂载,存储自身重启

  3. 挂载好的目录掉线是否能够自动恢复挂载

  4. 服务端异常是否能够自动卸载并不影响ls目录

  5. 新增挂载目录是否会自动挂载(权限755并写入.nfs检测文件)

实现代码

#!/usr/bin/env python # -*-coding:utf-8-*-#Tue Sep 17 15:50:00 CST 2015import sys,os,time,threading,subprocessclass Nfs():    def __init__(self):        self.dirSet = ('11.11.11.200:/data/test')        global dstList,allDst,errDst        dstList = []        allDst = []    #生成正常目录list       def excute(slef,mnt):        dst = mnt.split(' ')[-1]        '''创建挂载点'''        if not os.path.exists(dst) :            cmd = 'mkdir -p '+dst            os.popen(cmd)        '''检测是否挂载'''        file = dst + '/.nfs'        if os.path.exists(file):            dstList.append(dst)        else:            print mnt            os.popen(mnt)            if os.path.exists(file):                dstList.append(dst)    #获取挂载执行命令list    def mountCmd(self):          tcpIp = os.popen("ifconfig |grep 'addr:192'|awk -F'[ :]+' 'NR==1{print $4}'").read().strip()         cmdList = []         for dir in self.dirSet:             #dir = self.dirIpReplace(dir)             '''跳过本机是服务目录'''             if tcpIp == dir.split(':')[0]:                 continue             dst = dir.split(':')[-1]             cmd = 'mount -t nfs -o vers=3 -o nolock '+dir+' '+dst             cmdList.append(cmd)             allDst.append(dst)         return cmdList     #检测入口       def check(self):         '''尝试重新挂载'''         for mnt in self.mountCmd():             t = threading.Thread(target=self.excute,args=(mnt,))             t.setDaemon(True)             t.start()         time.sleep(20)         errDst = list(set(allDst)-set(dstList))         '''卸载有问题目录'''         print errDst         if len(errDst):             for dir in errDst:                 cmd = 'umount -l ' + dir                 print cmd                 os.popen(cmd)Nfs().check()
0 0
原创粉丝点击