zoukankan      html  css  js  c++  java
  • 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 = '
    '.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


  • 相关阅读:
    windows蓝屏错误小全
    大数据量下高并发同步的讲解(不看,保证你后悔)
    本地或者服务器同时启动2个或多个tomcat
    如何在同一系统里同时启动多个Tomcat
    Java配置----JDK开发环境搭建及环境变量配置
    Tomcat8内置jdk8运行环境发布web项目
    Tomcat 部署项目的三种方法
    Java Web(五) JSP详解(四大作用域九大内置对象等)
    Java 四大作用域总结
    JSP九大隐式对象
  • 原文地址:https://www.cnblogs.com/xiaodf/p/5027168.html
Copyright © 2011-2022 走看看