snmp.pm 15 KB

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