snmpd_ifAliasQuagga 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #! /usr/bin/perl
  2. use strict;
  3. my $baseoid = ".1.3.6.1.2.1.31.1.1.1.18";
  4. my $vtysh = "/usr/local/bin/vtysh -c 'sh interface'";
  5. my $cache_secs = 60;
  6. # ----------------------------------
  7. my $mib;
  8. my $mibtime;
  9. $| = 1;
  10. while (my $cmd = <STDIN>) {
  11. chomp $cmd;
  12. if ($cmd eq "PING") {
  13. print "PONG\n";
  14. } elsif ($cmd eq "get") {
  15. my $oid_in = <STDIN>;
  16. my $oid = get_oid($oid_in);
  17. my $mib = create_alias_mib();
  18. if ($oid != 0 && defined{$mib->{$oid}}) {
  19. print "$baseoid.$oid\n";
  20. print $mib->{$oid}[0]."\n";
  21. print $mib->{$oid}[1]."\n";
  22. } else {
  23. print "NONE\n";
  24. }
  25. } elsif ($cmd eq "getnext") {
  26. my $oid_in = <STDIN>;
  27. my $oid = get_oid($oid_in);
  28. my $found = 0;
  29. my $mib = create_alias_mib();
  30. my @s = sort { oidcmp($a, $b) } keys %{ $mib };
  31. for (my $i = 0; $i < @s; $i++) {
  32. if (oidcmp($oid, $s[$i]) == -1) {
  33. print "$baseoid.".$s[$i]."\n";
  34. print $mib->{$s[$i]}[0]."\n";
  35. print $mib->{$s[$i]}[1]."\n";
  36. $found = 1;
  37. last;
  38. }
  39. }
  40. if (!$found) {
  41. print "NONE\n";
  42. }
  43. } else {
  44. # Unknown command
  45. }
  46. }
  47. exit 0;
  48. sub get_oid
  49. {
  50. my ($oid) = @_;
  51. chomp $oid;
  52. my $base = $baseoid;
  53. $base =~ s/\./\\./g;
  54. if ($oid !~ /^$base(\.|$)/) {
  55. # Requested oid doesn't match base oid
  56. return 0;
  57. }
  58. $oid =~ s/^$base\.?//;
  59. return $oid;
  60. }
  61. sub oidcmp {
  62. my ($x, $y) = @_;
  63. my @a = split /\./, $x;
  64. my @b = split /\./, $y;
  65. my $i = 0;
  66. while (1) {
  67. if ($i > $#a) {
  68. if ($i > $#b) {
  69. return 0;
  70. } else {
  71. return -1;
  72. }
  73. } elsif ($i > $#b) {
  74. return 1;
  75. }
  76. if ($a[$i] < $b[$i]) {
  77. return -1;
  78. } elsif ($a[$i] > $b[$i]) {
  79. return 1;
  80. }
  81. $i++;
  82. }
  83. }
  84. sub create_alias_mib
  85. {
  86. # We cache the results for $cache_secs seconds
  87. if (time - $mibtime < $cache_secs) {
  88. return $mib;
  89. }
  90. my %name;
  91. my %alias;
  92. # retrieve the interface alias & number
  93. open Q, "$vtysh |";
  94. while(my $l = <Q>) {
  95. if ($l =~ /^Interface ([[:graph:]]+)[^[:graph:]]+.*/) {
  96. my $ifname = $1;
  97. my $ifdesc= "";
  98. my $ifindex= -1;
  99. $l = <Q>;
  100. if ($l =~ /^[[:space:]]*Description: (.+)/) {
  101. $ifdesc = $1;
  102. $l = <Q>;
  103. }
  104. if ($l =~ /^[[:space:]]*index[[:space:]]+(\d+) .*/) {
  105. $ifindex = $1;
  106. }
  107. if ($ifindex != -1) {
  108. $alias{$ifindex} = [ "string", $ifdesc ];
  109. }
  110. }
  111. }
  112. $mib = \%alias;
  113. $mibtime = time;
  114. return $mib;
  115. }