forked from arigaud/Home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_vmware_ntp.pl
216 lines (194 loc) · 7.19 KB
/
check_vmware_ntp.pl
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/perl
#
# check_vmware_ntp.pl - based on vi-check-host-time.pl written by Andrew Sullivan
#
# Copyright (C) 2016 Alexandre Rigaud <arigaud.prosodie.cap@free.fr>
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Report bugs to: nagiosplug-help@lists.sourceforge.net
#
# Examples:
# Check all hosts connected to a vCenter server
# ./check_vmware_ntp.pl --server your.vi.server --warning 3 --critical 1
#
# Check a specific host
# ./check_vmware_ntp.pl --server some.esxi.host
# CRITICAL: some.esxi.host. is -369 seconds off!, 5 Configured NTP servers, service ntpd running|drift=-369s peers=5;1;3;1;3
#
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";
use VMware::VIRuntime;
use Time::Local;
use lib "/usr/local/nagios/libexec";
use utils qw ($TIMEOUT %ERRORS &print_revision &support);
my %STATES = (
0 => "OK",
1 => "WARNING",
2 => "CRITICAL",
3 => "UNKNOWN",
);
my %opts = (
warning => {
default => 1,
type => "=i",
variable => "warning",
help => "number of seconds that is tolerable for drift is alarmed as warning (default:1).",
required => 0,
},
critical => {
default => 3,
type => "=i",
variable => "critical",
help => "number of seconds that is tolerable for drift is alarmed as critical (default:3).",
required => 0,
},
'H' => {
type => '=s',
variable => 'VI_SERVER',
help => 'VI server to connect to. Required if url is not present',
required => 0,
},
peer_warning => {
default => 3,
type => "=i",
variable => "peer_warning",
help => "Set the warning threshold for number of peers (default:3).",
required => 0,
},
peer_critical => {
default => 1,
type => "=i",
variable => "peer_critical",
help => "Set the warning threshold for number of peers (default:1).",
required => 0,
},
);
my $list;
my $msg;
my $drift;
my $ret;
Opts::add_options(%opts);
Opts::parse();
if( !Opts::option_is_set('server') )
{
my $hostname = Opts::get_option('H');
if( defined($hostname) && $hostname ne '' )
{
Opts::set_option('server', $hostname);
}
}
Opts::validate();
Util::connect();
my $warn = Opts::get_option('warning');
my $crit = Opts::get_option('critical');
my $peer_warn = Opts::get_option('peer_warning');
my $peer_crit = Opts::get_option('peer_critical');
$TIMEOUT = 3;
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
print "UNKNOWN - Plugin Timed out\n";
exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
# get only hosts that are connected to vCenter, get only the name and dateTimeSystem MOR
my $hosts = Vim::find_entity_views(
view_type => 'HostSystem',
filter => {
'summary.runtime.connectionState' => "connected"
}#,
# properties => [ 'summary.config.name', 'configManager.dateTimeSystem' ]
);
foreach my $host (@$hosts)
{
# get the date of the system and all the parts so we can convert to epoch time
my $dts = Vim::get_view( mo_ref => $host->get_property('configManager.dateTimeSystem') );
my $hosttime = $dts->QueryDateTime();
my ($year, $month, $day, $hour, $minutes, $seconds) = $hosttime =~ /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*/;
# do the convertion to epoch time
my $hostepoch = timegm($seconds, $minutes, $hour, $day, $month - 1, $year - 1900);
# get the epoch time for the local system
my $localtime = time();
# count ntp servers configured
my $ntp_srv_count = list($dts);
# check for drift, output something
$drift = $hostepoch - $localtime;
if ( abs($drift) > $crit) {
$msg .= $host->get_property('summary.config.name') . " is " . ($drift) . " seconds off!";
$ret = 2;
} elsif ( abs($drift) > $warn) {
$msg .= $host->get_property('summary.config.name') . " is " . ($drift) . " seconds off!";
$ret = 1;
} else {
$msg .= $host->get_property('summary.config.name') . " is within allowable drift (" . ($drift) . "s)";
$ret = 0;
}
if ( $ntp_srv_count > $peer_warn )
{
$msg .= ", $ntp_srv_count Configured NTP servers";
} elsif ($ntp_srv_count <= $peer_crit) {
$msg .= ", only $ntp_srv_count Configured NTP servers!";
$ret = 2;
} elsif($ntp_srv_count <= $peer_warn) {
$msg .= ", only $ntp_srv_count Configured NTP servers!";
$ret = 1 if ($ret < 2);
} else {
$msg .= ", No NTP servers configured!";
$ret = 2;
}
# get ntpd status
my $serviceSystem = Vim::get_view(mo_ref => $host->get_property('configManager.serviceSystem') );
my $services = $serviceSystem->serviceInfo->service;
foreach(@$services) {
if($_->key eq 'ntpd')
{
if($_->running)
{
$msg .= ", service ntpd running";
$ret = 0 if (!defined $ret);
} else
{
$msg .= ", service ntpd not running";
$ret = 2;
}
}
}
$msg .= "|drift=" . $drift . "s peers=$ntp_srv_count;$warn;$crit;$peer_crit;$peer_warn\n";
print "$STATES{$ret}: $msg";
}
Util::disconnect();
exit $ret;
## subs
sub get_servers {
my ($dts) = @_;
my $ntp_config = $dts->{dateTimeInfo}->{ntpConfig};
if (defined($ntp_config)) {
return $ntp_config->{server};
}
return undef;
}
sub list {
my ($dts) = @_;
my $servers = get_servers($dts);
my $ntp_srv_count=0;
if (defined($servers) && scalar(@$servers)) {
foreach my $server (@$servers) {
$ntp_srv_count++;
}
}
return $ntp_srv_count;
}