Powershell script doesn't work over Cacti
Moderators: Developers, Moderators
Powershell script doesn't work over Cacti
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.
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.
Re: Powershell script doesn't work over Cacti
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%
Re: Powershell script doesn't work over Cacti
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:
For returning data, I always used a StringBuilder and then handed it back to Cacti with:
Code: Select all
Powershell.exe -command "& '<path_cacti>/scripts/CE_RxCount.ps1'"
Powershell.exe -command "& '<path_cacti>/scripts/CE_Performance.ps1' " <operation>
Code: Select all
$sb = new-object System.Text.StringBuilder
<collect data>
write-host $sb.ToString().Trim()
| Scripts: Monitor processes | RFC1213 MIB | DOCSIS Stats | Dell PowerEdge | Speedfan | APC UPS | DOCSIS CMTS | 3ware | Motorola Canopy |
| Guides: Windows Install | [HOWTO] Debug Windows NTFS permission problems |
| Tools: Windows All-in-one Installer |
Re: Powershell script doesn't work over Cacti
Hi,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:For returning data, I always used a StringBuilder and then handed it back to Cacti with:Code: Select all
Powershell.exe -command "& '<path_cacti>/scripts/CE_RxCount.ps1'" Powershell.exe -command "& '<path_cacti>/scripts/CE_Performance.ps1' " <operation>
Code: Select all
$sb = new-object System.Text.StringBuilder <collect data> write-host $sb.ToString().Trim()
Thanks for your answer.
I tried this:
Data Input Methods >
My PS script looks like :C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Unrestricted -command "& '<path_cacti>/scripts/ServerStatusApache.ps1' -target <hostname>"
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()
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"
*-*-*-*-*
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.
Re: Powershell script doesn't work over Cacti
Will try this on monday, but ... I would prefer to use a PS script. And I'm not familiar with a batch script.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%
thx
Re: Powershell script doesn't work over Cacti
Hi,
Any idea on how to sort out this problem?
Regards.
Any idea on how to sort out this problem?
Regards.
-
- Cacti Pro User
- Posts: 613
- Joined: Tue Aug 29, 2006 4:09 pm
- Location: NL
Re: Powershell script doesn't work over Cacti
Add the full path to curl in the command line. Maybe curl is not on $PATH. Same goes for gawk.adhara wrote:Hi,
Any idea on how to sort out this problem?
Regards.
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
Cacti 1.* is now officially supported on Debian Stretch via Debian backports
FAQ Ubuntu and Debian differences
Generic cacti debugging
Re: Powershell script doesn't work over Cacti
And if paulgevers tips dont help, then time to dig into the problem with Sysinternals Process Monitor...
| Scripts: Monitor processes | RFC1213 MIB | DOCSIS Stats | Dell PowerEdge | Speedfan | APC UPS | DOCSIS CMTS | 3ware | Motorola Canopy |
| Guides: Windows Install | [HOWTO] Debug Windows NTFS permission problems |
| Tools: Windows All-in-one Installer |
Who is online
Users browsing this forum: No registered users and 3 guests