Simple Perl SysInfo Parser

Perl is an ideal way to parse SysInfo data. You have two choices for getting SysInfo data into Perl. The first is to use the SysInfo Perl API. The second is to write your own simple parser in Perl. If you need basic data such as system model, memory, list of disks, etc, then a simple parser as described here is probably easier than learning the SysInfo Perl API.

When the --format report option is used, the data requested will be in a field deliminated format which is easy to parse. By default the deliminated string used is " | " (SPACE Vertical-Pipe SPACE). Here is some sample, truncated output for class general:

appname|Application Name|SysInfo
appversion|Application Version|6.0 H2
general||hostname|Host Name|dune.magnicomp.com
general||hostaliases|Host Aliases|dune localhost.localdomain
general||hostaddrs|Host Address(es)|127.0.0.1
general||hostid|Host ID|007f0100
general||man|Manufacturer|Dell Corporation
general||manshort|Manufacturer (Short)|Dell
general||manlong|Manufacturer (Full)|Dell Corporation
general||model|System Model|Dimension 4500
general||memory|Main Memory|512 MB
...

The following perl program will run SysInfo, parse the output and report the system model and amount of system memory (RAM).

#!/usr/bin/perl

#
# Location of SysInfo command.
#
my $SysInfoCmd = "/opt/sysinfo/bin/mcsysinfo";

#
# Build command ARRAY.
#
my @Cmd;
push(@Cmd, $SysInfoCmd);
push(@Cmd, "--format");
push(@Cmd, "report");		# Output parsable data
push(@Cmd, "--class");
push(@Cmd, "general");	# Limit data to General system info
push(@Cmd, "--repsep");
push(@Cmd, "%p");		# Use | as field deliminator

#
# Global variables
#
my $SysModel;
my $SysMemory;

#printf "Running %s\n", join(' ', @Cmd);

if (!open(CMD, '-|', @Cmd)) {
    printf stderr ("Failed to open command %s", join(' ', @Cmd));
    exit 1;
}

while (my $Line = ) {
    chomp($Line);
    my @Argv = split('\|', $Line);

    if (lc($Argv[0]) =~ 'general') {	# Class field
	if (lc($Argv[2]) =~ 'model') {	# Keyword field
	    $SysModel = $Argv[4];
	} elsif (lc($Argv[2]) =~ 'memory') {
	    $SysMemory = $Argv[4];
	}
    }
}

close(CMD);

printf "System Model is %s with %s memory\n", $SysModel, $SysMemory;
exit(0);

When the above program is run, you will see output like:

System Model is Dimension 4500 with 512 MB memory