Applications Systems

Python-based Windows Script Monitor does not return data (SAM)

This article describes why a Python-based Windows Script Monitor in SolarWinds SAM cannot return any data.

First published date

11/15/2018 1:48 AM

Last published date

3/31/2022 12:18 PM

Overview

You can use Windows scripting languages to gather metrics and other information that you wish to monitor in SAM. For details, see Configure a Windows Script Monitor and also Using a Python Script as a SAM Component in the SolarWinds online IT community, THWACK.   2003-2018 SolarWinds Worldwide, LLC. All rights reserved , available at https://support.solarwinds.com/  obtained on November 29,2018)

This article describes how a Python-based Windows Script Monitor cannot return data when characters are truncated due to a full buffer that is not flushed.

Here is an example of what may appear in log files when this issue occurs:

Target: 127.0.0.1 
Engine: python 
Script: ==================================================== 
print ("Statistic: 100") 
print ("Message: Things are working as expected") 
Output: ====================================================
Errors: ====================================================
Result: ====================================================
Component Evidence 
Type: DynamicEvidence 
Component Type: WindowsScript 
Actual Outcome: Undefined
 
  • The default location for SAM logs is c:\ProgramData\SolarWinds\Logs\APM.
  • SAM 6.6.1 and later
  • Python 3.x
  • Python extensions for Windows (pywin32) versions that match the Python version

Product section

Server Application Monitor

Cause

SAM's Windows Script Monitor use CScript, a front-end application for the Windows Script Host, but does not get data back from the CScript process. If you run CScript manually from the command line (for example, cscript //E:python test.py), output appears. But if you redirect standard output to a file (cscript //E:python test.py > result.txt), no data appears unless you print enough (about 8 KB) characters from the Python script. Any data that exceeds the buffer size will be truncated when the buffer is full again. This occurs because Pywin32 replaces sys.stdout with its own structure, which does not support flushing.

Resolution

Use the script structure shown in the following example:  

# Restore stdout
import sys 
sys.stdout = sys.__stdout__ 

# Your script here
print("Statistic: 100") 
print("Message: Test") 

# Flush stdout
sys.stdout.flush();