File: rrd-tune

package info (click to toggle)
cricket 1.0.5-9
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,480 kB
  • ctags: 390
  • sloc: perl: 8,287; sh: 920; ansic: 318; makefile: 62; sql: 16
file content (282 lines) | stat: -rwxr-xr-x 7,786 bytes parent folder | download | duplicates (7)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/local/bin/perl -w
# -*- perl -*-

# Cricket: a configuration, polling and data display wrapper for RRD files
#
#    Copyright (C) 1998 Jeff R. Allen and WebTV Networks, Inc.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

BEGIN {
	my $programdir = (($0 =~ m:^(.*/):)[0] || "./") . "..";
	eval "require '$programdir/cricket-conf.pl'";
	eval "require '/usr/local/etc/cricket-conf.pl'"
					unless $Common::global::gInstallRoot;
	$Common::global::gInstallRoot ||= $programdir;
}

use lib "$Common::global::gInstallRoot/lib";

use RRDs 1.000101;

use ConfigTree::Cache;
use Common::HandleTarget;
use Common::Map;
use Common::Log;
use Getopt::Long;

my($VERSION) = "rrd-tune 1.1";

# Parse common options & script specific arguments
my($clear) = localOptions();

$Common::global::gCT = new ConfigTree::Cache;
$gCT = $Common::global::gCT;
$gCT->Base($Common::global::gConfigRoot);
$gCT->Warn(\&Warn);
 
if (! $gCT->init()) {
    Die("Failed to open compiled config tree from " .
		"$Common::global::gConfigRoot/config.db: $!");
}

my($recomp, $why) = $gCT->needsRecompile();
if ($recomp) {
    Die("Config tree needs to be recompiled: $why");
}

# Remaining arguments are the subtrees
my (@root) = (@ARGV) ? @ARGV : "/";

# foreach subtree to do
#   find the base node of that subtree
#   foreach leaf node of this subtree
#       process it
 
my($subtree);
foreach $subtree (@root) {
    if ($gCT->nodeExists($subtree)) {
		$gCT->visitLeafs($subtree, \&handleTarget,
			\&handleTargetInstance, \&localHandleTargetInstance);
    } else {
        Warn("Unknown subtree $subtree.");
    }
}

exit;

sub localHandleTargetInstance {
	my($name, $target) = @_;

	my($tname) = $target->{'auto-target-name'};

	# don't try to tune multitargets
	if ($target->{'targets'}) {
		return;
	}

	my($ttype) = $target->{'target-type'};
	if (! $ttype) {
		Warn("Skipping RRD tune for $tname; no target type.");
		return;
	}

	my($ttRef) = $main::gCT->configHash($name, 'targettype',
										lc($ttype), $target);
	if (! defined($ttRef)) {
		Warn("Skipping RRD tune for $tname; unknown target type.");
		return;
	}

	my($dslist) = $ttRef->{'ds'};
	if (! defined($dslist)) {
		Warn("Skipping RRD tune for $tname; no DS's for target-type $ttype.");
		return;
	}

    # Does the RRD file exist
    my($rrd) = $target->{'rrd-datafile'};
    if (! $rrd) {
        Warn("Skipping RRD tune for $tname; could not find " .
                " rrd-datafile.");
        return;
    }

    # Run the tuning agent to ajust RRD max and min values for each DS of the RRD
    tuneRRD($name, $target, $ttype, $ttRef, $dslist, $rrd);

    # Remove orphaned alarms in the meta data file associated with a valid RRD
    cleanMeta($name, $target);

    return 1;
}

sub cleanMeta {
	my($name, $target) = @_;
	my($tname) = $target->{'auto-target-name'};
    return if ($tname ne "chassis");

    ### meta file processing that contains active alarms
    my($rrd) = new RRD::File( -file=>$target->{'rrd-datafile'} );
    my($refMeta) = $rrd->getMeta();

    ### create a space delimited string with the monitor thresholds
    my ($monitors);
    if (defined $target->{'monitor-thresholds'}) {
        $monitors = ' ' . $target->{'monitor-thresholds'};
        $monitors =~ s/[\s,]+/ /g;
    }

    foreach my $key (keys %{$refMeta}) {
        Info ("  Clear this alarm?: $key data: $refMeta->{$key}");
        next if ($key =~ /last-inst/);
        if ($monitors) {
            my $found = ($monitors =~ / $key /) ? 1 : 0;
            delete($refMeta->{$key}) if (!$found || $clear);
            Info ("  Clearing alarm: $key") if (!$found || $clear);
        } else {
            delete($refMeta->{$key});
            Info ("  Clearing alarm: $key");
        }
    }

    $rrd->setMeta($refMeta);
    return 1;
}

sub tuneRRD {
	my($name, $target, $ttype, $ttRef, $dslist, $rrd) = @_;
	my($tname) = $target->{'auto-target-name'};

	# run rrd tune on the RRD

	my(@arg);
	my($dsnum) = 0;
	my($dsname);

	foreach $dsname (split(/\s*,\s*/, $dslist)) {

		my($ds) = $main::gCT->configHash($name, 'datasource',
									lc($dsname), $target);

		if (! defined($ds)) {
			Warn("Unknown datasource: $dsname");
			next;
		}

        my($dst) = $ds->{'rrd-ds-type'};
        $dst = "GAUGE" unless (defined($dst));
        $dst = uc($dst);
        # Never tune COMPUTE data sources; it will corrupt the header.
        # If the user really wants to replace the COMPUTE data source with
		# another type, they can do it manually.
        if ($dst eq 'COMPUTE') {
            $dsnum++;
		    Info("Skipping RRD tune for COMPUTE data source $dsname"); 
            next;
        }

        my($hb) = $ds->{'rrd-heartbeat'};
        $hb = $target->{'rrd-heartbeat'}
            if (defined($target->{'rrd-heartbeat'}));
        $hb = 1800 unless (defined($hb));

        my($min) = $ds->{'rrd-min'};
        $min = $target->{'rrd-min'}
            if (defined($target->{'rrd-min'}));
        $min = 'U' unless (defined($min));

        my($max) = $ds->{'rrd-max'};
        $max = $target->{'rrd-max'}
            if (defined($target->{'rrd-max'}));
        $max = 'U' unless (defined($max));

		push(@arg,
			'-d', "ds$dsnum:$dst",
			'-h', "ds$dsnum:$hb",
			'-i', "ds$dsnum:$min",
			'-a', "ds$dsnum:$max");
		$dsnum++;
	}

	Info("Tuning $tname");
	Debug("RRDs::tune $rrd ", join(" ", @arg));
	RRDs::tune $rrd, @arg;

	my($err) = RRDs::error();
	if ($error) {
		Warn("Unable to tune $rrd: $error\n");
		return;
	}

	return 1;
}

sub localOptions {
    my ($logLevel, $logFormat, $base, $clear, $usage);

    # default to 'info' unless there's a environment variable
    # or a commandline arg

    $logLevel = $Common::global::gLogLevel if $Common::global::gLogLevel;
    $logLevel = $ENV{'CRICKET_LOG_LEVEL'} if $ENV{'CRICKET_LOG_LEVEL'};
    $logLevel ||= "info";

    # default to 'standard' unless there's a environment variable
    $logFormat = $Common::global::gLogFormat if $Common::global::gLogFormat;
    $logFormat = $ENV{'CRICKET_LOG_FORMAT'} if $ENV{'CRICKET_LOG_FORMAT'};
    $logFormat ||= "standard";

    GetOptions( "loglevel:s" => \$logLevel,
                "logformat:s" => \$logFormat,
                "base:s" => \$base,
                "clear" => \$clear,
                "help|h" => \$usage);

    usage() if ($usage);
    $Common::global::gConfigRoot = $base if $base;
    Common::Log::setLevel($logLevel);
    Common::Log::setFormat($logFormat);

    return ($clear);
}

sub usage {
#'
    print STDERR <<EOD;

Version: $VERSION

USAGE: $0 [options] <subtreename>

   Default Actions:        - Cleanout orphaned monitor thresholds alarms
                           - Ajust min and max RRD database values

   --clear                 - Remove all monitor threshold alarms
   <subtreename>           - Select a protion of the config-tree
   --loglevel <loglevel>   - Select the logging level
   --logformat <logformat> - Select the logging format
   --help | -h             - Display the current help file


EXAMPLE:  $0
EXAMPLE:  $0 /cisco-routers/siteA/routerA
EXAMPLE:  $0 --clear /cisco-routers
EXAMPLE:  $0 --clear

EOD
#'
    exit(1);
}