#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |          _           _           _       _   __   _______        |
# |       __| |_  ___ __| |__  _ __ | |__   / | /  \ |__ / _ \       |
# |      / _| ' \/ -_) _| / / | '  \| / /   | || () | |_ \_, /       |
# |      \__|_||_\___\__|_\_\_|_|_|_|_\_\   |_(_)__(_)___//_/        |
# |                                            check_mk 1.0.39       |
# |                                                                  |
# | Copyright Mathias Kettner 2009                mk@mathias-kettner |
# +------------------------------------------------------------------+
# 
# This file is part of check_mk 1.0.39.
# 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-
# ails.  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.


           
def inventory_lsi(checkname, info):

    # convert [ 0, 1, 2, 3, 4, 5, ...] into [ (0,1), (2,3), (4,5), ... ]
    entries = zip(info[::2], info[1::2])

    arrays  = []
    disks   = []
    for entry in entries:
        id    = int(entry[0][1])
        state = entry[1][1].split('(')[-1][:-1]
        if entry[0][0] == 'VolumeID':
            arrays.append((id, state))
        else:
            disks.append((id, state))
    
    if checkname == 'lsi.array':
        return [(id, state, "None") for (id, state) in arrays]
    else:
        return [(id, state, '"%s"' % state) for (id, state) in disks]

def check_lsi_array(item, params, info):
    volumeid = -1
    for line in info:
        if line[0] == 'VolumeID':
            volumeid = int(line[1])
        elif line[0] == 'Statusofvolume' and volumeid == item:
            status = line[1]
            if status == 'Okay(OKY)':
                return (0, 'OK - Status is Okay (OKY)')
            else:
                return (2, 'CRIT - Status is %s' % (status,))
    return (2, 'CRIT - RAID volume %d not existing' % item)

    
def check_lsi_disk(item, target_state, info):
    target_id = -1
    for line in info:
       if line[0] == 'TargetID':
           target_id = int(line[1])
       elif line[0] == 'State' and target_id == item:
           state = line[1].split('(')[-1][:-1]
           if state == target_state:
               return (0, 'OK - disk has state %s' % state)
           else:
               return (2, 'CRIT - disk has state %s (should be %s)' % (state, target_state))
    return (2, 'CRIT - disk not present')


check_info['lsi.array'] = (check_lsi_array, "RAID array %s", 0,  inventory_lsi)
check_info['lsi.disk'] = (check_lsi_disk, "RAID disk %s", 0,  inventory_lsi)
