Forum Discussion

MaddyM's avatar
2 years ago

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. 

  • The 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.

  • I 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!

  • Anonymous's avatar
    Anonymous

    You should be able to post the script. @A11ey could you check for @aarkkelin ?

  • It 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…

    # 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

    write-host "hostname=" $hostname;
    write-host "collectorName=" $collectorName;

    # 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
    }