前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python raid monitor

python raid monitor

作者头像
py3study
发布2020-01-12 20:38:52
6090
发布2020-01-12 20:38:52
举报
文章被收录于专栏:python3python3

#!/usr/bin/env python

#-*-coding:utf8-*-

"""

   FUN: Monitor Raid Stat

"""

import os ,time ,sys, traceback,commands,subprocess

def oper_log(log_name,item,info):

   path  = '/tmp/'

   stime = time.strftime('%Y-%m-%d %H:%M:%S')

   formats= '[%s] %s:: %s \n' % (stime,item,info)

   if not os.path.exists(path):

       os.mkdir(path)

   try:

       if log_name.split('.')[1] == 'log':

           fp = open(path + log_name,'a')

       elif log_name.split('.')[1] == 'info':

           fp = open(path + log_name,'w')

       else:

           fp = open(path + log_name,'a')

       fp.write(formats)

       fp.close()

   except Exception,e:

       pass

def _exec(cmds):

   try:

       sps = subprocess.Popen(cmds, stdin = subprocess.PIPE,

                                   stdout = subprocess.PIPE,

                                   stderr = subprocess.PIPE,

                                   shell = True)

       sps.wait()

       return sps.stdout.read().strip()

   except:

       oper_log('raid_monitor.log','_exec',traceback.format_exc())

       return ''

def disk_online():

   ''' Getting raid online stat '''

   try:

       cmds = 'megacli  -PDList -aALL | grep Online | wc -l'

       return  _exec(cmds)

   except:

       oper_log('raid_monitor.log','disk_online',traceback.format_exc())

       return ''

def disk_failed():

   try:

       cmds = "megacli -AdpAllInfo -a0 |grep 'Failed Disks'|awk '{print $4}'"

       res = _exec(cmds)

       return res

   except:

       print traceback.format_exc()

       #oper_log('raid_monitor.log','disk_failed',traceback.format_exc())

       return ''

def disk_badSectors():

   turl_num = []

   fail_num = []

   try:

       cmds = "megacli  -PDList -aALL|grep Other|awk '{print $4}'"

       res =  _exec(cmds).split('\n')

       for  i in res:

           i = int(i)

           if i != 0:

               fail_num.append(i)

           else:

               turl_num.append(i)

       return len(fail_num)

   except:

       oper_log('raid_monitor.log','disk_badSectors',traceback.format_exc())

       return ''

def work():

   try:

       if sys.argv[1] == 'disk_online':

           print disk_online()

       elif sys.argv[1] == 'disk_failed':

           print disk_failed()

       elif sys.argv[1] == 'disk_badSectors':

           print disk_badSectors()

       elif sys.argv[1] == 'all':

           print disk_online(),disk_failed(),disk_badSectors()

       else:

           print "Please enter the correct parameters, thank you!"

   except IndexError,e:

       print "Please enter the correct parameters, thank you: --- --- --- "

if __name__ == "__main__":

   st = work()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档