实现服务器性能指标监控

我是多个应用系统的管理员,需要管理多台基于.NET的服务器,为了方便我对各个服务器进行硬件性能指标的监控,我编写了一个程序,每隔10秒采样一次服务器各项硬件性能指标,并把相应的指标写入远程SQL Server2000。然后在一个页面读取该数据库,即可同时获取各台服务器各项性能指标。页面每隔10秒刷新一次。

改进方向:1、在监控页面加入无刷新技术;2、性能指标达到一定的伐值,触发短信通知;3、加入IIS Head头信息返回,即可获取IIS状态。

硬件监控代码如下(VS2003):

Imports System
Imports System.Management
Imports System.Environment

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private _oPerformanceCounter As PerformanceCounter

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 5000
        Timer1.Enabled = True
    End Sub


    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Disposed
        Timer1.Enabled = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Dispose()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        '获取CPU占用率
        _oPerformanceCounter = New PerformanceCounter("Processor", "% Processor Time", "_Total")
        Dim _oA As CounterSample = _oPerformanceCounter.NextSample
        System.Threading.Thread.Sleep(1000)
        Dim _oB As CounterSample = _oPerformanceCounter.NextSample
        Dim _nVal As Single
        _nVal = CounterSample.Calculate(_oA, _oB)
        Dim _s As String
        _s = _nVal.ToString() + "%"
        Label10.Text = _s
        '----------

        '----获取ASP.NET占用的内存总数
        Label12.Text = Getaspnetn()
        '------------------

        '获取可用内存数量
        Dim myMemory As New PerformanceCounter
        myMemory.CategoryName = "Memory"
        myMemory.CounterName = "Available MBytes"
        Label7.Text = CInt(myMemory.NextValue().ToString()) & "MB"
        '---------------------

        '--获取总内存数量
        Dim seacher As ManagementObjectSearcher = New ManagementObjectSearcher("Select * From Win32_LogicalMemoryConfiguration")
        For Each obj As ManagementBaseObject In seacher.Get
            Label6.Text = CStr(CInt(obj("TotalPhysicalMemory").ToString() / 1024)) & "MB"
            Label14.Text = obj("TotalVirtualMemory").ToString
        Next


        Label16.Text = Environment.MachineName   '获取计算机名
        Label18.Text = Environment.OSVersion.ToString    '获取操作系统
        Label20.Text = Environment.SystemDirectory    '获取系统目录
        Label22.Text = CStr(CInt((Environment.TickCount / 1000) / 60)) & "分钟"
        Label24.Text = Environment.UserName

        '--获取CPU信息

        Dim CPU_searcher As New ManagementObjectSearcher("select * from Win32_Processor") '
        For Each obj As ManagementBaseObject In CPU_searcher.Get
            Label9.Text = Trim(obj("Name").ToString())
        Next
        '------

        '获取主板信息
        Dim board_searcher As New ManagementObjectSearcher("select * from Win32_baseboard") '
        For Each obj As ManagementBaseObject In board_searcher.Get
            Label26.Text = Trim(obj("Manufacturer").ToString())
            Label28.Text = Trim(obj("Product").ToString())
        Next
        '----

        '--获取内存使用率

    End Sub

    Private Function Getaspnetn() As String
        Dim temp As String
        Try
            temp = (CType(System.Diagnostics.Process.GetCurrentProcess.WorkingSet, Double) / 1048576).ToString("N2") & "M"
        Catch ex As Exception
            temp = "未知"
        End Try
        Return Trim(temp)
    End Function
End Class

你可能感兴趣的:(我的ASP.NET程序)