Powershell / ZABBIX – Gathering information about processes, that has the same name, by PID.

The first post will be about my last issue. My colleague from work wanted me to monitor memory utilization by each oracle processes – and connect them to the appropriate service. ZABBIX let us to monitor procesess, but the problem is, as far as I know, we can monitor them by name, not by the PID, and everything will be all right if those processes haven’t got the same name “oracle.exe”…

So from that point I knew I should use the “UserParamater” to get them. Additional issue, and very obvious, is that those PIDs are changing with every OS reboot. The solution is a script in powershell that you can run after each reboot, for ex. in “task scheduler”. It will create/update *.vbs scripts that are responsible for gathering the data about the memory utilization, of course zabbix_agentd.conf has to contain lines like:

UserParameter=GetMemoryVCS02DB,%systemroot%\system32\cscript.exe /nologo c:\zabbix\OracleServiceVCS02DB.vbs

to refer to those scripts. Script is very simple and looks like this:

$services = (gwmi WIn32_Service | where {$_.name -match "OracleService"})

foreach ($service in $services) {
$f = $service.name + ".vbs"
$p = $service.ProcessID
If ($f) {Clear-Content $f}
out-file -filepath $f -append -inputobject 'Set objWMI = GetObject("winmgmts:\\.\root\cimv2")'
out-file -filepath $f -append -inputobject "Set colObjects = objWMI.ExecQuery(`"SELECT * FROM Win32_Process WHERE ProcessID =`'$p`'`")"
out-file -filepath $f -append -inputobject 'For Each Item in colObjects'
out-file -filepath $f -append -inputobject 'wScript.Echo Item.WorkingSetSize'
out-file -filepath $f -append -inputobject 'Next'