-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutextgraph
More file actions
executable file
·141 lines (119 loc) · 2.59 KB
/
utextgraph
File metadata and controls
executable file
·141 lines (119 loc) · 2.59 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/perl -w
use strict;
use warnings;
use Term::ANSIColor qw(:constants);
# simple txt bar graph v.2 :)
# (c) by Ender <ender@kofeina.net> 2010
binmode(STDOUT, ":utf8");
my $localmax = 8;
my $localmin = 0;
my $max=0;
my $min=100000000;
my $step=1;
my @vals;
my $Conf;
my $graph = {
0 => "\x{2581}",
1 => "\x{2581}",
2 => "\x{2582}",
3 => "\x{2583}",
4 => "\x{2584}",
5 => "\x{2585}",
6 => "\x{2586}",
7 => "\x{2587}",
8 => "\x{2588}",
};
# help
if(defined($ARGV[0]) && ($ARGV[0] eq '-h' || $ARGV[0] eq '--help')){
print qq{$0 (txt bar graph) by Ender
under GPL v2
Usage:
cat values | $0 [--c=2][--w=1][--time=1]
example:
ldavg-1
sar -q | awk '{print \$5}' | perl utextgraph --c=2
or (time val format)
sar -q | awk '{print \$1," ",\$5}' | perl utextgraph --c=2 --w=1 --time
};
exit;
};
if(defined($ARGV[0])){
@vals=@ARGV;
foreach(@vals){
if(/^--([^\s]+)\=([^\s]+)/){
$Conf->{$1} = $2;
};
if(/^--time/){
$Conf->{time} = 1;
}
};
};
#use Data::Dumper;
#print Dumper $Conf;
@vals=<STDIN>;
my @time_arr;
my @val_arr;
for my $val (@vals){
$val =~ s/,/./;
$val =~ s/\n//;
my $time = '';
# detect time:
if(defined($Conf->{time})){
if($val =~ /(^[0-9:]*\.*[0-9:]+)\s+([+-]?[0-9]*\.*[0-9]+)/){
$time = $1;
$val = $2;
push @time_arr, $time;
$max=$val if $val>$max;
$min=$val if $val<$min;
push @val_arr,$val if $val ne '';
};
}
else{
if($val =~ /^[+-]?[0-9]*\.*[0-9]+$/){
$max=$val if $val>$max;
$min=$val if $val<$min;
}
else{
$val='';
};
push @val_arr,$val if $val ne '';
};
};
$step=($max-$min)/$localmax;
print "Min: $min\tMax: ";
if(defined $Conf->{c} && $max>$Conf->{c}){
print BOLD.RED;
}
elsif(defined $Conf->{w} && $max>$Conf->{w}){
print BOLD.YELLOW;
};
print $max.RESET;
print "\tStep: $step\n";
for(@val_arr){
my $t = ($_-$min)/$step;
my $val = int(($_-$min)/$step);
if(defined $Conf->{c} && $_>$Conf->{c}){
print BOLD.RED;
}
elsif(defined $Conf->{w} && $_>$Conf->{w}){
print BOLD.YELLOW;
};
print $graph->{$val} . RESET;
};
print $/;
if($time_arr[0]){
for(my $i=0;$i<9;$i=$i+1){
for(@time_arr){
if(length($_)>$i){
print substr($_,$i,1);
}
else{
print " ";
};
};
print $/;
};
};
print $/;
# this software is licensed under the GNU General Public License version 2. For the
# full license information, please visit http://www.gnu.org/copyleft/gpl.html