]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - html/cgi-bin/hardwaregraphs.cgi
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / hardwaregraphs.cgi
index a25d511ff3ed1cb04181db7d95a09e42d7eb701d..9e3fe60d5af1a7e6f6a5270944a7d44498ab07e7 100644 (file)
 #!/usr/bin/perl
-#
-# This code is based on hddGraph and mbmongraph
-# written by weizen_42 and distributed under the
-# terms of the GPLv2.
-# http://www.ban-solms.de/t/IPCop-mbmongraph.html
-# http://www.ban-solms.de/t/IPCop-hddgraph.html
-#
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2005-2011  IPFire Team                                        #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
 
 use strict;
 
 # enable only the following on debugging purpose
-# use warnings;
-# use CGI::Carp 'fatalsToBrowser';
+#use warnings;
+#use CGI::Carp 'fatalsToBrowser';
 
 require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
+require "${General::swroot}/graphs.pl";
 
-my %cgiparams=();
-my @cgigraphs=();
-my @graphs=();
+my %color = ();
+my %mainsettings = ();
+&General::readhash("${General::swroot}/main/settings", \%mainsettings);
+&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
 
-&Header::showhttpheaders();
+my %sensorsettings = ();
+
+my @sensorsgraphs = ();
+
+# Main directory where rrdlog puts the sensor data.
+my $sensorsdir = "$mainsettings{'RRDLOG'}/collectd/localhost";
 
-my $graphdir = "/srv/web/ipfire/html/graphs";
+# Open sensors directory.
+opendir(SENSORS, "$sensorsdir") or die "Could not opendir $sensorsdir: $!\n";
 
-&Header::getcgihash(\%cgiparams);
+# Read-in all sensors.
+my @sensor_dirs = readdir(SENSORS);
 
-$ENV{'QUERY_STRING'} =~ s/&//g;
-@cgigraphs = split(/graph=/,$ENV{'QUERY_STRING'});
-$cgigraphs[1] = '' unless defined $cgigraphs[1];
+# Close directory handle.
+closedir(SENSORS);
 
-my %mbmon_settings = ();
-my %mbmon_values = ();
-&General::readhash("/var/log/mbmon-values", \%mbmon_values);
-my $key;
+# Loop through the grabbed sensors.
+foreach my $sensor_dir (@sensor_dirs) {
+       # Skip everything which does not start with "sensors-".
+       next unless $sensor_dir =~ /^sensors-/;
 
-if ( $cgiparams{'ACTION'} eq $Lang::tr{'save'} )
-{
-  $mbmon_settings{'GRAPH_TEMP'} = ($cgiparams{'TEMP'} eq 'on');
-  $mbmon_settings{'GRAPH_FAN'} = ($cgiparams{'FAN'} eq 'on');
-  $mbmon_settings{'GRAPH_VOLT'} = ($cgiparams{'VOLT'} eq 'on');
-  $mbmon_settings{'GRAPH_HDD'} = ($cgiparams{'HDD'} eq 'on');
+       # Check if the omitet element is a directory.
+       next unless (-d "$sensorsdir/$sensor_dir");
 
-  foreach my $line (sort keys %cgiparams) 
-  {
-    if ( index($line, "LINE-") != -1 )
-    {
-      $mbmon_settings{$line} = 'on';
-    }
+       # Open sensor directory and lookup for sensors.
+       opendir(SENSOR_DIR, "$sensorsdir/$sensor_dir") or die "Could not opendir $sensorsdir/$sensor_dir: $!\n";
 
-    if ( index($line, "LABEL-") != -1 )
-    {
-      $mbmon_settings{$line} = $cgiparams{$line};
-    }
-  }
+       # Grab single sensors from the directory.
+       my @sensors = readdir(SENSOR_DIR);
 
-  &General::writehash("${General::swroot}/mbmon/settings", \%mbmon_settings);
+       # Close directory handle.
+       closedir(SENSOR_DIR);
+
+       # Loop through the omited sensors.
+       foreach my $sensor (@sensors) {
+               # Skip everything which is not a regular file.
+               next unless (-f "$sensorsdir/$sensor_dir/$sensor");
+
+               # Add sensor to the array of sensorsgrapghs.
+               push(@sensorsgraphs, "$sensorsdir/$sensor_dir/$sensor");
+       }
 }
-else
-{
-  &General::readhash("${General::swroot}/mbmon/settings", \%mbmon_settings);
+
+# Look for ACPI Thermal Zone sensors.
+my @thermal_zone_sensors = grep(/thermal-thermal_zone/, @sensor_dirs);
+
+# If a thermal zone sensor has been found add it to the sensorsgraphs array.
+if (@thermal_zone_sensors) {
+       # Loop through the array of thermal zone sensors.
+       foreach my $thermal_zone_sensor (@thermal_zone_sensors) {
+               # Add the sensor to the array.
+               push(@sensorsgraphs, "$sensorsdir/$thermal_zone_sensor");
+       }
 }
 
-my $selected_temp = '';
-my $selected_fan = '';
-my $selected_volt = '';
-my $selected_hdd = '';
-
-$selected_temp = "checked='checked'" if ( $mbmon_settings{'GRAPH_TEMP'} == 1 );
-$selected_fan  = "checked='checked'" if ( $mbmon_settings{'GRAPH_FAN'} == 1 );
-$selected_volt = "checked='checked'" if ( $mbmon_settings{'GRAPH_VOLT'} == 1 );
-$selected_hdd = "checked='checked'" if ( $mbmon_settings{'GRAPH_HDD'} == 1 );
-
-my %mbmon_graphs = ();
-foreach $key ( sort(keys %mbmon_values) ) 
-{
-  $mbmon_graphs{$key} = "checked='checked'" if ( $mbmon_settings{'LINE-'.$key} eq 'on' );
-  if ( !defined($mbmon_settings{'LABEL-'.$key}) )
-  {
-    $mbmon_settings{'LABEL-'.$key} = $key;
-  }
+&Header::showhttpheaders();
+&Header::openpage($Lang::tr{'hardware graphs'}, 1, '');
+&Header::openbigbox('100%', 'left');
+
+&Header::getcgihash(\%sensorsettings);
+
+if ( $sensorsettings{'ACTION'} eq $Lang::tr{'save'} ) {
+       foreach(@sensorsgraphs){
+               chomp($_);
+                       $_ =~ /\/(.*)sensors-(.*)\/(.*)\.rrd/;
+                       my $label = $2.$3;$label=~ s/-//g;
+                       if ( $sensorsettings{'LINE-'.$label} ne "on" ){
+                               $sensorsettings{'LINE-'.$label} = 'off';
+                       } elsif ($sensorsettings{'LINE-'.$label} eq "on" ){
+                               $sensorsettings{'LINE-'.$label} = 'checked';
+                       }
+                       $sensorsettings{'LABEL-'.$label} =~ s/\W//g;
+       }
+       &General::writehash("${General::swroot}/sensors/settings", \%sensorsettings);
 }
 
-&Header::openpage($Lang::tr{'harddisk temperature graphs'}, 1, '');
+# This should be save, because no user given content will be processed.
+#my @disks = `ls -1 /sys/block | grep -E '^sd|^nvme' | sort | uniq`;
+my @disks = &get_disks();
 
-&Header::openbigbox('100%', 'left');
+foreach (@disks){
+       my $disk = $_;
+       chomp $disk;
+       my @array = split(/\//,$disk);
 
-###############
-# DEBUG DEBUG
-#&Header::openbox('100%', 'left', 'DEBUG');
-#my $debugCount = 0;
-#foreach my $line (sort keys %cgiparams) {
-#  print "$line = $cgiparams{$line}<br />\n";
-#  $debugCount++;
-#}
-#print "&nbsp;Count: $debugCount\n";
-#&Header::closebox();
-# DEBUG DEBUG
-###############
-
-if ($cgigraphs[1] =~ /(temp|fan|volt)/) 
-{
-  my $graph = $cgigraphs[1];
-  my $graphname = $Lang::tr{"mbmon $cgigraphs[1]"};
-  &Header::openbox('100%', 'center', "$graphname $Lang::tr{'graph'}");
-
-  if (-e "$graphdir/mbmon-${graph}-day.png") 
-  {
-    my $ftime = localtime((stat("$graphdir/mbmon-${graph}-day.png"))[9]);
-    print "<center>";
-    print "<b>$Lang::tr{'the statistics were last updated at'}: $ftime</b></center><br /><hr />\n";
-    print "<img src='/graphs/mbmon-${graph}-day.png' border='0' /><hr />";
-    print "<img src='/graphs/mbmon-${graph}-week.png' border='0' /><hr />";
-    print "<img src='/graphs/mbmon-${graph}-month.png' border='0' /><hr />";
-    print "<img src='/graphs/mbmon-${graph}-year.png' border='0' />";
-  }
-  else 
-  {
-    print $Lang::tr{'no information available'};
-  }
-  &Header::closebox();
-  print "<div align='center'><table width='80%'><tr><td align='center'>";
-  print "<a href='/cgi-bin/mbmongraph.cgi'>";
-  print "$Lang::tr{'back'}</a></td></tr></table></div>\n";
+       &Header::openbox('100%', 'center', "$array[$#array] $Lang::tr{'graph'}");
+       &Graphs::makegraphbox("hardwaregraphs.cgi",$array[$#array],"day");
+       &Header::closebox();
 }
-elsif ($cgigraphs[1] =~ /(hdd)/) 
-{
-  my $graph = $cgigraphs[1];
-  my $graphname = $Lang::tr{"harddisk temperature"};
-  &Header::openbox('100%', 'center', "$graphname $Lang::tr{'graph'}");
-
-    if (-e "$graphdir/hddtemp-day.png")
-     {
-      my $ftime = localtime((stat("$graphdir/hddtemp-day.png"))[9]);
-      print "<center>";
-      print "<b>$Lang::tr{'the statistics were last updated at'}: $ftime</b></center><br /><hr />\n";
-      print "<img src='/graphs/hddtemp-day.png' border='0' /><hr />";
-      print "<img src='/graphs/hddtemp-week.png' border='0' /><hr />";
-      print "<img src='/graphs/hddtemp-month.png' border='0' /><hr />";
-      print "<img src='/graphs/hddtemp-year.png' border='0' />";
-      if ( -e "/var/log/hddgraph_smartctl_out" ) 
-      {
-        my $output = `/bin/cat /var/log/hddgraph_smartctl_out`;
-        $output = &Header::cleanhtml($output);
-        print "<hr><pre>$output</pre>\n";
-      }
-    }
-    else 
-    {
-      print $Lang::tr{'no information available'};
-    }
-  &Header::closebox();
-  print "<div align='center'><table width='80%'><tr><td align='center'>";
-  print "<a href='/cgi-bin/mbmongraph.cgi'>";
-  print "$Lang::tr{'back'}</a></td></tr></table></div>\n";
+
+if ( grep(/thermal-thermal_zone/, @sensorsgraphs) ) {
+       &Header::openbox('100%', 'center', "ACPI Thermal-Zone Temp $Lang::tr{'graph'}");
+       &Graphs::makegraphbox("hardwaregraphs.cgi","thermaltemp","day");
+       &Header::closebox();
 }
-else 
-{
-  if ( $mbmon_settings{'GRAPH_TEMP'} == 1 )
-  {
-    &Header::openbox('100%', 'center', "$Lang::tr{'mbmon temp'} $Lang::tr{'graph'}");
-    if (-e "$graphdir/mbmon-temp-day.png") 
-    {
-      my $ftime = localtime((stat("$graphdir/mbmon-temp-day.png"))[9]);
-      print "<center><b>$Lang::tr{'the statistics were last updated at'}: $ftime</b></center><br />\n";
-      print "<a href='/cgi-bin/mbmongraph.cgi?graph=temp'>";
-      print "<img src='/graphs/mbmon-temp-day.png' border='0' />";
-      print "</a>";
-    }
-    else 
-    {
-      print $Lang::tr{'no information available'};
-    }
-    print "<br />\n";
-    &Header::closebox();
-  }
-
-  if ( $mbmon_settings{'GRAPH_FAN'} == 1 )
-  {
-    &Header::openbox('100%', 'center', "$Lang::tr{'mbmon fan'} $Lang::tr{'graph'}");
-    if (-e "$graphdir/mbmon-fan-day.png") 
-    {
-      my $ftime = localtime((stat("$graphdir/mbmon-fan-day.png"))[9]);
-      print "<center><b>$Lang::tr{'the statistics were last updated at'}: $ftime</b></center><br />\n";
-      print "<a href='/cgi-bin/mbmongraph.cgi?graph=fan'>";
-      print "<img src='/graphs/mbmon-fan-day.png' border='0' />";
-      print "</a>";
-    }
-    else 
-    {
-      print $Lang::tr{'no information available'};
-    }
-    print "<br />\n";
-    &Header::closebox();
-  }
-
-  if ( $mbmon_settings{'GRAPH_VOLT'} == 1 )
-  {
-    &Header::openbox('100%', 'center', "$Lang::tr{'mbmon volt'} $Lang::tr{'graph'}");
-    if (-e "$graphdir/mbmon-volt-day.png") 
-    {
-      my $ftime = localtime((stat("$graphdir/mbmon-volt-day.png"))[9]);
-      print "<center><b>$Lang::tr{'the statistics were last updated at'}: $ftime</b></center><br />\n";
-      print "<a href='/cgi-bin/mbmongraph.cgi?graph=volt'>";
-      print "<img src='/graphs/mbmon-volt-day.png' border='0' />";
-      print "</a>";
-    } 
-    else 
-    {
-      print $Lang::tr{'no information available'};
-    }
-    print "<br />\n";
-    &Header::closebox();
-  }
-
-  if ( $mbmon_settings{'GRAPH_HDD'} == 1 )
-  {
-    &Header::openbox('100%', 'center', $Lang::tr{'harddisk temperature'});
-    if (-e "$graphdir/hddtemp-day.png")
-     {
-      my $ftime = localtime((stat("$graphdir/hddtemp-day.png"))[9]);
-      print "<center>";
-      print "<b>$Lang::tr{'the statistics were last updated at'}: $ftime</b></center><br /><hr />\n";
-      print "<a href='/cgi-bin/hddgraph.cgi?graph=hdd'>";
-      print "<img src='/graphs/hddtemp-day.png' border='0' /><hr />";
-      print "</a>";
-    }
-    else 
-    {
-      print $Lang::tr{'no information available'};
-    }
-    print "<br />\n";
-    &Header::closebox();
-  }
-
-  &Header::openbox('100%', 'center', $Lang::tr{'settings'});
-print <<END
-<form method='post' action='$ENV{'SCRIPT_NAME'}'>
-<table width='100%'>
-<tr><td colspan='2'><input type='checkbox' name='TEMP' $selected_temp />&nbsp;$Lang::tr{'mbmon temp'} $Lang::tr{'graph'}</td></tr>
-<tr><td colspan='2'><input type='checkbox' name='FAN' $selected_fan />&nbsp;$Lang::tr{'mbmon fan'} $Lang::tr{'graph'}</td></tr>
-<tr><td colspan='2'><input type='checkbox' name='VOLT' $selected_volt />&nbsp;$Lang::tr{'mbmon volt'} $Lang::tr{'graph'}</td></tr>
-<tr><td colspan='2'><input type='checkbox' name='HDD' $selected_hdd />&nbsp;$Lang::tr{'harddisk temperature'}-$Lang::tr{'graph'}</td></tr>
-</table>
-<hr />
-<table width='100%' border='0' cellspacing='1' cellpadding='0'>
-<tr><td align='center' width='10%'><b>$Lang::tr{'mbmon display'}</b></td><td align='center' width='15%'>&nbsp;</td><td align='center' width='15%'><b>$Lang::tr{'mbmon value'}</b></td><td align='left'><b>$Lang::tr{'mbmon label'}</b></td></tr>
-END
-;
 
-my $i = 0;
-foreach $key ( sort(keys %mbmon_values) ) 
-{
-  if ( $i % 2 )
-  {
-    print("<tr bgcolor='$Header::table2colour'>");
-  }
-  else 
-  {
-    print("<tr bgcolor='$Header::table1colour'>");
-  }
-  $mbmon_settings{'LABEL-'.$key} = &Header::cleanhtml($mbmon_settings{'LABEL-'.$key});
-  print("<td align='center'><input type='checkbox' name='LINE-$key' $mbmon_graphs{$key}/></td>");
-  print("<td>$key</td><td align='center'>$mbmon_values{$key}</td>\n");
-  print("<td>&nbsp;<input type='text' name='LABEL-$key' value='$mbmon_settings{'LABEL-'.$key}' size='25' /></td></tr>\n");
-  $i++;
+if ( grep(/temperature-/, @sensorsgraphs) ) {
+       &Header::openbox('100%', 'center', "hwtemp $Lang::tr{'graph'}");
+       &Graphs::makegraphbox("hardwaregraphs.cgi","hwtemp","day");
+       Header::closebox();
 }
 
-print <<END
-</table>
+if ( grep(/fanspeed-/, @sensorsgraphs) ) {
+       &Header::openbox('100%', 'center', "hwfan $Lang::tr{'graph'}");
+       &Graphs::makegraphbox("hardwaregraphs.cgi","hwfan","day");
+       &Header::closebox();
+}
 
-<table width='100%'>
-<tr><td class='base' valign='top'>&nbsp;</td><td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr>
-</table>
+if ( grep(/voltage-/, @sensorsgraphs) ) {
+       &Header::openbox('100%', 'center', "hwvolt $Lang::tr{'graph'}");
+       &Graphs::makegraphbox("hardwaregraphs.cgi","hwvolt","day");
+       &Header::closebox();
+}
+
+if ( @sensorsgraphs ) {
+       sensorsbox();
+}
+&Header::closebigbox();
+&Header::closepage();
 
+sub sensorsbox {
+       &Header::openbox('100%', 'center', "$Lang::tr{'mbmon settings'}");
+
+       print <<END
+<form method='post' action='$ENV{'SCRIPT_NAME'}'>
+<table width='100%' border='0' cellspacing='5' cellpadding='0' align='center'>
+<tr>
+       <td align='right' width='40%'><b>$Lang::tr{'mbmon display'}</b></td>
+       <td align='left'><b>$Lang::tr{'mbmon label'}</b></td>
+</tr>
+END
+;
+
+       foreach (@sensorsgraphs){
+                       $_ =~ /\/(.*)sensors-(.*)\/(.*)\.rrd/;
+                       my $label = $2.$3;$label=~ s/-//g;
+                       $sensorsettings{'LABEL-'.$label}="$label";
+                       $sensorsettings{'LINE-'.$label}="checked";
+                       &General::readhash("${General::swroot}/sensors/settings", \%sensorsettings);
+                       print("<tr><td align='right'><input type='checkbox' name='LINE-$label' $sensorsettings{'LINE-'.$label} /></td>");
+                       print("<td><input type='text' name='LABEL-$label' value='$sensorsettings{'LABEL-'.$label}' size='25' /></td></tr>\n");
+       }
+
+       print <<END
+<tr>
+       <td align='center' colspan='2' ><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
+</tr>
+</table>
 </form>
 END
 ;
-  &Header::closebox();
+       &Header::closebox();
 }
 
-&Header::closebox();
+sub get_disks () {
+       my @disks;
 
-&Header::closebigbox();
-&Header::closepage();
+       # Open virtal sys FS and grab block devices.
+       opendir(SYS, "/sys/block") or die "Could not opendir /sys/block/: $!\n";
+
+       # Grab all available block devices.
+       my @blockdevs = readdir(SYS);
+
+       # Close directory handle.
+       closedir(SYS);
+
+       # Loop through the array of blockdevs.
+       foreach my $dev (@blockdevs) {
+               # Skip all devices which does not start with "sd" or "nvme".
+               next unless (( $dev =~ /^sd/) || ($dev =~ /^nvme/));
+
+               # Add the device to the array of disks.
+               push(@disks, $dev);
+       }
+
+       # Remove duplicates.
+       my @disks = &uniq(@disks);
+
+       # Sort the array.
+       my @disks = sort(@disks);
+
+       # Return the array.
+       return @disks;
+}
+
+# Tiny code snipped to get a uniq() like function.
+sub uniq {
+       my %seen;
+       return grep { !$seen{$_}++ } @_;
+}