Help in creating script to monitor exe memory utilization on server

Home Forums Scripting Windows Script Host Help in creating script to monitor exe memory utilization on server

This topic contains 6 replies, has 4 voices, and was last updated by Avatar dineshtla 8 years, 9 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • Avatar
    dineshtla
    Member
    #153503

    Hi,

    I want to monitor the memory utilization of specific exe “EWBIC.exe” on one of my Windows 2003 server and need to restart the respective service “IC Service” if the specific EWBIC.exe memory utiliztion is 500MB and above

    I want to get any alert / email when any of this happen by running a script after every 10 mints on the server.

    Thanks & Regards
    Dinesh Cholekkavil

    Avatar
    vonPryz
    Member
    #347585

    Re: Help in creating script to monitor exe memory utilization on server

    Use WMI. Win32_process might be a good starting point. An example at Guy Thomas’.

    -vP

    Avatar
    dineshtla
    Member
    #360827

    Re: Help in creating script to monitor exe memory utilization on server

    Do we have any script readily available for this.. The link which you gave me gave 3 different scripts.. Procees,Stop and start etc..

    Can you plz help me to merge all these scripts..

    Thanks
    Dinesh

    Avatar
    Dumber
    Participant
    #201606

    Re: Help in creating script to monitor exe memory utilization on server

    Euh I hope you can do some scripting, otherwise it would be a good idea to start learning it.
    It will become more and more important though.

    Avatar
    dineshtla
    Member
    #360829

    Re: Help in creating script to monitor exe memory utilization on server

    How to find out the memory utilization of one specific Exe file. The Script is only giving the running exe names not utilization.. need your help

    Thanks & Regards
    Dinesh Cholekkavil

    Avatar
    vonPryz
    Member
    #347588

    Re: Help in creating script to monitor exe memory utilization on server

    You didn’t read MSDN documentation for the process class, did you?

    -vP

    Rems
    Rems
    Moderator
    #227948

    Re: Help in creating script to monitor exe memory utilization on server

    dineshtla;231579 wrote:
    Hi,

    I want to monitor the memory utilization of specific exe “EWBIC.exe” on one of my Windows 2003 server and need to restart the respective service “IC Service” if the specific EWBIC.exe memory utiliztion is 500MB and above

    I want to get any alert / email when any of this happen by running a script after every 10 mints on the server.

    Thanks & Regards
    Dinesh Cholekkavil

    Code:
    option explicit

    Const PROCESS = “[B]EWBIC.exe[/B]”
    Const MEM_LIMIT_KB = [B]511999[/B]
    Const SERVICE_NAME = “[B]IC Service[/B]” ‘! make sure to use the [U]service name[/U] here not the displayname of the service

    Dim objSWbemObject, strComputer
    Dim objSWbemServices, objSWbemEventSource
    Dim objProcess, iPID, iMem

    strComputer = “.”

    Set objSWbemServices = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” _
    & strComputer & “rootcimv2”)

    ‘ Enable the SeDebugPrivilege privilege to terminate
    ‘ processes running under a different security context.
    objSWbemServices.Security_.ImpersonationLevel = 3
    objSWbemServices.Security_.privileges. _
    addasstring “SeDebugPrivilege”, True

    Set objSWbemEventSource = objSWbemServices.ExecNotificationQuery( _
    “SELECT * FROM __InstanceModificationEvent [B]WITHIN 60[/B] Where (” & _
    “TargetInstance ISA ‘Win32_Process’ AND ” & _
    “TargetInstance.Name='” & PROCESS & “‘ AND ” & _
    “TargetInstance.WorkingSetSize > ” & MEM_LIMIT_KB *1024 & “)”)

    On Error Resume Next
    Do: Set objSWbemObject = objSWbemEventSource.NextEvent()

    Select Case objSWbemObject.TargetInstance.Path_.Class

    Case “Win32_Process”
    wscript.sleep 10000 ‘ wait 10 seconds then re-check mem usage by this process

    For Each objProcess in getobject(“winmgmts://”).instancesof(_
    “Win32_Process Where Name = ‘” & PROCESS & “‘ AND WorkingSetSize > ” & MEM_LIMIT_KB *1024)
    iPID = objProcess.Processid
    iMem = objProcess.WorkingSetSize /1024

    objProcess.Terminate() ‘ optional

    call notifyMe (iPID, iMem)

    ‘ stop/start service (do not use its displayname here)
    With objSWbemServices.Get(“Win32_Service.Name='” & SERVICE_NAME & “‘”)
    .StopService()
    wscript.sleep 10000 ‘ wait 10 seconds
    .StartService()
    wscript.sleep 10000 ‘ wait 10 seconds
    End With
    Next

    End Select

    Loop

    Sub notifyMe (PID, Mem)

    With CreateObject(“WScript.Shell”)
    .popup PROCESS & ” [ID ” & PID & “] terminated ” _
    & “because its memory usage was: ” & Mem & ” kb”, _
    5, strComputer & ” Event”, 0+64
    End With

    End Sub

    WITHIN 60 is the polling interval in seconds. It should not be set smaller than 1 sec. If you like to check every 5 minutes set the value to 300

    The code in sub “notifyMe” can be replace by e-mailsending code.

    You could enhance the script so it can also monitor the state of the service, to ensure the service will be restarted anytime it stopped for what ever reason.

    additionally… you could schedule a watchdog script waching the monitor script’s back. Execute the watchdog script periodically to check if the above monitor script is still running, if it is terminated for some reason it is launched it again by the watchdog.

    Rems

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.