#!/usr/bin/env perl
#
# Copyright: 2017 Sipwise Development Team <support@sipwise.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# On Debian systems, the complete text of the GNU General
# Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".

use 5.014;
use strict;
use warnings;
use English;
use Cwd 'abs_path';
use Getopt::Long;
use File::Spec;
use Text::CSV;
use Time::Piece;

# This script parses kamailio syslog file(<kamailio_log>) and produces:
# - <dest_dir>/<pid>_pkg.csv (appends the info if file exists)
# - <dest_dir>/<pid>_shm.csv (appends the info if file exists)
# - <dest_dir>/<module>_pkg_<pid>.csv (appends the info if file exists)

sub usage
{
  my $output = "usage: $PROGRAM_NAME [-h] <kamailio_log> <dest_dir>\n";
  $output .= "Options:\n";
  $output .= "\t-h: this help\n";
  $output .= "\tkamailio_log\n";
  $output .= "\tdest_dir\n";
  return $output
}

my $help = 0;
GetOptions ("h|help" => \$help)
  or die("Error in command line arguments\n".usage());

die("wrong number of parameters[@ARGV]\n".usage()) if (@ARGV != 2 || $help);

my $data = {};

my $filename = abs_path($ARGV[0]);
my $output_dir = abs_path($ARGV[1]);

die("$ARGV[1] doesn't exists") if (!$output_dir || ! -d $output_dir);

my $summary = {modules=>{}};

sub save_detail {
  my ($pid,$mem) = @_;
  my $path = File::Spec->catfile($output_dir, "${pid}_${mem}.csv");
  my $csv = Text::CSV->new({eol => "\n", quote_char => "'", binary => 1});

  return unless @{$data->{$pid}->{$mem}->{data}} > 0;
  open(my $fh, '>>', $path) or die "Can't write to '$path': $ERRNO";
  $csv->print($fh, $_) foreach @{$data->{$pid}->{$mem}->{data}};
  close($fh);
  print "[$pid] $path report saved\n";
  return;
}

sub save_summary {
  my $mem = shift;

  foreach my $pid (sort keys %{$data}) {
    my $data_pid = $data->{$pid}->{$mem};
    foreach my $mod (sort keys %{$data_pid}) {
      next if $mod eq 'data';
      my $path = File::Spec->catfile($output_dir, "${mod}_${mem}_${pid}.csv");
      my $csv = Text::CSV->new({eol => "\n", quote_char => "'", binary => 1});
      open(my $fh, '>>', $path) or die "Can't write to '$path': $ERRNO";
      my @row = (
        $data->{$pid}->{date},
        int($data_pid->{$mod}->{size}),
        int($data_pid->{$mod}->{count}),
      );
      $csv->print($fh, \@row);
      close($fh);
      print "$path report saved\n";
    }
  }
  return;
}

my ($date, $pid, $mem, $count, $size, $mod, $desc);
open(my $fh, '<', "$filename")
  or die "Couldn't open kamailio log '$filename', $ERRNO";
while (my $row = <$fh>) {
  if (($date, $pid, $mem) = ($row =~ m/^(\w{3}\s+\d{1,2} \d{2}:\d{2}:\d{2}).+proxy\[(\d+)\]:.+<core>.+: Memory still-in-use summary \((\w+)\).*$/))
  {
    my $t = Time::Piece->strptime($date, "%b %d %T");
    if (!exists $data->{$pid}) {
      print "[$pid] detected\n";
      $data->{$pid} = {
        type => $mem,
        date => $t->strftime("%d/%m %T"),
        pkg => {data => []},
        shm => {data => []},
      };
    } elsif (@{$data->{$pid}->{$mem}->{data}} > 0) {
      print "[$pid] detected but previous data is already collected.\n";
      save_detail($pid, 'pkg');
      save_detail($pid, 'shm');
      delete $data->{$pid};
      $data->{$pid} = {
        type => $mem,
        date => $t->strftime("%d/%m %T"),
        pkg => {data => []},
        shm => {data => []},
      };
    } else {
      print "[$pid] switch to $mem\n";
      $data->{$pid}->{type} = $mem;
    }
  }
  elsif (($date, $pid, $count, $size, $mod, $desc) = ($row =~ m/^(\w{3}\s+\d{1,2} \d{2}:\d{2}:\d{2}).+proxy\[(\d+)\]:.+fm_status:\s+count=\s+(\d+)\s+size=\s+(\d+) bytes from (\w+): (.+)$/))
  {
    my $t = Time::Piece->strptime($date, "%b %d %T");
    if (!exists $data->{$pid}) {
      print "[$pid] no data found. Skip line\n";
      next;
    }
    my $data_type = $data->{$pid}->{type};
    my $pid_info = $data->{$pid}->{$data_type};
    push @{$pid_info->{data}}, [
      $t->strftime("%d/%m %T"), $count, $size, $mod, $desc];
    next unless ($data_type eq 'pkg');
    if (!exists $pid_info->{$mod}) {
      $pid_info->{$mod} = { count => $count, size => $size };
      if (!exists $summary->{modules}->{$mod}) {
        $summary->{modules}->{$mod} = $size;
      }
    } else {
      $pid_info->{$mod}->{count} = $pid_info->{$mod}->{count} + $count;
      $pid_info->{$mod}->{size} = $pid_info->{$mod}->{size} + $size;
      $summary->{modules}->{$mod} = $summary->{modules}->{$mod} + $size;
    }
  }
}
close($fh);
my @pids = sort(keys %{$data});
foreach my $pid (@pids) {
  save_detail($pid, 'pkg');
  save_detail($pid, 'shm');
}
save_summary('pkg');
