-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_tcp_established
120 lines (83 loc) · 1.91 KB
/
check_tcp_established
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
118
119
120
#!/usr/bin/perl
##################
#
# Check established TCP connections for port 1883 (mosquitto)
#
# Usage: ...
#
# Be careful! Parameters are passed to the shell using qx//
#
# Copyright (C) 2024 Bernd Arnold
# https://github.com/wopfel/monitoring-plugins
#
##################
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
# Sub routines
sub print_help();
my $opt_verbose;
my $opt_help;
my $answer = "";
my $crit_count = 0;
my $warn_count = 0;
my $status = "Unknown";
my @perfdata = ();
my @additional = ();
#
# Process command line options
#
GetOptions( "v|verbose" => \$opt_verbose,
"help" => \$opt_help,
)
or do { print STDERR "Error in command line argument processing. Try '$0 -help'.\n"; exit 4; };
if ( $opt_help ) {
print_help();
exit 4; # Outside of the official monitoring plugin's exit codes
}
#
# Query network
#
my $output = qx{ ss -ntp --no-header state established src :1883 };
print Dumper( $output ) if $opt_verbose;
my $count = 0;
for my $line ( split /\n/, $output ) {
$count++;
}
$answer .= "$count connections. ";
push @perfdata, "connections=$count";
#
# Check results
#
# TODO
###
if ( $crit_count ) {
$status = "Critical";
} elsif ( $warn_count ) {
$status = "Warning";
} else {
$status = "Ok";
}
print "$status: ";
print "$answer\n";
print "| " . join(" ", @perfdata) . "\n" if @perfdata;
print join("\n", @additional) . "\n" if @additional;
exit 2 if $status eq "Critical";
exit 1 if $status eq "Warning";
exit 0 if $status eq "Ok";
exit 3; # 'Unknown' return code
#
# Print help
#
sub print_help() {
print STDERR <<~"END"
Check established network connections on port 1883 (mosquitto)
Usage: $0
Example: $0
Just show the values
Parameters:
-h, --help Show this help text
-v, --verbose Verbose output (for debugging)
END
}