1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #!/usr/bin/perl -w # chordStats.pl - create music based on system status use strict; my $vmStatCmd = "vmstat 1"; # run vmstat every second my $totalPackets = 0; # total of packets received and transmitted my $lineCount = 0; # count number of vmstat output lines my %fields = (); my $count = 0; # the field headers in the vmstat output, useful for referring to them by name for( split " ", "r b swpd free buff cache si so bi bo in cs us sy id wa" ){ $fields{$_} = $count; $count++; } # buffering output must be turned off because fluidsynth does not appear to # accept buffered input from stdin $|=1; |
1 2 3 4 5 6 7 8 9 10 11 12 13 | # open the vmstat program to read from open( INPIPE, "$vmStatCmd |" ) || die "can't read from vmstat"; # wait while the fluidsynth program opens sleep(3); while(my $statLine = <INPIPE> ){ # ignore the header display, and the fieldname display lines if( $statLine !~ /\-\-\-\-/ && $statLine !~ /swpd/ ){ # the first line of vmstat data is a recent average, ignore if( $lineCount > 2 ){ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # reset wavetable synthesis if( $totalTime % 10 == 0 ){ print "reset\n" } $totalTime ++; my $note = ""; my @currLine = split " ", $statLine; # user cpu usage $note = $currLine[ $fields{us} ]; sendNote( $note, 14, 12, 96 ); # conglomerate disk i/o fields to one stat $note = $currLine[ $fields{bi} ] + $currLine[ $fields{bo} ]; if( $note > 1000 ){ $note = 1000; } $note = $note/10; sendNote( $note, 8, 12, 96 ); # network throughput on eth0 $note = getNetworkStats(); sendNote( $note, 5, 12, 84 ); }#if not first 3 lines to ignore }#if not a header line $lineCount++; }#while reading the pipe close(INPIPE); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | sub sendNote { my( $noteVal, $noteChan, $min, $max ) = @_; if( $noteVal < $min ){ $noteVal = $min; }else{ # divide it into twelve parts $noteVal = sprintf( "%0.0f", $noteVal/12); # reduce the note to 12 at the very least; $noteVal = ($noteVal * 12); if( $noteVal > $max ) { $noteVal = $max } }#if note is > minimum print "noteon $noteChan $noteVal 100\n"; }#sendNote |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | sub getNetworkStats { my $networkCmd = "/sbin/ifconfig eth0 | grep 'RX bytes'"; $networkCmd = `$networkCmd`; my $rxBytes = 0; my $txBytes = 0; chomp($networkCmd); for( $networkCmd ){ $rxBytes = substr($_, 19); $rxBytes = substr($rxBytes,0,index($rxBytes," ")); $txBytes = substr($_, 52); $txBytes = substr($txBytes,0,index($txBytes," ")); my $bothBytes = $rxBytes + $txBytes; if( $totalPackets == 0 ){ $totalPackets = $bothBytes; }else{ # find the difference between measurements, set maximum difference to # 1Mbit, which works well for `saturated' on a 100Mbit/sec network # reduce the value by a factor of 10000, which spreads the usage # nicely over 1-100 my $diffRX = $bothBytes - $totalPackets; if( $diffRX > 1000000 ){ $diffRX = 1000000 } $diffRX = ($diffRX / 10000); $totalPackets = $bothBytes; return( $diffRX ); }# if not first packet check }# packet count check }#getNetworkStats |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |