298c39186110e752a86d65f854faabb32a349875
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / memory.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "${General::swroot}/graphs.pl";
32
33 my %cgiparams=();
34
35 &Graphs::updatememgraph ("day");
36
37 &Header::showhttpheaders();
38 &Header::getcgihash(\%cgiparams);
39 &Header::openpage($Lang::tr{'memory information'}, 1, '');
40 &Header::openbigbox('100%', 'left');
41
42 &Header::openbox('100%', 'center', "Memory $Lang::tr{'graph'}");
43 if (-e "$Header::graphdir/memory-day.png") {
44 my $ftime = localtime((stat("$Header::graphdir/memory-day.png"))[9]);
45 print "<center><b>$Lang::tr{'the statistics were last updated at'}: $ftime</b></center><br />\n";
46 print "<a href='/cgi-bin/graphs.cgi?graph=memory'>";
47 print "<img alt='' src='/graphs/memory-day.png' border='0' />";
48 print "</a>";
49 } else {
50 print $Lang::tr{'no information available'};
51 }
52 print "<br />\n";
53 &Header::closebox();
54
55 &Header::openbox('100%', 'center', "Swap $Lang::tr{'graph'}");
56 if (-e "$Header::graphdir/swap-day.png") {
57 my $ftime = localtime((stat("$Header::graphdir/swap-day.png"))[9]);
58 print "<center><b>$Lang::tr{'the statistics were last updated at'}: $ftime</b></center><br />\n";
59 print "<a href='/cgi-bin/graphs.cgi?graph=swap'>";
60 print "<img alt='' src='/graphs/swap-day.png' border='0' />";
61 print "</a>";
62 } else {
63 print $Lang::tr{'no information available'};
64 }
65 print "<br />\n";
66 &Header::closebox();
67
68 &Header::openbox('100%', 'center', $Lang::tr{'memory'});
69 print "<table width='95%' cellspacing='5'>";
70 my $ram=0;
71 my $size=0;
72 my $used=0;
73 my $free=0;
74 my $percent=0;
75 my $shared=0;
76 my $buffers=0;
77 my $cached=0;
78 open(FREE,'/usr/bin/free |');
79 while(<FREE>)
80 {
81 if ($_ =~ m/^\s+total\s+used\s+free\s+shared\s+buffers\s+cached$/ )
82 {
83 print <<END
84 <tr>
85 <td align='center'>&nbsp;</td>
86 <td align='center' class='boldbase'><b>$Lang::tr{'size'}</b></td>
87 <td align='center' class='boldbase'><b>$Lang::tr{'used'}</b></td>
88 <td align='center' class='boldbase'><b>$Lang::tr{'free'}</b></td>
89 <td align='left' class='boldbase' colspan='2'><b>$Lang::tr{'percentage'}</b></td>
90 </tr>
91 END
92 ;
93 } else {
94 if ($_ =~ m/^Mem:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/) {
95 ($ram,$size,$used,$free,$shared,$buffers,$cached) = ($1,$1,$2,$3,$4,$5,$6);
96 ($percent = ($used/$size)*100) =~ s/^(\d+)(\.\d+)?$/$1%/;
97 print <<END
98 <tr>
99 <td class='boldbase'><b>$Lang::tr{'ram'}</b></td>
100 <td align='center'>$size</td>
101 END
102 ;
103 } elsif ($_ =~ m/^Swap:\s+(\d+)\s+(\d+)\s+(\d+)$/) {
104 ($size,$used,$free) = ($1,$2,$3);
105 if ($size != 0)
106 {
107 ($percent = ($used/$size)*100) =~ s/^(\d+)(\.\d+)?$/$1%/;
108 } else {
109 ($percent = '');
110 }
111 print <<END
112 <tr>
113 <td class='boldbase'><b>$Lang::tr{'swap'}</b></td>
114 <td align='center'>$size</td>
115 END
116 ;
117 } elsif ($ram and $_ =~ m/^-\/\+ buffers\/cache:\s+(\d+)\s+(\d+)$/ ) {
118 ($used,$free) = ($1,$2);
119 ($percent = ($used/$ram)*100) =~ s/^(\d+)(\.\d+)?$/$1%/;
120 print "<tr><td colspan='2' class='boldbase'><b>$Lang::tr{'excluding buffers and cache'}</b></td>"
121 }
122 print <<END
123 <td align='center'>$used</td>
124 <td align='center'>$free</td>
125 <td>
126 END
127 ;
128 &percentbar($percent);
129 print <<END
130 </td>
131 <td align='left'>$percent</td>
132 </tr>
133 END
134 ;
135 }
136 }
137 close FREE;
138 print <<END
139 <tr><td class='boldbase' colspan='2'><br /></td></tr>
140 <tr><td class='boldbase'><b>$Lang::tr{'shared'}</b></td><td align='center'>$shared</td></tr>
141 <tr><td class='boldbase'><b>$Lang::tr{'buffers'}</b></td><td align='center'>$buffers</td></tr>
142 <tr><td class='boldbase'><b>$Lang::tr{'cached'}</b></td><td align='center'>$cached</td></tr>
143 </table>
144 END
145 ;
146 &Header::closebox();
147
148 &Header::closebigbox();
149
150 &Header::closepage();
151
152 sub percentbar
153 {
154 my $percent = $_[0];
155 my $fg = '#a0a0a0';
156 my $bg = '#e2e2e2';
157
158 if ($percent =~ m/^(\d+)%$/ )
159 {
160 print <<END
161 <table width='100' border='1' cellspacing='0' cellpadding='0' style='border-width:1px;border-style:solid;border-color:$fg;width:100px;height:10px;'>
162 <tr>
163 END
164 ;
165 if ($percent eq "100%") {
166 print "<td width='100%' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'>"
167 } elsif ($percent eq "0%") {
168 print "<td width='100%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>"
169 } else {
170 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'>"
171 }
172 print <<END
173 <img src='/images/null.gif' width='1' height='1' alt='' /></td></tr></table>
174 END
175 ;
176 }
177 }