Home > Shell Scripts, Syswatch > Basic Hardware Inventory

Basic Hardware Inventory

May 13th, 2006

I decided to add a modules to my Syswatch system that would do basic hardware inventory (Processor[speed,number,type, etc] and Memory). Basicly this was so that DBA’s could look at a web page to see what OS and hardware a machine was running. So far each OS has it’s own way of doing this and this is how I am getting the information.

Solaris:

if ($OS == "SunOS") {
                //We are going to look at the output of several commands:
                //psrinfo -p to get number of physcial processors
                //psrinfo to get the total number of cores
                //psrinfo -v to get the processor speed
                //prtconf to get the memory in the machine
                $NumberOfPhysical=shell_exec("/usr/sbin/psrinfo -p");
                $NumberOfVirtual=shell_exec("/usr/sbin/psrinfo | /bin/wc -l | /bin/sed ‘s/ //g’");
                $FirstProcessor=shell_exec("/usr/sbin/psrinfo | /bin/head -1 | /bin/awk ‘{print $1}’");
                $ProcessorSpeed=shell_exec("/usr/sbin/psrinfo $FirstProcessor | /bin/grep operates | /bin/awk ‘{print $6}’");
                $MemorySize=shell_exec("/usr/sbin/prtconf | /bin/grep Memory |/bin/head -1 | /bin/awk ‘{print $3}’");
                //Processor type, different ways for 5.8,5.9 and 5.10
                if ($OSRevision=="5.8") {
                        //psrinfo command does not work for this OS as it is missing the -p option, so get the info out
                        //of prtconf -v grep for Ultra (we don’t really have any machines running 8 that are not Ultra’s anymore
                        //This will also assume that the machine is running the same type of processor for all processors
                        $ProcessorType=shell_exec("/usr/sbin/prtconf | /bin/grep Ultra | /bin/awk -F’,’ ‘{print $2}’ | /bin/awk ‘{print $1}’ | /bin/head -1");
                }
                if ($OSRevision=="5.9") {
                        //Need to find the first processor, do NOT assume that 0 will be it. that does not work on the E25K!
                        //Use the $FirstProcessor from above    
                        $ProcessorType=shell_exec("/usr/sbin/psrinfo -pv $FirstProcessor | /bin/awk ‘{print $2}’");
                }
                if ($OSRevision=="5.10") {
                        //Need to find first processor same as for 5.9, but the output of psrinfo has changed and the
                        //Processor type is on the second line.
                        $ProcessorType=shell_exec("/usr/sbin/psrinfo -pv $FirstProcessor |/bin/tail -1 | /bin/awk ‘{print $1}’");
                }
        }

What the above code does is populate the following Variables:

$NumberOfPhysical : Number of Physical processors in the machine
$NumberOfVirutal: Number of Cores/threads that are in the machine
$FirstProcessor: The Numeric ID of the first processor in the machine. For example in an E25K your first processor could be a non 0 number
$MemorySize: Size of memory in MB
$ProcessorType: The Processor type, i.e. UltraSPARC-III+
$ProcessorSpeed: Speed of the Processor in MHz

For AIX the commands are done a little differently:

if ($OS == "AIX" ) {
                //Going to use the following commands to get the number of processors and memory
                //lsdev -C | grep -c proc
                //lsattr -El proc# to get the speed and processor type
                $NumberOfPhysical=shell_exec("/usr/sbin/lsdev -C | /usr/bin/grep -c proc");
                $ProcessorType=shell_exec("/usr/sbin/lsattr -El proc0 | /usr/bin/grep type | /usr/bin/awk ‘{print $2}’");
                $ProcessorSpeed=shell_exec("/usr/sbin/lsattr -El proc0 | /usr/bin/grep Speed | /usr/bin/awk ‘{print $2}’");
                $ProcessorSpeed=$ProcessorSpeed/1024/1024; //Speed in MHZ
                $MemorySize=shell_exec("/usr/sbin/lsattr -El mem0 | /usr/bin/grep Total | /usr/bin/awk ‘{print $2}’");
                $NumberOfVirtual=0; //Not sure how to find them at the moment as we have no machines that have them            
        }

Same variables are availabe as in Solaris.

For Apple MacOSX you do it like this:

if ($OS == "Darwin") {
                //Use the system_profiler command
                //system_profiler SPHardwareDataType
               
                $NumberOfPhysical=shell_exec("/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/grep \"Number Of CPUs\" | /usr/bin/awk -F’:’ ‘{print $2}’");
                $NumberOfVirtual=shell_exec("/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/grep \"Number Of CPUs\" | /usr/bin/awk -F’:’ ‘{print $2}’");
                //$NumberOfVirtual=shell_exec("/usr/sbin/psrinfo | /bin/wc -l | /bin/sed ‘s/ //g’");
                $ProcessorSpeedTEMP=shell_exec("/usr/sbin/system_profiler SPHardwareDataType | /usr/sbin/grep \"CPU Speed\" | /usr/bin/awk -F’:’ ‘{print $2}’");
                if (ereg("GHz",$ProcessorSpeedTEMP)){
                        $ProcessorSpeed=ereg_replace(" GHz","",$ProcessorSpeedTEMP);
                        $ProcessorSpeed=$ProcessorSpeed*1024;

                $ProcessorType=shell_exec("/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/grep \"CPU Type\" | /usr/bin/awk -F’:’ ‘{print $2}’");
                $MemorySizeTemp=shell_exec("/usr/sbin/system_profiler SPHardwareDataType | /usr/sbin/grep \"Memory\" | /usr/bin/awk -F’:’ ‘{print $2}’");
                if (ereg("GB",$MemorySizeTemp)) {
                        $MemorySize=ereg_replace(" GB","",$MemorySizeTemp);
                        $MemorySize=$MemorySize*1024;
                }
                if (ereg("MB",$MemorySizeTemp) {
                        $MemorySize=ereg_replace(" MB","",$MemorySizeTemp);
                };
        }

Mac was the most intersting to do as I looked around for a while to try and find a command that would output what I wanted. I figured out how to do it while watching the Activity monitor and ran the gui system _profiler command. I decided to run “man system_profiler” and found that you could run it via the command line. Made it really easy to get info out. What I use above is just 1 of many data types that system_profiler will return.

I still have to write the Linux part to this module, and it will use the “/proc” file system with the cpuinfo and meminfo files to get info. Once I get that done next week I will post it as well.

Shell Scripts, Syswatch ,

Comments are closed.