Add soem perfdata

This commit is contained in:
CPbN 2019-04-08 11:47:01 +02:00
parent bbd5339f49
commit 608181aa16
2 changed files with 25 additions and 12 deletions

View File

@ -133,20 +133,22 @@ sub settings {
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Content-Type', value => 'application/json;charset=UTF-8');
if (defined($self->{cookie})) {
if (defined($self->{cookie}) && defined($self->{xsrf})) {
$self->{http}->add_header(key => 'Cookie', value => '.AspNetCore.Cookies=' . $self->{cookie});
$self->{http}->add_header(key => 'X-XSRF-TOKEN', value => $self->{xsrf});
}
$self->{http}->set_options(%{$self->{option_results}});
}
sub get_cookie {
sub authenticate {
my ($self, %options) = @_;
my $has_cache_file = $options{statefile}->read(statefile => '3cx_api_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
my $cookie = $options{statefile}->get(name => 'cookie');
my $xsrf = $options{statefile}->get(name => 'xsrf');
my $expires_on = $options{statefile}->get(name => 'expires_on');
if ($has_cache_file == 0 || !defined($cookie) || (($expires_on - time()) < 10)) {
if ($has_cache_file == 0 || !defined($cookie) || !defined($xsrf) || (($expires_on - time()) < 10)) {
my $post_data = '{"Username":"' . $self->{api_username} . '",' .
'"Password":"' . $self->{api_password} . '"}';
@ -155,27 +157,35 @@ sub get_cookie {
my $content = $self->{http}->request(method => 'POST', query_form_post => $post_data,
url_path => '/api/login');
$cookie = $self->{http}->get_header(name => 'Set-Cookie');
if (defined ($cookie) && $cookie =~ /^.AspNetCore.Cookies=([^;]+);.*/) {
my $header = $self->{http}->get_header(name => 'Set-Cookie');
if (defined ($header) && $header =~ /(?:^| ).AspNetCore.Cookies=([^;]+);.*/) {
$cookie = $1;
} else {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Error retrieving cookie");
$self->{output}->option_exit();
}
if (defined ($header) && $header =~ /(?:^| )XSRF-TOKEN=([^;]+);.*/) {
$xsrf = $1;
} else {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Error retrieving xsrf-token");
$self->{output}->option_exit();
}
my $datas = { last_timestamp => time(), cookie => $cookie, expires_on => time() + (3600 * 24) };
my $datas = { last_timestamp => time(), cookie => $cookie, xsrf => $xsrf, expires_on => time() + (3600 * 24) };
$options{statefile}->write(data => $datas);
}
return $cookie;
$self->{cookie} = $cookie;
$self->{xsrf} = $xsrf;
}
sub request_api {
my ($self, %options) = @_;
if (!defined($self->{cookie})) {
$self->{cookie} = $self->get_cookie(statefile => $self->{cache});
$self->authenticate(statefile => $self->{cache});
}
$self->settings();

View File

@ -55,14 +55,17 @@ sub run {
$self->manage_selection(%options);
foreach my $item (sort keys %{$self->{single}}) {
$self->{output}->output_add(severity => $self->{single}->{$item} ? 'OK' : 'CRITICAL',
short_msg => $item . ': ' . ($self->{single}->{$item} ? 'OK' : 'NOK'));
short_msg => $item);
}
$self->{output}->output_add(severity => $self->{system}->{HasNotRunningServices} ? 'CRITICAL' : 'OK',
short_msg => 'Services: ' . ($self->{system}->{HasNotRunningServices} ? 'NOK' : 'OK'));
short_msg => 'Services');
$self->{output}->output_add(severity => $self->{system}->{HasUnregisteredSystemExtensions} ? 'CRITICAL' : 'OK',
short_msg => 'Extensions: ' . ($self->{system}->{HasUnregisteredSystemExtensions} ? 'NOK' : 'OK'));
short_msg => 'Extensions');
$self->{output}->display(force_ignore_perfdata => 1);
$self->{output}->perfdata_add(label => "CallsActive", unit => '', value => $self->{system}->{CallsActive});
$self->{output}->perfdata_add(label => "ExtensionsRegistered", unit => '', value => $self->{system}->{ExtensionsRegistered});
$self->{output}->display();
$self->{output}->exit();
}