"Fossies" - the Fresh Open Source Software Archive

Member "squidanalyzer-6.6/squid-analyzer" (7 May 2017, 6808 Bytes) of package /linux/privat/squidanalyzer-6.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "squid-analyzer": 6.5_vs_6.6.

    1 #!/usr/bin/env perl
    2 #
    3 # Perl frontend to SquidAnalyzer.pm.
    4 #
    5 use strict;
    6 use SquidAnalyzer;
    7 use Getopt::Long qw(:config no_ignore_case bundling);
    8 use Benchmark;
    9 use POSIX ":sys_wait_h";
   10 
   11 $| = 1;
   12 
   13 my $DEFAULT_CONFFILE = '/etc/squidanalyzer/squidanalyzer.conf';
   14 
   15 my @logfile    = ();
   16 my $obsolete   = 0;
   17 my $configfile = '';
   18 my $help       = '';
   19 my $rebuild    = '';
   20 my $preserve   = '';
   21 my $debug      = 0;
   22 my $version    = 0;
   23 my $build_date = '';
   24 my $pid_dir    = '/tmp';
   25 my $pidfile    = 'squid-analyzer.pid';
   26 my $queue_size = 0;
   27 my $timezone   = '';
   28 my $no_year_stat = 0;
   29 my $no_week_stat = 0;
   30 my $t0 = Benchmark->new;
   31 my $start_time = '';
   32 my $stop_time = '';
   33 
   34 # get the command line parameters
   35 my $result = GetOptions (
   36     "c|configfile=s"       => \$configfile,
   37     "b|build_date=s"       => \$build_date,
   38     "d|debug!"             => \$debug,
   39     "h|help"               => \$help,
   40     "j|jobs=i"             => \$queue_size,
   41     "l|logfile"            => \$obsolete,
   42     "p|preserve=i"         => \$preserve,
   43     "P|pid_dir=s"          => \$pid_dir,
   44     "r|rebuild!"           => \$rebuild,
   45     "s|start=s"            => \$start_time,
   46     "S|stop=s"             => \$stop_time,
   47     "t|timezone=s"         => \$timezone,
   48     "v|version!"           => \$version,
   49     "no-year-stat!"        => \$no_year_stat,
   50     "no-week-stat!"        => \$no_week_stat,
   51 ); 
   52 
   53 # Show warning for obsolete options
   54 if ($obsolete) {
   55     print STDERR "WARNING: use of obsolete -l option, log files must be given at command line arguments.\n"
   56 }
   57 
   58 # Show version and exit
   59 if ($version || $debug) {
   60     print "SquidAnalyzer version $SquidAnalyzer::VERSION\n";
   61     exit 0 if ($version);
   62 }
   63 
   64 if ($build_date) {
   65     $rebuild = 1;
   66     if ( ($build_date !~ /^\d{4}-\d{2}-\d{2}$/) && ($build_date !~ /^\d{4}-\d{2}$/) && ($build_date !~ /^\d{4}$/) ) {
   67         die("FATAL: bad syntax for build_date, expecting format: yyyy-mm-dd, yyyy-mm or yyyy\n");
   68         exit 0;
   69     }
   70 }
   71 
   72 if ($start_time && $start_time !~ /^[0-2]\d:[0-5]\d$/) {
   73     die("FATAL: bad format on start time, must be HH:MM.\n"); 
   74 }
   75 if ($stop_time && $stop_time !~ /^[0-2]\d:[0-5]\d$/) {
   76     die("FATAL: bad format on stop time, must be HH:MM.\n");
   77 }
   78 
   79 # Add multiple log files given from command line
   80 foreach my $f (@ARGV) {
   81     push(@logfile, $f) if (-f $f && !-z $f);
   82 }
   83 
   84 # Set default configuration file
   85 $configfile ||= $DEFAULT_CONFFILE;
   86 
   87 if ($help) {
   88     &usage;
   89     exit;
   90 }
   91 
   92 # Check if an other process is already running
   93 if (-e "$pid_dir/$pidfile") {
   94     die "FATAL: pid file ($pid_dir/$pidfile) exists, an other squid-analalyzer process may still running.\n";
   95 }
   96 
   97 # Write the pid file
   98 open(OUT, ">$pid_dir/$pidfile") or die "FATAL: can not write to pid file $pid_dir/$pidfile, $!\n";
   99 print OUT "$$";
  100 close(OUT);
  101 
  102 # Clear multiprocess temporary file if any
  103 unlink("$pid_dir/last_parsed.tmp");
  104 
  105 # Instanciate SquidAnalyzer.pm perl module
  106 my $sa = new SquidAnalyzer($configfile, join(',', @logfile), $debug, $rebuild, $pid_dir, $pidfile, $timezone);
  107 $sa->{no_year_stat} = $no_year_stat;
  108 $sa->{no_week_stat} = $no_week_stat;
  109 $sa->{queue_size} = $queue_size;
  110 $sa->{TimeStart} = $start_time;
  111 $sa->{TimeStop} = $stop_time;
  112 
  113 # Die cleanly on signal
  114 sub terminate
  115 {
  116         # block signal
  117         local $SIG{TERM} = 'IGNORE';
  118         local $SIG{INT}  = 'IGNORE';
  119 
  120         print("LOG: Received terminating signal.\n");
  121 
  122     $sa->{terminate} = 1;
  123 
  124     # Save last parse line
  125     $sa->save_current_line();
  126 
  127         # Wait for all child processes to die except for the logger
  128     my $kid = 0;
  129     do {
  130                 sleep(1);
  131         $kid = waitpid(-1, WNOHANG);
  132     } while ($kid > 0);
  133 
  134     # Removed pid iand temp file
  135         if (-e "$pid_dir/$pidfile") {
  136                 unlink("$pid_dir/$pidfile") or print("ERROR: Unable to remove pid file $pid_dir/$pidfile, $!\n");
  137     }
  138         if (-e "$pid_dir/last_parsed.tmp") {
  139                 unlink("$pid_dir/last_parsed.tmp") or print("ERROR: Unable to remove temp file $pid_dir/last_parsed.tmp, $!\n");
  140         }
  141 
  142         exit 0;
  143 }
  144 
  145 # Handle signals to die cleanly
  146 $SIG{'INT'} = \&terminate;
  147 $SIG{'TERM'} = \&terminate;
  148 $SIG{'CHLD'} = 'DEFAULT';
  149 
  150 my $t1;
  151 # Run parsing only if we have a log file or that we are not in rebuild mode
  152 if (!$rebuild || ($#{$sa->{LogFile}} >= 0)) {
  153     $sa->parseFile();
  154     if ($debug) {
  155         $t1 = Benchmark->new;
  156         my $td = timediff($t1, $t0);
  157         print STDERR "DEBUG: the log statistics gathering took:", timestr($td), "\n";
  158     }
  159 }
  160 
  161 # Remove old statistics
  162 if ($preserve) {
  163     $sa->{preserve} = $preserve;
  164 }
  165 
  166 # In rebuild mode history time is not use and we must store the
  167 # specific rebuild date if any is provided at command line.
  168 if ($rebuild) {
  169     $sa->{history_time} = '';
  170     $sa->{build_date} = $build_date;
  171 }
  172 
  173 # Generate graphics and html
  174 $sa->buildHTML();
  175 if ($debug) {
  176     my $t2 = Benchmark->new;
  177     if (defined $t1) {
  178         my $td = timediff($t2, $t1);
  179         print STDERR "DEBUG: generating HTML output took:", timestr($td), "\n";
  180     }
  181 
  182     my $td = timediff($t2, $t0);
  183     print STDERR "DEBUG: total execution time:", timestr($td), "\n";
  184 }
  185 
  186 # Remove PID file
  187 unlink("$pid_dir/$pidfile");
  188 
  189 exit(0);
  190 
  191 
  192 sub usage
  193 {
  194     print qq{
  195 Usage: squid-analyzer [ -c squidanalyzer.conf ] [logfile(s)]
  196 
  197     -c | --configfile filename : path to the SquidAnalyzer configuration file.
  198                  By default: $DEFAULT_CONFFILE
  199     -b | --build_date date     : set the date to be rebuilt, format: yyyy-mm-dd
  200                  or yyyy-mm or yyyy. Used with -r or --rebuild.
  201     -d | --debug               : show debug information.
  202     -h | --help                : show this message and exit.
  203     -j | --jobs number         : number of jobs to run at same time. Default
  204                                  is 1, run as single process.
  205     -p | --preserve number     : used to set the statistic obsolescence in
  206                  number of month. Older stats will be removed.
  207     -P | --pid_dir directory   : set directory where pid file will be stored.
  208                  Default /tmp/
  209     -r | --rebuild             : use this option to rebuild all html and graphs
  210                  output from all data files.
  211     -s | --start HH:MM         : log lines before this time will not be parsed.
  212     -S | --stop  HH:MM         : log lines after this time will not be parsed.
  213     -t | --timezone +/-HH      : set number of hours from GMT of the timezone.
  214                                  Use this to adjust date/time of SquidAnalyzer
  215                                  output when it is run on a different timezone
  216                                  than the squid server.
  217     -v | version               : show version and exit.
  218     --no-year-stat             : disable years statistics, reports will start
  219                                  from month level only.
  220     --no-week-stat             : disable weekly statistics.
  221 
  222 Log files to parse can be given as command line arguments or as a comma separated
  223 list of file for the LogFile configuration directive. By default SquidAnalyer will
  224 use file: /var/log/squid/access.log
  225 
  226 };
  227 
  228     exit 0;
  229 }
  230