File: pmlines.pl

package info (click to toggle)
cricket 1.0.5-19
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,936 kB
  • sloc: perl: 8,276; ansic: 329; sh: 162; makefile: 63; sql: 16
file content (197 lines) | stat: -rwxr-xr-x 5,839 bytes parent folder | download | duplicates (7)
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
#!/usr/local/bin/perl

# pmlines.pl
#
# 1998/08/04

#
# Created by  Carlos.Canau@EUnet.pt with cfgmaker  from Tobias Oetiker
# <oetiker@ee.ethz.ch> as skeleton
#
# returns:
# #async
# #digital
# #isdn
# #sync
# #other
#

# 1998/10/30
#
# Modified by Butch Kemper <kemper@tstar.net> to allow multiple PortMasters
# to be specified on the argument line and to output the totals.
#

# 1999/4/18
#
# Modified by Butch Kemper <kemper@tstar.net> to process PM2 systems and
# to distingish between async and isdn ports.
#
# Changed name to PM2lines.pl
#

# 1999/4/19
#
# Modified by Butch Kemper <kemper@tstar.net> to process both PM2 and PM3
# systems.
#
# Changed name to PMlines.pl
#

use SNMP_Session;
use BER;
use Socket;
use strict;

# PMip is livingston.livingstonMib.livingstonInterfaces.livingstonSerial.livingstonSerialTable.livingstonSerialEntry.livingstonSerialIpAddress
# PMty is livingston.livingstonMib.livingstonInterfaces.livingstonSerial.livingstonSerialTable.livingstonSerialEntry.livingstonSerialPhysType

# TODO

# - sum all the calls, connects, detects, renegotiates, and retrains
# from livingstonModemTable.
#
# - think about using livingstonModemStatus instead of
# livingstonSerialIpAddress to count lines in use (not portable to
# PM2)  (or livingstonSerialPortStatus?)

%snmpget::OIDS = ('ifNumber' => '1.3.6.1.2.1.2.1.0',
                  'PMip' => '1.3.6.1.4.1.307.3.2.1.1.1.14',
                  'PMty' => '1.3.6.1.4.1.307.3.2.1.1.1.3',
                  );

my($tot_isdn, $tot_modems, $args) = (0,0,0);
my($tot_isdn, $tot_digitalmodems, $tot_asyncmodems,
   $tot_sync, $tot_other, $args) = (0,0,0,0,0,0);

my($input_string, $PROGNAME, $interfaces)="";
($PROGNAME = $0) =~ s/.*\///;

diexit(0) if $#ARGV < 0;

for ($args=0; $args < $#ARGV+1; $args++) {
    $input_string = $ARGV[$args];
    my($community,$router) = split /\@/, $input_string;

    diexit(0) unless $community && $router;

    $interfaces = snmpget($router,$community,'ifNumber');
    my @PMip = snmpgettable($router,$community,'PMip');
    my @PMty = snmpgettable($router,$community,'PMty');
    my ($i);

    for ($i=0; $i < $#PMip+1; $i++) {
      #
      # instead of livingstonSerialIpAddress we should use livingstonSerialPortStatus
      # and check for "established" (which might be applicable to terminal sessions)
      #
        if ($PMip[$i] ne "0.0.0.0") {
            if ($PMty[$i] == 2) {
                $tot_asyncmodems++;
            } elsif ($PMty[$i] == 3) {
                $tot_sync++;
            } elsif ($PMty[$i] == 5) {
                $tot_digitalmodems++;
            } elsif ($PMty[$i] == 4 || $PMty[$i] == 6 || $PMty[$i] == 7) {
                $tot_isdn++;
            } else {
                $tot_other++;
            }
        }
    }
}

printf "$tot_asyncmodems\n";
printf "$tot_digitalmodems\n";
printf "$tot_isdn\n";
printf "$tot_sync\n";
printf "$tot_other\n";

exit(0);

sub diexit {
    die ("USAGE: $PROGNAME  community\@portmaster [community\@portmaster]" .
        " \.\.\.\n" .
        "       community   = snmp read community string\n" .
        "       portmaster  = FQN of PortMaster\n");
}

sub snmpget{
    my($host,$community,@vars) = @_;
    my(@enoid, $var,$response, $bindings, $binding, $value, $inoid,$outoid,
       $upoid,$oid,@retvals);
    foreach $var (@vars) {
        die "Unknown SNMP var $var\n"
            unless $snmpget::OIDS{$var};
        push @enoid,  encode_oid((split /\./, $snmpget::OIDS{$var}));
    }
    srand();
    my $session = SNMP_Session->open ($host ,
                                      $community,
                                      161);
    if ($session->get_request_response(@enoid)) {
        $response = $session->pdu_buffer;
        ($bindings) = $session->decode_get_response ($response);
        $session->close ();
        while ($bindings) {
            ($binding,$bindings) = decode_sequence ($bindings);
            ($oid,$value) = decode_by_template ($binding, "%O%@");
            # WARNING: this makes getting values such as timeticks in raw form impossible
            my $tempo = pretty_print($value);
            $tempo=~s/\t/ /g;
            $tempo=~s/\n/ /g;
            $tempo=~s/^\s+//;
            $tempo=~s/\s+$//;

            push @retvals,  $tempo;
        }
        return (@retvals);
    } else {
        die "No answer from $input_string. You may be using the wrong community\n";
    }
}

sub snmpgettable{
    my($host,$community,$var) = @_;
    my($next_oid,$enoid,$orig_oid,
       $response, $bindings, $binding, $value, $inoid,$outoid,
       $upoid,$oid,@table,$tempo);
    die "Unknown SNMP var $var\n"
        unless $snmpget::OIDS{$var};

    $orig_oid = encode_oid(split /\./, $snmpget::OIDS{$var});
    $enoid=$orig_oid;
    srand();
    my $session = SNMP_Session->open ($host ,
                                      $community,
                                      161);
    for(;;) {
        if ($session->getnext_request_response(($enoid))) {
            $response = $session->pdu_buffer;
            ($bindings) = $session->decode_get_response ($response);
            ($binding,$bindings) = decode_sequence ($bindings);
            ($next_oid,$value) = decode_by_template ($binding, "%O%@");
            # quit once we are outside the table
            last unless BER::encoded_oid_prefix_p($orig_oid,$next_oid);
            $tempo = pretty_print($value);
            #print "$var: '$tempo'\n";
            $tempo=~s/\t/ /g;
            $tempo=~s/\n/ /g;
            $tempo=~s/^\s+//;
            $tempo=~s/\s+$//;
            push @table, $tempo;
        } else {
            die "No answer from $input_string\n";
        }
        $enoid=$next_oid;
    }
    $session->close ();
    return (@table);
}

# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# tab-width: 4
# perl-indent-level: 4
# End: