]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/hardwaregraphs.cgi
suricata: Change midstream policy to "pass-flow"
[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
4190b2a6
LAH
93&Header::showhttpheaders();
94&Header::openpage($Lang::tr{'hardware graphs'}, 1, '');
95&Header::openbigbox('100%', 'left');
96
97&Header::getcgihash(\%sensorsettings);
98
99if ( $sensorsettings{'ACTION'} eq $Lang::tr{'save'} ) {
100 foreach(@sensorsgraphs){
101 chomp($_);
102 $_ =~ /\/(.*)sensors-(.*)\/(.*)\.rrd/;
103 my $label = $2.$3;$label=~ s/-//g;
104 if ( $sensorsettings{'LINE-'.$label} ne "on" ){
105 $sensorsettings{'LINE-'.$label} = 'off';
106 } elsif ($sensorsettings{'LINE-'.$label} eq "on" ){
107 $sensorsettings{'LINE-'.$label} = 'checked';
108 }
109 $sensorsettings{'LABEL-'.$label} =~ s/\W//g;
3961e831 110 }
4190b2a6
LAH
111 &General::writehash("${General::swroot}/sensors/settings", \%sensorsettings);
112}
3961e831 113
4190b2a6
LAH
114# This should be save, because no user given content will be processed.
115#my @disks = `ls -1 /sys/block | grep -E '^sd|^nvme' | sort | uniq`;
116my @disks = &get_disks();
2d281532 117
4190b2a6
LAH
118foreach (@disks){
119 my $disk = $_;
120 chomp $disk;
121 my @array = split(/\//,$disk);
2d281532 122
4190b2a6
LAH
123 &Header::openbox('100%', 'center', "$array[$#array] $Lang::tr{'graph'}");
124 &Graphs::makegraphbox("hardwaregraphs.cgi",$array[$#array],"day");
125 &Header::closebox();
126}
2d281532 127
4190b2a6
LAH
128if ( grep(/thermal-thermal_zone/, @sensorsgraphs) ) {
129 &Header::openbox('100%', 'center', "ACPI Thermal-Zone Temp $Lang::tr{'graph'}");
130 &Graphs::makegraphbox("hardwaregraphs.cgi","thermaltemp","day");
131 &Header::closebox();
132}
30189c50 133
4190b2a6
LAH
134if ( grep(/temperature-/, @sensorsgraphs) ) {
135 &Header::openbox('100%', 'center', "hwtemp $Lang::tr{'graph'}");
136 &Graphs::makegraphbox("hardwaregraphs.cgi","hwtemp","day");
137 Header::closebox();
138}
2d281532 139
4190b2a6
LAH
140if ( grep(/fanspeed-/, @sensorsgraphs) ) {
141 &Header::openbox('100%', 'center', "hwfan $Lang::tr{'graph'}");
142 &Graphs::makegraphbox("hardwaregraphs.cgi","hwfan","day");
143 &Header::closebox();
144}
2d281532 145
4190b2a6
LAH
146if ( grep(/voltage-/, @sensorsgraphs) ) {
147 &Header::openbox('100%', 'center', "hwvolt $Lang::tr{'graph'}");
148 &Graphs::makegraphbox("hardwaregraphs.cgi","hwvolt","day");
149 &Header::closebox();
150}
3961e831 151
4190b2a6
LAH
152if ( @sensorsgraphs ) {
153 sensorsbox();
154}
155&Header::closebigbox();
156&Header::closepage();
3961e831 157
2d281532
CS
158sub sensorsbox {
159 &Header::openbox('100%', 'center', "$Lang::tr{'mbmon settings'}");
3961e831 160
2d281532
CS
161 print <<END
162<form method='post' action='$ENV{'SCRIPT_NAME'}'>
163<table width='100%' border='0' cellspacing='5' cellpadding='0' align='center'>
164<tr>
165 <td align='right' width='40%'><b>$Lang::tr{'mbmon display'}</b></td>
166 <td align='left'><b>$Lang::tr{'mbmon label'}</b></td>
167</tr>
3961e831
AF
168END
169;
2d281532
CS
170
171 foreach (@sensorsgraphs){
172 $_ =~ /\/(.*)sensors-(.*)\/(.*)\.rrd/;
173 my $label = $2.$3;$label=~ s/-//g;
174 $sensorsettings{'LABEL-'.$label}="$label";
175 $sensorsettings{'LINE-'.$label}="checked";
176 &General::readhash("${General::swroot}/sensors/settings", \%sensorsettings);
177 print("<tr><td align='right'><input type='checkbox' name='LINE-$label' $sensorsettings{'LINE-'.$label} /></td>");
178 print("<td><input type='text' name='LABEL-$label' value='$sensorsettings{'LABEL-'.$label}' size='25' /></td></tr>\n");
179 }
180
181 print <<END
182<tr>
1d55fccd 183 <td align='center' colspan='2' ><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
2d281532
CS
184</tr>
185</table>
186</form>
3961e831
AF
187END
188;
2d281532 189 &Header::closebox();
3961e831 190}
508547f9
SS
191
192sub get_disks () {
193 my @disks;
194
195 # Open virtal sys FS and grab block devices.
196 opendir(SYS, "/sys/block") or die "Could not opendir /sys/block/: $!\n";
197
198 # Grab all available block devices.
199 my @blockdevs = readdir(SYS);
200
201 # Close directory handle.
202 closedir(SYS);
203
204 # Loop through the array of blockdevs.
205 foreach my $dev (@blockdevs) {
206 # Skip all devices which does not start with "sd" or "nvme".
207 next unless (( $dev =~ /^sd/) || ($dev =~ /^nvme/));
208
209 # Add the device to the array of disks.
210 push(@disks, $dev);
211 }
212
213 # Remove duplicates.
214 my @disks = &uniq(@disks);
215
216 # Sort the array.
217 my @disks = sort(@disks);
218
219 # Return the array.
220 return @disks;
221}
222
223# Tiny code snipped to get a uniq() like function.
224sub uniq {
225 my %seen;
226 return grep { !$seen{$_}++ } @_;
227}