1
0

fetch_new_arp.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use strict;
  8. use DBI;
  9. use Time::Local;
  10. use Net::Patricia;
  11. use Data::Dumper;
  12. use Date::Parse;
  13. use Socket;
  14. use Rstat::config;
  15. use Rstat::main;
  16. use Rstat::net_utils;
  17. use Rstat::snmp;
  18. use Rstat::mysql;
  19. use NetAddr::IP;
  20. setpriority(0,0,19);
  21. my %mac_history;
  22. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());
  23. $month += 1;
  24. $year += 1900;
  25. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  26. my $now_day=sprintf "%04d-%02d-%02d",$year,$month,$day;
  27. if (!$arp_discovery) {
  28. db_log_verbose($dbh,'Arp discovery disabled by config');
  29. } else {
  30. db_log_verbose($dbh,'Arp discovery started.');
  31. if ($ARGV[0]) {
  32. db_log_verbose($dbh,'Active check started!');
  33. my $subnets=get_subnets_ref($dbh);
  34. foreach my $net (keys %$subnets) {
  35. next if (!$net);
  36. next if (!$subnets->{$net}{discovery});
  37. my $run_cmd="$fping -g $subnets->{$net}{subnet} -B1.0 -c 1 >/dev/null 2>&1";
  38. db_log_debug($dbh,"Checked network $subnets->{$net}{subnet}") if ($debug);
  39. do_exec($run_cmd);
  40. }
  41. }
  42. my @router_ref = get_records_sql($dbh,"SELECT * FROM devices WHERE deleted=0 AND device_type=2 AND discovery=1 AND snmp_version>0 ORDER by ip" );
  43. #get userid list
  44. my @authlist_ref = get_records_sql($dbh,"SELECT * FROM User_auth WHERE deleted=0 ORDER by ip_int" );
  45. my $users = new Net::Patricia;
  46. my %ip_list;
  47. foreach my $row (@authlist_ref) {
  48. $users->add_string($row->{ip},$row->{id});
  49. $ip_list{$row->{id}}->{id}=$row->{id};
  50. $ip_list{$row->{id}}->{ip}=$row->{ip};
  51. $ip_list{$row->{id}}->{mac}=mac_splitted($row->{mac}) || '';
  52. }
  53. foreach my $router (@router_ref) {
  54. my $router_ip=$router->{ip};
  55. my $snmp_version=$router->{snmp_version};
  56. my $community=$router->{community};
  57. #print "Analyze $router_ip $snmp_version $community\n";
  58. my $arp_table=get_arp_table($router_ip,$community,$snmp_version);
  59. next if (!$arp_table);
  60. foreach my $ip (keys %$arp_table) {
  61. next if (!$arp_table->{$ip});
  62. my $mac=trim($arp_table->{$ip});
  63. $mac=mac_splitted($mac);
  64. next if (!$mac);
  65. next if ($mac=~/ff:ff:ff:ff:ff:ff/i);
  66. next if ($mac!~/(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/);
  67. my $simple_mac=mac_simplify($mac);
  68. $ip=trim($ip);
  69. my $ip_aton=StrToIp($ip);
  70. $mac_history{$simple_mac}{changed}=0;
  71. $mac_history{$simple_mac}{ip}=$ip;
  72. $mac_history{$simple_mac}{auth_id}=0;
  73. next if (!$office_networks->match_string($ip));
  74. db_log_debug($dbh,"Analyze ip: $ip mac: $mac") if ($debug);
  75. my $auth_id = $users->match_string($ip);
  76. my $cur_auth_id=resurrection_auth($dbh,$ip,$mac,'arp');
  77. $mac_history{$simple_mac}{auth_id}=$cur_auth_id;
  78. if ($auth_id ne $cur_auth_id) {
  79. $mac_history{$simple_mac}{changed}=1;
  80. }
  81. }
  82. }
  83. db_log_verbose($dbh,'Arp discovery stopped.');
  84. }
  85. #MAC Discavery
  86. if (!$mac_discovery) {
  87. db_log_verbose($dbh,'Mac discovery disabled by config');
  88. } else {
  89. sleep(1);
  90. db_log_verbose($dbh,'Mac discovery started.');
  91. my %connections=();
  92. my $connections_list=do_sql($dbh,"Select id,auth_id,port_id from connections order by auth_id");
  93. foreach my $connection (@$connections_list) {
  94. next if (!$connection);
  95. my ($conn_id,$conn_auth_id,$conn_port_id)=@$connection;
  96. $connections{$conn_auth_id}{port}=$conn_port_id;
  97. $connections{$conn_auth_id}{id}=$conn_id;
  98. }
  99. my $auth_filter='';
  100. if ($arp_discovery) { $auth_filter=" and last_found >='".$now_day."' "; }
  101. my $auth_sql="Select id,mac from User_auth where mac is not null and deleted=0 $auth_filter order by id asc";
  102. my $auth_list=do_sql($dbh,$auth_sql);
  103. my %auth_table;
  104. foreach my $auth (@$auth_list) {
  105. next if (!$auth);
  106. my ($auth_id,$auth_mac)=@$auth;
  107. $auth_mac=mac_simplify($auth_mac);
  108. $auth_table{oper_table}{$auth_mac}=$auth_id;
  109. }
  110. $auth_sql="Select id,mac from User_auth where mac is not null and deleted=0 order by id asc";
  111. my $auth_full_list=do_sql($dbh,$auth_sql);
  112. foreach my $auth (@$auth_full_list) {
  113. next if (!$auth);
  114. my ($auth_id,$auth_mac)=@$auth;
  115. $auth_mac=mac_simplify($auth_mac);
  116. $auth_table{full_table}{$auth_mac}=$auth_id;
  117. }
  118. my $unknown_list=do_sql($dbh,"Select id,mac,port_id,device_id from Unknown_mac where mac !='' order by mac");
  119. my %unknown_table;
  120. foreach my $unknown (@$unknown_list) {
  121. next if (!$unknown);
  122. my ($unknown_id,$unknown_mac,$unknown_port_id,$unknown_device_id)=@$unknown;
  123. $unknown_mac=mac_simplify($unknown_mac);
  124. $unknown_table{$unknown_mac}{unknown_id}=$unknown_id;
  125. $unknown_table{$unknown_mac}{port_id}=$unknown_port_id;
  126. $unknown_table{$unknown_mac}{device_id}=$unknown_device_id;
  127. }
  128. my $device_ref = do_sql($dbh,"SELECT ip,snmp_version,community,fdb_snmp_index,device_name,id from devices WHERE deleted=0 and discovery=1 and snmp_version>0" );
  129. foreach my $device (@$device_ref) {
  130. my %port_snmp_index=();
  131. my %port_index=();
  132. my %mac_port_count=();
  133. my %mac_address_table=();
  134. my %port_links=();
  135. my ($dev_ip,$dev_snmp_ver,$dev_community,$dev_fdb_index,$dev_name,$dev_id)=@$device;
  136. next if (!$dev_id);
  137. #print "$dev_ip,$dev_snmp_ver,$dev_community,$dev_fdb_index,$dev_name,$dev_id\n" if ($debug);
  138. my $fdb=get_fdb_table($dev_ip,$dev_community,$dev_snmp_ver);
  139. next if (!$fdb);
  140. my $device_ports = do_sql($dbh,"Select port,snmp_index,target_port_id,id,skip,vlan from device_ports where device_id=$dev_id");
  141. foreach my $port (@$device_ports) {
  142. my ($port,$snmp_index,$target_port_id,$port_id,$skip_port,$vlan)=@$port;
  143. my $port_index=$port;
  144. if (!$vlan) { $vlan=1; }
  145. if ($dev_fdb_index) {
  146. if (!$snmp_index) { next; }
  147. $port_index=$snmp_index;
  148. }
  149. my $current_vlan = get_vlan_at_port($dev_ip,$dev_community,$dev_snmp_ver,$port_index);
  150. if (!$current_vlan or $current_vlan=~/noSuchInstance/i or !is_integer($current_vlan)) { $current_vlan=1; }
  151. if ($current_vlan != $vlan) {
  152. my $dev_ports;
  153. $dev_ports->{vlan}=$current_vlan;
  154. update_record($dbh,'device_ports',$dev_ports,"device_id=$dev_id and port=$port");
  155. db_log_verbose($dbh,"Vlan changed at device $dev_name [$port] old: $vlan current: $current_vlan");
  156. }
  157. next if ($skip_port);
  158. $port_snmp_index{$snmp_index}=$port;
  159. $port_index{$port}=$port_id;
  160. $port_links{$port}=$target_port_id;
  161. $mac_port_count{$port}=0;
  162. }
  163. foreach my $mac (keys %$fdb) {
  164. my $port = $fdb->{$mac};
  165. next if (!$port);
  166. if ($dev_fdb_index) {
  167. if (!exists $port_snmp_index{$port}) { next; }
  168. $port=$port_snmp_index{$port};
  169. }
  170. if (!exists $port_index{$port}) { next; }
  171. $mac_port_count{$port}++;
  172. $mac_address_table{$mac}=$port;
  173. }
  174. foreach my $port (keys %mac_port_count) {
  175. if (!$port) { next; }
  176. if (!exists $port_index{$port}) { next; }
  177. #skip uplink|downlink
  178. if ($port_links{$port}>0) { next; }
  179. my $dev_ports;
  180. $dev_ports->{last_mac_count}=$mac_port_count{$port};
  181. update_record($dbh,'device_ports',$dev_ports,"device_id=$dev_id and port=$port");
  182. }
  183. foreach my $mac (keys %mac_address_table) {
  184. my $port = $mac_address_table{$mac};
  185. if (!$port) { next; }
  186. if (!exists $port_index{$port}) { next; }
  187. #skip uplink|downlink
  188. if ($port_links{$port}>0) { next; }
  189. my $simple_mac=mac_simplify($mac);
  190. $mac_history{$simple_mac}{port_id}=$port_index{$port};
  191. $mac_history{$simple_mac}{dev_id}=$dev_id;
  192. if (!$mac_history{$simple_mac}{changed}) { $mac_history{$simple_mac}{changed}=0; }
  193. my $port_id=$port_index{$port};
  194. if (exists $auth_table{full_table}{$mac} or exists $auth_table{oper_table}{$mac}) {
  195. my $auth_id;
  196. if (exists $auth_table{oper_table}{$mac}) { $auth_id=$auth_table{oper_table}{$mac}; } else {
  197. $auth_id=$auth_table{full_table}{$mac};
  198. if ($debug) {
  199. db_log_debug($dbh,"Mac not found in oper ARP-table. Use old values auth_id: $auth_id [$mac] at device $dev_name [$port]");
  200. }
  201. }
  202. if (exists $connections{$auth_id}) {
  203. if ($port_id == $connections{$auth_id}{port}) {
  204. if (exists $auth_table{oper_table}{$mac}) {
  205. my $auth_rec;
  206. $auth_rec->{last_found}=$now_str;
  207. update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
  208. }
  209. next;
  210. }
  211. $connections{$auth_id}{port}=$port_id;
  212. $mac_history{$simple_mac}{changed}=1;
  213. $mac_history{$simple_mac}{auth_id}=$auth_id;
  214. db_log_info($dbh,"Found auth_id: $auth_id [$mac] at device $dev_name [$port]. Update connection");
  215. my $auth_rec;
  216. $auth_rec->{last_found}=$now_str;
  217. update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
  218. my $conn_rec;
  219. $conn_rec->{port_id}=$port_id;
  220. $conn_rec->{device_id}=$dev_id;
  221. update_record($dbh,'connections',$conn_rec,"auth_id=$auth_id");
  222. } else {
  223. $mac_history{$simple_mac}{changed}=1;
  224. $mac_history{$simple_mac}{auth_id}=$auth_id;
  225. $connections{$auth_id}{port}=$port_id;
  226. db_log_info($dbh,"Found auth_id: $auth_id [$mac] at device $dev_name [$port]. Create connection.");
  227. my $auth_rec;
  228. $auth_rec->{last_found}=$now_str;
  229. update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
  230. my $conn_rec;
  231. $conn_rec->{port_id}=$port_id;
  232. $conn_rec->{device_id}=$dev_id;
  233. $conn_rec->{auth_id}=$auth_id;
  234. insert_record($dbh,'connections',$conn_rec);
  235. }
  236. } else {
  237. if (exists $unknown_table{$simple_mac}{unknown_id}) {
  238. next if ($unknown_table{$simple_mac}{port_id} == $port_id and $unknown_table{$simple_mac}{device_id} == $dev_id);
  239. $mac_history{$simple_mac}{changed}=1;
  240. $mac_history{$simple_mac}{auth_id}=0;
  241. $mac=mac_splitted($mac);
  242. db_log_debug($dbh,"Unknown mac $mac moved to $dev_name [$port]") if ($debug);
  243. my $unknown_rec;
  244. $unknown_rec->{port_id}=$port_id;
  245. $unknown_rec->{device_id}=$dev_id;
  246. update_record($dbh,'Unknown_mac',$unknown_rec,"id=$unknown_table{$simple_mac}{unknown_id}");
  247. } else {
  248. $mac=mac_splitted($mac);
  249. $mac_history{$simple_mac}{changed}=1;
  250. $mac_history{$simple_mac}{auth_id}=0;
  251. db_log_debug($dbh,"Unknown mac $mac found at $dev_name [$port]") if ($debug);
  252. my $unknown_rec;
  253. $unknown_rec->{port_id}=$port_id;
  254. $unknown_rec->{device_id}=$dev_id;
  255. $unknown_rec->{mac}=$simple_mac;
  256. insert_record($dbh,'Unknown_mac',$unknown_rec);
  257. }
  258. }
  259. }
  260. }
  261. db_log_verbose($dbh,'Mac discovery stopped.');
  262. }
  263. foreach my $mac (keys %mac_history) {
  264. next if (!$mac);
  265. next if (!$mac_history{$mac}->{changed});
  266. my $h_dev_id='';
  267. $h_dev_id=$mac_history{$mac}->{dev_id} if ($mac_history{$mac}->{dev_id});
  268. my $h_port_id='';
  269. $h_port_id=$mac_history{$mac}->{port_id} if ($mac_history{$mac}->{port_id});
  270. my $h_ip='';
  271. $h_ip=$mac_history{$mac}->{ip} if ($mac_history{$mac}->{ip});
  272. my $h_auth_id=$mac_history{$mac}->{auth_id} if ($mac_history{$mac}->{auth_id});
  273. if (!$h_auth_id) { $h_auth_id=0; }
  274. next if (!$h_dev_id);
  275. my $history_rec;
  276. $history_rec->{device_id}=$h_dev_id;
  277. $history_rec->{port_id}=$h_port_id;
  278. $history_rec->{mac}=$mac;
  279. $history_rec->{ip}=$h_ip;
  280. $history_rec->{auth_id}=$h_auth_id;
  281. insert_record($dbh,'mac_history',$history_rec);
  282. }
  283. $dbh->disconnect;
  284. exit 0;