-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_mikrotik_vrrp
executable file
·151 lines (126 loc) · 3.77 KB
/
check_mikrotik_vrrp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/perl -w
#
# Copyright (C) 2017 Oliver Hitz <oliver@net-track.ch>
#
# Check state of VRRP interfaces on a Mikrotik router.
#
# Doesn't work with nagios' embedded perl, so switch it off:
# nagios: -epn
use Getopt::Long qw(:config no_ignore_case);
use FindBin;
use lib $FindBin::Bin;
use Mtik;
use strict;
use constant {
STATE_OK => 0,
STATE_WARNING => 1,
STATE_CRITICAL => 2,
STATE_UNKNOWN => 3,
};
my $cfg_verbosity = 0;
my $cfg_host;
my $cfg_port = 8728;
my $cfg_username;
my $cfg_password;
my $cfg_interface;
my $cfg_critical = "disabled";
my $cfg_warning = "backup";
sub usage
{
print <<EOT;
Usage: $0 [parameters]
Parameters:
-H hostname Host (name or IP) to connect to
-p port API port (default: 8728)
-U username API username
-P password API password
-I interface Name of VRRP interface to check
-C state,state... States to be considered as critical
-W state,state... States to be considered as warning
-v, -vv Increase verbosity
EOT
exit(STATE_UNKNOWN);
}
sub debug
{
my $verbose = shift;
my $format = shift;
my (@args) = @_;
if ($verbose <= $cfg_verbosity) {
printf $format, @args;
}
}
sub quit
{
my $status = shift;
my (@messages) = @_;
my $status_text;
if ($status == STATE_OK) {
$status_text = "OK";
} elsif ($status == STATE_WARNING) {
$status_text = "WARNING";
} elsif ($status == STATE_CRITICAL) {
$status_text = "CRITICAL";
} else {
$status_text = "UNKNOWN";
}
printf
"VRRP interface %s - %s\n",
$status_text,
join(" | ", @messages);
exit($status);
}
if (!GetOptions("h|help" => sub { usage(); },
"v|verbose" => sub { $cfg_verbosity = 1; },
"vv" => sub { $cfg_verbosity = 2; },
"H|host=s" => \$cfg_host,
"p|port=i" => \$cfg_port,
"U|username=s" => \$cfg_username,
"P|password=s" => \$cfg_password,
"I|interface=s" => \$cfg_interface,
"C|crit=s" => \$cfg_critical,
"W|warn=s" => \$cfg_warning)) {
usage();
}
if (!$cfg_host || !$cfg_username || !$cfg_interface) {
usage();
}
debug(1, "Logging in to Mikrotik API\n");
if (!Mtik::login($cfg_host, $cfg_username, $cfg_password, $cfg_port)) {
quit(STATE_CRITICAL, "Cannot connect to Mikrotik API.");
}
debug(1, "Ok, logged in\n");
my (%vrrp_ifs) = Mtik::get_by_key("/interface/vrrp/print", "name");
if ($cfg_verbosity == 2) {
debug(3, "VRRP:\n");
foreach my $vrrp_if (keys %vrrp_ifs) {
printf
"%s: %s\n",
$vrrp_if,
join(", ", map{qq{$_=$vrrp_ifs{$vrrp_if}{$_}}} keys %{$vrrp_ifs{$vrrp_if}});
}
}
my $vrrp_if = $vrrp_ifs{$cfg_interface};
if (!$vrrp_if) {
quit(STATE_CRITICAL, sprintf("VRRP interface '%s' not found.", $cfg_interface));
}
my $vrrp_state = "unknown";
if ($vrrp_if->{disabled} eq "true") {
$vrrp_state = "disabled";
} elsif (defined($vrrp_if->{master}) && $vrrp_if->{master} eq "true") {
$vrrp_state = "master";
} elsif (defined($vrrp_if->{backup}) && $vrrp_if->{backup} eq "true") {
$vrrp_state = "backup";
}
if (grep { $_ eq $vrrp_state } split(/,/, $cfg_warning)) {
quit(STATE_WARNING,
sprintf("VRRP interface %s is %s", $cfg_interface, $vrrp_state),
sprintf("Interface details: %s", join(", ", map{qq{$_=$vrrp_if->{$_}}} keys %{$vrrp_if})));
} elsif (grep { $_ eq $vrrp_state } split(/,/, $cfg_critical)) {
quit(STATE_CRITICAL,
sprintf("VRRP interface '%s' is %s", $cfg_interface, $vrrp_state),
sprintf("Interface details: %s", join(", ", map{qq{$_=$vrrp_if->{$_}}} keys %{$vrrp_if})));
}
quit(STATE_OK,
sprintf("VRRP interface '%s' is %s", $cfg_interface, $vrrp_state),
sprintf("Interface details: %s", join(", ", map{qq{$_=$vrrp_if->{$_}}} keys %{$vrrp_if})));