using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using System.IO;
using System.Collections;
using System.Drawing.Imaging;
namespace VMSTAT_Analyzer
{
public partial class frmVMSTATAnalyzer : Form
{
#region Global Variables
int intGlobalTotalRowsInVMSTATFile = 1;
int intGlobalIntervalForDataPoint = 1;
float floatGlobalPercentileValue = 90;
int intGlobalGraphFieldDescriptorValue = 0;
string strGlobalVMSTATFile = null;
string strGlobalGraphTitle = "Procs";
string strGlobalGraphXAxis = "Elapsed Time";
string strGlobalGraphYAxis = "processes";
string strGlobalHint1 = "r";
string strGlobalHint2 = "b";
string strGlobalHint3 = null;
string strGlobalHint4 = null;
string strGlobalHint5 = null;
string[] stringArrayGlobalDataPointForX;
#endregion
public frmVMSTATAnalyzer()
{
InitializeComponent();
#region After_Initialize_Component
GraphPane myPane = zedGraphControl.GraphPane;
myPane.XAxis.Type = AxisType.Text;
myPane.XAxis.Scale.FontSpec.Angle = 40;
myPane.XAxis.Scale.FontSpec.Size = 8;
myPane.YAxis.Scale.FontSpec.Size = 8;
myPane.YAxis.Scale.IsUseTenPower = false;
toolStripSetIntervalForDataPoint.Text = "Set &Interval for Data Point. Currently : " + intGlobalIntervalForDataPoint.ToString() + " (sec)";
ToolStripMenuItemSetPercentileValue.Text = "Set &Percentile Value. Currently : " + floatGlobalPercentileValue.ToString() + " %";
#endregion
}
#region Form_Load And Form_Resize Event
private void frmVMSTATAnalyzer_Load(object sender, EventArgs e)
{
toolStripOptions.Enabled = false;
SetSizeForZedGraphControl();
ClearValues(zedGraphControl);
frmHelp objFrmHelp = new frmHelp();
objFrmHelp.ShowDialog();
}
private void frmVMSTATAnalyzer_Resize(object sender, EventArgs e)
{
SetSizeForZedGraphControl();
}
#endregion
#region Menu Data.Load VMSTAT File...
private void toolStripLoadVmstatFile_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open VMTSAT File...";
fDialog.Filter = "Log Files|*.log|Txt Files|*.txt|All Files|*.*";
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
if (fDialog.ShowDialog() == DialogResult.OK)
{
toolStripOptions.Enabled = true;
strGlobalVMSTATFile = fDialog.FileName.ToString();
CheckIntegrity();
stringArrayGlobalDataPointForX = CreatePointForX();
CreateGraph(zedGraphControl);
this.Text = "VMSTAT Analyzer [" + fDialog.FileName.ToString() + "]";
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "VMSTAT Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region Menu Data.Exit
private void toolStripExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
#endregion
#region Menu Options.Graph Field Descriptor
private void toolStripProcs_Click(object sender, EventArgs e)
{
UncheckAllToolStrip(sender);
toolStripProcs.Checked = true;
ClearValues(zedGraphControl);
strGlobalGraphTitle = "Procs";
strGlobalGraphXAxis = "Elapsed Time";
strGlobalGraphYAxis = "processes";
strGlobalHint1 = "r";
strGlobalHint2 = "b";
strGlobalHint3 = null;
strGlobalHint4 = null;
strGlobalHint5 = null;
intGlobalGraphFieldDescriptorValue = 0;
CreateGraph(zedGraphControl);
}
private void toolStripMemory_Click(object sender, EventArgs e)
{
UncheckAllToolStrip(sender);
toolStripMemory.Checked = true;
ClearValues(zedGraphControl);
strGlobalGraphTitle = "Memory";
strGlobalGraphXAxis = "Elapsed Time";
strGlobalGraphYAxis = "memory";
strGlobalHint1 = "swpd";
strGlobalHint2 = "free";
strGlobalHint3 = "buff";
strGlobalHint4 = "cache";
strGlobalHint5 = null;
intGlobalGraphFieldDescriptorValue = 1;
CreateGraph(zedGraphControl);
}
private void toolStripSwap_Click(object sender, EventArgs e)
{
UncheckAllToolStrip(sender);
toolStripSwap.Checked = true;
ClearValues(zedGraphControl);
strGlobalGraphTitle = "Swap";
strGlobalGraphXAxis = "Elapsed Time";
strGlobalGraphYAxis = "memory swapped in from or to disk.";
strGlobalHint1 = "si";
strGlobalHint2 = "so";
strGlobalHint3 = null;
strGlobalHint4 = null;
strGlobalHint5 = null;
intGlobalGraphFieldDescriptorValue = 2;
CreateGraph(zedGraphControl);
}
private void toolStripIO_Click(object sender, EventArgs e)
{
UncheckAllToolStrip(sender);
toolStripIO.Checked = true;
ClearValues(zedGraphControl);
strGlobalGraphTitle = "IO";
strGlobalGraphXAxis = "Elapsed Time";
strGlobalGraphYAxis = "Blocks received from or sent to device.";
strGlobalHint1 = "bi";
strGlobalHint2 = "bo";
strGlobalHint3 = null;
strGlobalHint4 = null;
strGlobalHint5 = null;
intGlobalGraphFieldDescriptorValue = 3;
CreateGraph(zedGraphControl);
}
private void toolStripSystem_Click(object sender, EventArgs e)
{
UncheckAllToolStrip(sender);
toolStripSystem.Checked = true;
ClearValues(zedGraphControl);
strGlobalGraphTitle = "System";
strGlobalGraphXAxis = "Elapsed Time";
strGlobalGraphYAxis = "interrupts and context switches";
strGlobalHint1 = "in";
strGlobalHint2 = "cs";
strGlobalHint3 = null;
strGlobalHint4 = null;
strGlobalHint5 = null;
intGlobalGraphFieldDescriptorValue = 4;
CreateGraph(zedGraphControl);
}
private void toolStripCPU_Click(object sender, EventArgs e)
{
UncheckAllToolStrip(sender);
toolStripCPU.Checked = true;
ClearValues(zedGraphControl);
strGlobalGraphTitle = "CPU";
strGlobalGraphXAxis = "Elapsed Time";
strGlobalGraphYAxis = "CPU Usage (%)";
strGlobalHint1 = "us";
strGlobalHint2 = "sy";
strGlobalHint3 = "id";
strGlobalHint4 = "wa";
strGlobalHint5 = "cu";
intGlobalGraphFieldDescriptorValue = 5;
CreateGraph(zedGraphControl);
}
#endregion
#region Menu Options.Configuration
private void toolStripSetTitle_Click(object sender, EventArgs e)
{
frmPopUp objFrmPopUp = new frmPopUp("Set Title for the Graph");
if (objFrmPopUp.ShowDialog() == DialogResult.OK)
{
if (objFrmPopUp.PopUpText.CompareTo("") != 0)
{
strGlobalGraphTitle = objFrmPopUp.PopUpText;
ChangeTitle(zedGraphControl, objFrmPopUp.PopUpText);
}
else
{
MessageBox.Show("Please enter some value for the Title of the graph.", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void toolStripSetYAxisName_Click(object sender, EventArgs e)
{
frmPopUp objFrmPopUp = new frmPopUp("Set Y Axis Name in the Graph");
if (objFrmPopUp.ShowDialog() == DialogResult.OK)
{
if (objFrmPopUp.PopUpText.CompareTo("") != 0)
{
strGlobalGraphYAxis = objFrmPopUp.PopUpText;
ChangeYAxisName(zedGraphControl, objFrmPopUp.PopUpText);
}
else
{
MessageBox.Show("Please enter some value for the Y Axis in the graph.", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void toolStripSetXAxisName_Click(object sender, EventArgs e)
{
frmPopUp objFrmPopUp = new frmPopUp("Set X Axis Name in the Graph");
if (objFrmPopUp.ShowDialog() == DialogResult.OK)
{
if (objFrmPopUp.PopUpText.CompareTo("") != 0)
{
strGlobalGraphXAxis = objFrmPopUp.PopUpText;
ChangeXAxisName(zedGraphControl, objFrmPopUp.PopUpText);
}
else
{
MessageBox.Show("Please enter some value for the X Axis in the graph.", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void toolStripSetIntervalForDataPoint_Click(object sender, EventArgs e)
{
try
{
frmPopUp objFrmPopUp = new frmPopUp("Set Interval for Data Point. Currently : " + intGlobalIntervalForDataPoint + " (sec)");
if (objFrmPopUp.ShowDialog() == DialogResult.OK)
{
if (objFrmPopUp.PopUpText.CompareTo("") != 0)
{
intGlobalIntervalForDataPoint = int.Parse(objFrmPopUp.PopUpText);
toolStripSetIntervalForDataPoint.Text = "Set &Interval for Data Point. Currently : " + intGlobalIntervalForDataPoint.ToString() + " (sec)";
stringArrayGlobalDataPointForX = CreatePointForX();
CreateGraph(zedGraphControl);
}
else
{
MessageBox.Show("Please enter some value (sec) for the Data Point.", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (FormatException)
{
MessageBox.Show("Please enter some integer value (sec) for the Data Point.", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception)
{
MessageBox.Show("Please enter some integer value (sec) for the Data Point.", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ToolStripMenuItemSetPercentileValue_Click(object sender, EventArgs e)
{
try
{
frmPopUp objFrmPopUp = new frmPopUp("Set Percentile Value. Currently : " + floatGlobalPercentileValue + " %");
if (objFrmPopUp.ShowDialog() == DialogResult.OK)
{
if (objFrmPopUp.PopUpText.CompareTo("") != 0)
{
if (((int.Parse(objFrmPopUp.PopUpText)) >= 0) && ((int.Parse(objFrmPopUp.PopUpText)) <= 100))
{
floatGlobalPercentileValue = float.Parse(objFrmPopUp.PopUpText);
ToolStripMenuItemSetPercentileValue.Text = "Set &Percentile Value. Currently : " + floatGlobalPercentileValue.ToString() + " %";
CreateGraph(zedGraphControl);
}
else
{
MessageBox.Show("Please enter some percentile value 0-100 %", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Please enter some percentile value 0-100 %", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (FormatException)
{
MessageBox.Show("Please enter some integer value (sec) for the Data Point.", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception)
{
MessageBox.Show("Please enter some integer value (sec) for the Data Point.", "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region Menu Options.Clear Content
private void toolStripClearContent_Click(object sender, EventArgs e)
{
ClearValues(zedGraphControl);
GraphPane myPane = zedGraphControl.GraphPane;
// Set the title and axis labels
myPane.Title.Text = "Title";
myPane.XAxis.Title.Text = "X Axis";
myPane.YAxis.Title.Text = "y Axis";
zedGraphControl.AxisChange();
RepaintScreen();
}
#endregion
#region Menu Options.Line Color
private void toolStripLine1_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
toolStripLine1.ForeColor = colorDialog.Color;
CreateGraph(zedGraphControl);
}
}
private void toolStripLine2_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
toolStripLine2.ForeColor = colorDialog.Color;
CreateGraph(zedGraphControl);
}
}
private void toolStripLine3_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
toolStripLine3.ForeColor = colorDialog.Color;
CreateGraph(zedGraphControl);
}
}
private void toolStripLine4_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
toolStripLine4.ForeColor = colorDialog.Color;
CreateGraph(zedGraphControl);
}
}
private void toolStripLine5_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
toolStripLine5.ForeColor = colorDialog.Color;
CreateGraph(zedGraphControl);
}
}
#endregion
#region ContextMenu Export_To_CSV
private void toolStripMenuItemExportToCSV_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog fDialog = new SaveFileDialog();
fDialog.Title = "Save Statistic Values...";
fDialog.Filter = "CSV File|*.CSV";
fDialog.CheckPathExists = true;
if (fDialog.ShowDialog() == DialogResult.OK)
{
Export_To_CSV(fDialog.FileName);
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "VMSTAT Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region Menu Help
private void toolStripHelp_Click(object sender, EventArgs e)
{
frmHelp objFrmHelp = new frmHelp();
objFrmHelp.ShowDialog();
}
#endregion
#region CheckBox Click_For_Filtered_Graph_Content_Display
private void checkBoxViewGraph1_Click(object sender, EventArgs e)
{
ClearGraph(zedGraphControl);
FilteredCreateGraph(zedGraphControl);
}
private void checkBoxViewGraph2_Click(object sender, EventArgs e)
{
ClearGraph(zedGraphControl);
FilteredCreateGraph(zedGraphControl);
}
private void checkBoxViewGraph3_Click(object sender, EventArgs e)
{
ClearGraph(zedGraphControl);
FilteredCreateGraph(zedGraphControl);
}
private void checkBoxViewGraph4_Click(object sender, EventArgs e)
{
ClearGraph(zedGraphControl);
FilteredCreateGraph(zedGraphControl);
}
private void checkBoxViewGraph5_Click(object sender, EventArgs e)
{
ClearGraph(zedGraphControl);
FilteredCreateGraph(zedGraphControl);
}
#endregion
#region Methods Change_Graph_Title_Y_X_Axis_Name
private void ChangeTitle(ZedGraphControl zgc, string strTitle)
{
GraphPane myPane = zgc.GraphPane;
myPane.Title.Text = strTitle;
zgc.AxisChange();
RepaintScreen();
}
private void ChangeYAxisName(ZedGraphControl zgc, string strTitle)
{
GraphPane myPane = zgc.GraphPane;
myPane.YAxis.Title.Text = strTitle;
zgc.AxisChange();
RepaintScreen();
}
private void ChangeXAxisName(ZedGraphControl zgc, string strTitle)
{
GraphPane myPane = zgc.GraphPane;
myPane.XAxis.Title.Text = strTitle;
zgc.AxisChange();
RepaintScreen();
}
#endregion
#region Method Repaint_Screen
private void RepaintScreen()
{
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
#endregion
#region Method Uncheck_All_Menu_For_Graph Field Descriptor
private void UncheckAllToolStrip(object sender)
{
if (sender is ToolStripMenuItem)
{
foreach (ToolStripMenuItem item in (((ToolStripMenuItem)sender).GetCurrentParent().Items))
{
if (item == sender) item.Checked = true;
if ((item != null) && (item != sender))
{
item.Checked = false;
}
}
}
}
#endregion
#region Method Check_Validity_And_Integrity_Of_VMSTAT_File
private void CheckIntegrity()
{
int intNumberOfDataPoint = 0;
intGlobalTotalRowsInVMSTATFile = 0;
string strVMSTATLineForTotalRows = null;
try
{
StreamReader readerTotalRows = File.OpenText(strGlobalVMSTATFile);
while ((strVMSTATLineForTotalRows = readerTotalRows.ReadLine()) != null)
{
intGlobalTotalRowsInVMSTATFile = intGlobalTotalRowsInVMSTATFile + 1;
}
readerTotalRows.Close();
stringArrayGlobalDataPointForX = new string[intGlobalTotalRowsInVMSTATFile - 2];
StreamReader reader = File.OpenText(strGlobalVMSTATFile);
//First line of VMSTAT report
string[] strFirstLineVMSTATArr = { "procs", "-----------memory----------", "---swap--", "-----io----", "--system--", "----cpu----" };
//Second line of VMSTAT report
string[] strSecondLineVMSTATArr = { "r", "b", "swpd", "free", "buff", "cache", "si", "so", "bi", "bo", "in", "cs", "us", "sy", "id", "wa" };
//int intNumberOfDataPoint = 0;
string strVMSTATEachLine = null;
string[] strVMSTATEachLineArray;
bool boolLineFlag = false;
ArrayList arrayListVMSTATLine = new ArrayList();
while ((strVMSTATEachLine = reader.ReadLine()) != null)
{
strVMSTATEachLineArray = strVMSTATEachLine.Split(' ');
//Check for the first line if it is valid
if (intNumberOfDataPoint == 0)
{
foreach (string strFirstLineVMSTAT in strFirstLineVMSTATArr)
{
boolLineFlag = false;
foreach (string strParseLine in strVMSTATEachLineArray)
{
if (strParseLine.CompareTo(strFirstLineVMSTAT) == 0)
{
boolLineFlag = true;
break;
}
}
if (boolLineFlag == false)
{
if (MessageBox.Show("\"" + strFirstLineVMSTAT + "\" is not avalibale. The file may be invalid or corrupt.\nThere may be error in parsing the file. Do you want to continue ?", "VMSTAT Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Application.Exit();
throw new Exception("User has terminated the process.");
}
}
}
}
//Check for the second line if it is valid
if (intNumberOfDataPoint == 1)
{
foreach (string strSecondLineVMSTAT in strSecondLineVMSTATArr)
{
boolLineFlag = false;
foreach (string strParseLine in strVMSTATEachLineArray)
{
if (strParseLine.CompareTo(strSecondLineVMSTAT) == 0)
{
boolLineFlag = true;
break;
}
}
if (boolLineFlag == false)
{
if (MessageBox.Show("\"" + strSecondLineVMSTAT + "\" is not avalibale. The file may be invalid or corrupt.\nThere may be error in parsing the file. Do you want to continue ?", "VMSTAT Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Application.Exit();
throw new Exception("User has terminated the process.");
}
}
}
}
//Check valididty for rest of the lines
if (intNumberOfDataPoint >= 2)
{
boolLineFlag = false;
arrayListVMSTATLine.Clear();
foreach (string strParseLine in strVMSTATEachLineArray)
{
if (strParseLine.CompareTo("") != 0)
{
int intParseLine = int.Parse(strParseLine);
arrayListVMSTATLine.Add(intParseLine);
}
}
object[] objStringArr = arrayListVMSTATLine.ToArray();
if (objStringArr.Length == 16)
{
boolLineFlag = true;
}
if (boolLineFlag == false)
{
if (MessageBox.Show("Line number : " + intNumberOfDataPoint + " is not valid. The file may be invalid or corrupt or some value is missing in mentioned line number.\nThere may be error in parsing the file. Do you want to continue ?", "VMSTAT Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Application.Exit();
throw new Exception("User has terminated the process.");
}
}
}
intNumberOfDataPoint = intNumberOfDataPoint + 1;
}
reader.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message + "\n\nLine number : " + intNumberOfDataPoint + " is not valid. The file may be invalid or corrupt or some value is missing in mentioned line number.", "VMSTAT Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region Method Set_Size_For_Graph
private void SetSizeForZedGraphControl()
{
zedGraphControl.Location = new Point(12, 38);
zedGraphControl.Size = new Size(this.ClientRectangle.Width - 22, this.ClientRectangle.Height - 178);
}
#endregion
#region Method Clear_All_Values
private void ClearValues(ZedGraphControl zgc)
{
zgc.GraphPane.CurveList.Clear();
lblSubDesc1.Text = "";
lblSubDesc2.Text = "";
lblSubDesc3.Text = "";
lblSubDesc4.Text = "";
lblSubDesc5.Text = "";
txtMinimum1.Text = "";
txtMinimum2.Text = "";
txtMinimum3.Text = "";
txtMinimum4.Text = "";
txtMinimum5.Text = "";
txtAverage1.Text = "";
txtAverage2.Text = "";
txtAverage3.Text = "";
txtAverage4.Text = "";
txtAverage5.Text = "";
txtMaximum1.Text = "";
txtMaximum2.Text = "";
txtMaximum3.Text = "";
txtMaximum4.Text = "";
txtMaximum5.Text = "";
txtPercentile1.Text = "";
txtPercentile2.Text = "";
txtPercentile3.Text = "";
txtPercentile4.Text = "";
txtPercentile5.Text = "";
txtStdDeviation1.Text = "";
txtStdDeviation2.Text = "";
txtStdDeviation3.Text = "";
txtStdDeviation4.Text = "";
txtStdDeviation5.Text = "";
checkBoxViewGraph1.Enabled = false;
checkBoxViewGraph2.Enabled = false;
checkBoxViewGraph3.Enabled = false;
checkBoxViewGraph4.Enabled = false;
checkBoxViewGraph5.Enabled = false;
checkBoxViewGraph1.Checked = false;
checkBoxViewGraph2.Checked = false;
checkBoxViewGraph3.Checked = false;
checkBoxViewGraph4.Checked = false;
checkBoxViewGraph5.Checked = false;
}
#endregion
#region Method Clear_Only_Graph_Content
private void ClearGraph(ZedGraphControl zgc)
{
zgc.GraphPane.CurveList.Clear();
}
#endregion
#region Method Create_Graph_With_Its_Value
private void CreateGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
// Set the title and axis labels
myPane.Title.Text = strGlobalGraphTitle;
myPane.XAxis.Title.Text = strGlobalGraphXAxis;
myPane.YAxis.Title.Text = strGlobalGraphYAxis;
//Graph Field Descriptor is Procs
if (intGlobalGraphFieldDescriptorValue == 0)
{
ClearValues(zedGraphControl);
lblSubDesc1.Text = "r: The number of processes waiting for run time.";
lblSubDesc2.Text = "b: The number of processes in uninterruptible sleep.";
LineItem myCurve1 = myPane.AddCurve("r", null, CreatePointForY(0), toolStripLine1.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
LineItem myCurve2 = myPane.AddCurve("b", null, CreatePointForY(1), toolStripLine2.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
}
//Graph Field Descriptor is Memory
if (intGlobalGraphFieldDescriptorValue == 1)
{
ClearValues(zedGraphControl);
lblSubDesc1.Text = "swpd: the amount of virtual memory used.";
lblSubDesc2.Text = "free: the amount of idle memory.";
lblSubDesc3.Text = "buff: the amount of memory used as buffers.";
lblSubDesc4.Text = "cache: the amount of memory used as cache.";
//pointPairListGlobalList1 = CreatePointPair(2);
// Generate curve
LineItem myCurve1 = myPane.AddCurve("swpd", null, CreatePointForY(2), toolStripLine1.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList2 = CreatePointPair(3);
// Generate curve
LineItem myCurve2 = myPane.AddCurve("free", null, CreatePointForY(3), toolStripLine2.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList3 = CreatePointPair(4);
// Generate curve
LineItem myCurve3 = myPane.AddCurve("buff", null, CreatePointForY(4), toolStripLine3.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList4 = CreatePointPair(5);
// Generate curve
LineItem myCurve4 = myPane.AddCurve("cache", null, CreatePointForY(5), toolStripLine4.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
}
//Graph Field Descriptor is Swap
if (intGlobalGraphFieldDescriptorValue == 2)
{
ClearValues(zedGraphControl);
lblSubDesc1.Text = "si: Amount of memory swapped in from disk (/s).";
lblSubDesc2.Text = "so: Amount of memory swapped to disk (/s).";
//pointPairListGlobalList1 = CreatePointPair(6);
// Generate curve
LineItem myCurve1 = myPane.AddCurve("si", null, CreatePointForY(6), toolStripLine1.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList2 = CreatePointPair(7);
// Generate curve
LineItem myCurve2 = myPane.AddCurve("so", null, CreatePointForY(7), toolStripLine2.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
}
//Graph Field Descriptor is IO
if (intGlobalGraphFieldDescriptorValue == 3)
{
ClearValues(zedGraphControl);
lblSubDesc1.Text = "bi: Blocks received from a block device (blocks/s).";
lblSubDesc2.Text = "bo: Blocks sent to a block device (blocks/s).";
//pointPairListGlobalList1 = CreatePointPair(8);
// Generate curve
LineItem myCurve1 = myPane.AddCurve("bi", null, CreatePointForY(8), toolStripLine1.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList2 = CreatePointPair(9);
// Generate curve
LineItem myCurve2 = myPane.AddCurve("bo", null, CreatePointForY(9), toolStripLine2.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
}
//Graph Field Descriptor is System
if (intGlobalGraphFieldDescriptorValue == 4)
{
ClearValues(zedGraphControl);
lblSubDesc1.Text = "in: The number of interrupts per second, including the clock.";
lblSubDesc2.Text = "cs: The number of context switches per second.";
//pointPairListGlobalList1 = CreatePointPair(10);
// Generate curve
LineItem myCurve1 = myPane.AddCurve("in", null, CreatePointForY(10), toolStripLine1.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList2 = CreatePointPair(11);
// Generate curve
LineItem myCurve2 = myPane.AddCurve("cs", null, CreatePointForY(11), toolStripLine2.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
}
//Graph Field Descriptor is CPU
if (intGlobalGraphFieldDescriptorValue == 5)
{
ClearValues(zedGraphControl);
lblSubDesc1.Text = "us: Time spent running non-kernel code. (user time, including nice time)";
lblSubDesc2.Text = "sy: Time spent running kernel code. (system time)";
lblSubDesc3.Text = "id: Time spent idle.";
lblSubDesc4.Text = "wa: Time spent waiting for IO.";
lblSubDesc5.Text = "cu: Total CPU Usage.";
//pointPairListGlobalList1 = CreatePointPair(12);
// Generate curve
LineItem myCurve1 = myPane.AddCurve("us", null, CreatePointForY(12), toolStripLine1.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList2 = CreatePointPair(13);
// Generate curve
LineItem myCurve2 = myPane.AddCurve("sy", null, CreatePointForY(13), toolStripLine2.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList3 = CreatePointPair(14);
// Generate curve
LineItem myCurve3 = myPane.AddCurve("id", null, CreatePointForY(14), toolStripLine3.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList4 = CreatePointPair(15);
// Generate curve
LineItem myCurve4 = myPane.AddCurve("wa", null, CreatePointForY(15), toolStripLine4.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
//pointPairListGlobalList5 = CreatePointPair(16);
// Generate curve
LineItem myCurve5 = myPane.AddCurve("cu", null, CreatePointForY(16), toolStripLine5.ForeColor, SymbolType.None);
myPane.XAxis.Scale.TextLabels = stringArrayGlobalDataPointForX;
}
// Calculate the Axis Scale Ranges
zgc.AxisChange();
RepaintScreen();
}
#endregion
#region Method Create_Points_For_X_Axis
private string[] CreatePointForX()
{
string[] strXValue = new string[intGlobalTotalRowsInVMSTATFile - 2];
/*if ((intGlobalTotalRowsInVMSTATFile - 2) < 60)
{
for (int i = 0; i < (intGlobalTotalRowsInVMSTATFile - 2); i++)
{
strXValue[i] = ((i+1)*intGlobalIntervalForDataPoint).ToString() + " sec";
}
return strXValue;
}
if (((intGlobalTotalRowsInVMSTATFile - 2) >= 60) && ((intGlobalTotalRowsInVMSTATFile - 2) < 3600))
{
int intTempSec = 0;
int intTempMin = 0;
for (int i = 0; i < (intGlobalTotalRowsInVMSTATFile - 2); i++)
{
intTempSec = intTempSec + 1;
strXValue[i] = intTempMin.ToString() + " min " + (intTempSec * intGlobalIntervalForDataPoint).ToString() + " sec";
if ((intTempSec * intGlobalIntervalForDataPoint) >= 60)
{
intTempSec = 0;
intTempMin = intTempMin + 1;
}
}
return strXValue;
}
if ((intGlobalTotalRowsInVMSTATFile - 2) >= 3600)
{
int intTempSec = 0;
int intTempMin = 0;
int intTempHr = 0;
for (int i = 0; i < (intGlobalTotalRowsInVMSTATFile - 2); i++)
{
intTempSec = intTempSec + 1;
strXValue[i] = intTempHr.ToString() + " hr " + intTempMin.ToString() + " min " + (intTempSec * intGlobalIntervalForDataPoint).ToString() + " sec";
if ((intTempSec * intGlobalIntervalForDataPoint) >= 60)
{
intTempSec = 0;
intTempMin = intTempMin + 1;
}
if (intTempMin >= 60)
{
intTempSec = 0;
intTempMin = 0;
intTempHr = intTempHr + 1;
}
}
return strXValue;
}*/
int intTempSec = 0;
int intTempMin = 0;
int intTempHr = 0;
int intTemp = 1;
for (int i = 0; i < (intGlobalTotalRowsInVMSTATFile - 2); i++)
{
intTempSec = intTemp * intGlobalIntervalForDataPoint;
strXValue[i] = intTempHr.ToString("d2") + ":" + intTempMin.ToString("d2") + ":" + intTempSec.ToString("d2");
if (intTempSec >= 59)
{
intTemp = 0;
intTempMin = intTempMin + 1;
}
if (intTempMin >= 60)
{
intTemp = 0;
intTempMin = 0;
intTempHr = intTempHr + 1;
}
if (intTempSec != 59)
{
intTemp = intTemp + 1;
}
}
return strXValue;
}
#endregion
#region Method Create_Points_For_Y_Axis
private double[] CreatePointForY(int intGraphField)
{
//PointPairList PointPairListForXAndYAxis = new PointPairList();
int intNumberOfDataPoint = 0;
string strVMSTATEachLine = null;
string[] strVMSTATEachLineArray;
ArrayList arrayListVMSTATLine = new ArrayList();
int intYValue = 0;
int intXValue = 0;
int intMin = 2147483647;
int intMax = 0;
decimal decimalAvg = 0;
double doublePercentileValue = 0;
double doublePopStdDeviation = 0;
double[] doubleYValue = new double[intGlobalTotalRowsInVMSTATFile - 2];
try
{
StreamReader reader = File.OpenText(strGlobalVMSTATFile);
while ((strVMSTATEachLine = reader.ReadLine()) != null)
{
if (intNumberOfDataPoint >= 2)
{
strVMSTATEachLineArray = strVMSTATEachLine.Split(' ');
arrayListVMSTATLine.Clear();
foreach (string strParseLineVMSTAT in strVMSTATEachLineArray)
{
if (strParseLineVMSTAT.CompareTo("") != 0)
{
int intParseLineVMSTAT = int.Parse(strParseLineVMSTAT);
arrayListVMSTATLine.Add(intParseLineVMSTAT);
}
}
object[] objStringArrayVMSTAT = arrayListVMSTATLine.ToArray();
//When CPU Usage is not requested or else calculate by 100 - CPU Idle Time
if (intGraphField != 16)
{
intYValue = (int)objStringArrayVMSTAT[intGraphField];
intXValue = (intNumberOfDataPoint * intGlobalIntervalForDataPoint) - 2;//GetXValue(intNumberOfDataPoint);
}
else
{
intYValue = 100 - (int)objStringArrayVMSTAT[14];
intXValue = (intNumberOfDataPoint * intGlobalIntervalForDataPoint) - 2;
}
//Get Minimum value
if (intMin > intYValue)
{
intMin = intYValue;
}
//Get Maximum value
if (intYValue > intMax)
{
intMax = intYValue;
}
//Get Total value
decimalAvg = decimalAvg + intYValue;
//PointPairListForXAndYAxis.Add(intXValue, intYValue);
doubleYValue[intNumberOfDataPoint - 2] = intYValue;
}
intNumberOfDataPoint = intNumberOfDataPoint + 1;
}
reader.Close();
//Get Average value
decimalAvg = decimalAvg / (intNumberOfDataPoint - 3);
doublePercentileValue = GetPercentileValue(doubleYValue);
doublePopStdDeviation = GetPopulationStdDeviationValue(doubleYValue);
DisplayMinAvgMaxPercStdDValue(intMin, decimalAvg, intMax, doublePercentileValue, doublePopStdDeviation, intGraphField);
return doubleYValue;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "VMSTAT Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return doubleYValue;
}
}
#endregion
#region Method Display_Min_Avg_Max_Percentile_StdDeviation
private void DisplayMinAvgMaxPercStdDValue(int intMin, decimal decimalAvg, int intMax, double doublePercentileValue, double doublePopStdDeviation, int intGraphField)
{
if ((intGraphField == 0) || (intGraphField == 2) || (intGraphField == 6) || (intGraphField == 8) || (intGraphField == 10) || (intGraphField == 12))
{
txtMinimum1.Text = intMin.ToString();
txtAverage1.Text = decimalAvg.ToString();
txtMaximum1.Text = intMax.ToString();
txtPercentile1.Text = doublePercentileValue.ToString();
txtStdDeviation1.Text = doublePopStdDeviation.ToString();
checkBoxViewGraph1.Enabled = true;
checkBoxViewGraph1.Text = strGlobalHint1;
checkBoxViewGraph1.Checked = true;
}
if ((intGraphField == 1) || (intGraphField == 3) || (intGraphField == 7) || (intGraphField == 9) || (intGraphField == 11) || (intGraphField == 13))
{
txtMinimum2.Text = intMin.ToString();
txtAverage2.Text = decimalAvg.ToString();
txtMaximum2.Text = intMax.ToString();
txtPercentile2.Text = doublePercentileValue.ToString();
txtStdDeviation2.Text = doublePopStdDeviation.ToString();
checkBoxViewGraph2.Enabled = true;
checkBoxViewGraph2.Text = strGlobalHint2;
checkBoxViewGraph2.Checked = true;
}
if ((intGraphField == 4)|| (intGraphField == 14))
{
txtMinimum3.Text = intMin.ToString();
txtAverage3.Text = decimalAvg.ToString();
txtMaximum3.Text = intMax.ToString();
txtPercentile3.Text = doublePercentileValue.ToString();
txtStdDeviation3.Text = doublePopStdDeviation.ToString();
checkBoxViewGraph3.Enabled = true;
checkBoxViewGraph3.Text = strGlobalHint3;
checkBoxViewGraph3.Checked = true;
}
if ((intGraphField == 5)|| (intGraphField == 15))
{
txtMinimum4.Text = intMin.ToString();
txtAverage4.Text = decimalAvg.ToString();
txtMaximum4.Text = intMax.ToString();
txtPercentile4.Text = doublePercentileValue.ToString();
txtStdDeviation4.Text = doublePopStdDeviation.ToString();
checkBoxViewGraph4.Enabled = true;
checkBoxViewGraph4.Text = strGlobalHint4;
checkBoxViewGraph4.Checked = true;
}
if (intGraphField == 16)
{
txtMinimum5.Text = intMin.ToString();
txtAverage5.Text = decimalAvg.ToString();
txtMaximum5.Text = intMax.ToString();
txtPercentile5.Text = doublePercentileValue.ToString();
txtStdDeviation5.Text = doublePopStdDeviation.ToString();
checkBoxViewGraph5.Enabled = true;
checkBoxViewGraph5.Text = strGlobalHint5;
checkBoxViewGraph5.Checked = true;
}
}
#endregion
#region Method Display_Filtered_Graph_Content
private void FilteredCreateGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
//Graph Field Descriptor is Procs
if (intGlobalGraphFieldDescriptorValue == 0)
{
if (checkBoxViewGraph1.Checked == true)
{
LineItem myCurve1 = myPane.AddCurve("r", null, CreatePointForY(0), toolStripLine1.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph2.Checked == true)
{
LineItem myCurve2 = myPane.AddCurve("b", null, CreatePointForY(1), toolStripLine2.ForeColor, SymbolType.None);
}
}
//Graph Field Descriptor is Memory
if (intGlobalGraphFieldDescriptorValue == 1)
{
if (checkBoxViewGraph1.Checked == true)
{
LineItem myCurve1 = myPane.AddCurve("swpd", null, CreatePointForY(2), toolStripLine1.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph2.Checked == true)
{
LineItem myCurve2 = myPane.AddCurve("free", null, CreatePointForY(3), toolStripLine2.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph3.Checked == true)
{
LineItem myCurve3 = myPane.AddCurve("buff", null, CreatePointForY(4), toolStripLine3.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph4.Checked == true)
{
LineItem myCurve4 = myPane.AddCurve("cache", null, CreatePointForY(5), toolStripLine4.ForeColor, SymbolType.None);
}
}
//Graph Field Descriptor is Swap
if (intGlobalGraphFieldDescriptorValue == 2)
{
if (checkBoxViewGraph1.Checked == true)
{
LineItem myCurve1 = myPane.AddCurve("si", null, CreatePointForY(6), toolStripLine1.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph2.Checked == true)
{
LineItem myCurve2 = myPane.AddCurve("so", null, CreatePointForY(7), toolStripLine2.ForeColor, SymbolType.None);
}
}
//Graph Field Descriptor is IO
if (intGlobalGraphFieldDescriptorValue == 3)
{
if (checkBoxViewGraph1.Checked == true)
{
LineItem myCurve1 = myPane.AddCurve("bi", null, CreatePointForY(8), toolStripLine1.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph2.Checked == true)
{
LineItem myCurve2 = myPane.AddCurve("bo", null, CreatePointForY(9), toolStripLine2.ForeColor, SymbolType.None);
}
}
//Graph Field Descriptor is System
if (intGlobalGraphFieldDescriptorValue == 4)
{
if (checkBoxViewGraph1.Checked == true)
{
LineItem myCurve1 = myPane.AddCurve("in", null, CreatePointForY(10), toolStripLine1.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph2.Checked == true)
{
LineItem myCurve2 = myPane.AddCurve("cs", null, CreatePointForY(11), toolStripLine2.ForeColor, SymbolType.None);
}
}
//Graph Field Descriptor is CPU
if (intGlobalGraphFieldDescriptorValue == 5)
{
if (checkBoxViewGraph1.Checked == true)
{
LineItem myCurve1 = myPane.AddCurve("us", null, CreatePointForY(12), toolStripLine1.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph2.Checked == true)
{
LineItem myCurve2 = myPane.AddCurve("sy", null, CreatePointForY(13), toolStripLine2.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph3.Checked == true)
{
LineItem myCurve3 = myPane.AddCurve("id", null, CreatePointForY(14), toolStripLine3.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph4.Checked == true)
{
LineItem myCurve4 = myPane.AddCurve("wa", null, CreatePointForY(15), toolStripLine4.ForeColor, SymbolType.None);
}
if (checkBoxViewGraph5.Checked == true)
{
LineItem myCurve5 = myPane.AddCurve("cu", null, CreatePointForY(16), toolStripLine5.ForeColor, SymbolType.None);
}
}
zgc.AxisChange();
RepaintScreen();
}
#endregion
#region Method Calculate_Percentile_Value
private double GetPercentileValue(double[] intArrayYValue)
{
int intLength = 0;
float floatPercentileRow = 0;
int intPercentileRow = 0;
double doublePercentileValue = 0;
try
{
IEnumerable<double> sortAscendingQuery =
from intArrayYValueAscSort in intArrayYValue
orderby intArrayYValueAscSort //descending
select intArrayYValueAscSort;
intLength = intArrayYValue.Length;
floatPercentileRow = (floatGlobalPercentileValue / 100) * intLength;
intPercentileRow = Convert.ToInt32(Math.Ceiling(floatPercentileRow));
doublePercentileValue = sortAscendingQuery.ElementAt(intPercentileRow);
return doublePercentileValue;
}
catch (Exception ee)
{
MessageBox.Show("There was an error in calculating Percentile value.\n" + ee.Message, "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Error);
return doublePercentileValue;
}
}
#endregion
#region Method Calculate_Std_Deviation_Value
private double GetPopulationStdDeviationValue(double[] intArrayYValue)
{
//How to calculate Population Standard Deviation. In Excel, formula is STDEVP.
//Step1:Get the average value for the entire series of numbers. x'
//Step2:Subtract each number with x'. So it is (x-x')
//Step3:Square each number. So it is (x-x')^2
//Step4:Sum up all these numbers. SUM((x-x')^2)
//Step5:Take average value. (SUM((x-x')^2))/n
//Step6:Calculate ((SUM((x-x')^2))/n)^(1/2)
double doublePopStdDeviationValue = 0;
int intTotalRowValue = 0;
double doubleFirstAverage = 0;
double doubleSingleTempYValue = 0;
double doubleSumFirstValue = 0;
double doubleSecondAverage = 0;
try
{
intTotalRowValue = intArrayYValue.Length;
doubleFirstAverage = intArrayYValue.Average();//Step1:
for (int i = 0; i < intTotalRowValue; i++)
{
doubleSingleTempYValue = 0;
doubleSingleTempYValue = intArrayYValue[i] - doubleFirstAverage;//Step2:
doubleSingleTempYValue = doubleSingleTempYValue * doubleSingleTempYValue;//Step3:
doubleSumFirstValue = doubleSumFirstValue + doubleSingleTempYValue;//Step4:
}
doubleSecondAverage = doubleSumFirstValue / intTotalRowValue;//Step5:
doublePopStdDeviationValue = Math.Sqrt(doubleSecondAverage);//Step6:
return doublePopStdDeviationValue;
}
catch (Exception ee)
{
MessageBox.Show("There was an error in calculating Population Standard Deviation value.\n" + ee.Message, "VMSTAT Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Error);
return doublePopStdDeviationValue;
}
}
#endregion
#region Method Export_To_CSV
public void Export_To_CSV(string strFileName)
{
try
{
TextWriter tw = new StreamWriter(strFileName);
tw.WriteLine("," + checkBoxViewGraph1.Text + "," + checkBoxViewGraph2.Text + "," + checkBoxViewGraph3.Text + "," + checkBoxViewGraph4.Text + "," + checkBoxViewGraph5.Text);
tw.WriteLine("Minimum" + "," + txtMinimum1.Text + "," + txtMinimum2.Text + "," + txtMinimum3.Text + "," + txtMinimum4.Text + "," + txtMinimum5.Text);
tw.WriteLine("Average" + "," + txtAverage1.Text + "," + txtAverage2.Text + "," + txtAverage3.Text + "," + txtAverage4.Text + "," + txtAverage5.Text);
tw.WriteLine("maximum" + "," + txtMaximum1.Text + "," + txtMaximum2.Text + "," + txtMaximum3.Text + "," + txtMaximum4.Text + "," + txtMaximum5.Text);
tw.WriteLine("Percentile" + "," + txtPercentile1.Text + "," + txtPercentile2.Text + "," + txtPercentile3.Text + "," + txtPercentile4.Text + "," + txtPercentile5.Text);
tw.WriteLine("Std Deviation" + "," + txtStdDeviation1.Text + "," + txtStdDeviation2.Text + "," + txtStdDeviation3.Text + "," + txtStdDeviation4.Text + "," + txtStdDeviation5.Text);
tw.WriteLine("");
tw.WriteLine(lblSubDesc1.Text);
tw.WriteLine(lblSubDesc2.Text);
tw.WriteLine(lblSubDesc3.Text);
tw.WriteLine(lblSubDesc4.Text);
tw.WriteLine(lblSubDesc5.Text);
tw.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "VMSTAT Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
}
}