Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
blinkIO/blink-IO.pl
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
146 lines (128 sloc)
2.43 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use Getopt::Long; | |
my (@listb , @lista , @IOa , @IOb , $suma , $sumb , $diff , @traffica , @trafficb ); | |
our %options; | |
our $delay = 2; | |
sub USAGE { | |
return <<'__EOF__'; | |
usage: | |
blink-IO --daemon options | |
--hdd :check HDD IO | |
--net :check Net traffic | |
blink-IO --where :where am I? flash 30 times white | |
__EOF__ | |
} | |
sub get_traffic { | |
my @tra; | |
open(NET,'/proc/net/dev') or die "$!"; | |
while(<NET>) { | |
next unless /net\d\d/; | |
my @net = split(/\s+/); | |
$tra[0] += $net[2]; | |
$tra[1] += $net[9]; | |
} | |
close NET; | |
@tra | |
} | |
sub blink_rec { | |
my $res=diffi(@_); | |
if ($res > 250) { | |
`blink1-tool -l 2 --yellow --flash 5`; | |
} #else { | |
#`blink1-tool --off`; | |
#} | |
} | |
sub blink_trans { | |
my $i=diffi(@_); | |
if ($i>250) { | |
`blink1-tool -l 2 --red --flash 5`; | |
}# else { | |
# `blink1-tool --off`; | |
#} | |
} | |
sub no_blink { | |
my $look=system('blink1-tool','--list'); | |
if ($look > 0) { | |
warn "No Blink Device detected\n"; | |
exit; | |
} | |
} | |
sub get_IO { | |
my ($dump , $IO , @IO); | |
opendir(DIR,"/proc") or die "$!\n"; | |
while(readdir DIR) { | |
next unless /^\d+\d*/; | |
open(FILE,"<","/proc/$_/io") or next; | |
while(<FILE>) { | |
next unless /^write_byte/; | |
($dump,$IO)=split(/:/,$_,2); | |
if ( $IO > 0) { | |
push(@IO,$IO); | |
} | |
} | |
close FILE; | |
} | |
closedir DIR; | |
@IO | |
} | |
sub sum_IO { | |
my $sum; | |
foreach (@_) { | |
$sum += $_; | |
} | |
$sum | |
} | |
sub diffi { | |
use integer; | |
my $diff = (($_[1] - $_[0])/1024/1024)/$delay; | |
$diff | |
} | |
sub blink_IO { | |
my $diff = diffi(@_); | |
if ( $diff >= 250 ) { | |
`blink1-tool -l 1 --blue --flash 5`; | |
} #else { | |
# `blink1-tool --off`; | |
#} | |
} | |
GetOptions ( | |
'daemon' => \$options{'daemon'}, | |
'hdd' => \$options{'hdd'}, | |
'net' => \$options{'net'}, | |
'where' => \$options{'where'}, | |
) or die USAGE; | |
no_blink(); | |
if ($options{'daemon'}) { | |
use POSIX 'setsid'; | |
use Cwd 'chdir'; | |
my $pid =fork; | |
exit 0 if $pid; | |
exit 1 if not defined $pid; | |
setsid(); | |
$pid=fork; | |
exit 0 if $pid; | |
exit 1 if not defined $pid; | |
chdir '/' or die $!; | |
umask 0; | |
if ($options{'hdd'}) { | |
while (1) { | |
@IOa = get_IO(); | |
sleep $delay; | |
@IOb = get_IO(); | |
$suma = sum_IO(@IOa); | |
$sumb = sum_IO(@IOb); | |
blink_IO($suma , $sumb); | |
} | |
} elsif ($options{'net'}) { | |
while (1) { | |
@traffica = get_traffic(); | |
sleep $delay; | |
@trafficb = get_traffic(); | |
blink_trans($traffica[1],$trafficb[1]); | |
blink_rec($traffica[0],$trafficb[0]); | |
} | |
} else { die USAGE; } | |
} elsif ($options{'where'}) { | |
`blink1-tool --flash 30 &>/dev/null &`; | |
} else { die USAGE; } |