<commit_before># -*- coding: utf-8 -*-

# Copyright (C) 2012-2014:
#    <NAME>, <EMAIL>
#
# This file is part of SNMP Booster Shinken Module.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with SNMP Booster Shinken Module.
# If not, see <http://www.gnu.org/licenses/>.


""" This module contains the function which compute triggers and return the
exit code of a service
"""


from shinken.log import logger

from utils import rpn_calculator


__all__ = ("get_trigger_result", )


# Triggers functions
def diff(ds_data):
    """ Are last computed value and last-1 computed  value the same ? """
    return ds_data['ds_oid_value_computed'] == ds_data['ds_oid_value_last_computed']


def prct(ds_data):
    """ Do the percent of value computed using max value """
    try:
        max_ = float(ds_data['ds_max_oid_value_computed'])
    except:
        raise Exception("Cannot calculate prct, max value for the "
                        "datasource '%s' is missing" % ds_data['ds_name'])
    return float(ds_data['ds_oid_value_computed']) * 100 / max_


def last(ds_data):
    """ Get the last value computed """
    return ds_data['ds_oid_value_computed']


RPN_FUNCTIONS = {"diff": diff,
                 "prct": prct,
                 "last": last,
                 }

# End Triggers functions


def get_trigger_result(service):
    """ Get return code from trigger calculator
    return error_message, exit_code
    :error_message:     is None if there no error
    :exit_code:         0, 1, 2 or 3
    """
    errors = {'unknown': 3,
              'critical': 2,
              'warning': 1,
              'ok': 0,
              }

    try:
        # First we launch critical triggers for each datasource
        # If one is true, then we are in critical
        # Second we launch warning triggers for each datasource
        # If one is true, then we are in waring
        for error_name in ['critical', 'warning']:
            # Browse all triggers
            for trigger in service['triggers'].values():
                rpn_list = []
                if error_name in trigger:
                    # Check if the trigger is set for this state
                    if trigger[error_name] is None:
                        # Trigger not set for this state (warning or critical)
                        continue
                    # If yes we will try to evaluate it
                    for element in trigger[error_name]:
                        tmp = element.split(".")
                        if len(tmp) > 1:
                            # detect ds_name with function
                            ds_name, fct = tmp
                            # Check if ds_name is define in the service
                            ds_data = service['ds'].get(ds_name)<commit_msg>Add: treatment of unknown triggers<commit_after># -*- coding: utf-8 -*-

# Copyright (C) 2012-2014:
#    <NAME>, <EMAIL>
#
# This file is part of SNMP Booster Shinken Module.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with SNMP Booster Shinken Module.
# If not, see <http://www.gnu.org/licenses/>.


""" This module contains the function which compute triggers and return the
exit code of a service
"""


from shinken.log import logger

from utils import rpn_calculator


__all__ = ("get_trigger_result", )


# Triggers functions
def diff(ds_data):
    """ Are last computed value and last-1 computed  value the same ? """
    return ds_data['ds_oid_value_computed'] == ds_data['ds_oid_value_last_computed']


def prct(ds_data):
    """ Do the percent of value computed using max value """
    try:
        max_ = float(ds_data['ds_max_oid_value_computed'])
    except:
        raise Exception("Cannot calculate prct, max value for the "
                        "datasource '%s' is missing" % ds_data['ds_name'])
    return float(ds_data['ds_oid_value_computed']) * 100 / max_


def last(ds_data):
    """ Get the last value computed """
    return ds_data['ds_oid_value_computed']


RPN_FUNCTIONS = {"diff": diff,
                 "prct": prct,
                 "last": last,
                 }

# End Triggers functions


def get_trigger_result(service):
    """ Get return code from trigger calculator
    return error_message, exit_code
    :error_message:     is None if there no error
    :exit_code:         0, 1, 2 or 3
    """
    errors = {'unknown': 3,
              'critical': 2,
              'warning': 1,
              'ok': 0,
              }

    try:
        # First we launch critical triggers for each datasource
        # If one is true, then we are in critical
        # Second we launch warning triggers for each datasource
        # If one is true, then we are in waring
        # Third we launch unknown triggers for each datasource
        # If one is true, then we are in unknown
        for error_name in ['critical', 'warning', 'unknown']:
            # Browse all triggers
            for trigger in service['triggers'].values():
                rpn_list = []
                if error_name in trigger:
                    # Check if the trigger is set for this state
                    if trigger[error_name] is None:
                        # Trigger not set for this state (warning or critical)
                        continue
                    # If yes we will try to evaluate it
                    for element in trigger[error_name]:
                        tmp = element.split(".")
                        if len(tmp) > 1:
                            # detect ds_name with function
                            ds_name, fct = tmp
                            # Check if ds_name is define in the service
                            ds_data = service['ds'].get(ds_name)
{'fraction_non_alphanumeric': 0.07957913455838767, 'fraction_numerical': 0.006816834617664493, 'mean_word_length': 2.097292335933915, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '79369', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1758, 'n_words': 731}