/[nagios-plugins-perl]/trunk/plugins/libplugins.pm
ViewVC logotype

Contents of /trunk/plugins/libplugins.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 151 - (show annotations) (download)
Tue Jan 7 10:21:09 2014 UTC (6 years, 10 months ago) by racvision
File size: 3497 byte(s)
mise à jour fr_FR
1 #!/usr/bin/perl -w
2 #
3 # (c) 2003-2006 Stéphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 # DSI - Académie de Nancy-Metz
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty
13 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # you should have received a copy of the GNU General Public License
17 # along with this program (or with Netsaint); if not, write to the
18 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA
20 #
21 #
22 package libplugins;
23
24 use strict; # should never be differently :-)
25 use warnings;
26 use Carp;
27
28 use POSIX qw(setlocale) ; # needed for setlocale()
29
30 use Pod::Usage; # documentation
31 use Locale::gettext; # add i18n support
32
33 use Getopt::Long; # get command line args
34 Getopt::Long::Configure ("no_ignore_case");
35
36 require Exporter;
37 use vars qw(@ISA @EXPORT @EXPORT_OK );
38 use vars qw(%EXIT_CODES %ERR_LEVELS);
39 @ISA = qw(Exporter);
40 @EXPORT = qw(%EXIT_CODES %ERR_LEVELS &showUsage &showVersion);
41 @EXPORT_OK = qw( &getBestUnit);
42
43 # i18n :
44 setlocale(LC_MESSAGES, '');
45 textdomain('nagios-plugins-perl');
46
47 # Predefined exit codes for Nagios/NetSaint:
48 %EXIT_CODES = (
49 'OK' => 0,
50 'WARNING' => 1,
51 'CRITICAL' => 2,
52 'UNKNOWN' => 3,
53 'DEPENDENT' => 4
54 );
55
56 # Predefined log levels:
57 %ERR_LEVELS = (
58 'EMERG' => 0, # système inutilisable
59 'ALERT' => 1, # action à effectuer immédiatement
60 'CRIT' => 2, # conditions critiques
61 'ERR' => 3, # conditions d'erreurs
62 'WARNING' => 4, # message d'avertissement
63 'NOTICE' => 5, # normal mais significatif
64 'INFO' => 6, # informations
65 'DEBUG' => 7, # messages de débugage
66 'TEST' => 8 # for testing only
67 );
68
69
70 # a wrapper function for gettext :
71 sub _($) {
72 gettext($_[0]);
73 }
74
75
76 # Use pod2usage to get documentation
77 sub showUsage {
78 my ($opt_m,$ref) = @_;
79 my %DEFVALUES = ();
80 if ( defined($ref) ) {
81 %DEFVALUES = %{$ref};
82 }
83
84 if ( $> == 0) {
85 # we are root ... lets try to drop privilege.
86 my $manuser = 0;
87 my @tm ;
88
89 if ( @tm = getpwnam('man') ) {
90 $manuser = $tm[2];
91 } elsif ( @tm = getpwnam('nobody') ) {
92 $manuser = $tm[2];
93 } else {
94 croak _("Enable to drop privileges !");
95 }
96
97 printf (_("Changing uid & euid to %s ...")."\n",$manuser) ;
98 # let's become man user
99 $<=$manuser;
100 $>=$manuser;
101
102 }
103
104 pod2usage(
105 -exitstatus => (defined($opt_m)?0:'NOEXIT'),
106 -verbose => (defined($opt_m)?2:1)
107 );
108
109 # Print default values:
110 if ( keys %DEFVALUES > 0) {
111 print _("Default values:")."\n";
112 foreach my $var (keys %DEFVALUES) {
113 printf (" %-15s = %s (%s)\n",$var,$DEFVALUES{$var}{v},$DEFVALUES{$var}{desc});
114 }
115 }
116
117 exit $EXIT_CODES{UNKNOWN};
118 }
119
120 sub showVersion {
121 $0 =~ /\/(.*)$/;
122 print _("Plugin version: ").$1.' v'.$_[0]."\n";
123 exit $EXIT_CODES{UNKNOWN};
124 }
125
126 sub getBestUnit {
127 # Search the best multiple for the given memory (given in KB)
128
129 my ($byte) = @_;
130 my $unit=_("KB");
131 if (($byte / 1024) > 1) {
132 $byte = $byte / 1024;
133 $unit=_("MB");
134 if (($byte / 1024) > 1) {
135 $byte = $byte / 1024;
136 $unit=_("GB");
137 if (($byte / 1024) > 1) {
138 $byte = $byte / 1024;
139 $unit=_("TB");
140 }
141 }
142 }
143 $byte = int(($byte*100))/100;
144 return "$byte$unit";
145 }
146
147 1;

  ViewVC Help
Powered by ViewVC 1.1.8