Skip to content

Instantly share code, notes, and snippets.

@arran4
Created June 9, 2013 01:18
Show Gist options
  • Save arran4/5737211 to your computer and use it in GitHub Desktop.
Save arran4/5737211 to your computer and use it in GitHub Desktop.
Some computer "stats" script I found. It COULD be mine from high school...
#!/usr/bin/perl -w
print compstats();
print "\n";
sub cpustats
{
my (@cpuinfo, $processors, $output, @linesplit);
$processors = 0;
$output = "";
open (FILE,"</proc/cpuinfo");
@cpuinfo = <FILE>;
close (FILE);
chomp @cpuinfo;
foreach $line (@cpuinfo)
{
$processors++ if ($line =~ /^processor/);
@linesplit = split(/\:/,$line);
if ($line =~ /^model\ name/)
{
$output .= "\nA$linesplit[1] with";
} elsif ($line =~ /^cpu\ MHz/)
{
$output .= "$linesplit[1] MHz";
}
}
$output = "There are $processors Processors$output"
if ($processors > 1);
$output = "The processor is $output" if ($processors == 1);
return $output;
}
sub ramstats
{
my (@raminfo, $output, @linesplit, $mem, $memtype, $memsize, $memusage);
open (FILE,"</proc/meminfo");
@raminfo = <FILE>;
close (FILE);
chomp @raminfo;
foreach $line (@raminfo)
{
$mem = "";
@linesplit = split(/\ \ */,$line);
if (($linesplit[0] eq "Mem:") || ($linesplit[0] eq "Swap:"))
{
$memtype = "ram";
$memtype = "swap" if ($linesplit[0] eq "Swap:");
$memsize = int($linesplit[1] / 1024 / 1024) . "mb";
$memusage= int($linesplit[2] / $linesplit[1] * 100)."%";
$mem = "There is a total of $memsize $memtype and of "
."which $memusage is used";
}
if ($mem)
{ if (!$output)
{ $output = $mem; }
else
{ $output = "$output\n$mem"; }
}
}
return $output;
}
sub compstats
{
return cpustats() . "\n" . ramstats() . "\n" . ethstats() . "\n"
. osversion() . "\n" . loadavg();
}
sub fixspaces
{
($_) = @_;
s/\:/\ /g;
s/\ \ \ */\ /g;
s/^\ //;
return $_;
}
sub ethstats
{
my (@netinfo, $output, @linesplit, $txmbqty, $rxmbqty, $txqty, $rxqty);
my ($ethname) = @_;
$ethname = "eth0" if (!$ethname);
my ($line, $avgrx, $avgtx);
open (FILE,"</proc/net/dev");
@netinfo = <FILE>;
close (FILE);
chomp @netinfo;
foreach $line (@netinfo)
{
@linesplit = split(/\ /,fixspaces($line));
if ($linesplit[0] eq $ethname)
{
$txqty = int($linesplit[2]);
$rxqty = int($linesplit[10]);
$txmbqty = int($linesplit[1] / 1024 / 1024);
$rxmbqty = int($linesplit[9] / 1024 / 1024);
$avgrx = int($linesplit[9] / $rxqty);
$avgtx = int($linesplit[1] / $txqty);
$output = "$ethname has transmited $txmbqty mb in "
."$txqty packets (average $avgtx b) and recieved "
."$rxmbqty mb in $rxqty packets (average $avgrx b).";
}
}
return $output;
}
sub osversion
{
my (@osverbody, $output);
open (FILE, "</proc/version");
@osverbody = <FILE>;
close(FILE);
chomp @osverbody;
@osverbody = split(/\ \(/,$osverbody[0]);
return $osverbody[0];
}
sub loadavg
{
my (@loadavgbd, $output);
open (FILE, "</proc/loadavg");
@loadavgbd = <FILE>;
close(FILE);
chomp @loadavgbd;
@loadavgbd = split(/\ /,$loadavgbd[0]);
$output = "Load over 1 min $loadavgbd[0], load over 5 min $loadavgbd[1]"
.", load over 15 min $loadavgbd[2].\nActive Processes $loadavgbd[3]"
.", next free process ID $loadavgbd[4].";
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment