#!/usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author:  Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
#          https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.

# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md

"""See the check's README for more details.
"""

import argparse  # pylint: disable=C0413
import base64  # pylint: disable=C0413
import sys  # pylint: disable=C0413

import lib.base  # pylint: disable=C0413
import lib.redfish  # pylint: disable=C0413
import lib.txt  # pylint: disable=C0413
import lib.url  # pylint: disable=C0413
from lib.globals import (STATE_CRIT, STATE_OK,  # pylint: disable=C0413
                          STATE_UNKNOWN, STATE_WARN)

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2024031401'

DESCRIPTION = """Checks the state of all drives or other physical storage media in the Systems
                 collection."""

DEFAULT_INSECURE = True
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 8
DEFAULT_URL = 'https://localhost:5000'


def parse_args():
    """Parse command line arguments using argparse.
    """
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument(
        '-V', '--version',
        action='version',
        version='%(prog)s: v{} by {}'.format(__version__, __author__)
    )

    parser.add_argument(
        '--always-ok',
        help='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '--insecure',
        help='This option explicitly allows to perform "insecure" SSL connections. '
             'Default: %(default)s',
        dest='INSECURE',
        action='store_true',
        default=DEFAULT_INSECURE,
    )

    parser.add_argument(
        '--no-proxy',
        help='Do not use a proxy. '
             'Default: %(default)s',
        dest='NO_PROXY',
        action='store_true',
        default=DEFAULT_NO_PROXY,
    )

    parser.add_argument(
        '--password',
        help='Redfish API password.',
        dest='PASSWORD',
    )

    parser.add_argument(
        '--timeout',
        help='Network timeout in seconds. '
             'Default: %(default)s (seconds)',
        dest='TIMEOUT',
        type=int,
        default=DEFAULT_TIMEOUT,
    )
    parser.add_argument(
        '--url',
        help='Redfish API URL. '
             'Default: %(default)s',
        dest='URL',
        default=DEFAULT_URL,
    )

    parser.add_argument(
        '--username',
        help='Redfish API username.',
        dest='USERNAME',
    )

    return parser.parse_args()


def main():
    """The main function. Hier spielt die Musik.
    """

    # parse the command line, exit with UNKNOWN if it fails
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    base_url = args.URL
    if not args.URL.startswith('http'):
        lib.base.cu('--url parameter has to start with "http://" or https://".')

    # Authorization (if needed)
    header = {}
    header['Accept'] = 'application/json'
    if args.USERNAME and args.PASSWORD:
        auth = '{}:{}'.format(args.USERNAME, args.PASSWORD)
        encoded_auth = lib.txt.to_text(base64.b64encode(lib.txt.to_bytes(auth)))
        header['Authorization'] = 'Basic {}'.format(encoded_auth)

    # "/redfish/v1/Systems"
    url = '{}/redfish/v1/Systems'.format(base_url)
    result = lib.base.coe(lib.url.fetch_json(
        url,
        header=header,
        insecure=args.INSECURE,
        no_proxy=args.NO_PROXY,
        timeout=args.TIMEOUT,
    ))
    # "Members": [
    #     {
    #         "@odata.id": "/redfish/v1/Systems/437XR1138R2"
    #     }
    # ],
    if len(result.get('Members', [])) == 0:
        lib.base.cu('Nothing to check, no Redfish members found.')

    # init some vars
    msg = ''
    state = STATE_OK
    perfdata = ''

    member_count = 0
    # fetch and analyze data by following the "Members" found
    for member in result.get('Members', []):
        # "/redfish/v1/Systems/437XR1138R2"
        url = '{}{}'.format(base_url, member['@odata.id'])
        systems = lib.base.coe(lib.url.fetch_json(
            url,
            header=header,
            insecure=args.INSECURE,
            no_proxy=args.NO_PROXY,
            timeout=args.TIMEOUT,
        ))
        systems = lib.redfish.get_systems(systems)
        if systems['Status_State'] not in ['Enabled', 'Quiesced']:
            continue
        member_count += 1
        systems_state = lib.redfish.get_state(systems)
        state = lib.base.get_worst(state, systems_state)

        msg += 'Member:'
        msg += ' {}'.format(systems['Manufacturer']) if systems['Manufacturer'] else ''
        msg += ' {}'.format(systems['Model']) if systems['Model'] else ''
        msg += ', '
        msg += 'HostName: {}, '.format(systems['HostName']) if systems['HostName'] else ''
        msg += 'Processors: {}x'.format(systems['ProcessorSummary_Count'])
        msg += ' {}'.format(systems['ProcessorSummary_Model']) if systems['ProcessorSummary_Model'] else ''
        msg += ' ({} logical)'.format(systems['ProcessorSummary_LogicalProcessorCount']) if systems['ProcessorSummary_LogicalProcessorCount'] else ''
        msg += ', '
        msg += 'BIOS: {}, '.format(systems['BiosVersion'])
        msg += 'Power: {}, '.format(systems['PowerState']) if systems['PowerState'] else ''
        msg += 'LED: {}, '.format(systems['IndicatorLED']) if systems['IndicatorLED'] else ''
        msg += 'SKU: {}, '.format(systems['SKU']) if systems['SKU'] else ''
        msg += 'SerNo: {}, '.format(systems['SerialNumber']) if systems['SerialNumber'] else ''
        msg = msg[:-2] + lib.base.state2str(systems_state, prefix=' ')

        # get all available storage links for the member
        if not systems['Storage_@odata.id']:
            msg += '\n\n'
            continue

        # "/redfish/v1/Systems/437XR1138R2/Storage"
        url = '{}{}'.format(base_url, systems['Storage_@odata.id'])
        storages = lib.base.coe(lib.url.fetch_json(
            url,
            header=header,
            insecure=args.INSECURE,
            no_proxy=args.NO_PROXY,
            timeout=args.TIMEOUT,
        ))
        table_data = []
        table_data_drive = []
        for storage in storages.get('Members', []):
            # "/redfish/v1/Systems/437XR1138R2/Storage/RAID.SL.7-1"
            url = '{}{}'.format(base_url, storage['@odata.id'])
            storage_data = lib.base.coe(lib.url.fetch_json(
                url,
                header=header,
                insecure=args.INSECURE,
                no_proxy=args.NO_PROXY,
                timeout=args.TIMEOUT,
            ))

            # get drives attached to the storage member
            for drive in storage_data.get('Drives', []):
                # "/redfish/v1/Systems/437XR1138R2/Storage/RAID.SL.7-1/Drives/Disk.Bay.0:Enclosure.Internal.0-1:RAID.SL.7-1"
                url = '{}{}'.format(base_url, drive['@odata.id'])
                drive_data = lib.base.coe(lib.url.fetch_json(
                    url,
                    header=header,
                    insecure=args.INSECURE,
                    no_proxy=args.NO_PROXY,
                    timeout=args.TIMEOUT,
                ))
                drive_data = lib.redfish.get_systems_storage_drives(drive_data)
                if drive_data['Status_State'] not in ['Enabled', 'Quiesced']:
                    continue
                # is the storage_data state healthy at all?
                drive_data_state = lib.redfish.get_state(drive_data)
                state = lib.base.get_worst(state, drive_data_state)
                drive_data['State'] = lib.base.state2str(drive_data_state, empty_ok=False)
                table_data_drive.append(drive_data)

            storage_data = lib.redfish.get_systems_storage(storage_data)
            if storage_data['Status_State'] not in ['Enabled', 'Quiesced']:
                continue
            # is the storage_data state healthy at all?
            storage_data_state = lib.redfish.get_state(storage_data)
            state = lib.base.get_worst(state, storage_data_state)
            storage_data['State'] = lib.base.state2str(storage_data_state, empty_ok=False)
            table_data.append(storage_data)

        if table_data_drive:
            keys = ['Name', 'MediaType', 'Protocol', 'Manufacturer', 'Model', 'SerialNumber', 'CapacityBytes', 'PredictedMediaLifeLeftPercent', 'State']
            headers = ['Disk', 'Type', 'Proto', 'Manufacturer', 'Model', 'SerialNumber', 'Size', 'LifeLeft %', 'State']
            msg += '\n\n' + lib.base.get_table(table_data_drive, keys, header=headers)

        if table_data:
            keys = ['Id', 'Name', 'Description', 'Drives@odata.count', 'State']
            headers = ['ID', 'Name', 'Description', 'Drives', 'State']
            msg += '\n\n' + lib.base.get_table(table_data, keys, header=headers)

        msg += '\n\n'

    if state == STATE_CRIT:
        msg = 'Checked storage on {} {}. There are critical errors.\n\n'.format(
            member_count,
            lib.txt.pluralize('member', member_count)
        ) + msg
    elif state == STATE_WARN:
        msg = 'Checked storage on {} {}. There are warnings.\n\n'.format(
            member_count,
            lib.txt.pluralize('member', member_count)
        ) + msg
    else:
        msg = 'Everything is ok, checked storage on {} {}.\n\n'.format(
            member_count,
            lib.txt.pluralize('member', member_count)
        ) + msg

    # over and out
    lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)


if __name__ == '__main__':
    try:
        main()
    except Exception:   # pylint: disable=W0703
        lib.base.cu()
