User Tools

Site Tools


extensions:itop-data-collector-base
Translations of this page:

Data Collector Base

name:
Data Collector Base
version:
1.0.10
release:
2015-06-30
description:
Base module for writing data collection applications
itop-version:
2.0
keyword:
inventory, data synchronization
dependencies:
itop-config-mgmt
author:
Combodo
download:
http://www.combodo.com/itop-extensions/itop-data-collector-base-1.0.10-97.zip

This module provides the base for creating an industrial data collection and synchronization application for iTop. Developers can rely on this module to perform all the heavy lifting related to the iTop data import and synchronization process in order to focus on the data collection.

Features

  • Simple API to make the creation of a new collector fast and easy.
  • Automatic creation and update of Synchronization Data Sources in iTop, based on JSON definitions.
  • Support small variations to the target Data Model via explicitely “Optional” attributes
  • Basic validation of the CSV format compared to the expected fields in the synchro data source.
  • Data upload and synchronization by chunks of max_chunk_size (configurable).
  • Extensible mechanism for handling configuration parameters.
  • Extensible list of placeholders in the JSON definition.
  • Automatic management (via placeholders) of the contact to notify and the user to use for running the synchro.
  • Validation of the minimum version of PHP and the needed extensions (as specified by the collector).
  • Command line tool to produce the JSON definition from an existing Synchro Data Source in iTop.
  • Capability to run independently: the configuration of the data sources, the data collection and the data synchronization.
  • Configurable log level (for console output or syslog logging)
  • Simple framework for quickly creating SQL based collectors

Revision History

Version Release Date Comments
1.0.10 2015-06-30 New class of collector: MySQLCollector which forces the retrieved data to be encoded in UTF-8.
1.0.9 2015-06-09 Performance enhancement: retrieve only the needed fields when building a lookup table.
1.0.8 2015-06-02 Better checking of files access rights for writing. SQL connection string (for SQL collectors) is now fully configurable.
1.0.7 2015-05-20 Bug fixes: Support of backslashes in file names. Removed a warning by marking Utils::Substitute() static.
1.0.6 2015-05-13 Added the support of “ignoring” some rows in the data while re-processing them. SQL collector can be configured to safely ignore some fields.
1.0.4 2015-02-16 Added the configuration parameter stop_on_synchro_error.
1.0.3 2015-01-06 Handling of non UTF-8 data (via the overloading of GetCharset()), error checking for the data import phase, optimization for iTop 2.1.0: ignoring any change in the database_table_name field.
1.0.2 2014-11-03 Added the base class SQLCollector for easily creating SQL based collectors.
1.0.1 2014-10-11 Added the method AttributeIsOptional to handle variations in the target Data Model.
1.0.0 2014-05-13 First version

Limitations

  • Data upload to iTop is done only via the syncho_import web service (could use the command line version or direct SQLcommands. TBD later, maybe)
  • Prior to the revision 3805 of iTop from SVN (from 2015-10-12!) the collector will NOT work properly if the account used to connect to iTop is not configured to use English as the language !!

Requirements

  • PHP Version 5.3.0 (support of namespaces may be required by some collectors)
  • An access to the iTop web services (REST + synchro_import.php and synchro_exec.php)

Installation

  • Expand the content of the zip archive on a folder on the machine were the collector will run.
  • Edit the content of the file conf/params.local.xml to suit your installation.

Configuration

At minimum, edit the file params.local.xml and adjust the following values:

  <itop_url>https://localhost/</itop_url>
  <itop_login>admin</itop_login>
  <itop_password>admin</itop_password>
Parameter Meaning Sample value
itop_login Login (user account) for connecting to iTop. Must have admin rights for executing the data synchro. admin
itop_password Password for the iTop account.
itop_url URL to the iTop Application https://localhost/itop

Optional parameters

The following parameters can be redefined to alter the default behavior of the collector:

Parameter Meaning Default value
max_chunk_size Maximum number of elements to process in one iteration (for upload and synchro in iTop). If there are more elements than this number, the process will automatically iterate. 1000
itop_synchro_timeout Timeout for waiting for the execution of one data synchro task (in seconds). 600
stop_on_synchro_error Whether or not to stop when an error occurs during a synchronization (yes or no). no

The file params.distrib.xml contains the default values for the parameters. Both files (params.distrib.xml and params.local.xml) use exactly the same format. But params.distrib.xml is considered as the reference and should remain unmodified. Should you need to change the value of a parameter, copy and modify its definition in params.local.xml. The values in params.local.xml have precedence over the ones in params.distrib.xml

Usage

To launch the data collection and synchronization with iTop, run the following command (from the root directory where the application is installed):

php exec.php

The following (optional) command line options are available:

Option Meaning default value
--console_log_level=<level> Level of ouput to the console. From -1 (none) to 9 (debug). 6 (info)
--collect_only Run only the data collection, but do not synchronize the data with iTop false
--synchro_only Synchronizes the data previously collected (stored in the data directory) with iTop. Do not run the collection. false
--configure_only Check (and update if necessary) the synchronization data sources in iTop and exit. Do NOT run the collection or the synchronization
--max_chunk_size=<size> Maximum number of items to process in one pass, for preserving the memory of the system. If there are more items to process, the application will iterate. 1000

Creating a collector

The specifics about a collector resides inside the “collectors” folder. There must be at least one file main.php inside this folder. The purpose of main.php is to register all the Collector classes for your module and load the corresponding classes (either via require_once(…) or by registering an auto-loader).

A collector is a PHP class that provides the data for a given Synchronization Data Source. Collector classes are derived from the abstract Collector class. Each collector is associated with a Synchronization Data Source, defined in JSON format. The default implementation simply looks for a JSON file with the same name as the collector class and the extension “.json”, in the collectors folder.

Specifying required extensions

If your collector needs a specific extension (or a minimum PHP version), you can indicate this dependency by calling the static method Orchestrator::AddRequirement($sMinRequiredVersion, $sExtension = 'PHP') in main.php:

For example:

Orchestrator::AddRequirement('5.4.0'); //This code requires at least PHP 5.4
Orchestrator::AddRequirement('1.2.0', 'ldap'); //This code requires the ldap extension, with at least version 1.2.0

Creating the JSON definition file

The simpler way to create the JSON file for a Synchro Data Source, is to export the definition of an existing data source.

  • Create the synchronisation data source in iTop, adjust its parameters (attributes, etc.) to suit your needs
  • Use the command line tool dump_task.php (available in the toolkit folder to produce the JSON file:
php toolkit/dump_task.php --task_name="name of the task to export" > myCollector.json

Inside your data source definition you can use special placeholders to make the data source configurable by the user of the application, or to adjust its behavior via some special settings:

Placeholder code Meaning Sample value
$version$ The version of the module. Useful for versioning your application, for example in the “description” of the synchro data source. 1.0.0
$synchro_user$ The user to run the synchro, specified by its login in the configuration file. The identifier of the User object is available via this placeholder. 12
$contact_to_notify$ The contact to notify, specified by its email address in the configuration file. The identifer of the contact is supplied via this placeholder. 48

On top of the 3 special configuration parameters listed above, all parameters defined in the section 'json_placeholders' of the configuration file are also available as placeholders inside the JSON file.

Sample configuration file:

params.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--  Local values for parameters. The values defined in this file have precedence over the ones defined in params.distrib.xml -->
<parameters>
  <itop_url>https://localhost/trunk</itop_url>
  <itop_login>admin</itop_login>
  <itop_password>admin</itop_password>
  <console_log_level>9</console_log_level>
  <contact_to_notify>denis.flaven@combodo.com</contact_to_notify>
  <synchro_user>admin</synchro_user>
  <json_placeholders type="hash">
    <test>Test 1</test>
  </json_placeholders>
</parameters>

Sample Synchro Data Source definition file, notice the use of the $version$, $synchro_user$, $contact_to_notify$ and $test$ placeholders:

MyCollector.json
{
	"name": "$test$",
	"description": "A test synchro data source - version $version$",
	"status": "implementation",
	"user_id": "$synchro_user$",
	"notify_contact_id": "$contact_to_notify$",
	"scope_class": "Server",
	"database_table_name": "",
	"scope_restriction": "",
	"full_load_periodicity": "0",
	"reconciliation_policy": "use_attributes",
	"action_on_zero": "create",
	"action_on_one": "update",
	"action_on_multiple": "error",
	"delete_policy": "ignore",
	"delete_policy_update": "",
	"delete_policy_retention": "0",
	"attribute_list": [
		{
			"attcode": "applicationsolution_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "applicationsolution_list"
		},
		{
			"attcode": "asset_number",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "asset_number"
		},
		{
			"attcode": "brand_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "brand_id"
		},
		{
			"attcode": "business_criticity",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "business_criticity"
		},
		{
			"attcode": "contacts_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "contacts_list"
		},
		{
			"attcode": "cpu",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "cpu"
		},
		{
			"attcode": "description",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "description"
		},
		{
			"attcode": "documents_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "documents_list"
		},
		{
			"attcode": "enclosure_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "enclosure_id"
		},
		{
			"attcode": "end_of_warranty",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "end_of_warranty"
		},
		{
			"attcode": "location_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "location_id"
		},
		{
			"attcode": "logicalvolumes_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "logicalvolumes_list"
		},
		{
			"attcode": "managementip",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "managementip"
		},
		{
			"attcode": "model_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "model_id"
		},
		{
			"attcode": "move2production",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "move2production"
		},
		{
			"attcode": "name",
			"update": "1",
			"reconcile": "1",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "name"
		},
		{
			"attcode": "nb_u",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "nb_u"
		},
		{
			"attcode": "networkdevice_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "networkdevice_list"
		},
		{
			"attcode": "org_id",
			"update": "1",
			"reconcile": "1",
			"update_policy": "master_locked",
			"reconciliation_attcode": "name",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "org_id"
		},
		{
			"attcode": "osfamily_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "osfamily_id"
		},
		{
			"attcode": "oslicence_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "oslicence_id"
		},
		{
			"attcode": "osversion_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "osversion_id"
		},
		{
			"attcode": "powerA_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "powerA_id"
		},
		{
			"attcode": "powerB_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "powerB_id"
		},
		{
			"attcode": "providercontracts_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "providercontracts_list"
		},
		{
			"attcode": "purchase_date",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "purchase_date"
		},
		{
			"attcode": "rack_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "rack_id"
		},
		{
			"attcode": "ram",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "ram"
		},
		{
			"attcode": "san_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "san_list"
		},
		{
			"attcode": "serialnumber",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "serialnumber"
		},
		{
			"attcode": "services_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "services_list"
		},
		{
			"attcode": "status",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "status"
		},
		{
			"attcode": "tickets_list",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "tickets_list"
		}
	],
	"user_delete_policy": "nobody",
	"url_icon": "",
	"url_application": "",
	"friendlyname": "Test 1",
	"user_id_friendlyname": "",
	"user_id_finalclass_recall": "",
	"notify_contact_id_friendlyname": " ",
	"notify_contact_id_finalclass_recall": ""
}

If you specify a non-empty value for database_table_name, this name MUST BEGIN WITH <prefix>synchro_data. Where <prefix> is the prefix used for all tables in iTop (configured using the db_subname parameter in the iTop configuration file).

Implementing your collector

Your collector must be a class derived from Collector. It must implement (at least) the Fetch() method. Fetch must return either, for each object to load, an array using the format attribute_code => value or false when the end of the set of objects has been reached.

The array returned by Fetch() must contain:

  • an entry primary_key that uniquely identifies the object being synchronized with iTop. The entry can contain whatever unique ID you can obtain from the inventory collection, or a unique identifier generated as a combination of the various fields of the object. It's up to the collector application to guarantee the unicity of this identifier (and its stability in time)
  • an entry for each attribute of the object to be loaded in iTop.

The sample code below generates a set of 10 servers, named 'Server 1', 'Server 2' … 'Server 10', and initialized 3 fields of the servers: their name, their organization (always 'Demo') and their description.

main.php
class MyCollector extends Collector
{
	protected $idx;
 
	public function Prepare()
	{
		$bResult = parent::Prepare();
		$this->idx = 0;
		return $bResult;
	}
 
	public function Fetch()
	{
		if ($this->idx < 10)
		{
			$this->idx++;
			return array('primary_key' => $this->idx, 'name' => 'Server '.$this->idx, 'org_id' => 'Demo', 'description' => 'Test Collector');
		}
		return false;
	}
}
 
// Register the collector, as the 1st to run
Orchestrator::AddCollector(1, 'MyCollector');

If the data returned by your collector are not encoded in the UTF-8 character set, overload the method GetCharset() of your collector to return the name of the character set (must return a value accepted by iconv on the iTop server)

Registering your collector

To register your collector, call the static method Orchestrator::AddCollector(). The two parameters are:

  • The order in which the collector should be run (when you need to run several collectors one after the other)
  • The name of the class (derived from Collector) in which the collector is implemented.

Default values for the parameters

A collector module can provide default values for its parameters by providing a file params.distrib.xml in the collectors folder. If such a file exists, its values are merged over the equivalent file in the conf directory.

SQL Collectors

The 'core' folder provides an abstract class SQLCollector which can serve as the basis for quickly creating collectors that retrieve their data via a SQL query.

To create such a collector you need to:

  1. Create a class derived from SQLCollector
  2. Create the json definition file for the data synchro source
  3. Add a configuration parameter (in params.distrib.xml) to define the SQL query to run
  4. Register your collector in collectors/main.php

The configuration parameters for the SQL Collectors are:

Parameter Meaning Default Value
sql_engine The PDO driver/engine to use for the database connection. mysql
sql_host The name or IP address of the database server to connect to. localhost
sql_database The name of the database to connect to. empty
sql_login The login to use when connecting to the database root
sql_password The password to use when connecting to the database n/a
sql_connection_string New in 1.0.8 The format of the PDO connection string. 3 placeholders are available inside the format string: %1$s = sql_engine, %2$s= sql_database and %3$s = sql_host %1$s:dbname=%2$s;host=%3$s
collector_class_query The query to run for the collector which PHP class is collector_class
collector_class_ignored_attributes New in 1.0.6 To take into account the possible variations of the data model, without re-writing a collector each time, it is possible to mark some of the collected attributes as “optional” so that the collector can run even if the corresponding attribute does not exist in the data model. Supply an array of attribute codes to ignore, here.

To specify a port number (or any other driver specific options), change the format of the sql_connection_string. For example: %1$s:dbname=%2$s;host=%3$s;port=3307

For versions prior to 1.0.8, to specify a port number (other than the default port), use the syntax host;port=xxxx for the sql_host parameter. Example: localhost;port=3307

Starting with version 1.0.10, the framework provides a new class of collector: MySQLCollector. This class is identical to SQLCollector except that it forces the retrieved data to be encoded in UTF-8 by issuing the SQL command SET NAMES 'utf8' at the beginning of the each connection to the database. To avoid any problem with the character set of the data, it is recommended to use this new class for all connections to a MySQL/MariaDB database.

Example of a simple SQL Collector

Let's create a very simple SQL collector which copies the “Notes” documents (class DocumentNote) from one iTop instance to another. Since the collector inherits all its behavior from the base class, the PHP code for the collector is simply:

DocumentNotesCollector.class.inc.php
<?php
class DocumentNoteCollector extends SQLCollector
{
}

The JSON definition file is

DocumentNotesCollector.json
{
	"name": "$prefix$DocumentNote",
	"description": "",
	"status": "production",
	"user_id": "$synchro_user$",
	"notify_contact_id": "$contact_to_notify$",
	"scope_class": "DocumentNote",
	"database_table_name": "",
	"scope_restriction": "",
	"full_load_periodicity": "604800",
	"reconciliation_policy": "use_attributes",
	"action_on_zero": "create",
	"action_on_one": "update",
	"action_on_multiple": "error",
	"delete_policy": "update",
	"delete_policy_update": "status:obsolete",
	"delete_policy_retention": "0",
	"attribute_list": [
		{
			"attcode": "cis_list",
			"update": "0",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "cis_list"
		},
		{
			"attcode": "contracts_list",
			"update": "0",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "contracts_list"
		},
		{
			"attcode": "description",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "description"
		},
		{
			"attcode": "documenttype_id",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "documenttype_id"
		},
		{
			"attcode": "name",
			"update": "1",
			"reconcile": "1",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "name"
		},
		{
			"attcode": "org_id",
			"update": "1",
			"reconcile": "1",
			"update_policy": "master_locked",
			"reconciliation_attcode": "",
			"finalclass": "SynchroAttExtKey",
			"friendlyname": "org_id"
		},
		{
			"attcode": "services_list",
			"update": "0",
			"reconcile": "0",
			"update_policy": "master_locked",
			"row_separator": "|",
			"attribute_separator": ";",
			"value_separator": ":",
			"attribute_qualifier": "'",
			"finalclass": "SynchroAttLinkSet",
			"friendlyname": "services_list"
		},
		{
			"attcode": "status",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "status"
		},
		{
			"attcode": "text",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "text"
		},
		{
			"attcode": "version",
			"update": "1",
			"reconcile": "0",
			"update_policy": "master_locked",
			"finalclass": "SynchroAttribute",
			"friendlyname": "version"
		}
	],
	"user_delete_policy": "nobody",
	"url_icon": "",
	"url_application": ""
}

Then in params.distrib.xml, add the foollowing entries:

  <sql_database>test</sql_database>
  <sql_login>root</sql_login>
  <sql_password>s3cret</sql_password>
  <documentnotecollector_query>SELECT id as primary_key, name, text, description, status, '2.0' as version, documenttype_id, 1 as org_id FROM view_DocumentNote</documentnotecollector_query>
  <documentnotecollector_ignored_attributes type="array">
    <attribute>location_id</attribute>
    <attribute>version_id</attribute>
  </documentnotecollector_ignored_attributes>

Finally, in collectors/main.php add the following lines:

main.php
<?php
require_once(APPROOT.'collectors/DocumentNoteCollector.class.inc.php');
Orchestrator::AddCollector(1 /* $iRank */, 'DocumentNoteCollector');

Advanced collectors

The collector framework provides means to perform some advanced processing for real-life collectors:

  • DataMapping for configurable data normalizations
  • LookupTable for advanced reconciliations based on multiple fields

Data Mapping

Raw data collected by inventory scripts sometimes require a normalization before being imported into iTop, in order to obtain homogenous data. The framework provides the helper class MappingTable for performing simple normalizations tasks.

A mapping table is configured (in the params.xxx.xml configuration file) as an ordered list of patterns, with a value associated to each pattern. The “clean” value returned by the mapping table is the value associated with the first pattern that matches the input value. Patterns are expressed as regular expressions. Values can use the placeholders to refer to some part of the matched pattern (%1$s is the whole pattern, %2$s the first group inside the regular expression, etc.).

Example of configuration (Brand normalization):

  <brand_mapping type="array">
    <!-- Syntax /pattern/replacement where:
      any delimiter can be used (not only /) but the delimiter cannot be present in the "replacement" string
      pattern is a RegExpr pattern
      replacement is a sprintf string in which:
          %1$s will be replaced by the whole matched text,
          %2$s will be replaced by the first matched group, if any group is defined in the RegExpr
          %3$s will be replaced by the second matched group, etc...
    -->
    <pattern>/IBM/IBM</pattern>
    <pattern>/Hewlett.Packard/Hewlett-Packard</pattern>
    <pattern>/Dell/Dell</pattern>
    <pattern>/.*/%1$s</pattern>
  </brand_mapping>

This example file performs the following normalization:

  1. Every string containing “IBM” is transformed into “IBM”,
  2. Every string containing “Hewlett”, followed by any character, followed by “Packard” is transformed into “Hewlett-Packard”,
  3. Every string containing “Dell” is transformed into “Dell”,
  4. All other strings are kept as is.

Using a mapping table in your code

  1. Create an instance of the MappingTable class, passing it the name of the XML tag in which to look for its configuration (inside the XML param file)
  2. Use the MapValue method to process each value as needed (the second parameter is the default value, when no match is found in the mapping table).

Usage example:

// Turns the raw brand string ('brand_id') into a normalized brand
// Use 'Other' for brands not found in the normalization table
 
class TestCollector extends SQLCollector
{
    protected $oBrandMapping;
 
    public function Prepare()
    {
        $bRet = parent::Prepare();
        // Create the MappingTable once at the initialization of your collector
        $this->oBrandMapping = new MappingTable('brand_mapping');
        return $bRet;
    }
 
    public function Fetch()
    {
        $aData = parent::Fetch();
        if ($aData !== false)
        {
            // Then process each collected brand
            $aData['brand_id'] = $this->oBrandMapping->MapValue($aData['brand_id'], 'Other');
        }
        return $aData;
    }
}

Advanced Lookups

The data synchronization mechanism embedded in iTop is not capable of performing reconciliations based on multiple fields (like searching for a Model based on both the Brand name and the Model name). The LookupTable class provides this reconciliation capability for any number of fields.

The class LookupTable builds a lookup table by retrieving the specified fields of a set of iTop objects, and storing the resulting identifier of the objects in iTop.

An instance of LookupTable is created by specifying an OQL query (the set of iTop objects to retrieve) and the fields of the objects that will be used for the mapping.

The list (and the order) of the fields specified during the creation of the LookupTable instance is the list of fields to be passed later on when performing a Lookup(…)

Once the LookupTable has been initialized, a call to the Lookup($aData, array(Field1, Field2, …), destField) method will replace in $aData the value of the column destField by identifier of the iTop object whose specified fields match the values passed in $aData as the columns Field1, Field2….

Since version 1.0.6, the Lookup method returns false if not corresponding lookup was found. In such a case the code can either supply a default value, of throw an exception IgnoredRowException to tell the collector to reject the whole line of collected data.

Example

In iTop, the operating system version is represented as a version depending on an OS family object. We can have the following objects in iTop:

  • Windows, versions 7.0 and 8.1,
  • Debian version 12.0.0.

This will be stored in iTop as shown below:

Object class id name
OSFamily 1 Windows
OSFamily 2 Linux Debian
Object class id osfamily_id name
OSVersion 1 1 7.0.0
OSVersion 2 1 8.1.0
OSVersion 3 2 12.0.0

Now let's imagine that our collector script gives us the two informations: 'Windows' and '8.1.0'. We can store the 'Windows' text string in the 'osfamily_id' field of the data synchro table and configure the synchro data source to perform the reconciliation based on the 'name' (this will properly replace 'Windows' by 1).

But to retrieve the identifier of the version 8.1.0 of Windows (which is 2 in our example) we need both the OS Family ('Windows') and the version number ('8.1.0'). The Synchronization Data Source is not capable of doing this composite lookup, this where the LookupTable comes into play.

$oOSVersionLookup = new LookupTable('SELECT OSVersion', array('osfamily_id_friendlyname', 'name'));

This will build - in memory - the following table:

lookup_key id
Windows_7.0.0 1
Windows_8.1.0 2
Debian_12.0.0 3

So if we have in $aData the following values:

osfamily_id osversion_id
Windows 8.1.0

Calling:

$oOSVersionLookup->Lookup($aData, array('osfamily_id', 'osversion_id'), 'osversion_id', 0);

Will place in the column 'osversion_id' the result of the lookup for the values $aData['osfamily_id'] and $aData['osversion_id'].

$aData will then contain the following values:

osfamily_id osversion_id
Windows 2

We then have to configure the Synchro Data Source so that it accepts the oversion_id as-is without performing any reconciliation on it.

The last parameter of Lookup(…) must contain the line number inside the CSV file being processed. This is used internally to perform some initializations only once when processing the first line of the file.

The advanced reconciliation works by retrieving (via the REST/JSON API), the objects to be matched against the composite key, after the data collection but before pushing the data to iTop. Therefore, in order to use this advanced lookup mechanism, you must tell the framework that the collector has to reprocess the collected data before the actual synchro. This is achieved by overloading the method MustProcessBeforeSynchro of the collector; and returning true.

The collector framework provides two additional methods which can be overloaded:

  1. InitProcessBeforeSynchro is called after the data collection, but before starting to reprocess each line of the collected data. This is the plece where to create the LookupTable instance
  2. ProcessLineBeforeSynchro is called for each line of the collected data (including the header line of the CSV file, which index is zero)

Usage Example

The following code fragment shows to use cases of lookup tables altogether: one for brand + model and one for OS family + OS version.

	protected function MustProcessBeforeSynchro()
	{
		// We must reprocess the CSV data obtained from the inventory script
		// to lookup the Brand/Model and OSFamily/OSVersion in iTop
		return true;
	}
 
	protected function InitProcessBeforeSynchro()
	{
		// Retrieve the identifiers of the OSVersion since we must do a lookup based on two fields: Family + Version
		// which is not supported by the iTop Data Synchro... so let's do the job of an ETL
		$this->oOSVersionLookup = new LookupTable('SELECT OSVersion', array('osfamily_id_friendlyname', 'name'));		
 
		// Retrieve the identifiers of the Model since we must do a lookup based on two fields: Brand + Model
		// which is not supported by the iTop Data Synchro... so let's do the job of an ETL
		$this->oModelLookup = new LookupTable('SELECT Model', array('brand_id_friendlyname', 'name'));		
	}
 
	protected function ProcessLineBeforeSynchro(&$aLineData, $iLineIndex)
	{
		// Process each line of the CSV
		if (!$this->oOSVersionLookup->Lookup($aLineData, array('osfamily_id', 'osversion_id'), 'osversion_id', $iLineIndex))
		{
		    throw New IgnoreRowException('Unknown OS Version');
		}
		if (!$this->oModelLookup->Lookup($aLineData, array('brand_id', 'model_id'), 'model_id', $iLineIndex))
		{
		    throw New IgnoreRowException('Unknown Model');
		}
	}

Data Model Variants

It may happen that the target Data Model has some variants (depending on the set of modules chosen during the installation). If a given attribute can be missing in some configurations, you can tell your collector to accept this variation, by overloading the method AttributeIsOptional. (This is simpler than writing a specific collector for each combination).

If an attribute specified in the JSON definition of the Synchro Data Source is missing, the processing will stop with an error, unless this attribute is declared as optional. In the later case, the name of the skipped attribute is recorded in the protected member variable $this->aSkippedAttributes and the processing continues. The code of the collector can later check the content of the array $this->aSkippedAttributes to determine which fields have to be collected or not.

Example of implementation of AttributeIsOptional as a method of the VirtualMachineCollector class:

public function AttributeIsOptional($sAttCode)
{
	// If the module Service Management for Service Providers is selected during the setup
	// there is no "services_list" attribute on VirtualMachines. Let's safely ignore it.
	if ($sAttCode == 'services_list') return true;
 
	return parent::AttributeIsOptional($sAttCode);
}

Troubleshooting

When troubleshooting the reconciliation mechanism it is useful to compare the original (raw) values as reported by the inventory script with the result of the reconciliation process. Whenever the method MustProcessBeforeSynchro of a collector returns true, the framework generates two files inthe data subdirectory. You can easily compare the values before/after the lookup by comparing the two CSV files:

  1. <collector_name>.raw-<index>.csv: the original data, as produced by the inventory script,
  2. <collector_name>-<index>.csv: the reprocessed data, to be uploaded to iTop.
extensions/itop-data-collector-base.txt · Last modified: 2017/11/16 16:07 (external edit)

";