parse_flow.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use English;
  6. use base;
  7. use FindBin '$Bin';
  8. use lib "$Bin/";
  9. use strict;
  10. use DBI;
  11. use Time::Local;
  12. use Net::Patricia;
  13. use Data::Dumper;
  14. use Date::Parse;
  15. use Socket;
  16. use Rstat::config;
  17. use Rstat::main;
  18. use Rstat::net_utils;
  19. use Rstat::mysql;
  20. use Parallel::ForkManager;
  21. my $router_id;
  22. if (scalar @ARGV>1) { $router_id=shift(@ARGV); } else { $router_id=$ARGV[0]; }
  23. if (!$router_id) {
  24. db_log_error($dbh,"Router id not defined! Bye...");
  25. exit 110;
  26. }
  27. my $fork_count = $cpu_count*10;
  28. my $timeshift = get_option($dbh,55)*60;
  29. db_log_debug($dbh,"Import traffic from router id: $router_id start. Timestep $timeshift sec.") if ($debug);
  30. my %stats;
  31. $stats{pkt}{all}=0;
  32. $stats{pkt}{user_in}=0;
  33. $stats{pkt}{user_out}=0;
  34. $stats{pkt}{free}=0;
  35. $stats{pkt}{unknown}=0;
  36. $stats{line}{all}=0;
  37. $stats{line}{user}=0;
  38. $stats{line}{free}=0;
  39. $stats{line}{unknown}=0;
  40. # net objects
  41. my $users = new Net::Patricia;
  42. InitSubnets();
  43. #get userid list
  44. my @auth_list_ref = get_records_sql($dbh,"SELECT id,ip,user_id,save_traf FROM User_auth where deleted=0 ORDER by user_id");
  45. my %user_stats;
  46. foreach my $row (@auth_list_ref) {
  47. $users->add_string($row->{ip},$row->{id});
  48. $user_stats{$row->{ip}}{net}=$row->{ip};
  49. $user_stats{$row->{ip}}{auth_id}=$row->{id};
  50. $user_stats{$row->{ip}}{user_id}=$row->{user_id};
  51. $user_stats{$row->{ip}}{save_traf}=$row->{save_traf};
  52. $user_stats{$row->{ip}}{in}=0;
  53. $user_stats{$row->{ip}}{out}=0;
  54. $user_stats{$row->{ip}}{pkt_in}=0;
  55. $user_stats{$row->{ip}}{pkt_out}=0;
  56. }
  57. my $last_time = localtime();
  58. my $hour_date;
  59. my $minute_date;
  60. my @batch_sql_traf=();
  61. my $pm = Parallel::ForkManager->new($fork_count);
  62. $pm->run_on_finish(
  63. sub {
  64. my ($pid, $exit, $ident, $signal, $core, $data) = @_;
  65. if ($data) {
  66. my $dataref = ${$data};
  67. my $f_dbh=init_db();
  68. foreach my $user_ip (keys %{$dataref->{stats}}) {
  69. $user_stats{$user_ip}{in} += $dataref->{stats}{$user_ip}{in};
  70. $user_stats{$user_ip}{pkt_in} +=$dataref->{stats}{$user_ip}{pkt_in};
  71. $user_stats{$user_ip}{out} += $dataref->{stats}{$user_ip}{out};
  72. $user_stats{$user_ip}{pkt_out} +=$dataref->{stats}{$user_ip}{pkt_out};
  73. }
  74. $stats{pkt}{all}+=$dataref->{pkt}{all};
  75. $stats{pkt}{user_in}+=$dataref->{pkt}{user_in};
  76. $stats{pkt}{user_out}+=$dataref->{pkt}{user_out};
  77. $stats{pkt}{free}+=$dataref->{pkt}{free};
  78. $stats{pkt}{unknown}+=$dataref->{pkt}{unknown};
  79. $stats{line}{all}+=$dataref->{line}{all};
  80. $stats{line}{user}+=$dataref->{line}{user};
  81. $stats{line}{free}+=$dataref->{line}{free};
  82. $stats{line}{unknown}+=$dataref->{line}{unknown};
  83. $last_time = $dataref->{last_time};
  84. if (scalar(@{$dataref->{sql}})) { batch_db_sql($f_dbh,\@{$dataref->{sql}}); }
  85. $f_dbh->disconnect;
  86. }
  87. }
  88. );
  89. $dbh->disconnect;
  90. my @input_buf=();
  91. my $line_count = 0;
  92. my $child_count = 0;
  93. while (my $line = <STDIN>) {
  94. chomp($line);
  95. $line=~s/\s+//g;
  96. $line_count++;
  97. push(@input_buf,$line);
  98. if ($line_count < 50000) { next; }
  99. $line_count = 0;
  100. $child_count ++;
  101. my @tmp = @input_buf;
  102. undef @input_buf;
  103. $pm->start and next;
  104. db_log_debug($dbh,"Started child $child_count") if ($debug);
  105. my $ret = calc_stats(\@tmp);
  106. $pm->finish(0, \$ret);
  107. db_log_debug($dbh,"Stopped child $child_count") if ($debug);
  108. }
  109. if (scalar(@input_buf)) {
  110. $pm->start;
  111. my $ret = calc_stats(\@input_buf);
  112. $pm->finish(0, \$ret);
  113. }
  114. $pm->wait_all_children;
  115. undef(@input_buf);
  116. sub calc_stats {
  117. my $lines = shift;
  118. my $f_dbh = init_db();
  119. my $lines_stats;
  120. $lines_stats->{pkt}{all}=0;
  121. $lines_stats->{pkt}{user_in}=0;
  122. $lines_stats->{pkt}{user_out}=0;
  123. $lines_stats->{pkt}{free}=0;
  124. $lines_stats->{pkt}{unknown}=0;
  125. $lines_stats->{line}{all}=0;
  126. $lines_stats->{line}{user}=0;
  127. $lines_stats->{line}{free}=0;
  128. $lines_stats->{line}{unknown}=0;
  129. foreach my $line (@$lines) {
  130. my ($l_time,$l_proto,$l_src_ip,$l_dst_ip,$l_src_port,$l_dst_port,$l_packets,$l_bytes,$l_in_dev,$l_out_dev) = split(/;/,$line);
  131. $lines_stats->{pkt}{all}+=$l_packets;
  132. $lines_stats->{line}{all}++;
  133. $lines_stats->{last_time} = $l_time;
  134. if (!$l_time) { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  135. if ($l_src_ip eq '0.0.0.0') { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  136. if ($l_dst_ip eq '0.0.0.0') { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  137. if ($l_src_ip eq '255.255.255.255') { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  138. if ($l_dst_ip eq '255.255.255.255') { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  139. #special networks
  140. if ($Special_Nets->match_string($l_src_ip) or $Special_Nets->match_string($l_dst_ip)) { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  141. #unknown networks
  142. if (!$office_networks->match_string($l_src_ip) and !$office_networks->match_string($l_dst_ip)) { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  143. #local forward
  144. if ($office_networks->match_string($l_src_ip) and $office_networks->match_string($l_dst_ip)) { $lines_stats->{line}{free}++; $lines_stats->{line}{free}+=$l_packets; next; }
  145. #free forward
  146. if ($office_networks->match_string($l_src_ip) and $free_networks->match_string($l_dst_ip)) { $lines_stats->{line}{free}++; $lines_stats->{line}{free}+=$l_packets; next; }
  147. if ($free_networks->match_string($l_src_ip) and $office_networks->match_string($l_dst_ip)) { $lines_stats->{line}{free}++; $lines_stats->{line}{free}+=$l_packets; next; }
  148. my $l_src_ip_aton=StrToIp($l_src_ip);
  149. my $l_dst_ip_aton=StrToIp($l_dst_ip);
  150. my ($sec,$min,$hour,$day,$month,$year,$zone) = (localtime($l_time))[0,1,2,3,4,5];
  151. $month++;
  152. $year += 1900;
  153. my $full_time = $f_dbh->quote(sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec);
  154. my $user_ip;
  155. my $auth_id;
  156. # find user
  157. if ($users->match_string($l_src_ip)) {
  158. $user_ip = $l_src_ip;
  159. $lines_stats->{stats}{$user_ip}{ip}=$user_ip;
  160. if (!$lines_stats->{stats}{$user_ip}{out}) { $lines_stats->{stats}{$user_ip}{out}=0; }
  161. if (!$lines_stats->{stats}{$user_ip}{pkt_out}) { $lines_stats->{stats}{$user_ip}{pkt_out}=0; }
  162. $lines_stats->{stats}{$user_ip}{out} += $l_bytes;
  163. $lines_stats->{stats}{$user_ip}{pkt_out} +=$l_packets;
  164. $lines_stats->{line}{user}++;
  165. $lines_stats->{pkt}{user_out}+=$l_packets;
  166. }
  167. if ($users->match_string($l_dst_ip)) {
  168. $user_ip = $l_dst_ip;
  169. $lines_stats->{stats}{$user_ip}{ip}=$l_dst_ip;
  170. if (!$lines_stats->{stats}{$user_ip}{in}) { $lines_stats->{stats}{$user_ip}{in}=0; }
  171. if (!$lines_stats->{stats}{$user_ip}{pkt_in}) { $lines_stats->{stats}{$user_ip}{pkt_in}=0; }
  172. $lines_stats->{stats}{$user_ip}{in} += $l_bytes;
  173. $lines_stats->{stats}{$user_ip}{pkt_in} +=$l_packets;
  174. $lines_stats->{line}{user}++;
  175. $lines_stats->{pkt}{user_in}+=$l_packets;
  176. }
  177. my $auth_id;
  178. #save full packet
  179. if ($save_detail) {
  180. if (($user_ip and $user_stats{$user_ip}{save_traf}) or (!$auth_id and $config_ref{save_detail})) {
  181. if ($user_ip) { $auth_id = $users->match_string($user_ip); }
  182. if (!$auth_id) { $auth_id = 0; }
  183. push(@{$lines_stats->{sql}},"INSERT INTO Traffic_detail (auth_id,router_id,timestamp,proto,src_ip,dst_ip,src_port,dst_port,bytes,pkt) VALUES($auth_id,$router_id,$full_time,'$l_proto',$l_src_ip_aton,$l_dst_ip_aton,'$l_src_port','$l_dst_port','$l_bytes','$l_packets')");
  184. }
  185. }
  186. if ($auth_id) { next; }
  187. if (!$config_ref{add_unknown_user}) { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  188. #add user by src ip only if dst not office network!!!!
  189. #ignore dst traffic for create user
  190. if (!$office_networks->match_string($l_dst_ip) and $office_networks->match_string($l_src_ip)) {
  191. $user_ip = $l_src_ip;
  192. $users->add_string($user_ip,0);
  193. }
  194. if (!$user_ip) { $lines_stats->{line}{illegal}++; $lines_stats->{pkt}{illegal}+=$l_packets; next; }
  195. if ($user_ip eq $l_src_ip) {
  196. $lines_stats->{stats}{$user_ip}{ip}=$user_ip;
  197. $lines_stats->{stats}{$user_ip}{auth_id}= 0;
  198. if (!$lines_stats->{stats}{$user_ip}{out}) { $lines_stats->{stats}{$user_ip}{out}=0; }
  199. if (!$lines_stats->{stats}{$user_ip}{pkt_out}) { $lines_stats->{stats}{$user_ip}{pkt_out}=0; }
  200. $lines_stats->{stats}{$user_ip}{out} += $l_bytes;
  201. $lines_stats->{stats}{$user_ip}{pkt_out} +=$l_packets;
  202. $lines_stats->{line}{user}++;
  203. $lines_stats->{pkt}{user_out}+=$l_packets;
  204. }
  205. }
  206. $f_dbh->disconnect;
  207. return $lines_stats;
  208. }
  209. my $m_dbh=init_db();
  210. ####################################################################################################
  211. if (!$last_time) { $last_time = localtime(); }
  212. #start hour
  213. my ($min,$hour,$day,$month,$year) = (localtime($last_time))[1,2,3,4,5];
  214. #flow time
  215. my $flow_date = $m_dbh->quote(sprintf "%04d-%02d-%02d %02d:%02d:00",$year+1900,$month+1,$day,$hour,$min);
  216. #start stat time
  217. my $hour_date1 = $m_dbh->quote(sprintf "%04d-%02d-%02d %02d:00:00",$year+1900,$month+1,$day,$hour);
  218. #end hour
  219. ($hour,$day,$month,$year) = (localtime($last_time+3600))[2,3,4,5];
  220. my $hour_date2 = $m_dbh->quote(sprintf "%04d-%02d-%02d %02d:00:00",$year+1900,$month+1,$day,$hour);
  221. # update database
  222. foreach my $user_ip (keys %user_stats) {
  223. my $user_ip_aton=StrToIp($user_ip);
  224. my $auth_id = $user_stats{$user_ip}{auth_id};
  225. if (!$auth_id) {
  226. $auth_id=new_auth($m_dbh,$user_ip);
  227. $user_stats{$user_ip}{auth_id}=$auth_id;
  228. #fix traffic detail for new users
  229. push(@batch_sql_traf,"UPDATE Traffic_detail set auth_id=$auth_id WHERE auth_id=0 AND `timestamp`>='$hour_date1' AND `timestamp`<'$hour_date2' AND (src_ip=$user_ip_aton OR dst_ip=$user_ip_aton)");
  230. }
  231. #skip empty stats
  232. if ($user_stats{$user_ip}{in} + $user_stats{$user_ip}{out} ==0) { next; }
  233. #current stats
  234. my $tSQL="INSERT INTO User_stats_full (timestamp,auth_id,router_id,byte_in,byte_out,pkt_in,pkt_out,step) VALUES($flow_date,'$auth_id','$router_id','$user_stats{$user_ip}{in}','$user_stats{$user_ip}{out}','$user_stats{$user_ip}{pkt_in}','$user_stats{$user_ip}{pkt_out}','$timeshift')";
  235. push (@batch_sql_traf,$tSQL);
  236. #last found timestamp
  237. $tSQL="UPDATE User_auth SET `last_found`=$flow_date WHERE id='$auth_id'";
  238. push (@batch_sql_traf,$tSQL);
  239. #hour stats
  240. # get current stats
  241. my $sql = "SELECT id, byte_in, byte_out FROM User_stats WHERE `timestamp`>=$hour_date1 AND `timestamp`<$hour_date2 AND router_id=$router_id AND auth_id=$auth_id";
  242. my $hour_stat = get_record_sql($m_dbh,$sql);
  243. if (!$hour_stat) {
  244. my $dSQL="INSERT INTO User_stats (timestamp,auth_id,router_id,byte_in,byte_out) VALUES($flow_date,'$auth_id','$router_id','$user_stats{$user_ip}{in}','$user_stats{$user_ip}{out}')";
  245. push (@batch_sql_traf,$dSQL);
  246. next;
  247. }
  248. if (!$hour_stat->{byte_in}) { $hour_stat->{byte_in}=0; }
  249. if (!$hour_stat->{byte_out}) { $hour_stat->{byte_out}=0; }
  250. $hour_stat->{byte_in} += $user_stats{$user_ip}{in};
  251. $hour_stat->{byte_out} += $user_stats{$user_ip}{out};
  252. $tSQL="UPDATE User_stats SET byte_in='".$hour_stat->{byte_in}."', byte_out='".$hour_stat->{byte_out}."' WHERE id=".$auth_id;
  253. push (@batch_sql_traf,$tSQL);
  254. }
  255. print Dumper(\@batch_sql_traf);
  256. batch_db_sql($m_dbh,\@batch_sql_traf);
  257. db_log_debug($m_dbh,"Import traffic from router id: $router_id stop") if ($debug);
  258. db_log_verbose($m_dbh,"Recalc quotes started");
  259. recalc_quotes($m_dbh,$router_id);
  260. db_log_verbose($m_dbh,"Recalc quotes stopped");
  261. db_log_verbose($m_dbh,"router id: $router_id stop Traffic statistics, lines: all => $stats{line}{all}, user=> $stats{line}{user}, free => $stats{line}{free}, illegal=> $stats{line}{illegal}");
  262. db_log_verbose($m_dbh,sprintf("router id: %d stop Traffic speed, line/s: all => %.2f, user=> %.2f, free => %.2f, unknown=> %.2f", $router_id, $stats{line}{all}/$timeshift, $stats{line}{user}/$timeshift, $stats{line}{free}/$timeshift, $stats{line}{illegal}/$timeshift));
  263. db_log_verbose($m_dbh,"router id: $router_id stop Traffic statistics, pkt: all => $stats{pkt}{all}, user_in=> $stats{pkt}{user_in}, user_in=> $stats{pkt}{user_out}, free => $stats{pkt}{free}, illegal=> $stats{pkt}{illegal}");
  264. db_log_verbose($m_dbh,sprintf("router id: %d stop Traffic speed, pkt/s: all => %.2f, user_in=> %.2f, user_out=> %.2f, free => %.2f, unknown=> %.2f", $router_id, $stats{pkt}{all}/$timeshift, $stats{pkt}{user_in}/$timeshift, $stats{pkt}{user_out}/$timeshift, $stats{pkt}{free}/$timeshift, $stats{pkt}{illegal}/$timeshift));
  265. $m_dbh->disconnect;
  266. exit 0;