File: rrdTuneAberrant.cgi

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 (200 lines) | stat: -rwxr-xr-x 5,748 bytes parent folder | download | duplicates (8)
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
#!/usr/local/bin/perl -wT
# -*- 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 {
    # this script lives in the util subdirectory
    my $programdir = (($0 =~ m:^(.*/)util:)[0] || "./") . ".";
    eval "require '$programdir/cricket-conf.pl'";
    eval "require '$programdir/../cricket-conf.pl'"
                    unless $Common::global::gInstallRoot;
    $Common::global::gInstallRoot ||= $programdir;
}

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

use CGI qw(carpout fatalsToBrowser);
use RRDs 1.000101;
use ConfigTree::Cache;
use Common::Version;
use Common::global;
use Common::HandleTarget;
use Common::Util;
use Common::Log;

$log = 'debug';
Common::Log::setLevel($log);

$gQ = new CGI;
fixHome($gQ);

$Common::global::gCT = new ConfigTree::Cache;
$gCT = $Common::global::gCT;
$gCT->Base($Common::global::gConfigRoot);
$gCT->Warn(\&Warn);

print $gQ -> header('text/plain');
 
if (! $gCT->init()) {
    print "Failed to open compiled config tree from " .
        "$Common::global::gConfigRoot/config.db: $!";
    exit(0);
}

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

# validate target name
my($name) = $gQ->param("target");

my($targetRef) =  $gCT->configHash($name,'target');
if (!defined($targetRef)) {
    print "Specified target missing or invalid";
    exit;
}
ConfigTree::Cache::addAutoVariables($name, $targetRef, $Common::global::gConfigRoot);

my($ttRef) = $gCT->configHash($name, 'targettype',
                              lc($targetRef->{'target-type'}));

if (! defined($ttRef)) {
    print "Invalid or missing target-type for $name";
    exit;
}

# validate data source
my($ds) = $gQ -> param('ds');
if (!defined($ds)) {
    print "Missing data source name";
    exit;
}

my($dslist) = $ttRef->{'ds'};
my($dsnum) = 0;
my($bFound) = 0;
foreach $dsname (split(/\s*,\s*/, $dslist))
{
    if ($dsname eq $ds) {
        $bFound = 1;
        last;
    }
    $dsnum++
}
if (!$bFound) {
    print "Specified data source $ds does not exist for target $name";
    exit;
}

my($rrd) = $targetRef->{'rrd-datafile'};

# is this a vector-instance target?
# borrowed from HandleTarget
my(@inst);
if (defined($targetRef->{'inst'})) {
    my($inst) = $targetRef->{'inst'};
    $inst = ConfigTree::Cache::expandString($inst, $targetRef, \&Warn);
# assume $inst is something like (1..8) or (50,51,52)  
    $inst =~ s/^\s*\(//;
    $inst =~ s/\)\s*$//;
    if ($inst =~ /([0-9]+)\.\.([0-9]+)/) {
        if ($2 > $1) {
            my ($start,$end) = ($1, $2);
            for ( ;$start <= $end; ++$start) { push @inst, $start; } 
        }
    } else {
        @inst = split(/\s*,\s*/,$inst);
    }
} else {
    @inst = ();
}
if (scalar(@inst) > 1) {
    if (!defined($gQ->param('inst'))) {
        print "Target is a vector instance, but no instance specified";
        exit;
    }
    # is the inst valid?
    $bFound = 0;
    foreach $inst (@inst) {
        if ($inst eq $gQ->param('inst')) {
            $bFound = 1;
            last;
        }
    }
    if (!$bFound) {
        print "Instance specified " . $gQ->param('inst') 
        . " is not valid for target $name";
        exit;
    }
    # because inst appears in rrd-datafile, we need to remap inst to a scalar
    my($inst_save) = $targetRef->{'inst'};
    $targetRef->{'inst'} = $gQ->param('inst');
    $rrd = ConfigTree::Cache::expandString($rrd, $targetRef, \&Warn); 
    # not strictly necessary because at the moment this value won't be used
    # down stream... however, that may change in the future
    $targetRef->{'inst'} = $inst_save;
} else {
    $rrd = ConfigTree::Cache::expandString($rrd, $targetRef, \&Warn); 
}

# at this point, we have:
# a rrd data file pathname
my @arg; 
if ($Common::global::gLongDSName) {
    @arg = ('--aberrant-reset',"$dsname");
} else {
# a data source number (to compute the Cricket ds# name)
    @arg = ('--aberrant-reset',"ds$dsnum");
}
RRDs::tune $rrd, @arg;
my($err) = RRDs::error(); 
if ($err) {
    print "Failed:\ntune $rrd " . join(" ",@arg);
    print "\nRRDtool: $err\n";
} else {
    print "Success:\ntune $rrd " . join(" ",@arg) . "\n";
}

1; 

# borrowed from grapher.cgi
sub fixHome {
    return if (defined($Common::global::gCricketHome) &&
           $Common::global::gCricketHome =~ /\//);

    my($sname) = $gQ->script_name();
    if ($sname =~ /\/~([^\/]*)\//) {
        my($username) = $1;
        my($home) = (getpwnam($username))[7];
        if ($home) {
            $Common::global::gCricketHome = $home;
            return;
        } else {
            Info("Could not find a home directory for user $username." .
                "gCricketHome is probably not set right.");
        }
    } else {
        Info("Could not find a username in SCRIPT_NAME. " .
            "gCricketHome is probably not set right.");
    }
    $Common::global::gCricketHome ||= $Common::global::gInstallRoot . "/..";
}