]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/memory.cgi
suricata: Change midstream policy to "pass-flow"
[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) 2005-2010 IPFire Team #
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 %color = ();
34 my %mainsettings = ();
35 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
36 &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
37
38 my @querry = split(/\?/,$ENV{'QUERY_STRING'});
39 $querry[0] = '' unless defined $querry[0];
40 $querry[1] = 'hour' unless defined $querry[1];
41
42 if ( $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');
54
55 &Header::openbox('100%', 'center', "Memory $Lang::tr{'graph'}");
56 &Graphs::makegraphbox("memory.cgi","memory","day");
57 &Header::closebox();
58
59 if (-f "$mainsettings{'RRDLOG'}/collectd/localhost/swap") {
60 &Header::openbox('100%', 'center', "Swap $Lang::tr{'graph'}");
61 &Graphs::makegraphbox("memory.cgi","swap","day");
62 &Header::closebox();
63 }
64
65 &Header::openbox('100%', 'center', $Lang::tr{'memory'});
66 print "<table width='95%' cellspacing='5'>";
67 my $size=0;
68 my $used=0;
69 my $free=0;
70 my $percent=0;
71 my $shared=0;
72 my $buffers=0;
73 my $cached=0;
74 my $available=0;
75
76 # output format: kibibytes, wide mode (buffers and cache in two columns)
77 open(my $cmd_fh, "-|", '/usr/bin/free -k -w') or die $!;
78 while(<$cmd_fh>){
79 if ($_ =~ m/^\s+total\s+used\s+free\s+shared\s+buffers\s+cache\s+available$/ ){
80 print <<END
81 <tr>
82 <td align='center'>&nbsp;</td>
83 <td align='center' class='boldbase'><b>$Lang::tr{'size'}</b></td>
84 <td align='center' class='boldbase'><b>$Lang::tr{'used'}</b></td>
85 <td align='center' class='boldbase'><b>$Lang::tr{'free'}</b></td>
86 <td align='left' class='boldbase' colspan='2'><b>$Lang::tr{'percentage'}</b></td>
87 </tr>
88 END
89 ;
90 }else{
91 if ($_ =~ m/^Mem:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/){
92 ($size,$used,$free,$shared,$buffers,$cached,$available) = ($1,$2,$3,$4,$5,$6,$7);
93 ($percent = ($used/$size)*100) =~ s/^(\d+)(\.\d+)?$/$1%/;
94 print <<END
95 <tr>
96 <td class='boldbase'><b>$Lang::tr{'ram'}</b></td>
97 END
98 ;
99 }elsif($_ =~ m/^Swap:\s+(\d+)\s+(\d+)\s+(\d+)$/){
100 ($size,$used,$free) = ($1,$2,$3);
101 if ($size != 0){
102 ($percent = ($used/$size)*100) =~ s/^(\d+)(\.\d+)?$/$1%/;
103 }else{
104 ($percent = '');
105 }
106 print <<END
107 <tr>
108 <td class='boldbase'><b>$Lang::tr{'swap'}</b></td>
109 END
110 ;
111 }
112 print <<END
113 <td align='center'>$size KiB</td>
114 <td align='center'>$used KiB</td>
115 <td align='center'>$free KiB</td>
116 <td>
117 END
118 ;
119 &percentbar($percent);
120 print <<END
121 </td>
122 <td align='left'>$percent</td>
123 </tr>
124 END
125 ;
126 }
127 }
128 close($cmd_fh);
129 print <<END
130 <tr><td colspan='6'><br /></td></tr>
131 <tr><td class='boldbase'><b>$Lang::tr{'shared'}</b></td><td align='center'>$shared KiB</td></tr>
132 <tr><td class='boldbase'><b>$Lang::tr{'buffers'}</b></td><td align='center'>$buffers KiB</td></tr>
133 <tr><td class='boldbase'><b>$Lang::tr{'cached'}</b></td><td align='center'>$cached KiB</td></tr>
134 <tr><td class='boldbase'><b>$Lang::tr{'available'}</b></td><td align='center'>$available KiB</td></tr>
135 </table>
136 END
137 ;
138 &Header::closebox();
139
140 &Header::closebigbox();
141 &Header::closepage();
142 }
143
144 sub percentbar{
145 my $percent = $_[0];
146 my $fg = '#a0a0a0';
147 my $bg = '#e2e2e2';
148
149 if ($percent =~ m/^(\d+)%$/ ){
150 print <<END
151 <table width='100' border='1' cellspacing='0' cellpadding='0' style='border-width:1px;border-style:solid;border-color:$fg;width:100px;height:10px;'>
152 <tr>
153 END
154 ;
155 if ($percent eq "100%"){
156 print "<td width='100%' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'>";
157 }elsif($percent eq "0%"){
158 print "<td width='100%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>";
159 }else{
160 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'>";
161 }
162 print <<END
163 <img src='/images/null.gif' width='1' height='1' alt='' /></td></tr></table>
164 END
165 ;
166 }
167 }