1
0

parse_flow.pl 13 KB

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