fetch_new_arp.pl 12 KB

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