Data Protector Device Discovery Provider Probe Action

HP.DataProtector.PA.Device.DiscoveryProvider (ProbeActionModuleType)

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
PA ProbeAction Microsoft.Windows.ScriptDiscoveryProbe Default

Source Code:

<ProbeActionModuleType ID="HP.DataProtector.PA.Device.DiscoveryProvider" Accessibility="Public" Batching="false" PassThrough="false">
<Configuration>
<xsd:element minOccurs="1" name="MPElement" type="xsd:string"/>
<xsd:element minOccurs="1" name="TargetID" type="xsd:string"/>
<xsd:element minOccurs="1" name="ComputerName" type="xsd:string"/>
</Configuration>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="PA" TypeID="Windows!Microsoft.Windows.ScriptDiscoveryProbe">
<ScriptName>MPDPDeviceDiscoveryProvider.js</ScriptName>
<Arguments>$Config/MPElement$ $Config/TargetID$ $Config/ComputerName$ </Arguments>
<ScriptBody><Script>/* ============================================================================
|
| Copyright (c) 2013 ComTrade d.o.o. All rights reserved.
|
| Possession of this software does not grant any rights to use, reproduce,
| modify or distribute it or to use any concept it may contain.
|
| Licensed under ComTrade d.o.o. license ("the License"); you may not use
| this software unless in compliance with the License. Any use of the software
| without such license is a violation of copyright laws and may be subject to
| legal actions (remedies and/or criminal prosecution).
|
| NOTE:
| If you receive this content in error, please let us know by contacting
| ComTrade d.o.o. legal department ([email protected]) and destroy any
| copy you may have.
|
+=============================================================================*/

/* ======================================================================

NAME: MPDPDeviceDiscoveryProvider.js

AUTHOR: AMS Team , Comtrade d.o.o.
DATE : 10/10/2013

COMMENT:

========================================================================= */

Main();

WScript.Quit(0);

function MPDPInstalledOnNode() {}

function Main() {
var oArgs = WScript.Arguments;
var oAPI, oDiscoveryData, oInst;
try {
oAPI = WScript.CreateObject("MOM.ScriptAPI");
} catch (e) {
WScript.Echo(e.message);
WScript.Quit(-1);
}

if (oArgs.Count() &lt; 3) {
oAPI.LogScriptEvent("MPDPDeviceDiscoveryProvider.js", 101, 1, "Too few script arguments. Quit(-1).");
WScript.Quit(-1);
}

var SourceID, ManagedEntityId;
var TargetComputer = "";

SourceID = oArgs(0);
ManagedEntityId = oArgs(1);
TargetComputer = oArgs(2);

// Debug info.
oAPI.LogScriptEvent("MPDPDeviceDiscoveryProvider.js", 200, 0, "Running discovery on target " + TargetComputer + " - managed entity: " + ManagedEntityId);

try {
oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceID, ManagedEntityId);
} catch (e) {
WScript.Echo(e.message);
oAPI.LogScriptEvent("MPDPDeviceDiscoveryProvider.js", 101, 1, "oAPI.CreateDiscoveryData(): " + e.message);
WScript.Quit(-1);
}

var utility = new MPDPUtility();

//check if MPDP agent is installed on node!
var MPDPInstalled = false;
agtPath = utility.readFromRegistry(utility.agentPathRegistryKey, "");
if (agtPath.length &gt; 0) {
MPDPInstalled = true;
}

var xmlDoc;
try {
xmlDoc = utility.getXmlDataDoc();
} catch (e) {
//WScript.Echo(e.message);
//log this only if agent is already installed!
if (MPDPInstalled == true) {
oAPI.LogScriptEvent("MPDPDeviceDiscoveryProvider.js", 101, 1, "utility.getXmlDataDoc(): " + e.message);
}
oAPI.Return(oDiscoveryData);
WScript.Quit(0);
}

var cell_node, cellserver_node;

var bLicensed;
bLicensed = false;

var computerName = TargetComputer.toLowerCase();

// there will be always only one cell in xml document
cell_node = xmlDoc.getElementsByTagName("cell")[0];
if (typeof(cell_node) != "undefined" &amp;&amp; cell_node != null) {
cellserver_node = cell_node.getElementsByTagName("cellserver")[0];

if (typeof(cellserver_node) != "undefined" &amp;&amp; cellserver_node != null) {
var v_cellID = cell_node.getAttribute("id");
var devices_node, dc_nodes, dc_node;

devices_node = cell_node.getElementsByTagName("devices")[0];
if (typeof(devices_node) != "undefined" &amp;&amp; devices_node != null) {
dc_nodes = devices_node.childNodes;

var oDeviceGroupObj;
oDeviceGroupObj = oDiscoveryData.CreateClassInstance("$MPElement[Name='HP.DataProtector.Cell.DeviceGroup']$");
oDeviceGroupObj.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", "Devices");
oDeviceGroupObj.AddProperty("$MPElement[Name='HP.DataProtector.Cell.DeviceGroup']/CellId$", v_cellID); //key
oDeviceGroupObj.AddProperty("$MPElement[Name='HP.DataProtector.Cell']/Id$", v_cellID); //host key

while (dc_node = dc_nodes.nextNode()) //each of the devices
{
if (dc_node.nodeName == "device") {

var v_deviceName = (dc_node.getAttribute("name") != null) ? dc_node.getAttribute("name") : "";
var v_deviceDescription = (dc_node.getAttribute("description") != null) ? dc_node.getAttribute("description") : "";
var v_deviceHost = (dc_node.getAttribute("host") != null) ? dc_node.getAttribute("host") : "";
var v_devicePolicy = (dc_node.getAttribute("policy") != null) ? dc_node.getAttribute("policy") : "";
var v_deviceType = (dc_node.getAttribute("type") != null) ? dc_node.getAttribute("type") : "";
var v_devicePool = (dc_node.getAttribute("pool") != null) ? dc_node.getAttribute("pool") : "";
var v_deviceLibrary = (dc_node.getAttribute("library") != null) ? dc_node.getAttribute("library") : "";
var v_deviceRestorePool = (dc_node.getAttribute("restoredevicepool") != null) ? dc_node.getAttribute("restoredevicepool") : "";
var v_deviceCopyPool = (dc_node.getAttribute("copydevicepool") != null) ? dc_node.getAttribute("copydevicepool") : "";
var v_deviceSerial = (dc_node.getAttribute("devserial") != null) ? dc_node.getAttribute("devserial") : "";

var oDeviceObj;
oDeviceObj = oDiscoveryData.CreateClassInstance("$MPElement[Name='HP.DataProtector.Device']$");
oDeviceObj.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", v_deviceName);

oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/Name$", v_deviceName); // key
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/Description$", v_deviceDescription);
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/Host$", v_deviceHost);
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/Policy$", v_devicePolicy);
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/Type$", v_deviceType);
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/Pool$", v_devicePool);
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/Library$", v_deviceLibrary);
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/RestoreDevicePool$", v_deviceRestorePool);
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/CopyDevicePool$", v_deviceCopyPool);
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Device']/DevSerial$", v_deviceSerial);

oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Cell.DeviceGroup']/CellId$", v_cellID); //key
oDeviceObj.AddProperty("$MPElement[Name='HP.DataProtector.Cell']/Id$", v_cellID); //host key
oDiscoveryData.AddInstance(oDeviceObj);

var oClientsFolder2ClientRel;
oClientsFolder2ClientRel = oDiscoveryData.CreateRelationshipInstance("$MPElement[Name='HP.DataProtector.DeviceGroupHostsDevice']$");
oClientsFolder2ClientRel.Source = oDeviceGroupObj;
oClientsFolder2ClientRel.Target = oDeviceObj;
oDiscoveryData.AddInstance(oClientsFolder2ClientRel);
}
}
}
}
}
oAPI.Return(oDiscoveryData);
}

function getShortName(MPDPServerDnsName) {
var shortName = MPDPServerDnsName;
if (MPDPServerDnsName != null) {
if (MPDPServerDnsName.length &gt; 0 &amp;&amp; MPDPServerDnsName.indexOf('.') &gt; 0) {
//get string
shortName = MPDPServerDnsName.substring(0, MPDPServerDnsName.indexOf('.'));
}
}
return shortName;
}


function MPDPUtility() {
this.agentPathRegistryKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Hewlett-Packard\\DataProtector MP Agent\\DataDir";
this.dataFileLock = "mpdp-data.lock";
this.dataFile = "mpdp-data.xml";
this.agentPath = "";

this.getXmlDataDoc = function () {
var xmlDoc = WScript.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false;

datFilePath = this.getDataFilePath();
agtPath = this.getAgentPath();

// lock data file and load the content
var fs = WScript.CreateObject("Scripting.FileSystemObject");

var fileLock,
lockFileName;
var repeatCount = 100; // the number of tries for retreiving the lock
lockFileName = agtPath + "\\" + this.dataFileLock;
do {
try {
fileLock = fs.OpenTextFile(lockFileName, 2, true);
break;
} catch (e) {
repeatCount--;
WScript.Sleep(100);
}
} while (repeatCount &gt; 0);

if (repeatCount == 0) {
throw new Error("Could not acquire lock for reading MPDP MP data file '" + lockFileName + "'.");
}

if (!xmlDoc.load(datFilePath)) {
// before throwing exception close the lock
fileLock.Close();
throw new Error("Error loading MPDP MP data file '" + datFilePath + "'. Error code: " + xmlDoc.parseError.reason);
}

// afther we read the data file contnent we can unlock it
fileLock.Close();

// return the xml doc object
return xmlDoc;
}

this.getAgentPath = function () {
if (this.agentPath.length == 0) {
agtPath = this.readFromRegistry(this.agentPathRegistryKey, "");
if (agtPath.length &lt;= 0) {
throw new Error("Cannot resolve MPDP Agent path. No registry key '" + this.agentPathRegistryKey + "'.");
}
this.agentPath = agtPath;
}

return this.agentPath;
}

this.getDataFilePath = function () {
agtPath = this.getAgentPath();
return agtPath + "\\" + this.dataFile;
}

this.readFromRegistry = function (strRegistryKey, strDefault) {
try {
var value;
var WSHShell = WScript.CreateObject("WScript.Shell");
value = WSHShell.RegRead(strRegistryKey);
valueReadFromRegistry = value;
WSHShell = null;
} catch (err) {
valueReadFromRegistry = strDefault;
}
return valueReadFromRegistry;
}

//parses ISO 8601 formated timestamps ("yyyy'-'MM'-'dd'T'HH':'mm':'ss")
parseDate = function (date) {
var timestamp,
dtParams;
// 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec
if ((dtParams = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?)?$/.exec(date))) {
var dtParamsIdx = [1, 4, 5, 6, 7];
// avoid NaN timestamps
for (var i = 0, k; (k = dtParamsIdx[i]); ++i) {
dtParams[k] = +dtParams[k] || 0;
}
// allow undefined days and months
dtParams[2] = (+dtParams[2] || 1) - 1;
dtParams[3] = +dtParams[3] || 1;
timestamp = new Date(dtParams[1], dtParams[2], dtParams[3], dtParams[4], dtParams[5], dtParams[6], dtParams[7]);
} else
timestamp = new Date(date);

return timestamp;
}
}</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="PA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
<InputType>System!System.BaseData</InputType>
</ProbeActionModuleType>