aaron.harnly.net

machine_info.pl: Getting your bearings on a new machine

Continuing the spate of brain-dumps of scripts I use often, this is a favorite: machine_info.pl

I’m often hunting around for machines to run jobs on, and so log into a bunch of different machines to find one well-suited to my task. So when I log into a machine, I often want to get a quick sense of its capabilities. I got tired of trying to remember that Solaris uses /usr/sbin/psrinfo, while Linux uses /proc/cpuinfo, and which lines to grep for in each. So I wrote this script.

I also find this script handy for my duties as part-time administrator; it helps me keep tabs on which machines we’ve upgraded to how much RAM, etc.

Example output — at home:

[11:14 AM mithras@powerbook: ~] machineinfo.pl cpucount=1 cpuspeedmhz=667 cputype=PowerMacintosh hostip=xxx.xx.245.99 hostmac=00:03:93:a3:95:c2 hostname=powerbook.school.edu os=Darwin osnotes=Mac OS X 10.4 (build 8A428) osversion=8.0.0 rammbytes=768

And on a work server:

08:17 PM mithras@server6: src] machineinfo.pl cpucount=4 cpuspeedmhz=3056 cputype=i686 hostip=xxx.xx.22.98 hostmac=00:0E:0C:31:59:CF hostname=server6.school.edu os=Linux osnotes=Red Hat Linux release 9 (Shrike) osversion=2.4.20-31.9.progeny.6smp ram_mbytes=2016

Here’s the script:

#!/usr/bin/perl -w
use strict;
 
#
# FILE: machine_info.pl
# AUTHOR: Mithras THe Prophet (mithras.the.prophet, which is a gmail account)
# DATE: June 2004
#
# This script runs on Linux, Solaris, and Mac OS X machines
#  to give you a quick rundown of the setup of the machine:
#  The processor type and speed, memory, disks, etc.
#  
# I find this useful when I've just logged into an unfamiliar machine
#  and want to get my bearings.
#
 
 
#
#
#---- settings
 
#-----
#
#
 
#
#----- globals
 my %info;
#--
#
 
 
#
# Step 0: Determine platform
#
my $PLATFORM;
{
 my $uname_s = `uname -s`;
 chomp $uname_s;
 my $uname_m = `uname -m`;
 chomp $uname_m;
 $uname_m =~ s/ /_/g;
 $PLATFORM=$uname_s . "-" . $uname_m;
 
# print STDERR "## machineinfo\n";
# print STDERR "##\n";
# print STDERR "## running on $PLATFORM\n";
# print STDERR "##\n";
}
 
#
# os
#
{
 my $uname_s = `uname -s`;
 chomp $uname_s;
 
 $info{'os'} = $uname_s;
}
 
 
 
#
# cpu_count
#
{
 my $cpu_num="1";
 if ( $info{'os'} eq "Darwin" )
 {
  my $command = '/usr/sbin/sysctl hw.ncpu | awk \'{print $2}\' ';
  $cpu_num = `$command`;
  chomp $cpu_num;
 }
 elsif ( $info{'os'} eq "Linux" )
 {
  my $command = 'awk \'/processor/ { max_num=$3 } END { print max_num " +1"} \' /proc/cpuinfo | bc ';
  $cpu_num = `$command`;
  chomp $cpu_num;
 }
 elsif ( $info{'os'} eq "SunOS" )
 {
  my $command = '/usr/sbin/psrinfo -v | perl -ne \' BEGIN { $count = 0 } if (/processor (\d+)/) { $count++ } END { print ($count) }\' ';
  $cpu_num = `$command`;
  chomp $cpu_num;
 }
 
 $info{'cpu_count'} = $cpu_num;
}
 
 
#
# cpu_speed_mhz
#
{
 my $cpu_speed="unknown";
 
 if ( $info{'os'} eq "Darwin" )
 {
  my $command = '/usr/sbin/sysctl hw.cpufrequency | awk \'{print $2 "/ 1000000"}\' | bc';
  $cpu_speed=`$command`;
  chomp $cpu_speed;
#  $cpu_speed .= " MHz";
 }
 elsif ( $info{'os'} eq "Linux" )
 {
  my $command = 'cat /proc/cpuinfo | perl -ne \'if (/cpu MHz\s+: (\d+)/) { print "$1\n" unless ($done); $done=1 } \' ';
  $cpu_speed=`$command`;
  chomp $cpu_speed;
#  $cpu_speed .= " MHz";
 }
 elsif ( $info{'os'} eq "SunOS" )
 {
  my $command = '/usr/sbin/psrinfo -v | perl -ne \'if (/(\d+) MHz/) { print "$1\n" unless ($done); $done=1 }\'  ';
  $cpu_speed=`$command`;
  chomp $cpu_speed;
#  $cpu_speed .= " MHz";
 }
 
 $info{'cpu_speed_mhz'} = $cpu_speed;
}
 
#
# config_cpu_type
#
{
 my $uname_m = `uname -m`;
 chomp $uname_m;
 $uname_m =~ s/ /_/g;
 
 $info{'cpu_type'} = $uname_m;
}
 
 
#
# hostname
#
{
 my $hostname=`hostname`;
 chomp $hostname;
 
 $info{'host_name'} = $hostname;
}
 
 
#
# host_ip
#
{
 my $dig_command="dig " . $info{'host_name'} . " | awk 'BEGIN { ans=0 } /;;/ { if (ans==1) { ans=0 } } /ANSWER SECTION/ { ans=1 } /" 
  . $info{'host_name'} . "/ { if (ans==1) { print \$5 } } '";
 my $ip = `$dig_command`;
 chomp $ip;
 
 $info{'host_ip'} = $ip; 
}
 
#
# host_mac
#
{
 my $mac = "unknown";
 if ( $info{'os'} eq "Darwin" )
 {
  my $command = '/sbin/ifconfig | perl -ne ' . "'" . 'if (/en0/) { $next = 1; } ; if (/ether\s+(.*)/) { if ($next==1) { print $1; $next=0; } }' . "'";
  $mac=`$command`
 }
 elsif ( $info{'os'} eq "Linux" )
 {
  my $command = '/sbin/ifconfig  | awk ' . "'" . '/eth0/ {print $5}' . "'";
  $mac=`$command`;
 }
 elsif ( $info{'os'} eq "SunOS" )
 {
  my $command = 'arp `hostname` | awk ' . "'" . '{print $4}' . "'";
  $mac=`$command`;
 }
 chomp $mac;
 $info{'host_mac'} = $mac;
}
#
# OS-version
#
{
 my $uname_r = `uname -r`;
 chomp $uname_r;
 
 $info{'os_version'} = $uname_r;
}
 
#
# OS-notes
#
{
 my $os_flavor="";
 if ($info{'os'} eq "Darwin")
 {
  if (-e '/usr/bin/sw_vers')
  {
   my $productName = `/usr/bin/sw_vers -productName`;
   chomp $productName;
   my $productVersion = `/usr/bin/sw_vers -productVersion`;
   chomp $productVersion;
   my $buildVersion = `/usr/bin/sw_vers -buildVersion`;
   chomp $buildVersion;
   $os_flavor = "$productName $productVersion (build $buildVersion)";
  }
 }
 elsif ($info{'os'} eq "Linux")
 {
  if (-f '/etc/redhat-release')
  {
   $os_flavor=`cat '/etc/redhat-release'`;
   chomp $os_flavor;
  }
  else
  {
   $os_flavor="unknown Linux distro";
  }
 }
 elsif ($info{'os'} eq "SunOS")
 {
  if (-f '/etc/release')
  {
   $os_flavor=`head -n 1 '/etc/release'`;
   chomp $os_flavor;
   $os_flavor =~ s/^\s+//;
  }
 }
 $info{'os_notes'} = $os_flavor;
}
 
#
# ram
#
{
 my $mem="unknown";
 if ($info{'os'} eq "Darwin")
 {
  my $command = 'sysctl hw.memsize | awk \'{print $2 " / (1024*1024)"}\' | bc';
  $mem = `$command`;
  chomp $mem;
#  $mem .= " MB";
 }
 elsif ( $info{'os'} eq "Linux" )
 {
  my $command = 'cat /proc/meminfo | perl -ne \'if (/Mem:\s+(\d+)/) { print int( $1 / (1024 * 1024) ) }\' ';
  $mem = `$command`;
  chomp $mem;
#  $mem .= " MB";
 }
 elsif ( $info{'os'} eq "SunOS" )
 {
  my $command = '/usr/local/bin/sysinfo -msglevel terse -show "memory"';
  $mem = `$command`;
  chomp $mem;
  if ($mem =~ m/(\d+) MB/) # chop out the ' MB' part
  {
   $mem = $1;
  }
 }
 $info{'ram_mbytes'} = $mem;
}
 
#########
# print out info
foreach my $key (sort keys %info)
{
 if ($key ne "")
 {
  my $value = $info{$key};
  print "$key=$value\n";
 }
}
Digg this     Create a del.icio.us Bookmark     Add to Newsvine

No Responses to “machine_info.pl: Getting your bearings on a new machine”

No comments yet

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word