What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Issue adding PowerShell custom sensor values

Votes:

0

Hi,

We seem to be having an adding a custom powershell sensor to PRTG. The sensor accept literals within values on the sensor, however when I use variables within the script it returns 0 instead of a value.

Please see the script and sample output below

Script

$Query = "
SET NOCOUNT ON;
IF OBJECT_ID('TempDB..#tokens','U') IS NOT NULL DROP TABLE #tokens;
IF OBJECT_ID('TempDB..#TokenOutput','U') IS NOT NULL DROP TABLE #TokenOutput;
DECLARE 
       @publication sysname
       ,@tokenID AS INT
       ,@StartTime DATETIME = GETDATE();
SET @publication = N'TRN-WLRCRM'; 

EXEC sys.sp_posttracertoken 
  @publication = @publication,
  @tracer_token_id = @tokenID OUTPUT;



WAITFOR DELAY '00:00:2';


CREATE TABLE #tokens (tracer_id int, publisher_commit datetime);

INSERT #tokens (tracer_id, publisher_commit)
EXEC sys.sp_helptracertokens @publication = @publication;
SET @tokenID = (SELECT TOP 1 tracer_id FROM #tokens
ORDER BY publisher_commit DESC);


CREATE TABLE #TokenOutput (ID INT IDENTITY(1,1),  [distributor_latency] int, [subscriber] nvarchar(128), [subscriber_db] nvarchar(128), [subscriber_latency] int, [overall_latency] int  );

WHILE 1 = 1
BEGIN
       WAITFOR DELAY '00:00:01';  
       INSERT INTO #TokenOutput
       EXEC sys.sp_helptracertokenhistory 
         @publication = @publication, 
         @tracer_id = @tokenID;
       IF EXISTS (SELECT TOP 1 1 FROM #TokenOutput WHERE  overall_latency IS NULL)
              BEGIN
                     IF DATEDIFF(SECOND,@StartTime,GETDATE()) > 60
                           BEGIN
                                  UPDATE #TokenOutput SET overall_latency = 99 WHERE overall_latency IS NULL;
                           END
                     ELSE
                           BEGIN
                                  DELETE FROM #TokenOutput WHERE overall_latency IS NULL;
                           END
              END
       IF (SELECT COUNT(DISTINCT subscriber) FROM #TokenOutput WHERE  overall_latency IS NOT NULL) = 2
              BEGIN
                     BREAK;
              END
END    

SELECT DISTINCT
       CASE subscriber WHEN 'REP-01' THEN 1 WHEN 'Server2' THEN 2 END Id
       ,subscriber
       ,overall_latency
FROM 
       #TokenOutput;

"
$Output = Invoke-Sqlcmd -ServerInstance TargetServer -Database WLRCRM -Query $Query -QueryTimeout 65


$Rep01 = $Output |  Where-Object {$_.subscriber -eq "Server1"}|SELECT-Object overall_latency 
$DBS01 = $Output |  Where-Object {$_.subscriber -eq "Server2"}|SELECT-Object overall_latency
$Server1.Value = $Server1.overall_latency
$Server2Value = $Server2overall_latency



Powershell script


Write-Host "<prtg>"
    "<result>"
        "<channel>Server2</channel>"
        "<value>$Server2Value</value>"
     "</result>"
     "<result>"
        "<channel>Server1</channel>"
        "<value>$Server1Value</value>"
     "</result>"
"</prtg>" 


Output
<prtg>
<result>
<channel>Server1</channel>
<value>9</value>
</result>
<result>
<channel>Server2</channel>
<value>7</value>
</result>
</prtg>

custom-sensor powershell replication

Created on Oct 21, 2020 3:00:45 PM

Last change on Oct 22, 2020 5:09:04 AM by  Marijan Horsky [Paessler Support]



2 Replies

Votes:

0

Hi,

Please note that we cannot offer support for custom scripts. Maybe our community can help you to find the issue.

Created on Oct 22, 2020 11:03:41 AM by  Marijan Horsky [Paessler Support]



Votes:

0

Hi Krishn, for me looks like a typo in your script and your querry.

In your querry you define $Server1.Value = $Server1.overall_latency $Server2Value = $Server2overall_latency

and in your Powershell Script you declare $Server2Value and $Server1Value without dot.

Does this solve your problem?

Created on Oct 22, 2020 11:23:07 AM




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.