~asanjar/charms/trusty/hdp-hadoop/test

« back to all changes in this revision

Viewing changes to hooks/test/hdp_scripts/hdp_manual_install_rpm_helper_files-2.1.1.385/configuration_files/nagios/plugins/check_cpu.pl

  • Committer: amir sanjar
  • Date: 2014-07-21 19:53:44 UTC
  • Revision ID: amir.sanjar@canonical.com-20140721195344-a23z0lrebqzhl167
namenode & data node initialization

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl -w 
2
 
#
3
 
#
4
 
# Licensed to the Apache Software Foundation (ASF) under one
5
 
# or more contributor license agreements.  See the NOTICE file
6
 
# distributed with this work for additional information
7
 
# regarding copyright ownership.  The ASF licenses this file
8
 
# to you under the Apache License, Version 2.0 (the
9
 
# "License"); you may not use this file except in compliance
10
 
# with the License.  You may obtain a copy of the License at
11
 
#
12
 
#   http://www.apache.org/licenses/LICENSE-2.0
13
 
#
14
 
# Unless required by applicable law or agreed to in writing,
15
 
# software distributed under the License is distributed on an
16
 
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
 
# KIND, either express or implied.  See the License for the
18
 
# specific language governing permissions and limitations
19
 
# under the License.
20
 
#
21
 
#
22
 
use strict;
23
 
use Net::SNMP;
24
 
use Getopt::Long;
25
 
 
26
 
# Variable
27
 
my $base_proc = "1.3.6.1.2.1.25.3.3.1";   
28
 
my $proc_load = "1.3.6.1.2.1.25.3.3.1.2"; 
29
 
my $o_host =    undef;
30
 
my $o_community = undef;
31
 
my $o_warn=     undef;
32
 
my $o_crit=     undef;
33
 
my $o_timeout = 15;
34
 
my $o_port = 161;
35
 
 
36
 
sub Usage {
37
 
    print "Usage: $0 -H <host> -C <snmp_community> -w <warn level> -c <crit level>\n";
38
 
}
39
 
 
40
 
Getopt::Long::Configure ("bundling");
41
 
GetOptions(
42
 
  'H:s'   => \$o_host,  
43
 
  'C:s'   => \$o_community,     
44
 
  'c:s'   => \$o_crit,        
45
 
  'w:s'   => \$o_warn
46
 
          );
47
 
if (!defined $o_host || !defined $o_community || !defined $o_crit || !defined $o_warn) {
48
 
  Usage();
49
 
  exit 3;
50
 
}
51
 
$o_warn =~ s/\%//g; 
52
 
$o_crit =~ s/\%//g;
53
 
alarm ($o_timeout);
54
 
$SIG{'ALRM'} = sub {
55
 
 print "Unable to contact host: $o_host\n";
56
 
 exit 3;
57
 
};
58
 
 
59
 
# Connect to host
60
 
my ($session,$error);
61
 
($session, $error) = Net::SNMP->session(
62
 
                -hostname  => $o_host,
63
 
                -community => $o_community,
64
 
                -port      => $o_port,
65
 
                -timeout   => $o_timeout
66
 
          );
67
 
if (!defined($session)) {
68
 
   printf("Error opening session: %s.\n", $error);
69
 
   exit 3;
70
 
}
71
 
 
72
 
my $exit_val=undef;
73
 
my $resultat =  (Net::SNMP->VERSION < 4) ?
74
 
          $session->get_table($base_proc)
75
 
        : $session->get_table(Baseoid => $base_proc);
76
 
 
77
 
if (!defined($resultat)) {
78
 
   printf("ERROR: Description table : %s.\n", $session->error);
79
 
   $session->close;
80
 
   exit 3;
81
 
}
82
 
 
83
 
$session->close;
84
 
 
85
 
my ($cpu_used,$ncpu)=(0,0);
86
 
foreach my $key ( keys %$resultat) {
87
 
  if ($key =~ /$proc_load/) {
88
 
    $cpu_used += $$resultat{$key};
89
 
    $ncpu++;
90
 
  }
91
 
}
92
 
 
93
 
if ($ncpu==0) {
94
 
  print "Can't find CPU usage information : UNKNOWN\n";
95
 
  exit 3;
96
 
}
97
 
 
98
 
$cpu_used /= $ncpu;
99
 
 
100
 
print "$ncpu CPU, ", $ncpu==1 ? "load" : "average load";
101
 
printf(" %.1f%%",$cpu_used);
102
 
$exit_val=0;
103
 
 
104
 
if ($cpu_used > $o_crit) {
105
 
 print " > $o_crit% : CRITICAL\n";
106
 
 $exit_val=2;
107
 
} else {
108
 
  if ($cpu_used > $o_warn) {
109
 
   print " > $o_warn% : WARNING\n";
110
 
   $exit_val=1;
111
 
  }
112
 
}
113
 
print " < $o_warn% : OK\n" if ($exit_val eq 0);
114
 
exit $exit_val;