source: npl/monitoring/cricket/snmp.pm @ b4abfab

perl-5.22
Last change on this file since b4abfab was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 4.1 KB
Line 
1# -*-perl-*-
2
3# Cricket: a configuration, polling and data display wrapper for RRD files
4#
5#    Copyright (C) 1998 Jeff R. Allen and WebTV Networks, Inc.
6#
7#    This program is free software; you can redistribute it and/or modify
8#    it under the terms of the GNU General Public License as published by
9#    the Free Software Foundation; either version 2 of the License, or
10#    (at your option) any later version.
11#
12#    This program is distributed in the hope that it will be useful,
13#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#    GNU General Public License for more details.
16#
17#    You should have received a copy of the GNU General Public License
18#    along with this program; if not, write to the Free Software
19#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21# This lets people replace our choice of snmp libraries by simply
22# replacing snmpUtils.pm (Ed, this means you! <wink>)
23
24use snmpUtils;
25
26use Common::Log;
27
28use strict;
29
30# This tells the collector that we are open for business and
31# happy to handle snmp:// datasources.
32$main::gDSFetch{'snmp'} = \&snmpFetch;
33
34sub snmpFetch {
35    # This procedure is passed a REFERENCE to an array of SNMP datasources.
36    #
37    # Each element consists of a string like:
38    #       index://community@host:port/oid
39    # community and port are optional. If they are left out, they
40    # will default to public and 161, respectively.
41    #
42    # VERY IMPORTANT: The index MUST be returned with the corresponding value,
43    # otherwise it'll get put back into the wrong spot in the RRD.
44
45    my($dsList, $name, $target) = @_;
46    my(%oidsPerSnmp) = ();
47
48    my($dsspec);
49    my($oidMap) = $main::gCT->configHash($name, 'oid');
50    foreach $dsspec (@{ $dsList }) {
51        my($index);
52
53        ($index, $dsspec) = split(/:/, $dsspec, 2);
54        $dsspec =~ s#^//##;
55
56        # This little hack is for people who like to use slashes in their
57        # community strings.
58        my($comm, $cinfo) = split(/@/, $dsspec, 2);
59        my($snmp, $oid)   = split(/\//, $cinfo, 2);
60        $snmp = $comm . "@" . $snmp;
61
62        $oid = mapOid($oidMap, $oid);
63        if (! defined $oid) {
64            Warn("Could not find an OID in $dsspec");
65            return ();
66        }
67
68        # Debug("SNMP parser: snmp://$snmp/$oid");
69
70        # we only try to process them if we didn't find a problem
71        # above (hence the check for undef)
72        push(@{ $oidsPerSnmp{$snmp} }, "$index:$oid")
73            if (defined $oid);
74    }
75
76    my(@results) = ();
77
78    while (my($snmp, $snmpDSRef) = each %oidsPerSnmp) {
79        my(@oidsToQuery) = ();
80
81        if ($#{ $snmpDSRef } >= 0) {
82            my(@oidsToQuery) = ();
83            my(@indices) = ();
84            my($line);
85            while ($line = shift @{ $snmpDSRef }) {
86                my($index, $oid) = split(/:/, $line);
87                push(@indices, $index); push(@oidsToQuery, $oid);
88            }
89
90            Debug("Getting from $snmp ", join(" ", @oidsToQuery));
91            my(@hostResults) = snmpUtils::get($snmp, @oidsToQuery);
92            if (isDebug()) { Debug("Got: ", join(" ", @hostResults)); }
93
94            # it tells us everything went to hell by returning
95            # scalar -1. Unfortunately, we interpret that as the first
96            # result. So, make it undef so that we fix it later.
97            # hopefully, we don't need to fetch a -1 all the time...
98
99            if (defined($hostResults[0]) && $hostResults[0] == -1) {
100                $hostResults[0] = undef;
101            }
102
103            # turn undefs into "U"'s to make RRD happy
104            my($ctr);
105            for $ctr ( 0..$#indices ) {
106                my($res) = $hostResults[$ctr];
107                $res = "U" if (! defined($res));
108                                # previously called fixNum() here, but that breaks mapped
109                                # instances which rely on the fact that the snmp result
110                                # may be non-numeric
111                push(@results, "$indices[$ctr]:" . $res);
112            }
113        }
114    }
115
116    return @results;
117}
118
1191;
120
121# Local Variables:
122# mode: perl
123# indent-tabs-mode: nil
124# tab-width: 4
125# perl-indent-level: 4
126# End:
Note: See TracBrowser for help on using the repository browser.