]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/hardwaregraphs.cgi
Merge branch 'perl-system' into next
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / hardwaregraphs.cgi
CommitLineData
3961e831
AF
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
1e18d32b 5# Copyright (C) 2005-2011 IPFire Team #
3961e831
AF
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
22use strict;
23
24# enable only the following on debugging purpose
bcad0fd0
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
3961e831
AF
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31require "${General::swroot}/graphs.pl";
32
2d281532
CS
33my %color = ();
34my %mainsettings = ();
35&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 36&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
2d281532 37
3961e831 38my %sensorsettings = ();
3961e831 39
3961e831 40my @sensorsgraphs = ();
508547f9
SS
41
42# Main directory where rrdlog puts the sensor data.
43my $sensorsdir = "$mainsettings{'RRDLOG'}/collectd/localhost";
44
45# Open sensors directory.
46opendir(SENSORS, "$sensorsdir") or die "Could not opendir $sensorsdir: $!\n";
47
48# Read-in all sensors.
49my @sensor_dirs = readdir(SENSORS);
50
51# Close directory handle.
52closedir(SENSORS);
53
54# Loop through the grabbed sensors.
55foreach my $sensor_dir (@sensor_dirs) {
56 # Skip everything which does not start with "sensors-".
57 next unless $sensor_dir =~ /^sensors-/;
58
59 # Check if the omitet element is a directory.
60 next unless (-d "$sensorsdir/$sensor_dir");
61
62 # Open sensor directory and lookup for sensors.
63 opendir(SENSOR_DIR, "$sensorsdir/$sensor_dir") or die "Could not opendir $sensorsdir/$sensor_dir: $!\n";
64
65 # Grab single sensors from the directory.
66 my @sensors = readdir(SENSOR_DIR);
67
68 # Close directory handle.
69 closedir(SENSOR_DIR);
70
71 # Loop through the omited sensors.
72 foreach my $sensor (@sensors) {
73 # Skip everything which is not a regular file.
74 next unless (-f "$sensorsdir/$sensor_dir/$sensor");
75
76 # Add sensor to the array of sensorsgrapghs.
77 push(@sensorsgraphs, "$sensorsdir/$sensor_dir/$sensor");
78 }
79}
80
81# Look for ACPI Thermal Zone sensors.
82my @thermal_zone_sensors = grep(/thermal-thermal_zone/, @sensor_dirs);
83
84# If a thermal zone sensor has been found add it to the sensorsgraphs array.
85if (@thermal_zone_sensors) {
86 # Loop through the array of thermal zone sensors.
87 foreach my $thermal_zone_sensor (@thermal_zone_sensors) {
88 # Add the sensor to the array.
89 push(@sensorsgraphs, "$sensorsdir/$thermal_zone_sensor");
3961e831
AF
90 }
91}
92
2d281532
CS
93my @querry = split(/\?/,$ENV{'QUERY_STRING'});
94$querry[0] = '' unless defined $querry[0];
95$querry[1] = 'hour' unless defined $querry[1];
96
97if ( $querry[0] =~ "hwtemp"){
98 print "Content-type: image/png\n\n";
99 binmode(STDOUT);
100 &Graphs::updatehwtempgraph($querry[1]);
101}elsif ( $querry[0] =~ "hwfan"){
102 print "Content-type: image/png\n\n";
103 binmode(STDOUT);
104 &Graphs::updatehwfangraph($querry[1]);
105}elsif ( $querry[0] =~ "hwvolt"){
106 print "Content-type: image/png\n\n";
107 binmode(STDOUT);
108 &Graphs::updatehwvoltgraph($querry[1]);
30189c50
AF
109}elsif ( $querry[0] =~ "thermaltemp"){
110 print "Content-type: image/png\n\n";
111 binmode(STDOUT);
112 &Graphs::updatethermaltempgraph($querry[1]);
269e21c6 113}elsif ( $querry[0] =~ "sd?" ){
2d281532
CS
114 print "Content-type: image/png\n\n";
115 binmode(STDOUT);
116 &Graphs::updatehddgraph($querry[0],$querry[1]);
ca9c45b3
AF
117}elsif ( $querry[0] =~ "nvme?" ){
118 print "Content-type: image/png\n\n";
119 binmode(STDOUT);
120 &Graphs::updatehddgraph($querry[0],$querry[1]);
2d281532
CS
121}else{
122 &Header::showhttpheaders();
fb6446d2 123 &Header::openpage($Lang::tr{'hardware graphs'}, 1, '');
2d281532
CS
124 &Header::openbigbox('100%', 'left');
125
126 &Header::getcgihash(\%sensorsettings);
127
128 if ( $sensorsettings{'ACTION'} eq $Lang::tr{'save'} ) {
129 foreach(@sensorsgraphs){
130 chomp($_);
131 $_ =~ /\/(.*)sensors-(.*)\/(.*)\.rrd/;
132 my $label = $2.$3;$label=~ s/-//g;
e4e74858
CS
133 if ( $sensorsettings{'LINE-'.$label} ne "on" ){
134 $sensorsettings{'LINE-'.$label} = 'off';
135 } elsif ($sensorsettings{'LINE-'.$label} eq "on" ){
136 $sensorsettings{'LINE-'.$label} = 'checked';
137 }
138 $sensorsettings{'LABEL-'.$label} =~ s/\W//g;
2d281532
CS
139 }
140 &General::writehash("${General::swroot}/sensors/settings", \%sensorsettings);
3961e831 141 }
3961e831 142
2386d2ef 143 # This should be save, because no user given content will be processed.
508547f9
SS
144 #my @disks = `ls -1 /sys/block | grep -E '^sd|^nvme' | sort | uniq`;
145 my @disks = &get_disks();
2d281532
CS
146
147 foreach (@disks){
148 my $disk = $_;
149 chomp $disk;
150 my @array = split(/\//,$disk);
151
152 &Header::openbox('100%', 'center', "$array[$#array] $Lang::tr{'graph'}");
153 &Graphs::makegraphbox("hardwaregraphs.cgi",$array[$#array],"day");
154 &Header::closebox();
3961e831 155 }
2d281532 156
508547f9 157 if ( grep(/thermal-thermal_zone/, @sensorsgraphs) ) {
e4e74858
CS
158 &Header::openbox('100%', 'center', "ACPI Thermal-Zone Temp $Lang::tr{'graph'}");
159 &Graphs::makegraphbox("hardwaregraphs.cgi","thermaltemp","day");
160 &Header::closebox();
30189c50
AF
161 }
162
508547f9 163 if ( grep(/temperature-/, @sensorsgraphs) ) {
e4e74858 164 &Header::openbox('100%', 'center', "hwtemp $Lang::tr{'graph'}");
a249ccd2 165 &Graphs::makegraphbox("hardwaregraphs.cgi","hwtemp","day");
e4e74858 166 Header::closebox();
30114625 167 }
2d281532 168
508547f9 169 if ( grep(/fanspeed-/, @sensorsgraphs) ) {
e4e74858
CS
170 &Header::openbox('100%', 'center', "hwfan $Lang::tr{'graph'}");
171 &Graphs::makegraphbox("hardwaregraphs.cgi","hwfan","day");
172 &Header::closebox();
30114625 173 }
2d281532 174
508547f9 175 if ( grep(/voltage-/, @sensorsgraphs) ) {
e4e74858 176 &Header::openbox('100%', 'center', "hwvolt $Lang::tr{'graph'}");
a249ccd2 177 &Graphs::makegraphbox("hardwaregraphs.cgi","hwvolt","day");
e4e74858 178 &Header::closebox();
30114625 179 }
3961e831 180
508547f9 181 if ( @sensorsgraphs ) {
e4e74858 182 sensorsbox();
30189c50 183 }
2d281532
CS
184 &Header::closebigbox();
185 &Header::closepage();
3961e831 186
3961e831
AF
187}
188
3961e831 189
2d281532
CS
190sub sensorsbox {
191 &Header::openbox('100%', 'center', "$Lang::tr{'mbmon settings'}");
3961e831 192
2d281532
CS
193 print <<END
194<form method='post' action='$ENV{'SCRIPT_NAME'}'>
195<table width='100%' border='0' cellspacing='5' cellpadding='0' align='center'>
196<tr>
197 <td align='right' width='40%'><b>$Lang::tr{'mbmon display'}</b></td>
198 <td align='left'><b>$Lang::tr{'mbmon label'}</b></td>
199</tr>
3961e831
AF
200END
201;
2d281532
CS
202
203 foreach (@sensorsgraphs){
204 $_ =~ /\/(.*)sensors-(.*)\/(.*)\.rrd/;
205 my $label = $2.$3;$label=~ s/-//g;
206 $sensorsettings{'LABEL-'.$label}="$label";
207 $sensorsettings{'LINE-'.$label}="checked";
208 &General::readhash("${General::swroot}/sensors/settings", \%sensorsettings);
209 print("<tr><td align='right'><input type='checkbox' name='LINE-$label' $sensorsettings{'LINE-'.$label} /></td>");
210 print("<td><input type='text' name='LABEL-$label' value='$sensorsettings{'LABEL-'.$label}' size='25' /></td></tr>\n");
211 }
212
213 print <<END
214<tr>
1d55fccd 215 <td align='center' colspan='2' ><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
2d281532
CS
216</tr>
217</table>
218</form>
3961e831
AF
219END
220;
2d281532 221 &Header::closebox();
3961e831 222}
508547f9
SS
223
224sub get_disks () {
225 my @disks;
226
227 # Open virtal sys FS and grab block devices.
228 opendir(SYS, "/sys/block") or die "Could not opendir /sys/block/: $!\n";
229
230 # Grab all available block devices.
231 my @blockdevs = readdir(SYS);
232
233 # Close directory handle.
234 closedir(SYS);
235
236 # Loop through the array of blockdevs.
237 foreach my $dev (@blockdevs) {
238 # Skip all devices which does not start with "sd" or "nvme".
239 next unless (( $dev =~ /^sd/) || ($dev =~ /^nvme/));
240
241 # Add the device to the array of disks.
242 push(@disks, $dev);
243 }
244
245 # Remove duplicates.
246 my @disks = &uniq(@disks);
247
248 # Sort the array.
249 my @disks = sort(@disks);
250
251 # Return the array.
252 return @disks;
253}
254
255# Tiny code snipped to get a uniq() like function.
256sub uniq {
257 my %seen;
258 return grep { !$seen{$_}++ } @_;
259}