Jump to content
Sign in to follow this  
WhiteCrow

[Solved]Processor usage question

Recommended Posts

WhiteCrow

I can't find a clear way to see at what cpu % usage my machine is, anyone know how to ?

Becouse i have made this server restart program, wich works fine exept when the server application it monitors "freezes", and the monitored application just sits there using 100% cpu utilisation.

So if the monitored application hits 100% cpu usage it should be closed, and restarted.

Here is what i have, and works fine:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.1.4 (beta)
 Author:         R.Westerhof (www.drakkaa.nl)

 Script Function:
    Restarts a MangOs server
    With logging and a inifile

#ce ----------------------------------------------------------------------------



$x=0
$y=0
$crashnumber_m=-1
$crashnumber_r=-1

$time=IniRead ( "restart.ini", "Config", "Time", "320" )
$mangos_ini=IniRead ( "restart.ini", "Config", "MangOS", "mangosd.exe" )
$realmd_ini=IniRead ( "restart.ini", "Config", "Realmd", "realmd.exe" )
$mangos_path=IniRead ( "restart.ini", "Config", "Path", "c:\mangos\" )
$logging_enabled=IniRead ( "restart.ini", "Logging", "LogEnabled", "1" )
$log_filename=IniRead ( "restart.ini", "Logging", "LogName", "restartlog.txt" )
$save_number=IniRead ( "restart.ini", "AccountSave", "save_number", "1" )
$starttime=IniRead ( "restart.ini", "Config", "StartTime", "320" )



Sleep($starttime*1000)

$time=$time*1000

If $logging_enabled=1 Then
        FileWriteLine ( $log_filename, @CRLF )
    FileWriteLine ( $log_filename, TimeStamp() & " - MangOs restarter Started"  )
endif

;MainLoop

While 1
    
Sleep($time)
;wich errors window to check, and enter away.
IF WinExists ( "Microsoft Windows") Then
Send ("{Enter}")
Sleep(3000)
EndIf
IF WinExists ( "mangosd.exe - Toepassingsfout") Then
Send ("{Enter}")
Sleep(3000)
EndIf
IF WinExists ( "mangosd.exe - Application Error") Then
Send ("{Enter}")
Sleep(3000)
EndIf
IF WinExists ( "Microsoft Visual C++ Runtime Library") Then
Send ("{Enter}")
Sleep(3000)
EndIf
IF WinExists ( "mysqld-nt.exe - Application Error") Then
Send ("{Enter}")
Sleep(3000)
EndIf

;check if its time for backup
If @HOUR = 13 or @HOUR=1 Then
    BackupDB()
EndIf

;check if the server process1 exists
If ProcessExists($mangos_ini) = 0 then
    If $logging_enabled=1 Then
        $crashnumber_m+=1
        If $crashnumber_m > 0 then
            FileWriteLine ( $log_filename, TimeStamp() & " - " & $mangos_ini & " - process missing from memory. " & "Restart (" & $crashnumber_m & ")" )
        EndIf
  endif  
    Run($mangos_path & $mangos_ini)
Endif

;check if the server process2 exists
If ProcessExists($realmd_ini) = 0 then
    If $logging_enabled=1 Then
        $crashnumber_r+=1
        If $crashnumber_r > 0 Then
            FileWriteLine ( $log_filename, TimeStamp() & " - " & $realmd_ini & " - process missing from memory. " & "Restart (" & $crashnumber_r & ")" )    
        EndIf
    EndIf
     Run($mangos_path & $realmd_ini) 
endif     

Wend

;Timestamp for logging
Func TimeStamp()
    $far=@MON & "-" & @MDAY & "-" & @YEAR & " / " & @HOUR & ":" & @MIN & ":" & @SEC
Return $far
Endfunc

;backup the database and copy the files to the backup directory
 Func BackupDB()
  If $backuprun=1 Then
      return 0
  Else
     run("backup.bat")
     Sleep(300000)
;copy the freshly made MySQL backup files to @scriptdir \ backup dir
     Filecopy( @ScriptDir & "\" & "realmd_backup.sql", @ScriptDir & "\backup\" & "realmd_backup" & @YEAR & @MON & @MDAY & ".sql", 9)
     Filecopy( @ScriptDir & "\" & "characters_backup.sql", @ScriptDir & "\backup\" & "characters_backup" & @YEAR & @MON & @MDAY & ".sql", 9)
     EndIf
 EndFunc

The ini file:

[Config]
;howmany seconds much restarter wait to begin opening the server processes.
StartTime=60

;every how many seconds does restarter check server memory for server processes, or errors.
Time=10

;MangOS .exe name (case sensitive.)
MangOS=mangosd.exe

;RealmD .exe name (case sensitive.)
Realmd=realmd.exe

;The path of mangos, include the last \   e.g. : c:\mangos\ (case sencitive.)
Path=c:\mangos\

[Logging]

;are crashlogs enabled 1 for yes and 0 for no.
LogEnabled=1

;the filename of the crashlog.
LogName=restartlog.txt


;========== server restarter and config by www.drakkaa.nl==========
Edited by WhiteCrow

Share this post


Link to post
Share on other sites
WhiteCrow

Sorry to bump this so soon, but i'm really curious how to read the cpu usage.

And i have no idea where to look :)

Share this post


Link to post
Share on other sites
RWBaker

I can't find a clear way to see at what cpu % usage my machine is, anyone know how to ?

Becouse i have made this server restart program, wich works fine exept when the server application it monitors "freezes", and the monitored application just sits there using 100% cpu utilisation.

So if the monitored application hits 100% cpu usage it should be closed, and restarted.

Here is what i have, and works fine:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.1.4 (beta)
 Author:         R.Westerhof (www.drakkaa.nl)

 Script Function:
    Restarts a MangOs server
    With logging and a inifile

#ce ----------------------------------------------------------------------------



$x=0
$y=0
$crashnumber_m=-1
$crashnumber_r=-1

$time=IniRead ( "restart.ini", "Config", "Time", "320" )
$mangos_ini=IniRead ( "restart.ini", "Config", "MangOS", "mangosd.exe" )
$realmd_ini=IniRead ( "restart.ini", "Config", "Realmd", "realmd.exe" )
$mangos_path=IniRead ( "restart.ini", "Config", "Path", "c:\mangos\" )
$logging_enabled=IniRead ( "restart.ini", "Logging", "LogEnabled", "1" )
$log_filename=IniRead ( "restart.ini", "Logging", "LogName", "restartlog.txt" )
$save_number=IniRead ( "restart.ini", "AccountSave", "save_number", "1" )
$starttime=IniRead ( "restart.ini", "Config", "StartTime", "320" )



Sleep($starttime*1000)

$time=$time*1000

If $logging_enabled=1 Then
        FileWriteLine ( $log_filename, @CRLF )
    FileWriteLine ( $log_filename, TimeStamp() & " - MangOs restarter Started"  )
endif

;MainLoop

While 1
    
Sleep($time)
;wich errors window to check, and enter away.
IF WinExists ( "Microsoft Windows") Then
Send ("{Enter}")
Sleep(3000)
EndIf
IF WinExists ( "mangosd.exe - Toepassingsfout") Then
Send ("{Enter}")
Sleep(3000)
EndIf
IF WinExists ( "mangosd.exe - Application Error") Then
Send ("{Enter}")
Sleep(3000)
EndIf
IF WinExists ( "Microsoft Visual C++ Runtime Library") Then
Send ("{Enter}")
Sleep(3000)
EndIf
IF WinExists ( "mysqld-nt.exe - Application Error") Then
Send ("{Enter}")
Sleep(3000)
EndIf

;check if its time for backup
If @HOUR = 13 or @HOUR=1 Then
    BackupDB()
EndIf

;check if the server process1 exists
If ProcessExists($mangos_ini) = 0 then
    If $logging_enabled=1 Then
        $crashnumber_m+=1
        If $crashnumber_m > 0 then
            FileWriteLine ( $log_filename, TimeStamp() & " - " & $mangos_ini & " - process missing from memory. " & "Restart (" & $crashnumber_m & ")" )
        EndIf
  endif  
    Run($mangos_path & $mangos_ini)
Endif

;check if the server process2 exists
If ProcessExists($realmd_ini) = 0 then
    If $logging_enabled=1 Then
        $crashnumber_r+=1
        If $crashnumber_r > 0 Then
            FileWriteLine ( $log_filename, TimeStamp() & " - " & $realmd_ini & " - process missing from memory. " & "Restart (" & $crashnumber_r & ")" )    
        EndIf
    EndIf
     Run($mangos_path & $realmd_ini) 
endif     

Wend

;Timestamp for logging
Func TimeStamp()
    $far=@MON & "-" & @MDAY & "-" & @YEAR & " / " & @HOUR & ":" & @MIN & ":" & @SEC
Return $far
Endfunc

;backup the database and copy the files to the backup directory
 Func BackupDB()
  If $backuprun=1 Then
      return 0
  Else
     run("backup.bat")
     Sleep(300000)
;copy the freshly made MySQL backup files to @scriptdir \ backup dir
     Filecopy( @ScriptDir & "\" & "realmd_backup.sql", @ScriptDir & "\backup\" & "realmd_backup" & @YEAR & @MON & @MDAY & ".sql", 9)
     Filecopy( @ScriptDir & "\" & "characters_backup.sql", @ScriptDir & "\backup\" & "characters_backup" & @YEAR & @MON & @MDAY & ".sql", 9)
     EndIf
 EndFunc

The ini file:

[Config]
;howmany seconds much restarter wait to begin opening the server processes.
StartTime=60

;every how many seconds does restarter check server memory for server processes, or errors.
Time=10

;MangOS .exe name (case sensitive.)
MangOS=mangosd.exe

;RealmD .exe name (case sensitive.)
Realmd=realmd.exe

;The path of mangos, include the last \   e.g. : c:\mangos\ (case sencitive.)
Path=c:\mangos\

[Logging]

;are crashlogs enabled 1 for yes and 0 for no.
LogEnabled=1

;the filename of the crashlog.
LogName=restartlog.txt


;========== server restarter and config by www.drakkaa.nl==========
When I get the the CPU%, I have the Task Manager active but minimized...here is what I do:

$cpu_usage = StatusbarGetText("Windows Task Manager","",2)

$cpu_usage = StringReplace(StringStripWS(StringRegExpReplace($cpu_usage,"^[^:]*:",""),8),"%","")

msgbox(0,"",$cpu_usage)

Does this help?

-RWBaker


RW Baker

Share this post


Link to post
Share on other sites
WhiteCrow

When I get the the CPU%, I have the Task Manager active but minimized...here is what I do:

$cpu_usage = StatusbarGetText("Windows Task Manager","",2)

$cpu_usage = StringReplace(StringStripWS(StringRegExpReplace($cpu_usage,"^[^:]*:",""),8),"%","")

msgbox(0,"",$cpu_usage)

Does this help?

-RWBaker

A good reply, thank you, but there has to be another way ;)

I'll see if i can find it :)

Here another version made by Jos (but its still not what i'm looking for :D )

Func _Busy( $WINDOW )
  Do
     Do
        Sleep( 100 )
        $PC = StatusbarGetText("Windows Task Manager","",2)
        $PC = StringTrimRight( $PC, 1)   ; remove the %
        $PC = StringRight( $PC, 3 )        ; get the possible 3 digits
        $PC = Int(StringReplace($PC,":","")); remove colon and convert to int.
     Until  $PC < 10
     Do
        Sleep( 100 )
     Until  MouseGetCursor( ) <> 15
  Until WinActive($WINDOW)
EndFunc ;==>_Busy
Edited by WhiteCrow

Share this post


Link to post
Share on other sites
WhiteCrow

Found the solution:

Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly
Global $strComputer = @ComputerName

Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Global $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlags)

$n = 0
$sMsg = ""
For $objItem In $colItems
    $sMsg &= $n & ":  LoadPercentage: " & $objItem.LoadPercentage & @CRLF
    $n += 1
Next

MsgBox(64, "CPU Load", $sMsg)

Share this post


Link to post
Share on other sites
zackrspv

Here's something that was quickly created by scriptomatic:

Perhaps it'll help you, it shows the idle time of the processor:

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems

      $Output = $Output & "PercentIdleTime: " & $objItem.PercentIdleTime & @CRLF
      if ConsoleWrite($Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PerfFormattedData_PerfOS_Processor" )
Endif

May, or may not be what you want, but it was fun to figure out.


-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites
RWBaker

I like it a lot...Good work!

RWBaker


RW Baker

Share this post


Link to post
Share on other sites
Xand3r

#include<GUIConstants.au3>
Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly
Global $strComputer = @ComputerName
Dim $procs[10]
Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
GUICreate("Test" , 480,90)

$a=GUICtrlCreateProgress(0,10,400,20)
$aa=GUICtrlCreateLabel("0%" , 400,10,80,20)
$b=GUICtrlCreateProgress(0,40,400,20)
$bb=GUICtrlCreateLabel("0%" , 400,40,80,20)
$time=GUICtrlCreateLabel("" , 0 , 70, 400,20)
GUISetState()
$timer=TimerInit()
While GUIGetMsg()<>$GUI_EVENT_CLOSE
    Global $colItems = $objWMIService.ExecQuery("SELECT LoadPercentage FROM Win32_Processor", "WQL", $wbemFlags)
    $n = 0
    $sMsg = ""
    For $objItem In $colItems
        $sMsg &= $n & ":  LoadPercentage: " & $objItem.LoadPercentage & @CRLF
        $procs[$n]=$objItem.LoadPercentage
        $n += 1
    Next
    GUICtrlSetData($a , $procs[0])
    GUICtrlSetData($b , $procs[1])
    GUICtrlSetData($aa , $procs[0]&"%")
    GUICtrlSetData($bb , $procs[1]&"%")
    GUICtrlSetData($time , Round(TimerDiff($timer)/1000)&" secs refresh interval")
    $timer=TimerInit()
    Sleep(5)
WEnd

is there any way to make it faster??? cause it gives me a 2 seconds refresh interval...


Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites
WhiteCrow

This is faster, with my computer this code gives me about the same interval as the task manager does.

Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly
Global $strComputer = @ComputerName
Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")


Func CPU_utilize()
    Global $colItems = $objWMIService.ExecQuery("SELECT LoadPercentage FROM Win32_Processor", "WQL", $wbemFlags)
    For $objItem In $colItems
        $sMsg = INT($objItem.LoadPercentage)
    Next
    Return $sMsg
EndFunc
    
ProgressOn ( "CPU Utilisation", "" , "")

While 1
    $returned=CPU_utilize()
    ProgressSet($returned, $returned & " %")
WEnd

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×