#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
#from cmk.base.check_legacy_includes.cisco_sensor_item import *  # pylint: disable=wildcard-import,unused-wildcard-import


def inventory_scality_disk(info):
    for line in info[0]:
        yield (line[0],{})

def check_scality_disk(item, params, info):
    if params.get("disks"):
        warn, crit = params["disks"]
        for line in info[0]:
            if item in line:
                ringdisktotal = int(line[3])
                perfdata = [("Disks", ringdisktotal)]
                infotext = "Number of disks: %s. " %ringdisktotal
                if ringdisktotal <= warn:
                    if ringdisktotal <= crit:
                        infotext += "This is lower or equal with %s, critical level" %crit
                        yield 2, infotext, perfdata
                    else:
                       infotext += "This is lower or equal with %s, warning level" %warn
                       yield 1, infotext, perfdata
                else:
                    yield 0, infotext, perfdata

    else:
        for line in info[0]:
            if item in line:
                ringdisktotal = line[3]
                perfdata = [("Disks", ringdisktotal)]
                infotext = "Number of disks: %s" %ringdisktotal
                yield 0, infotext, perfdata

check_info["scality_ring.disk"] = {
    "check_function": check_scality_disk,
    "inventory_function": inventory_scality_disk,
    "service_description": "Ring %s Disks",
    "has_perfdata": True,
    "group": "scality_disks"
}
##############RING STATUS############
def inventory_scality_supervisor(info):
    for line in info[1]:
        yield (line[0],{})

def check_scality_supervisor(item, no_params, info):
    for line in info[1]:
        if item == str(line[0]):
            infotext = ""
            status = 0
            supNbSrvTotal = int(line[1])
            supNbSrvOk = int(line[2])
            supNbSrvNok = int(line[3])
            supAvailable = int(line[4])
            if supAvailable != 1 :
                status = 2
                infotext += "Supervisor is not available"
                yield status, infotext
            else:
                infotext = "Supervisor is available "
                yield status, infotext
            if supNbSrvNok > 0 :
                status = 2
                infotext = "There are %s unavailable servers" %supNbSrvNok
                yield status, infotext
            else:
                infotext = "All servers are available"
                yield status, infotext
            
            

check_info["scality_ring.supervisor"] = {
    "check_function": check_scality_supervisor,
    "inventory_function": inventory_scality_supervisor,
    "service_description": "Supervisor %s",
}


#############Storage#############
from cmk.base.check_legacy_includes.df import *  
from cmk.base.check_legacy_includes.size_trend import *
#factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS

def inventory_scality_storage(info):
    for line in info[0]:
        yield (line[0],{})

def check_scality_storage(item, params, info):
    for line in info[0]:
        if item in line:
            ringStorageAvailable = float(line[5])
            ringStorageTotal = float(line[6])
            fslist=[(item, ringStorageTotal, ringStorageAvailable, 0)]
            return df_check_filesystem_list(item, params, fslist)

check_info["scality_ring.storage"] = {
    "check_function": check_scality_storage,
    "inventory_function": inventory_scality_storage,
    "service_description": "Ring %s Storage",
    "default_levels_variable": "filesystem_default_levels",
    "has_perfdata": True,
    "group": "filesystem",
}


##############RING STATUS############
def inventory_scality_ring(info):
    for line in info[0]:
        yield (line[0],{})

def check_scality_ring(item, no_params, info):
    for line in info[0]:
        if item in line:
            ringstaterun = line[1]
            ringstate = line[2]
            if ringstate == "RUN":
                status = 0
                text = "Ring is in Run State"
            elif ringstate =="LOOP":
                status = 2
                text = "Ring is LOOP State"
            else:
                status = 2
                text = "Ring is in Balancing State"
        
    yield status, text



check_info["scality_ring"] = {
    "check_function": check_scality_ring,
    "inventory_function": inventory_scality_ring,
    "service_description": "Ring %s State",
    "snmp_info": [
        (".1.3.6.1.4.1.37489.2.1.1.1.4.1.1", 
            [ 
                "2", #ringName
                "3", #ringStateRun
                "8", #ringState
                "9", #ringDiskTotal
                "13", #ringStorageUsed
                "14", #ringStorageAvailable
                "15", #ringStorageTotal
            ]),
        (".1.3.6.1.4.1.37489.2.1.1.1.4.2.1",
            [
                "2", #supName
                "5", #supNbSrvTotal
                "6", #supNbSrvOK
                "7", #supNbSrvNok
                "8", #supAvailable
            ],
        ),
    ],
    "snmp_scan_function": lambda oid: "scality" in oid(".1.3.6.1.2.1.1.1.0").lower(),
}