Sample patterns to read a csv file during Discovery, and sync the data into the CMDB

Version 4
    Share This:

    ============================================================================================================================

    How to get Started creating CMDB Syncmapping Extension patterns:  Getting Started creating a CMDB Syncmapping Extension pattern

    Info about custom patterns and syncmappings, how to test, etc:  Helix Support: Discovery custom patterns discussion

    Sample syncmapping 'stubs' / starting points :  Helix Support: Discovery: Sharing my library of syncmapping extension stubs

    Sample patterns and syncmappings posted by Lisa:  https://goo.gl/8KUS3S

    ============================================================================================================================

    DISCLAIMER: Samples provided on the communities are only examples to be tested by the customer on a TEST system.          

    Swim at your own risk.

     

    The (attached) example patterns are used to pull custom data into Discovery via a csv file, and then to push the data to the CMDB,

    Data is to be collected from Windows or Linux/Unix in a file deployed by system admins. This file contains a header and one row of 4-column data, and looks like this:

    ======================= Sample csv file ======================================

    Customer,              Application,        Datacentre,    VMType
    Acme Systems Inc,      MT Srv,             Phoenix,       QA

    ==========================================================================

    Location of files:   

        Windows:    C:\addm\addm.csv
        Linux/Unix:  /addm/addm.csv

     

    To achieve the goal requires 2 TPL modules:

       1) A Discovery pattern to read the csv file and add the attributes during Discovery

       2) A Syncmapping tpl to add the new attributes to the CMDB

     

    TEST STEPS:

    1) Upload both TPL files onto your test Discovery system, from the Manage->Knowledge page

    2) Add the following attributes to the CMDB as character string attributes in the BMC_ComputerSystem class:

             CSV_Customer
            CSV_Application
            CSV_Datacentre
            CSV_VMType

    3) Restart the Discovery services

     

    4) Put the csv files onto some test hosts in this location:

         Windows:    C:\addm\addm.csv

         Linux/Unix:  /addm/addm.csv

     

    5) Scan those hosts that have the csv files in a Discovery scan.

         Check for these attribute values on the scanned hosts:

               csv_customer

               csv_application

               csv_datacentre

               csv_vmtype

     

    6) Allow (or cause) those hosts to be Sync'd to the CMDB.

          Check for these attribute values in the CMDB on the BMC_ComputerSystem form:

            CSV_Customer

            CSV_Application
            CSV_Datacentre
            CSV_VMType

     

    ================================= Discovery pattern below:  csv_file.tpl ==================================

    tpl 1.14 module CSVFile;

     

    pattern CSVFile 1.0

      '''

      Demo pattern to show how a CSV file can be extracted abd host node updated (Windows only)

      '''

      overview

        tags Custom, Windows, CSV;

      end overview;

     

      constants

        csvregex := regex '([^,\n]*),([^,\n]*),([^,\n]*),([^,\n]*)';

      end constants;

     

      triggers

        on h := Host created, confirmed;

      end triggers;

     

      body

        csvfile := none;

        if h.os_class = 'Windows' then

            csvfile := raw 'C:\addm\addm.csv';

        elif h.os_class matches "Linux" or h.os_class matches "Unix" then

            csvfile := raw '/addm/addm.csv';

        else

            log.info("Wrong OS type");

            stop;

        end if;

     

        //

        // Discover CSV file

        //

        log.debug('Trying to find ADDM CSV file=%csvfile% on host=%h.name%');

        f := discovery.fileGet(h, csvfile);

        if not f then

          log.error('Failed to find %csvfile% on %h.name%');

          stop;

        end if;

     

        //

        // Extract data from CSV file

        //

        if not f.content then

          log.info('No data found in %csvfile% on %h.name%');

          stop;

        end if;

     

        data := regex.extractAll(f.content, csvregex);

        if not data or size(data) <> 2 or size(data[1]) <> 4 then

          log.error('Unexpected data format in %csvfile% on %h.name%');

          stop;

        end if;

     

        customer := text.strip(data[1][0]);

        application := text.strip(data[1][1]);

        datacentre := text.strip(data[1][2]);

        vmtype := text.strip(data[1][3]);

     

        //

        // Update Host node

        //

        h.csv_customer := customer;

        h.csv_application := application;

        h.csv_datacentre := datacentre;

        h.csv_vmtype := vmtype;

     

        model.addDisplayAttribute(h, "csv_customer");

        model.addDisplayAttribute(h, "csv_application");

        model.addDisplayAttribute(h, "csv_datacentre");

        model.addDisplayAttribute(h, "csv_vmtype");

     

      end body;

    end pattern;

    ================================= End  Discovery pattern below:  csv_file.tpl ==================================

     

     

    ================================= Syncmapping pattern below:  CMDB.csv_file.tpl ==================================

     

    tpl 1.14 module CMDB.Extension.ComputerSystem_CSVFile_Augment;

    from CMDB.Host_ComputerSystem import Host_ComputerSystem 2.3;

     

    syncmapping ComputerSystem_Augment_csv_fiel 1.0

      '''

      Add attributes to BMC_ComputerSystem CI

      '''

      overview

        tags CMDB, Extension, CSV;

      end overview;

     

      mapping from Host_ComputerSystem.host as host

        // No further traversals required

      end mapping;

     

      body

        computersystem := Host_ComputerSystem.computersystem;

     

        if host.csv_customer then

          computersystem.CSV_Customer := host.csv_customer;

          log.info('Pushing CSV_Customer: %host.csv_customer%');

        end if;

        if host.csv_application then

          computersystem.CSV_Application := host.csv_application;

          log.info('Pushing CSV_Application: %host.csv_application%');

        end if;

        if host.csv_datacentre then

          computersystem.CSV_Datacentre := host.csv_datacentre;

          log.info('Pushing CSV_Datacentre: %host.csv_datacentre%');

        end if;

        if host.csv_vmtype then

          computersystem.CSV_VMType := host.csv_vmtype;

          log.info('Pushing CSV_VMType: %host.csv_vmtype%');

        end if;

      end body;

    end syncmapping;

    ================================= End Syncmapping pattern below:  CMDB.csv_file.tpl ==================================