lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/Status.pm

91 lines
2.5 KiB
Perl
Raw Normal View History

2017-01-16 22:00:50 +01:00
# Status plugin
#
# this plugin adds /portalStatus entrypoint which display session count
# by category
package Lemonldap::NG::Portal::Plugins::Status;
use strict;
use Mouse;
2017-02-18 15:25:51 +01:00
use JSON qw(from_json);
2017-01-16 22:00:50 +01:00
use MIME::Base64;
2018-06-12 21:00:10 +02:00
use IO::Socket::INET;
2017-01-16 22:00:50 +01:00
2020-10-09 22:26:00 +02:00
our $VERSION = '2.0.10';
2017-01-16 22:00:50 +01:00
extends 'Lemonldap::NG::Portal::Main::Plugin';
# INITIALIZATION
sub init {
my ($self) = @_;
2020-10-09 22:26:00 +02:00
$self->addUnauthRoute( portalStatus => 'status', ['GET'] )
->addAuthRoute( portalStatus => 'status', ['GET'] );
2017-01-16 22:00:50 +01:00
return 1;
}
sub status {
my ( $self, $req ) = @_;
my $res = {};
2017-02-18 15:25:51 +01:00
if ( my $p = $self->p->HANDLER->tsv->{statusPipe} ) {
2018-06-12 21:00:10 +02:00
my ( $args, $buf );
my $out = $self->p->HANDLER->tsv->{statusOut};
if ( $ENV{LLNGSTATUSHOST} ) {
foreach ( 64322 .. 64331 ) {
if ( $out =
IO::Socket::INET->new( Proto => 'udp', LocalPort => $_ ) )
{
$args = " host="
. ( $ENV{LLNGSTATUSCLIENT} || 'localhost' ) . ":$_";
last;
}
}
}
2020-02-25 22:01:04 +01:00
return $self->p->sendError( $req, 'No status connection' )
unless ($out);
2018-06-12 21:00:10 +02:00
$p->print("STATUS json=1$args\n");
while ( $_ = $out->getline ) {
2017-02-18 15:25:51 +01:00
last if (/^END$/);
$buf .= $_;
}
if ($buf) {
2017-09-28 14:52:14 +02:00
eval { $res = from_json( $buf, { allow_nonref => 1 } ) };
2017-02-18 15:25:51 +01:00
if ($@) {
$self->logger->error("JSON error: $@");
$self->logger->error("JSON: $buf");
}
foreach (qw(total average)) {
if ( $res->{$_} ) {
foreach my $k ( keys %{ $res->{$_} } ) {
delete $res->{$_}->{$k} unless ( $k =~ /^PORTAL/ );
}
}
}
}
}
2017-01-16 22:00:50 +01:00
foreach my $type (qw(global persistent cas saml oidc)) {
if ( $self->conf->{"${type}Storage"} ) {
my %modOpts = (
%{ $self->conf->{"${type}StorageOptions"} },
backend => $self->conf->{"${type}Storage"}
);
2017-09-26 22:15:50 +02:00
eval {
my $sessions = Lemonldap::NG::Common::Apache::Session->searchOn(
\%modOpts,
_session_kind => 'SSO',
'_session_id'
);
if (%$sessions) {
my @s = keys %$sessions;
$res->{storage}->{$type} = @s;
}
};
2017-01-16 22:00:50 +01:00
}
}
2017-02-15 07:41:50 +01:00
return $self->p->sendJSONresponse( $req, $res );
2017-01-16 22:00:50 +01:00
}
1;