This file is indexed.

/usr/share/munin/plugins/snmp__if_multi is in munin-plugins-core 2.0.37-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#!/usr/bin/perl -w
# -*- cperl -*-

=head1 NAME

snmp__if_multi - SNMP card plugin to monitor the network interfaces of any networked equipment.

=head1 APPLICABLE SYSTEMS

Any SNMP capable networked computer equipment.  Using a command such
as "munin-node-configure --snmp switch.langfeldt.net --snmpversion 2c
--snmpcommunity public | sh -x" should auto-detect all applicable
interfaces.  On a typical switch you will get one plugin pr. ethernet
port.  On a router you might get one plugin pr. VLAN interface.

=head1 CONFIGURATION

As a rule SNMP plugins need site specific configuration.  The default
configuration (shown here) will only work on insecure sites/devices:

   [snmp_*]
	env.version 2
        env.community public
        env.ifTypeOnly ethernetCsmacd

In general SNMP is not very secure at all unless you use SNMP version
3 which supports authentication and privacy (encryption).  But in any
case the community string for your devices should not be "public".

Please see 'perldoc Munin::Plugin::SNMP' for further configuration
information.

The ifTypeOnly is a space separated list of media types to show.  By
default the plugin shows only interfaces identified as
'ethernetCsmacd'.  To see what interface types your switch, router, or
other net thing uses you can use this command:

  snmpwalk -c public -v 2c switch 1.3.6.1.2.1.2.2.1.3

It may show something like this:

  IF-MIB::ifType.1 = INTEGER: ethernetCsmacd(6)
  IF-MIB::ifType.4 = INTEGER: mplsTunnel(150)
  IF-MIB::ifType.5 = INTEGER: other(1)
  IF-MIB::ifType.6 = INTEGER: softwareLoopback(24)
  IF-MIB::ifType.8 = INTEGER: tunnel(131)
  IF-MIB::ifType.13 = INTEGER: propVirtual(53)
  IF-MIB::ifType.123 = INTEGER: l2vlan(135)

propVirtual or l2vlan is usualy used for VLAN interfaces.  Tunnel
would normaly be for VPNs.  A minor horde of different interface types
are supposted, please see IANAifType-MIB (on your system or find with
Google) for a full list.

=head1 INTERPRETATION

The graph shows a stright forward "bits per second" incomming and
outgoing thruput.  "Incomming" is towards the monitored device.

Note: The internal representation of the speeds is in bytes
pr. second.  The plugin multiplies everyting by 8 to get bits
pr. second.

=head1 MIB INFORMATION

This plugin requires the IF-MIB the standard IETF MIB for network
interfaces.

It reports the contents of the
IF-MIB::ifHCInOctets/IF-MIB::ifHCOutOctets if available,
IF-MIB::ifInOctets/IF-MIB::ifOutOctets if not.  The former are 64 bit
counters only available with SNMP 2 and later.  The later are 32 bit
counters (see FEATURES below).

For errors it reports ifInErrors + ifINDiscards and ifOutErrors +
ifOutDiscards.

=head1 MAGIC MARKERS

  #%# family=snmpauto
  #%# capabilities=snmpconf

=head1 VERSION

  $Id: snmp__if_.in 1818 2009-01-03 19:29:30Z janl $

=head1 BUGS

Should support indexing by

  - ifIndex
  - ifName
  - ifDescr
  - ifAlias
  - mac-address

(and anything else MRTG can index by) in addition to OID-index as now.

Pulling in a user definable set of ifName/ifDescr/ifAlias for textual
description and even graph_title would also be nice.

If we get a patch to support the .oldname attribute then we may use
that to let the operator change the indexing dynamicaly without data
loss.

=head1 FEATURES

You may get strange results if you use SNMPv1, or SNMPv2 on
switches that do not support 64 bit byte counters.  If the interface
traffic exceeds about 50Mbps a 32 bit byte counter will wrap around in
less than 5 minutes making the graph for the interface show random
results.

If you have a switch/device that supports 64 bit byte counters this
plugin will use them and the graph will be fine.  The graph
information will inform about this.  You must use SNMPv2c or SNMPv3 to
be able to use 64 bit counters - if the device supports them.

This problem is a feature of the device SNMP implementation or your
usage of it, it is nothing the plugin can fix.  In the future Munin
may be able to run the plugin more often than the counter wraps
around.

=head1 AUTHOR

Copyright (C) 2004-2010 Jimmy Olsen, Dagfinn Ilmari Mannsaaker,
Nicolai Langfeldt, Redpill Linpro AS and others.

Original snmp__if_ plugin: Copyright (C) 2004-2009 Jimmy Olsen, Dagfinn
Ilmari Mannsaaker.

Initial SNMPv3 support by "Confusedhacker".

Documentation, porting to Munin::Plugin::SNMP and
further grooming by Nicolai Langfeldt.

Reworked to snmp__if_multi by Nicolai Langfeldt.

=head1 LICENSE

GPLv2

=cut

use strict;
use Munin::Plugin;
use Munin::Plugin::SNMP;

need_multigraph();


my $response;
my $iface;

# This is the snmpwalk:
# .1.3.6.1.2.1.2.1.0 = INTEGER: 2
# .1.3.6.1.2.1.2.2.1.1.1 = INTEGER: 1
# .1.3.6.1.2.1.2.2.1.1.65539 = INTEGER: 65539
# .1.3.6.1.2.1.2.2.1.2.1 = STRING: MS TCP Loopback interface
# .1.3.6.1.2.1.2.2.1.2.65539 = STRING: Broadcom NetXtreme Gigabit Ethernet
# .1.3.6.1.2.1.2.2.1.3.1 = INTEGER: softwareLoopback(24)
# .1.3.6.1.2.1.2.2.1.3.65539 = INTEGER: ethernetCsmacd(6)
# .1.3.6.1.2.1.2.2.1.4.1 = INTEGER: 1520
# .1.3.6.1.2.1.2.2.1.4.65539 = INTEGER: 1500
# .1.3.6.1.2.1.2.2.1.5.1 = Gauge32: 10000000
# .1.3.6.1.2.1.2.2.1.5.65539 = Gauge32: 1000000000
# .1.3.6.1.2.1.2.2.1.6.1 = STRING:
# .1.3.6.1.2.1.2.2.1.6.65539 = STRING: 0:30:48:75:65:5e
# .1.3.6.1.2.1.2.2.1.7.1 = INTEGER: up(1)
# .1.3.6.1.2.1.2.2.1.7.65539 = INTEGER: up(1)
# .1.3.6.1.2.1.2.2.1.8.1 = INTEGER: up(1)
# .1.3.6.1.2.1.2.2.1.8.65539 = INTEGER: up(1)
#
# 64 bit counters:
# .1.3.6.1.2.1.31.1.1.1.6.   Counter64 ifHCInOctets
# .1.3.6.1.2.1.31.1.1.1.10.  Counter64 ifHCOutOctets

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf") {
	print "require 1.3.6.1.2.1.2.2.1.1.\n";
	exit 0;
}

my $sysDescr           = '1.3.6.1.2.1.1.1.0';
my $sysLocation        = '1.3.6.1.2.1.1.6.0';
my $sysContact         = '1.3.6.1.2.1.1.4.0';
my $sysName            = '1.3.6.1.2.1.1.5.0';

my $ifOIDBase          = "1.3.6.1.2.1.2.2.1";		# ifEntry
my $ifv2OIDBase        = "1.3.6.1.2.1.31.1.1.1";	# ifXEntry

my %ifTypes = (
    other => 1,
    regular1822 => 2,
    hdh1822 => 3,
    ddnX25 => 4,
    rfc877x25 => 5,
    ethernetCsmacd => 6,
    iso88023Csmacd => 7,
    iso88024TokenBus => 8,
    iso88025TokenRing => 9,
    iso88026Man => 10,
    starLan => 11,
    proteon10Mbit => 12,
    proteon80Mbit => 13,
    hyperchannel => 14,
    fddi => 15,
    lapb => 16,
    sdlc => 17,
    ds1 => 18,
    e1 => 19,
    basicISDN => 20,
    primaryISDN => 21,
    propPointToPointSerial => 22,
    ppp => 23,
    softwareLoopback => 24,
    eon => 25,
    ethernet3Mbit => 26,
    nsip => 27,
    slip => 28,
    ultra => 29,
    ds3 => 30,
    sip => 31,
    frameRelay => 32,
    rs232 => 33,
    para => 34,
    arcnet => 35,
    arcnetPlus => 36,
    atm => 37,
    miox25 => 38,
    sonet => 39,
    x25ple => 40,
    iso88022llc => 41,
    localTalk => 42,
    smdsDxi => 43,
    frameRelayService => 44,
    v35 => 45,
    hssi => 46,
    hippi => 47,
    modem => 48,
    aal5 => 49,
    sonetPath => 50,
    sonetVT => 51,
    smdsIcip => 52,
    propVirtual => 53,
    propMultiplexor => 54,
    ieee80212 => 55,
    fibreChannel => 56,
    hippiInterface => 57,
    frameRelayInterconnect => 58,
    aflane8023 => 59,
    aflane8025 => 60,
    cctEmul => 61,
    fastEther => 62,
    isdn => 63,
    v11 => 64,
    v36 => 65,
    g703at64k => 66,
    g703at2mb => 67,
    qllc => 68,
    fastEtherFX => 69,
    channel => 70,
    ieee80211 => 71,
    ibm370parChan => 72,
    escon => 73,
    dlsw => 74,
    isdns => 75,
    isdnu => 76,
    lapd => 77,
    ipSwitch => 78,
    rsrb => 79,
    atmLogical => 80,
    ds0 => 81,
    ds0Bundle => 82,
    bsc => 83,
    async => 84,
    cnr => 85,
    iso88025Dtr => 86,
    eplrs => 87,
    arap => 88,
    propCnls => 89,
    hostPad => 90,
    termPad => 91,
    frameRelayMPI => 92,
    x213 => 93,
    adsl => 94,
    radsl => 95,
    sdsl => 96,
    vdsl => 97,
    iso88025CRFPInt => 98,
    myrinet => 99,
    voiceEM => 100,
    voiceFXO => 101,
    voiceFXS => 102,
    voiceEncap => 103,
    voiceOverIp => 104,
    atmDxi => 105,
    atmFuni => 106,
    atmIma => 107,
    pppMultilinkBundle => 108,
    ipOverCdlc => 109,
    ipOverClaw => 110,
    stackToStack => 111,
    virtualIpAddress => 112,
    mpc => 113,
    ipOverAtm => 114,
    iso88025Fiber => 115,
    tdlc => 116,
    gigabitEthernet => 117,
    hdlc => 118,
    lapf => 119,
    v37 => 120,
    x25mlp => 121,
    x25huntGroup => 122,
    trasnpHdlc => 123,
    interleave => 124,
    fast => 125,
    ip => 126,
    docsCableMaclayer => 127,
    docsCableDownstream => 128,
    docsCableUpstream => 129,
    a12MppSwitch => 130,
    tunnel => 131,
    coffee => 132,
    ces => 133,
    atmSubInterface => 134,
    l2vlan => 135,
    l3ipvlan => 136,
    l3ipxvlan => 137,
    digitalPowerline => 138,
    mediaMailOverIp => 139,
    dtm => 140,
    dcn => 141,
    ipForward => 142,
    msdsl => 143,
    ieee1394 => 144,
    'if-gsn' => 145,
    dvbRccMacLayer => 146,
    dvbRccDownstream => 147,
    dvbRccUpstream => 148,
    atmVirtual => 149,
    mplsTunnel => 150,
    srp => 151,
    voiceOverAtm => 152,
    voiceOverFrameRelay => 153,
    idsl => 154,
    compositeLink => 155,
    ss7SigLink => 156,
    propWirelessP2P => 157,
    frForward => 158,
    rfc1483 => 159,
    usb => 160,
    ieee8023adLag => 161,
    bgppolicyaccounting => 162,
    frf16MfrBundle => 163,
    h323Gatekeeper => 164,
    h323Proxy => 165,
    mpls => 166,
    mfSigLink => 167,
    hdsl2 => 168,
    shdsl => 169,
    ds1FDL => 170,
    pos => 171,
    dvbAsiIn => 172,
    dvbAsiOut => 173,
    plc => 174,
    nfas => 175,
    tr008 => 176,
    gr303RDT => 177,
    gr303IDT => 178,
    isup => 179,
    propDocsWirelessMaclayer => 180,
    propDocsWirelessDownstream => 181,
    propDocsWirelessUpstream => 182,
    hiperlan2 => 183,
    propBWAp2Mp => 184,
    sonetOverheadChannel => 185,
    digitalWrapperOverheadChannel => 186,
    aal2 => 187,
    radioMAC => 188,
    atmRadio => 189,
    imt => 190,
    mvl => 191,
    reachDSL => 192,
    frDlciEndPt => 193,
    atmVciEndPt => 194,
    opticalChannel => 195,
    opticalTransport => 196,
    propAtm => 197,
    voiceOverCable => 198,
    infiniband => 199,
    teLink => 200,
    q2931 => 201,
    virtualTg => 202,
    sipTg => 203,
    sipSig => 204,
    docsCableUpstreamChannel => 205,
    econet => 206,
    pon155 => 207,
    pon622 => 208,
    bridge => 209,
    linegroup => 210,
    voiceEMFGD => 211,
    voiceFGDEANA => 212,
    voiceDID => 213,
    mpegTransport => 214,
    sixToFour => 215,
    gtp => 216,
    pdnEtherLoop1 => 217,
    pdnEtherLoop2 => 218,
    opticalChannelGroup => 219,
    homepna => 220,
    gfp => 221,
    ciscoISLvlan => 222,
    actelisMetaLOOP => 223,
    fcipLink => 224,
    rpr => 225,
    qam => 226,
    lmp => 227,
    cblVectaStar => 228,
    docsCableMCmtsDownstream => 229,
    adsl2 => 230,
    macSecControlledIF => 231,
    macSecUncontrolledIF => 232,
    aviciOpticalEther => 233,
    atmbond => 234,
    voiceFGDOS => 235,
    mocaVersion1 => 236,
    ieee80216WMAN => 237,
    adsl2plus => 238,
    dvbRcsMacLayer => 239,
    dvbTdm => 240,
    dvbRcsTdma => 241,
    x86Laps => 242,
    wwanPP => 243,
    wwanPP2 => 244
    );

my %ifTypeByNum = map { $ifTypes{$_} => $_; } keys %ifTypes;

# Needed as globals
my $snmpinfo;
my $snmpinfoX;

sub do_collect {
    # Collect information from SNMP agent

    my $session = Munin::Plugin::SNMP->session();

    $snmpinfo = $session->get_hash(
	-baseoid => $ifOIDBase,
	-cols    =>
	{ 2  => 'ifDescr',
	  # Type: See above
	  3  => 'ifType',
	  4  => 'ifMtu',
	  5  => 'ifSpeed',
	  6  => 'ifMac',
	  7  => 'ifAdminStatus',
	  # Oper: 1) up 2) down 3) testing
	  # 4) unknown, 5) dormant 6) not present
	  # 7) lowerLayerDown
	  8  => 'ifOperStatus',
	  10 => 'ifInOctets',
	  11 => 'ifInUcastPkts',
	  13 => 'ifInDiscards',
	  14 => 'ifInErrors',
	  15 => 'ifUnUnknownProtos',
	  16 => 'ifOutOctets',
	  18 => 'ifOutNUcastPkts',
	  19 => 'ifOutDiscards',
	  20 => 'ifOutErrors',
	});

    # ifXEntry - SNMP v2 and up only - on some devices
    $snmpinfoX = $session->get_hash(
	-baseoid => $ifv2OIDBase,
	-cols    =>
	{ 2  => 'ifInMulticastPkts',
	  3  => 'ifInBroadcastPkts',
	  4  => 'ifOutMulticastPkts',
	  5  => 'ifOutBroadcastPkts',
	  6  => 'ifHCInOctets',
	  10 => 'ifHCOutOctets',
	  15 => 'ifHighSpeed',
	  18 => 'ifAlias',
	});
}



sub do_preprocess_if {
    my ($mediatype, $if) = @_;

    my $response = $snmpinfo->{$if}->{ifType} || 1;

    if (defined($mediatype)) {
	if (defined($mediatype->{$response})) {
	    # This is one of the interesting media types
	} else {
	    # This media type is not interesting.  Remove.
	    delete $snmpinfo->{$if} if exists $snmpinfo->{$if};
	    delete $snmpinfoX->{$if} if exists $snmpinfoX->{$if};
	    return;
	}
    }

    if (defined ($response = $snmpinfo->{$if}->{ifOperStatus})) {
	# 1 = up,  2 = down,  7 = lowerLayerDown
	if ($response == 2 or $response == 7) {
	    # Fold recognized down states into one.
	    $response = $snmpinfo->{$if}->{ifOperStatus} = 2;
	} elsif ($response != 1) {
	    # This interface is fishy, remove and forget.
	    delete $snmpinfo->{$if} if exists $snmpinfo->{$if};
	    delete $snmpinfoX->{$if} if exists $snmpinfoX->{$if};
	    return;
	}
    }
}


sub do_preprocess {
    my $mediatypes = 'ethernetCsmacd';

    if (exists( $ENV{'ifTypeOnly'} )) {
	$mediatypes = $ENV{'ifTypeOnly'};
    }

    my @mediatypes = split(/[ ,]+/,$mediatypes);

    # Hash of numerical media types the user finds interesting.
    my $mediatype={}; 

    if ($mediatypes eq 'ALL') {
	$mediatype = undef;
    } else {
	foreach my $type (@mediatypes) {
	    if (exists($ifTypes{$type})) {
		$mediatype->{$ifTypes{$type}} = 1;
	    } else {
		die "Unknown media type '$type' specified in ifTypeOnly\n";
	    }
	}
    }

    foreach my $if (keys %{$snmpinfo}) {
	do_preprocess_if($mediatype, $if);
    }
}


sub do_config_root {
    my ($host) = @_;

    print "multigraph if_bytes\n";
    print "graph_title $host interface traffic\n";
    print "graph_order recv send\n";
    print "graph_args --base 1000\n";
    print "graph_vlabel bits in (-) / out (+) per \${graph_period}\n";
    print "graph_category network\n";
    print "graph_info This graph shows the total traffic for $host\n";

    print "send.info Bits sent/received by $host\n";
    print "recv.label recv\n";
    print "recv.type DERIVE\n";
    print "recv.graph no\n";
    print "recv.cdef recv,8,*\n";
    print "recv.min 0\n";
    print "send.label bps\n";
    print "send.type DERIVE\n";
    print "send.negative recv\n";
    print "send.cdef send,8,*\n";
    print "send.min 0\n";

    print "multigraph if_errors\n";
    print "graph_title $host interface errors\n";
    print "graph_order recv send\n";
    print "graph_scale no\n";
    print "graph_args --base 1000\n";
    print "graph_vlabel errors in (-) / out (+) per \${graph_period}\n";
    print "graph_category network\n";
    print "graph_info This graph shows the total errors for $host\n";

    print "send.info Errors in outgoing/incoming traffic on $host\n";
    print "recv.label recv\n";
    print "recv.type DERIVE\n";
    print "recv.graph no\n";
    print "recv.min 0\n";
    print "send.label Errors\n";
    print "send.type DERIVE\n";
    print "send.negative recv\n";
    print "send.min 0\n";
}


sub do_config_if {
    my ($host,$version,$if) = @_;

    my $alias = $snmpinfoX->{$if}->{ifAlias} || $snmpinfo->{$if}->{ifDescr} || "Interface $if";

    if (! ($alias =~ /\d+/) ) {
	# If there are no numbers in the $alias add the if index
	$alias .=" (if $if)";
    }

    my $extrainfo = '';

    if (defined ($response = $snmpinfo->{$if}->{ifOperStatus})) {
	if ($response == 2) {
	    # Interface is down
	    $extrainfo .= ' The interface is currently down.'
	}
    }

    my $warn = undef;
    my $speed = 0;

    if (defined ($speed = $snmpinfoX->{$if}->{ifHighSpeed}) and $speed) {
      # Speed in 1,000,000 bits per second
      $speed = $speed * 1000000;
      $warn = $speed * 75 / 100;

      my $textspeed = scaleNumber($speed,'bps','',
				  'The interface speed is %.1f%s%s.');

      $extrainfo .= ' '.$textspeed if $textspeed;
    } elsif (defined ($speed = $snmpinfo->{$if}->{ifSpeed}) and $speed) {
        # Speed in bits pr. second
	$warn = $speed/100*75;

	my $textspeed = scaleNumber($speed,'bps','',
				    'The interface speed is %.1f%s%s.');

	$extrainfo .= " ".$textspeed if $textspeed;
    }

    if (defined ($snmpinfoX->{$if}->{ifHCInOctets})) {
	# If we get an answer at the 64 bit OID then this switch
	# supports the extended MIB

	$extrainfo .= " This switch supports 64 bit byte counters and these are used by this plugin.";
    } else {
	# If not we only have a 32 bit counter and are lost.
	$extrainfo .= " NOTE! This switch supports only 32 bit byte counters which makes the plugin unreliable and unsuitable for most 100Mb (or faster) interfaces, where bursts are expected to exceed 50Mbps.  This means that for interfaces where much traffic is sent this plugin will report false thruputs and cannot be trusted.";

	# unless perhaps the operator can get us snmp version 2c or 3?
	$extrainfo .= " I notice that you use SNMP version 1 which does not support 64 bit quantities.  You may get better results if you switch to SNMP version 2c or 3.  Please refer to the plugin documentation."
	  if $version == 1;
    }

    $response = $snmpinfo->{$if}->{ifType};
    $extrainfo .= " This is a '".$ifTypeByNum{$response}."' interface.";

    print "multigraph if_bytes.if_$if\n";

    print "graph_title Interface $alias traffic\n";
    print "graph_order recv send\n";
    print "graph_args --base 1000\n";
    print "graph_vlabel bits in (-) / out (+) per \${graph_period}\n";
    print "graph_category network\n";
    print "graph_info This graph shows traffic for the \"$alias\" network interface.$extrainfo\n";
    print "send.info Bits sent/received by this interface.\n";
    print "recv.label recv\n";
    print "recv.type DERIVE\n";
    print "recv.graph no\n";
    print "recv.cdef recv,8,*\n";
    print "recv.max $speed\n" if $speed;
    print "recv.min 0\n";
    print "recv.warning ", (-$warn), "\n" if defined $warn;
    print "send.label bps\n";
    print "send.type DERIVE\n";
    print "send.negative recv\n";
    print "send.cdef send,8,*\n";
    print "send.max $speed\n" if $speed;
    print "send.min 0\n";
    print "send.warning $warn\n" if defined $warn;

    print "multigraph if_errors.if_$if\n";

    print "graph_title Interface $alias errors\n";
    print "graph_order recv send\n";
    print "graph_args --base 1000\n";
    print "graph_scale no\n";
    print "graph_vlabel errors in (-) / out (+) per \${graph_period}\n";
    print "graph_category network\n";
    print "graph_info This graph shows errors for the \"$alias\" network interface.$extrainfo\n";
    print "send.info Errors in outgoing/incoming traffic on this interface.\n";
    print "recv.label recv\n";
    print "recv.type DERIVE\n";
    print "recv.graph no\n";
    print "recv.min 0\n";
    print "recv.warning 1\n";
    print "send.label errors\n";
    print "send.type DERIVE\n";
    print "send.negative recv\n";
    print "send.min 0\n";
    print "send.warning 1\n";
}


sub do_fetch_root {
    # Construct root graphs fro the if_bytes and if_error spaces

    my $in = 0;
    my $out = 0;
    my $inerr = 0;
    my $outerr = 0;

    foreach my $if (keys %{$snmpinfo}) {
	# Do not take interface up or down-ness into account, the
	# interface counters are not reset when the interface goes up
	# so we must always account for all interfaces.

	# But note that do_fetch_if has previously removed all
	# interfaces with a "fishy" status.  That is a possible bug.
	# Or a feature.  We'll see.

	$in += $snmpinfoX->{$if}->{ifHCInOctets} || $snmpinfo->{$if}->{ifInOctets} || 0;
	$out += $snmpinfoX->{$if}->{ifHCOutOctets} || $snmpinfo->{$if}->{ifOutOctets} || 0;
	$inerr += $snmpinfoX->{$if}->{ifInErrors} || $snmpinfo->{$if}->{ifInDiscards} || 0;
	$outerr += $snmpinfoX->{$if}->{ifOutErrors} || $snmpinfo->{$if}->{ifOutDiscards} || 0;

    }

    print "multigraph if_bytes\n";
    print "recv.value $in\n";
    print "send.value $out\n";

    print "multigraph if_errors\n";
    print "recv.value $inerr\n";
    print "send.value $outerr\n";
}


sub do_fetch_if {
    my($if) = @_;

    my $status = $snmpinfo->{$if}->{ifOperStatus} || 1;

    my $response;

    # 1 means up
    # 2 means set to down
    # Everything else we ignore.

    print "multigraph if_bytes.if_$if\n";

    if ($status == 2) {
	# Interface is down
	print "recv.value U\n";
	print "send.value U\n";
	print "send.extinfo This interface is currently down.\n";
	goto if_errors;
    }

    if (defined ($response = $snmpinfoX->{$if}->{ifHCInOctets} ||
		 $snmpinfo->{$if}->{ifInOctets})) {
	print "recv.value ", $response, "\n";
    } else {
	# No response...
	print "recv.value U\n";
   }

    if (defined ($response = $snmpinfoX->{$if}->{ifHCOutOctets} ||
		 $snmpinfo->{$if}->{ifOutOctets})) {
	print "send.value ", $response, "\n";
    } else {
	# No response...
	print "send.value U\n";
    }

  if_errors:

    print "multigraph if_errors.if_$if\n";

    if ($status == 2) {
	print "recv.value U\n";
	print "send.value U\n";
	print "send.extinfo This interface is down\n";
	return;
    }

    $response = ( $snmpinfo->{$if}->{ifInErrors} || 0 ) +
      ( $snmpinfo->{$if}->{ifInDiscards} || 0 );

    print "recv.value $response\n";

    $response = ( $snmpinfo->{$if}->{ifOutErrors} || 0 ) +
      ( $snmpinfo->{$if}->{ifOutDiscards} || 0 );

    print "send.value $response\n";
}


sub do_config {

    my ($host,undef,$version) = Munin::Plugin::SNMP->config_session();

    print "host_name $host\n" unless $host eq 'localhost';

    # Also do overview graph with all ports summarized
    foreach my $if (sort {$a <=> $b} keys %{$snmpinfo}) {
	do_config_if($host,$version,$if);
    }

    do_config_root($host);
}


sub do_fetch {
    # Also do overview graph with all ports summarized and collected
    # meta information in graph_info.
    # Add to this error graphs and so on.  And meta information about
    # interface type.

    foreach my $if (sort {$a <=> $b} keys %{$snmpinfo}) {
	do_fetch_if($if);
    }
    do_fetch_root();
}

# ############################## MAIN ################################

do_collect;
do_preprocess;

if ($ARGV[0] and $ARGV[0] eq "config") {
    do_config();
    exit 0;
}

do_fetch();