]> git.ipfire.org Git - ipfire-2.x.git/blame - src/scripts/hddshutdown
hddshutdown: replace kudzu.
[ipfire-2.x.git] / src / scripts / hddshutdown
CommitLineData
6c666a3b 1#!/usr/bin/perl
324bb888
AF
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###############################################################################
6c666a3b 21#
51585c70 22# IPFire HDD Shutdown state reader
6c666a3b 23#
6c666a3b 24
5902dd46 25my @devices = `find /sys/block/* -maxdepth 0 ! -name sr* ! -name loop* ! -name ram* -exec basename {} \\; | sort | uniq`;
56b7a3e2
CS
26my $diskstats = "";
27my $newdiskstats = "";
51585c70 28my $debug = 1;
471a46f7 29my $status = "";
6c666a3b 30
51585c70 31if ($debug){print "### Searching for available Disks ###\n";}
6c666a3b 32
51585c70 33foreach (@devices){
471a46f7 34 chomp $_;
5384809b 35 my @array = split(/\//,$_);
324bb888 36 $diskstats = `cat /var/run/hddstats-$array[$#array] 2>/dev/null`;
471a46f7
CS
37 chomp $diskstats;
38 $newdiskstats = `iostat -d -t $_ | tail -2 | head -1 | awk '{ print \$5","\$6}'`;
39 chomp $newdiskstats;
40 $status = `hdparm -C /dev/$_ | tail -1 | cut -d: -f2`;
41 chomp $status;
42
43 if ($debug){print "Device ".$_." IDE Power status:".$status."\n";}
965f4547 44 if (-e "/var/run/hddshutdown-$array[$#array]" && $status !~/standby/)
471a46f7
CS
45 {
46 if ($debug){print "Remove wrong standby marking\n";}
965f4547 47 if ( -e "/var/run/hddshutdown-$array[$#array]" ) { system("unlink /var/run/hddshutdown-$array[$#array]"); }
471a46f7
CS
48 }
49
50 if ($debug){print "Device ".$_." has ".$newdiskstats." write and read Requests, was ".$diskstats." at last run.\n";}
965f4547 51 if ($diskstats eq $newdiskstats && (! -e "/var/run/hddshutdown-$array[$#array]") )
471a46f7
CS
52 {
53 if ($debug){print "Setting Device ".$_." to standby ... ";}
54 $status = `hdparm -y /dev/$_ 2>&1`;
55 chomp $status;
5384809b 56 if ($status !~/Invalid/)
471a46f7
CS
57 {
58 if ($debug){print "OK\n";}
965f4547 59 system("touch /var/run/hddshutdown-$array[$#array]");
471a46f7
CS
60 }
61 else
62 {
5384809b 63 if ($debug){print "FAIL\n";}
471a46f7
CS
64 }
65 }
66 if ($diskstats ne $newdiskstats)
67 {
68 if ($debug){print "Device ".$_." is active.\n";}
965f4547 69 if ( -e "/var/run/hddshutdown-$array[$#array]" ) { system("unlink /var/run/hddshutdown-$array[$#array]"); }
471a46f7 70 }
965f4547 71 system("echo $newdiskstats > /var/run/hddstats-$array[$#array]");
6c666a3b 72}
3bc32eab 73
6c666a3b 74# end