[racktables-users] Re: Help with Object Import

  • From: Denis Ovsienko <pilot@xxxxxxxxxx>
  • To: racktables-users@xxxxxxxxxxxxx
  • Date: Wed, 1 Apr 2009 11:54:26 +0400

> I am going to migrate 400+ hosts from a home grown inventory database into
> RackTables.
> 
> I figured why not ask for advice to make sure I am not re-inventing the
> wheel.  Does anyone have an example script, or is there a way to use the
> existing RackTables code to import from a flat file?  I can export the data
> into a flat file with the needed fields. Then I'd like to import as much
> information as possible, meaning static data, optional attributes and tags,
> if at all possible.

Ernest, you seem to be the first one.

There are two relatively idependent works here: to export from one
database and to import to another. First you need to stick with the
format of intermediate file, IOW, with the set and ordering of CSV file
columns. After that you make a backup of your RackTables database and
start debugging your import script, which is a bit more, than the
example code from php.net:


$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $cname = $data[0];
        $asset_tag = $data[1];
        commitAddObject ($cname, '', '', 4, $asset_tag);
}

To add data about optional attributes in the same cycle, you need to map
the string retrieved from CSV into key value from the dictionary with
own function. Then, if the mapping was successful, the following pattern
would store the value:

// get the object_id of the last object added
// $object_id = ...

if (($key = os_to_key ($osname)) != NULL)
        // set "SW type"
        commitUpdateAttrValue ($object_id, 4, $key);

if (($key = hardware_to_key ($hwname) != NULL)
        // set "HW type"
        commitUpdateAttrValue ($object_id, 2, $key);

... and so on. You'll definitely have to restore from backup and re-run
the import script several times until the actual result is what you
intended.

-- 

Other related posts: