]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/memory.cgi
collectd initskript detects and enable/disble thermal and swap plugin
[ipfire-2.x.git] / html / cgi-bin / memory.cgi
CommitLineData
72fe12a9 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
4e481c3a 5# Copyright (C) 2008 Michael Tremer & Christian Schmidt #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
72fe12a9
MT
21
22use strict;
23
24# enable only the following on debugging purpose
cb5e9c6c
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
72fe12a9
MT
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
350b52c5 31require "${General::swroot}/graphs.pl";
72fe12a9 32
4e481c3a
CS
33my %color = ();
34my %mainsettings = ();
35&General::readhash("${General::swroot}/main/settings", \%mainsettings);
36&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
72fe12a9 37
4e481c3a
CS
38my @querry = split(/\?/,$ENV{'QUERY_STRING'});
39$querry[0] = '' unless defined $querry[0];
40$querry[1] = 'hour' unless defined $querry[1];
72fe12a9 41
4e481c3a
CS
42if ( $querry[0] =~ "memory"){
43 print "Content-type: image/png\n\n";
44 binmode(STDOUT);
45 &Graphs::updatememorygraph($querry[1]);
46}elsif ( $querry[0] =~ "swap"){
47 print "Content-type: image/png\n\n";
48 binmode(STDOUT);
49 &Graphs::updateswapgraph($querry[1]);
50}else{
51 &Header::showhttpheaders();
52 &Header::openpage($Lang::tr{'memory information'}, 1, '');
53 &Header::openbigbox('100%', 'left');
72fe12a9 54
4e481c3a
CS
55 &Header::openbox('100%', 'center', "Memory $Lang::tr{'graph'}");
56 &Graphs::makegraphbox("memory.cgi","memory","day");
57 &Header::closebox();
72fe12a9 58
4e481c3a
CS
59 &Header::openbox('100%', 'center', "Swap $Lang::tr{'graph'}");
60 &Graphs::makegraphbox("memory.cgi","swap","day");
61 &Header::closebox();
62
63 &Header::openbox('100%', 'center', $Lang::tr{'memory'});
64 print "<table width='95%' cellspacing='5'>";
65 my $ram=0;
66 my $size=0;
67 my $used=0;
68 my $free=0;
69 my $percent=0;
70 my $shared=0;
71 my $buffers=0;
72 my $cached=0;
72fe12a9 73
4e481c3a
CS
74 open(FREE,'/usr/bin/free |');
75 while(<FREE>){
76 if ($_ =~ m/^\s+total\s+used\s+free\s+shared\s+buffers\s+cached$/ ){
77 print <<END
72fe12a9
MT
78<tr>
79<td align='center'>&nbsp;</td>
80<td align='center' class='boldbase'><b>$Lang::tr{'size'}</b></td>
81<td align='center' class='boldbase'><b>$Lang::tr{'used'}</b></td>
82<td align='center' class='boldbase'><b>$Lang::tr{'free'}</b></td>
83<td align='left' class='boldbase' colspan='2'><b>$Lang::tr{'percentage'}</b></td>
84</tr>
85END
86;
4e481c3a
CS
87 }else{
88 if ($_ =~ m/^Mem:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/){
89 ($ram,$size,$used,$free,$shared,$buffers,$cached) = ($1,$1,$2,$3,$4,$5,$6);
90 ($percent = ($used/$size)*100) =~ s/^(\d+)(\.\d+)?$/$1%/;
91 print <<END
72fe12a9
MT
92<tr>
93<td class='boldbase'><b>$Lang::tr{'ram'}</b></td>
33e1f48c 94<td align='center'>$size KB</td>
72fe12a9
MT
95END
96;
4e481c3a
CS
97 }elsif($_ =~ m/^Swap:\s+(\d+)\s+(\d+)\s+(\d+)$/){
98 ($size,$used,$free) = ($1,$2,$3);
99 if ($size != 0){
100 ($percent = ($used/$size)*100) =~ s/^(\d+)(\.\d+)?$/$1%/;
101 }else{
102 ($percent = '');
103 }
104 print <<END
72fe12a9
MT
105<tr>
106<td class='boldbase'><b>$Lang::tr{'swap'}</b></td>
33e1f48c 107<td align='center'>$size KB</td>
72fe12a9
MT
108END
109;
4e481c3a
CS
110 }elsif($ram and $_ =~ m/^-\/\+ buffers\/cache:\s+(\d+)\s+(\d+)$/ ){
111 ($used,$free) = ($1,$2);
112 ($percent = ($used/$ram)*100) =~ s/^(\d+)(\.\d+)?$/$1%/;
113 print "<tr><td colspan='2' class='boldbase'><b>$Lang::tr{'excluding buffers and cache'}</b></td>";
114 }
115 print <<END
33e1f48c
CS
116<td align='center'>$used KB</td>
117<td align='center'>$free KB</td>
72fe12a9
MT
118<td>
119END
120;
4e481c3a
CS
121 &percentbar($percent);
122 print <<END
72fe12a9
MT
123</td>
124<td align='left'>$percent</td>
125</tr>
126END
127;
4e481c3a
CS
128 }
129 }
130 close FREE;
131 print <<END
72fe12a9 132<tr><td class='boldbase' colspan='2'><br /></td></tr>
33e1f48c
CS
133<tr><td class='boldbase'><b>$Lang::tr{'shared'}</b></td><td align='center'>$shared KB</td></tr>
134<tr><td class='boldbase'><b>$Lang::tr{'buffers'}</b></td><td align='center'>$buffers KB</td></tr>
135<tr><td class='boldbase'><b>$Lang::tr{'cached'}</b></td><td align='center'>$cached KB</td></tr>
72fe12a9
MT
136</table>
137END
138;
4e481c3a 139 &Header::closebox();
72fe12a9 140
4e481c3a
CS
141 &Header::closebigbox();
142 &Header::closepage();
143}
72fe12a9 144
4e481c3a
CS
145sub percentbar{
146 my $percent = $_[0];
147 my $fg = '#a0a0a0';
148 my $bg = '#e2e2e2';
72fe12a9 149
4e481c3a
CS
150 if ($percent =~ m/^(\d+)%$/ ){
151 print <<END
72fe12a9
MT
152<table width='100' border='1' cellspacing='0' cellpadding='0' style='border-width:1px;border-style:solid;border-color:$fg;width:100px;height:10px;'>
153<tr>
154END
155;
4e481c3a
CS
156 if ($percent eq "100%"){
157 print "<td width='100%' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'>";
158 }elsif($percent eq "0%"){
159 print "<td width='100%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>";
160 }else{
161 print "<td width='$percent' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'></td><td width='" . (100-$1) . "%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>";
162 }
163 print <<END
72fe12a9
MT
164<img src='/images/null.gif' width='1' height='1' alt='' /></td></tr></table>
165END
166;
4e481c3a 167 }
72fe12a9 168}