Generating a performance-monitoring graph

In the Reporting on performance data recipe, you saw how you could take the data logged by a PLA data collector set and create a performance report. The report in that recipe showed CPU utilization of SRV1. That output is in the form of a table and is a summary of the performance of the server.

Another way to view the performance data is in the form of a graph. PowerShell does not have direct cmdlet support for displaying rich graphs, but the .NET Framework's System.Windows.Forms.DataVisualization namespace does.

This recipe uses the data visualization's Chart object to create a chart and save it as a Portable Network Graphic (PNG) file. You then display the graphic on your workstation.

Getting ready

You run this recipe on SRV1. This recipe uses the output of the PLA data collector set similar to the one you created and started in the Creating and using PLA data collector sets recipe. Note that the input to this recipe is a CSV file produced by PLA.

How to do it...

  1. Load the assembly containing the DataVisualization classes:
    Add-Type -AssemblyName System.Windows.Forms.DataVisualization
  2. Import the CSV data from earlier, and fix the row 0 issue:
    $CSVFile     = Get-ChildItem -Path C:PerfLogsAdmin*.csv -rec
    $Counters    = Import-Csv $CSVFile
    $Counters[0] = $Counters[1]        # fix row 0 issue
  3. Create a chart object:
    $Type = 'System.Windows.Forms.DataVisualization.Charting.Chart'
    $CPUChart = New-Object -TypeName $Type
  4. Define the chart dimensions:
    $CPUChart.Width  = 1000
    $CPUChart.Height = 600
    $CPUChart.Titles.Add("SRV1 CPU Utilisation") | Out-Null
  5. Create and define the chart area:
    $Type = 'System.Windows.Forms.DataVisualization.' +
            'Charting.ChartArea'
    $ChartArea             = New-Object -TypeName $Type
    $ChartArea.Name        = "SRV1 CPU Usage"
    $ChartArea.AxisY.Title = "% CPU Usage"
    $CPUChart.ChartAreas.Add($ChartArea)
  6. Get the date/time column:
    $Name = ($counters[0] | 
              Get-Member | 
                Where-Object MemberType -EQ "NoteProperty")[0].Name
  7. Add the counter sample values to the chart:
    $CPUChart.Series.Add("CPUPerc")  | Out-Null
    $CPUChart.Series["CPUPerc"].ChartType = "Line"
    $CPUCounter = '\SRV1Processor(_Total)\% Processor Time'
    $Counters | 
      ForEach-Object {$CPUChart.Series["CPUPerc"].Points.AddXY($_.$Name,$_.$CPUCounter)|
      Out-Null
    }
  8. Ensure the output folder exists, then save the chart image as a PNG file in the folder:
    $NIHT = @{
      Path        = 'C:PerflogsReports'
      ItemType    = 'Directory'
      ErrorAction = 'SilentlyContinue' 
    }
    New-Item @NIHT  # create the folder if it does not exist
    $CPUChart.SaveImage("C:PerfLogsReportsSrv1CPU.Png", 'PNG')
  9. Use the mspaint.exe application to view the chart image:
    mspaint.exe C:PerfLogsReportsSRV1cpu.Png

How it works...

Like the Reporting on performance data recipe, the steps in this recipe produce no output (except step 9 where you view the chart). That is usual when you use many objects in the .NET Framework or when you use COM objects.

In step 1, you load the assembly containing the .NET classes you are using in this recipe. By default, this is an assembly that is not loaded by PowerShell.

With step 2, you import the PLA-created CPU data for SRV1. This file consists of a number of counter samples. You configured these details in the Creating and using PLA data collector sets recipe.

In step 3, you create a chart object, and in step 4, you define the chart's dimensions. In step 5, you create and configure a chart area object then add it to the chart.

In step 6, you get the name of the time and date column within the performance counters. Step 7 adds the data to the chart. Step 8 saves the chart as a PNG file.

Executing step 9 invokes the mspaint.exe application, which displays the PNG file, as follows:

How it works...

There's more...

This recipe showed you how to create a simple report graphing one counter, CPUutilization, across several hours of monitoring one server. You could add a second series, such as memory pages per second, to the chart. The result could be a customized graph that is similar to what you see in Performance Monitor. You could also incorporate data from more servers to the chart.

To support ongoing server monitoring, consider creating scheduled tasks to create the performance graphs and email the resultant output to those who need to know. Or have the scheduled task create a new web page on your intranet and drop the graphs into the page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.206.254