snmp.pm 15 KB

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