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. [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想

    Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...

  2. android webview web里面的数据透传到java以及java的数据透传到web

    详见: http://tutorials.jenkov.com/android/android-web-apps-using-android-webview.html#android-web-app- ...

  3. GL10控制图形旋转

    GL10提供了glRotatef(float  angle , float  x ,  float  y , float  z)方法,该方法用于控制旋转,该方法种angle控制旋转角度:而x.y.z参 ...

  4. js 数组去除空值

    for(var i = 0 ;i<wordarr.length;i++)                {                    if(wordarr[i] == "& ...

  5. c++普通高精除单精

    //没有在网上测试 //手测几组无误 //如有错误,还望指出,不胜感激. #include<cstdio>#include<cstring>int a1[600],a2,a4[ ...

  6. Problem B 队列

    Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, nu ...

  7. 自定义的dialog

    自定义的dialog  其中包含置顶 删除 和取消 下面的是BaseDialog package com.free.csdn.view.dialog; import android.app.Dialo ...

  8. [流媒体]live555简介(转)

    live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现 了对多种音视频编码 ...

  9. hql抓取要注意的点

    fetchtype是lazy,那就用到了在通过缓存中的关联去取,用不到不取:lazy遇到joinfetch就失去意义,但是由于hql语句是自己编写的,可以控制加不加fetch 所以如果主力是hql语句 ...

  10. Embedded binary is not signed with the same certificate as the parent app

    I face the same issue too,I solve it by this: First, I reCreate my team develop certificate(Because ...