fetch_new_arp.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use English;
  7. use base;
  8. use FindBin '$Bin';
  9. use lib "$Bin/";
  10. use strict;
  11. use DBI;
  12. use Time::Local;
  13. use Net::Patricia;
  14. use Data::Dumper;
  15. use Date::Parse;
  16. use Socket;
  17. use Rstat::config;
  18. use Rstat::main;
  19. use Rstat::net_utils;
  20. use Rstat::snmp;
  21. use Rstat::mysql;
  22. use NetAddr::IP;
  23. use Fcntl qw(:flock);
  24. use Parallel::ForkManager;
  25. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  26. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  27. setpriority(0,0,19);
  28. if ($config_ref{config_mode}) { log_info("System in configuration mode! Skip discovery."); exit; }
  29. my %mac_history;
  30. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());
  31. $month += 1;
  32. $year += 1900;
  33. my $fork_count = $cpu_count*5;
  34. #disable fork for debug
  35. #if ($debug) { $fork_count = 0; }
  36. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  37. my $now_day=sprintf "%04d-%02d-%02d",$year,$month,$day;
  38. db_log_verbose($dbh,'Arp discovery started.');
  39. if ($ARGV[0]) {
  40. db_log_verbose($dbh,'Active check started!');
  41. my $subnets=get_subnets_ref($dbh);
  42. my @fping_cmd=();
  43. foreach my $net (keys %$subnets) {
  44. next if (!$net);
  45. next if (!$subnets->{$net}{discovery});
  46. my $run_cmd="$fping -g $subnets->{$net}{subnet} -B1.0 -c 1 >/dev/null 2>&1";
  47. db_log_debug($dbh,"Checked network $subnets->{$net}{subnet}") if ($debug);
  48. push(@fping_cmd,$run_cmd);
  49. }
  50. $parallel_process_count = $cpu_count*2;
  51. run_in_parallel(@fping_cmd);
  52. }
  53. my @router_ref = get_records_sql($dbh,"SELECT * FROM devices WHERE deleted=0 AND (device_type=2 or device_type=0) AND discovery=1 AND snmp_version>0 ORDER by ip" );
  54. my @arp_array=();
  55. my $pm_arp = Parallel::ForkManager->new($fork_count);
  56. # data structure retrieval and handling
  57. $pm_arp -> run_on_finish (
  58. sub {
  59. my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
  60. if (defined($data_structure_reference)) {
  61. # children are not forced to send anything
  62. my $result = ${$data_structure_reference};
  63. push(@arp_array,$result);
  64. }
  65. }
  66. );
  67. DATA_LOOP:
  68. foreach my $router (@router_ref) {
  69. my $router_ip=$router->{ip};
  70. my $snmp_version=$router->{snmp_version};
  71. my $community=$router->{community};
  72. if (!HostIsLive($router_ip)) { log_info("Host id: $router->{id} name: $router->{device_name} ip: $router_ip is down! Skip."); next; }
  73. $pm_arp->start() and next DATA_LOOP;
  74. my $arp_table=get_arp_table($router_ip,$community,$snmp_version);
  75. $pm_arp->finish(0, \$arp_table);
  76. }
  77. $pm_arp->wait_all_children;
  78. ########################### end fork's #########################
  79. $dbh=init_db();
  80. #get userid list
  81. my @authlist_ref = get_records_sql($dbh,"SELECT * FROM User_auth WHERE deleted=0 ORDER by ip_int" );
  82. my $users = new Net::Patricia;
  83. my %ip_list;
  84. foreach my $row (@authlist_ref) {
  85. $users->add_string($row->{ip},$row->{id});
  86. $ip_list{$row->{id}}->{id}=$row->{id};
  87. $ip_list{$row->{id}}->{ip}=$row->{ip};
  88. $ip_list{$row->{id}}->{mac}=mac_splitted($row->{mac}) || '';
  89. }
  90. foreach my $arp_table (@arp_array) {
  91. foreach my $ip (keys %$arp_table) {
  92. next if (!$arp_table->{$ip});
  93. my $mac=trim($arp_table->{$ip});
  94. $mac=mac_splitted($mac);
  95. next if (!$mac);
  96. next if ($mac=~/ff:ff:ff:ff:ff:ff/i);
  97. next if ($mac!~/(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/);
  98. my $simple_mac=mac_simplify($mac);
  99. $ip=trim($ip);
  100. my $ip_aton=StrToIp($ip);
  101. $mac_history{$simple_mac}{changed}=0;
  102. $mac_history{$simple_mac}{ip}=$ip;
  103. $mac_history{$simple_mac}{auth_id}=0;
  104. next if (!$office_networks->match_string($ip));
  105. db_log_debug($dbh,"Analyze ip: $ip mac: $mac") if ($debug);
  106. my $auth_id = $users->match_string($ip);
  107. my $arp_record;
  108. $arp_record->{ip} = $ip;
  109. $arp_record->{mac} = $mac;
  110. $arp_record->{type} = 'arp';
  111. $arp_record->{ip_aton} = $ip_aton;
  112. $arp_record->{hotspot} = is_hotspot($dbh,$ip);
  113. my $cur_auth_id=resurrection_auth($dbh,$arp_record);
  114. next if (!$cur_auth_id);
  115. $mac_history{$simple_mac}{auth_id}=$cur_auth_id;
  116. if ($auth_id ne $cur_auth_id) { $mac_history{$simple_mac}{changed}=1; }
  117. }
  118. }
  119. db_log_verbose($dbh,'Mac discovery started.');
  120. my %connections=();
  121. my @connections_list=get_records_sql($dbh,"SELECT * FROM connections ORDER BY auth_id");
  122. foreach my $connection (@connections_list) {
  123. next if (!$connection);
  124. $connections{$connection->{auth_id}}{port}=$connection->{port_id};
  125. $connections{$connection->{auth_id}}{id}=$connection->{id};
  126. }
  127. my $auth_filter=" AND last_found >='".$now_day."' ";
  128. my $auth_sql="SELECT id,mac FROM User_auth WHERE mac IS NOT NULL AND deleted=0 $auth_filter ORDER BY id ASC";
  129. my @auth_list=get_records_sql($dbh,$auth_sql);
  130. my %auth_table;
  131. foreach my $auth (@auth_list) {
  132. next if (!$auth);
  133. my $auth_mac=mac_simplify($auth->{mac});
  134. $auth_table{oper_table}{$auth_mac}=$auth->{id};
  135. }
  136. $auth_sql="SELECT id,mac FROM User_auth WHERE mac IS NOT NULL AND deleted=0 ORDER BY id DESC";
  137. my @auth_full_list=get_records_sql($dbh,$auth_sql);
  138. foreach my $auth (@auth_full_list) {
  139. next if (!$auth);
  140. my $auth_mac=mac_simplify($auth->{mac});
  141. next if (exists $auth_table{full_table}{$auth_mac});
  142. $auth_table{full_table}{$auth_mac}=$auth->{id};
  143. }
  144. my @unknown_list=get_records_sql($dbh,"SELECT id,mac,port_id,device_id FROM Unknown_mac WHERE mac !='' ORDER BY mac");
  145. my %unknown_table;
  146. foreach my $unknown (@unknown_list) {
  147. next if (!$unknown);
  148. my $unknown_mac=mac_simplify($unknown->{mac});
  149. $unknown_table{$unknown_mac}{unknown_id}=$unknown->{id};
  150. $unknown_table{$unknown_mac}{port_id}=$unknown->{port_id};
  151. $unknown_table{$unknown_mac}{device_id}=$unknown->{device_id};
  152. }
  153. my @device_list = get_records_sql($dbh,"SELECT * FROM devices WHERE deleted=0 AND discovery=1 AND device_type<=2 AND snmp_version>0" );
  154. my @fdb_array=();
  155. my $pm_fdb = Parallel::ForkManager->new($fork_count);
  156. # data structure retrieval and handling
  157. $pm_fdb -> run_on_finish (
  158. sub {
  159. my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
  160. if (defined($data_structure_reference)) { # children are not forced to send anything
  161. my $result = ${$data_structure_reference}; # child passed a string reference
  162. push(@fdb_array,$result);
  163. }
  164. }
  165. );
  166. my %dev_ifindex=();
  167. foreach my $device (@device_list) {
  168. my $dev_id = $device->{id};
  169. my @device_ports = get_records_sql($dbh,"SELECT * FROM device_ports WHERE device_id=$dev_id");
  170. foreach my $port_data (@device_ports) {
  171. if (!$port_data->{snmp_index}) { $port_data->{snmp_index} = $port_data->{port}; }
  172. $dev_ifindex{$dev_id}->{$port_data->{port}}=$port_data->{snmp_index};
  173. }
  174. }
  175. FDB_LOOP:
  176. foreach my $device (@device_list) {
  177. my $int_list = get_snmp_ifindex($device->{ip},$device->{community},$device->{snmp_version});
  178. if (!$int_list) { log_info("Host id: $device->{id} name: $device->{device_name} ip: $device->{ip} is down! Skip."); next; }
  179. $pm_fdb->start() and next FDB_LOOP;
  180. my $fdb=get_fdb_table($device->{ip},$device->{community},$device->{snmp_version},$dev_ifindex{$device->{id}});
  181. my $result;
  182. $result->{id}=$device->{id};
  183. $result->{fdb} = $fdb;
  184. $pm_fdb->finish(0, \$result);
  185. }
  186. $pm_fdb->wait_all_children;
  187. my %fdb_ref;
  188. foreach my $fdb_table (@fdb_array){
  189. next if (!$fdb_table);
  190. $fdb_ref{$fdb_table->{id}}{fdb}=$fdb_table->{fdb};
  191. }
  192. ################################ end fork's ##############################
  193. $dbh=init_db();
  194. foreach my $device (@device_list) {
  195. my %port_snmp_index=();
  196. my %port_index=();
  197. my %mac_port_count=();
  198. my %mac_address_table=();
  199. my %port_links=();
  200. my $dev_id = $device->{id};
  201. my $dev_name = $device->{device_name};
  202. my $fdb=$fdb_ref{$dev_id}{fdb};
  203. next if (!$fdb);
  204. my @device_ports = get_records_sql($dbh,"SELECT * FROM device_ports WHERE device_id=$dev_id");
  205. foreach my $port_data (@device_ports) {
  206. my $fdb_port_index=$port_data->{port};
  207. my $port_id = $port_data->{id};
  208. if (!$port_data->{snmp_index}) { $port_data->{snmp_index} = $port_data->{port}; }
  209. $fdb_port_index=$port_data->{snmp_index};
  210. next if ($port_data->{skip});
  211. #snmp-индекс порта = номеру порта
  212. $port_snmp_index{$port_data->{snmp_index}}=$port_data->{port};
  213. # номер порта = id записи порта в таблице
  214. $port_index{$port_data->{port}}=$port_data->{id};
  215. # номер порта = id записи порта аплинка/даунлинка свича
  216. $port_links{$port_data->{port}}=$port_data->{target_port_id};
  217. $mac_port_count{$port_data->{port}}=0;
  218. }
  219. my $sw_mac;
  220. if ($device->{vendor_id} eq '9') {
  221. #get device mac
  222. my $sw_auth = get_record_sql($dbh,"SELECT mac FROM User_auth WHERE deleted=0 and ip='".$device->{ip}."'");
  223. $sw_mac = mac_simplify($sw_auth->{mac});
  224. $sw_mac =~s/.{2}$//s;
  225. }
  226. foreach my $mac (keys %$fdb) {
  227. #port from fdb table
  228. my $port = $fdb->{$mac};
  229. next if (!$port);
  230. #real port number
  231. if (!exists $port_snmp_index{$port}) { next; }
  232. #get real port number by snmp our snmp index
  233. $port=$port_snmp_index{$port};
  234. if (!exists $port_index{$port}) { next; }
  235. #mikrotik patch - skip mikrotik device mac
  236. if ($sw_mac and $mac=~/^$sw_mac/i) { next; }
  237. $mac_port_count{$port}++;
  238. #мак = номер порта
  239. $mac_address_table{$mac}=$port;
  240. }
  241. # обновляем число маков на порту
  242. foreach my $port (keys %mac_port_count) {
  243. if (!$port) { next; }
  244. if (!exists $port_index{$port}) { next; }
  245. #skip uplink|downlink
  246. if ($port_links{$port}>0) { next; }
  247. my $dev_ports;
  248. $dev_ports->{last_mac_count}=$mac_port_count{$port};
  249. update_record($dbh,'device_ports',$dev_ports,"device_id=$dev_id and port=$port");
  250. }
  251. foreach my $mac (keys %mac_address_table) {
  252. my $port = $mac_address_table{$mac};
  253. if (!$port) { next; }
  254. if (!exists $port_index{$port}) { next; }
  255. #skip uplink|downlink
  256. if ($port_links{$port}>0) { next; }
  257. my $simple_mac=mac_simplify($mac);
  258. my $mac_splitted=mac_splitted($mac);
  259. $mac_history{$simple_mac}{port_id}=$port_index{$port};
  260. $mac_history{$simple_mac}{dev_id}=$dev_id;
  261. if (!$mac_history{$simple_mac}{changed}) { $mac_history{$simple_mac}{changed}=0; }
  262. my $port_id=$port_index{$port};
  263. if (exists $auth_table{full_table}{$simple_mac} or exists $auth_table{oper_table}{$simple_mac}) {
  264. my $auth_id;
  265. if (exists $auth_table{oper_table}{$simple_mac}) {
  266. $auth_id=$auth_table{oper_table}{$simple_mac};
  267. } else {
  268. $auth_id=$auth_table{full_table}{$simple_mac};
  269. db_log_debug($dbh,"Mac not found in oper ARP-table. Use old values auth_id: $auth_id [$simple_mac] at device $dev_name [$port]",$auth_id);
  270. }
  271. if (exists $connections{$auth_id}) {
  272. if ($port_id == $connections{$auth_id}{port}) {
  273. if (exists $auth_table{oper_table}{$simple_mac}) {
  274. my $auth_rec;
  275. $auth_rec->{last_found}=$now_str;
  276. update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
  277. }
  278. next;
  279. }
  280. $connections{$auth_id}{port}=$port_id;
  281. $mac_history{$simple_mac}{changed}=1;
  282. $mac_history{$simple_mac}{auth_id}=$auth_id;
  283. db_log_info($dbh,"Found auth_id: $auth_id ip: $mac_history{$simple_mac}{ip} [$mac_splitted] at device $dev_name [$port]. Update connection",$auth_id);
  284. my $auth_rec;
  285. $auth_rec->{last_found}=$now_str;
  286. update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
  287. my $conn_rec;
  288. $conn_rec->{port_id}=$port_id;
  289. $conn_rec->{device_id}=$dev_id;
  290. update_record($dbh,'connections',$conn_rec,"auth_id=$auth_id");
  291. } else {
  292. $mac_history{$simple_mac}{changed}=1;
  293. $mac_history{$simple_mac}{auth_id}=$auth_id;
  294. $connections{$auth_id}{port}=$port_id;
  295. db_log_info($dbh,"Found auth_id: $auth_id ip: $mac_history{$simple_mac}{ip} [$mac_splitted] at device $dev_name [$port]. Create connection.",$auth_id);
  296. my $auth_rec;
  297. $auth_rec->{last_found}=$now_str;
  298. update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
  299. my $conn_rec;
  300. $conn_rec->{port_id}=$port_id;
  301. $conn_rec->{device_id}=$dev_id;
  302. $conn_rec->{auth_id}=$auth_id;
  303. insert_record($dbh,'connections',$conn_rec);
  304. }
  305. } else {
  306. if (exists $unknown_table{$simple_mac}{unknown_id}) {
  307. next if ($unknown_table{$simple_mac}{port_id} == $port_id and $unknown_table{$simple_mac}{device_id} == $dev_id);
  308. $mac_history{$simple_mac}{changed}=1;
  309. $mac_history{$simple_mac}{auth_id}=0;
  310. db_log_debug($dbh,"Unknown mac $mac_splitted moved to $dev_name [$port]") if ($debug);
  311. my $unknown_rec;
  312. $unknown_rec->{port_id}=$port_id;
  313. $unknown_rec->{device_id}=$dev_id;
  314. update_record($dbh,'Unknown_mac',$unknown_rec,"id=$unknown_table{$simple_mac}{unknown_id}");
  315. } else {
  316. $mac_history{$simple_mac}{changed}=1;
  317. $mac_history{$simple_mac}{auth_id}=0;
  318. db_log_debug($dbh,"Unknown mac $mac_splitted found at $dev_name [$port]") if ($debug);
  319. my $unknown_rec;
  320. $unknown_rec->{port_id}=$port_id;
  321. $unknown_rec->{device_id}=$dev_id;
  322. $unknown_rec->{mac}=$simple_mac;
  323. insert_record($dbh,'Unknown_mac',$unknown_rec);
  324. }
  325. }
  326. }
  327. }
  328. foreach my $mac (keys %mac_history) {
  329. next if (!$mac);
  330. next if (!$mac_history{$mac}->{changed});
  331. my $h_dev_id='';
  332. $h_dev_id=$mac_history{$mac}->{dev_id} if ($mac_history{$mac}->{dev_id});
  333. my $h_port_id='';
  334. $h_port_id=$mac_history{$mac}->{port_id} if ($mac_history{$mac}->{port_id});
  335. my $h_ip='';
  336. $h_ip=$mac_history{$mac}->{ip} if ($mac_history{$mac}->{ip});
  337. my $h_auth_id=$mac_history{$mac}->{auth_id} if ($mac_history{$mac}->{auth_id});
  338. if (!$h_auth_id) { $h_auth_id=0; }
  339. next if (!$h_dev_id);
  340. my $history_rec;
  341. $history_rec->{device_id}=$h_dev_id;
  342. $history_rec->{port_id}=$h_port_id;
  343. $history_rec->{mac}=$mac;
  344. $history_rec->{ip}=$h_ip;
  345. $history_rec->{auth_id}=$h_auth_id;
  346. insert_record($dbh,'mac_history',$history_rec);
  347. }
  348. $dbh->disconnect;
  349. exit 0;