snmp.pm 15 KB

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