需要使perl脚本使用https

时间:2018-03-19 21:30:57

标签: perl

我需要使这个监控脚本使用https和端口443而不是端口80.脚本登录服务器以查看有多少"端口"资源正在使用中。通常意义上这些是网络端口。它们是VOIP端口资源。

无论如何,脚本需要在ssl模式下使用LWP :: UserAgent。 。 。或者其他的东西。

    use strict;
    use Getopt::Long;
    use LWP::UserAgent;
    use HTTP::Request::Common qw(GET POST);
    use XML::Simple;
    use Data::Dumper;
    use Sys::Hostname;
    Getopt::Long::Configure('bundling');

    my ( $oHelp, $oHost, $oUser, $oPw, $oAddress, $oPort, $oWarn, $oCrit );

    my @warn = ( -1, -1 );
    my @crit = ( -1, -1 );
    my $state    = 'OK';
    my $output   = '';
    my $perfdata = '';
    my $percentage;
    my @tmp;
    my $chr1;
    my $chr2;

    my $userAgent;
    my $token;
    my $uToken;
# Bridge refers to the telephony server itself. 
    my $bridgeIp   = '';
    my $bridgeUser = '';
    my $bridgePw   = '';
    my $hostName   = hostname;
    my $bridgePort = '80';
# I tried changing the above 80 to 443 but it did not help.
    my $bridgeUrl  = '';

    # Exit-Status-Array
    my %ERRORS = ( 'UNKNOWN', '-1', 'OK', '0', 'WARNING', '1', 'CRITICAL', '2' );

    # Parameter handling ###########################################################

    GetOptions(
        "H|host:s"    => \$oHost,
        "U|user:s"    => \$oUser,
        "P|pw:s"      => \$oPw,
        "A|address:s" => \$oAddress,
        "w|warn:s"    => \$oWarn,
        "c|crit:s"    => \$oCrit,
        "p|port:i"    => \$oPort,
        "h|help"      => \$oHelp
    );

    if ( $oHelp || !$oUser || !$oHost || !$oPw || !$oAddress ) {
        print <<EOU;
    Usage: $0 -H <FQDN/IP: string> -A <URL/Address: string> -U <Username: string> 
    -P <Password: string> [-p <Port: integer>] [-w <audio ports warn: integer>,<video ports warn: integer>]
    [-c <audio ports warn: integer>,<video ports warn: integer>]


    Options:

    -H --host STRING or IPADDRESS
        FQDN or IP-Address of the bridge
    -A --address STRING 
        Address to the XML API (e.g. http://<hostname>/Receiver.asp)
    -U --user STRING
        User name on the bridge
    -P --pw STRING
        Password of the account on the bridge
    -p --port INTEGER
        TCP port the API listens on the bridge
    -w --warn INT.INT or INT%,INT%
        Warning thresholds. Give maximum number or percentage of audio/video ports to be 
        occupied before warning state occurs.

        Example:
            A value of 50,25 would warn on 51 occupied audio- and/or 26 occupied
            A value of 75%,85% would warn if 76% of the total audio- and/or 86% of the total
            video ports are in use.
    -c --crit INT.INT or INT%,INT%
        Critical thresholds. Give maximum number of audio/video ports to be 
        occupied before critical state occurs.

        Example:
            A value of 60,30 would alert on 61 occupied audio- and/or 31 occupied
            video ports.
            A value of 85%,95% would alert if 86% of the total audio- and/or 96% of the total
            video ports are in use.

    EOU
        exit( $ERRORS{'UNKNOWN'} );
    }

    $bridgeIp   = $oHost;
    $bridgeUrl  = $oAddress;
    $bridgeUser = $oUser;
    $bridgePw   = $oPw;

    if ($oPort) {
        $bridgePort = $oPort;
    }

    if (substr($oWarn, length($oWarn) -1, 1) =~ /\%/) {
        $percentage = "1";
        @tmp = split( ',', $oWarn);
        $chr1 = chop($tmp[0]);
        $chr2 = chop($tmp[1]);
        @warn = @tmp;
    }
    else {
        @warn = split( ',', $oWarn );
    }

    if (substr($oCrit, length($oCrit) -1, 1) =~ /\%/) {
        $percentage = "1";
        @tmp = split( ',', $oCrit);
        $chr1 = chop($tmp[0]);
        $chr2 = chop($tmp[1]);
        @crit = @tmp;
    }
    else {
        @crit = split( ',', $oCrit );
    }

    #if ($oWarn) {
    #    @warn = split( ',', $oWarn );
    #}
    #if ($oCrit) {
    #    @crit = split( ',', $oCrit );
    #}

    # Script start #################################################################

    connect_bridge() if !$userAgent;

    my $request =
        '<TRANS_RSRC_REPORT><TRANS_COMMON_PARAMS><MCU_TOKEN>' 
      . $token
      . '</MCU_TOKEN>'
      . '<MCU_USER_TOKEN>'
      . $uToken
      . '</MCU_USER_TOKEN><MESSAGE_ID>0</MESSAGE_ID>'
      . '</TRANS_COMMON_PARAMS><ACTION><GET_CARMEL_REPORT/></ACTION></TRANS_RSRC_REPORT>';

    my $responseXml = http_post( $bridgeUrl, $request );
    my $oXml = XMLin($responseXml, ForceArray=>['RSRC_REPORT_RMX']);

    #print( $responseXml . "\n" );
    # DEBUG: print Dumper($oXml);
    #print Dumper($oXml);

    my $audioPorts;
    my $videoPorts;
    my $audioPortsHD;
    my $videoPortsHD;

    foreach my $report (
        @{
            $oXml->{'ACTION'}->{'GET_CARMEL_REPORT'}->{'RSRC_REPORT_RMX_LIST'}
              ->{'RSRC_REPORT_RMX'}
        }
      )
    {
        if ( $report->{'RSRC_REPORT_ITEM'} eq 'audio' ) {

            # Possible options:
            #$report->{'RSRC_REPORT_ITEM'}->{'RESERVED'}
            #$report->{'RSRC_REPORT_ITEM'}->{'OCCUPIED'}
            #$report->{'RSRC_REPORT_ITEM'}->{'TOTAL'}
            #$report->{'RSRC_REPORT_ITEM'}->{'FREE'}

            $audioPorts = $report;
        }
        elsif ( $report->{'RSRC_REPORT_ITEM'} eq 'video' ) {
            $videoPorts = $report;
        }
    }

    my $videoTypeHD;

    foreach my $reportHD (
        @{
            $oXml->{'ACTION'}->{'GET_CARMEL_REPORT'}->{'RSRC_REPORT_RMX_LIST_HD'}
              ->{'RSRC_REPORT_RMX'}
        }
      )
    {
        if ( $reportHD->{'RSRC_REPORT_ITEM'} eq 'audio' ) {

            # Possible options:
            #$report->{'RSRC_REPORT_ITEM'}->{'RESERVED'}
            #$report->{'RSRC_REPORT_ITEM'}->{'OCCUPIED'}
            #$report->{'RSRC_REPORT_ITEM'}->{'TOTAL'}
            #$report->{'RSRC_REPORT_ITEM'}->{'FREE'}

            $audioPortsHD = $reportHD;
        }
        elsif ( $reportHD->{'RSRC_REPORT_ITEM'} eq 'HD1080p60_video' ) {
            $videoPortsHD = $reportHD;
        }
        elsif ( $reportHD->{'RSRC_REPORT_ITEM'} eq 'HD1080p30_video' ) {
            $videoPortsHD = $reportHD;
        }
        elsif ( $reportHD->{'RSRC_REPORT_ITEM'} eq 'HD720p30_video' ) {
            $videoPortsHD = $reportHD;
        }
    }

    # Audio thresholds

    if ( $audioPorts ) {
        if ( $percentage ) {
            if ( $audioPorts->{'TOTAL'} != 0 ) {
                if ( $warn[0] != -1 && (( $audioPorts->{'OCCUPIED'} / $audioPorts->{'TOTAL'} ) * 100) > $warn[0] ) {
                    $state = 'WARNING';
                }
            }
        } elsif ( $warn[0] != -1 && $audioPorts->{'OCCUPIED'} > $warn[0] ) {
            $state = 'WARNING';
        }

        if ( $percentage ) {
            if ( $audioPorts->{'TOTAL'} != 0 ) {
                if ( $crit[0] != -1 && (( $audioPorts->{'OCCUPIED'} / $audioPorts->{'TOTAL'} ) * 100) > $crit[0] ) {
                    $state = 'WARNING';
                }
            }
        } elsif ( $crit[0] != -1 && $audioPorts->{'OCCUPIED'} > $crit[0] ) {
            $state = 'CRITICAL';
        }
    }

    # Audio HD Thresholds

    if ( $audioPortsHD ) {
        if ( $percentage ) {
            if ( $audioPortsHD->{'TOTAL'} != 0 ) {
                if ( $warn[0] != -1 && (( $audioPortsHD->{'OCCUPIED'} / $audioPortsHD->{'TOTAL'} ) * 100) > $warn[0] ) {
                    $state = 'WARNING';
                }
            }
        } elsif ( $warn[0] != -1 && $audioPortsHD->{'OCCUPIED'} > $warn[0] ) {
            $state = 'WARNING';
        }

        if ( $percentage ) {
            if ( $audioPortsHD->{'TOTAL'} != 0 ) {
                if ( $crit[0] != -1 && (( $audioPortsHD->{'OCCUPIED'} / $audioPortsHD->{'TOTAL'} ) * 100) > $crit[0] ) {
                    $state = 'WARNING';
                }
            }
        } elsif ( $crit[0] != -1 && $audioPortsHD->{'OCCUPIED'} > $crit[0] ) {
            $state = 'CRITICAL';
        }
    }

    # Video tresholds
    if ( $percentage ) {
        if ( $warn[0] != -1 && (( $videoPorts->{'OCCUPIED'} / $videoPorts->{'TOTAL'} ) * 100) > $warn[0] ) {
            $state = 'WARNING';
        }
    } elsif ( $warn[1] != -1 && $videoPorts->{'OCCUPIED'} > $warn[1] ) {
        $state = 'WARNING';
    }
    if ( $percentage ) {
        if ( $crit[0] != -1 && (( $videoPorts->{'OCCUPIED'} / $videoPorts->{'TOTAL'} ) * 100) > $crit[0] ) {
            $state = 'CRITICAL';
        }
    } elsif ( $crit[1] != -1 && $videoPorts->{'OCCUPIED'} > $crit[1] ) {
        $state = 'CRITICAL';
    }

    # HD Video tresholds
    if ( $percentage ) {
        if ( $warn[0] != -1 && (( $videoPortsHD->{'OCCUPIED'} / $videoPortsHD->{'TOTAL'} ) * 100) > $warn[0] ) {
            $state = 'WARNING';
        }
    } elsif ( $warn[1] != -1 && $videoPortsHD->{'OCCUPIED'} > $warn[1] ) {
        $state = 'WARNING';
    }
    if ( $percentage ) {
        if ( $crit[0] != -1 && (( $videoPortsHD->{'OCCUPIED'} / $videoPortsHD->{'TOTAL'} ) * 100) > $crit[0] ) {
            $state = 'CRITICAL';
        }
    } elsif ( $crit[1] != -1 && $videoPortsHD->{'OCCUPIED'} > $crit[1] ) {
        $state = 'CRITICAL';
    }

    if ( $audioPorts ) {
        $output =
            $state
          . ': Audio in use: '
          . $audioPorts->{'OCCUPIED'} . '/'
          . $audioPorts->{'TOTAL'} . 'SD, '
          . $audioPortsHD->{'OCCUPIED'} . '/'
          . $audioPortsHD->{'TOTAL'} . 'HD, '
          . ' Video in use: '
          . $videoPorts->{'OCCUPIED'} . '/'
          . $videoPorts->{'TOTAL'} . 'SD, '
          . $videoPortsHD->{'OCCUPIED'} . '/'
          . $videoPortsHD->{'TOTAL'} . 'HD';

        $perfdata =
            'audio='
          . $warn[0] . ';'
          . $crit[0] . ';'
          . $audioPorts->{'OCCUPIED'} . ';'
          . $audioPorts->{'TOTAL'} . ';'
          . $audioPortsHD->{'OCCUPIED'} . ';'
          . $audioPortsHD->{'TOTAL'}
          . ' video='
          . $warn[1] . ';'
          . $crit[1] . ';'
          . $videoPorts->{'OCCUPIED'} . ';'
          . $videoPorts->{'TOTAL'} . ';'
          . $videoPortsHD->{'OCCUPIED'} . ';'
          . $videoPortsHD->{'TOTAL'};
    } else {
        $output =
            $state
          . ': Audio in use: 0/0SD, 0/0HD, '
          . ' Video in use: '
          . $videoPorts->{'OCCUPIED'} . '/'
          . $videoPorts->{'TOTAL'} . 'SD, '
          . $videoPortsHD->{'OCCUPIED'} . '/'
          . $videoPortsHD->{'TOTAL'} . 'HD';

        $perfdata =
            'audio='
          . $warn[0] . ';'
          . $crit[0] . ';0;0;0;0'
          . ' video='
          . $warn[1] . ';'
          . $crit[1] . ';'
          . $videoPorts->{'OCCUPIED'} . ';'
          . $videoPorts->{'TOTAL'} . ';'
          . $videoPortsHD->{'OCCUPIED'} . ';'
          . $videoPortsHD->{'TOTAL'};
    }

    print( $output. ' | ' . $perfdata . "\n" );
    exit( $ERRORS{$state} );

    # Subs #########################################################################

    sub login_bridge {
        my $request =
            '<TRANS_MCU><TRANS_COMMON_PARAMS><MCU_TOKEN>-1</MCU_TOKEN>'
          . '<MCU_USER_TOKEN>-1</MCU_USER_TOKEN></TRANS_COMMON_PARAMS><ACTION><LOGIN>'
          . '<MCU_IP><IP>'
          . $bridgeIp . '</IP>'
          . '<LISTEN_PORT>'
          . $bridgePort
          . '</LISTEN_PORT>'
          . '</MCU_IP><USER_NAME>'
          . $bridgeUser
          . '</USER_NAME>'
          . '<PASSWORD>'
          . $bridgePw
          . '</PASSWORD>'
          . '<STATION_NAME>'
          . $hostName
          . '</STATION_NAME>'
          . '</LOGIN></ACTION></TRANS_MCU>';

        my $responseXml = http_post( $bridgeUrl, $request );
        my $xml = XMLin($responseXml);

        # DEBUG: print Dumper($xml);

        my $token  = 0;
        my $uToken = 0;
        if ( $xml->{'RETURN_STATUS'}->{'ID'} == 0 ) {
            $token  = $xml->{'ACTION'}->{'LOGIN'}->{'MCU_TOKEN'};
            $uToken = $xml->{'ACTION'}->{'LOGIN'}->{'MCU_USER_TOKEN'};
        }

        return ( $token, $uToken );
    }

    sub connect_bridge {
        $userAgent = LWP::UserAgent->new( agent => 'VCMgmtLib', keep_alive => 1 );

        ( $token, $uToken ) = login_bridge();

        if ( $token == 0 || $uToken == 0 ) {

            # Login failed
            print("UNKNOWN: Login failed!\n");
            exit( $ERRORS{'UNKNOWN'} );
        }
    }

    sub http_post {
        my ( $url, $content ) = @_;

        my $req = POST $bridgeUrl,
          Content_Type => 'application/x-www-form-urlencoded',
          Content      => $content;

        my $response = $userAgent->request($req);

        print $response->error_as_HTML unless $response->is_success;

        return $response->content;
    }

所以我想也许我可以用 sub connect_bridge {来改变这一节,但我无处可去。

这就是我的尝试:

sub connect_bridge {
    $userAgent = LWP::UserAgent->new( agent => 'VCMgmtLib', keep_alive => 1 );
    ssl_opts => {
        SSL_ca_path     => '/etc/ssl/certs',
        verify_hostname => 0,
    }

    if ( $token == 0 || $uToken == 0 ) {
    }
}

但这会破坏剧本。

语法错误/usr/lib64/nagios/plugins/check_rmx_ports_ssl.pl第385行,靠近&#34;){&#34; 语法错误/usr/lib64/nagios/plugins/check_rmx_ports_ssl.pl第387行,靠近&#34;}&#34;

Can't use global @_ in "my" at /usr/lib64/nagios/plugins/check_rmx_ports_ssl.pl line 390, near "= @_"
Global symbol "$content" requires explicit package name at /usr/lib64/nagios/plugins/check_rmx_ports_ssl.pl line 394.
syntax error at /usr/lib64/nagios/plugins/check_rmx_ports_ssl.pl line 401, near "}"
Execution of /usr/lib64/nagios/plugins/check_rmx_ports_ssl.pl aborted due to compilation errors.

2 个答案:

答案 0 :(得分:0)

正确的语法:

require LWP::Protocol::https;
# Fail at compile time if dependencies are not met.
# This belongs near the `use` statements at the top.

...

$userAgent = LWP::UserAgent->new(
    agent => 'VCMgmtLib',
    keep_alive => 1,
    ssl_opts => {
        SSL_ca_path     => '/etc/ssl/certs',
        verify_hostname => 0,
    }
);

请参阅LWP::UserAgent#ssl_opts

相关:How to ignore 'Certificate Verify Failed' error in perl?

答案 1 :(得分:0)

以下是我如何纳入daxim的建议。非常感谢帮助!!!

sub connect_bridge {
$userAgent = LWP::UserAgent->new(
    agent => 'VCMgmtLib',
    keep_alive => 1,
    ssl_opts => {
        SSL_ca_path     => '/etc/ssl/certs',
        verify_hostname => 0,
    }
);
    ( $token, $uToken ) = login_bridge();

    if ( $token == 0 || $uToken == 0 ) {

        # Login failed
        print("UNKNOWN: Login failed!\n");
        exit( $ERRORS{'UNKNOWN'} );
    }
}

    if ( $token == 0 || $uToken == 0 ) {
}