<commit_before>#!/usr/bin/perl -w

use strict;
use Frontier::Client;
use Nagios::Plugin;
use Nagios::Plugin::Getopt;
use Nagios::Plugin::Threshold;

my $plugin = Nagios::Plugin->new(shortname => "check_sipgate_balance");

my $options = Nagios::Plugin::Getopt->new(
    usage => "Usage: %s [OPTIONS]",
    version => "1.0.0",
);
$options->arg(
    spec => "critical|c=i",
    help => "Balance threshold for a critical warning",
    required => 0,
    default => 2,
);
$options->arg(
    spec => "warning|w=i",
    help => "Balance threshold for a warning",
    required => 0,
    default => 5,
);
$options->arg(
    spec => "username=s",
    help => "API username",
    required => 1
);
$options->arg(
    spec => "password=s",
    help => "API password",
    required => 1,
);
                                        
$options->getopts();

my $threshold = Nagios::Plugin::Threshold->set_thresholds(
    warning  => $options->warning . ":",
    critical => $options->critical . ":",
);

alarm $options->timeout;

my $url = sprintf("https://%s:%s\@samurai.sipgate.net/RPC2",
                  $options->username,
                  $options->password);
my $client = Frontier::Client->new(url => $url);

my $result = $client->call("samurai.BalanceGet");
my $balance =  $result->{'CurrentBalance'}->{'TotalIncludingVat'};

$plugin->add_perfdata(
    label => "balance",
    value => $balance,
    uom => $result->{'CurrentBalance'}->{'Currency'},
    threshold => $threshold,
);

$plugin->nagios_exit(
    return_code => $threshold->get_status($balance),
);<commit_msg>Improved error handling: Frontier::Client will die() on us in case of errors, so we need to make sure that these cases are transformed into proper Nagios exit codes.<commit_after>#!/usr/bin/perl -w

use strict;
use Frontier::Client;
use Nagios::Plugin;
use Nagios::Plugin::Getopt;
use Nagios::Plugin::Threshold;

my $plugin = Nagios::Plugin->new(shortname => "check_sipgate_balance");

my $options = Nagios::Plugin::Getopt->new(
    usage => "Usage: %s [OPTIONS]",
    version => "1.0.0",
);
$options->arg(
    spec => "critical|c=i",
    help => "Balance threshold for a critical warning",
    required => 0,
    default => 2,
);
$options->arg(
    spec => "warning|w=i",
    help => "Balance threshold for a warning",
    required => 0,
    default => 5,
);
$options->arg(
    spec => "username=s",
    help => "API username",
    required => 1
);
$options->arg(
    spec => "password=s",
    help => "API password",
    required => 1,
);
                                        
$options->getopts();

my $threshold = Nagios::Plugin::Threshold->set_thresholds(
    warning  => $options->warning . ":",
    critical => $options->critical . ":",
);

alarm $options->timeout;

my $url = sprintf("https://%s:%s\@samurai.sipgate.net/RPC2",
                  $options->username,
                  $options->password);
my $client = Frontier::Client->new(url => $url);

my $result;
eval { 
  $result = $client->call("samurai.BalanceGet");
};

$plugin->nagios_die("sipgate request failed") if $@;

my $balance = $result->{'CurrentBalance'}->{'TotalIncludingVat'};

$plugin->add_perfdata(
    label => "balance",
    value => $balance,
    uom => $result->{'CurrentBalance'}->{'Currency'},
    threshold => $threshold,
);

$plugin->nagios_exit(
    return_code => $threshold->get_status($balance),
);
{'fraction_non_alphanumeric': 0.1907032181168057, 'fraction_numerical': 0.0059594755661501785, 'mean_word_length': 3.3428201811125486, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 98, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5290443', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1078, 'n_words': 243}