aboutsummaryrefslogtreecommitdiff
path: root/libexec/check_xmpp
blob: 3494ad018757b06dfbc9fd1946e36597b3a74587 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!perl
#
# Connect to an XMPP service to check it's working.
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# License: MIT
#
package main;

# Force me to write this properly
use strict;
use warnings;
use utf8;

# Old Perl should be OK
use 5.006;

# Import required modules
use English '-no_match_vars';
use Monitoring::Plugin '%ERRORS';
use Net::XMPP;

# Decree package version
our $VERSION = '1.01';

our $DESCRIPTION = <<'EOF';
This plugin uses Perl Net::XMPP to connect to an XMPP server, to test whether
it is accepting connections. It does not perform any authentication.
EOF
our $LICENSE = <<'EOF';
MIT License <https://opensource.org/licenses/MIT>
EOF

# Add custom options
our @OPTS = (
    {
        spec  => 'hostname|H=s',
        label => 'HOSTNAME',
        help  => 'Hostname or IP address of device to check',
    },
    {
        spec  => 'port|p=i',
        label => 'PORT',
        help  => 'TCP port to check',
    },
    {
        spec => 'tls|s',
        help => 'Use a secure connection',
    },
);

# Build Monitoring::Plugin object
my $mp = Monitoring::Plugin->new(
    usage => 'Usage: %s'
      . ' [--hostname|-H HOSTNAME]'
      . ' [--port|-p PORT]'
      . ' [--tls|-s]',
    version => $VERSION,
    blurb   => $DESCRIPTION,
    license => $LICENSE,
) or die "Failed plugin build\n";

# Anything fatal in here exits UNKNOWN
eval {

    # Add and read custom options
    for my $opt (@OPTS) {
        $mp->add_arg( %{$opt} );
    }
    $mp->getopts();

    # Start counting down to timeout
    alarm $mp->opts->timeout();

    # Create XMPP object
    my $xmpp = Net::XMPP::Client->new()
      or die "Failed object construct\n";

    # Build connection parameters based on options
    my %conn;
    for my $opt (qw(hostname port tls)) {
        defined( my $val = $mp->opts->get($opt) )
          or next;
        $conn{$opt} = $val;
    }

    # If TLS is being used, get certificate authority details
    if ( $conn{tls} ) {
        require IO::Socket::SSL;
        my %default_ca = IO::Socket::SSL::default_ca();
        for my $key ( keys %default_ca ) {
            $conn{ lc $key } = $default_ca{$key};
        }
    }

    # Attempt connection and add an appropriate message
    my ( $code, $message );
    if ( $xmpp->Connect(%conn) ) {
        ( $code, $message ) = ( $ERRORS{OK}, 'Successful XMPP connection' );
        $xmpp->Disconnect();
    }
    else {
        ( $code, $message ) = ( $ERRORS{CRITICAL}, 'Failed XMPP connection' );
    }
    $mp->add_message( $code, $message );

    # Exit with the status of our most severe error
    $mp->plugin_exit(
        $mp->check_messages(
            join     => ', ',
            join_all => ', ',
        ),
    );

} or $mp->plugin_die($EVAL_ERROR);

1;