#!/usr/bin/perl

use v5.36;

use JSON::XS;

my @out = qx(/usr/sbin/ngcp-kamctl proxy dispatcher dump);
die "cannot talk to kamailio proxy: $?\n" if $?;

my %peers = ();

use constant PEER_PROBE_ID => 100;

my $probe_id;

my $peers = decode_json("@out");

foreach my $record (@{$peers->{RECORDS}}) {
    my $probe = $record->{SET};

    my $probe_id = $probe->{ID};

    next unless defined $probe_id and $probe_id == PEER_PROBE_ID;

    foreach my $target (@{$probe->{TARGETS}}) {
        my $uri = $target->{DEST}->{URI};
        my $flags = $target->{DEST}->{FLAGS};
        my $attrs = $target->{DEST}->{ATTRS}->{BODY};

        my $peerid = $attrs =~ s/^.*peerid=(\d+).*$/$1/r;
        my $peername = $attrs =~ s/^.*peername="([^"]+)".*$/$1/r;
        my $peergid = $attrs =~ s/^.*peergid=(\d+).*$/$1/r;

        my $check = 'unknown';
        if ($flags =~ /P/i) {
            $check = 'yes';
        } elsif ($flags =~ /X/i) {
            $check = 'pending';
        }

        my $status = 'unknown';
        if ($flags =~ /A/i) {
            $status = 'up';
        } elsif ($flags =~ /I/i) {
            $status = 'down';
        }

        $peers{$peername} = {
            id => $peerid,
            name => $peername,
            groupid => $peergid,
            uri => $uri,
            check => $check,
            status => $status,
        };
    }
}

# Print the list of peers and their current status.
my @peers = sort { $peers{$a}->{status} cmp $peers{$b}->{status} } keys %peers;
foreach my $peername (@peers) {
    my $peer = $peers{$peername};
    my $status = uc $peer->{status};

    say "$status\tid=$peer->{id}\tname=$peer->{name} uri=$peer->{uri}";
}

__END__

=head1 NAME

ngcp-peerprobe-status - print the list of peers and their status

=head1 SYNOPSIS

B<ngcp-peerprobe-status>

=head1 DESCRIPTION

This program will print the current list of peers and their status.

The output consists of one line per peer, and each line is organized in
columns, with the first column representing the status, followed by
key=value pairs separated by whitespace.

The currently output  keys are B<id>, B<name> and B<uri>.

=head1 SEE ALSO

B<ngcp-kamctl>(8).

=head1 BUGS AND LIMITATIONS

Please report problems you notice to the Sipwise
Development Team <support@sipwise.com>.

=head1 AUTHOR

Guillem Jover <gjover@sipwise.com>

=head1 LICENSE

Copyright (C) 2020 Sipwise GmbH, Austria

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
