File: rrd-percent.pl

package info (click to toggle)
remstats 1.0.13a-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,176 kB
  • ctags: 1,283
  • sloc: perl: 16,135; ansic: 2,776; sh: 1,061; makefile: 688
file content (149 lines) | stat: -rw-r--r-- 4,215 bytes parent folder | download | duplicates (2)
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
142
143
144
145
146
147
148
149
#!@@PERL@@ @@PERLOPTS@@

# rrd-percent - show value which 95% of values fall under or over
# $Id: rrd-percent.pl,v 1.4 2002/08/14 11:29:11 remstats Exp $
# from remstats @@VERSION@@

# Copyright 1999, 2000, 2001, 2002 (c) Thomas Erskine <@@AUTHOR@@>
# See the COPYRIGHT file with the distribution.

# - - -   Configuration   - - -

use strict;

# What is this program called, for error-messages and file-names
$main::prog = 'rrd-percent';
# What is the threshold % that the values have to fall under (or over)
$main::threshold_percent = 0.95;
# fetch data through which consolidation function
my $cf = 'AVERAGE';
# How close do we have to be to the threshold?
$main::close_enough = .001;

# - - -   Version History   - - -

$main::version = (split(' ', '$Revision: 1.4 $'))[1];

# - - -   Setup   - - -

my ($error, $rrdfile, $dsname, %opt, $begin_time, $end_time, $show_over);

use lib '.', '@@LIBDIR@@', '@@RRDLIBDIR@@';
use Getopt::Std;
use RRDs;

# Parse the command-line
%opt = ();
getopts('b:c:d:e:hl:ot:', \%opt);
unless (@main::ARGV == 2) { &usage; }
$rrdfile = shift @main::ARGV;
$dsname = shift @main::ARGV;

if (defined $opt{'h'}) { &usage; } # no return
if (defined $opt{'b'}) { $begin_time = $opt{'b'}; }
if (defined $opt{'c'}) { $cf = $opt{'c'}; }
if (defined $opt{'d'}) { $main::debug = $opt{'d'}; } else { $main::debug = 0; }
if (defined $opt{'e'}) { $end_time = $opt{'e'}; }
if (defined $opt{'l'}) { $main::close_enough = $opt{'l'}+0; }
if (defined $opt{'o'}) { $show_over = 1; } else { $show_over = 0; }
if (defined $opt{'t'}) { $main::threshold_percent = $opt{'t'}/100; }

# No buffering when debugging
if ($main::debug) { $| = 1; }

unless (-e $rrdfile) { &abort("no such file as $rrdfile"); }

# set default times
unless (defined $end_time) {
	$end_time = RRDs::last $rrdfile;
	$error = RRDs::error;
	if ($error) { &abort("last error: $error"); }
}
unless (defined $begin_time) {
	$begin_time = $end_time - 24*60*60;
}

# - - -   Mainline   - - -

my ($start, $step, $names, $data, $dsindex, @data, $line, $max, $min, $value,
	$index);

($start, $step, $names, $data) = RRDs::fetch $rrdfile, 
	$cf, '--start', $begin_time, '--end', $end_time;
$error = RRDs::error;
if ($error) { &abort("fetch error: $error"); }

# Find the dsname in the list
for ($dsindex=0; $dsindex < @$names; ++$dsindex) {
	last if ($$names[$dsindex] eq $dsname);
}
if ($dsindex >= @$names) { &abort("unknown dsname ($dsname)"); }
&debug("start=$start, step=$step, index=$dsindex") if ($main::debug);

# Collect just the values for our dsname
@data = ();
for $line (@$data) {
	$value = $$line[$dsindex];
	next unless (defined $value);
	push @data, $value;
	&debug("value=$value") if ($main::debug>1);
	if (!defined $max) {
		$max = $value;
	}
	elsif ($max < $value) {
		$max = $value;
	}
	if (!defined $min) {
		$min = $value;
	}
	elsif ($min > $value) {
		$min = $value;
	}
}
unless (defined $max) { &abort("no values"); }

# Special case
if (abs($max - $min) < $main::close_enough) {
	print (($max + $min) / 2, "\n");
	exit 0;
}

sub numerically { $a <=> $b };
@data = sort numerically @data;
if ($show_over) { @data = reverse @data; }
$index = int($main::threshold_percent * @data + .5);
print $data[$index], "\n";
exit 0;

#----------------------------------------------------------------- usage ---
sub usage {
	print STDERR <<"EOD_USAGE";
$main::prog version $main::version from remstats @@VERSION@@
usage: $0 [options] rrdfile dsname
where options are:
    -b bbb  start looking at time 'bbb' [-1d]
    -d nnn  enable debugging output at level 'nnn'
    -e eee  stop looking at time 'eee' [now]
    -h      show this help
    -l lll  how close the threshold must be [$main::close_enough]
    -o      values must be over threshold, not under
    -t ttt  set threshold to 'ttt' [$main::threshold_percent]
EOD_USAGE
	exit 0;
}

#----------------------------------------------------------------- debug ---
sub debug {
	print STDERR 'DEBUG: ', @_, "\n";
}

#------------------------------------------------------------------ abort ---
sub abort {
	print STDERR 'ABORT: ', @_, "\n";
	exit 6;
}

#------------------------------------------------------------------ error ---
sub error {
	print STDERR 'ERROR: ', @_, "\n";
}