Powershell script doesn't work over Cacti

Post support questions that relate to the Windows 2003/2000/XP operating systems.

Moderators: Developers, Moderators

Post Reply
adhara
Posts: 11
Joined: Thu Aug 29, 2013 7:21 am

Powershell script doesn't work over Cacti

Post by adhara »

Hi,

Thanks in advance for your help. Here below the problem I'm facing [all tests done with the same user as the one used by Cacti].

When executing a powershell script over Cacti (cmd.php), it not works at all. So if I execute it over a DOS command line or a PS command line, the output is well displayed.

Data input method:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Unrestricted -file <path_cacti>/scripts/ServerStatusApache.ps1 -target <hostname>

ServerStatusApache.ps1 script contains:

param(
[string]$target
)

Function cmdline {
C:/Windows/System32/curl.exe --connect-timeout 5 --max-time 5 -u user:pwd -s http://$target/server-status?auto? | gawk -f C:/inetpub/wwwroot/cacti/scripts/serverstatus.awk
}
cmdline

## Already tried without function + write-host at end. Like :

$cmdline = C:/Windows/System32/curl.exe --connect-timeout 5 --max-time 5 -u user:pwd -s http://$target/server-status?auto? | gawk -f C:/inetpub/wwwroot/cacti/scripts/serverstatus.awk

write-host $cmdline

Same result over Cacti (cmd.php).

*-*-*-*-*-*-*
When executing my ps1 script on a DOS or PS command line, I get what I expect to have.

Output looks like :
Hit:0 Oct:0 BusyWorkers:0 IdleWorkers:0 KeepAlive:0 Closing:0 Reading:0 Sending:0 StartingUp:0 DNSLookup:0 Logging:0 GracefullyFinishing:0 IdleCleanup:0 Waiting:0 free:0

Over Cacti (cmd.php) :

08/29/2013 09:17:58 AM - CMDPHP: Poller[0] Host[23] DS[1283] CMD: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Unrestricted -file C:/inetpub/wwwroot/cacti/scripts/ServerStatusApache.ps1 -target XXXX, output: The term 'C:/Windows/System32

I tried to modify my PS script:

Not to specify absolute path for used binaries (eg: curl instead of C:/Windows/System32/curl.exe ).
--> Ok over DOS or PS command line / NOK over Cacti.
Replace \ by / in my PS script.
--> Ok over DOS or PS command line / NOK over Cacti.
Put directly "C:/Windows/System32/curl.exe --connect-timeout 5 --max-time 5 -u user:pwd -s http://$target/server-status?auto? | gawk -f C:/inetpub/wwwroot/cacti/scripts/serverstatus.awk" as Input String (Data Input Methods)
--> NOK over Cacti.

I would appreciate your help.

Regards.
jftuga
Posts: 49
Joined: Mon Feb 07, 2011 11:50 am
Location: Athens, GA

Re: Powershell script doesn't work over Cacti

Post by jftuga »

Try creating a batch file and then just call this from cacti. In the first part of the batch file you could have something like this to log the fact the the batch file is actually being ran.

Code: Select all

@echo off
set LOG=c:\temp\runtime.log

for /f "usebackq tokens=1,2,3,4,5,6,7 delims=/:. " %%a in (`echo %DATE% %TIME%`) do set NOW=%%d%%b%%c_%%e%%f%%g
@echo script run at: %NOW% >> %LOG%
User avatar
BSOD2600
Cacti Moderator
Posts: 12171
Joined: Sat May 08, 2004 12:44 pm
Location: USA

Re: Powershell script doesn't work over Cacti

Post by BSOD2600 »

I ran into similar problems when developing some PowerShell scripts. Using the 'Data Input Methods' here are two example of a Input method which works:

Code: Select all

Powershell.exe -command "& '<path_cacti>/scripts/CE_RxCount.ps1'"

Powershell.exe -command "& '<path_cacti>/scripts/CE_Performance.ps1' " <operation>
For returning data, I always used a StringBuilder and then handed it back to Cacti with:

Code: Select all

    $sb = new-object System.Text.StringBuilder
<collect data>
    write-host $sb.ToString().Trim()    
adhara
Posts: 11
Joined: Thu Aug 29, 2013 7:21 am

Re: Powershell script doesn't work over Cacti

Post by adhara »

BSOD2600 wrote:I ran into similar problems when developing some PowerShell scripts. Using the 'Data Input Methods' here are two example of a Input method which works:

Code: Select all

Powershell.exe -command "& '<path_cacti>/scripts/CE_RxCount.ps1'"

Powershell.exe -command "& '<path_cacti>/scripts/CE_Performance.ps1' " <operation>
For returning data, I always used a StringBuilder and then handed it back to Cacti with:

Code: Select all

    $sb = new-object System.Text.StringBuilder
<collect data>
    write-host $sb.ToString().Trim()    
Hi,

Thanks for your answer.
I tried this:

Data Input Methods >
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Unrestricted -command "& '<path_cacti>/scripts/ServerStatusApache.ps1' -target <hostname>"
My PS script looks like :

Code: Select all

param(
[string]$target
)

$sb = new-object System.Text.StringBuilder
curl --connect-timeout 5 --max-time 5 -u user:pwd-s http://$target/server-status?auto? | gawk -f C:/inetpub/wwwroot/cacti/scripts/serverstatus.awk

write-host $sb.ToString().Trim()  
But I still get issues when running over Cacti (cmd.php).

08/30/2013 04:21:58 PM - CMDPHP: Poller[0] Host[23] DS[1283] WARNING: Result from CMD not valid. Partial Result: The term 'curl' is n

It's like Cacti is not able to resolve system path to point out variables or to find my binaries.

The same command ran over DOS works well:

Code: Select all

C:\inetpub\wwwroot\cacti\scripts>Powershell.exe -noprofile -executionpolicy Unrestricted - command "& '.\ServerStatusApache.ps1' -target FQDNxxx"
Hit:2121624 Oct:37436706816 BusyWorkers:1 IdleWorkers:599 KeepAlive:0 Closing:0 Reading:0 Sending:1 StartingUp:0 DNSLook up:0 Logging:0 GracefullyFinishing:0 IdleCleanup:0 Waiting:599 free:0


*-*-*-*-*
UPDATE
*-*-*-*-*

Got this with degeug mode:

PM - CMDPHP: Poller[0] Host[23] DS[1283] WARNING: Result from CMD not valid. Partial Result: File C:\inetpub\wwwr
08/30/2013 05:41:58 PM - CMDPHP: Poller[0] Host[23] DS[1283] CMD: Powershell.exe -command "& 'C:/inetpub/wwwroot/cacti/scripts/ServerStatusApache.ps1' -target xxxxx", output: File C:\inetpub\wwwroot\cacti\scripts\ServerStatusApache.ps1

it s not help me much...
Last edited by adhara on Fri Aug 30, 2013 10:47 am, edited 1 time in total.
adhara
Posts: 11
Joined: Thu Aug 29, 2013 7:21 am

Re: Powershell script doesn't work over Cacti

Post by adhara »

jftuga wrote:Try creating a batch file and then just call this from cacti. In the first part of the batch file you could have something like this to log the fact the the batch file is actually being ran.

Code: Select all

@echo off
set LOG=c:\temp\runtime.log

for /f "usebackq tokens=1,2,3,4,5,6,7 delims=/:. " %%a in (`echo %DATE% %TIME%`) do set NOW=%%d%%b%%c_%%e%%f%%g
@echo script run at: %NOW% >> %LOG%
Will try this on monday, but ... I would prefer to use a PS script. And I'm not familiar with a batch script.

thx
adhara
Posts: 11
Joined: Thu Aug 29, 2013 7:21 am

Re: Powershell script doesn't work over Cacti

Post by adhara »

Hi,

Any idea on how to sort out this problem?

Regards.
paulgevers
Cacti Pro User
Posts: 613
Joined: Tue Aug 29, 2006 4:09 pm
Location: NL

Re: Powershell script doesn't work over Cacti

Post by paulgevers »

adhara wrote:Hi,

Any idea on how to sort out this problem?

Regards.
Add the full path to curl in the command line. Maybe curl is not on $PATH. Same goes for gawk.
Maintainer of cacti in Debian (and Ubuntu).
Cacti 1.* is now officially supported on Debian Stretch via Debian backports
FAQ Ubuntu and Debian differences
Generic cacti debugging
User avatar
BSOD2600
Cacti Moderator
Posts: 12171
Joined: Sat May 08, 2004 12:44 pm
Location: USA

Re: Powershell script doesn't work over Cacti

Post by BSOD2600 »

And if paulgevers tips dont help, then time to dig into the problem with Sysinternals Process Monitor...
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests