python编程解析如下smart结构数据,得到一行smart信息

run: smartctl -a /dev/sda
out: smartctl 6.3 2014-07-26 r3976 [x86_64-linux-2.6.18-164.el5] (local build)
out: Copyright (C) 2002-14, Bruce Allen, Christian Franke, www.smartmontools.org
out:
out: === START OF INFORMATION SECTION ===
out: Vendor: TOSHIBA
out: Product: MBF2300RC
out: Revision: 0109
out: User Capacity: 300,000,000,000 bytes [300 GB]
out: Logical block size: 512 bytes
out: Rotation Rate: 10025 rpm
out: Form Factor: 2.5 inches
out: Logical Unit id: 0x50000393d84b42bc
out: Serial number: EB00PC208HFC
out: Device type: disk
out: Transport protocol: SAS (SPL-3)
out: Local Time is: Tue Dec 30 00:10:03 2014 CST
out: SMART support is: Available - device has SMART capability.
out: SMART support is: Enabled
out: Temperature Warning: Enabled
out:
out: === START OF READ SMART DATA SECTION ===
out: SMART Health Status: OK
out:
out: Current Drive Temperature: 28 C
out: Drive Trip Temperature: 65 C
out:
out: Manufactured in week 08 of year 2012
out: Specified cycle count over device lifetime: 50000
out: Accumulated start-stop cycles: 21
out: Specified load-unload count over device lifetime: 200000
out: Accumulated load-unload cycles: 69
out: Elements in grown defect list: 0
out:
out: Error counter log:
out: Errors Corrected by Total Correction Gigabytes Total
out: ECC rereads/ errors algorithm processed uncorrected
out: fast | delayed rewrites corrected invocations [10^9 bytes] errors
out: read: 0 0 0 0 0 300744.962 0
out: write: 0 0 0 0 0 10841.446 0
out:
out: Non-medium error count: 0
out:
out: No self-tests have been logged
out:
out:

python文件如下:

#!/bin/env python

import os,time,re,sys
import logging logging.basicConfig(filename = os.path.join(os.getcwd(), 'load.log'), level = logging.INFO, format = '%(asctime)s - %(levelname)s: %(message)s') class attribute:
pattern = ''
value = '-1' if __name__ == '__main__':
log_file = os.path.join(os.getcwd(), sys.argv[1])
if os.path.exists(log_file):
logging.info('start loading %s...' % (log_file))
else:
logging.error('%s not exists' % (log_file)) update_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat(log_file).st_ctime))
block_list = []
attrs = {} #information section
attrs['serial_number'] = attribute()
attrs['serial_number'].pattern = 'Serial number:\s*(\w*)'
attrs['vendor'] = attribute()
attrs['vendor'].pattern = 'Vendor:\s*(\w*)'
attrs['product'] = attribute()
attrs['product'].pattern = 'Product:\s*(\w*).'
attrs['revision'] = attribute()
attrs['revision'].pattern = 'Revision:\s*(\w*)'
attrs['compliance'] = attribute()
attrs['compliance'].pattern = 'Compliance:\s*(\w*)'
attrs['user_capacity'] = attribute()
attrs['user_capacity'].pattern = 'User Capacity:.*\[(\w*)'
attrs['logical_block_size'] = attribute()
attrs['logical_block_size'].pattern = 'Logical block size:\s*(\w*)'
attrs['rotation_rate'] = attribute()
attrs['rotation_rate'].pattern = 'Rotation Rate:\s*(\w*)'
attrs['form_factor'] = attribute()
attrs['form_factor'].pattern = 'Form Factor:\s*([\w\.]*)'
attrs['logical_unit_id'] = attribute()
attrs['logical_unit_id'].pattern = 'Logical Unit id:\s*(\w*)'
attrs['device_type'] = attribute()
attrs['device_type'].pattern = 'Device type:\s*(\w*)'
attrs['transport_protocol'] = attribute()
attrs['transport_protocol'].pattern = 'Transport protocol:\s*(.*)'
attrs['smart_support'] = attribute()
attrs['smart_support'].pattern = 'SMART support is:\s*(\w*)'
attrs['smart_enable'] = attribute()
attrs['smart_enable'].pattern = 'SMART support is:\s*(Enabled|Disabled)'
attrs['temperature_warning'] = attribute()
attrs['temperature_warning'].pattern = 'Temperature Warning:\s*(\w*)'
attrs['ip'] = attribute()
attrs['ip'].pattern = '\[([\w\.]*)' #smart data section
attrs['smart_health_status'] = attribute()
attrs['smart_health_status'].pattern = 'SMART Health Status:\s*(\w*)'
attrs['current_drive_temperature'] = attribute()
attrs['current_drive_temperature'].pattern = 'Current Drive Temperature:\s*(\w*)'
attrs['drive_trip_temperature'] = attribute()
attrs['drive_trip_temperature'].pattern = 'Drive Trip Temperature:\s*(\w*)'
attrs['elements_in_grown_defect_list'] = attribute()
attrs['elements_in_grown_defect_list'].pattern = 'Elements in grown defect list:\s*(\w*)'
attrs['manufactured_time'] = attribute()
attrs['manufactured_time'].pattern = 'Manufactured in (.*)'
attrs['cycle_count'] = attribute()
attrs['cycle_count'].pattern = 'Specified cycle count over device lifetime:\s*(\w*)'
attrs['start_stop_cycles'] = attribute()
attrs['start_stop_cycles'].pattern = 'Accumulated start-stop cycles:\s*(\w*)'
attrs['load_unload_count'] = attribute()
attrs['load_unload_count'].pattern = 'Specified load-unload count over device lifetime:\s*(\w*)'
attrs['load_unload_cycles'] = attribute()
attrs['load_unload_cycles'].pattern = 'Accumulated load-unload cycles:\s*(\w*)'
attrs['blocks_sent_to_initiator'] = attribute()
attrs['blocks_sent_to_initiator'].pattern = 'Blocks sent to initiator =\s*(\w*)'
attrs['blocks_received_from_initiator'] = attribute()
attrs['blocks_received_from_initiator'].pattern = 'Blocks received from initiator =\s*(\w*)'
attrs['blocks_read_from_cache'] = attribute()
attrs['blocks_read_from_cache'].pattern = 'Blocks read from cache and sent to initiator =\s*(\w*)'
attrs['num_commands_size_not_larger_than_segment_size'] = attribute()
attrs['num_commands_size_not_larger_than_segment_size'].pattern = '<= segment size =\s*(\w*)'
attrs['num_commands_size_larger_than_segment_size'] = attribute()
attrs['num_commands_size_larger_than_segment_size'].pattern = '> segment size=\s*(\w*)'
attrs['num_hours_powered_up'] = attribute()
attrs['num_hours_powered_up'].pattern = 'number of hours powered up =\s*(\w*)'
attrs['num_minutes_next_test'] = attribute()
attrs['num_minutes_next_test'].pattern = 'number of minutes until next internal SMART test =\s*(\w*)'
attrs['non_medium_error_count'] = attribute()
attrs['non_medium_error_count'].pattern = 'Non-medium error count:\s*(\w*)' new_information_count = 0
insert_smart_count = 0
fail_count = 0 for line in open(log_file):
if line.find('run:') != -1 or not line.strip(): #contains 'run' or blank line
block = '\n'.join(block_list)
if block and re.search('smartctl 6.3', block):
for (k, v) in attrs.items():
attrs[k].value = '-1'
match = re.search(attrs[k].pattern, block)
if match:
attrs[k].value = match.group(1)
if attrs['vendor'].value == 'LSI':
block_list = []
logging.error('ip with LSI vendor: %s' % (attrs['ip'].value));
continue #insert information section
if attrs['serial_number'].value == '-1':
block_list = []
fail_count = fail_count + 1
logging.info('invalid ip without serial number(-1): %s' % (attrs['ip'].value))
continue #print 'hive-xdf-information'+";"+attrs['serial_number'].value+";"+ update_time+";"+ attrs['vendor'].value+";"+ attrs['product'].value+";"+ attrs['revision'].value+";"+attrs['compliance'].value+";"+ attrs['user_capacity'].value+";"+ attrs['logical_block_size'].value+";"+ attrs['rotation_rate'].value+";"+ attrs['form_factor'].value+";"+attrs['logical_unit_id'].value+";"+ attrs['device_type'].value+";"+ attrs['transport_protocol'].value+";"+ attrs['smart_support'].value+";"+ attrs['smart_enable'].value+";"+attrs['temperature_warning'].value+";"+ attrs['ip'].value+";"+ update_time
#insert smart data section match = re.search('read:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d\.]+)\s+(\d+)', block)
if match:
read_corrected_ecc_fast = match.group(1)
read_corrected_ecc_delayed = match.group(2)
read_corrected_re = match.group(3)
read_total_errors_corrected = match.group(4)
read_correction_algo_invocations = match.group(5)
read_gigabytes_processed = match.group(6)
read_total_uncorrected_errors = match.group(7)
else:
read_corrected_ecc_fast = bytes(-1)
read_corrected_ecc_delayed = bytes(-1)
read_corrected_re = bytes(-1)
read_total_errors_corrected = bytes(-1)
read_correction_algo_invocations = bytes(-1)
read_gigabytes_processed = bytes(-1)
read_total_uncorrected_errors = bytes(-1) match = re.search('write:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d\.]+)\s+(\d+)', block)
if match:
write_corrected_ecc_fast = match.group(1)
write_corrected_ecc_delayed = match.group(2)
write_corrected_re = match.group(3)
write_total_errors_corrected = match.group(4)
write_correction_algo_invocations = match.group(5)
write_gigabytes_processed = match.group(6)
write_total_uncorrected_errors = match.group(7)
else:
write_corrected_ecc_fast = bytes(-1)
write_corrected_ecc_delayed = bytes(-1)
write_corrected_re = bytes(-1)
write_total_errors_corrected = bytes(-1)
write_correction_algo_invocations = bytes(-1)
write_gigabytes_processed = bytes(-1)
write_total_uncorrected_errors = bytes(-1) match = re.search('verify:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d\.]+)\s+(\d+)', block)
if match:
verify_corrected_ecc_fast = match.group(1)
verify_corrected_ecc_delayed = match.group(2)
verify_corrected_re = match.group(3)
verify_total_errors_corrected = match.group(4)
verify_correction_algo_invocations = match.group(5)
verify_gigabytes_processed = match.group(6)
verify_total_uncorrected_errors = match.group(7)
else:
verify_corrected_ecc_fast = bytes(-1)
verify_corrected_ecc_delayed = bytes(-1)
verify_corrected_re = bytes(-1)
verify_total_errors_corrected = bytes(-1)
verify_correction_algo_invocations = bytes(-1)
verify_gigabytes_processed = bytes(-1)
verify_total_uncorrected_errors = bytes(-1) insert_smart_count = insert_smart_count + 1
print sys.argv[2]+"@@"+attrs['serial_number'].value+";"+ update_time+";"+ attrs['smart_health_status'].value+";"+ attrs['current_drive_temperature'].value+";"+ attrs['drive_trip_temperature'].value+";"+attrs['elements_in_grown_defect_list'].value+";"+ attrs['manufactured_time'].value+";"+ attrs['cycle_count'].value+";"+ attrs['start_stop_cycles'].value+";"+ attrs['load_unload_count'].value+";"+attrs['load_unload_cycles'].value+";"+ attrs['blocks_sent_to_initiator'].value+";"+ attrs['blocks_received_from_initiator'].value+";"+ attrs['blocks_read_from_cache'].value+";"+attrs['num_commands_size_not_larger_than_segment_size'].value+";"+attrs['num_commands_size_larger_than_segment_size'].value+";"+ attrs['num_hours_powered_up'].value+";"+ attrs['num_minutes_next_test'].value+";"+ attrs['non_medium_error_count'].value+';'+read_corrected_ecc_fast+';'+ read_corrected_ecc_delayed+';'+read_corrected_re+';'+ read_total_errors_corrected+';'+ read_correction_algo_invocations+';'+ read_gigabytes_processed+';'+ read_total_uncorrected_errors+';'+ write_corrected_ecc_fast+';'+ write_corrected_ecc_delayed+';'+ write_corrected_re+';'+ write_total_errors_corrected+';'+ write_correction_algo_invocations+';'+ write_gigabytes_processed+';'+ write_total_uncorrected_errors+';'+ verify_corrected_ecc_fast+';'+ verify_corrected_ecc_delayed+';'+ verify_corrected_re+';'+verify_total_errors_corrected+';'+ verify_correction_algo_invocations+';'+ verify_gigabytes_processed+';'+ verify_total_uncorrected_errors block_list = []
elif line.find('out:') != -1:
block_list.append(line.strip())

解析结果如下:

hive-xdf-smart_data@@EB00PC208HFC;2015-06-23 18:56:09;OK;28;65;0;week 08 of year 2012;50000;21;200000;69;-1;-1;-1;-1;-1;-1;-1;0;0;0;0;0;0;300744.962;0;0;0;0;0;0;10841.446;0;-1;-1;-1;-1;-1;-1;-1

python解析smart结构数据的更多相关文章

  1. 使用Python解析JSON数据

    使用Python解析百度API返回的JSON格式的数据 # coding:utf-8 # !/usr/bin/env python import matplotlib.pyplot as plt fr ...

  2. 使用Python解析JSON数据的基本方法

    这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下:     ----------------------------------- ...

  3. python解析robot framework的output.xml,并生成html

    一.背景 Jenkins自动构建RF脚本,生成的RF特有HTML报告不能正常打开. 需求:用Python解析测试报告的xml数据,放在普通HTML文件中打开 二.output.xml数据 三.用pyh ...

  4. python 解析json loads dumps

    认识 引用模块 重要函数 案例 排序 缩进参数 压缩 参考 认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standa ...

  5. Python解析器源码加密系列之(二):一次使用标准c的FILE*访问内存块的尝试

    摘要:由于近期打算修改Python解释器以实现pyc文件的加密/解密,出于保密的要求,解密之后的数据只能放在内存中,不能写入到文件中.但是后续的解析pyc文件的代码又只能接受FILE*作为入参,所以就 ...

  6. python 解析XML python模块xml.dom解析xml实例代码

    分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...

  7. python解析xml模块封装代码

    在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...

  8. python解析xml之lxml

    虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ...

  9. Python解析生成XML-ElementTree VS minidom

    OS:Windows 7 关键字:Python3.4,XML,ElementTree,minidom 本文介绍用Python解析生成以下XML: <Persons> <Person& ...

随机推荐

  1. select option居中显示

    <style> .ch-select{ padding:0px;} .ch-select input[type=text]{ width:100%; position:relative; ...

  2. fwite写入文件

    用双引号(")定义字符串,PHP 懂得更多特殊字符的转义序列: 转移序列 说明 \n 换行 \r 回车 \t 水平制表符 \[/td> 反斜线 \$ 美元符号 \" 双引号 ...

  3. Difference between Linearizability and Serializability

    原文:http://stackoverflow.com/questions/8200015/what-is-the-difference-between-serializability-and-lin ...

  4. [js]变量声明、函数声明、函数定义式、形参之间的执行顺序

    一.当函数声明和函数定义式(变量赋值)同名时 function ledi(){ alert('ledi1'); }; ledi(); var ledi = function (){ alert('le ...

  5. [转] jQuery源码分析-如何做jQuery源码分析

    jQuery源码分析系列(持续更新) jQuery的源码有些晦涩难懂,本文分享一些我看源码的方法,每一个模块我基本按照这样的顺序去学习. 当我读到难度的书或者源码时,会和<如何阅读一本书> ...

  6. 再谈IT行业工程师文化

    为什么是再呢?因为“工程师文化”这种说法网上很多,各种理解,各种版 本,我只是简单说说我的认识,说的不对的地方敬请各位大牛,高手,高高手多多指教,我本身也是个技术人员,不过只是技术不怎么样而已.写这个 ...

  7. 使用windows服务和MSMQ和进行日志管理(解决高并发问题)

    首先,建立一个windows服务项目 然后进行设计视图 在工作区空白处右属,添加一个安装项目 然后就可以写我们的代码了,我们的服务需要实时监视MSMQ的队列中有没有记录,如果有,就向数据库中插入 核心 ...

  8. Linux 常用

    1,解决ssh登录慢的问题记录 vim /etc/ssh/ssh_config    #   GSSAPIAuthentication no  把下面这一行的注释去掉 2,Linux查看当前是什么系统 ...

  9. CIO谈:基于K2 BPM平台怎么做报销?

    即时!可视!可控!高效! 面对报销系统四大业务目标,有一个对策——用K2! 演讲人:沈明 大鹏天然气CIO 查看完章分享内容请关注K2官方微信

  10. 响应式架构:消息模式Actor实现与Scala、Akka应用集成

    这是一本最近很流行的书. 有时间就去看看