<commit_before>#
# Copyright 2020 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package notification::telegram::mode::alert;

use base qw(centreon::plugins::mode);

use strict;
use warnings;
use centreon::plugins::http;
use JSON::XS;

my %telegram_icon_host = (
    up => "\x{2705}",
    down => "\x{1F525}",
    unreachable => "\x{2753}",
);
my %telegram_icon_service = (
    ok => "\x{2705}",
    warning => "\x{26A0}",
    critical => "\x{1F525}",
    unknown => "\x{2753}",
);

sub new {
    my ($class, %options) = @_;
    my $self = $class->SUPER::new(package => __PACKAGE__, %options);
    bless $self, $class;

    $options{options}->add_options(arguments => {
        "hostname:s"            => { name => 'hostname', default => 'api.telegram.org' },
        "port:s"                => { name => 'port', default => 443 },
        "proto:s"               => { name => 'proto', default => 'https' },
        "urlpath:s"             => { name => 'url_path', default => "/sendMessage" },
        "chat-id:s"             => { name => 'chat_id' },
        "bot-token:s"           => { name => 'bot_token' },
        "host-name:s"           => { name => 'host_name' },
        "host-state:s"          => { name => 'host_state' },
        "host-output:s"         => { name => 'host_output' },
        "service-description:s" => { name => 'service_description' },
        "service-state:s"       => { name => 'service_state' },
        "service-output:s"      => { name => 'service_output' },
        "graph-url:s"           => { name => 'graph_url' },
        "link-url:s"            => { name => 'link_url' },
        "centreon-url:s"        => { name => 'centreon_url' },
        "centreon-token:s"      => { name => 'centreon_token' },
        "timeout:s"             => { name => 'timeout' },
    });

    $self->{http} = centreon::plugins::http->new(%options);
    return $self;
}

sub check_options {
    my ($self, %options) = @_;

    $self->SUPER::init(%options);
    if (!defined($self->{option_results}->{chat_id})) {
        $self->{output}->add_option_msg(short_msg => "You need to set --chat-id option");
        $self->{output}->option_exit();
    }
    if (!defined($self->{option_results}->{bot_token})) {
        $self->{output}->add_option_msg(short_msg => "You need to set --bot-token option");
        $self->{output}->option_exit();
    }
    if (!defined($self->{option_results}->{host_name}) || $self->{option_results}->{host_name} eq '') {
        $self->{output}->add_option_msg(short_msg => "You need to specify --host-name option.");
        $self->{output}->option_exit();
    }

    foreach (('graph_url', 'link_url')) {
        if (defined($self->{option_results}->{$_})) {
            $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{option_results}->{$1}/g;
            eval "\$self->{option_results}->{\$_} = \"$self->{option_results}->{$_}\"";
        }
    }

    $self->{http}->set_options(%{$self->{option_results}});
}

sub host_message {
  my ($self, %options) = @_;

    if (defined($self->{option_results}->{host_state}) && $self->{option_results}->{host_state} ne '') {
        if (defined($telegram_icon_host{lc($self->{option_results}->{host_state})})) {
            $self->{message} = $telegram_icon_host{lc($self->{option_results}->{host_state})};
        }
    }

    $self->{message} .= " Host <i>" . $self->{option_results}->{host_name} . "</i>";

    if (defined($self->{option_results}->{host_state}) && $self->{option_results}->{host_state} ne '') {
        $self->{message} .= ' is <b>' . $self->{option_results}->{host_state} . '</b>';
    } else {
        $self->{message} .= ' alert';
    }

    if (defined($self->{option_results}->{link_url}) && $self->{option_results}->{link_url} ne '') {
        $self->{message} .= "\n <a href=\"" . $self->{option_results}->{link_url} . "\">Link</a>";
    }
}

sub service_message {<commit_msg>add missing host output to notification<commit_after>#
# Copyright 2020 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package notification::telegram::mode::alert;

use base qw(centreon::plugins::mode);

use strict;
use warnings;
use centreon::plugins::http;
use JSON::XS;

my %telegram_icon_host = (
    up => "\x{2705}",
    down => "\x{1F525}",
    unreachable => "\x{2753}",
);
my %telegram_icon_service = (
    ok => "\x{2705}",
    warning => "\x{26A0}",
    critical => "\x{1F525}",
    unknown => "\x{2753}",
);

sub new {
    my ($class, %options) = @_;
    my $self = $class->SUPER::new(package => __PACKAGE__, %options);
    bless $self, $class;

    $options{options}->add_options(arguments => {
        "hostname:s"            => { name => 'hostname', default => 'api.telegram.org' },
        "port:s"                => { name => 'port', default => 443 },
        "proto:s"               => { name => 'proto', default => 'https' },
        "urlpath:s"             => { name => 'url_path', default => "/sendMessage" },
        "chat-id:s"             => { name => 'chat_id' },
        "bot-token:s"           => { name => 'bot_token' },
        "host-name:s"           => { name => 'host_name' },
        "host-state:s"          => { name => 'host_state' },
        "host-output:s"         => { name => 'host_output' },
        "service-description:s" => { name => 'service_description' },
        "service-state:s"       => { name => 'service_state' },
        "service-output:s"      => { name => 'service_output' },
        "graph-url:s"           => { name => 'graph_url' },
        "link-url:s"            => { name => 'link_url' },
        "centreon-url:s"        => { name => 'centreon_url' },
        "centreon-token:s"      => { name => 'centreon_token' },
        "timeout:s"             => { name => 'timeout' },
    });

    $self->{http} = centreon::plugins::http->new(%options);
    return $self;
}

sub check_options {
    my ($self, %options) = @_;

    $self->SUPER::init(%options);
    if (!defined($self->{option_results}->{chat_id})) {
        $self->{output}->add_option_msg(short_msg => "You need to set --chat-id option");
        $self->{output}->option_exit();
    }
    if (!defined($self->{option_results}->{bot_token})) {
        $self->{output}->add_option_msg(short_msg => "You need to set --bot-token option");
        $self->{output}->option_exit();
    }
    if (!defined($self->{option_results}->{host_name}) || $self->{option_results}->{host_name} eq '') {
        $self->{output}->add_option_msg(short_msg => "You need to specify --host-name option.");
        $self->{output}->option_exit();
    }

    foreach (('graph_url', 'link_url')) {
        if (defined($self->{option_results}->{$_})) {
            $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{option_results}->{$1}/g;
            eval "\$self->{option_results}->{\$_} = \"$self->{option_results}->{$_}\"";
        }
    }

    $self->{http}->set_options(%{$self->{option_results}});
}

sub host_message {
  my ($self, %options) = @_;

    if (defined($self->{option_results}->{host_state}) && $self->{option_results}->{host_state} ne '') {
        if (defined($telegram_icon_host{lc($self->{option_results}->{host_state})})) {
            $self->{message} = $telegram_icon_host{lc($self->{option_results}->{host_state})};
        }
    }

    $self->{message} .= " Host <i>" . $self->{option_results}->{host_name} . "</i>";

    if (defined($self->{option_results}->{host_state}) && $self->{option_results}->{host_state} ne '') {
        $self->{message} .= ' is <b>' . $self->{option_results}->{host_state} . '</b>';
    } else {
        $self->{message} .= ' alert';
    }
    if (defined($self->{option_results}->{host_output}) && $self->{option_results}->{host_output} ne '') {
        $self->{message} .= "\n " . $self->{option_results}->{host_output};
    }
    if (defined($self->{option_results}->{link_url}) && $self->{option_results}->{link_url} ne '') {
        $self->{message} .= "\n <a href=\"" . $self->{option_results}->{link_url} . "\">Link</a>";
    }
}

sub service_message {
{'fraction_non_alphanumeric': 0.20823579484996807, 'fraction_numerical': 0.008299638220898064, 'mean_word_length': 2.8410298324478953, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 252, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3500560', 'n_tokens_mistral': 3160, 'n_tokens_neox': 3086, 'n_words': 614}