forked from HariSekhon/Nagios-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_ambari_services.pl
executable file
·124 lines (104 loc) · 3.49 KB
/
check_ambari_services.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
#!/usr/bin/perl -T
# nagios: -epn
#
# Author: Hari Sekhon
# Date: 2013-12-02 20:31:30 +0000 (Mon, 02 Dec 2013)
#
# http://github.com/harisekhon
#
# License: see accompanying LICENSE file
#
$DESCRIPTION = "Nagios Plugin to check Hadoop service states via Ambari REST API
Checks:
- all service states by default
or
- a given service's state
- optionally suppresses alerts in maintenance mode if using the switch --maintenance-ok
Tested on Ambari 1.4.4 / 1.6.1 on Hortonworks HDP 2.0 and 2.1";
$VERSION = "0.6.3";
use strict;
use warnings;
BEGIN {
use File::Basename;
use lib dirname(__FILE__) . "/lib";
}
use HariSekhonUtils;
use HariSekhon::Ambari;
$ua->agent("Hari Sekhon $progname $main::VERSION");
my $service_state = 0;
my $all_service_states = 0;
my $maintenance_ok = 0;
%options = (
%hostoptions,
%useroptions,
%ambari_options_service,
"maintenance-ok" => [ \$maintenance_ok, "Suppress service alerts in maintenance mode" ],
);
splice @usage_order, 10, 0, qw/maintenance-ok/;
get_options();
$host = validate_host($host);
$port = validate_port($port);
$user = validate_user($user);
$password = validate_password($password);
$cluster = validate_ambari_cluster($cluster) if $cluster;
$service = validate_ambari_service($service) if $service;
#$component = validate_ambari_component($component) if $component;
validate_tls();
vlog2;
set_timeout();
$status = "OK";
$url_prefix = "http://$host:$port$api";
list_ambari_components();
cluster_required();
sub get_service_state($){
my $json = shift() || code_error "no hash passed to get_service_state()";
my $msg;
my $service_name = get_field2($json, "ServiceInfo.service_name");
my $service_state = get_field2($json, "ServiceInfo.state");
my $maintenance_state = get_field2($json, "ServiceInfo.maintenance_state");
$service_name = hadoop_service_name $service_name;
if($maintenance_ok and $maintenance_state ne "OFF"){
# suppress alerts if in maintenance mode and --maintenance-ok
$maintenance_state = lc $maintenance_state;
} elsif($service_state eq "STARTED"){
# ok
$service_state = lc $service_state;
} elsif($service_state eq "INSTALLED"){
# This depends on the capitalization from hadoop_service_name
if(grep { $service_name eq $_ } qw/HCatalog Pig Slider Sqoop Tez/){
#ok
$service_state = lc $service_state;
} else {
$service_state = "STOPPED";
critical;
}
} elsif($service_state eq "UNKNOWN"){
unknown;
} elsif(grep { $service_state eq $_ } qw/STARTING INIT UPGRADING MAINTENANCE INSTALLING/){
warning;
} elsif(grep { $service_state eq $_ } qw/INSTALL_FAILED STOPPING UNINSTALLING UNINSTALLED WIPING_OUT/){
critical;
} else {
unknown;
}
$msg .= "$service_name=$service_state";
if($verbose){
$msg .= " (maintenance=$maintenance_state)";
}
return $msg;
}
$msg .= "Ambari service";
if($service){
$json = curl_ambari "$url_prefix/clusters/$cluster/services/$service?fields=ServiceInfo/state,ServiceInfo/maintenance_state";
$msg .= ": " . get_service_state($json);
} else {
$msg .= "s: ";
$json = curl_ambari "$url_prefix/clusters/$cluster/services?fields=ServiceInfo/state,ServiceInfo/maintenance_state";
my @items = get_field_array("items");
foreach(@items){
$msg .= get_service_state($_) . ", ";
}
$msg =~ s/, $//;
}
vlog2;
quit $status, $msg;