This is a project that came out of a wish list from a coworker to monitor, at a glance, if something is up or down as well as have a bit of a history to analyze for any slow or dropped pings, hence the graph. The first phase (script) ping.ps1 for performing the ping tests is from an external source that my coworker found and then modified by the two of us to get what we want, the second phase of the process is GeneratePingCharts.ps1 written by me to generate the graphs from the output of the first script using RGraph which is an HTML 5 charts library.
|GeneratePingCharts.ps1 output sample||ping.ps1 output sample
I am writing about this one first since I wrote it from scratch. The script calculates the ping response time averages for month, week, & day utilizing the data collected and saved in a CSV file and outputs the graphs and averages to an HTML file. For flexibility I allow the folder containing the CSV files, where the HTML should be outputted, and the location of the RGraph library files to be passed as parameters to the script.
The script starts by outputting the CSS utilized and then gathers the list of files containing the CSV extension from the folder path passed in. For each file found a new job is started, with a maximum of 10 jobs running at a time, to process the CSV file. The script waits for all 10 jobs to finish and then removes the jobs prior to starting 10 more jobs. This process continues until all files have been handled.
The line which starts the job by running the script block and passing the parameters is:
Start-Job $ProcessCSVScriptBlock -ArgumentList $file.FullName,$outputPath,$rGraphJS
The script block then initiates the processing of the CSV file by calling the Process-CSV function. The CSV file is imported into a variable using the Import-CSV PowerShell cmdlet followed by each row being processed. The sum of all the response times and number of response times collected is tracked to calculate the averages. The response times are stored in a string with the format [a,b,c,….,z] where a-z is the response time value and will contain as many as there are stored in the CSV for the given time frame. The only exception to this is month, which stores the average response time over $mInc points; otherwise, the graph would not draw do to the large quantity of points. All this data is then stored into a custom object which is passed to the Generate-Chart function.
The Generate-Chart function stores the HTML for the graph page in a variable inserting the appropriate information stored in the $chartOut parameter in with the HTML string so that the:
- Machine name and run time are displayed at the top
- Averages are displayed in a table
- Day, week, and month graphs contain the appropriate data points.
Worth mentioning is that the Get-My-Date function is used to convert the date stored in an unsupported format (MM_DD_YYYY) to a supported format (YYYY-MM-DD) so that within the Process-CSV function the date can be compared against today’s date allowing the script to know which graphs the ping result belong too.