-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_ntpd.rb
executable file
·151 lines (123 loc) · 4.17 KB
/
check_ntpd.rb
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/ruby
require 'optparse';
##### Helper Functions #####
def print_server_list()
puts "---------------------------";
$peer_health.each do |peer, health|
puts "Received #{health}% of the traffic from #{peer}.";
end
end
def print_overall_health(status)
puts "#{status} - NTPd Health is #{$overall_health}% with #{$peer_count} peers.";
print_server_list();
end
##### Options Parsing #####
# Define default values for command line arguments
options = {
:critical_threshold => 50,
:warning_threshold => 75,
:peer_critical_threshold => 1,
:peer_warning_threshold => 2
};
# Parse command line arguments
OptionParser.new do |opts|
opts.banner = "Usage: check_ntpd.rb [options]";
opts.on("-c", "--critical [INT]", "Set the Critical Health Threshold.") do |c|
options[:critical_threshold] = c;
end
opts.on("-w", "--warning [INT]", "Set the Warning Health Threshold.") do |w|
options[:warning_threshold] = w;
end
opts.on("--peer_critical [INT]", "Set the Critical threshold on the number of active peers.") do |c|
options[:peer_critical_threshold] = c;
end
opts.on("--peer_warning [INT]", "Set the Warning threshold on the number of active peers.") do |w|
options[:peer_warning_threshold] = w;
end
end.parse!
##### Variable declaration #####
$peer_count = 0;
$peer_health = {};
$selected_primary = false;
$selected_backup = 0;
$overall_health = 0;
##### Script Logic #####
# Grab the current NTP peer list
server_list = `/usr/sbin/ntpq -pn`.chomp;
# Parse the server list
# Convert string to array by new line character
server_list = server_list.split(/\n/);
# Get rid of the header row
server_list.delete_at(0);
# Get rid of the header seperator
server_list.delete_at(0);
$peer_count = server_list.size;
# Split the remaining server entries into sub arrays
for i in 0 ... $peer_count
server_list[i] = server_list[i].gsub(/\s+/m, " ").strip.split(" ");
# Check for first character of peer
# space = Discarded due to high stratum and/or failed sanity checks.
# x = Designated falseticker by the intersection algorithm.
# . = Culled from the end of the candidate list.
# - = Discarded by the clustering algorithm.
# + = Included in the final selection set.
# # = Selected for synchronization but distance exceeds maximum.
# * = Selected for synchronization.
# o = Selected for synchronization, pps signal in use.
if server_list[i][0] =~ /^\*/
$selected_primary = true;
elsif server_list[i][0] =~ /^\+/
$selected_backup += 1;
end
# Reset health count to 0
health_count = 0;
# Convert the Reach to Octal and then Binary
reach = server_list[i][6].to_i(8).to_s(2);
# Cycle through reach string to calculate the health percent
for j in 0 ... reach.size
health_count += reach[j..j].to_i;
end
# Calculate this peer's health as a percentage
health = (health_count / reach.size) * 100;
# Add peer to hash
$peer_health [server_list[i][0]] = health;
end
# Cycle through peer hash and add up the peer's healths
$peer_health.each do |peer, health|
$overall_health += health.to_i;
end
# Calculate the average health
$overall_health /= $peer_count;
##### Nagios parsing #####
# If overall health is at or below the critical threshold value, critical
if ($overall_health <= options[:critical_threshold])
print_overall_health("Critical");
exit 2;
# If overall health is at or below than the warning threshold value, warning
elsif ($overall_health <= options[:warning_threshold])
print_overall_health("Warning");
exit 1;
end
# If the number of peers is at or below the critical threshold, critical
if ($peer_count <= options[:peer_critical_threshold])
print_overall_health("Critical");
exit 2;
# If the number of peers is at or below the warning threshold, warning
elsif ($peer_count <= options[:peer_warning_threshold])
print_overall_health("Warning");
exit 1;
end
# If there are no primary NTP peers selected, critical
if ($selected_primary != true)
print_overall_health("Critical");
puts "No primary NTP peer selected.";
exit 2;
# If there are no backup NTP peers selected, warning
elsif ($selected_backup < 1)
print_overall_health("Warning");
puts "No backup NTP peer selected.";
exit 1;
end
# If none of the previous checks triggered an alarm, OK
print_overall_health("OK");
exit 0;