File: BackupPC_rrdUpdate

package info (click to toggle)
backuppc 4.4.0-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,752 kB
  • sloc: perl: 37,523; sh: 607; javascript: 176; makefile: 38; ansic: 6
file content (243 lines) | stat: -rwxr-xr-x 7,751 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_rrdUpdate: Nightly pool graph update
#
# DESCRIPTION
#
#   Usage: BackupPC_rrdUpdate
#
# AUTHORS
#   Alexander Moisseev  <moiseev@mezonplus.ru>
#   Craig Barratt       <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
#   Copyright (C) 2013  Alexander Moisseev and Craig Barratt
#
#   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 3 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, see <http://www.gnu.org/licenses/>.
#
#========================================================================
#
# Version 4.4.0, released 20 Jun 2020.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================

use strict;
no utf8;

use lib "__INSTALLDIR__/lib";
use BackupPC::Lib qw( :BPC_DT_ALL );
use File::Path;
use Data::Dumper;
our(%Info);

die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
my $TopDir = $bpc->TopDir();
my $BinDir = $bpc->BinDir();
my $LogDir = $bpc->LogDir();
my %Conf   = $bpc->Conf();

if ( !-x $Conf{RrdToolPath} ) {
    print("$Conf{RrdToolPath} is not a valid executable\n");
    exit(0);
}

my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
if ( $err ) {
    print("Can't connect to server ($err)\n");
    exit(1);
}
my $reply = $bpc->ServerMesg("status info");
$reply = $1 if ( $reply =~ /(.*)/s );
eval($reply);

my $Now     = time();
my $NowRnd0 = 86400 * int($Now / 86400);
my $NowRnd1 = $NowRnd0 + 86400;
my $RRDFile = "$LogDir/poolUsage.rrd";

if ( -f $RRDFile ) {
    my $lastUpdate = $bpc->cmdSystemOrEval([$Conf{RrdToolPath}, "last", $RRDFile]);
    if ( $? ) {
        print $lastUpdate;
        exit(1);
    } elsif ( $lastUpdate == $NowRnd1 ) {
        print "skipping repeated RRD update for the same day: last update time is $lastUpdate\n";
        exit(0);
    }
}

RRDUpdate();
RRDGraph(4);
RRDGraph(52);
exit(0);

#
# Feeds pool size data values into an RRD $RRDFile
#
sub RRDUpdate
{
    #
    # We store data in RRD with predicted time of next update.
    #
    if ( !-f $RRDFile ) {
        if ( (
            $err = $bpc->cmdSystemOrEval([
                $Conf{RrdToolPath},         "create",
                $RRDFile,                   "--step",
                "86400",                    "--start",
                "$NowRnd0 - 1d",            "DS:c3kB:GAUGE:172800:0:U",
                "DS:tpkB:GAUGE:172800:0:U", "DS:p3kB:GAUGE:172800:0:U",
                "DS:p4kB:GAUGE:172800:0:U", "DS:c4kB:GAUGE:172800:0:U",
                "RRA:LAST:0:1:732"
            ])
        ) ) {
            print "$err";
        }
    }

    #
    # Calculate pool size totals prior to poolng and compression
    #
    my $sizeTot;
    foreach my $host ( sort(keys(%{$bpc->HostInfoRead()})) ) {
        my @Backups = $bpc->BackupInfoRead($host);
        for ( my $i = 0 ; $i < @Backups ; $i++ ) {
            $sizeTot += $Backups[$i]{size} || 0;
        }
    }
    $sizeTot = $sizeTot / 1024;

    if ( (
        $err = $bpc->cmdSystemOrEval([
            $Conf{RrdToolPath},
            "update",
            $RRDFile,
            $NowRnd1 . ":"
              . $Info{"cpoolKb"} . ":"
              . $sizeTot . ":"
              . $Info{"poolKb"} . ":"
              . $Info{"pool4Kb"} . ":"
              . $Info{"cpool4Kb"}
        ])
    ) ) {
        print "$err";
    } else {
        printf("RRD updated: date %s; cpoolKb %f; total %f; poolKb %f; pool4Kb %f; cpool4Kb %f\n",
            $NowRnd1, $Info{"cpoolKb"}, $sizeTot, $Info{"poolKb"}, $Info{"pool4Kb"}, $Info{"cpool4Kb"});
    }
}

#
# Generate pool size RRDtool graph image
#
sub RRDGraph
{
    my($weeks) = @_;
    my($fdOut);
    my @poolMax;

    #
    # Get each pool max value from RRD
    #
    $bpc->cmdSystemOrEvalLong(
        [
            $Conf{RrdToolPath},            "graphv",
            "-",                           "--end=$NowRnd1",
            "--start=end-${weeks}w",       "DEF:p3kB=$RRDFile:p3kB:LAST",
            "DEF:c3kB=$RRDFile:c3kB:LAST", "DEF:p4kB=$RRDFile:p4kB:LAST",
            "DEF:c4kB=$RRDFile:c4kB:LAST", "PRINT:p3kB:MAX:%lf",
            "PRINT:c3kB:MAX:%lf",          "PRINT:p4kB:MAX:%lf",
            "PRINT:c4kB:MAX:%lf",
        ],
        sub {
            if ( $_[0] =~ /^print\[([0-3])\] = "([.,0-9]+)"$/ ) {
                $poolMax[$1] = $2 unless ( $2 == 0 );
            }
        },
        1,
        undef
    );

    my $poolSizeGraph = [
        "$Conf{RrdToolPath}",          "graph",
        "-",                           "--end=$NowRnd1",
        "--start=end-${weeks}w",       "--title=BackupPC Pool Size (${weeks} weeks)",
        "--vertical-label=",           "--width=600",
        "--height=100",                "--rigid",
        "--alt-autoscale-max",         "--base=1024",
        "--logarithmic",               "--units=si",
        "--color",                     "BACK#FFFFFF",
        "--slope-mode",                "--imgformat=PNG",
        "--font",                      "TITLE:10:",
        "--font",                      "AXIS:8:",
        "--font",                      "LEGEND:8:",
        "--font",                      "UNIT:8:",
        "--font-render-mode",          "mono",
        "DEF:p3kB=$RRDFile:p3kB:LAST", "DEF:c3kB=$RRDFile:c3kB:LAST",
        "DEF:p4kB=$RRDFile:p4kB:LAST", "DEF:c4kB=$RRDFile:c4kB:LAST",
        "DEF:tpkB=$RRDFile:tpkB:LAST", "CDEF:p3B=p3kB,1024,*",
        "CDEF:c3B=c3kB,1024,*",        "CDEF:p4B=p4kB,1024,*",
        "CDEF:c4B=c4kB,1024,*",        "CDEF:tpB=tpkB,1024,*"
    ];

    #
    # Draw stacked pool graphs (and legend entry) for non empty pools
    #
    push(@$poolSizeGraph,
        'AREA:p3B#FFADA7:Pool  V3 in bytes               ',
        'GPRINT:p3B:LAST:Current\\:%8.2lf %s',
        'GPRINT:p3B:AVERAGE:Average\\:%8.2lf %s',
        'GPRINT:p3B:MAX:Maximum\\:%8.2lf %s\n')
      if $poolMax[0];

    push(@$poolSizeGraph,
        'AREA:c3B#FFDBA7:CPool V3 in bytes               :STACK',
        'GPRINT:c3B:LAST:Current\\:%8.2lf %s',
        'GPRINT:c3B:AVERAGE:Average\\:%8.2lf %s',
        'GPRINT:c3B:MAX:Maximum\\:%8.2lf %s\n')
      if $poolMax[1];

    push(@$poolSizeGraph,
        'AREA:p4B#97E7A5:Pool  V4 in bytes               :STACK',
        'GPRINT:p4B:LAST:Current\\:%8.2lf %s',
        'GPRINT:p4B:AVERAGE:Average\\:%8.2lf %s',
        'GPRINT:p4B:MAX:Maximum\\:%8.2lf %s\n')
      if $poolMax[2];

    push(@$poolSizeGraph,
        'AREA:c4B#95B8DB:CPool V4 in bytes               :STACK',
        'GPRINT:c4B:LAST:Current\\:%8.2lf %s',
        'GPRINT:c4B:AVERAGE:Average\\:%8.2lf %s',
        'GPRINT:c4B:MAX:Maximum\\:%8.2lf %s\n')
      if $poolMax[3];

    #
    # Draw total line graph
    #
    push(@$poolSizeGraph,
        'LINE1:tpB#FF0000:Prior to pooling and compression',
        'GPRINT:tpB:LAST:Current\\:%8.2lf %s',
        'GPRINT:tpB:AVERAGE:Average\\:%8.2lf %s',
        'GPRINT:tpB:MAX:Maximum\\:%8.2lf %s\\n');

    if ( !open($fdOut, ">", "$LogDir/poolUsage$weeks.png") ) {
        print("Can't open/create $LogDir/poolUsage$weeks.png\n");
        return;
    }
    $bpc->cmdSystemOrEvalLong($poolSizeGraph, sub { print $fdOut $_[0] }, 1, undef);
    close($fdOut);
}