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& ...
随机推荐
- 修改PE文件的入口函数OEP
修改入口函数地址.这个是最省事的办法,在原PE文件中新增加一个节,计算新节的RVA,然后修改入口代码,使其指向新增加的节.当然,如果.text节空隙足够大的话,不用添加新节也可以. BOOL Chan ...
- UVa 10561 - Treblecross
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 二模 (2) day2
第一题: 题目描述: 在一个长方形框子里,最多有 N(0≤N≤6)个相异的点.在其中任何-个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能放 ...
- Redis系列-存储篇list主要操作函数小结
在总结list之前,先要弄明白几个跟list相关的概念: 列表:一个从左到右的队列,个人理解更类似于一个栈,常规模式下,先进列表的元素,后出. 表头元素:列表最左端第一个元素. 表尾元素:列表最右端的 ...
- 在线体验K2 BPM微信审批
“微信审批”在江湖中传言已久,但很多人依然“只闻其声,未见其人”,这传说中的手感到底有多好?今天,我们就一起来揭开它的真面目吧. 故事发生在上周六傍晚,我接到了加班电话. 晚上21:30终于加完班了, ...
- android中正确导入第三方jar包
android中正确导入第三方jar包 andriod中如果引入jar包的方式不对就会出现一些奇怪的错误. 工作的时候恰好有一个jar包需要调用,结果用了很长时间才解决出现的bug. 刚开始是这样引用 ...
- iphone获取当前磁盘信息
获取iphone磁盘总大小.已使用空间.空闲空间 [代码]悦德财富:https://www.yuedecaifu.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- Ajax方法实现登录页面
Note: ajax技术 不用刷新页面,做局部刷新不用form表单,因为不需要提交,通过JQuery控制必须要有id如果要用ajax可以用JQuery也可以用js写,推荐JQuery 因为简单,直接引 ...
- iOS 7 教程:定制iOS 7中的导航栏和状态栏
目录(?)[-] iOS 7中默认的导航栏 设置导航栏的背景颜色 在导航栏中使用背景图片 定制返回按钮的颜 修改导航栏标题的字体 修改导航栏标题为图片 添加多个按钮 修改状态栏的风格 隐藏状态栏 总结 ...
- (转)Sqlite中INTEGER PRIMARY KEY AUTOINCREMENT和rowid的使用
原文:http://www.cnblogs.com/peida/archive/2008/11/29/1343832.html Sqlite中INTEGER PRIMARY KEY AUTOINCRE ...