]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/perl | |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2007-2011 IPFire Team <info@ipfire.org> # | |
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 | # IPFire HDD Shutdown state reader | |
23 | # | |
24 | ||
25 | # Also devices that cannot shutdown must be here for mediagraphs. | |
26 | my @devices = `ls -1 /sys/block | grep -E '^sd|^xvd|^vd|^md' | sort | uniq`; | |
27 | my $diskstats = ""; | |
28 | my $newdiskstats = ""; | |
29 | my $debug = 1; | |
30 | my $status = ""; | |
31 | ||
32 | if ($debug){print "### Searching for available Disks ###\n";} | |
33 | ||
34 | foreach (@devices){ | |
35 | chomp $_; | |
36 | my @array = split(/\//,$_); | |
37 | $diskstats = `cat /var/run/hddstats-$array[$#array] 2>/dev/null`; | |
38 | chomp $diskstats; | |
39 | $newdiskstats = `iostat -d -t $_ | tail -2 | head -1 | awk '{ print \$5","\$6}'`; | |
40 | chomp $newdiskstats; | |
41 | $status = `hdparm -C /dev/$_ | tail -1 | cut -d: -f2`; | |
42 | chomp $status; | |
43 | ||
44 | if ($debug){print "Device ".$_." IDE Power status:".$status."\n";} | |
45 | if (-e "/var/run/hddshutdown-$array[$#array]" && $status !~/standby/) | |
46 | { | |
47 | if ($debug){print "Remove wrong standby marking\n";} | |
48 | if ( -e "/var/run/hddshutdown-$array[$#array]" ) { system("unlink /var/run/hddshutdown-$array[$#array]"); } | |
49 | } | |
50 | ||
51 | if ($debug){print "Device ".$_." has ".$newdiskstats." write and read Requests, was ".$diskstats." at last run.\n";} | |
52 | if ($diskstats eq $newdiskstats && (! -e "/var/run/hddshutdown-$array[$#array]") ) | |
53 | { | |
54 | if ($debug){print "Setting Device ".$_." to standby ... ";} | |
55 | $status = `hdparm -y /dev/$_ 2>&1`; | |
56 | chomp $status; | |
57 | if ($status !~/Invalid/) | |
58 | { | |
59 | if ($debug){print "OK\n";} | |
60 | system("touch /var/run/hddshutdown-$array[$#array]"); | |
61 | } | |
62 | else | |
63 | { | |
64 | if ($debug){print "FAIL\n";} | |
65 | } | |
66 | } | |
67 | if ($diskstats ne $newdiskstats) | |
68 | { | |
69 | if ($debug){print "Device ".$_." is active.\n";} | |
70 | if ( -e "/var/run/hddshutdown-$array[$#array]" ) { system("unlink /var/run/hddshutdown-$array[$#array]"); } | |
71 | } | |
72 | system("echo $newdiskstats > /var/run/hddstats-$array[$#array]"); | |
73 | } | |
74 | ||
75 | # end |