<commit_before>#
# Copyright 2015 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 cloud::docker::mode::image;

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

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

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

    $self->{version} = '1.0';
    $options{options}->add_options(arguments =>
        {
            "hostname:s"            => { name => 'hostname' },
            "port:s"                => { name => 'port', default => '2376'},
            "proto:s"               => { name => 'proto', default => 'https' },
            "urlpath:s"             => { name => 'url_path', default => '/' },
            "name:s"                => { name => 'name' },
            "id:s"                  => { name => 'id' },
            "image:s"               => { name => 'image' },
            "registry-hostname:s"   => { name => 'registry_hostname' },
            "registry-proto:s"      => { name => 'registry_proto', default => 'https' },
            "registry-port:s"       => { name => 'registry_port' },
            "credentials"           => { name => 'credentials' },
            "username:s"            => { name => 'username' },
            "password:s"            => { name => 'password' },
            "ssl:s"                 => { name => 'ssl', },
            "cert-file:s"           => { name => 'cert_file' },
            "key-file:s"            => { name => 'key_file' },
            "cacert-file:s"         => { name => 'cacert_file' },
            "timeout:s"             => { name => 'timeout' },
        });

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

sub check_options {
    my ($self, %options) = @_;
    $self->SUPER::init(%options);

    if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) {
        $self->{output}->add_option_msg(short_msg => "Please set the name or id option");
        $self->{output}->option_exit();
    }

    if ((!defined($self->{option_results}->{name})) && (!defined($self->{option_results}->{id}))) {
        $self->{output}->add_option_msg(short_msg => "Please set the name or id option");
        $self->{output}->option_exit();
    }

    if (!defined($self->{option_results}->{image})) {
        $self->{output}->add_option_msg(short_msg => "Please set the image option");
        $self->{output}->option_exit();
    }

    if (!defined($self->{option_results}->{registry_hostname})) {
        $self->{output}->add_option_msg(short_msg => "Please set the registry-hostname option");
        $self->{output}->option_exit();
    }

    if (!defined($self->{option_results}->{registry_proto})) {
        $self->{output}->add_option_msg(short_msg => "Please set the registry-proto option");
        $self->{output}->option_exit();
    }

    if (defined($self->{option_results}->{id})) {
        $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json";
    } elsif (defined($self->{option_results}->{name})) {
        $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/json";
    }

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

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

    my ($jsoncontent,$jsoncontent2, $webcontent, $webcontent2);

    $jsoncontent = $self->{http}->request();

    my $json = JSON->new;

    eval {
        $webcontent = $json->decode($jsoncontent);
    };

    if ($@) {
        $self->{output}->add_option_msg(short_msg => "Cannot decode json response");
        $self->{output}->option_exit();
    }

    my $container_id = $webcontent->{Image};

    $self->{option_results}->{url_path} = "/v1/repositories/".$self->{option_results}->{image}."/tags";
    $self->{option_results}->{port} = $self->{option_results}->{registry_port};
    $self->{option_results}->{proto} = $self->{option_results}->{registry_proto};
    $self->{option_results}->{hostname} = $self->{option_results}->{registry_hostname};

    $jsoncontent2 = $self->{http}->request();

    my $json2 = JSON->new;

    eval {<commit_msg>fix error with double request<commit_after>#
# Copyright 2015 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 cloud::docker::mode::image;

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

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

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

    $self->{version} = '1.0';
    $options{options}->add_options(arguments =>
        {
            "hostname:s"            => { name => 'hostname' },
            "port:s"                => { name => 'port', default => '2376'},
            "proto:s"               => { name => 'proto', default => 'https' },
            "urlpath:s"             => { name => 'url_path', default => '/' },
            "name:s"                => { name => 'name' },
            "id:s"                  => { name => 'id' },
            "image:s"               => { name => 'image' },
            "registry-hostname:s"   => { name => 'registry_hostname' },
            "registry-proto:s"      => { name => 'registry_proto', default => 'https' },
            "registry-port:s"       => { name => 'registry_port' },
            "credentials"           => { name => 'credentials' },
            "username:s"            => { name => 'username' },
            "password:s"            => { name => 'password' },
            "ssl:s"                 => { name => 'ssl', },
            "cert-file:s"           => { name => 'cert_file' },
            "key-file:s"            => { name => 'key_file' },
            "cacert-file:s"         => { name => 'cacert_file' },
            "timeout:s"             => { name => 'timeout' },
        });

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

sub check_options {
    my ($self, %options) = @_;
    $self->SUPER::init(%options);

    if ((defined($self->{option_results}->{name})) && (defined($self->{option_results}->{id}))) {
        $self->{output}->add_option_msg(short_msg => "Please set the name or id option");
        $self->{output}->option_exit();
    }

    if ((!defined($self->{option_results}->{name})) && (!defined($self->{option_results}->{id}))) {
        $self->{output}->add_option_msg(short_msg => "Please set the name or id option");
        $self->{output}->option_exit();
    }

    if (!defined($self->{option_results}->{image})) {
        $self->{output}->add_option_msg(short_msg => "Please set the image option");
        $self->{output}->option_exit();
    }

    if (!defined($self->{option_results}->{registry_hostname})) {
        $self->{output}->add_option_msg(short_msg => "Please set the registry-hostname option");
        $self->{output}->option_exit();
    }

    if (!defined($self->{option_results}->{registry_proto})) {
        $self->{output}->add_option_msg(short_msg => "Please set the registry-proto option");
        $self->{output}->option_exit();
    }

    if (defined($self->{option_results}->{id})) {
        $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{id}."/json";
    } elsif (defined($self->{option_results}->{name})) {
        $self->{option_results}->{url_path} = "/containers/".$self->{option_results}->{name}."/json";
    }

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

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

    my ($jsoncontent,$jsoncontent2, $webcontent, $webcontent2);

    $jsoncontent = $self->{http}->request();

    my $json = JSON->new;

    eval {
        $webcontent = $json->decode($jsoncontent);
    };

    if ($@) {
        $self->{output}->add_option_msg(short_msg => "Cannot decode json response");
        $self->{output}->option_exit();
    }

    my $container_id = $webcontent->{Image};

    $self->{option_results}->{url_path} = "/v1/repositories/".$self->{option_results}->{image}."/tags";
    $self->{option_results}->{port} = $self->{option_results}->{registry_port};
    $self->{option_results}->{proto} = $self->{option_results}->{registry_proto};
    $self->{option_results}->{hostname} = $self->{option_results}->{registry_hostname};
    $self->{http}->set_options(%{$self->{option_results}});

    $jsoncontent2 = $self->{http}->request();

    my $json2 = JSON->new;

    eval {
{'fraction_non_alphanumeric': 0.19464720194647203, 'fraction_numerical': 0.0038523925385239256, 'mean_word_length': 2.71004136893569, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 272, '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': '2613293', 'n_tokens_mistral': 3129, 'n_tokens_neox': 3080, 'n_words': 615}