snmp.pm 14 KB

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