]> git.ipfire.org Git - ipfire-2.x.git/blob - src/scripts/hddshutdown
Neue Version von hddshutdown
[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 # 20.08.2007 Maniacikarus - IPFire.org - maniacikarus@ipfire.org
8 #
9
10 # begin
11
12 my @proc = `cat /proc/diskstats`;
13 my @devices = `kudzu -qps -c HD | grep device: | cut -d" " -f2 | sort | uniq`;
14 my %diskstatus = "";
15 my $debug = 1;
16 my $status = "unknown";
17
18 if ( @ARGV[0] eq "state" ){
19 my $hdd = @ARGV[1];
20 $status = `hdparm -C /dev/$hdd | tail -1 | cut -d: -f2`;
21
22 if ( $status=~/standby/){
23 my $ftime = localtime((stat("/tmp/hddshutdown-$hdd"))[9]);
24 print"<B>Disk $hdddev status: <font color=#00FF00>standby</font></B> (since $ftime)";
25 }
26 else{
27 print"<B>Disk $hdddev status: <font color=#FF0000>active</font></B>";
28 }
29 exit;
30 }
31
32 if ($debug){print "### Reading Diskstats ###\n";}
33
34 foreach (@proc){
35 my @line = split(/ +/,$_);
36 $diskstatus{$line[3]} =$line[12];
37 if ($debug){print "Getting device ".$line[3]." with the following value ".$line[12]."\n";}
38 }
39
40 if ($debug){print "### Searching for available Disks ###\n";}
41
42 foreach (@devices){
43 chomp $_;
44 if ($debug){print "Device ".$_." has ".$diskstatus{$_}." IO Requests.\n";}
45 $status = `hdparm -C /dev/$_ | tail -1 | cut -d: -f2`;
46
47 if ($diskstatus{$_} eq "0" && $status !=~/standby/){
48 if ($debug){print "Device ".$_." is set to standy.\n";}
49 system("/sbin/hdparm -y /dev/$_");
50 system("touch /tmp/hddshutdown-$_");
51 }
52 elsif ($diskstatus{$_} ne "0" || $status !=~/standby/){
53 if ($debug){print "Device ".$_." is active.\n";}
54 if ( -e "/tmp/hddshutdown-$_" ) { system("unlink /tmp/hddshutdown-$_"); }
55 }
56 }
57 # end