snmp.pm 16 KB

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