打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python做硬盘检测、解析smart结构数据

smartctl 6.3 2014-07-26 r3976 [x86_64-linux-2.6.18-164.el5] (local build)

Copyright (C) 2002-14, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===

Vendor:               TOSHIBA

Product:              MBF2300RC

Revision:             0109

User Capacity:        300,000,000,000 bytes [300 GB]

Logical block size:   512 bytes

Rotation Rate:        10025 rpm

Form Factor:          2.5 inches

Logical Unit id:      0x50000393d84b42bc

Serial number:        EB00PC208HFC

Device type:          disk

Transport protocol:   SAS (SPL-3)

Local Time is:        Tue Dec 30 00:10:03 2014 CST

SMART support is:     Available - device has SMART capability.

SMART support is:     Enabled

Temperature Warning:  Enabled

=== START OF READ SMART DATA SECTION ===

SMART Health Status: OK

Current Drive Temperature:     28 C

Drive Trip Temperature:        65 C

Manufactured in week 08 of year 2012

Specified cycle count over device lifetime:  50000

Accumulated start-stop cycles:  21

Specified load-unload count over device lifetime:  200000

Accumulated load-unload cycles:  69

Elements in grown defect list: 0

Error counter log:

        Errors Corrected by           Total   Correction     Gigabytes    Total

            ECC          rereads/    errors   algorithm      processed    uncorrected

        fast | delayed   rewrites  corrected  invocations   [10^9 bytes]  errors

read:          0        0         0         0          0     300744.962           0

write:         0        0         0         0          0      10841.446           0

Non-medium error count:        0

No self-tests have been logged

==================================

  1. #!/bin/env python


  2. import os,time,re,sys

  3. import logging


  4. logging.basicConfig(filename = os.path.join(os.getcwd(), 'load.log'), level = logging.INFO, format = '%(asctime)s - %(levelname)s: %(message)s')


  5. class attribute:

  6. pattern = ''

  7. value = '-1'


  8. if __name__ == '__main__':

  9. log_file = os.path.join(os.getcwd(), sys.argv[1])

  10. if os.path.exists(log_file):

  11. logging.info('start loading %s...' % (log_file))

  12. else:

  13. logging.error('%s not exists' % (log_file))


  14. update_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat(log_file).st_ctime))

  15. block_list = []

  16. attrs = {}


  17. #information section

  18. attrs['serial_number'] = attribute()

  19. attrs['serial_number'].pattern = 'Serial number:\s*(\w*)'

  20. attrs['vendor'] = attribute()

  21. attrs['vendor'].pattern = 'Vendor:\s*(\w*)'

  22. attrs['product'] = attribute()

  23. attrs['product'].pattern = 'Product:\s*(\w*).'

  24. attrs['revision'] = attribute()

  25. attrs['revision'].pattern = 'Revision:\s*(\w*)'

  26. attrs['compliance'] = attribute()

  27. attrs['compliance'].pattern = 'Compliance:\s*(\w*)'

  28. attrs['user_capacity'] = attribute()

  29. attrs['user_capacity'].pattern = 'User Capacity:.*\[(\w*)'

  30. attrs['logical_block_size'] = attribute()

  31. attrs['logical_block_size'].pattern = 'Logical block size:\s*(\w*)'

  32. attrs['rotation_rate'] = attribute()

  33. attrs['rotation_rate'].pattern = 'Rotation Rate:\s*(\w*)'

  34. attrs['form_factor'] = attribute()

  35. attrs['form_factor'].pattern = 'Form Factor:\s*([\w\.]*)'

  36. attrs['logical_unit_id'] = attribute()

  37. attrs['logical_unit_id'].pattern = 'Logical Unit id:\s*(\w*)'

  38. attrs['device_type'] = attribute()

  39. attrs['device_type'].pattern = 'Device type:\s*(\w*)'

  40. attrs['transport_protocol'] = attribute()

  41. attrs['transport_protocol'].pattern = 'Transport protocol:\s*(.*)'

  42. attrs['smart_support'] = attribute()

  43. attrs['smart_support'].pattern = 'SMART support is:\s*(\w*)'

  44. attrs['smart_enable'] = attribute()

  45. attrs['smart_enable'].pattern = 'SMART support is:\s*(Enabled|Disabled)'

  46. attrs['temperature_warning'] = attribute()

  47. attrs['temperature_warning'].pattern = 'Temperature Warning:\s*(\w*)'

  48. attrs['ip'] = attribute()

  49. attrs['ip'].pattern = '\[([\w\.]*)'


  50. #smart data section

  51. attrs['smart_health_status'] = attribute()

  52. attrs['smart_health_status'].pattern = 'SMART Health Status:\s*(\w*)'

  53. attrs['current_drive_temperature'] = attribute()

  54. attrs['current_drive_temperature'].pattern = 'Current Drive Temperature:\s*(\w*)'

  55. attrs['drive_trip_temperature'] = attribute()

  56. attrs['drive_trip_temperature'].pattern = 'Drive Trip Temperature:\s*(\w*)'

  57. attrs['elements_in_grown_defect_list'] = attribute()

  58. attrs['elements_in_grown_defect_list'].pattern = 'Elements in grown defect list:\s*(\w*)'

  59. attrs['manufactured_time'] = attribute()

  60. attrs['manufactured_time'].pattern = 'Manufactured in (.*)'

  61. attrs['cycle_count'] = attribute()

  62. attrs['cycle_count'].pattern = 'Specified cycle count over device lifetime:\s*(\w*)'

  63. attrs['start_stop_cycles'] = attribute()

  64. attrs['start_stop_cycles'].pattern = 'Accumulated start-stop cycles:\s*(\w*)'

  65. attrs['load_unload_count'] = attribute()

  66. attrs['load_unload_count'].pattern = 'Specified load-unload count over device lifetime:\s*(\w*)'

  67. attrs['load_unload_cycles'] = attribute()

  68. attrs['load_unload_cycles'].pattern = 'Accumulated load-unload cycles:\s*(\w*)'

  69. attrs['blocks_sent_to_initiator'] = attribute()

  70. attrs['blocks_sent_to_initiator'].pattern = 'Blocks sent to initiator =\s*(\w*)'

  71. attrs['blocks_received_from_initiator'] = attribute()

  72. attrs['blocks_received_from_initiator'].pattern = 'Blocks received from initiator =\s*(\w*)'

  73. attrs['blocks_read_from_cache'] = attribute()

  74. attrs['blocks_read_from_cache'].pattern = 'Blocks read from cache and sent to initiator =\s*(\w*)'

  75. attrs['num_commands_size_not_larger_than_segment_size'] = attribute()

  76. attrs['num_commands_size_not_larger_than_segment_size'].pattern = '<= segment size =\s*(\w*)'

  77. attrs['num_commands_size_larger_than_segment_size'] = attribute()

  78. attrs['num_commands_size_larger_than_segment_size'].pattern = '> segment size=\s*(\w*)'

  79. attrs['num_hours_powered_up'] = attribute()

  80. attrs['num_hours_powered_up'].pattern = 'number of hours powered up =\s*(\w*)'

  81. attrs['num_minutes_next_test'] = attribute()

  82. attrs['num_minutes_next_test'].pattern = 'number of minutes until next internal SMART test =\s*(\w*)'

  83. attrs['non_medium_error_count'] = attribute()

  84. attrs['non_medium_error_count'].pattern = 'Non-medium error count:\s*(\w*)'


  85. new_information_count = 0

  86. insert_smart_count = 0

  87. fail_count = 0


  88. for line in open(log_file):

  89. if line.find('run:') != -1 or not line.strip(): #contains 'run' or blank line

  90. block = '\n'.join(block_list)

  91. if block and re.search('smartctl 6.3', block):

  92. for (k, v) in attrs.items():

  93. attrs[k].value = '-1'

  94. match = re.search(attrs[k].pattern, block)

  95. if match:

  96. attrs[k].value = match.group(1)

  97. if attrs['vendor'].value == 'LSI':

  98. block_list = []

  99. logging.error('ip with LSI vendor: %s' % (attrs['ip'].value));

  100. continue


  101. #insert information section

  102. if attrs['serial_number'].value == '-1':

  103. block_list = []

  104. fail_count = fail_count + 1

  105. logging.info('invalid ip without serial number(-1): %s' % (attrs['ip'].value))

  106. continue


  107. #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

  108. #insert smart data section


  109. match = re.search('read:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d\.]+)\s+(\d+)', block)

  110. if match:

  111. read_corrected_ecc_fast = match.group(1)

  112. read_corrected_ecc_delayed = match.group(2)

  113. read_corrected_re = match.group(3)

  114. read_total_errors_corrected = match.group(4)

  115. read_correction_algo_invocations = match.group(5)

  116. read_gigabytes_processed = match.group(6)

  117. read_total_uncorrected_errors = match.group(7)

  118. else:

  119. read_corrected_ecc_fast = bytes(-1)

  120. read_corrected_ecc_delayed = bytes(-1)

  121. read_corrected_re = bytes(-1)

  122. read_total_errors_corrected = bytes(-1)

  123. read_correction_algo_invocations = bytes(-1)

  124. read_gigabytes_processed = bytes(-1)

  125. read_total_uncorrected_errors = bytes(-1)



  126. match = re.search('write:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d\.]+)\s+(\d+)', block)

  127. if match:

  128. write_corrected_ecc_fast = match.group(1)

  129. write_corrected_ecc_delayed = match.group(2)

  130. write_corrected_re = match.group(3)

  131. write_total_errors_corrected = match.group(4)

  132. write_correction_algo_invocations = match.group(5)

  133. write_gigabytes_processed = match.group(6)

  134. write_total_uncorrected_errors = match.group(7)

  135. else:

  136. write_corrected_ecc_fast = bytes(-1)

  137. write_corrected_ecc_delayed = bytes(-1)

  138. write_corrected_re = bytes(-1)

  139. write_total_errors_corrected = bytes(-1)

  140. write_correction_algo_invocations = bytes(-1)

  141. write_gigabytes_processed = bytes(-1)

  142. write_total_uncorrected_errors = bytes(-1)


  143. match = re.search('verify:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d\.]+)\s+(\d+)', block)

  144. if match:

  145. verify_corrected_ecc_fast = match.group(1)

  146. verify_corrected_ecc_delayed = match.group(2)

  147. verify_corrected_re = match.group(3)

  148. verify_total_errors_corrected = match.group(4)

  149. verify_correction_algo_invocations = match.group(5)

  150. verify_gigabytes_processed = match.group(6)

  151. verify_total_uncorrected_errors = match.group(7)

  152. else:

  153. verify_corrected_ecc_fast = bytes(-1)

  154. verify_corrected_ecc_delayed = bytes(-1)

  155. verify_corrected_re = bytes(-1)

  156. verify_total_errors_corrected = bytes(-1)

  157. verify_correction_algo_invocations = bytes(-1)

  158. verify_gigabytes_processed = bytes(-1)

  159. verify_total_uncorrected_errors = bytes(-1)


  160. insert_smart_count = insert_smart_count + 1

  161. 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


  162. block_list = []

  163. elif line.find('out:') != -1:

  164. 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

来源:https://blog.csdn.net/DF_XIAO/article/details/46692985?utm_source=blogxgwz6

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【每周一坑】特殊的生日
NAND FLASH ECC校验原理与实现
如何成为一个具有领导力的SSD主控?
Linux驱动程序开发 - 设备驱动模型初探
关于LDAP的一个java编程实例
打印--学习如何使用打印服务api
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服