Logoj0ke.net Open Build Service > Projects > server:monitoring > nagios-plugins-snmp > check_cpu
Sign Up | Log In

File check_cpu of Package nagios-plugins-snmp

 
1
#!/usr/bin/perl  
2
# -*- perl -*-
3
4
#===============================================================================
5
# Name
6
#   check_cpu
7
#
8
# Description
9
#   Checks the CPU utilization of routers
10
#   Returns text to STDOUT and return code to be used by the netsaint process
11
#
12
# TODO
13
#
14
# Author
15
#  Original author Matthew Davy (mpd@iu.edu)
16
#
17
# Copyright
18
#  Copyright (c) 2002 The Trustees of Indiana University.
19
#  All Rights Reserved.
20
#===============================================================================
21
22
#===============================================================================
23
#                               Configuration        
24
#===============================================================================
25
26
my $lastScriptModification = '2002-11-24';
27
28
#===============================================================================
29
#                              Initialization
30
#===============================================================================
31
32
#-------------------------------------------------------------------------------
33
#    Libraries
34
#-------------------------------------------------------------------------------
35
36
use Getopt::Std;
37
38
39
#-------------------------------------------------------------------------------
40
#    Global var declarations
41
#-------------------------------------------------------------------------------
42
my (
43
   # Working vars 
44
   $usage,      # String containing usage help
45
   $hostname,       # Hostname of machine being queried via SNMP
46
   $community,      # SNMP community string to use for query
47
   $oid         # CPU OID 
48
   );
49
50
#-------------------------------------------------------------------------------
51
#    Global var initializations
52
#-------------------------------------------------------------------------------
53
54
$usage = <<"EOF";
55
usage:  $0 [-h] -r <hostname> -c <community> -v <vendor>
56
57
[-h]            :       Print this message
58
[-r] <router>   :       IP Address or Hostname of the router
59
[-c] <community>:       SNMP Community String  (default = "public")
60
[-v] <vendor>   :       juniper | cisco | lancom
61
                               
62
$lastScriptModification
63
 
64
EOF
65
66
#===============================================================================
67
#                              Input Phase
68
#===============================================================================
69
70
# Check the usage
71
die $usage if (!getopts('hr:c:v:') || $opt_h);
72
die $usage if (!$opt_r || !$opt_v || $opt_h );
73
74
# Build the args for the check_snmp command 
75
$hostname = $opt_r;
76
$community = $opt_c || "public";
77
if ($opt_v eq "juniper") {
78
    $oid = ".1.3.6.1.4.1.2636.3.1.13.1.8.9.1.0.0";
79
} elsif ($opt_v eq "lancom") {
80
    $oid = ".1.3.6.1.4.1.2356.600.3.54.1.47.3";
81
} elsif ($opt_v eq "cisco") {
82
    $oid = ".1.3.6.1.4.1.9.2.1.58.0";
83
} else {
84
    die $usage;
85
}
86
87
88
# Call the check_snmp command with the appropriate args
89
$_ = `/usr/lib/nagios/plugins/check_snmp -H $hostname -C $community -o $oid`;
90
91
92
#===============================================================================
93
#                              Output Phase
94
#===============================================================================
95
96
# Print a line to STDOUT and generate an exit code based on what the 
97
# check_snmp command returns.
98
99
if ( (/SNMP OK - (\d+)/) || (/SNMP OK - INTEGER: (\d+)/)) {
100
    $cpu = $1;  
101
102
    # Print some useful text...
103
    print "5 Minute Avg CPU Usage is $cpu%\n";
104
105
    # Exit with the correct status (2=Critical, 1=Warning, 0=OK, -1=error)
106
    if ($cpu > 75) {
107
        exit(2);
108
    } elsif ($cpu > 50) { 
109
        exit(1);
110
    } else {
111
        exit(0);
112
    }
113
} else {
114
    print "Error: No response from $hostname\n";
115
    exit(-1);
116
}
117
118