2015-01-29 2 views
1

2 초마다 CPU 사용량을 모니터링하는 Windows Server 2008 상자에서 .NET 콘솔 응용 프로그램을 실행하고 싶습니다.Y 초 동안 X % 이상의 프로세스에서 CPU 스파이크 모니터링

단일 응용 프로그램이> 30 % CPU를 사용하는 경우 두 번 연속해서 기록해야합니다.

2 초마다 "CheckCpu"가 실행되지만 내 문제는 각 프로세스의 CPU 사용량을 어떻게 결정합니까? 그리고, 효율적으로 그래서 나는

Dim lStrProcessCurrent As List(Of String) 
Dim lStrProcessPrevious As List(Of String) 

Private Sub CheckCpu() 

    Dim pcc As New PerformanceCounterCategory("Process") 

    'Clear "Current" run 
    lStrProcessCurrent = New List(Of String) 

    For Each instance As String In pcc.GetInstanceNames 
     Using cnt As PerformanceCounter = New PerformanceCounter("Process", "ID Process", instance, True) 

      Dim processName As String = cnt.InstanceName 

' ????? HOW TO DETERMINE CPU USAGE ???? 
      Dim cpuUsage As Long = 31 

      If cpuUsage >= 30 Then 

       'Was over 30% in Previous check 
       If lStrProcessPrevious.Contains(processName) Then 
        LogCpuSpike(processName) 
       End If 

       lStrProcessCurrent.Add(processName) 
      End If 

     End Using 
    Next 

    'Update "Previous" run 
    lStrProcessPrevious = lStrProcessCurrent 

End Sub 
+0

질문에 downvoted가 있지만 의견이 없습니다. 제 질문이 명확하지 않았습니까? 네 번째 개발 노력을 기울이지 않았습니까? – adam

+0

Windows 리소스 모니터는 코드를 전혀 작성하지 않고도이를 수행 할 수 있습니다. –

+0

좋은 질문이라고 생각합니다. 어떤 사람들은 그들이 이해하지 못하는 질문을 즉시 downvote. – xpda

답변

1

당신은 아래의 코드처럼 뭔가를 시도 할 수 우리의 서버를 :) 수렁하지 않습니다. 효과적으로 CheckCpu()를 호출 할 때마다 각 프로세스의 TotalProcessorTime을 확인한 다음 이전 실행에서이 값을 빼고 두 검사 사이에 경과 된 총 시간으로 나눕니다. 프로세스가 당신이 GetProcesses()를 호출 한 다음 목록을 처리하는 사이에 사망하는 것이 가능하기 때문에 당신은 아마 좀 더 검사를 추가해야 프로덕션 환경에서

Sub Main() 

    Dim previousCheckTime As New DateTime 
    Dim previousProcessList As New List(Of ProcessInformation) 

    ' Kick off an initial check 
    previousCheckTime = Now 
    previousProcessList = CheckCPU(previousProcessList, Nothing) 

    For i As Integer = 0 To 10 
     Threading.Thread.Sleep(1000) 

     previousProcessList = CheckCPU(previousProcessList, Now - previousCheckTime) 
     previousCheckTime = Now 

     For Each process As ProcessInformation In previousProcessList 
      Console.WriteLine(process.Id & " - " & Math.Round(process.CpuUsage, 2).ToString & "%") 
     Next 
     Console.WriteLine("-- Next check --") 
    Next 

    Console.ReadLine() 
End Sub 

Private Function CheckCPU(previousProcessList As List(Of ProcessInformation), timeSinceLastCheck As TimeSpan) As List(Of ProcessInformation) 

    Dim currentProcessList As New List(Of ProcessInformation) 
    For Each process As Process In Process.GetProcesses() 

     ' Id = 0 is the system idle process so we don't check that 
     If process.Id <> 0 Then 

      ' See if this process existed last time we checked 
      Dim cpuUsage As Double = -1 
      Dim previousProcess As ProcessInformation = previousProcessList.SingleOrDefault(Function(p) p.Id = process.Id) 

      ' If it did then we can calculate the % of CPU time it has consumed 
      If previousProcess IsNot Nothing AndAlso timeSinceLastCheck <> Nothing Then 
       cpuUsage = ((process.TotalProcessorTime - previousProcess.TotalProcessorTime).Ticks/(Environment.ProcessorCount * timeSinceLastCheck.Ticks)) * 100 
      End If 

      ' Add to the current process list 
      currentProcessList.Add(New ProcessInformation With {.Id = process.Id, .CpuUsage = cpuUsage, .TotalProcessorTime = process.TotalProcessorTime}) 
     End If 

    Next 

    Return currentProcessList 

End Function 

Class ProcessInformation 
    Public Id As Integer 
    Public TotalProcessorTime As TimeSpan 
    Public CpuUsage As Double 
End Class 

. 프로세스가 사라진 경우 TotalProcessorTime 속성에 액세스하려고하면 오류가 발생합니다.

+0

이 코드의 수정 된 버전을 사용하여 끝났습니다. 도움 주셔서 대단히 감사합니다! . . . . (CurrCPU - PreviousCPU)/(CurrTime - PreviousTime) * 100 – adam

관련 문제