#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             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-
# 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.

# Developed by Thorsten Bruhns from OPITZ CONSULTING Deutschland GmbH

# <<<oracle_crs_voting>>>
# 1. ONLINE   0a6884c063904f50bf7ef4516b728a2d (/dev/oracleasm/disks/DATA1) [DATA1]

def inventory_oracle_crs_voting(info):
    inventory = []
    return [ ('', None) for line in info ]
    for line in info:
        if line[1] == 'ONLINE':
            return (0, None)
        elif len(line) == 3:
            # CRS 10.2 + 11.1 has 3 entries
            return (0, None)
    return None

def check_oracle_crs_voting(item, params, info):
    # state = -1 => no data for Service
    state = -1
    infotext = ''
    votecount = 0
    votedisk = ''
    for line in info:
        if line[1] == 'ONLINE':
             votecount += 1
             votedisk += '[%s] ' % line[3]
        elif len(line) == 3:
             votecount += 1
             votedisk += '[%s] ' % line[2]

    if votecount in (1,3,5):
        state = 0
        infotext = '%d Voting Disks found. %s' % (votecount, votedisk)
        return state, infotext
    elif votecount == 0:
        # cssd could not start without an existing voting disk!
        raise MKCounterWrapped("No Voting Disk(s) found. Maybe the cssd/crsd is not running!")
    else:
        state = 2
        infotext = 'missing Voting Disks (!!). %d Votes found %s' % (votecount, votedisk)
        return state, infotext

    # In case of missing information we assume that the clusterware
    # is not running and we simple skip the result
    raise MKCounterWrapped("No Voting Disk(s) found. Maybe the cssd/crsd is not running!")

check_info['oracle_crs_voting'] = {
    "check_function"          : check_oracle_crs_voting,
    "inventory_function"      : inventory_oracle_crs_voting,
    "service_description"     : "ORA-GI Voting",
    "group"                   : "oracle_crs_voting",
}
