Script Example: Aggregation of DCI values and applying the 95% percentile average

Revision as of 15:52, 5 October 2020 by Phil (talk | contribs)

WARNING: This page is no longer updated. Please visit NetXMS Scripting Language for current version of the documentation.


The example is based around a template which configures ICMP Packet Loss probes. This script will loop around the nodes, collect the required DCI values. The values are then ordered and the top 5 percent discarded, the remaining entries are averaged to give the required value;

sub main()
{

trace(1, "Global Ping Loss 95");
array pValue;
arrayI  = 0;

foreach(parent : GetNodeParents($node))
{
	trace(3, "Parent object: name='" . parent->name ."' id=" . parent->id);
	if (parent->name == "all voice")
	{
		foreach(vNode : GetObjectChildren(parent))
		{
			dciName = "ICMP: Packet loss to ".vNode->name;
			dciId = FindDCIByDescription(vNode, dciName);
			if (dciId > 0)
			{
				tmpValue = GetDCIValue(vNode,dciId);				
				if (tmpValue != null)
				{
					pValue[arrayI++] = tmpValue;
				}
			}
		}
	}
}

// Sort the Array
bubbleSort(pValue);

// Apply the 95 percent rule
upTo = arrayI * 0.95;
pLoss = 0;
pCount = 0;
for(ia = 0; ia < upTo; ia++)
{
	pLoss += pValue[ia];
	pCount = ia;
}
p95AvgLoss = pLoss / pCount;

trace(1, "Global Ping Loss 95 Summary: arrayI=".arrayI." upTo=".upTo." p95AvgLoss=".p95AvgLoss );

return p95AvgLoss;
}

sub bubbleSort(arr)
{
	swapped = true;
	while (swapped == true){
		swapped = false;
		for(ia = 1; arr[ia] != null; ia++)
		{
			ib = ia - 1;
			
			if (arr[ib] > arr[ia]){
				trace(3,"swap: ".ib.":".arr[ib]." with ".ia.":".arr[ia]);
				swapped=true;
				t = arr[ib];
				arr[ib] = arr[ia];
				arr[ia] = t;
				swapped = true;
			}
		}
	}
}

sub printArray(arr)
{
	for(ia = 0; arr[ia] != null; ia++)
	{
		trace(1,"printArray: ".ia.":".arr[ia]);
	}
}