#!/usr/bin/perl
##
# Check the responsiveness of a SIP server.
#
# Copyright © 2005-2006 O'Shaughnessy Evans <shaug+nagios@aloha.net>
# Copyright © 2011, 2015--2017 Sipwise GmbH, Austria
#
# Requires sipsak; see sipsak.org for more info.
##

use v5.36;

use Getopt::Long qw(:config no_ignore_case);
use NGCP::Gather::SIPOption;

my %ERRORS = (
    OK => 0,
    WARNING => 1,
    CRITICAL => 2,
    UNKNOWN => 3,
    DEPENDENT => 4,
);

my $ME = 'ngcp-check-sip-option';
my $VERSION = '0.1.5';

# Command to get connection state; 1st %s is for extra args, second is SIP URI.
my @Sipsak_Cmd = qw(/usr/bin/sipsak --nagios-code -v -D 5);
my %Flags;
my $URI;
my $Verbose = 0;
my $Proxy = '';
# Use a local port in 6000 <= port < 10000.
my $Local_Port = int(rand(4000) + 6000);
my $Perf = 0;
my $Warn_Threshold = 1;
my $Crit_Threshold = 5;

my $Usage = "$ME -U <sip-uri> [-P <proxy>] [-v] [-w <warn>] [-c <crit>] [-f]\n";
my $Help = <<EOF;
Usage:
  $ME [<option>...] -U <sip-uri>

Options:
  -U, --uri=<sip-uri> Full "sip:user\@host[:port]" URI.
  -P, --proxy=<name>  Outbound proxy hostname;
                        use if different than SIP URI host.
  -w, --warn=<secs>   Warning threshold (in seconds).
  -c, --crit=<secs>   Critical threshold (in seconds).
  -f, --perf          Include perfdata in result.
  -v, --verbose       Show progress details (repeated gives more information).
      --help          Show this usage text.
      --version       Show the version ($VERSION).
EOF

# Handle the command-line
GetOptions(
    'verbose|v+'   => \$Verbose,
    'version|V'    => \$Flags{version},
    'help|h'       => \$Flags{help},
    'proxy|P=s'    => \$Proxy,
    'uri|U=s'      => \$URI,
    'warn|w=s'     => \$Warn_Threshold,
    'crit|c=s'     => \$Crit_Threshold,
    'perf|f'       => \$Perf,
) or die($Usage);

if ($Flags{version}) {
    print "$ME $VERSION";
    exit 0;
} elsif ($Flags{help}) {
    print $Help;
    exit 0;
}

if (!$URI) {
    print "ERROR:  Sorry, but a full URI must be given.\n\n" . $Usage;
    exit $ERRORS{UNKNOWN};
}

my $sip = check_sip_option(
    verbose => $Verbose,
    uri => $URI,
    proxy => $Proxy,
    local_port => $Local_Port,
    warn_threshold => $Warn_Threshold,
    crit_threshold => $Crit_Threshold,
);

my $answer = $sip->{answer};
if (exists $sip->{duration}) {
    $answer .= "Duration: $sip->{duration}";
    if (defined $Perf && $Perf > 0) {
        $answer .= " | duration=$sip->{duration}";
    }
}

my $errname = uc $sip->{state};
my $rc = $ERRORS{$errname};
print "SIP $errname: $answer\n";
exit $rc;

__END__

=head1 NAME

ngcp-check-sip-option - sends an OPTIONS request to a SIP server

=head1 SYNOPSIS

B<ngcp-check-sip-option> [I<option>...] B<-U> I<sip-uri>

=head1 DESCRIPTION

This program sends an OPTIONS request to a SIP server specified by its URI.

It will report back the result in a format consumable by nagios compatible
consumers.

=head1 OPTIONS

=over

=item B<-U>, B<--uri>=I<sip-uri>

Full "I<sip>B<:>I<user>B<@>I<host>[B<:>I<port>]" SIP URI.

=item B<-P>, B<--proxy>=I<hostname>

Outbound proxy I<hostname>; use if it is different from the SIP URI I<host>.

=item B<-w>, B<--warn>=I<seconds>

Warning duration threshold (in I<seconds>).

=item B<-c>, B<--crit>=I<seconds>

Critical duration threshold (in I<seconds>).

=item B<-f>, B<--perf>

Include performance data in result.

=item B<-v>, B<--verbose>

Show progress details (repeated gives more information).

=item B<--help>

Show this usage text.

=item B<--version>

Show the version.

=back

=head1 SEE ALSO

B<sipsak>(1).

=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/>.
