snmp.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. my $is_mikrotik = snmp_get_oid($ip, $community, '.1.3.6.1.2.1.9999.1.1.1.1.0', $version);
  184. if (%$index_table and $is_mikrotik) {
  185. if ($is_mikrotik) {
  186. foreach my $row (keys(%$index_table)) {
  187. my $port_index = $index_table->{$row};
  188. next if (!$port_index);
  189. my $value;
  190. if ($row=~/\.([0-9]{1,10})$/) { $value = $1; }
  191. next if (!$value);
  192. $ifmib_map->{$value}=$port_index;
  193. }
  194. }
  195. } else {
  196. $index_table = snmp_get_oid($ip, $community, $ifIndex, $version);
  197. if (!%$index_table) { $index_table = snmp_walk_oid($ip, $community, $ifIndex, $version); }
  198. if (%$index_table) {
  199. foreach my $row (keys(%$index_table)) {
  200. my $port_index = $index_table->{$row};
  201. next if (!$port_index);
  202. $ifmib_map->{$port_index}=$port_index;
  203. };
  204. }
  205. }
  206. return $ifmib_map;
  207. }
  208. #-------------------------------------------------------------------------------------
  209. sub get_fdb_table {
  210. my ($host,$community,$version) = @_;
  211. # return if (!HostIsLive($host));
  212. my $port = 161;
  213. my $timeout = 5;
  214. if (!$version) { $version='2'; }
  215. my $ifindex_map = get_ifmib_index_table($host,$community,$version);
  216. my $fdb1=get_mac_table($host,$community,$fdb_table_oid,$version,$ifindex_map);
  217. my $fdb2=get_mac_table($host,$community,$fdb_table_oid2,$version,$ifindex_map);
  218. my $fdb;
  219. if ($fdb1) { $fdb = $fdb1; }
  220. if ($fdb2) {
  221. if (!$fdb) { $fdb = $fdb2; }
  222. else {
  223. foreach my $mac (keys %$fdb2) {
  224. if (!exists($fdb->{$mac})) { $fdb->{$mac}=$fdb2->{$mac}; }
  225. }
  226. }
  227. }
  228. #maybe cisco?!
  229. if (!$fdb) {
  230. my $vlan_table=snmp_get_oid($host,$community,$cisco_vlan_oid,$version);
  231. if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$community,$cisco_vlan_oid,$version); }
  232. #fuck!
  233. if (!$vlan_table) { return; }
  234. my %fdb_vlan;
  235. foreach my $vlan_oid (keys %$vlan_table) {
  236. next if (!$vlan_oid);
  237. my $vlan_id;
  238. if ($vlan_oid=~/\.([0-9]{1,4})$/) { $vlan_id=$1; }
  239. next if (!$vlan_id);
  240. next if ($vlan_id>1000 and $vlan_id<=1009);
  241. $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid,$version,$ifindex_map);
  242. if (!$fdb_vlan{$vlan_id}) { $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid2,$version,$ifindex_map); }
  243. }
  244. foreach my $vlan_id (keys %fdb_vlan) {
  245. next if (!exists $fdb_vlan{$vlan_id});
  246. if (defined $fdb_vlan{$vlan_id}) {
  247. my %tmp=%{$fdb_vlan{$vlan_id}};
  248. foreach my $mac (keys %tmp) {
  249. next if (!$mac);
  250. $fdb->{$mac}=$tmp{$mac};
  251. }
  252. }
  253. }
  254. }
  255. return $fdb;
  256. }
  257. #-------------------------------------------------------------------------------------
  258. sub get_vlan_at_port {
  259. my ($host,$community,$version,$port_index) = @_;
  260. my $port = 161;
  261. my $timeout = 5;
  262. if (!$version) { $version='2'; }
  263. my $vlan_oid=$port_vlan_oid.".".$port_index;
  264. # print "$host,$community,$vlan_oid,$version\n";
  265. my $vlan = snmp_get_req($host,$community,$vlan_oid,$version);
  266. return "1" if (!$vlan);
  267. return "1" if ($vlan=~/noSuchObject/i);
  268. return "1" if ($vlan=~/noSuchInstance/i);
  269. return $vlan;
  270. }
  271. #-------------------------------------------------------------------------------------
  272. sub get_switch_vlans {
  273. my ($host,$community,$version) = @_;
  274. my $port = 161;
  275. my $timeout = 5;
  276. if (!$version) { $version='2'; }
  277. my $result;
  278. #need for callback
  279. my $vlan_table = snmp_get_oid($host,$community,$port_vlan_oid,$version);
  280. if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$community,$port_vlan_oid,$version); }
  281. if ($vlan_table) {
  282. foreach my $vlan_oid (keys %$vlan_table) {
  283. if ($vlan_oid=~/\.([0-9]*)$/) { $result->{$1} = $vlan_table->{$vlan_oid}; }
  284. }
  285. }
  286. return $result;
  287. }
  288. #-------------------------------------------------------------------------------------
  289. sub get_snmp_ifindex {
  290. my ($host,$community,$snmp) = @_;
  291. ### open SNMP session
  292. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => 5);
  293. return if (!defined($snmp_session));
  294. my $if_index = $snmp_session->get_table($ifIndex);
  295. my $result;
  296. foreach my $row (keys(%$if_index)) {
  297. my $value = $if_index->{$row};
  298. $row=~s/^$ifIndex\.//;
  299. $result->{$row}=$value;
  300. };
  301. return $result;
  302. }
  303. #-------------------------------------------------------------------------------------
  304. sub get_interfaces {
  305. my ($host,$community,$snmp,$skip_empty) = @_;
  306. # return if (!HostIsLive($host));
  307. my $port = 161;
  308. ### open SNMP session
  309. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
  310. return if (!defined($snmp_session));
  311. $snmp_session->translate([-timeticks]);
  312. my $if_name = $snmp_session->get_table($ifName);
  313. my $if_alias = $snmp_session->get_table($ifAlias);
  314. my $if_descr = $snmp_session->get_table($ifDescr);
  315. my $if_index = $snmp_session->get_table($ifIndex);
  316. my $dev_cap;
  317. foreach my $row (keys(%$if_index)) {
  318. my $index = $if_index->{$row};
  319. next if ($if_name->{$ifName.".".$index} =~/^lo/i);
  320. next if ($if_name->{$ifName.".".$index} =~/^dummy/i);
  321. next if ($if_name->{$ifName.".".$index} =~/^enet/i);
  322. next if ($if_name->{$ifName.".".$index} =~/^Nu/i);
  323. # next if ($if_name->{$ifName.".".$index} =~/^Po/i);
  324. my $ifc_alias;
  325. $ifc_alias=$if_alias->{$ifAlias.".".$index} if ($if_alias->{$ifAlias.".".$index});
  326. my $ifc_name;
  327. $ifc_name=$if_name->{$ifName.".".$index} if ($if_name->{$ifName.".".$index});
  328. my $ifc_desc;
  329. $ifc_desc=$if_descr->{$ifDescr.".".$index} if ($if_descr->{$ifDescr.".".$index});
  330. $dev_cap->{$index}->{alias}=$ifc_alias if ($ifc_alias);
  331. $dev_cap->{$index}->{name}=$ifc_name if ($ifc_name);
  332. $dev_cap->{$index}->{desc}=$ifc_desc if ($ifc_desc);
  333. $dev_cap->{$index}->{index} = $index;
  334. };
  335. return $dev_cap;
  336. }
  337. #-------------------------------------------------------------------------------------
  338. sub get_router_state {
  339. my ($host,$community,$snmp,$skip_empty) = @_;
  340. # return if (!HostIsLive($host));
  341. my $port = 161;
  342. ### open SNMP session
  343. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
  344. return if (!defined($snmp_session));
  345. $snmp_session->translate([-timeticks]);
  346. my $router_status = $snmp_session->get_table("1.3.6.1.4.1.10.1");
  347. return ($router_status);
  348. }
  349. #-------------------------------------------------------------------------------------
  350. sub get_bgp {
  351. my ($host,$community,$snmp) = @_;
  352. return if (!HostIsLive($host));
  353. my $port = 161;
  354. ### open SNMP session
  355. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
  356. return if (!defined($snmp_session));
  357. $snmp_session->translate([-timeticks]);
  358. #bgp annonce counter exists?
  359. my $bgp_annonces;
  360. $bgp_annonces = $snmp_session->get_table($bgp_prefixes);
  361. return if (!$bgp_annonces);
  362. my $bgp_status = $snmp_session->get_table($bgp_aslist);
  363. return if (!$bgp_status);
  364. my $as;
  365. foreach my $row (keys(%$bgp_status)) {
  366. my $as_ip=$row;
  367. $as_ip=~s/1\.3\.6\.1\.2\.1\.15\.3\.1\.9\.//;
  368. $as->{$as_ip}=$bgp_status->{$row};
  369. }
  370. return $as;
  371. }
  372. #-------------------------------------------------------------------------------------
  373. sub snmp_get_req {
  374. my ($host,$community,$oid,$version) = @_;
  375. #return if (!HostIsLive($host));
  376. if (!$version) { $version='2'; }
  377. ### open SNMP session
  378. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community , -version=>$version, -timeout => $snmp_timeout );
  379. return if (!defined($snmp_session));
  380. $snmp_session->translate([-timeticks]);
  381. my $result = $snmp_session->get_request(-varbindlist => [$oid]) or return;
  382. $snmp_session->close();
  383. return $result->{$oid};
  384. }
  385. #-------------------------------------------------------------------------------------
  386. sub snmp_get_oid {
  387. my ($host,$community,$oid,$version) = @_;
  388. #return if (!HostIsLive($host));
  389. if (!$version) { $version='2'; }
  390. ### open SNMP session
  391. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community , -version=>$version , -timeout => $snmp_timeout, );
  392. return if (!defined($snmp_session));
  393. $snmp_session->translate([-timeticks]);
  394. my $table = $snmp_session->get_table($oid);
  395. $snmp_session->close();
  396. return $table;
  397. }
  398. #-------------------------------------------------------------------------------------
  399. sub snmp_walk_oid {
  400. my $host = shift;
  401. my $community = shift;
  402. my $oid = shift;
  403. my $version = shift || '2c';
  404. #return if (!HostIsLive($host));
  405. my ($session, $error) = Net::SNMP->session(
  406. -hostname => $host,
  407. -community => $community,
  408. -nonblocking => 1,
  409. -translate => [-octetstring => 0],
  410. -version => $version,
  411. -timeout => $snmp_timeout,
  412. );
  413. if (!defined $session) {
  414. printf "ERROR: %s.\n", $error;
  415. return;
  416. }
  417. my %table; # Hash to store the results
  418. my $result = $session->get_bulk_request(
  419. -varbindlist => [ $oid ],
  420. -callback => [ \&table_callback, \%table ],
  421. -maxrepetitions => 10,
  422. );
  423. snmp_dispatcher();
  424. $session->close();
  425. return \%table;
  426. }
  427. #-------------------------------------------------------------------------------------
  428. sub table_callback {
  429. my ($session, $table) = @_;
  430. my $list = $session->var_bind_list();
  431. if (!defined $list) {
  432. printf "ERROR: %s\n", $session->error();
  433. return;
  434. }
  435. my @names = $session->var_bind_names();
  436. my $next = undef;
  437. while (@names) {
  438. $next = shift @names;
  439. if (!oid_base_match($fdb_table, $next)) { return; }
  440. $table->{$next} = $list->{$next};
  441. }
  442. my $result = $session->get_bulk_request( -varbindlist => [ $next ], -maxrepetitions => 10);
  443. if (!defined $result) {
  444. printf "ERROR: %s.\n", $session->error();
  445. }
  446. return;
  447. }
  448. #-------------------------------------------------------------------------------------
  449. 1;
  450. }