Jump to content

Process monitor with graph


eignxing
 Share

Recommended Posts

I created a script to monitor process memory usage over time and another script to graph that. I not really motivated to do any more work on it, but I thought someone might be able to use or improve on it. The script checks processes and memory used every minute and records it to log files. You have to run the process monitor for a little while before there is any data to plot with the graph script.

Thanks to andybiochem for The attachment below is a modified version to include text.

Posted Image

modified gdiplus udf and scripts:

GraphGDIPlus.au3

process monitor.au3

process graph.au3

process monitor script:

#Include <Array.au3>

Global $process_file, $t_diff2 = 0

$data_dir = @WorkingDir&"/Data"

$t_init2 = TimerInit()
$t_init = TimerInit()
$x = 1
While $t_diff2/1000/60/60 <= 24
    $processlist_ar = ProcessList()
    $tCur = String(@YEAR&"/"&@MON&"/"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC)
    Sleep(10000)
    $t_diff2 = TimerDiff($t_init2)
    If TimerDiff($t_init) >= 60000 Then
        $t_init = TimerInit()
        $i = 1
        Do
            $processstats_ar = ProcessGetStats($processlist_ar[$i][1], 0)
            If IsArray($processstats_ar) Then
                $processstats_ar[0] = $processstats_ar[0]/1024
                $processstats_ar[1] = $processstats_ar[1]/1024
                If FileExists($data_dir&"/"&$processlist_ar[$i][0]&".log") = 0 Then
                    $process_file = FileOpen($data_dir&"/"&$processlist_ar[$i][0]&".log", 2)
                Else
                    $process_file = FileOpen($data_dir&"/"&$processlist_ar[$i][0]&".log", 1)
                EndIf
                FileWriteLine($process_file, $processlist_ar[$i][0]&"<>"&$processstats_ar[0]&"<>"&$tCur)
                FileClose($process_file)
            EndIf

            $i = $i + 1
        Until $i = $processlist_ar[0][0]
    EndIf
    $x = $x + 1
WEnd

process graph script:

#include <GraphGDIPlus.au3>
#Include <File.au3>
#Include <Array.au3>
#Include <Date.au3>

Opt("GUIOnEventMode", 1)

$desktopW = @DesktopWidth-20
$desktopH = @DesktopHeight-60

$GUI = GUICreate("",$desktopW,$desktopH, -1, 0)
GUISetBkColor(0x000000)
$MenuItem1 = GUICtrlCreateMenu("File")
$MenuItem3 = GUICtrlCreateMenuItem("Zoom In", $MenuItem1)
GUICtrlSetOnEvent(-1, "_zoomin")
$MenuItem2 = GUICtrlCreateMenuItem("Zoom Out", $MenuItem1)
GUICtrlSetOnEvent(-1, "_zoomout")
$separator1 = GUICtrlCreateMenuItem("", $MenuItem1, 2)
$MenuItem11 = GUICtrlCreateMenuItem("30 Minutes", $MenuItem1)
GUICtrlSetOnEvent(-1, "_30min")
$MenuItem10 = GUICtrlCreateMenuItem("2 Hours", $MenuItem1)
GUICtrlSetOnEvent(-1, "_120min")
$MenuItem4 = GUICtrlCreateMenuItem("12 Hours", $MenuItem1)
GUICtrlSetOnEvent(-1, "_12hrs")
$MenuItem5 = GUICtrlCreateMenuItem("24 Hours", $MenuItem1)
GUICtrlSetOnEvent(-1, "_24hrs")
$MenuItem6 = GUICtrlCreateMenuItem("3 Days", $MenuItem1)
GUICtrlSetOnEvent(-1, "_3days")
$MenuItem7 = GUICtrlCreateMenuItem("1 Week", $MenuItem1)
GUICtrlSetOnEvent(-1, "_1week")
$MenuItem8 = GUICtrlCreateMenuItem("1 Month", $MenuItem1)
GUICtrlSetOnEvent(-1, "_1mnth")
$MenuItem9 = GUICtrlCreateMenuItem("3 Months", $MenuItem1)
GUICtrlSetOnEvent(-1, "_3mnth")
$MenuItem9 = GUICtrlCreateMenuItem("6 Months", $MenuItem1)
GUICtrlSetOnEvent(-1, "_6mnth")

GUISetOnEvent(-3,"_Exit")
GUISetState()

$data_dir = @WorkingDir&"/Data"
$date_now = String(@YEAR&"/"&@MON&"/"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC)

;----- Create Graph area -----
Global $hMem = 100000, $xticks = 24, $xlabels = 24, $tick_every = 1, $xtimeframe = 24*60*60

$Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
_GraphGDIPlus_Set_RangeX($Graph,0,$xticks,$xlabels,1,0)
_GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
_GraphGDIPlus_Set_GridX($Graph,1,0xFF3F3F3F)
_GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)

Func _zoomin()
    _GraphGDIPlus_Delete($GUI,$Graph)
    $hMem = Round($hMem*.5, 0)
    $Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
    _GraphGDIPlus_Set_RangeX($Graph,0,$xticks,$xlabels,1,0)
    _GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
    _GraphGDIPlus_Refresh($Graph)
    _GraphGDIPlus_Set_GridX($Graph,$tick_every,0xFF3F3F3F)
    _GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)
    _Draw_Graph()
EndFunc

Func _zoomout()
    _GraphGDIPlus_Delete($GUI,$Graph)
    $hMem = Round($hMem*1.5, 0)
    $Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
    _GraphGDIPlus_Set_RangeX($Graph,0,$xticks,$xlabels,1,0)
    _GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
    _GraphGDIPlus_Refresh($Graph)
    _GraphGDIPlus_Set_GridX($Graph,$tick_every,0xFF3F3F3F)
    _GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)
    _Draw_Graph()
EndFunc

Func _30min()
    $xtimeframe = 60*30
    $xticks = 30
    $xlabels = 30
    $tick_every = 5
    _GraphGDIPlus_Delete($GUI,$Graph)
    $Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
    _GraphGDIPlus_Set_RangeX($Graph,0,$xticks,$xlabels,1,0)
    _GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
    _GraphGDIPlus_Refresh($Graph)
    _GraphGDIPlus_Set_GridX($Graph,$tick_every,0xFF3F3F3F)
    _GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)
    _Draw_Graph()
EndFunc

Func _120min()
    $xtimeframe = 120*60
    $xticks = 120
    $xlabels = 15
    $tick_every = 4
    _GraphGDIPlus_Delete($GUI,$Graph)
    $Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
    _GraphGDIPlus_Set_RangeX($Graph,0,$xticks,$xlabels,1,0)
    _GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
    _GraphGDIPlus_Refresh($Graph)
    _GraphGDIPlus_Set_GridX($Graph,$tick_every,0xFF3F3F3F)
    _GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)
    _Draw_Graph()
EndFunc

Func _12hrs()
    $xtimeframe = 12*60*60
    $xticks = 12
    $xlabels = 12
    $tick_every = 1
    _GraphGDIPlus_Delete($GUI,$Graph)
    $Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
    _GraphGDIPlus_Set_RangeX($Graph,0,$xticks,$xlabels,1,0)
    _GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
    _GraphGDIPlus_Refresh($Graph)
    _GraphGDIPlus_Set_GridX($Graph,$tick_every,0xFF3F3F3F)
    _GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)
    _Draw_Graph()
EndFunc

Func _24hrs()
    $xtimeframe = 24*60*60
    $xticks = 24
    $xlabels = 24
    $tick_every = 1
    _GraphGDIPlus_Delete($GUI,$Graph)
    $Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
    _GraphGDIPlus_Set_RangeX($Graph,0,$xticks,$xlabels,1,0)
    _GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
    _GraphGDIPlus_Refresh($Graph)
    _GraphGDIPlus_Set_GridX($Graph,$tick_every,0xFF3F3F3F)
    _GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)
    _Draw_Graph()
EndFunc

Func _3days()
    $xtimeframe = 36*60*60
    $xticks = 36
    $xlabels = 12
    $tick_every = 1
    _GraphGDIPlus_Delete($GUI,$Graph)
    $Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
    _GraphGDIPlus_Set_RangeX($Graph,0,$xticks,$xlabels,1,0)
    _GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
    _GraphGDIPlus_Refresh($Graph)
    _GraphGDIPlus_Set_GridX($Graph,$tick_every,0xFF3F3F3F)
    _GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)
    _Draw_Graph()
EndFunc

Func _1week()
    $xtimeframe = 168*60*60
    $xticks = 168
    $xlabels = 12
    $tick_every = 4
    _GraphGDIPlus_Delete($GUI,$Graph)
    $Graph = _GraphGDIPlus_Create($GUI,60,20,$desktopW*.93,$desktopH*.9,0xFF3F3F3F,0xFF000000)
    _GraphGDIPlus_Set_RangeX($Graph,0,$xticks ,$xlabels,1,0)
    _GraphGDIPlus_Set_RangeY($Graph,0,$hMem,20,1,0)
    _GraphGDIPlus_Refresh($Graph)
    _GraphGDIPlus_Set_GridX($Graph,$tick_every,0xFF3F3F3F)
    _GraphGDIPlus_Set_GridY($Graph,$hMem*.10,0xFF3F3F3F)
    _Draw_Graph()
EndFunc

Func _1mnth()
EndFunc
Func _3mnth()
EndFunc
Func _6mnth()
EndFunc

;----- Draw the graph -----
_Draw_Graph()

While 1
    Sleep(100)
WEnd

Func _Draw_Graph()

    $filelist_ar = _FileListToArray($data_dir, "*.log", 1)

    Dim $fileinfo_ar[$filelist_ar[0]][4]    ;processname, color, max_memory_index, max memory index time

    $i = 1
    Do
        $fileinfo_ar[$i][1] = _RandClr()
        $process_file = FileOpen($data_dir&"/"&$filelist_ar[$i], 0)
        $n = _FileCountLines($data_dir&"/"&$filelist_ar[$i])

        _GraphGDIPlus_Set_PenColor($Graph, $fileinfo_ar[$i][1])
        _GraphGDIPlus_Set_PenSize($Graph,2)

        $lineread = FileReadLine($process_file)
        $div1 = StringInStr($lineread, "<>", 0, 1)
        $fileinfo_ar[$i][0] = StringLeft($lineread, $div1-1)
        $div2 = StringInStr($lineread, "<>", 0, 2)
        $memory_point = StringMid($lineread, $div1+2, $div2-$div1-2) ;memory
        $timestamp = StringRight($lineread, StringLen($lineread)-$div2-1) ;timestamp
        $date_diff = _DateDiff("s", $timestamp, $date_now)
        $fileinfo_ar[$i][3] = 0
        _GraphGDIPlus_Plot_Start($Graph,(($xtimeframe-$date_diff)/$xtimeframe)*$xticks,$memory_point)
        $j = 2
        Do
            $lineread = FileReadLine($process_file)
            $div1 = StringInStr($lineread, "<>", 0, 1)
            $div2 = StringInStr($lineread, "<>", 0, 2)
            $memory_point = StringMid($lineread, $div1+2, $div2-$div1-2) ;memory
            $timestamp = StringRight($lineread, StringLen($lineread)-$div2-1) ;timestamp
            $date_diff = _DateDiff("s", $timestamp, $date_now)

            ;FileSetPos ($process_file, -StringLen($lineread)*2, 1)

            If $memory_point <> 0 Then
                If $memory_point < $hMem Then
                    If $date_diff < $xtimeframe Then
                        If $memory_point > $fileinfo_ar[$i][2] Then
                            $fileinfo_ar[$i][2] = $memory_point
                            $fileinfo_ar[$i][3] = $date_diff
                        EndIf

                        If $n = 1 Then
                            _GraphGDIPlus_Plot_Dot($Graph,(($xtimeframe-$date_diff)/$xtimeframe)*$xticks,$memory_point)
                        Else
                            $timestamp2 = StringRight(FileReadLine($process_file), StringLen($lineread)-$div2-1) ;timestamp
                            $internal_dif = _DateDiff("s", $timestamp, $timestamp2)
                            If $internal_dif <= 63 Then
                                _GraphGDIPlus_Plot_Line($Graph,(($xtimeframe-$date_diff)/$xtimeframe)*$xticks,$memory_point)
                            Else
                                _GraphGDIPlus_Plot_Start($Graph,(($xtimeframe-$date_diff+$internal_dif)/$xtimeframe)*$xticks,$memory_point)
                            EndIf
                        EndIf
                    Else
                        _GraphGDIPlus_Plot_Start($Graph,(($xtimeframe-$date_diff)/$xtimeframe)*$xticks,$memory_point)
                    EndIf
                Else
                    _GraphGDIPlus_Plot_Start($Graph,(($xtimeframe-$date_diff)/$xtimeframe)*$xticks,$memory_point)
                EndIf
            EndIf

            $j = $j + 1
        Until $j >= $n
        FileClose($process_file)
        _GraphGDIPlus_Refresh($Graph)
        IF $fileinfo_ar[$i][3] = 0 Then
            $fileinfo_ar[$i][3] = Random(0, $xtimeframe-20, 1)
        EndIf

        $i = $i + 1
    Until $i = $filelist_ar[0]

    $i = 1
    Do
        If $fileinfo_ar[$i][2] < $hMem And $fileinfo_ar[$i][3] <> 0 And $fileinfo_ar[$i][2] <> 0 Then
            If ($xtimeframe-$fileinfo_ar[$i][3])/$xtimeframe*$xticks < 5 Then
                _GraphGIDPlus_Plot_Text($Graph, Random(5, 10, 1), $fileinfo_ar[$i][2], $fileinfo_ar[$i][0], $fileinfo_ar[$i][1], 8, "Arial") ;$aGraphArray, $iX, $iY, $iText, $iColor)
            Else
                _GraphGIDPlus_Plot_Text($Graph, (($xtimeframe-$fileinfo_ar[$i][3])/$xtimeframe)*$xticks, $fileinfo_ar[$i][2], $fileinfo_ar[$i][0], $fileinfo_ar[$i][1], 8, "Arial") ;$aGraphArray, $iX, $iY, $iText, $iColor)
            EndIf
            ;MsgBox(0, "($xtimeframe-$fileinfo_ar[$i][3])/$xtimeframe)*$xticks", ($xtimeframe-$fileinfo_ar[$i][3])/$xtimeframe*$xticks)
        EndIf
        $i = $i + 1
    Until $i = $filelist_ar[0]
EndFunc

Func _RandClr()
    Return "0xFF" & Hex(Random(0, 16777215, 1), 6)
EndFunc

Func _Exit()
    ;----- close down GDI+ and clear graphic -----
    _GraphGDIPlus_Delete($GUI,$Graph)
    Exit
EndFunc
Link to comment
Share on other sites

Nice work. Personally, I like to use adlibs instead of timers when feasible.

Opt("MustDeclareVars", 1)

Global $gsDataDir = @WorkingDir & "/Data"
If Not FileExists($gsDataDir) Then DirCreate($gsDataDir)

AdlibRegister("_AdlibLogStats", 60000) ; 60 seconds
AdlibRegister("_Exit", 86400000) ; 24 hours (1440 min, 86400 seconds, 86400000 milliseconds)

While 1
    Sleep(1000)
WEnd

Func _AdlibLogStats()
    Local $i, $lsNow = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC
    Local $laProcessList = ProcessList()
    If @error or Not IsArray($laProcessList) Then Return SetError(1, 0, False)
    For $i = 1 to $laProcessList[0][0]
        FileWriteLine($gsDataDir & "\" & $laProcessList[$i][0] & ".log", $laProcessList[$i][1] & "<>" & $laProcessList[$i][1] & "<>" & $lsNow)
    Next
EndFunc

Func _Exit()
    Exit 0
EndFunc

I haven't tried it out yet, but I kind of like the idea. An INI file or a GUI would be nice to define how often to log processes, how long to log before stopping. Anyway, I get that the process monitor script was a quick and dirty version just to get something usable to graph. The graphing is the real work here.

One thought on the graphing. I see you have functions for fixed time functions. Why not have a single function to which you pass a variable timeframe entered via an edit control or a slider control or something?

Edited by c0deWorm

My UDFs: ExitCodes

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...