Bonjour,

J'ai besoin de créer un petit script pour nagios testant les valeurs retours pour certains tests.
Malheureusement, je connais trop peu perl pour pouvoir écrire aisément des programmes.
Voilà, j'ai récupéré quelques perl script déjà existant et j'essaye de comprendre le fonctionnement...
Je me permets de vous posez quelque question que je n'arrive pas éclaircir par rapport à un de ces scripts (voir ci-dessous):

1-Qu'obtient-on par ceci?Est-ce l'ensemble du plugins SNMP?

2-Pourquoi on ferme cette session??
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
# Close the session
$s->close();
3-J'ai remarqué qu'il test si le mib existe avec la fonction !defined, ensuite il récupère la valeur de ex.: $mem5minused par un foreach:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
foreach ( $s->var_bind_names() ) {
        $mem5minused = $s->var_bind_list()->{$_};
Où se trouve les déclarations de var_bind_names et var_bind_list???
Et si je comprends bien la variable $mem5minused récupère la valeur reçu en argument?? :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
$s->var_bind_list()->{$_}
Merci d'avance pour votre aide où vos idées, liens,etc...

Ci-joint l'exemple que j'ai trouvé :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#!/usr/bin/perl
#
#
# AUTHORS:
#    Copyright (C) 2003-2013 Opsview Limited. All rights reserved
#
#    This file is part of Opsview
#
#    Opsview is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    Opsview is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Opsview; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
 
use warnings;
use strict;
 
use FindBin qw($Bin);
use lib "/opt/opsview/perl/lib/perl5", "$Bin/../lib";
use Net::SNMP;
use Opsview::NagiosPlugin::SNMP;
 
my $np = Opsview::NagiosPlugin::SNMP->new(
    usage     => "Usage: %s",
    shortname => "check_snmp_cisco_memutil",
    version   => "2.1.1",
    blurb     => "Memory utilisation on Cisco devices",
    snmp      => {
        "snmp-version" => 1,
        "snmp-timeout" => 2,
    },
);
 
$np->add_arg(
    spec => "warning|w=s",
    help => qq{-w, --warning=INTEGER
    Warning threshold (%)},
    default => 90,
);
 
$np->add_arg(
    spec => "critical|c=s",
    help => qq{-c, --critical=INTEGER
    Critical threshold (%) },
    default => 95,
);
 
$np->getopts;
 
my $warning  = $np->opts->warning;
my $critical = $np->opts->critical;
 
my $s = $np->snmp;
 
my $oid_sysDescr    = ".1.3.6.1.2.1.1.1.0";
my $oid_mem5minUsed = "1.3.6.1.4.1.9.9.48.1.1.1.5.1";
my $oid_mem5minFree = "1.3.6.1.4.1.9.9.48.1.1.1.6.1";
 
my $returnstring = "";
my $status       = 0;
 
main();
 
# Close the session
$s->close();
 
if ( $returnstring eq "" ) {
    $status = 3;
}
 
if ( $status == 0 ) {
    print "Status is OK - $returnstring\n";
 
    # print "$returnstring\n";
}
elsif ( $status == 1 ) {
    print "Status is a WARNING level - $returnstring\n";
}
elsif ( $status == 2 ) {
    print "Status is CRITICAL - $returnstring\n";
}
else {
    print "Status is UNKNOWN\n";
}
 
exit $status;
 
####################################################################
# This is where we gather data via SNMP and return results         #
####################################################################
 
sub main {
 
    my $mem5minused = 0;
    my $mem5minfree = 0;
    my $memtotal    = 0;
    my $mempcused   = 0;
    my $temp        = 0;
    my $perfinfo    = "";
 
    if ( !defined( $s->get_request($oid_mem5minUsed) ) ) {
        if ( !defined( $s->get_request($oid_sysDescr) ) ) {
            $returnstring = "SNMP agent not responding";
            $status       = 1;
            return 1;
        }
        else {
            $returnstring = "SNMP OID does not exist";
            $status       = 1;
            return 1;
        }
    }
    foreach ( $s->var_bind_names() ) {
        $mem5minused = $s->var_bind_list()->{$_};
    }
 
    if ( !defined( $s->get_request($oid_mem5minFree) ) ) {
        if ( !defined( $s->get_request($oid_sysDescr) ) ) {
            $returnstring = "SNMP agent not responding";
            $status       = 1;
            return 1;
        }
        else {
            $returnstring = "SNMP OID does not exist";
            $status       = 1;
            return 1;
        }
    }
    foreach ( $s->var_bind_names() ) {
        $mem5minfree = $s->var_bind_list()->{$_};
    }
 
    $memtotal = $mem5minused + $mem5minfree;
    $mempcused = ( ( 100 / $memtotal ) * $mem5minused );
 
    $temp =
      sprintf "MEMORY: total: %.2f MB, used: %.2f MB (%.0f%%), free: %.2f MB",
      ( $memtotal / 1000000 ), ( $mem5minused / 1000000 ), $mempcused,
      ( $mem5minfree / 1000000 );
 
    append($temp);
 
    $perfinfo = sprintf "|memory=%.2f", ( $mem5minused / 1000000 );
    append($perfinfo);
 
    if ( $mempcused >= $warning ) {
        $status = 1;
    }
    if ( $mempcused >= $critical ) {
        $status = 2;
    }
}
 
####################################################################
# Appends string to existing $returnstring                         #
####################################################################
 
sub append {
    my $appendstring = $_[0];
    $returnstring = "$returnstring$appendstring";
}