]> git.ipfire.org Git - ipfire-2.x.git/blob - src/scripts/hddshutdown
Merge branch 'master' into openswan26
[ipfire-2.x.git] / src / scripts / hddshutdown
1 #!/usr/bin/perl
2 #
3 # IPFire HDD Shutdown state reader
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 # 18.09.2007 Maniacikarus - IPFire.org - maniacikarus@ipfire.org
8 # 22.09.2007 Arne_F - fitzenreiter.de - arne@fitzenreiter.de
9
10 # begin
11
12 my @devices = `kudzu -qps -c HD | grep device: | cut -d" " -f2 | sort | uniq`;
13 my $diskstats = "";
14 my $newdiskstats = "";
15 my $debug = 1;
16 my $status = "";
17
18 if ($debug){print "### Searching for available Disks ###\n";}
19
20 foreach (@devices){
21 chomp $_;
22 my @array = split(/\//,$_);
23 $diskstats = `cat /var/run/hddstats-$array[$#array]`;
24 chomp $diskstats;
25 $newdiskstats = `iostat -d -t $_ | tail -2 | head -1 | awk '{ print \$5","\$6}'`;
26 chomp $newdiskstats;
27 $status = `hdparm -C /dev/$_ | tail -1 | cut -d: -f2`;
28 chomp $status;
29
30 if ($debug){print "Device ".$_." IDE Power status:".$status."\n";}
31 if (-e "/var/run/hddshutdown-$array[$#array]" && $status !~/standby/)
32 {
33 if ($debug){print "Remove wrong standby marking\n";}
34 if ( -e "/var/run/hddshutdown-$array[$#array]" ) { system("unlink /var/run/hddshutdown-$array[$#array]"); }
35 }
36
37 if ($debug){print "Device ".$_." has ".$newdiskstats." write and read Requests, was ".$diskstats." at last run.\n";}
38 if ($diskstats eq $newdiskstats && (! -e "/var/run/hddshutdown-$array[$#array]") )
39 {
40 if ($debug){print "Setting Device ".$_." to standby ... ";}
41 $status = `hdparm -y /dev/$_ 2>&1`;
42 chomp $status;
43 if ($status !~/Invalid/)
44 {
45 if ($debug){print "OK\n";}
46 system("touch /var/run/hddshutdown-$array[$#array]");
47 }
48 else
49 {
50 if ($debug){print "FAIL\n";}
51 }
52 }
53 if ($diskstats ne $newdiskstats)
54 {
55 if ($debug){print "Device ".$_." is active.\n";}
56 if ( -e "/var/run/hddshutdown-$array[$#array]" ) { system("unlink /var/run/hddshutdown-$array[$#array]"); }
57 }
58 system("echo $newdiskstats > /var/run/hddstats-$array[$#array]");
59 }
60
61 # end