Anons79 Mini Shell

Directory : /usr/local/nagios/plugins/nc-smart-data-exporter/
Upload File :
Current File : //usr/local/nagios/plugins/nc-smart-data-exporter/export_smart_data.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import os
import json
import argparse
import glob
from pySMART import Device, DeviceList
import logging


def parse_args():
    _filename = os.path.basename(__file__)
    _default_logdir = '/var/log'

    parser = argparse.ArgumentParser(description='Smart data exporter')
    parser.add_argument("-g", "--glob", default='',
                        help="Devices in GLOB style. If omitted - perform autoscan")
    parser.add_argument("-f", "--output-file",
                        help="Output file name to save the result",
                        default='/tmp/smart_data_cache.json')
    parser.add_argument("-l", "--log-file", help="Log file",
                        default='{}/{}.log'.format(_default_logdir, _filename[0:-3]))
    parser.add_argument("--log-level", help="Log level", default="INFO")
    return parser.parse_args()

def parse_smart_attribute(attr):
    if not attr:
        return None
    _attr_name = getattr(attr, 'name', None)
    _attr_raw_value = getattr(attr, 'raw', None)
    return (_attr_name, _attr_raw_value)

class Mydict(dict):
    def __missing__(self, key):
        value = self[key] = type(self)()
        return value


if __name__ == '__main__':
    args = parse_args()
    logging.basicConfig(filename=args.log_file, filemode='a',
                        level=getattr(logging, args.log_level, logging.DEBUG))
    logging.info('=== Starting ===')

    if not args.glob:
        logging.info('Using autodetection')
        device_list = DeviceList().devices
    else:
        dev_paths = glob.glob(args.glob)
        logging.info('Trying to add devices by expression "{}": [{}]'.format(
            args.glob, ', '.join(dev_paths)))
        device_list = []
        for d in dev_paths:
            if "twa" in d:
                for i in range(32):
                    dev_obj = Device(d, str(i))
                    if dev_obj.model != None:
                        device_list.append(dev_obj)
                        logging.info('Added device /dev/{} 3ware,{} SN:{}'.format(dev_obj.name, i, dev_obj.serial))
            else:
                dev_obj = Device(d, "")
                device_list.append(dev_obj)
                logging.info('Added device /dev/{}'.format(dev_obj.name))

    devices = {}

    for device in device_list:
        _serial = device.serial
        _name = device.name
        _type = 'SSD' if device.is_ssd else 'HDD'
        _interface = device.interface
        _smart_attributes = dict(parse_smart_attribute(a) for a in device.attributes if a)
        _diags = device.diags
        _smart_data = _diags or _smart_attributes
#        devices[_serial] = {
#            'Name': _name,
#            'Type': _type,
#            'Interface': _interface,
#            'SMART': _smart_data
#        }

        if _type not in devices:
            devices[_type] = {}
        devices[_type].update({_serial: _smart_data})

        logging.info('Added SMART attributes for {}'.format(device))

    with open(args.output_file, 'w') as f:
        json.dump(devices, f, indent=2)

    logging.info('=== Finishing ===')

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]