eye-statd.pl 31 KB

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