PowerShell

Sample custom monitor in PowerShell.

Leave Feedback

Introduction

This document explains the configuration of PowerShell script to define a Custom Monitor.

PowerShell script configuration

Custom monitor PowerShell scripts are provided with below input parameters from Agent while invoking the script.

Cscript custom_script.ps1 
    "
    /metricName::metricName1|metricName2 
    /metric::metric1|metric2 
    /warn::warn1|warn2 
    /critical::crit1|crit2 
    /alert::do_alert1|do_alert2 
    /params::"args_string1|args_string2"
    "

Parsing parameters

Use the following block of code in all the PowerShell custom monitor scripts to parse the parameters.

 if($args.count -gt 0)
	{
	for($i=0 ;$i -le $args.count-1 ;$i++)
	{
		$strArgs = $strArgs + [string]($args[$i])
  	}
	}
	if($strArgs.Contains("/metricName::") -ne 0)
	{
		$MetricNameTokens1 = $strArgs -split "/metricName::", 2
                       $MetricNameTokens= $MetricNameTokens1[1].Trim() 
		if($MetricNameTokens.Contains("/") -ne 0)
		 {
			$MetricNameTokens1 = $MetricNameTokens -split "/", 2
                  		$MetricNameTokens = $MetricNameTokens1[0].Trim()
                  			#write-host "metricName " + $MetricNameTokens
	      		 }
	}
		if($strArgs.Contains("/metric::") -ne 0)
	{
                  $MetricTokens1 = $strArgs -split "/metric::", 2
                  	    $MetricTokens = $MetricTokens1[1].Trim()
	    if($MetricTokens.Contains("/") -ne 0)
	    {
	         $MetricTokens1 = $MetricTokens -split "/", 2
		    $MetricTokens = $MetricTokens1[0].Trim()
		    #write-host "Metric is " + $MetricTokens
	   }
         }

                if($strArgs.Contains("/warn::") -ne 0)
	 {
		   $Warning_ThresTokens1 = $strArgs -split "warn::", 2
		   $Warning_ThresTokens = $Warning_ThresTokens1[1].Trim()
        		   if($Warning_ThresTokens.Contains("/") -ne 0)
		   {
			   $Warning_ThresTokens1 = $Warning_ThresTokens  -split "/", 2
			   $Warning_ThresTokens =  $Warning_ThresTokens1[0].Trim()
			     #write-host "warn is " + $Warning_ThresTokens
		   }
          }
                 if($strArgs.Contains("/critical::") -ne 0)
	  {
		  $Critical_ThresTokens1 = $strArgs -split "/critical::", 2
		  $Critical_ThresTokens = $Critical_ThresTokens1[1].Trim()
		  if($Critical_ThresTokens.Contains("/") -ne 0)
		  {
	  	$Critical_ThresTokens1 = $Critical_ThresTokens -split "/", 2
		$Critical_ThresTokens = $Critical_ThresTokens1[0].Trim()
	#write-host "critical is " + $Critical_ThresTokens
		  }
	  }
                if($strArgs.Contains("/alert::") -ne 0)
	  {
		   $Alert_FlagTokens1 =  $strArgs -split "/alert::", 2
		   $Alert_FlagTokens = $Alert_FlagTokens1[1].Trim()
		   if($Alert_FlagTokens.Contains("/") -ne 0)
		   {
		    $Alert_FlagTokens1= $Alert_FlagTokens -split "/", 2
		    $Alert_FlagTokens=  $Alert_FlagTokens1[0].Trim()
		            #write-host "Alert_FlagTokens " + $Alert_FlagTokens
		   }
	    }
            if($strArgs.Contains("/params::") -ne 0)
	    {
		$ParamsTokens1 = $strArgs -split "/params::", 2
		$ParamsTokens = $ParamsTokens1[1].Trim()
		if($ParamsTokens.Contains("/") -ne 0)
    {
		$ParamsTokens1 = $ParamsTokens -split "/", 2
			$ParamsTokens = $ParamsTokens1[0].Trim()
			#write-host "params are " + $ParamsTokens
		}
     }

Sending performance data

The write-host parameter is used to send performance data.

For example: write-host "The Alert Flag tokens are " + $Alert_FlagTokens

Sending alerts

Use the following code to send alert.

 Function SendAlertToAB					
 {
    	param([string]$MetricInstance,[string]$Instance,[string]$OldState,[string]$NewState,[int]$Value)
	$currTime = [System.DateTime]::Now
		$timeStamp = [string]$currTime.Year + "-" + [String]$currTime.Month + "-" + [String]$currTime.Day + " " +
[string]$currTime.Hour + ":" + [string]$currTime.Minute + ":" + [string]$currTime.Second
	$SocketXML = ""
	$SocketXML = $SocketXML + "AlertOutput"
	$SocketXML = $SocketXML + ""+ $MetricInstance+""
	$SocketXML = $SocketXML + ""+ $NewState+""
	$SocketXML = $SocketXML + ""+ $OldState+""
	$SocketXML = $SocketXML + "Value of metric " + $MetricInstance + ":" + $Instance + " is " + $Value +""
	$SocketXML = $SocketXML + "" + $timeStamp + ""
	$SocketXML = $SocketXML + "Monitoring"
	$SocketXML = $SocketXML + ""+ $MetricInstance+ "_" +$Instance+""
	$SocketXML = $SocketXML + "Value of metric " + $MetricInstance + ":" + $Instance + " is "+ $Value +"  "
	$SocketXML = $SocketXML + ""

# Invoke exe which sends data through socket if($fileAccessPath.contains("x86"))
        {
		& "C:\Program Files (x86)\OpsRamp\Agent\bin\AgentSockIPC.exe" $SocketXml
	}
	else
	{
		& "C:\Program Files\OpsRamp\Agent\bin\AgentSockIPC.exe" $SocketXml
	}
     }