[slightly off topic] /proc/penguin debacle

Patrick St. Jean (psj@cgmlarson.com)
Fri, 11 Jul 1997 11:19:08 -0500 (CDT)


I screwed up totally... here's a perl script that will do the same thing
better (and in user space too!) I'm sorry for the messup... just say ni!
at me for a while... Just run this with a -r flag from your rc files so
the reboot count gets updated... it gives #reboots over current uptime.
If you want to reset it, just rm the data file. it'll start over then...

--- begin perl script ---
#!/usr/bin/perl
use strict;

my $DATAFILE;
my $REBOOTS = 0;
my $REBOOTFLAG;
my $LINE;
my $SECONDS;
my $TIME;
my $PENGUINS;
my $PREFIX;

$DATAFILE = "/etc/penguin.data";

# is this being called to update the number
# of reboots?

if(defined($ARGV[0])){
if($ARGV[0] =~ /-r/io){
$REBOOTFLAG = 1;
}else{
print(STDERR "Usage: penguin [-r]\n");
print(STDERR " -r: increment reboot count\n");
exit 0;
}
}

# if we don't have a data file, make one.
# it holds number of reboots...

if(! -f $DATAFILE){
open(DATA, ">$DATAFILE") || die "Couldn't open $DATAFILE for writing!\n";
print(DATA "1");
close(DATA);
if($REBOOTFLAG){
exit 0;
}
}

# get the number of reboots

open(DATA, "<$DATAFILE") || die "Couldn't open $DATAFILE for reading!\n";
$REBOOTS = <DATA>;
close(DATA);

# write out the new number of reboots and
# exit

if($REBOOTFLAG){
$REBOOTS++;
open(DATA, ">$DATAFILE") || die "Couldn't open $DATAFILE for writing!\n";
print(DATA "$REBOOTS");
close(DATA);
exit 0;
}
# check to see that /proc is mounted

if(! -d "/proc"){
print(STDERR "/proc must be mounted!!!\n");
exit 1;
}

# get our uptime. First of 2 fields

open(UPTIME, "</proc/uptime") || die "Couldn't open /proc/uptime for reading!\n";
$LINE = <UPTIME>;
close(UPTIME);

$SECONDS = (split(" ", $LINE, 2))[0];
$TIME = $SECONDS / 86400;
$PENGUINS = $REBOOTS / $TIME;

if($PENGUINS ge 1){
$PREFIX = "";
}elsif($PENGUINS ge .1){
$PREFIX = "deci";
$PENGUINS *= 10;
}elsif($PENGUINS ge .01){
$PREFIX = "centi";
$PENGUINS *= 100;
}elsif($PENGUINS ge .001){
$PREFIX = "milli";
$PENGUINS *= 1000;
}elsif($PENGUINS ge .000001){
$PREFIX = "micro";
$PENGUINS *= 1000000;
}elsif($PENGUINS ge .000000001){
$PREFIX = "nano";
$PENGUINS *= 1000000000;
}elsif($PENGUINS ge .000000000001){
$PREFIX = "pico";
$PENGUINS *= 1000000000000;
}

print(STDOUT "$PENGUINS ${PREFIX}penguins\n");
exit 0;
--- end perl script ---

Again, I'm sorry for being such an indiot...

Pat

-- 
+-----------------------------------------------------------------------------+
| Patrick St. Jean                                          psj@cgmlarson.com |
| Programmer & Systems Administrator                     +1 713-977-4177 x106 |
| Larson Software Technology                         http://www.cgmlarson.com |
+-----------------------------------------------------------------------------+