eye-statd.pl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. #!/usr/bin/perl -w
  2. use utf8;
  3. use open ":encoding(utf8)";
  4. use Encode;
  5. no warnings 'utf8';
  6. use English;
  7. use base;
  8. use FindBin '$Bin';
  9. use lib "/opt/Eye/scripts";
  10. use strict;
  11. use DBI;
  12. use Time::Local;
  13. use Net::Patricia;
  14. #use Data::Dumper;
  15. use Date::Parse;
  16. use DateTime;
  17. use eyelib::config;
  18. use eyelib::main;
  19. use eyelib::net_utils;
  20. use eyelib::database;
  21. use eyelib::snmp;
  22. use Socket qw(AF_INET6 inet_ntop);
  23. use IO::Socket;
  24. my $proc_name = $MY_NAME;
  25. $proc_name =~ s/\.[^.]+$//;
  26. my $pid_file = '/run/eye/'.$proc_name;
  27. my @router_ref = ();
  28. my @interfaces = ();
  29. my %mute;
  30. my %routers_svi;
  31. my %routers_by_ip;
  32. my %routers;
  33. my %wan_dev;
  34. my %lan_dev;
  35. my @traffic = ();
  36. my $saving = 0;
  37. #user statistics for cached data
  38. my %user_stats;
  39. my %wan_stats;
  40. my $MAXREAD = 9216;
  41. my $timeshift = get_option($dbh,55)*60;
  42. my $save_path = get_option($dbh,72);
  43. my $thread_count = $cpu_count;
  44. #save traffic to DB
  45. my $traf_lastflush = time();
  46. # NetFlow
  47. my $server_port = 2055;
  48. my $netflow5_header_len = 24;
  49. my $netflow5_flowrec_len = 48;
  50. my $netflow9_header_len = 20;
  51. my $netflow9_templates = {};
  52. # reap dead children
  53. $SIG{CHLD} = \&REAPER;
  54. $SIG{TERM} = \&TERM;
  55. $SIG{INT} = \&TERM;
  56. $SIG{HUP} = \&INIT;
  57. if (IsNotRun($pid_file)) {
  58. Add_PID($pid_file);
  59. } else {
  60. print "Daemon $MY_NAME already running!\n";
  61. exit 100;
  62. }
  63. sub REAPER {
  64. wait;
  65. $saving = 0;
  66. $SIG{CHLD} = \&REAPER;
  67. }
  68. sub TERM {
  69. print "SIGTERM received\n";
  70. flush_traffic(1);
  71. while (wait() != -1) {}
  72. if (IsMyPID($pid_file)) { Remove_PID($pid_file); }
  73. exit 0;
  74. }
  75. sub INIT {
  76. # Create new database handle. If we can't connect, die()
  77. my $hdb = init_db();
  78. InitSubnets();
  79. init_option($hdb);
  80. #a directory for storing traffic details in text form
  81. $save_path = get_option($dbh,72);
  82. #the period for resetting statistics from netflow to billing
  83. $timeshift = get_option($hdb,55)*60;
  84. @router_ref = get_records_sql($hdb,"SELECT * FROM devices WHERE deleted=0 AND device_type=2 AND snmp_version>0 ORDER by ip" );
  85. @interfaces = get_records_sql($hdb,"SELECT * FROM `device_l3_interfaces` ORDER by device_id" );
  86. #router device_id by known device ip
  87. foreach my $row (@router_ref) {
  88. setCommunity($row);
  89. $routers{$row->{id}}=$row;
  90. my $l3_list = getIpAdEntIfIndex($row->{ip},$row->{snmp});
  91. #create hash for interface snmp index => ip-address at interface =1;
  92. foreach my $router_ip (keys %$l3_list) { $routers_svi{$row->{id}}{$l3_list->{$router_ip}}{$router_ip}=1; }
  93. #create hash by all ip-addresses for router
  94. foreach my $router_ip (keys %$l3_list) {
  95. $routers_by_ip{$router_ip}->{id}=$row->{id};
  96. if ($config_ref{save_detail}) {
  97. $routers_by_ip{$router_ip}->{save}=$row->{netflow_save};
  98. } else { $routers_by_ip{$router_ip}->{save}=0; }
  99. }
  100. }
  101. #snmp index for WAN/LAN interface by device id
  102. foreach my $row (@interfaces) {
  103. if ($row->{interface_type}) { $wan_dev{$row->{device_id}}{$row->{snmpin}}=1; } else { $lan_dev{$row->{device_id}}{$row->{snmpin}}=1; }
  104. }
  105. #get userid list
  106. my @auth_list_ref = get_records_sql($hdb,"SELECT id,ip,save_traf FROM User_auth where deleted=0 ORDER by id");
  107. foreach my $row (@auth_list_ref) {
  108. $user_stats{$row->{ip}}{auth_id}=$row->{id};
  109. if ($config_ref{save_detail}) {
  110. $user_stats{$row->{ip}}{save_traf}=$row->{save_traf};
  111. } else {
  112. $user_stats{$row->{ip}}{save_traf}=0;
  113. }
  114. }
  115. $hdb->disconnect();
  116. }
  117. ############### MAIN ##########################
  118. #close default database
  119. $dbh->disconnect();
  120. INIT();
  121. my $lsn_nflow;
  122. my $sel = IO::Select->new();
  123. # prepare to listen for NetFlow UDP packets
  124. if ($server_port > 0) {
  125. $lsn_nflow = IO::Socket::INET->new(LocalPort => $server_port, Proto => "udp")
  126. or die "Couldn't be a NetFlow UDP server on port $server_port : $@\n";
  127. $sel->add($lsn_nflow);
  128. }
  129. my ($him,$datagram,$flags);
  130. # main datagram receive loop
  131. while (1) {
  132. while (my @ready = $sel->can_read) {
  133. foreach my $server (@ready) {
  134. $him = $server->recv($datagram, $MAXREAD);
  135. next if (!$him);
  136. my ($port, $ipaddr) = sockaddr_in($server->peername);
  137. if (defined($lsn_nflow) && $server == $lsn_nflow) {
  138. my ($version) = unpack("n", $datagram);
  139. if ($version == 5) {
  140. parse_netflow_v5($datagram, $ipaddr);
  141. } elsif ($version == 9) {
  142. parse_netflow_v9($datagram, $ipaddr);
  143. } else {
  144. print "unknown NetFlow version: $version\n";
  145. }
  146. }
  147. }
  148. }
  149. }
  150. sub parse_netflow_v5 {
  151. my $datagram = shift;
  152. my $ipaddr = shift;
  153. my ($version, $count, $sysuptime, $unix_secs, $unix_nsecs,
  154. $flow_sequence, $engine_type, $engine_id, $aggregation,
  155. $agg_version) = unpack("nnNNNNCCCC", $datagram);
  156. my $flowrecs = substr($datagram, $netflow5_header_len);
  157. #0 - N 0-3 srcaddr Source IP address
  158. #1 - N 4-7 dstaddr Destination IP address
  159. #2 - N 8-11 nexthop IP address of next hop router
  160. #3 - n 12-13 input SNMP index of input interface
  161. #4 - n 14-15 output SNMP index of output interface
  162. #5 - N 16-19 dPkts Packets in the flow
  163. #6 - N 20-23 dOctets Total number of Layer 3 bytes in the packets of the flow
  164. #7 - N 24-27 First SysUptime at start of flow
  165. #8 - N 28-31 Last SysUptime at the time the last packet of the flow was received
  166. #9 - n 32-33 src_port TCP/UDP source port number or equivalent
  167. #10- n 34-35 dst_port TCP/UDP destination port number or equivalent
  168. #11- C 36 pad1 Unused (zero) byte
  169. #12- C 37 tcp_flags Cumulative OR of TCP flags
  170. #13- C 38 prot IP protocol type (for example, TCP = 6; UDP = 17)
  171. #14- C 39 tos IP type of service (ToS)
  172. #15- n 40-41 src_as Autonomous system number of the source, either origin or peer
  173. #16- n 42-43 dst_as Autonomous system number of the destination, either origin or peer
  174. #17- C 44 src_mask Source address prefix mask bits
  175. #18- C 45 dst_mask Destination address prefix mask bits
  176. #19- n 46-47 pad2 Unused (zero) bytes
  177. for (my $i = 0; $i < $count; $i++) {
  178. my $flowrec = substr($datagram, $netflow5_header_len + ($i*$netflow5_flowrec_len), $netflow5_flowrec_len);
  179. my @flowdata = unpack("NNNnnNNNNnnCCCCnnCCn", $flowrec);
  180. my %flow;
  181. $flow{src_ip} = join '.', unpack 'C4', pack 'N', $flowdata[0];
  182. $flow{dst_ip} = join '.', unpack 'C4', pack 'N', $flowdata[1];
  183. $flow{snmp_in} = $flowdata[3] || 0;
  184. $flow{snmp_out} = $flowdata[4] || 0;
  185. $flow{pkts} = $flowdata[5] || 0;
  186. $flow{octets} = $flowdata[6] || 0;
  187. $flow{src_port} = $flowdata[9] || 0;
  188. $flow{dst_port} = $flowdata[10] || 0;
  189. $flow{proto} = $flowdata[13] || 0;
  190. $flow{xsrc_ip} = $flow{src_ip};
  191. $flow{xdst_ip} = $flow{dst_ip};
  192. $flow{starttime} = time();
  193. $flow{netflow_v} = '5';
  194. $flow{ipv} = '4';
  195. save_flow($ipaddr, \%flow);
  196. }
  197. }
  198. sub parse_netflow_v9 {
  199. my $datagram = shift;
  200. my $ipaddr = shift;
  201. # Parse packet
  202. my ($version, $count, $sysuptime, $unix_secs, $seqno, $source_id, @flowsets) = unpack("nnNNNN(nnX4/a)*", $datagram);
  203. # Loop through FlowSets and take appropriate action
  204. for (my $i = 0; $i < scalar @flowsets; $i += 2) {
  205. my $flowsetid = $flowsets[$i];
  206. my $flowsetdata = substr($flowsets[$i+1], 4); # chop off id/length
  207. if ($flowsetid == 0) {
  208. # 0 = Template FlowSet
  209. parse_netflow_v9_template_flowset($flowsetdata, $ipaddr, $source_id);
  210. } elsif ($flowsetid == 1) {
  211. # 1 - Options Template FlowSet
  212. } elsif ($flowsetid > 255) {
  213. # > 255: Data FlowSet
  214. parse_netflow_v9_data_flowset($flowsetid, $flowsetdata, $ipaddr, $source_id);
  215. } else {
  216. # reserved FlowSet
  217. print "Unknown FlowSet ID $flowsetid found\n";
  218. }
  219. }
  220. }
  221. sub parse_netflow_v9_template_flowset {
  222. my $templatedata = shift;
  223. my $ipaddr = shift;
  224. my $source_id = shift;
  225. # Note: there may be multiple templates in a Template FlowSet
  226. my @template_ints = unpack("n*", $templatedata);
  227. my $i = 0;
  228. while ($i < scalar @template_ints) {
  229. my $template_id = $template_ints[$i];
  230. my $fldcount = $template_ints[$i+1];
  231. last if (!defined($template_id) || !defined($fldcount));
  232. # print "Updated template ID $template_id (source ID $source_id, from " . inet_ntoa($ipaddr) . ")\n" if ($debug);
  233. my $template = [@template_ints[($i+2) .. ($i+2+$fldcount*2-1)]];
  234. $netflow9_templates->{$ipaddr}->{$source_id}->{$template_id}->{'template'} = $template;
  235. # total length of template data
  236. my $totallen = 0;
  237. for (my $j = 1; $j < scalar @$template; $j += 2) {
  238. $totallen += $template->[$j];
  239. }
  240. $netflow9_templates->{$ipaddr}->{$source_id}->{$template_id}->{'len'} = $totallen;
  241. $i += (2 + $fldcount*2);
  242. }
  243. }
  244. sub parse_netflow_v9_data_flowset {
  245. my $flowsetid = shift;
  246. my $flowsetdata = shift;
  247. my $ipaddr = shift;
  248. my $source_id = shift;
  249. my $template = $netflow9_templates->{$ipaddr}->{$source_id}->{$flowsetid}->{'template'};
  250. if (!defined($template)) {
  251. # print "Template ID $flowsetid from $source_id/" . inet_ntoa($ipaddr) . " does not (yet) exist\n" if ($debug);
  252. return;
  253. }
  254. # Flowset record types
  255. #define NF9_IN_BYTES 1
  256. #define NF9_IN_PACKETS 2
  257. #define NF9_IN_PROTOCOL 4
  258. #define NF9_L4_SRC_PORT 7
  259. #define NF9_IPV4_SRC_ADDR 8
  260. #define NF9_INPUT_SNMP 10
  261. #define NF9_L4_DST_PORT 11
  262. #define NF9_IPV4_DST_ADDR 12
  263. #define NF9_OUTPUT_SNMP 14
  264. #define NF9_OUT_BYTES 23
  265. #define NF9_OUT_PKTS 24
  266. #define NF9_DIRECTION 61
  267. #define NF_F_XLATE_SRC_ADDR_IPV4 225
  268. #define NF_F_XLATE_DST_ADDR_IPV4 226
  269. #define NF_F_XLATE_SRC_PORT 227
  270. #define NF_F_XLATE_DST_PORT 228
  271. #define NF9_IPV6_SRC_ADDR 27
  272. #define NF9_IPV6_DST_ADDR 28
  273. #define NF_F_XLATE_SRC_ADDR_IPV6 281
  274. #define NF_F_XLATE_DST_ADDR_IPV6 282
  275. my $len = $netflow9_templates->{$ipaddr}->{$source_id}->{$flowsetid}->{'len'};
  276. my $offset = 0;
  277. my $datalen = length($flowsetdata);
  278. while (($offset + $len) <= $datalen) {
  279. my %flow;
  280. $flow{netflow_v} = '9';
  281. $flow{ipv} = '4';
  282. $flow{starttime} = time();
  283. for (my $i = 0; $i < scalar @$template; $i += 2) {
  284. my $field_type = $template->[$i];
  285. my $field_length = $template->[$i+1];
  286. my $value = substr($flowsetdata, $offset, $field_length);
  287. $offset += $field_length;
  288. # IN_BYTES
  289. if ($field_type == 1) {
  290. if ($field_length == 4) {
  291. $flow{octets} = unpack("N", $value);
  292. } elsif ($field_length == 8) {
  293. $flow{octets} = unpack("Q>", $value);
  294. }
  295. }
  296. # IN_PACKETS
  297. elsif ($field_type == 2) {
  298. if ($field_length == 4) {
  299. $flow{pkts} = unpack("N", $value);
  300. } elsif ($field_length == 8) {
  301. $flow{pkts} = unpack("Q>", $value);
  302. }
  303. }
  304. # IN_PROTOCOL
  305. elsif ($field_type == 4) { $flow{proto} = unpack("C", $value); }
  306. # L4_SRC_PORT
  307. elsif ($field_type == 7) { $flow{src_port} = unpack("n", $value); }
  308. # IPV4_SRC_ADDR
  309. elsif ($field_type == 8) { $flow{src_ip} = inet_ntop(AF_INET, $value); }
  310. # INPUT_SNMP
  311. elsif ($field_type == 10) {
  312. if ($field_length == 2) {
  313. $flow{snmp_in} = unpack("n", $value);
  314. } elsif ($field_length == 4) {
  315. $flow{snmp_in} = unpack("N", $value);
  316. }
  317. }
  318. # L4_DST_PORT
  319. elsif ($field_type == 11) { $flow{dst_port} = unpack("n", $value); }
  320. # IPV4_DST_ADDR
  321. elsif ($field_type == 12) { $flow{dst_ip} = inet_ntop(AF_INET, $value); }
  322. # OUTPUT_SNMP
  323. elsif ($field_type == 14) {
  324. if ($field_length == 2) {
  325. $flow{snmp_out} = unpack("n", $value);
  326. } elsif ($field_length == 4) {
  327. $flow{snmp_out} = unpack("N", $value);
  328. }
  329. }
  330. # IP_PROTOCOL_VERSION
  331. elsif ($field_type == 60) { my $ipversion = unpack("C", $value);
  332. #skip ipv6
  333. if ($ipversion == 6) { %flow=(); last; }
  334. }
  335. # XLATE_SRC_ADDR_IPV4
  336. elsif ($field_type == 225) { $flow{xsrc_ip} = inet_ntop(AF_INET, $value); }
  337. # XLATE_DST_ADDR_IPV4
  338. elsif ($field_type == 226) { $flow{xdst_ip} = inet_ntop(AF_INET, $value); }
  339. }
  340. $flow{snmp_in} = 0 if (!$flow{snmp_in});
  341. $flow{snmp_out} = 0 if (!$flow{snmp_out});
  342. $flow{octets} = 0 if (!$flow{octets});
  343. $flow{pkts} = 0 if (!$flow{pkts});
  344. if (%flow) { save_flow($ipaddr, \%flow); }
  345. }
  346. }
  347. sub save_flow {
  348. my $router_ip = shift;
  349. my $flow = shift;
  350. $router_ip = inet_ntoa($router_ip);
  351. #direction for user, 0 - in, 1 - out
  352. $flow->{direction} = '0';
  353. my $router_id;
  354. #skip unknown router
  355. if (exists $routers_by_ip{$router_ip}) {
  356. $router_id = $routers_by_ip{$router_ip}{id};
  357. $flow->{router_ip} = $router_ip;
  358. $flow->{device_id} = $router_id;
  359. $flow->{save} = $routers_by_ip{$router_ip}{save};
  360. } else {
  361. if (!exists $mute{$router_ip}) { $mute{$router_ip} = time(); }
  362. if (time() - $mute{$router_ip} >=3600) {
  363. $mute{$router_ip} = time();
  364. log_warning("Found unknown router ip [".$router_ip."] in netflow!");
  365. }
  366. return;
  367. }
  368. #skip local traffic for router
  369. if (!exists $wan_dev{$router_id}->{$flow->{snmp_out}} and ! exists $wan_dev{$router_id}->{$flow->{snmp_in}}) { return; }
  370. #detect traffic direction
  371. if (exists $wan_dev{$router_id}->{$flow->{snmp_out}}) { $flow->{direction} = 1; }
  372. push(@traffic,$flow);
  373. flush_traffic(0);
  374. }
  375. sub flush_traffic {
  376. my $force = shift || 0;
  377. if (!$force && ($saving || ((time - $traf_lastflush) < $timeshift))) { return; }
  378. $saving++;
  379. my $pid = fork();
  380. INIT();
  381. if (!defined $pid) {
  382. $saving = 0;
  383. print "cannot fork! Save traffic and exit...\n";
  384. } elsif ($pid != 0) {
  385. # in parent
  386. $traf_lastflush = time();
  387. #clean main cache
  388. @traffic = ();
  389. return;
  390. }
  391. #create oper-cache
  392. my @flush_table = ();
  393. push(@flush_table,@traffic);
  394. my $hdb=init_db();
  395. #saved packet by users
  396. my @detail_traffic = ();
  397. my %saved_netflow = ();
  398. my %routers_found;
  399. #last packet timestamp
  400. my $last_time = time();
  401. my $start_time;
  402. foreach my $traf_record (@flush_table) {
  403. my ($auth_id,$l_src_ip,$l_dst_ip,$user_ip,$router_id);
  404. #print Dumper($traf_record) if ($debug);
  405. #skip unknown router
  406. next if (!$traf_record->{device_id});
  407. $router_id = $traf_record->{device_id};
  408. #prepare router traffic detailization data only if traffic retention is enabled globally
  409. if ($config_ref{save_detail} and $traf_record->{save}) {
  410. push(@{$saved_netflow{$traf_record->{device_id}}},join(';',$traf_record->{starttime},$traf_record->{proto},$traf_record->{snmp_in},$traf_record->{snmp_out},$traf_record->{src_ip},$traf_record->{dst_ip},$traf_record->{xsrc_ip},$traf_record->{xdst_ip},$traf_record->{src_port},$traf_record->{dst_port},$traf_record->{octets},$traf_record->{pkts}));
  411. }
  412. $routers_found{$router_id} = 1;
  413. #save start netflow time
  414. if (!$start_time) { $start_time = $traf_record->{starttime}; }
  415. #--- router statistics
  416. #input traffic and traffic originated from router
  417. if (!$traf_record->{snmp_out} or !$traf_record->{snmp_in}) {
  418. #input
  419. if (!$traf_record->{snmp_out} and exists $routers_svi{$router_id}{$traf_record->{snmp_in}}{$traf_record->{dst_ip}}) {
  420. #input
  421. if (!$free_networks->match_string($traf_record->{src_ip})) {
  422. if (exists $wan_stats{$router_id}{$traf_record->{snmp_in}}{in}) {
  423. $wan_stats{$router_id}{$traf_record->{snmp_in}}{in}+=$traf_record->{octets};
  424. } else {
  425. $wan_stats{$router_id}{$traf_record->{snmp_in}}{in}=$traf_record->{octets};
  426. }
  427. }
  428. next;
  429. }
  430. #output
  431. if (!$traf_record->{snmp_in} and exists $routers_svi{$router_id}{$traf_record->{snmp_out}}{$traf_record->{src_ip}}) {
  432. #output
  433. if (!$free_networks->match_string($traf_record->{dst_ip})) {
  434. if (exists $wan_stats{$router_id}{$traf_record->{snmp_out}}{out}) {
  435. $wan_stats{$router_id}{$traf_record->{snmp_out}}{out}+=$traf_record->{octets};
  436. } else {
  437. $wan_stats{$router_id}{$traf_record->{snmp_out}}{out}=$traf_record->{octets};
  438. }
  439. }
  440. next;
  441. }
  442. #unknown packet
  443. next;
  444. }
  445. #simple output traffic from router
  446. if (exists $wan_dev{$router_id}->{$traf_record->{snmp_out}} and exists $wan_dev{$router_id}->{$traf_record->{snmp_in}}) {
  447. if (exists $routers_svi{$router_id}{$traf_record->{snmp_out}}{$traf_record->{src_ip}}) {
  448. #output
  449. if (!$free_networks->match_string($traf_record->{dst_ip})) {
  450. if (exists $wan_stats{$router_id}{$traf_record->{snmp_out}}{out}) {
  451. $wan_stats{$router_id}{$traf_record->{snmp_out}}{out}+=$traf_record->{octets};
  452. } else {
  453. $wan_stats{$router_id}{$traf_record->{snmp_out}}{out}=$traf_record->{octets};
  454. }
  455. }
  456. next;
  457. }
  458. #It is unlikely that it will ever work out
  459. if (exists $routers_svi{$router_id}{$traf_record->{snmp_in}}{$traf_record->{dst_ip}}) {
  460. #input
  461. if (!$free_networks->match_string($traf_record->{src_ip})) {
  462. if (exists $wan_stats{$router_id}{$traf_record->{snmp_in}}{in}) {
  463. $wan_stats{$router_id}{$traf_record->{snmp_in}}{in}+=$traf_record->{octets};
  464. } else {
  465. $wan_stats{$router_id}{$traf_record->{snmp_in}}{in}=$traf_record->{octets};
  466. }
  467. }
  468. next;
  469. }
  470. #unknown packet
  471. next;
  472. } else {
  473. #forward
  474. if (!$free_networks->match_string($traf_record->{src_ip}) and !$free_networks->match_string($traf_record->{dst_ip})) {
  475. if ($traf_record->{direction}) {
  476. #out
  477. if (exists $wan_stats{$router_id}{$traf_record->{snmp_out}}{forward_out}) {
  478. $wan_stats{$router_id}{$traf_record->{snmp_out}}{forward_out}+=$traf_record->{octets};
  479. } else {
  480. $wan_stats{$router_id}{$traf_record->{snmp_out}}{forward_out}+=$traf_record->{octets};
  481. }
  482. } else {
  483. #in
  484. if (exists $wan_stats{$router_id}{$traf_record->{snmp_in}}{forward_in}) {
  485. $wan_stats{$router_id}{$traf_record->{snmp_in}}{forward_in}+=$traf_record->{octets};
  486. } else {
  487. $wan_stats{$router_id}{$traf_record->{snmp_in}}{forward_in}+=$traf_record->{octets};
  488. }
  489. }
  490. }
  491. }
  492. #--- user statistics
  493. my $free = 0;
  494. if ($traf_record->{direction}) {
  495. #outbound traffic
  496. if (exists $user_stats{$traf_record->{src_ip}}) {
  497. $user_ip = $traf_record->{src_ip};
  498. $l_src_ip = $traf_record->{src_ip};
  499. $l_dst_ip = $traf_record->{dst_ip};
  500. $free = $free_networks->match_string($l_dst_ip);
  501. #skip calculate free net
  502. if (!$free) {
  503. if (exists $user_stats{$user_ip}{$router_id}{out}) {
  504. $user_stats{$user_ip}{$router_id}{out}+=$traf_record->{octets};
  505. } else {
  506. $user_stats{$user_ip}{$router_id}{out}=$traf_record->{octets};
  507. }
  508. if (exists $user_stats{$user_ip}{$router_id}{pkt_out}) {
  509. $user_stats{$user_ip}{$router_id}{pkt_out}+=$traf_record->{pkts};
  510. } else {
  511. $user_stats{$user_ip}{$router_id}{pkt_out}=$traf_record->{pkts};
  512. }
  513. }
  514. }
  515. #a new user is created only by the presence of outgoing traffic
  516. if (!$user_ip and $config_ref{add_unknown_user}) {
  517. #skip create router interface as user
  518. if (exists $routers_by_ip{$traf_record->{src_ip}}) { next; }
  519. if (!$office_networks->match_string($traf_record->{src_ip})) {
  520. log_debug("Unknown src network at router $router_id:: int_in => $traf_record->{snmp_in} int_out=>$traf_record->{snmp_out} proto=>$traf_record->{proto} src: $traf_record->{src_ip}:$traf_record->{src_port} dst: $traf_record->{dst_ip}:$traf_record->{dst_port}");
  521. next;
  522. }
  523. $user_ip = $traf_record->{src_ip};
  524. $auth_id = new_auth($hdb,$user_ip);
  525. $l_src_ip = $traf_record->{src_ip};
  526. $l_dst_ip = $traf_record->{dst_ip};
  527. $user_stats{$user_ip}{auth_id}=$auth_id;
  528. $user_stats{$user_ip}{$router_id}{in}=0;
  529. $user_stats{$user_ip}{$router_id}{pkt_in}=0;
  530. $user_stats{$user_ip}{$router_id}{out}=0;
  531. $user_stats{$user_ip}{$router_id}{pkt_out}=0;
  532. $user_stats{$user_ip}{save_traf}=$config_ref{save_detail};
  533. $free = $free_networks->match_string($l_dst_ip);
  534. #skip calculate free net
  535. if (!$free) {
  536. $user_stats{$user_ip}{$router_id}{out}=$traf_record->{octets};
  537. $user_stats{$user_ip}{$router_id}{pkt_out}=$traf_record->{pkts};
  538. }
  539. }
  540. } else {
  541. #inbound traffic
  542. if (exists $user_stats{$traf_record->{xdst_ip}}) {
  543. $user_ip = $traf_record->{xdst_ip};
  544. $l_src_ip = $traf_record->{src_ip};
  545. $l_dst_ip = $traf_record->{xdst_ip};
  546. $free = $free_networks->match_string($l_src_ip);
  547. #skip calculate free net
  548. if (!$free) {
  549. if (exists $user_stats{$user_ip}{$router_id}{in}) {
  550. $user_stats{$user_ip}{$router_id}{in}+=$traf_record->{octets};
  551. } else {
  552. $user_stats{$user_ip}{$router_id}{in}=$traf_record->{octets};
  553. }
  554. if (exists $user_stats{$user_ip}{$router_id}{pkt_in}) {
  555. $user_stats{$user_ip}{$router_id}{pkt_in}+=$traf_record->{pkts};
  556. } else {
  557. $user_stats{$user_ip}{$router_id}{pkt_in}=$traf_record->{pkts};
  558. }
  559. }
  560. }
  561. if (!$user_ip) {
  562. log_debug("Unknown dst user ip at router $router_id:: int_in => $traf_record->{snmp_in} int_out=>$traf_record->{snmp_out} proto=>$traf_record->{proto} src: $traf_record->{src_ip}:$traf_record->{src_port} dst: $traf_record->{xdst_ip}:$traf_record->{dst_port}");
  563. }
  564. }
  565. next if (!$user_ip);
  566. $last_time = $traf_record->{starttime};
  567. $user_stats{$user_ip}{last_found} = $last_time;
  568. next if (!$config_ref{save_detail} and !$user_stats{$user_ip}{save_traf});
  569. my $l_src_ip_aton=StrToIp($l_src_ip);
  570. my $l_dst_ip_aton=StrToIp($l_dst_ip);
  571. my ($sec,$min,$hour,$day,$month,$year,$zone) = (localtime($last_time))[0,1,2,3,4,5];
  572. $month++;
  573. $year += 1900;
  574. my $full_time = sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  575. my @detail_array = ($user_stats{$user_ip}->{auth_id},$router_id,$full_time,$traf_record->{proto},$l_src_ip_aton,$l_dst_ip_aton,$traf_record->{src_port},$traf_record->{dst_port},$traf_record->{octets},$traf_record->{pkts});
  576. push(@detail_traffic,\@detail_array);
  577. }
  578. @flush_table=();
  579. #start hour
  580. my ($sec,$min,$hour,$day,$month,$year) = (localtime($last_time))[0,1,2,3,4,5];
  581. #save netflow
  582. if ($config_ref{save_detail}) {
  583. $save_path=~s/\/$//;
  584. foreach my $dev_id (keys %saved_netflow) {
  585. my $netflow_file_path = $save_path.'/'.$dev_id.'/'.sprintf "%04d/%02d/%02d/%02d/",$year+1900,$month+1,$day,$hour;
  586. my $nmin = int($min/10)*10;
  587. my $netflow_file_name = $netflow_file_path.sprintf "%04d%02d%02d-%02d%02d.csv",$year+1900,$month+1,$day,$hour,$nmin;
  588. if ($saved_netflow{$dev_id} and scalar @{$saved_netflow{$dev_id}}) {
  589. use File::Path;
  590. File::Path::make_path($netflow_file_path);
  591. if ( -e $netflow_file_name) {
  592. open (ND,">>$netflow_file_name") || die("Error open file $netflow_file_name!!! die...");
  593. binmode(ND,':utf8');
  594. } else {
  595. open (ND,">$netflow_file_name") || die("Error open file $netflow_file_name!!! die...");
  596. binmode(ND,':utf8');
  597. print ND join(';',"time","proto","snmp_in","snmp_out","src_ip","dst_ip","xsrc_ip","xdst_ip","src_port","dst_port","octets","pkts")."\n";
  598. }
  599. foreach my $row (@{$saved_netflow{$dev_id}}) {
  600. next if (!$row);
  601. print ND $row."\n";
  602. }
  603. close ND;
  604. @{$saved_netflow{$dev_id}}=();
  605. }
  606. }
  607. }
  608. undef %saved_netflow;
  609. #save statistics
  610. #start stat time
  611. my $hour_date1 = $hdb->quote(sprintf "%04d-%02d-%02d %02d:00:00",$year+1900,$month+1,$day,$hour);
  612. #end hour
  613. ($hour,$day,$month,$year) = (localtime($last_time+3600))[2,3,4,5];
  614. my $hour_date2 = $hdb->quote(sprintf "%04d-%02d-%02d %02d:00:00",$year+1900,$month+1,$day,$hour);
  615. my @batch_sql_traf=();
  616. #print Dumper(\%user_stats) if ($debug);
  617. # update database
  618. foreach my $user_ip (keys %user_stats) {
  619. next if (!exists $user_stats{$user_ip}{last_found});
  620. my $user_ip_aton=StrToIp($user_ip);
  621. my $auth_id = $user_stats{$user_ip}{auth_id};
  622. #last flow for user
  623. my ($sec,$min,$hour,$day,$month,$year) = (localtime($user_stats{$user_ip}{last_found}))[0,1,2,3,4,5];
  624. #flow time string
  625. my $flow_date = $hdb->quote(sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year+1900,$month+1,$day,$hour,$min,$sec);
  626. #last found timestamp
  627. my $tSQL="UPDATE User_auth SET `last_found`=$flow_date WHERE id='$auth_id'";
  628. push (@batch_sql_traf,$tSQL);
  629. #per router stats
  630. foreach my $router_id (keys %routers_found) {
  631. next if (!exists $user_stats{$user_ip}{$router_id});
  632. if (!exists $user_stats{$user_ip}{$router_id}{in}) { $user_stats{$user_ip}{$router_id}{in} = 0; }
  633. if (!exists $user_stats{$user_ip}{$router_id}{out}) { $user_stats{$user_ip}{$router_id}{out} = 0; }
  634. #skip empty stats
  635. if ($user_stats{$user_ip}{$router_id}{in} + $user_stats{$user_ip}{$router_id}{out} ==0) { next; }
  636. #packet count per router
  637. if (!exists $user_stats{$user_ip}{$router_id}{pkt_in}) { $user_stats{$user_ip}{$router_id}{pkt_in} = 0; }
  638. if (!exists $user_stats{$user_ip}{$router_id}{pkt_out}) { $user_stats{$user_ip}{$router_id}{pkt_out} = 0; }
  639. #current stats
  640. 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}{$router_id}{in}','$user_stats{$user_ip}{$router_id}{out}','$user_stats{$user_ip}{$router_id}{pkt_in}','$user_stats{$user_ip}{$router_id}{pkt_out}','$timeshift')";
  641. push (@batch_sql_traf,$tSQL);
  642. #hour stats
  643. # get current stats
  644. 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";
  645. my $hour_stat = get_record_sql($hdb,$sql);
  646. if (!$hour_stat) {
  647. 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}{$router_id}{in}','$user_stats{$user_ip}{$router_id}{out}')";
  648. push (@batch_sql_traf,$dSQL);
  649. next;
  650. }
  651. if (!$hour_stat->{byte_in}) { $hour_stat->{byte_in}=0; }
  652. if (!$hour_stat->{byte_out}) { $hour_stat->{byte_out}=0; }
  653. $hour_stat->{byte_in} += $user_stats{$user_ip}{$router_id}{in};
  654. $hour_stat->{byte_out} += $user_stats{$user_ip}{$router_id}{out};
  655. $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."'";
  656. push (@batch_sql_traf,$tSQL);
  657. }
  658. }
  659. #print Dumper(\%wan_stats) if ($debug);
  660. # update database
  661. foreach my $router_id (keys %wan_stats) {
  662. #last flow for user
  663. my ($sec,$min,$hour,$day,$month,$year) = (localtime($start_time))[0,1,2,3,4,5];
  664. #flow time string
  665. my $flow_date = $hdb->quote(sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year+1900,$month+1,$day,$hour,$min,$sec);
  666. #per interface stats
  667. foreach my $int_id (keys %{$wan_stats{$router_id}}) {
  668. if (!$wan_stats{$router_id}{$int_id}{in}) { $wan_stats{$router_id}{$int_id}{in} = 0; }
  669. if (!$wan_stats{$router_id}{$int_id}{out}) { $wan_stats{$router_id}{$int_id}{out} = 0; }
  670. if (!$wan_stats{$router_id}{$int_id}{forward_in}) { $wan_stats{$router_id}{$int_id}{forward_in} = 0; }
  671. if (!$wan_stats{$router_id}{$int_id}{forward_out}) { $wan_stats{$router_id}{$int_id}{forward_out} = 0; }
  672. #skip empty stats
  673. if ($wan_stats{$router_id}{$int_id}{in} + $wan_stats{$router_id}{$int_id}{out} + $wan_stats{$router_id}{$int_id}{forward_in} + $wan_stats{$router_id}{$int_id}{forward_out} ==0) { next; }
  674. #current stats
  675. my $tSQL="INSERT INTO Wan_stats (`time`,`router_id`,`interface_id`,`in`,`out`,`forward_in`,`forward_out`) VALUES($flow_date,'$router_id','$int_id','$wan_stats{$router_id}{$int_id}{in}','$wan_stats{$router_id}{$int_id}{out}','$wan_stats{$router_id}{$int_id}{forward_in}','$wan_stats{$router_id}{$int_id}{forward_out}')";
  676. push (@batch_sql_traf,$tSQL);
  677. }
  678. }
  679. #update statistics in DB
  680. batch_db_sql($hdb,\@batch_sql_traf);
  681. @batch_sql_traf = ();
  682. if ($config_ref{enable_quotes}) {
  683. db_log_debug($hdb,"Recalc quotes started");
  684. foreach my $router_id (keys %routers_found) { recalc_quotes($hdb,$router_id); }
  685. db_log_debug($hdb,"Recalc quotes stopped");
  686. }
  687. if (scalar(@detail_traffic)) {
  688. db_log_debug($hdb,"Start write traffic detail to DB. ".scalar @detail_traffic." lines count") if ($debug);
  689. #mysql dont work at parallel table lock
  690. batch_db_sql_csv("Traffic_detail", \@detail_traffic);
  691. @detail_traffic = ();
  692. db_log_debug($hdb,"Write traffic detail to DB stopped") if ($debug);
  693. }
  694. $hdb->disconnect();
  695. $saving = 0;
  696. exit;
  697. }
  698. if (IsMyPID($pid_file)) { Remove_PID($pid_file); }
  699. exit;