summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway.scripts/src/backend/rrd_mem.pl
blob: 39c4d92e66157cde9245779633dc2402efc27577 (plain)
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
#!/usr/bin/perl
#*******************************************************************************
# Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
# This file is part of Gluster Management Gateway.
#
# Gluster Management Gateway 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.
#
# Gluster Management Gateway 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/>.
#*******************************************************************************

use RRDs;

my $rrdlog = '/var/lib/rrd';
my $graphs = '/var/lib/rrd';

updatememdata ();
#updatememgraph ('day');
#updatememgraph ('week');
#updatememgraph ('month');
#updatememgraph ('year');

sub updatememgraph {
    my $period    = $_[0];

    RRDs::graph ("$graphs/memory-$period.png",
                "--start", "-1$period", "-aPNG", "-i", "-z",
                "--alt-y-grid", "-w 300", "-h 50", "-l 0", "-u 100", "-r",
                "--color", "SHADEA#FFFFFF",
                "--color", "SHADEB#FFFFFF",
                "--color", "BACK#FFFFFF",
                "-t memory usage per $period",
                "DEF:used=$rrdlog/mem.rrd:memused:AVERAGE",
                "DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE",
                "DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE",
                "CDEF:total=used,free,+",
                "CDEF:used1=used,buffer,cache,-,-",
                "CDEF:usedpct=100,used1,total,/,*",
                "CDEF:free1=total,used1,-",
                "CDEF:cachepct=100,cache,total,/,*",
                "CDEF:freepct=100,free1,total,/,*",
                "AREA:usedpct#0000FF:used memory\\j",
                "STACK:cachepct#FFFF00:cached memory\\j",
                "STACK:freepct#00FF00:free memory\\j");
    $ERROR = RRDs::error;
    print "Error in RRD::graph for mem: $ERROR\n" if $ERROR;

    RRDs::graph ("$graphs/swap-$period.png",
                "--start", "-1$period", "-aPNG", "-i", "-z",
                "--alt-y-grid", "-w 300", "-h 50", "-l 0", "-u 100", "-r",
                "--color", "SHADEA#FFFFFF",
                "--color", "SHADEB#FFFFFF",
                "--color", "BACK#FFFFFF",
                "-t swap usage per $period",
                "DEF:used=$rrdlog/mem.rrd:swapused:AVERAGE",
                "DEF:free=$rrdlog/mem.rrd:swapfree:AVERAGE",
                "CDEF:total=used,free,+",
                "CDEF:usedpct=100,used,total,/,*",
                "CDEF:freepct=100,free,total,/,*",
                "AREA:usedpct#0000FF:used swap\\j",
                "STACK:freepct#00FF00:free swap\\j");
    $ERROR = RRDs::error;
    print "Error in RRD::graph for swap: $ERROR\n" if $ERROR;
}

sub updatememdata {
    my ($memused, $memfree, $memshared, $membuffers, $memcache, $swapused, $swapfree);
    if ( ! -e "$rrdlog/mem.rrd") {
      RRDs::create ("$rrdlog/mem.rrd", "--step=300",
                        "DS:memused:ABSOLUTE:600:0:U",
                        "DS:memfree:ABSOLUTE:600:0:U",
                        "DS:memcache:ABSOLUTE:600:0:U",
		        "DS:membuffer:ABSOLUTE:600:0:U",
                        "DS:swapused:ABSOLUTE:600:0:U",
                        "DS:swapfree:ABSOLUTE:600:0:U",
                        "RRA:AVERAGE:0.5:1:576",
                        "RRA:AVERAGE:0.5:6:672",
                        "RRA:AVERAGE:0.5:24:732",
                    "RRA:AVERAGE:0.5:144:1460");
      $ERROR = RRDs::error;
      print "Error in RRD::create for mem: $ERROR\n" if $ERROR;
    }

    my @memdata = `free -b -o`;

    my $temp = $memdata[1];

    chomp( $temp );
    my @tempa = split (/\s+/, $temp);
    $memused    = $tempa [2];
    $memfree    = $tempa [3];
    $memshared  = $tempa [4];
    $membuffers = $tempa [5];
    $memcache   = $tempa [6];

    $temp = $memdata[2];
    chomp( $temp );
    @tempa = split (/\s+/, $temp);
    $swapused = $tempa [2];
    $swapfree = $tempa [3];


  RRDs::update ("$rrdlog/mem.rrd",
                "-t", "memused:memfree:memcache:membuffer:swapused:swapfree",
                "N:$memused:$memfree:$memcache:$membuffers:$swapused:$swapfree");

   $ERROR = RRDs::error;
    print "Error in RRD::update for mem: $ERROR\n" if $ERROR;
}