~ubuntu-branches/ubuntu/precise/nagios-snmp-plugins/precise

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash
############################## install.sh  #####################
# Version : 1.4
# Date :  Jan 13 2007
# Author  : Patrick Proy ( nagios at proy.org)
# Help : http://www.manubulon.com/nagios/
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
# TODO : 
# Contribs : 
#################################################################
#
# USAGE : ./install [<perl script name> | AUTO <dir> <tmp_dir> <perl_dir> [<install_location>] ]
# USAGE : by default all scripts will be installed
#
# REQUIREMENTS : /bin/bash and sed 
#
# This script will : 
# - Check perl binary (and asks for path)
# - Ask for nagios plugin path and checks for file "utils.pm" in it (default /usr/local/nagios/libexec)
# - Ask for temporary file location (default /tmp)
# - Check Net::SNMP version 
# - Install plugins in the plugins directory and modify paths if necessary.

############################ script list
PLUGINS="check_snmp_boostedge.pl check_snmp_css.pl check_snmp_linkproof_nhr.pl check_snmp_nsbox.pl check_snmp_vrrp.pl check_snmp_cpfw.pl check_snmp_env.pl check_snmp_load.pl check_snmp_process.pl check_snmp_win.pl check_snmp_css_main.pl check_snmp_int.pl check_snmp_mem.pl check_snmp_storage.pl"
############################ get script to install or install type

if [ $# -gt 0 ] ; then INSTSCRIPT=$1 ; else INSTSCRIPT="all" ; fi

if [ $INSTSCRIPT != "AUTO" ] ; then 
    ############################ Manual installation
    echo 
    echo "###### Nagios snmp scripts installer ######"
    echo 
    echo "Will install $INSTSCRIPT script(s)"
    echo

    ############################ default values

    SRCDIR=$PWD
    PERLHOME=`which perl 2>&1`
    if [ $? -ne 0 ]; then PERLHOME="" ; fi

    PLUGHOME=/usr/local/nagios/libexec
    TMPDATA=/tmp
    ############################ Checking Perl

    echo -n "What is your perl location ? [$PERLHOME] "
    read USERPERL
    if [ "ZZ$USERPERL" != "ZZ" ]; then  PERLHOME=$USERPERL ; fi

    if [ "z$PERLHOME" == "z" ]; then 
      echo "Can't find perl binary... exiting"
      echo "######### ERROR ########"
      exit 1
    fi

    NETSNMP=`$PERLHOME -e 'if (eval "require Net::SNMP") { print "Yes" ;}'`
    if [ $? -ne 0 ] ; then 
      echo "Error while checking Net::SNMP module"
      echo "######### ERROR ########"
      exit 1;  
    fi

    if [ "zz$NETSNMP" != "zzYes" ]; then
      echo "Module Net::SNMP not found!"
      echo "Install it with CPAN or manually : http://www.manubulon.com/nagios/faq.html#FAQ2"
      echo "######### ERROR ########"
      exit 1; 
    fi

    SNMPVER=`$PERLHOME -e 'require Net::SNMP;print Net::SNMP->VERSION'`
    echo "Net::SNMP module version is $SNMPVER [OK]"

    GETOPT=`$PERLHOME -e 'if (eval "require Getopt::Long") { print "Yes" ;}'`
    if [ "zz$GETOPT" != "zzYes" ]; then
      echo "Module Getopt::Long not found!"
      echo "Install it with CPAN or manually"
      echo "######### ERROR ########"
      exit 1; 
    fi
    echo "Module Getopt::Long found [OK]"

    ############################ Check nagios plugin directory and utils.pm

    echo
    echo "What is your nagios plugin location ? "
    echo -n "Note : file utils.pm must be present in it [$PLUGHOME] "
    read USERPLUG

    if [ "z$USERPLUG" != "z" ]; then PLUGHOME=$USERPLUG ; fi
    if [ ! -d $PLUGHOME ] ; then 
      echo "Directory $PLUGHOME does not exist !"
      echo "######### ERROR ########"
      exit 1
    fi
    if [ ! -f $PLUGHOME/utils.pm ] ; then 
      echo "File $PLUGHOME/utils.pm does not exist !"
      echo "Install it from nagios plugins"
      echo "######### ERROR ########"
      exit 1
    fi

    ############################ Asking for temp directory

    echo
    echo "Where do you want the plugins to put temporary data (only used by some plugins) ? "
    echo -n "Nagios user must be able to write files in it [$TMPDATA] "
    read USERTMP

    if [ "z$USERTMP" != "z" ]; then TMPDATA=$USERTMP ; fi

    if [ ! -d $TMPDATA ] ; then
      echo "Directory $TMPDATA does not exist !"
      echo "######### ERROR ########"
      exit 1
    fi

    ############################ Looks OK, copying with changes if necessary

    TRANS=""
    # Change '#!/usr/bin/perl -w' 
    if [ $PERLHOME != "/usr/bin/perl" ] ; then
      TRANS="-r -e s#/usr/bin/perl#$PERLHOME#"
    fi

    # Change 'use lib "/usr/local/nagios/libexec";'
    if [ $PLUGHOME != "/usr/local/nagios/libexec" ] ; then
      if [ "z$TRANS" == "z" ]; then TRANS="-r -e s#/usr/local/nagios/libexec#$PLUGHOME#"
      else TRANS="$TRANS -e s#/usr/local/nagios/libexec#$PLUGHOME#";fi
    fi

    # Change 'my $o_base_dir="/tmp/tmp_Nagios_'
    if [ $TMPDATA != "/tmp" ] ; then
      if [ "z$TRANS" == "z" ]; then TRANS="-r -e s#/tmp/tmp_Nagios#$TMPDATA/tmp_Nagios#"
      else TRANS="$TRANS -e s#/tmp/tmp_Nagios#$TMPDATA/tmp_Nagios#";fi
    fi

    ######################### script install

    echo
    echo "Will now install $INSTSCRIPT script(s) : "
    echo "in directory    : $PLUGHOME"
    echo "perl            : $PERLHOME"
    echo "temp directory  : $TMPDATA"
    echo 
    echo -n "OK ? [Y/n]"
    read INSTOK

    if [ "$INSTOK" == "n" ]; then 
      echo "Aborting....."
      echo "######### ERROR ########"
      exit 1
    fi

    ERROR=0

    if [ $INSTSCRIPT == "all" ] ; then 
      for i in $PLUGINS ; do
        echo 
    	if [ ! -f $i ] ; then 
    	  echo "Can't find source file $i : ##### ERROR #####"
    	else
    		echo -n "Installing $i : "
    		if [ "z$TRANS" == "z" ] ; then 
     	      cp $i $PLUGHOME/$i 2>&1
    		else
    		  sed $TRANS $i > $PLUGHOME/$i 2>&1
    		fi
    		if [ $? -ne 0 ] ; then 
    		   echo "##### ERROR #####";	   
    	       rm -f $PLUGHOME/$i  
    		   ERROR=1
    		else 
    		  echo "OK" 
    		  chmod 755 $PLUGHOME/$i 2>&1
    		fi
    	fi
      done
    else
        echo
        if [ ! -f $INSTSCRIPT ] ; then
    	  echo "Can't find source file $INSTSCRIPT : ##### ERROR #####"
    	else
    		echo -n "Installing $INSTSCRIPT : "
    		if [ "z$TRANS" == "z" ] ; then 
    		  cp $INSTSCRIPT >  $PLUGHOME/$INSTSCRIPT
    		else
    		  sed $TRANS $INSTSCRIPT > $PLUGHOME/$INSTSCRIPT 2>&1
    		fi
    		if [ $? -ne 0 ] ; then
    		  echo "##### ERROR #####";
    		  rm -f $PLUGHOME/$INSTSCRIPT
    		  ERROR=1
    		  exit 1;
    		else 
    		  echo "OK" 
    		  chmod 755 $PLUGHOME/$INSTSCRIPT 2>&1
    		fi		
    	fi
    fi

    echo
    if [ $ERROR -eq 1 ] ; then
      echo "Installation ended with errors. Check output above"
      exit 1;
    fi

    echo "Installation completed OK"
    echo "You can delete all the source files and directory"
    echo "Remember to look for informtation at http://www.manubulon.com/nagios/" 
    exit 0;
	
else
####################### Silent install with parameters ############
# PARAM AUTO <dir> <tmp_dir> <perl_dir> [<install_location>]
    if [ $# -ne 4 ] && [ $# -ne 5 ]  ; then exit 1; fi

    SRCDIR=$PWD
    PERLHOME=$4
    PLUGHOME=$2
    TMPDATA=$3
    INSTALLDIR=$5
      
    TRANS=""
    # Change '#!/usr/bin/perl -w' 
    if [ $PERLHOME != "/usr/bin/perl" ] ; then
      TRANS="-r -e s#/usr/bin/perl#$PERLHOME#"
    fi

    # Change 'use lib "/usr/local/nagios/libexec";'
    if [ $PLUGHOME != "/usr/local/nagios/libexec" ] ; then
      if [ "z$TRANS" == "z" ]; then TRANS="-r -e s#/usr/local/nagios/libexec#$PLUGHOME#"
      else TRANS="$TRANS -e s#/usr/local/nagios/libexec#$PLUGHOME#";fi
    fi

    # Change 'my $o_base_dir="/tmp/tmp_Nagios_'
    if [ $TMPDATA != "/tmp" ] ; then
      if [ "z$TRANS" == "z" ]; then TRANS="-r -e s#/tmp/tmp_Nagios#$TMPDATA/tmp_Nagios#"
      else TRANS="$TRANS -e s#/tmp/tmp_Nagios#$TMPDATA/tmp_Nagios#";fi
    fi

    ######################### script install
    ERROR=0
    if [ "z$INSTALLDIR" != "z" ] ; then
      PLUGHOME=$INSTALLDIR
    fi
    for i in $PLUGINS ; do
      if [ ! -f $i ] ; then 
	    ERROR=1
      else
    	if [ "z$TRANS" == "z" ] ; then 
     	    cp $i $PLUGHOME/$i 2>&1
    	else
     	    sed $TRANS $i > $PLUGHOME/$i 2>&1
    	fi
    	if [ $? -ne 0 ] ; then 
    	   rm -f $PLUGHOME/$i  
    	   ERROR=1
    	else 
    	  chmod 755 $PLUGHOME/$i 2>&1
     	  fi
    	fi
      done
    if [ $ERROR -eq 1 ] ; then
      exit 1;
    fi
    exit 0;
fi