Monitor version of Java
Is there a way to monitor the version of Java currently installed on a server? There is a group of Windows servers that host software that requires a certain version of Java to be installed. Java getting updated has caused issues, I was curious if there was a way for LogicMonitor to see what version is installed and to notify if that changes.
MaddyM
Posted 3 years ago·Last reply 3 years ago
13 comments
Eric Hale
·3 years agoIt looks like the script came through.
You can add a couple of Write-Host statements to your script to view the value of your variables like so…
Thanks @Stuart Weenig it was stuck in the spam filter! @aarkkelin should be good now!
LM User
·3 years agoYou should be able to post the script. @A11ey could you check for @aarkkelin ?
Aaron Arkkelin
·3 years agoI tried posting yesterday and it’s apparently still being reviewed by the moderators or something. I think the issue was I tried to post the PS script I was using. But long story short, I still can’t get it to output any data of use. It’s not giving me an error anymore so I THINK it’s running on the remote host now instead of the local but I also don’t know how to know that for sure. Without posting the script, do you know what command I should be using to essentially output the data and then have it add it to the property source?
PS I should have mentioned I’m not a PS guru by any means so I really appreciate any and all help here!
Aaron Arkkelin
·3 years agoSo, spent most of the day on this and am admittedly not a Powershell guru by any means. Unfortunately, I just cannot figure out how to get it to translate the output into anything meaningful. I don’t get the failure notices that I was getting before which makes me think it’s actually using remote PS but I also don’t know how to actually check that. I just want some verification that it’s actually seeing the response to (Get-Command java | Select-Object -ExpandProperty Version).toString() . Because the servers that I have Java installed on, I can run that command and get a response of what version number it is. But I can’t get LM to interpret that data. I’m guessing I’m missing sometime of “write/output” command but I find a lot of documentation about that online and am not sure how to use that again, in a way that LM knows how to use it.
Script “borrowed” and modified from @Stuart Weenig:
# Clears the CLI of any text
Clear-Host
# Clears memory of all previous variables
Remove-Variable * -ErrorAction SilentlyContinue
#------------------------------------------------------------------------------------------------------------
# Initialize Variables
$wmi_pass = '##WMI.PASS##'
$wmi_user = '##WMI.USER##'
$hostname = '##SYSTEM.SYSNAME##'
$collectorName = hostname
# Insert additional variables here
# If the hostname is an IP address query DNS for the FQDN
if ($hostname -match "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b") {
$hostname = [System.Net.Dns]::GetHostbyAddress($hostname).HostName
}
## This script block should contain all code that you want to execute remotely on the target host.
$scriptBlock = {
(Get-Command java | Select-Object -ExpandProperty Version).toString()
}
#------------------------------------------------------------------------------------------------------------
try {
#-----Determin the type of query to make-----
# check to see if this is monitoring the localhost collector, as we will not need to authenticate.
if ($hostname -like $collectorName) {
$response = Invoke-Command -ScriptBlock $scriptBlock
}
# are wmi user/pass set -- e.g. are these device props either not substiuted or blank
elseif (([string]::IsNullOrWhiteSpace($wmi_user) -and [string]::IsNullOrWhiteSpace($wmi_pass)) -or (($wmi_user -like '*WMI.USER*') -and ($wmi_pass -like '*WMI.PASS*'))) {
# no
$response = Invoke-Command -ComputerName $hostname -ScriptBlock $scriptBlock
} else {
# yes. convert user/password into a credential string
$remote_pass = ConvertTo-SecureString -String $wmi_pass -AsPlainText -Force;
$remote_credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $wmi_user, $remote_pass;
$response = Invoke-Command -ComputerName $hostname -Credential $remote_credential -ScriptBlock $scriptBlock
}
exit 0
} catch {
# exit code of non 0 will mean the script failed and not overwrite the instances that have already been found
throw $Error[0].Exception
exit 1
}
LM User
·3 years agoPiling on: https://github.com/sweenig/monitoring-recipes/tree/master/DataSources/PowerShell/Remoting
Aaron Arkkelin
·3 years agoThanks for the replies, @mnagel and @Michael Raymond. Fwiw, the support tech did spend awhile with me on this despite me essentially trying to spin up a new property/data source, which I know they like to remind us is always best help and out of scope. But by the end of it, the suggestion was to come back to this post and the community to try to troubleshoot more.
That being said, I will have to try some of your guys’ suggestions today and see if I can tweak this. Hoping it ends up working and will helpful for others so I can post it to the community exchange. Again, thanks for your help and I’ll probably be back with more questions later. :)
Michael Raymond
·3 years ago@mnagel Right on. Yeah with any custom module work, it’s all best effort help. That leaves it to chance a bit -- you may get someone who knows a lot about PowerShell or you may get someone you doesn’t know much at all.
@aarkkelin I do see you support ticket and have CC’d myself on it for any potential assistance. It’s currently in a pending state, so be sure to reply back to the ticket if you need any further help! And of course, please feel free to leave feedback about your support experience once the ticket is closed. It really does help!
mnagel
·3 years agoYeah, you are right -- saw Java and “support could not help” and assumed they told them to use Groovy -- my bad. That said, spending any significant time with support on this when nearly every Powershell module example there is runs against the target server remains negligent on the support side.
Michael Raymond
·3 years ago@mnagel I’m not sure I’m following your mention of Groovy. @aarkkelin did not mention LM Support confusing this PowerShell snippet with Groovy.
That said, the Support agent’s assertion is correct. By default, Get-Command will get commands for the local host it is being executed on. The code written in the module definition is always ran on the Collector host and, by default, within the Collector’s standalone PowerShell script engine (SPSE).
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-command?view=powershell-7.3#description
https://www.logicmonitor.com/support/terminology-syntax/scripting-support/embedded-powershell-scripting
The PropertySource script would need to leverage the Invoke-Command, and optionally PowerShell session, to run against a remote host. I do think your suggestion of referencing ‘Microsoft_Powershell_Info’ is very helpful. @aarkkelin On top of referencing that core PropertySource, I’ll leave you with this page as well. There’s some helpful snippets for you reference.
https://www.logicmonitor.com/support/logicmodules/datasources/powershell-support/using-powershell-scripts-in-logicmonitor
And some additional links bc I have them all open right now in my browser 😅
https://www.logicmonitor.com/support/terminology-syntax/scripting-support/powershell-tips-tricks
https://www.logicmonitor.com/support/configuring-winrm-for-windows-collector
https://www.logicmonitor.com/support/agent-conf-file-configurations#powershell
mnagel
·3 years agoThe PropertySource would be written in Powershell, not Groovy (sounds like you got a bad support tech, sadly pretty common). There is a PropertySource called Microsoft_Powershell_Info you can use as an example to get those results for the target server. Then just use the command I provided to set the auto property value and you should be all set.
Aaron Arkkelin
·3 years agoI’m trying to use this in a slightly different way, though I think it will still work if I can make it work. Essentially just looking to find out which servers actually have Java installed if they return with a version response from the cmdlet.
@mnagel When you mention setting that up in a PropertySource, do you input some other commands to get the right hostname of the remote device? I worked extensively with LM support on this earlier today and they confirmed with me that running the command and using AppliesTo doesn’t suffice because it just runs the Java cmdlet on the host the collector is running on instead of the remote device. So how do you suggest getting it to run on the actual remote devices instead?
I’d appreciate any help you can offer on this.
mnagel
·3 years agoThe Powershell command you need is:
(Get-Command java | Select-Object -ExpandProperty Version).toString()
If you set that up in a PropertySource, then you could write a datasource against that property with a datapoint that checks the version string against your required value.