#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2013             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# <<<df_zos>>>
# SYS5.OMVS.ALF0.HFS         720          92        504       16% /ALF0
# HFS, Read/Write, Device:2, ACLS=Y
# Filetag : T=off   codeset=0
# ##########
# SYS5.OMVS.SYSPLEX.ROOT     720         224        372       38% /
# HFS, Read Only, Device:1, ACLS=Y
# Filetag : T=off   codeset=0
# ##########

# FS Types:
# AUTOMNT
# TFS
# ZFS
# NFS
# HFS


def parse_df_zos(info):
    parsed = {}
    fs = None
    usage = []
    options = []

    for line in info:
        if line[0].startswith('#####'):
            # Add item for filesystem
            if fs and usage and options:
                parsed.setdefault(fs, {})
                parsed[fs].setdefault('size', usage)
                parsed[fs].setdefault('options', options)

            fs = None
            usage = []
            options = []
        elif line[0].startswith('Filesystem'):
            # Ignore header line
            continue
        else:
            if fs == None:
                # First line: filesystem with usage information
                fs = line[5]
                usage = line[1:5]
            elif not options:
                # Second line: filesystem options
                for option in line:
                    options.append(option.replace(',', ''))
                if 'Read' in options and 'Only' in options:
                    options.remove('Read')
                    options.remove('Only')
                    options.append('ReadOnly')
    return parsed


df_zos_exclude_list = ['AUTOMNT', 'TFS', 'NFS']

def inventory_df_zos(parsed):
    mplist = []

    for item in parsed.keys():
        fs_rw = False
        fs_ex = False

        # Check filesystem options
        for option in parsed[item]['options']:
            # Check if filesystem is rw
            if option == 'Read/Write':
                fs_rw = True

            # Check if filesystem is excluded
            if option in df_zos_exclude_list:
                fs_ex = True

        if fs_rw and not fs_ex:
            mplist.append(item)
    return df_inventory(mplist)


def check_df_zos(item, params, parsed):
    fslist = []

    for filesystem in parsed.keys():
        (size_mb, used_mb, avail_mb) = map(float, parsed[filesystem]['size'][0:3])
        size_mb  /= 1024
        used_mb  /= 1024
        avail_mb /= 1024
        fslist.append((filesystem, size_mb, avail_mb, 0))

    return df_check_filesystem_list(item, params, fslist)


check_info['df_zos'] = {
    'parse_function'          : parse_df_zos,
    'check_function'          : check_df_zos,
    'inventory_function'      : inventory_df_zos,
    'service_description'     : "Filesystem %s",
    'has_perfdata'            : True,
    'group'                   : 'filesystem',
    'default_levels_variable' : "filesystem_default_levels",
    'includes'                : [ 'df.include' ],
}
