snmp.pm 14 KB

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