void addData(VMusage.procVMinfo vmdata) { if (this.dataGridView1.InvokeRequired) { addDataCallback d = new addDataCallback(addData); this.Invoke(d, new object[] { vmdata }); } else { System.Diagnostics.Debug.WriteLine("addData() called"); dataGridView1.SuspendLayout(); //enqueue data to be saved to sqlite dataQueue.Enqueue(vmdata); if (bAllowGUIupdate) { //dataAccess.addSqlData(procStats); //dtProcesses.Rows.Clear(); dataAccess.addData(vmdata); //release queue data dataAccess.waitHandle.Set(); //object[] o = new object[7]{ procUsage.procStatistics. .procStatistics. [i].sApp, eventEntries[i].sArg, eventEntries[i].sEvent, // eventEntries[i].sStartTime, eventEntries[i].sEndTime, eventEntries[i].sType, eventEntries[i].sHandle }; } dataGridView1.Refresh(); dataGridView1.ResumeLayout(); } }
void dataAddThread() { try { while (bRunDataThread) { waitHandle.WaitOne(); if (dataQueue.Count > 10) { while (dataQueue.Count > 0) { VMusage.procVMinfo vmInfo = dataQueue.Dequeue(); lastTotaMemUse += vmInfo.memusage; lastMemMeasure = new DateTime(vmInfo.Time); addSqlData(vmInfo); } } Thread.Sleep(10); } } catch (ThreadAbortException ex) { System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); } }
void recvr_onUpdate(object sender, VMusage.procVMinfo data) { System.Diagnostics.Debug.WriteLine("recvr_onUpdate() called"); //string s = data.processID.ToString() + ", " + // data.sName + ", " + // data.procUsage.user.ToString() + ", " + // data.duration.ToString(); ////addLog(s); //System.Diagnostics.Debug.WriteLine( data.dumpStatistics() ); addData(data); }
public bool addData(VMusage.procVMinfo vmInfo) { bool bRet = false; try { //string txtSQLQuery = "insert into processes (desc) values ('" + txtDesc.Text + "')"; //ExecuteQuery(txtSQLQuery); object[] o = new object[] { vmInfo.remoteIP, vmInfo.name, vmInfo.memusage, vmInfo.slot, vmInfo.procID, new DateTime(vmInfo.Time), 0, }; DataRow dr; //check if data already exists dr = dsVMUsage.Tables[0].Rows.Find(vmInfo.name); if (dr == null) { //add a new row dr = dtVMUsage.NewRow(); dr.ItemArray = o; dtVMUsage.Rows.Add(dr); } else { dr.ItemArray = o; } dr.AcceptChanges(); dtVMUsage.AcceptChanges(); dsVMUsage.AcceptChanges(); bRet = true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("addData Exception: " + ex.Message); } return(bRet); }
//see also http://www.techcoil.com/blog/my-experience-with-system-data-sqlite-in-c/ public void addSqlData(VMusage.procVMinfo procVMStats) { //System.Diagnostics.Debug.WriteLine(procStats.dumpStatistics()); long rowID = 0; //last inserted row #region Process_data //build a list of field names of process table if (FieldsProcessTable.Length == 0) { //StringBuilder //FieldsProcessTable = new StringBuilder(); for (int ix = 0; ix < _fieldsProcess.Length; ix++) { FieldsProcessTable.Append(_fieldsProcess[ix].FieldName); if (ix < _fieldsProcess.Length - 1) { FieldsProcessTable.Append(", "); } } } StringBuilder FieldsProcessValues = new StringBuilder(); FieldsProcessValues.Append("'" + procVMStats.remoteIP + "', "); FieldsProcessValues.Append("'" + procVMStats.name.ToString() + "', "); FieldsProcessValues.Append("'" + procVMStats.memusage.ToString() + "', "); FieldsProcessValues.Append("'" + procVMStats.slot.ToString() + "', "); FieldsProcessValues.Append(procVMStats.procID.ToString() + ", "); FieldsProcessValues.Append(procVMStats.Time.ToString() + ", "); FieldsProcessValues.Append("NULL"); //add an idx although it is autoincrement string sqlStatement = "INSERT INTO VMUsage " + "(" + FieldsProcessTable + ")" + " VALUES(" + FieldsProcessValues.ToString() + ")"; rowID = executeNonQuery(sqlStatement); #endregion }
public int ImportMemUsageFromCSV(string sFileCSV, ref List<VMusage.procVMinfo> data) { int iRet = 0; sql_cmd = new SQLiteCommand(); sql_con = new SQLiteConnection(); connectDB(); if (sql_con.State != ConnectionState.Open) { sql_con.Close(); sql_con.Open(); } sql_cmd = sql_con.CreateCommand(); int iCnt = 0; //open file, split lines and add to database // input line: //05.11.2013 20:42:58 0xffff002 'NK.EXE' 0 0xaffed752 'filesys.exe' 0 0x4ffbaf3a 'device.exe' 5906432 0xaf10758a 'connmgr.exe' //1. entry = date and time //then three entries for every process // 1. process ID // 2. procfess name // 3. mem usage // the above repeats x times and the last field is the cummulated mem usage in paranthesis, ie (16244736) OnNewMessage("reading import file..."); List<string> lLines = new List<string>(); using (TextReader sr = new StreamReader(sFileCSV)) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { if (line.Length > 0) { lLines.Add(line); iCnt++; } } } OnNewMessage(iCnt.ToString()+" lines read"); //all lines read into memory string[] aLines = lLines.ToArray(); lLines.Clear(); createTablesSQL();//will clear all data and create empty table vmUsage int lCnt = 0; for (int i = 0; i < aLines.Length; i++) { OnNewMessage("processing line " + i.ToString()); Application.DoEvents(); //cleanup aLines[i] = aLines[i].Replace("\t\t", "\t"); //split string[] fields = aLines[i].Split(new char[] { '\t' }); //System.Diagnostics.Debug.WriteLine("line='" + aLines[i] + "'"); //foreach (string s in fields) // System.Diagnostics.Debug.Write(s + "\t"); //System.Diagnostics.Debug.WriteLine(""); if ((fields.Length-2) % 3 == 0) //test for right number of fields { //field 0 is datetime string sTime = fields[0]; DateTime dt = DateTime.Parse(sTime); long lTime = dt.Ticks; for (int j = 1; j < fields.Length; j += 3) { try { //field 1 is procID if (fields[j].StartsWith("(")) continue; uint procID = uint.Parse(fields[j].Replace("0x", ""), System.Globalization.NumberStyles.HexNumber | System.Globalization.NumberStyles.AllowLeadingWhite | System.Globalization.NumberStyles.AllowTrailingWhite); string procName = fields[j + 1].Replace("'", ""); uint memUse = uint.Parse(fields[j + 2]); VMusage.procVMinfo procData = new VMusage.procVMinfo(procName, procID, memUse, 0, lTime); data.Add(procData); //OnNewMessage("added new data: "); //int slot = 1; //string remoteIP = "0.0.0.0"; string sMemuse=""; string sProcID=""; string sTimestamp = ""; try { sMemuse = memUse.ToString(); sProcID = procID.ToString(); sTimestamp = lTime.ToString(); //add data to DB sql_cmd.CommandText = "Insert Into VMUsage (RemoteIP, Name, MemUsage, Slot, ProcID, Time) " + "VALUES('127.0.0.1', '" + procName + "', " + memUse.ToString() + ", 0, " + procID.ToString() + ", " + lTime.ToString() + ");"; lCnt = sql_cmd.ExecuteNonQuery(); iRet += lCnt; //OnNewMessage("Inserted new data"); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception Import(): " + ex.Message); OnNewMessage("Exception Import(): " + ex.Message); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in Import(): " + ex.Message + "\nlast sqlCmd" + sql_cmd.CommandText); OnNewMessage("Exception in Import(): " + ex.Message + "\nlast sqlCmd" + sql_cmd.CommandText); } } // data is // remoteIP|Name|MemUsage|Slot|ProcID|Time|idx }//fields } //convert db back to CSV (rotated) OnNewMessage("starting rotation..."); ExportMemUsage2CSV2(sFileCSV + "_n.csv", "0.0.0.0"); MessageBox.Show("exported data to '" + sFileCSV + "_n.csv'"); return iRet; }
public int ImportMemUsageFromCSV(string sFileCSV, ref List <VMusage.procVMinfo> data) { int iRet = 0; sql_cmd = new SQLiteCommand(); sql_con = new SQLiteConnection(); connectDB(); if (sql_con.State != ConnectionState.Open) { sql_con.Close(); sql_con.Open(); } sql_cmd = sql_con.CreateCommand(); int iCnt = 0; //open file, split lines and add to database // input line: //05.11.2013 20:42:58 0xffff002 'NK.EXE' 0 0xaffed752 'filesys.exe' 0 0x4ffbaf3a 'device.exe' 5906432 0xaf10758a 'connmgr.exe' //1. entry = date and time //then three entries for every process // 1. process ID // 2. procfess name // 3. mem usage // the above repeats x times and the last field is the cummulated mem usage in paranthesis, ie (16244736) OnNewMessage("reading import file..."); List <string> lLines = new List <string>(); using (TextReader sr = new StreamReader(sFileCSV)) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { if (line.Length > 0) { lLines.Add(line); iCnt++; } } } OnNewMessage(iCnt.ToString() + " lines read"); //all lines read into memory string[] aLines = lLines.ToArray(); lLines.Clear(); createTablesSQL();//will clear all data and create empty table vmUsage int lCnt = 0; for (int i = 0; i < aLines.Length; i++) { OnNewMessage("processing line " + i.ToString()); Application.DoEvents(); //cleanup aLines[i] = aLines[i].Replace("\t\t", "\t"); //split string[] fields = aLines[i].Split(new char[] { '\t' }); //System.Diagnostics.Debug.WriteLine("line='" + aLines[i] + "'"); //foreach (string s in fields) // System.Diagnostics.Debug.Write(s + "\t"); //System.Diagnostics.Debug.WriteLine(""); if ((fields.Length - 2) % 3 == 0) //test for right number of fields { //field 0 is datetime string sTime = fields[0]; DateTime dt = DateTime.Parse(sTime); long lTime = dt.Ticks; for (int j = 1; j < fields.Length; j += 3) { try { //field 1 is procID if (fields[j].StartsWith("(")) { continue; } uint procID = uint.Parse(fields[j].Replace("0x", ""), System.Globalization.NumberStyles.HexNumber | System.Globalization.NumberStyles.AllowLeadingWhite | System.Globalization.NumberStyles.AllowTrailingWhite); string procName = fields[j + 1].Replace("'", ""); uint memUse = uint.Parse(fields[j + 2]); VMusage.procVMinfo procData = new VMusage.procVMinfo(procName, procID, memUse, 0, lTime); data.Add(procData); //OnNewMessage("added new data: "); //int slot = 1; //string remoteIP = "0.0.0.0"; string sMemuse = ""; string sProcID = ""; string sTimestamp = ""; try { sMemuse = memUse.ToString(); sProcID = procID.ToString(); sTimestamp = lTime.ToString(); //add data to DB sql_cmd.CommandText = "Insert Into VMUsage (RemoteIP, Name, MemUsage, Slot, ProcID, Time) " + "VALUES('127.0.0.1', '" + procName + "', " + memUse.ToString() + ", 0, " + procID.ToString() + ", " + lTime.ToString() + ");"; lCnt = sql_cmd.ExecuteNonQuery(); iRet += lCnt; //OnNewMessage("Inserted new data"); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception Import(): " + ex.Message); OnNewMessage("Exception Import(): " + ex.Message); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in Import(): " + ex.Message + "\nlast sqlCmd" + sql_cmd.CommandText); OnNewMessage("Exception in Import(): " + ex.Message + "\nlast sqlCmd" + sql_cmd.CommandText); } } // data is // remoteIP|Name|MemUsage|Slot|ProcID|Time|idx }//fields } //convert db back to CSV (rotated) OnNewMessage("starting rotation..."); ExportMemUsage2CSV2(sFileCSV + "_n.csv", "0.0.0.0"); MessageBox.Show("exported data to '" + sFileCSV + "_n.csv'"); return(iRet); }