snmp.pm 13 KB

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