locked
AJAX with WMI and drawing your own graph RRS feed

  • Question

  • User-908220136 posted

    Here's a quick page that will draw an image based on a WMI counter. If you wrap it in an updatepanel and refresh it every few seconds you'll have a graph of the CPU. Please let me know if you improve on it any.

    Steve - steve.pyatt@voodoovenue.com

     

    <%@ Import Namespace = "System.Management" %>
    <%@ Import Namespace = "System.IO" %>
    <%@ Import Namespace="System.Drawing" %>
    <script runat="server">

      sub Page_Load(sender as Object, e as EventArgs)
            'On Error Resume Next
            Dim objManScope As ManagementScope
            Dim objQuery As ObjectQuery
            Dim objSearcher As ManagementObjectSearcher
            Dim colQueryCollection As ManagementObjectCollection
            Dim objInstance As Object
            Dim strWQLQuery As String
            Dim intCPU As Double
            Dim sw As StreamWriter = File.AppendText("perf_cpu.txt")
            Dim x As Int32
            Dim y As Int32
            Dim z As Int32
           
            strWQLQuery = "select * from Win32_PerfFormattedData_PerfOS_Processor where Name = '_Total'"

            objManScope = New System.Management.ManagementScope("\\.\root\cimv2")
            objQuery = New System.Management.ObjectQuery(strWQLQuery)
            objSearcher = New ManagementObjectSearcher(objManScope, objQuery)
            colQueryCollection = objSearcher.Get

            For Each objInstance In colQueryCollection
                intCPU = objInstance.Item("PercentProcessorTime")
                sw.WriteLine(intCPU)
            Next
            sw.Close()
            Dim sr As StreamReader = New StreamReader("perf_cpu.txt")
            Dim bmp As New System.Drawing.Bitmap(505, 100)
            Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
            g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            g.Clear(System.Drawing.Color.LightGray)
            Dim objPenLines As New Pen(Color.DarkGray)
            objPenLines.Width = 2.0F
            g.DrawLine(objPenLines, 15, 25, 505, 25)
            g.DrawLine(objPenLines, 0, 50, 505, 50)
            g.DrawLine(objPenLines, 15, 75, 505, 75)
            Dim drawFont As New Font("Arial", 8)
            g.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit
            g.DrawString("25%", drawFont, Brushes.Black, New Point(0, 68))
            g.DrawString("75%", drawFont, Brushes.Black, New Point(0, 18))
            Dim objPen As New Pen(Color.Black)
            objPen.Width = 2.0F
            y = 0
            Dim strTempPlots() As String
            Dim intLoop As Int32
            Dim intLooper As Int32
            Dim intLoopEnd As Int32 = 1
            strTempPlots = Split(sr.ReadToEnd, vbCrLf)
            sr.Close()
            intLoop = strTempPlots.Length - 2
            x = intLoop * 5
            If x > 500 Then x = 500
            z = CInt(strTempPlots(intLoop))
            If intLoop > 100 Then intLoopEnd = intLoop - 100
            For intLooper = intLoop To intLoopEnd Step -1
                z = CInt(strTempPlots(intLooper))
                If intLooper = intLoop Then y = z
                g.DrawLine(objPen, x, 100 - y, x - 5, 100 - z)
                y = z
                x = x - 5
               
            Next
            Response.Clear()
            Response.ContentType = "image/jpeg"
            bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Response.End()
        End Sub
    </script>

    Thursday, February 7, 2008 6:00 PM

All replies

  • User1367379393 posted

    This could be extremely handy for me ... Although when i use it , I just get a black box ?  I sorted out my permission errors but still get nothing.

     

    The perf_cpu.txt file remains empty.

     

    Any ideas ?

    Wednesday, October 8, 2008 2:01 AM
  • User-908220136 posted

    You may want to try the WMI query using WBEMTEST or another utility to ensure that it is returning data.

    Friday, January 9, 2009 12:38 PM