snmp.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package Rstat::snmp;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use strict;
  6. use English;
  7. use FindBin '$Bin';
  8. use lib "$Bin";
  9. use base 'Exporter';
  10. use vars qw(@EXPORT @ISA);
  11. use Data::Dumper;
  12. use Rstat::config;
  13. use Rstat::main;
  14. use Net::SNMP;
  15. @ISA = qw(Exporter);
  16. @EXPORT = qw(
  17. snmp_set_int
  18. snmp_get_request
  19. get_arp_table
  20. get_fdb_table
  21. get_mac_table
  22. get_vlan_at_port
  23. get_switch_vlans
  24. get_interfaces
  25. get_router_state
  26. get_bgp
  27. snmp_get_req
  28. snmp_get_oid
  29. snmp_walk_oid
  30. table_callback
  31. $ifAlias
  32. $ifName
  33. $ifDescr
  34. $ifIndex
  35. $bgp_prefixes
  36. $bgp_aslist
  37. $arp_oid
  38. $ipNetToMediaPhysAddress
  39. $fdb_table_oid
  40. $fdb_table_oid2
  41. $cisco_vlan_oid
  42. $port_vlan_oid
  43. $fdb_table;
  44. $snmp_timeout
  45. );
  46. BEGIN
  47. {
  48. our $ifAlias ='.1.3.6.1.2.1.31.1.1.1.18';
  49. our $ifName ='.1.3.6.1.2.1.31.1.1.1.1';
  50. our $ifDescr ='.1.3.6.1.2.1.2.2.1.2';
  51. our $ifIndex ='.1.3.6.1.2.1.2.2.1.1';
  52. our $bgp_prefixes ='.1.3.6.1.4.1.9.9.187.1.2.4.1.1';
  53. our $bgp_aslist ='.1.3.6.1.2.1.15.3.1.9';
  54. our $arp_oid ='.1.3.6.1.2.1.3.1.1.2';
  55. our $ipNetToMediaPhysAddress = '.1.3.6.1.2.1.4.22.1.2';
  56. our $fdb_table_oid ='.1.3.6.1.2.1.17.4.3.1.2';
  57. our $fdb_table_oid2='.1.3.6.1.2.1.17.7.1.2.2.1.2';
  58. our $port_vlan_oid ='.1.3.6.1.2.1.17.7.1.4.5.1.1';
  59. our $cisco_vlan_oid='.1.3.6.1.4.1.9.9.46.1.3.1.1.2';
  60. our $fdb_table;
  61. our $snmp_timeout = 15;
  62. #---------------------------------------------------------------------------------
  63. sub snmp_get_request {
  64. my $ip = shift;
  65. my $oid = shift;
  66. my $community = shift || $snmp_default_community;
  67. my $port = shift || '161';
  68. my $snmp_version = shift || '2';
  69. my ($session, $error) = Net::SNMP->session(
  70. -hostname => $ip,
  71. -community => $community,
  72. -port => $port,
  73. -version => $snmp_version,
  74. -timeout => $snmp_timeout
  75. );
  76. my $result = $session->get_request( -varbindlist => [$oid]);
  77. $session->close;
  78. return if (!$result->{$oid});
  79. return $result->{$oid};
  80. }
  81. #---------------------------------------------------------------------------------
  82. sub snmp_set_int {
  83. my $ip = shift;
  84. my $oid = shift;
  85. my $value = shift;
  86. my $community = shift || $snmp_default_community;
  87. my $port = shift || '161';
  88. my $snmp_version = shift || '2';
  89. my ($session, $error) = Net::SNMP->session(
  90. -hostname => $ip,
  91. -community => $community,
  92. -port => $port,
  93. -version => $snmp_version,
  94. -timeout => $snmp_timeout
  95. );
  96. my $result = $session->set_request( -varbindlist => [$oid,INTEGER,$value]);
  97. $session->close;
  98. return $result->{$oid};
  99. }
  100. #-------------------------------------------------------------------------------------
  101. sub get_arp_table {
  102. my ($host,$community,$version) = @_;
  103. # return if (!HostIsLive($host));
  104. my $port = 161;
  105. my $timeout = 5;
  106. if (!$version) { $version='2'; }
  107. ### open SNMP session
  108. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community , -version=>$version, -timeout => $snmp_timeout);
  109. return if (!defined($snmp_session));
  110. $snmp_session->translate([-all]);
  111. my $arp;
  112. my $arp_table1 = $snmp_session->get_table($arp_oid);
  113. my $arp_table2 = $snmp_session->get_table($ipNetToMediaPhysAddress);
  114. if ($arp_table1) {
  115. foreach my $row (keys(%$arp_table1)) {
  116. my ($mac_h) = unpack("H*",$arp_table1->{$row});
  117. next if (!$mac_h or $mac_h eq '000000000000' or $mac_h eq 'ffffffffffff');
  118. my $mac;
  119. if (length($mac_h)==12) { $mac=lc $mac_h; }
  120. next if (!$mac);
  121. $row=trim($row);
  122. my $ip;
  123. if ($row=~/\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) { $ip=$1.".".$2.".".$3.".".$4; }
  124. next if (!$ip);
  125. $arp->{$ip}=$mac;
  126. };
  127. }
  128. if ($arp_table2) {
  129. foreach my $row (keys(%$arp_table2)) {
  130. my ($mac_h) = unpack("H*",$arp_table2->{$row});
  131. next if (!$mac_h or $mac_h eq '000000000000' or $mac_h eq 'ffffffffffff');
  132. my $mac;
  133. if (length($mac_h)==12) { $mac=lc $mac_h; }
  134. next if (!$mac);
  135. $row=trim($row);
  136. my $ip;
  137. if ($row=~/\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) { $ip=$1.".".$2.".".$3.".".$4; }
  138. next if (!$ip);
  139. $arp->{$ip}=$mac;
  140. };
  141. }
  142. return $arp;
  143. }
  144. #-------------------------------------------------------------------------------------
  145. sub get_mac_table {
  146. my ($host,$community,$oid,$version) = @_;
  147. my $port = 161;
  148. my $timeout = 5;
  149. if (!$version) { $version='2'; }
  150. my $fdb;
  151. #need for callback
  152. $fdb_table=$oid;
  153. my $fdb_table1 = snmp_get_oid($host,$community,$oid,$version);
  154. if (!$fdb_table1) { $fdb_table1=snmp_walk_oid($host,$community,$oid,$version); }
  155. if ($fdb_table1) {
  156. foreach my $row (keys(%$fdb_table1)) {
  157. my $port_index = $fdb_table1->{$row};
  158. next if (!$port_index);
  159. my $mac;
  160. if ($row=~/\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) {
  161. $mac=sprintf "%02x%02x%02x%02x%02x%02x",$1,$2,$3,$4,$5,$6;
  162. }
  163. next if (!$mac);
  164. $fdb->{$mac}=$port_index;
  165. };
  166. return $fdb;
  167. }
  168. }
  169. #-------------------------------------------------------------------------------------
  170. sub get_fdb_table {
  171. my ($host,$community,$version) = @_;
  172. # return if (!HostIsLive($host));
  173. my $port = 161;
  174. my $timeout = 5;
  175. if (!$version) { $version='2'; }
  176. my $fdb=get_mac_table($host,$community,$fdb_table_oid,$version);
  177. if (!$fdb) { $fdb=get_mac_table($host,$community,$fdb_table_oid2,$version); }
  178. #maybe cisco?!
  179. if (!$fdb) {
  180. my $vlan_table=snmp_get_oid($host,$community,$cisco_vlan_oid,$version);
  181. if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$community,$cisco_vlan_oid,$version); }
  182. #fuck!
  183. if (!$vlan_table) { return; }
  184. my %fdb_vlan;
  185. foreach my $vlan_oid (keys %$vlan_table) {
  186. next if (!$vlan_oid);
  187. my $vlan_id;
  188. if ($vlan_oid=~/\.([0-9]{1,4})$/) { $vlan_id=$1; }
  189. next if (!$vlan_id);
  190. next if ($vlan_id>1000 and $vlan_id<=1009);
  191. $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid,$version);
  192. if (!$fdb_vlan{$vlan_id}) { $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid2,$version); }
  193. }
  194. foreach my $vlan_id (keys %fdb_vlan) {
  195. next if (!exists $fdb_vlan{$vlan_id});
  196. if (defined $fdb_vlan{$vlan_id}) {
  197. my %tmp=%{$fdb_vlan{$vlan_id}};
  198. foreach my $mac (keys %tmp) {
  199. next if (!$mac);
  200. $fdb->{$mac}=$tmp{$mac};
  201. }
  202. }
  203. }
  204. }
  205. return $fdb;
  206. }
  207. #-------------------------------------------------------------------------------------
  208. sub get_vlan_at_port {
  209. my ($host,$community,$version,$port_index) = @_;
  210. my $port = 161;
  211. my $timeout = 5;
  212. if (!$version) { $version='2'; }
  213. my $vlan_oid=$port_vlan_oid.".".$port_index;
  214. # print "$host,$community,$vlan_oid,$version\n";
  215. my $vlan = snmp_get_req($host,$community,$vlan_oid,$version);
  216. return "1" if (!$vlan);
  217. return "1" if ($vlan=~/noSuchObject/i);
  218. return "1" if ($vlan=~/noSuchInstance/i);
  219. return $vlan;
  220. }
  221. #-------------------------------------------------------------------------------------
  222. sub get_switch_vlans {
  223. my ($host,$community,$version) = @_;
  224. my $port = 161;
  225. my $timeout = 5;
  226. if (!$version) { $version='2'; }
  227. my $result;
  228. #need for callback
  229. my $vlan_table = snmp_get_oid($host,$community,$port_vlan_oid,$version);
  230. if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$community,$port_vlan_oid,$version); }
  231. if ($vlan_table) {
  232. foreach my $vlan_oid (keys %$vlan_table) {
  233. if ($vlan_oid=~/\.([0-9]*)$/) { $result->{$1} = $vlan_table->{$vlan_oid}; }
  234. }
  235. }
  236. return $result;
  237. }
  238. #-------------------------------------------------------------------------------------
  239. sub get_interfaces {
  240. my ($host,$community,$snmp,$skip_empty) = @_;
  241. # return if (!HostIsLive($host));
  242. my $port = 161;
  243. ### open SNMP session
  244. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
  245. return if (!defined($snmp_session));
  246. $snmp_session->translate([-timeticks]);
  247. my $if_name = $snmp_session->get_table($ifName);
  248. my $if_alias = $snmp_session->get_table($ifAlias);
  249. my $if_descr = $snmp_session->get_table($ifDescr);
  250. my $if_index = $snmp_session->get_table($ifIndex);
  251. my $dev_cap;
  252. foreach my $row (keys(%$if_index)) {
  253. my $index = $if_index->{$row};
  254. next if ($if_name->{$ifName.".".$index} =~/^lo/i);
  255. next if ($if_name->{$ifName.".".$index} =~/^dummy/i);
  256. next if ($if_name->{$ifName.".".$index} =~/^enet/i);
  257. next if ($if_name->{$ifName.".".$index} =~/^Nu/i);
  258. # next if ($if_name->{$ifName.".".$index} =~/^Po/i);
  259. my $ifc_alias;
  260. $ifc_alias=$if_alias->{$ifAlias.".".$index} if ($if_alias->{$ifAlias.".".$index});
  261. my $ifc_name;
  262. $ifc_name=$if_name->{$ifName.".".$index} if ($if_name->{$ifName.".".$index});
  263. my $ifc_desc;
  264. $ifc_desc=$if_descr->{$ifDescr.".".$index} if ($if_descr->{$ifDescr.".".$index});
  265. $dev_cap->{$index}->{alias}=$ifc_alias if ($ifc_alias);
  266. $dev_cap->{$index}->{name}=$ifc_name if ($ifc_name);
  267. $dev_cap->{$index}->{desc}=$ifc_desc if ($ifc_desc);
  268. $dev_cap->{$index}->{index} = $index;
  269. };
  270. return $dev_cap;
  271. }
  272. #-------------------------------------------------------------------------------------
  273. sub get_router_state {
  274. my ($host,$community,$snmp,$skip_empty) = @_;
  275. # return if (!HostIsLive($host));
  276. my $port = 161;
  277. ### open SNMP session
  278. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
  279. return if (!defined($snmp_session));
  280. $snmp_session->translate([-timeticks]);
  281. my $router_status = $snmp_session->get_table("1.3.6.1.4.1.10.1");
  282. return ($router_status);
  283. }
  284. #-------------------------------------------------------------------------------------
  285. sub get_bgp {
  286. my ($host,$community,$snmp) = @_;
  287. return if (!HostIsLive($host));
  288. my $port = 161;
  289. ### open SNMP session
  290. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
  291. return if (!defined($snmp_session));
  292. $snmp_session->translate([-timeticks]);
  293. #bgp annonce counter exists?
  294. my $bgp_annonces;
  295. $bgp_annonces = $snmp_session->get_table($bgp_prefixes);
  296. return if (!$bgp_annonces);
  297. my $bgp_status = $snmp_session->get_table($bgp_aslist);
  298. return if (!$bgp_status);
  299. my $as;
  300. foreach my $row (keys(%$bgp_status)) {
  301. my $as_ip=$row;
  302. $as_ip=~s/1\.3\.6\.1\.2\.1\.15\.3\.1\.9\.//;
  303. $as->{$as_ip}=$bgp_status->{$row};
  304. }
  305. return $as;
  306. }
  307. #-------------------------------------------------------------------------------------
  308. sub snmp_get_req {
  309. my ($host,$community,$oid,$version) = @_;
  310. #return if (!HostIsLive($host));
  311. if (!$version) { $version='2'; }
  312. ### open SNMP session
  313. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community , -version=>$version, -timeout => $snmp_timeout );
  314. return if (!defined($snmp_session));
  315. $snmp_session->translate([-timeticks]);
  316. my $result = $snmp_session->get_request(-varbindlist => [$oid]) or return;
  317. $snmp_session->close();
  318. return $result->{$oid};
  319. }
  320. #-------------------------------------------------------------------------------------
  321. sub snmp_get_oid {
  322. my ($host,$community,$oid,$version) = @_;
  323. #return if (!HostIsLive($host));
  324. if (!$version) { $version='2'; }
  325. ### open SNMP session
  326. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community , -version=>$version , -timeout => $snmp_timeout, );
  327. return if (!defined($snmp_session));
  328. $snmp_session->translate([-timeticks]);
  329. my $table = $snmp_session->get_table($oid);
  330. $snmp_session->close();
  331. return $table;
  332. }
  333. #-------------------------------------------------------------------------------------
  334. sub snmp_walk_oid {
  335. my $host = shift;
  336. my $community = shift;
  337. my $oid = shift;
  338. my $version = shift || '2c';
  339. #return if (!HostIsLive($host));
  340. my ($session, $error) = Net::SNMP->session(
  341. -hostname => $host,
  342. -community => $community,
  343. -nonblocking => 1,
  344. -translate => [-octetstring => 0],
  345. -version => $version,
  346. -timeout => $snmp_timeout,
  347. );
  348. if (!defined $session) {
  349. printf "ERROR: %s.\n", $error;
  350. return;
  351. }
  352. my %table; # Hash to store the results
  353. my $result = $session->get_bulk_request(
  354. -varbindlist => [ $oid ],
  355. -callback => [ \&table_callback, \%table ],
  356. -maxrepetitions => 10,
  357. );
  358. snmp_dispatcher();
  359. $session->close();
  360. return \%table;
  361. }
  362. #-------------------------------------------------------------------------------------
  363. sub table_callback {
  364. my ($session, $table) = @_;
  365. my $list = $session->var_bind_list();
  366. if (!defined $list) {
  367. printf "ERROR: %s\n", $session->error();
  368. return;
  369. }
  370. my @names = $session->var_bind_names();
  371. my $next = undef;
  372. while (@names) {
  373. $next = shift @names;
  374. if (!oid_base_match($fdb_table, $next)) { return; }
  375. $table->{$next} = $list->{$next};
  376. }
  377. my $result = $session->get_bulk_request( -varbindlist => [ $next ], -maxrepetitions => 10);
  378. if (!defined $result) {
  379. printf "ERROR: %s.\n", $session->error();
  380. }
  381. return;
  382. }
  383. #-------------------------------------------------------------------------------------
  384. 1;
  385. }