]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/scripts/hddshutdown
Laedt die USB-Controller von allein...
[people/pmueller/ipfire-2.x.git] / src / scripts / hddshutdown
CommitLineData
6c666a3b
MT
1#!/usr/bin/perl
2#
51585c70 3# IPFire HDD Shutdown state reader
6c666a3b
MT
4#
5# This code is distributed under the terms of the GPL
6#
51585c70 7# 20.08.2007 Maniacikarus - IPFire.org - maniacikarus@ipfire.org
6c666a3b
MT
8#
9
10# begin
11
51585c70
CS
12my @proc = `cat /proc/diskstats`;
13my @devices = `kudzu -qps -c HD | grep device: | cut -d" " -f2 | sort | uniq`;
14my %diskstatus = "";
15my $debug = 1;
16my $status = "unknown";
6c666a3b 17
51585c70
CS
18if ( @ARGV[0] eq "state" ){
19my $hdd = @ARGV[1];
20$status = `hdparm -C /dev/$hdd | tail -1 | cut -d: -f2`;
6c666a3b 21
51585c70
CS
22if ( $status=~/standby/){
23 my $ftime = localtime((stat("/tmp/hddshutdown-$hdd"))[9]);
a45de06e 24 print"<B>Disk $hdd status: <font color=#00FF00>standby</font></B> (since $ftime)";
51585c70
CS
25 }
26else{
a45de06e 27 print"<B>Disk $hdd status: <font color=#FF0000>active</font></B>";
51585c70
CS
28 }
29exit;
30}
6c666a3b 31
51585c70 32if ($debug){print "### Reading Diskstats ###\n";}
6c666a3b 33
51585c70
CS
34foreach (@proc){
35my @line = split(/ +/,$_);
36$diskstatus{$line[3]} =$line[12];
37if ($debug){print "Getting device ".$line[3]." with the following value ".$line[12]."\n";}
38}
6c666a3b 39
51585c70 40if ($debug){print "### Searching for available Disks ###\n";}
6c666a3b 41
51585c70
CS
42foreach (@devices){
43chomp $_;
44if ($debug){print "Device ".$_." has ".$diskstatus{$_}." IO Requests.\n";}
45$status = `hdparm -C /dev/$_ | tail -1 | cut -d: -f2`;
6c666a3b 46
51585c70
CS
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-$_");
6c666a3b 51 }
51585c70
CS
52 elsif ($diskstatus{$_} ne "0" || $status !=~/standby/){
53 if ($debug){print "Device ".$_." is active.\n";}
54 if ( -e "/tmp/hddshutdown-$_" ) { system("unlink /tmp/hddshutdown-$_"); }
6c666a3b 55 }
6c666a3b 56}
6c666a3b 57# end