captaincore/app/get/transferred-stats
2018-08-15 13:30:45 -04:00

128 lines
3.5 KiB
PHP
Executable file

#! /usr/bin/env php
<?php
#
# Extracts transferred stats from log files
#
# `captaincore get transferred-stats <file>`
#
// Assign file
if ( $argv && $argv[1] ) {
$file = $argv[1];
}
function secs_to_str( $duration ) {
$periods = array(
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1,
);
$parts = array();
foreach ( $periods as $name => $dur ) {
$div = floor( $duration / $dur );
if ( $div == 0 ) {
continue;
} elseif ( $div == 1 ) {
$parts[] = $div . ' ' . $name;
} else {
$parts[] = $div . ' ' . $name . 's';
}
$duration %= $dur;
}
$last = array_pop( $parts );
if ( empty( $parts ) ) {
return $last;
} else {
return join( ', ', $parts ) . ' and ' . $last;
}
}
if ( file_exists( $file ) ) {
$file = file_get_contents( $file );
// Bytes
$pattern = '/(\d.*)(?= Bytes )/';
preg_match_all( $pattern, $file, $matches );
$total_bytes = array_sum( $matches[0] );
// KBs
$pattern = '/(\d.*)(?= kBytes )/';
preg_match_all( $pattern, $file, $matches );
$total_kbytes = array_sum( $matches[0] );
// MBs
$pattern = '/(\d.*)(?= MBytes )/';
preg_match_all( $pattern, $file, $matches );
$total_mbytes = array_sum( $matches[0] );
// GBs
$pattern = '/(\d.*)(?= GBytes )/';
preg_match_all( $pattern, $file, $matches );
$total_gbytes = array_sum( $matches[0] );
// Add it all up
$total_gb = round( $total_bytes / 1024 / 1024 / 1024, 2 ) + round( $total_kbytes / 1024 / 1024, 2 ) + round( $total_mbytes / 1024, 2 ) + round( $total_gbytes, 2 );
// Errors
$pattern = '/Finished remote backup .+\nTransferred:.+\nErrors:.+(\d)/';
preg_match_all( $pattern, $file, $matches );
$total_errors = array_sum( $matches[1] );
// Checks
$pattern = '/Finished remote backup .+\nTransferred:.+\nErrors:.+\nChecks:.+(\d)/';
preg_match_all( $pattern, $file, $matches );
$total_checks = array_sum( $matches[1] );
// Transferred
$pattern = '/Finished remote backup .+\nTransferred:.+\nErrors:.+\nChecks:.+\nTransferred:.+(\d)/';
preg_match_all( $pattern, $file, $matches );
$total_transferred = array_sum( $matches[1] );
// Elapsed time
$pattern = '/Finished remote backup .+\nTransferred:.+\nErrors:.+\nChecks:.+\nTransferred:.+\nElapsed time:\s+(.+)/';
preg_match_all( $pattern, $file, $matches );
$elapsed_time = $matches[1];
$total_time_in_seconds = 0;
foreach ( $elapsed_time as $time ) {
if ( strpos( $time, 'ms' ) !== false ) {
// Search for ms
$pattern = '/(.+)(?:ms)/';
preg_match_all( $pattern, $time, $matches );
$micro_seconds = $matches[1][0] * 60 * 60;
$seconds = $micro_seconds / 1000;
} elseif ( strpos( $time, 'h' ) !== false ) {
// Search for hours
$pattern = '/(.+)(?:h)(.+)(?:m)(.+)(?:s)/';
preg_match_all( $pattern, $time, $matches );
$hours = $matches[1][0] * 60 * 60;
$minutes = $matches[2][0] * 60;
$seconds = $matches[3][0] + $hours + $minutes;
// Search for minutes
} elseif ( strpos( $time, 'm' ) !== false ) {
$pattern = '/(.+)(?:m)(.+)(?:s)/';
preg_match_all( $pattern, $time, $matches );
$minutes = $matches[1][0] * 60;
$seconds = $matches[2][0] + $minutes;
// Search for seconds
} elseif ( strpos( $time, 's' ) !== false ) {
$pattern = '/(.+)(?:s)/';
preg_match_all( $pattern, $time, $matches );
$seconds = $matches[1][0];
}
$total_time_in_seconds = $total_time_in_seconds + $seconds;
}
$total_time = secs_to_str( $total_time_in_seconds );
// return GBs transferred
echo $total_gb . ' GB - ' . $total_errors . ' errors - ' . $total_checks . " checks - $total_transferred transferred - $total_time";
}