snmp.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. package eyelib::snmp;
  2. #
  3. # Copyright (C) Roman Dmitriev, 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. getIpAdEntIfIndex
  29. get_interfaces
  30. get_router_state
  31. snmp_get_req
  32. snmp_get_oid
  33. snmp_walk_oid
  34. table_callback
  35. $ifAlias
  36. $ifName
  37. $ifDescr
  38. $ifIndex
  39. $ifIndex_map
  40. $arp_oid
  41. $ipNetToMediaPhysAddress
  42. $ipAdEntIfIndex
  43. $fdb_table_oid
  44. $fdb_table_oid2
  45. $cisco_vlan_oid
  46. $dot1qPortVlanEntry
  47. $fdb_table;
  48. $snmp_timeout
  49. setCommunity
  50. init_snmp
  51. );
  52. BEGIN
  53. {
  54. our $ifAlias ='.1.3.6.1.2.1.31.1.1.1.18';
  55. our $ifName ='.1.3.6.1.2.1.31.1.1.1.1';
  56. our $ifDescr ='.1.3.6.1.2.1.2.2.1.2';
  57. our $ifIndex ='.1.3.6.1.2.1.2.2.1.1';
  58. our $ifIndex_map ='.1.3.6.1.2.1.17.1.4.1.2';
  59. our $ipAdEntIfIndex ='.1.3.6.1.2.1.4.20.1.2';
  60. #RFC1213::atPhysAddress
  61. our $arp_oid ='.1.3.6.1.2.1.3.1.1.2';
  62. #RFC1213::ipNetToMediaPhysAddress
  63. our $ipNetToMediaPhysAddress = '.1.3.6.1.2.1.4.22.1.2';
  64. #RFC1493::dot1dTpFdbTable
  65. our $fdb_table_oid ='.1.3.6.1.2.1.17.4.3.1.2';
  66. #Q-BRIDGE-MIB::dot1qTpFdbPort
  67. our $fdb_table_oid2='.1.3.6.1.2.1.17.7.1.2.2.1.2';
  68. #Q-BRIDGE-MIB::dot1qPortVlanEntry
  69. our $dot1qPortVlanEntry ='.1.3.6.1.2.1.17.7.1.4.5.1.1';
  70. #CISCO-ES-STACK-MIB::
  71. our $cisco_vlan_oid='.1.3.6.1.4.1.9.9.46.1.3.1.1.2';
  72. our $fdb_table;
  73. our $snmp_timeout = 15;
  74. #---------------------------------------------------------------------------------
  75. sub snmp_get_request {
  76. my $ip = shift;
  77. my $oid = shift;
  78. my $snmp = shift;
  79. my $session = init_snmp ($ip,$snmp);
  80. return if (!defined($session) or !$session);
  81. my $result = $session->get_request( -varbindlist => [$oid]);
  82. $session->close;
  83. return if (!$result->{$oid});
  84. return $result->{$oid};
  85. }
  86. #---------------------------------------------------------------------------------
  87. sub snmp_set_int {
  88. my $ip = shift;
  89. my $oid = shift;
  90. my $value = shift;
  91. my $snmp = shift;
  92. my $session = init_snmp ($ip,$snmp,1);
  93. return if (!defined($session) or !$session);
  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,$snmp) = @_;
  101. my $session = init_snmp ($host,$snmp,0);
  102. return if (!defined($session) or !$session);
  103. $session->translate([-all]);
  104. my $arp;
  105. my $arp_table1 = $session->get_table($arp_oid);
  106. my $arp_table2 = $session->get_table($ipNetToMediaPhysAddress);
  107. $session->close();
  108. if ($arp_table1) {
  109. foreach my $row (keys(%$arp_table1)) {
  110. my ($mac_h) = unpack("H*",$arp_table1->{$row});
  111. next if (!$mac_h or $mac_h eq '000000000000' or $mac_h eq 'ffffffffffff');
  112. my $mac;
  113. if (length($mac_h)==12) { $mac=lc $mac_h; }
  114. next if (!$mac);
  115. $row=trim($row);
  116. my $ip;
  117. if ($row=~/\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) { $ip=$1.".".$2.".".$3.".".$4; }
  118. next if (!$ip);
  119. $arp->{$ip}=$mac;
  120. };
  121. }
  122. if ($arp_table2) {
  123. foreach my $row (keys(%$arp_table2)) {
  124. my ($mac_h) = unpack("H*",$arp_table2->{$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. return $arp;
  137. }
  138. #-------------------------------------------------------------------------------------
  139. sub get_ifmib_index_table {
  140. my $ip = shift;
  141. my $snmp = shift;
  142. my $ifmib_map;
  143. my $is_mikrotik = snmp_get_request($ip, '.1.3.6.1.2.1.9999.1.1.1.1.0', $snmp);
  144. my $mk_ros_version = 0;
  145. if ($is_mikrotik=~/MikroTik/i) {
  146. my $mikrotik_version = snmp_get_request($ip, '.1.0.8802.1.1.2.1.3.4.0', $snmp);
  147. $mk_ros_version = 6491;
  148. #"MikroTik RouterOS 6.46.8 (long-term) CRS326-24S+2Q+"
  149. if ($mikrotik_version =~/RouterOS\s+(\d)\.(\d{1,3})\.(\d{1,3})\s+/) {
  150. $mk_ros_version = $1*1000 + $2*10 + $3;
  151. }
  152. }
  153. if (!$mk_ros_version or $mk_ros_version > 6468) {
  154. my $index_map_table = snmp_get_oid($ip, $snmp, $ifIndex_map);
  155. if (!$index_map_table) { $index_map_table = snmp_walk_oid($ip, $snmp, $ifIndex_map); }
  156. if ($index_map_table) {
  157. foreach my $row (keys(%$index_map_table)) {
  158. my $port_index = $index_map_table->{$row};
  159. next if (!$port_index);
  160. my $value;
  161. if ($row=~/\.([0-9]{1,10})$/) { $value = $1; }
  162. next if (!$value);
  163. $ifmib_map->{$value}=$port_index;
  164. }
  165. }
  166. }
  167. if (!$ifmib_map) {
  168. my $index_table = snmp_get_oid($ip, $snmp, $ifIndex);
  169. if (!$index_table) { $index_table = snmp_walk_oid($ip, $snmp, $ifIndex); }
  170. foreach my $row (keys(%$index_table)) {
  171. my $port_index = $index_table->{$row};
  172. next if (!$port_index);
  173. my $value;
  174. if ($row=~/\.([0-9]{1,10})$/) { $value = $1; }
  175. next if (!$value);
  176. $ifmib_map->{$value}=$value;
  177. };
  178. }
  179. return $ifmib_map;
  180. }
  181. #-------------------------------------------------------------------------------------
  182. sub get_mac_table {
  183. my ($host,$snmp,$oid,$index_map) = @_;
  184. my $fdb;
  185. #need for callback
  186. $fdb_table=$oid;
  187. my $fdb_table1 = snmp_get_oid($host,$snmp,$oid);
  188. if (!$fdb_table1) { $fdb_table1=snmp_walk_oid($host,$snmp,$oid,undef); }
  189. if ($fdb_table1) {
  190. foreach my $row (keys(%$fdb_table1)) {
  191. my $port_index = $fdb_table1->{$row};
  192. next if (!$port_index);
  193. my $mac;
  194. 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})$/) {
  195. $mac=sprintf "%02x%02x%02x%02x%02x%02x",$1,$2,$3,$4,$5,$6;
  196. }
  197. next if (!$mac);
  198. if ($index_map and exists $index_map->{$port_index}) { $port_index = $index_map->{$port_index}; }
  199. $fdb->{$mac}=$port_index;
  200. };
  201. return $fdb;
  202. }
  203. }
  204. #-------------------------------------------------------------------------------------
  205. sub get_fdb_table {
  206. my ($host,$snmp) = @_;
  207. my $ifindex_map = get_ifmib_index_table($host,$snmp);
  208. # print "IFINDEX_MAP: " . Dumper($ifindex_map);
  209. my $fdb1=get_mac_table($host,$snmp,$fdb_table_oid,$ifindex_map);
  210. # print "FDB1: " . Dumper($fdb1);
  211. my $fdb2=get_mac_table($host,$snmp,$fdb_table_oid2,$ifindex_map);
  212. # print "FDB2: " . Dumper($fdb1);
  213. my $fdb;
  214. #join tables
  215. if (!$fdb1 and $fdb2) { $fdb = $fdb2; }
  216. if (!$fdb2 and $fdb1) { $fdb = $fdb1; }
  217. if ($fdb1 and $fdb2) { $fdb = { %$fdb1,%$fdb2 }; }
  218. my $snmp_cisco = $snmp;
  219. #maybe cisco?!
  220. if (!$fdb) {
  221. my $vlan_table=snmp_get_oid($host,$snmp,$cisco_vlan_oid);
  222. if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$snmp,$cisco_vlan_oid); }
  223. # just empty
  224. if (!$vlan_table) { return; }
  225. #fucking cisco!
  226. my %fdb_vlan;
  227. foreach my $vlan_oid (keys %$vlan_table) {
  228. next if (!$vlan_oid);
  229. my $vlan_id;
  230. if ($vlan_oid=~/\.([0-9]{1,4})$/) { $vlan_id=$1; }
  231. next if (!$vlan_id);
  232. next if ($vlan_id>1000 and $vlan_id<=1009);
  233. $snmp_cisco->{'ro-community'} = $snmp->{'ro-community'}.'@'.$vlan_id;
  234. $fdb_vlan{$vlan_id}=get_mac_table($host,$snmp_cisco,$fdb_table_oid,$ifindex_map);
  235. if (!$fdb_vlan{$vlan_id}) { $fdb_vlan{$vlan_id}=get_mac_table($host,$snmp_cisco,$fdb_table_oid2,$ifindex_map); }
  236. }
  237. foreach my $vlan_id (keys %fdb_vlan) {
  238. next if (!exists $fdb_vlan{$vlan_id});
  239. if (defined $fdb_vlan{$vlan_id}) {
  240. my %tmp=%{$fdb_vlan{$vlan_id}};
  241. foreach my $mac (keys %tmp) {
  242. next if (!$mac);
  243. $fdb->{$mac}=$tmp{$mac};
  244. }
  245. }
  246. }
  247. }
  248. return $fdb;
  249. }
  250. #-------------------------------------------------------------------------------------
  251. sub get_vlan_at_port {
  252. my ($host,$snmp,$port_index) = @_;
  253. my $vlan_oid=$dot1qPortVlanEntry.".".$port_index;
  254. my $vlan = snmp_get_req($host,$snmp,$vlan_oid);
  255. return "1" if (!$vlan);
  256. return "1" if ($vlan=~/noSuchObject/i);
  257. return "1" if ($vlan=~/noSuchInstance/i);
  258. return $vlan;
  259. }
  260. #-------------------------------------------------------------------------------------
  261. sub get_switch_vlans {
  262. my ($host,$snmp) = @_;
  263. my $result;
  264. #need for callback
  265. my $vlan_table = snmp_get_oid($host,$snmp,$dot1qPortVlanEntry);
  266. if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$snmp,$dot1qPortVlanEntry); }
  267. if ($vlan_table) {
  268. foreach my $vlan_oid (keys %$vlan_table) {
  269. if ($vlan_oid=~/\.([0-9]*)$/) { $result->{$1} = $vlan_table->{$vlan_oid}; }
  270. }
  271. }
  272. return $result;
  273. }
  274. #-------------------------------------------------------------------------------------
  275. sub get_snmp_ifindex {
  276. my ($host,$snmp) = @_;
  277. my $session = init_snmp($host,$snmp,0);
  278. return if (!defined($session) or !$session);
  279. my $if_index = $session->get_table($ifIndex);
  280. my $result;
  281. foreach my $row (keys(%$if_index)) {
  282. my $value = $if_index->{$row};
  283. $row=~s/^$ifIndex\.//;
  284. $result->{$row}=$value;
  285. };
  286. $session->close();
  287. return $result;
  288. }
  289. #-------------------------------------------------------------------------------------
  290. #get ip interfaces
  291. sub getIpAdEntIfIndex {
  292. my ($host,$snmp) = @_;
  293. my $session = init_snmp ($host,$snmp,0);
  294. return if (!defined($session) or !$session);
  295. $session->translate([-timeticks]);
  296. my $if_ipaddr = $session->get_table($ipAdEntIfIndex);
  297. my $l3_list;
  298. foreach my $row (keys(%$if_ipaddr)) {
  299. my $ipaddr = $row;
  300. $ipaddr=~s/$ipAdEntIfIndex\.//;
  301. $l3_list->{$ipaddr}=$if_ipaddr->{$row};
  302. }
  303. $session->close();
  304. return $l3_list;
  305. }
  306. #-------------------------------------------------------------------------------------
  307. sub get_interfaces {
  308. my ($host,$snmp,$skip_empty) = @_;
  309. my $session = init_snmp ($host,$snmp,0);
  310. return if (!defined($session) or !$session);
  311. $session->translate([-timeticks]);
  312. my $if_name = $session->get_table($ifName);
  313. my $if_alias = $session->get_table($ifAlias);
  314. my $if_descr = $session->get_table($ifDescr);
  315. my $if_index = $session->get_table($ifIndex);
  316. $session->close();
  317. my $dev_cap;
  318. foreach my $row (keys(%$if_index)) {
  319. my $index = $if_index->{$row};
  320. next if ($if_name->{$ifName.".".$index} =~/^lo/i);
  321. next if ($if_name->{$ifName.".".$index} =~/^dummy/i);
  322. next if ($if_name->{$ifName.".".$index} =~/^enet/i);
  323. next if ($if_name->{$ifName.".".$index} =~/^Nu/i);
  324. # next if ($if_name->{$ifName.".".$index} =~/^Po/i);
  325. my $ifc_alias;
  326. $ifc_alias=$if_alias->{$ifAlias.".".$index} if ($if_alias->{$ifAlias.".".$index});
  327. my $ifc_name;
  328. $ifc_name=$if_name->{$ifName.".".$index} if ($if_name->{$ifName.".".$index});
  329. my $ifc_desc;
  330. $ifc_desc=$if_descr->{$ifDescr.".".$index} if ($if_descr->{$ifDescr.".".$index});
  331. $dev_cap->{$index}->{alias}=$ifc_alias if ($ifc_alias);
  332. $dev_cap->{$index}->{name}=$ifc_name if ($ifc_name);
  333. $dev_cap->{$index}->{desc}=$ifc_desc if ($ifc_desc);
  334. $dev_cap->{$index}->{index} = $index;
  335. };
  336. return $dev_cap;
  337. }
  338. #-------------------------------------------------------------------------------------
  339. sub get_router_state {
  340. my ($host,$snmp,$skip_empty) = @_;
  341. my $session = init_snmp ($host,$snmp,0);
  342. return if (!defined($session) or !$session);
  343. $session->translate([-timeticks]);
  344. my $router_status = $session->get_table("1.3.6.1.4.1.10.1");
  345. $session->close();
  346. return ($router_status);
  347. }
  348. #-------------------------------------------------------------------------------------
  349. sub snmp_get_req {
  350. my ($host,$snmp,$oid) = @_;
  351. my $session = init_snmp ($host,$snmp,0);
  352. return if (!defined($session) or !$session);
  353. $session->translate([-timeticks]);
  354. my $result = $session->get_request(-varbindlist => [$oid]) or return;
  355. $session->close();
  356. return $result->{$oid};
  357. }
  358. #-------------------------------------------------------------------------------------
  359. sub snmp_get_oid {
  360. my ($host,$snmp,$oid) = @_;
  361. my $port = 161;
  362. my $session = init_snmp ($host,$snmp,0);
  363. return if (!defined($session) or !$session);
  364. $session->translate([-timeticks]);
  365. my $table = $session->get_table($oid);
  366. $session->close();
  367. return $table;
  368. }
  369. #-------------------------------------------------------------------------------------
  370. sub snmp_walk_oid {
  371. my $host = shift;
  372. my $snmp = shift;
  373. my $oid = shift;
  374. my $rw = 'ro';
  375. ### open SNMP session
  376. my ($session, $error);
  377. if ($snmp->{version} <= 2) {
  378. ($session, $error) = Net::SNMP->session(
  379. -hostname => $host,
  380. -community => $snmp->{'ro-community'} ,
  381. -version => $snmp->{version},
  382. -port => $snmp->{port},
  383. -timeout => $snmp->{timeout},
  384. -nonblocking => 1,
  385. -translate => [-octetstring => 0],
  386. );
  387. } else {
  388. ($session, $error) = Net::SNMP->session(
  389. -hostname => $host,
  390. -version => 'snmpv3',
  391. -username => $snmp->{$rw.'-user'},
  392. -authprotocol => $snmp->{'auth-proto'},
  393. -privprotocol => $snmp->{'priv-proto'},
  394. -authpassword => $snmp->{$rw.'-password'},
  395. -privpassword => $snmp->{$rw.'-password'},
  396. -port => $snmp->{port},
  397. -timeout => $snmp->{timeout},
  398. -nonblocking => 1,
  399. -translate => [-octetstring => 0],
  400. );
  401. }
  402. return if (!defined($session) or !$session);
  403. my %table; # Hash to store the results
  404. my $result = $session->get_bulk_request(
  405. -varbindlist => [ $oid ],
  406. -callback => [ \&table_callback, \%table ],
  407. -maxrepetitions => 10,
  408. );
  409. snmp_dispatcher();
  410. $session->close();
  411. return \%table;
  412. }
  413. #-------------------------------------------------------------------------------------
  414. sub table_callback {
  415. my ($session, $table) = @_;
  416. my $list = $session->var_bind_list();
  417. if (!defined $list) {
  418. printf "ERROR: %s\n", $session->error();
  419. return;
  420. }
  421. my @names = $session->var_bind_names();
  422. my $next = undef;
  423. while (@names) {
  424. $next = shift @names;
  425. if (!oid_base_match($fdb_table, $next)) { return; }
  426. $table->{$next} = $list->{$next};
  427. }
  428. my $result = $session->get_bulk_request( -varbindlist => [ $next ], -maxrepetitions => 10);
  429. if (!defined $result) {
  430. printf "ERROR: %s.\n", $session->error();
  431. }
  432. return;
  433. }
  434. #-------------------------------------------------------------------------------------
  435. sub init_snmp {
  436. my ($host,$snmp,$rw) = @_;
  437. return if (!$host);
  438. my $community = $snmp->{'ro-community'};
  439. if (!$rw) { $rw = 'ro' }
  440. else {
  441. $rw = 'rw';
  442. $community = $snmp->{'rw-community'};
  443. }
  444. ### open SNMP session
  445. my ($session, $error);
  446. if ($snmp->{version} <=2) {
  447. ($session, $error) = Net::SNMP->session(
  448. -hostname => $host,
  449. -community => $community ,
  450. -version => $snmp->{'version'},
  451. -port => $snmp->{port},
  452. -timeout => $snmp->{timeout},
  453. );
  454. } else {
  455. ($session, $error) = Net::SNMP->session(
  456. -hostname => $host,
  457. -version => 'snmpv3',
  458. -username => $snmp->{$rw.'-user'},
  459. -authprotocol => $snmp->{'auth-proto'},
  460. -privprotocol => $snmp->{'priv-proto'},
  461. -authpassword => $snmp->{$rw.'-password'},
  462. -privpassword => $snmp->{$rw.'-password'},
  463. -port => $snmp->{port},
  464. -timeout => $snmp->{timeout},
  465. );
  466. }
  467. if ($error) {
  468. log_debug("SNMP init-request status for $host:");
  469. log_debug(Dumper($error));
  470. }
  471. return if (!defined($session) or !$session);
  472. return $session;
  473. }
  474. #-------------------------------------------------------------------------------------
  475. sub setCommunity {
  476. my $device = shift;
  477. $device->{snmp}->{'port'} = 161;
  478. $device->{snmp}->{'timeout'} = $snmp_timeout;
  479. $device->{snmp}->{'version'} = $device->{snmp_version} || '2';
  480. $device->{snmp}->{'ro-community'} = $device->{community} || $snmp_default_community;
  481. $device->{snmp}->{'rw-community'} = $device->{rw_community} || $snmp_default_community;
  482. #snmpv3
  483. $device->{snmp}->{'auth-proto'} = $device->{snmp3_auth_proto} || 'sha512';
  484. $device->{snmp}->{'priv-proto'} = $device->{snmp3_priv_proto} || 'aes128';
  485. $device->{snmp}->{'ro-user'} = $device->{snmp3_user_ro} || '';
  486. $device->{snmp}->{'rw-user'} = $device->{snmp3_user_rw} || '';
  487. $device->{snmp}->{'ro-password'} = $device->{snmp3_user_ro_password} || $snmp_default_community;
  488. $device->{snmp}->{'rw-password'} = $device->{snmp3_user_rw_password} || $snmp_default_community;
  489. }
  490. #-------------------------------------------------------------------------------------
  491. 1;
  492. }