snmp.pm 14 KB

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