python解析smart结构数据
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结构数据的更多相关文章
- 使用Python解析JSON数据
使用Python解析百度API返回的JSON格式的数据 # coding:utf-8 # !/usr/bin/env python import matplotlib.pyplot as plt fr ...
- 使用Python解析JSON数据的基本方法
这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下: ----------------------------------- ...
- python解析robot framework的output.xml,并生成html
一.背景 Jenkins自动构建RF脚本,生成的RF特有HTML报告不能正常打开. 需求:用Python解析测试报告的xml数据,放在普通HTML文件中打开 二.output.xml数据 三.用pyh ...
- python 解析json loads dumps
认识 引用模块 重要函数 案例 排序 缩进参数 压缩 参考 认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standa ...
- Python解析器源码加密系列之(二):一次使用标准c的FILE*访问内存块的尝试
摘要:由于近期打算修改Python解释器以实现pyc文件的加密/解密,出于保密的要求,解密之后的数据只能放在内存中,不能写入到文件中.但是后续的解析pyc文件的代码又只能接受FILE*作为入参,所以就 ...
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
- python解析xml模块封装代码
在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...
- python解析xml之lxml
虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ...
- Python解析生成XML-ElementTree VS minidom
OS:Windows 7 关键字:Python3.4,XML,ElementTree,minidom 本文介绍用Python解析生成以下XML: <Persons> <Person& ...
随机推荐
- XPATH 带命名空间数据的读取
在XML中,很多情况下有命名空间,如果直接使用XPATH 读取是会读到空节点. 解决办法如下: InputStream is=loader.getResourceAsStream("com/ ...
- redhat enterprixe 5.0 下DHCP服务器rpm安装配置及其测试
一.了解DHCP DHCP服务提供动态指定IP地址和配置参数的机制.有动态和静态两种方式. 二.rpm安装 因为配过Samba,所以感觉挺简单. 首先找到主程序和几个附属程序的rpm的安装包.应该都是 ...
- DataGridView批量执行Insert和Remove行时特别慢的解决方案
向DataGridView循环插入110条数据耗时5秒多. 在循环前执行: var oldAutoSizeRowsMode = this.AutoSizeRowsMode; var oldAutoSi ...
- 反质数(Antiprimes)
转载http://www.cnblogs.com/tiankonguse/archive/2012/07/29/2613877.html 问题描述: 对于任何正整数x,起约数的个数记做g(x).例如g ...
- 一模 (4) day1
第一题: 题目大意:给出N个人之间转账的手续X%,求出A转给B至少要多少钱才能使B得到100元.结果保留8位小数:N<=2000 解题过程: 1.很容易看出这题的图论模型,每条边的权值就是(1- ...
- C#点击按钮用DataGridView动态增加行、删除行,增加按钮列
原来有一行: 点击添加,在下面增加同样的一行 新增加的行有一列删除按钮,点击某行的删除按钮时,删除当前行 方法: 哈哈,我果然好聪明啊 1.文本框.文本框.添加按钮 2.一个DataGridView( ...
- 查看UI调试界面利器 revealapp
官网 http://revealapp.com 做iOS的开发,UI是非常非常重要的一环.调试时我们一般用模拟器,提交前用真机做测试.用模拟器来调试UI效果虽然快捷方便,但有时仍然希望有更强大的工具来 ...
- xcode开发的6个小技巧
Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...
- 调整label中text显示的行间距
调整label中text显示的行间距最近再做一个项目时,发现UILabel中text的系统默认行间距不能满足要求,于是在网上找到了调整行间距的代码.跟大家分享一下,希望能对你有所帮助.悦德财富:htt ...
- Titanium vs PhoneGap
http://mobile.51cto.com/Titanium-318049.htm http://www.ibm.com/developerworks/cn/opensource/os-titan ...