LostS has asked for the wisdom of the Perl Monks concerning the following question:

OK I need to do this:
I have 3 drives on my system. I am doing an SNMP walk. Now I get the values back for all 3 drives in this setup. What I need to do is say:
Drive C - Used Space OID = 1.2.3.4.5.1
Drive C - Size OID = 1.2.3.4.6.1
Drive C - Max Size = 200000
Drive C - Alocation Units = 4096

Drive E - Used Space OID = 1.2.3.4.5.3
Drive E - Size OID = 1.2.3.4.6.3
Drive E - Max Size = 230000
Drive E - Alocation Units = 4096

Drive F - Used Space OID = 1.2.3.4.5.4
Drive F - Size OID = 1.2.3.4.6.4
Drive F - Max Size = 230000
Drive F - Alocation Units = 4096

Virtual Mem - Used Space OID = 1.2.3.4.5.5
Virtual Mem - Size OID = 1.2.3.4.6.5
Virtual Mem - Max Size = 2000
Virtual Mem - Alocation Units = 1024


If you notice you will see that the OID's differance's are the last number referring to the Unit. So 1 is Drive C, 2 is Drive D, 3 is Drive E and so forth. Now I need to be able to get that info and then recall it to make a config file for MRTG. Any suggestions on how to dumb the data to an array or something and then recall it depending on which drive i am looking at??/

-----------------------
Billy S.
Slinar Hardtail - Hand of Dane
Datal Ephialtes - Guildless
RallosZek.Net Admin/WebMaster

perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'

Replies are listed 'Best First'.
Re: Array/Hash?? Help Needed
by bart (Canon) on Jan 14, 2003 at 23:37 UTC
    It seems to me that you are looking for HoH (hash of hashes) or AoH (array of hashes); anyway, a two level data structure. On the first level, there's the id of this particular device, either the name or a numerical ID, as you hinted at. On the second level, there's a record, a hash, containing all the data for this particular device.

    An introduction can be found in perlreftut, which is part of the standard documentation. The complete, authoritive reference is in perlref.

    A bit more practical: I would think that there are mainly two ways to add this data to the data structure: either by field, or by record. I'll give an example of both.

    my %data; # By record: $data{'Drive C'} = { 'Used Space OID' => '1.2.3.4.5.1', 'Size OID' => '1.2.3.4.6.1', 'Max Size' => 200000, 'Alocation Units' => 4096, }; # By field $data{'Drive C'}{'Used Space OID'} = '1.2.3.4.5.1'; $data{'Drive C'}{'Size OID'} = '1.2.3.4.6.1'; $data{'Drive C'}{'Max Size'} = 200000; $data{'Drive C'}{'Alocation Units'} = 4096;
    Now the data structure these produce are identical — provided the hash entry didn't exist yet: $data{'Drive C'} now contains a reference to a hash, which in turn contains all the data for this particular drive. Autovivification will make this secondary hash spring to life in the latter case; for the former, the new anonymous hash replaces whatever was there before.

    With sufficient caution, you can use any hash related function on it, for example keys():

    foreach(sort keys %{$data{'Drive C'}}) { print "$_ = '$data{'Drive C'}{$_}'\n"; }

    Since the value for $data{'Drive C'} is a hash reference, you can copy that value into a scalar — the copy still references to the same hash, you can even modify the original data through it — and access all the data for 'Drive C' in a simpler, possibly even somewhat faster, manner:

    my $h = $data{'Drive C'}; foreach(sort keys %$h) { print "$_ = '$h->{$_}'\n"; }

    As you hinted towards unique numerical ID's, you might use an array instead, pretty much in the same way:

    $data[1] = { 'Used Space OID' => '1.2.3.4.5.1', 'Size OID' => '1.2.3.4.6.1', ... } $data[1]{'Used Space OID'} = '1.2.3.4.5.1';
    Well, HTH.
Re: Array/Hash?? Help Needed
by tall_man (Parson) on Jan 14, 2003 at 23:29 UTC
    If I understand your question correctly, you are trying to put the device data into a file format that would be easy to load into a perl data structure, like a hash of hashes.

    You could read an XML config file with XML::Simple. I think that would be more readable and maintainable than using Data::Dumper.

    For example:

    <config> <drive name="C" used_oid="1.2.3.4.5.1" size_oid="1.2.3.4.6.1"/> <drive name="D" used_oid="1.2.3.4.5.3" size_oid="1.2.3.4.6.3"/> <drive name="E" used_oid="1.2.3.4.5.3" size_oid="1.2.3.4.6.3"/> </config>

    Then pull it in using:

    use strict; use XML::Simple; my $config = XMLin("www.xml"); my $drivename = "C"; print $config->{drive}->{$drivename}->{used_oid},"\n";
    Update: Added an initial problem restatement.
Re: Array/Hash?? Help Needed
by Enlil (Parson) on Jan 14, 2003 at 23:37 UTC
    You could do a hash of hashes in the form of:
    $hash_of_drives{$drive_name}{$attribute};
    All you would need to do is (I am assuming the data is as it is shown above), split on the - and =, and put the values in a hash.

    -enlil

Re: (nrd) Array/Hash?? Help Needed
by newrisedesigns (Curate) on Jan 15, 2003 at 00:49 UTC

    Take a look at perldsc. It has some useful information about creating hashes-of-hashes and such. You can save it to a file using a multi-level database file driver like MLDBM or outputting to a file with Data::Dumper.

    John J Reiser
    newrisedesigns.com