]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame_incremental - html/cgi-bin/hardwaregraphs.cgi
Merge branch 'perl-system' into next
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / hardwaregraphs.cgi
... / ...
CommitLineData
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2005-2011 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
22use strict;
23
24# enable only the following on debugging purpose
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31require "${General::swroot}/graphs.pl";
32
33my %color = ();
34my %mainsettings = ();
35&General::readhash("${General::swroot}/main/settings", \%mainsettings);
36&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
37
38my %sensorsettings = ();
39
40my @sensorsgraphs = ();
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");
90 }
91}
92
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]);
109}elsif ( $querry[0] =~ "thermaltemp"){
110 print "Content-type: image/png\n\n";
111 binmode(STDOUT);
112 &Graphs::updatethermaltempgraph($querry[1]);
113}elsif ( $querry[0] =~ "sd?" ){
114 print "Content-type: image/png\n\n";
115 binmode(STDOUT);
116 &Graphs::updatehddgraph($querry[0],$querry[1]);
117}elsif ( $querry[0] =~ "nvme?" ){
118 print "Content-type: image/png\n\n";
119 binmode(STDOUT);
120 &Graphs::updatehddgraph($querry[0],$querry[1]);
121}else{
122 &Header::showhttpheaders();
123 &Header::openpage($Lang::tr{'hardware graphs'}, 1, '');
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;
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;
139 }
140 &General::writehash("${General::swroot}/sensors/settings", \%sensorsettings);
141 }
142
143 # This should be save, because no user given content will be processed.
144 #my @disks = `ls -1 /sys/block | grep -E '^sd|^nvme' | sort | uniq`;
145 my @disks = &get_disks();
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();
155 }
156
157 if ( grep(/thermal-thermal_zone/, @sensorsgraphs) ) {
158 &Header::openbox('100%', 'center', "ACPI Thermal-Zone Temp $Lang::tr{'graph'}");
159 &Graphs::makegraphbox("hardwaregraphs.cgi","thermaltemp","day");
160 &Header::closebox();
161 }
162
163 if ( grep(/temperature-/, @sensorsgraphs) ) {
164 &Header::openbox('100%', 'center', "hwtemp $Lang::tr{'graph'}");
165 &Graphs::makegraphbox("hardwaregraphs.cgi","hwtemp","day");
166 Header::closebox();
167 }
168
169 if ( grep(/fanspeed-/, @sensorsgraphs) ) {
170 &Header::openbox('100%', 'center', "hwfan $Lang::tr{'graph'}");
171 &Graphs::makegraphbox("hardwaregraphs.cgi","hwfan","day");
172 &Header::closebox();
173 }
174
175 if ( grep(/voltage-/, @sensorsgraphs) ) {
176 &Header::openbox('100%', 'center', "hwvolt $Lang::tr{'graph'}");
177 &Graphs::makegraphbox("hardwaregraphs.cgi","hwvolt","day");
178 &Header::closebox();
179 }
180
181 if ( @sensorsgraphs ) {
182 sensorsbox();
183 }
184 &Header::closebigbox();
185 &Header::closepage();
186
187}
188
189
190sub sensorsbox {
191 &Header::openbox('100%', 'center', "$Lang::tr{'mbmon settings'}");
192
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>
200END
201;
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>
215 <td align='center' colspan='2' ><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
216</tr>
217</table>
218</form>
219END
220;
221 &Header::closebox();
222}
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}