-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqhist
executable file
·58 lines (53 loc) · 1.65 KB
/
qhist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl
#
# Author: Robert Fielding <fieldingrp@gmail.com>
#
# Quick comandline version one-line histogram
#
# cat /var/log/system.log | perl -MDate::Parse -ne '($t) = ( /(\w{3}\s+\d+\s+\d{2}:\d{2}:\d{2})/ ); $ts = str2time($t); $int++; if ($ts-$p >= ($ri||3600) ||eof) { printf("%02d:%02d:%02d %10.2f %s\n", (localtime($p))[2,1,0], $int, "." x ($int/(1000/45))); $p=$ts; $int=0;}'
#
use strict;
use warnings;
use Date::Parse;
use Getopt::Long;
my $graph = 1000;
my $start;
my $end;
my $interval = 3600 ;
GetOptions (
"graph=i" => \$graph,
"start=i" => \$start,
"end=i" => \$end,
"interval=i" => \$interval);
my $counter = 0;
my $last = 0;
# snap start/end time to interval
$start = int($start / $interval) * $interval if $start;
$end = int($end / $interval) * $interval if $end;
sub mktimestamp { my ($str) = @_;
my $ts ;
# Mon DD HH:MM:SS, 2016-01-03T00:00:00
if ($str =~ /(\w{3}\s+\d+\s+\d{2}:\d{2}:\d{2}|\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\S+)/) {
$ts = str2time($1);
} elsif ($str =~ /(\d{10}[\.\d]*)/) {
$ts = $1;
}
return $ts;
}
while (my $line = <>) {
my $timestamp = mktimestamp($line);
next unless $timestamp;
$timestamp = int($timestamp / $interval) * $interval;
next if $start && $start > $timestamp;
last if $end && $end < $timestamp;
$counter ++;
if ($timestamp - $last >= $interval || eof) {
$timestamp += $interval if eof;
printf("%02d:%02d:%02d %10.2f %s\n",
(localtime(int($timestamp/$interval)*$interval))[2,1,0],
$counter,
'.' x ($counter/($graph/45)));
$last = $timestamp;
$counter = 0;
}
}