snmp.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package eyelib::snmp;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use strict;
  8. use English;
  9. use FindBin '$Bin';
  10. use lib "/opt/Eye/scripts";
  11. use base 'Exporter';
  12. use vars qw(@EXPORT @ISA);
  13. use Data::Dumper;
  14. use eyelib::config;
  15. use eyelib::main;
  16. use Net::SNMP;
  17. @ISA = qw(Exporter);
  18. @EXPORT = qw(
  19. snmp_set_int
  20. snmp_get_request
  21. get_arp_table
  22. get_fdb_table
  23. get_mac_table
  24. get_vlan_at_port
  25. get_switch_vlans
  26. get_snmp_ifindex
  27. get_ifmib_index_table
  28. get_interfaces
  29. get_router_state
  30. snmp_get_req
  31. snmp_get_oid
  32. snmp_walk_oid
  33. table_callback
  34. $ifAlias
  35. $ifName
  36. $ifDescr
  37. $ifIndex
  38. $ifIndex_map
  39. $arp_oid
  40. $ipNetToMediaPhysAddress
  41. $fdb_table_oid
  42. $fdb_table_oid2
  43. $cisco_vlan_oid
  44. $dot1qPortVlanEntry
  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. #RFC1213::atPhysAddress
  56. our $arp_oid ='.1.3.6.1.2.1.3.1.1.2';
  57. #RFC1213::ipNetToMediaPhysAddress
  58. our $ipNetToMediaPhysAddress = '.1.3.6.1.2.1.4.22.1.2';
  59. #RFC1493::dot1dTpFdbTable
  60. our $fdb_table_oid ='.1.3.6.1.2.1.17.4.3.1.2';
  61. #Q-BRIDGE-MIB::dot1qTpFdbPort
  62. our $fdb_table_oid2='.1.3.6.1.2.1.17.7.1.2.2.1.2';
  63. #Q-BRIDGE-MIB::dot1qPortVlanEntry
  64. our $dot1qPortVlanEntry ='.1.3.6.1.2.1.17.7.1.4.5.1.1';
  65. #CISCO-ES-STACK-MIB::
  66. our $cisco_vlan_oid='.1.3.6.1.4.1.9.9.46.1.3.1.1.2';
  67. our $fdb_table;
  68. our $snmp_timeout = 15;
  69. #---------------------------------------------------------------------------------
  70. sub snmp_get_request {
  71. my $ip = shift;
  72. my $oid = shift;
  73. my $community = shift || $snmp_default_community;
  74. my $port = shift || '161';
  75. my $snmp_version = shift || '2';
  76. my ($session, $error) = Net::SNMP->session(
  77. -hostname => $ip,
  78. -community => $community,
  79. -port => $port,
  80. -version => $snmp_version,
  81. -timeout => 5
  82. );
  83. return if (!defined($session));
  84. my $result = $session->get_request( -varbindlist => [$oid]);
  85. $session->close;
  86. return if (!$result->{$oid});
  87. return $result->{$oid};
  88. }
  89. #---------------------------------------------------------------------------------
  90. sub snmp_set_int {
  91. my $ip = shift;
  92. my $oid = shift;
  93. my $value = shift;
  94. my $community = shift || $snmp_default_community;
  95. my $port = shift || '161';
  96. my $snmp_version = shift || '2';
  97. my ($session, $error) = Net::SNMP->session(
  98. -hostname => $ip,
  99. -community => $community,
  100. -port => $port,
  101. -version => $snmp_version,
  102. -timeout => $snmp_timeout
  103. );
  104. return if (!defined($session));
  105. my $result = $session->set_request( -varbindlist => [$oid,INTEGER,$value]);
  106. $session->close;
  107. return $result->{$oid};
  108. }
  109. #-------------------------------------------------------------------------------------
  110. sub get_arp_table {
  111. my ($host,$community,$version) = @_;
  112. # return if (!HostIsLive($host));
  113. my $port = 161;
  114. my $timeout = 5;
  115. if (!$version) { $version='2'; }
  116. ### open SNMP session
  117. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community , -version=>$version, -timeout => $snmp_timeout);
  118. return if (!defined($snmp_session));
  119. $snmp_session->translate([-all]);
  120. my $arp;
  121. my $arp_table1 = $snmp_session->get_table($arp_oid);
  122. my $arp_table2 = $snmp_session->get_table($ipNetToMediaPhysAddress);
  123. if ($arp_table1) {
  124. foreach my $row (keys(%$arp_table1)) {
  125. my ($mac_h) = unpack("H*",$arp_table1->{$row});
  126. next if (!$mac_h or $mac_h eq '000000000000' or $mac_h eq 'ffffffffffff');
  127. my $mac;
  128. if (length($mac_h)==12) { $mac=lc $mac_h; }
  129. next if (!$mac);
  130. $row=trim($row);
  131. my $ip;
  132. if ($row=~/\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) { $ip=$1.".".$2.".".$3.".".$4; }
  133. next if (!$ip);
  134. $arp->{$ip}=$mac;
  135. };
  136. }
  137. if ($arp_table2) {
  138. foreach my $row (keys(%$arp_table2)) {
  139. my ($mac_h) = unpack("H*",$arp_table2->{$row});
  140. next if (!$mac_h or $mac_h eq '000000000000' or $mac_h eq 'ffffffffffff');
  141. my $mac;
  142. if (length($mac_h)==12) { $mac=lc $mac_h; }
  143. next if (!$mac);
  144. $row=trim($row);
  145. my $ip;
  146. if ($row=~/\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) { $ip=$1.".".$2.".".$3.".".$4; }
  147. next if (!$ip);
  148. $arp->{$ip}=$mac;
  149. };
  150. }
  151. return $arp;
  152. }
  153. #-------------------------------------------------------------------------------------
  154. sub get_ifmib_index_table {
  155. my $ip = shift;
  156. my $community = shift;
  157. my $version = shift;
  158. my $ifmib_map;
  159. my $is_mikrotik = snmp_get_request($ip, '.1.3.6.1.2.1.9999.1.1.1.1.0', $community, 161, $version);
  160. my $mk_ros_version = 0;
  161. if ($is_mikrotik=~/MikroTik/i) {
  162. my $mikrotik_version = snmp_get_request($ip, '.1.0.8802.1.1.2.1.3.4.0', $community, 161, $version);
  163. $mk_ros_version = 6491;
  164. #"MikroTik RouterOS 6.46.8 (long-term) CRS326-24S+2Q+"
  165. if ($mikrotik_version =~/RouterOS\s+(\d)\.(\d{1,3})\.(\d{1,3})\s+/) {
  166. $mk_ros_version = $1*1000 + $2*10 + $3;
  167. }
  168. }
  169. if (!$mk_ros_version or $mk_ros_version > 6468) {
  170. my $index_map_table = snmp_get_oid($ip, $community, $ifIndex_map, $version);
  171. if (!$index_map_table) { $index_map_table = snmp_walk_oid($ip, $community, $ifIndex_map, $version); }
  172. if ($index_map_table) {
  173. foreach my $row (keys(%$index_map_table)) {
  174. my $port_index = $index_map_table->{$row};
  175. next if (!$port_index);
  176. my $value;
  177. if ($row=~/\.([0-9]{1,10})$/) { $value = $1; }
  178. next if (!$value);
  179. $ifmib_map->{$value}=$port_index;
  180. }
  181. }
  182. }
  183. if (!$ifmib_map) {
  184. my $index_table = snmp_get_oid($ip, $community, $ifIndex, $version);
  185. if (!$index_table) { $index_table = snmp_walk_oid($ip, $community, $ifIndex, $version); }
  186. foreach my $row (keys(%$index_table)) {
  187. my $port_index = $index_table->{$row};
  188. next if (!$port_index);
  189. my $value;
  190. if ($row=~/\.([0-9]{1,10})$/) { $value = $1; }
  191. next if (!$value);
  192. $ifmib_map->{$value}=$value;
  193. };
  194. }
  195. return $ifmib_map;
  196. }
  197. #-------------------------------------------------------------------------------------
  198. sub get_mac_table {
  199. my ($host,$community,$oid,$version,$index_map) = @_;
  200. my $port = 161;
  201. my $timeout = 5;
  202. if (!$version) { $version='2'; }
  203. my $fdb;
  204. #need for callback
  205. $fdb_table=$oid;
  206. my $fdb_table1 = snmp_get_oid($host,$community,$oid,$version);
  207. if (!$fdb_table1) { $fdb_table1=snmp_walk_oid($host,$community,$oid,$version,undef); }
  208. if ($fdb_table1) {
  209. foreach my $row (keys(%$fdb_table1)) {
  210. my $port_index = $fdb_table1->{$row};
  211. next if (!$port_index);
  212. my $mac;
  213. 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})$/) {
  214. $mac=sprintf "%02x%02x%02x%02x%02x%02x",$1,$2,$3,$4,$5,$6;
  215. }
  216. next if (!$mac);
  217. if ($index_map and exists $index_map->{$port_index}) { $port_index = $index_map->{$port_index}; }
  218. $fdb->{$mac}=$port_index;
  219. };
  220. return $fdb;
  221. }
  222. }
  223. #-------------------------------------------------------------------------------------
  224. sub get_fdb_table {
  225. my ($host,$community,$version,$iflist) = @_;
  226. # return if (!HostIsLive($host));
  227. my $port = 161;
  228. my $timeout = 5;
  229. if (!$version) { $version='2'; }
  230. my $ifindex_map = get_ifmib_index_table($host,$community,$version);
  231. my $fdb1=get_mac_table($host,$community,$fdb_table_oid,$version,$ifindex_map);
  232. my $fdb2=get_mac_table($host,$community,$fdb_table_oid2,$version,$ifindex_map);
  233. my $fdb3;
  234. if ($fdb2 and $iflist) {
  235. foreach my $mac (keys %$fdb2) {
  236. if (exists $iflist->{$fdb2->{$mac}}) { $fdb3->{$mac}=$iflist->{$fdb2->{$mac}}; }
  237. }
  238. }
  239. my $fdb;
  240. if ($fdb1 and !$fdb3) { $fdb = $fdb1; }
  241. if (!$fdb1 and $fdb3) { $fdb = $fdb3; }
  242. #join tables
  243. if ($fdb1 and $fdb3) { $fdb = { %$fdb1,%$fdb3 }; }
  244. #maybe cisco?!
  245. if (!$fdb) {
  246. my $vlan_table=snmp_get_oid($host,$community,$cisco_vlan_oid,$version);
  247. if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$community,$cisco_vlan_oid,$version); }
  248. #fuck!
  249. if (!$vlan_table) { return; }
  250. my %fdb_vlan;
  251. foreach my $vlan_oid (keys %$vlan_table) {
  252. next if (!$vlan_oid);
  253. my $vlan_id;
  254. if ($vlan_oid=~/\.([0-9]{1,4})$/) { $vlan_id=$1; }
  255. next if (!$vlan_id);
  256. next if ($vlan_id>1000 and $vlan_id<=1009);
  257. $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid,$version,$ifindex_map);
  258. if (!$fdb_vlan{$vlan_id}) { $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid2,$version,$ifindex_map); }
  259. }
  260. foreach my $vlan_id (keys %fdb_vlan) {
  261. next if (!exists $fdb_vlan{$vlan_id});
  262. if (defined $fdb_vlan{$vlan_id}) {
  263. my %tmp=%{$fdb_vlan{$vlan_id}};
  264. foreach my $mac (keys %tmp) {
  265. next if (!$mac);
  266. $fdb->{$mac}=$tmp{$mac};
  267. }
  268. }
  269. }
  270. }
  271. return $fdb;
  272. }
  273. #-------------------------------------------------------------------------------------
  274. sub get_vlan_at_port {
  275. my ($host,$community,$version,$port_index) = @_;
  276. my $port = 161;
  277. my $timeout = 5;
  278. if (!$version) { $version='2'; }
  279. my $vlan_oid=$dot1qPortVlanEntry.".".$port_index;
  280. # print "$host,$community,$vlan_oid,$version\n";
  281. my $vlan = snmp_get_req($host,$community,$vlan_oid,$version);
  282. return "1" if (!$vlan);
  283. return "1" if ($vlan=~/noSuchObject/i);
  284. return "1" if ($vlan=~/noSuchInstance/i);
  285. return $vlan;
  286. }
  287. #-------------------------------------------------------------------------------------
  288. sub get_switch_vlans {
  289. my ($host,$community,$version) = @_;
  290. my $port = 161;
  291. my $timeout = 5;
  292. if (!$version) { $version='2'; }
  293. my $result;
  294. #need for callback
  295. my $vlan_table = snmp_get_oid($host,$community,$dot1qPortVlanEntry,$version);
  296. if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$community,$dot1qPortVlanEntry,$version); }
  297. if ($vlan_table) {
  298. foreach my $vlan_oid (keys %$vlan_table) {
  299. if ($vlan_oid=~/\.([0-9]*)$/) { $result->{$1} = $vlan_table->{$vlan_oid}; }
  300. }
  301. }
  302. return $result;
  303. }
  304. #-------------------------------------------------------------------------------------
  305. sub get_snmp_ifindex {
  306. my ($host,$community,$snmp) = @_;
  307. ### open SNMP session
  308. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => 5);
  309. return if (!defined($snmp_session));
  310. my $if_index = $snmp_session->get_table($ifIndex);
  311. my $result;
  312. foreach my $row (keys(%$if_index)) {
  313. my $value = $if_index->{$row};
  314. $row=~s/^$ifIndex\.//;
  315. $result->{$row}=$value;
  316. };
  317. return $result;
  318. }
  319. #-------------------------------------------------------------------------------------
  320. sub get_interfaces {
  321. my ($host,$community,$snmp,$skip_empty) = @_;
  322. # return if (!HostIsLive($host));
  323. my $port = 161;
  324. ### open SNMP session
  325. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
  326. return if (!defined($snmp_session));
  327. $snmp_session->translate([-timeticks]);
  328. my $if_name = $snmp_session->get_table($ifName);
  329. my $if_alias = $snmp_session->get_table($ifAlias);
  330. my $if_descr = $snmp_session->get_table($ifDescr);
  331. my $if_index = $snmp_session->get_table($ifIndex);
  332. my $dev_cap;
  333. foreach my $row (keys(%$if_index)) {
  334. my $index = $if_index->{$row};
  335. next if ($if_name->{$ifName.".".$index} =~/^lo/i);
  336. next if ($if_name->{$ifName.".".$index} =~/^dummy/i);
  337. next if ($if_name->{$ifName.".".$index} =~/^enet/i);
  338. next if ($if_name->{$ifName.".".$index} =~/^Nu/i);
  339. # next if ($if_name->{$ifName.".".$index} =~/^Po/i);
  340. my $ifc_alias;
  341. $ifc_alias=$if_alias->{$ifAlias.".".$index} if ($if_alias->{$ifAlias.".".$index});
  342. my $ifc_name;
  343. $ifc_name=$if_name->{$ifName.".".$index} if ($if_name->{$ifName.".".$index});
  344. my $ifc_desc;
  345. $ifc_desc=$if_descr->{$ifDescr.".".$index} if ($if_descr->{$ifDescr.".".$index});
  346. $dev_cap->{$index}->{alias}=$ifc_alias if ($ifc_alias);
  347. $dev_cap->{$index}->{name}=$ifc_name if ($ifc_name);
  348. $dev_cap->{$index}->{desc}=$ifc_desc if ($ifc_desc);
  349. $dev_cap->{$index}->{index} = $index;
  350. };
  351. return $dev_cap;
  352. }
  353. #-------------------------------------------------------------------------------------
  354. sub get_router_state {
  355. my ($host,$community,$snmp,$skip_empty) = @_;
  356. # return if (!HostIsLive($host));
  357. my $port = 161;
  358. ### open SNMP session
  359. my ($snmp_session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
  360. return if (!defined($snmp_session));
  361. $snmp_session->translate([-timeticks]);
  362. my $router_status = $snmp_session->get_table("1.3.6.1.4.1.10.1");
  363. return ($router_status);
  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. }