Tuesday, May 10, 2005

machine_info.pl: Get your bearings in 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: ~] machine_info.pl cpu_count=1 cpu_speed_mhz=667 cpu_type=Power_Macintosh host_ip=xxx.xx.245.99 host_mac=00:03:93:a3:95:c2 host_name=powerbook.school.edu os=Darwin os_notes=Mac OS X 10.4 (build 8A428) os_version=8.0.0 ram_mbytes=768

And on a work server:

08:17 PM mithras@server6: src] machine_info.pl cpu_count=4 cpu_speed_mhz=3056 cpu_type=i686 host_ip=xxx.xx.22.98 host_mac=00:0E:0C:31:59:CF host_name=server6.school.edu os=Linux os_notes=Red Hat Linux release 9 (Shrike) os_version=2.4.20-31.9.progeny.6smp ram_mbytes=2016
[ Click to show the entire 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"; } }

1 Comments:

Blogger Curious said...

hmm... doesn't appear to handle multiple network interfaces at all...

11:10 AM  

Post a Comment

<< Home