private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Timer tetiklenme anı: " + e.SignalTime.ToLongTimeString());

            timer1.Enabled = false;
            String line = "";
            Rec rec = new Rec();
            recList = new ArrayList();
            SshShell se = null;
            StringReader strR = null;

            try
            {
                L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> SshShell nesnesi için. Host: " + remoteHost + ", User: "******", Pass: *******");

                se = new SshShell(remoteHost, user);
                if (!String.IsNullOrEmpty(password))
                    se.Password = password;

                //L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Started to connect and parse lines.");
                if (se != null)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> SshShell nesnesi Üretildi.");
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> SshShell nesnesi Üretilemedi.");
                }

                if (!se.Connected)
                {
                    try
                    {
                        se.Connect(22, 2000);
                        L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Bağlantı açıldı.");
                    }
                    catch (Exception ex)
                    {
                        L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Connection problem. Hata : " + ex.ToString());
                    }
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Zaten kurulu olan bağlantı ile devam ediliyor.");
                }

                if (se.Connected)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Bağlantı şuan açık.");
                    if (se.ShellOpened)
                    {
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Shell şuan açık.");
                        if (se.ShellConnected)
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Shell Bağlantı kurdu.");
                            try
                            {
                                //We can collect data.
                                strR = GetData(se);
                            }
                            catch (Exception ex)
                            {
                                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Data Getirirken hata ile karşılaşıldı.");
                                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Hata: " + ex.ToString());
                            }
                        }
                    }
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Bağlantı kopuk!!!!");
                }

                while ((line = strR.ReadLine()) != null)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Line to Parse : " + line);
                    rec.Description = line;
                    String[] arr = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (arr.Length >= 8)
                    {
                        rec.Description = line;

                        if (arr[0].Contains("."))
                        {
                            rec.ComputerName = remoteHost;
                            rec.CustomStr1 = arr[0];
                            tempMac = arr[1];

                            tempMac = tempMac.Remove(0, 2);
                            string[] arrMac = tempMac.Split('.');
                            tempMac = "";
                            for (int i = 0; i < arrMac.Length; i++)
                            {
                                tempMac += arrMac[i];
                            }
                            rec.CustomStr2 = tempMac.Trim();
                            rec.EventType = arr[7];

                            for (int i = 2; i < 6; i++)
                            {
                                tempDate += arr[i] + " ";
                            }
                            tempDate = tempDate.Trim();

                            if (arr[6] == "PM" && Convert.ToDateTime(tempDate).Hour != 12)
                            {
                                rec.Datetime = Convert.ToDateTime(tempDate, CultureInfo.InvariantCulture).AddDays(-8).AddHours(12).ToString("dd/MM/yyyy HH:mm:ss");
                            }
                            else
                            {
                                rec.Datetime = Convert.ToDateTime(tempDate, CultureInfo.InvariantCulture).AddDays(-8).ToString("dd/MM/yyyy HH:mm:ss");
                            }

                            if (Convert.ToDateTime(tempDate).Date.Day == DateTime.Now.Date.Day)
                            {
                                rec.CustomStr3 = "Removed";
                            }

                            tempDate = "";
                            recList.Add(rec);
                        }
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Çekilen veriler tarihlerine bakılarak kaydedilecek.");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Kalınan LastPosition: " + Local_LastPosition);

                lastDate = new DateTime();
                bool lastPositionSetted = DateTime.TryParse(Local_LastPosition, out lastDate);

                foreach (Rec r in recList)
                {
                    if (!lastPositionSetted)
                    {
                        //İlk kayıtlar.
                        CustomServiceBase s = base.GetInstanceService("Security Manager Remote Recorder");
                        s.SetData(Dal, virtualhost, r);
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> LastPosition is null. Veri ataması gerçekleştirildi.");
                    }
                    else
                    {
                        //Daha önce kayıt alınmış.
                        if (Convert.ToDateTime(r.Datetime) > lastDate)
                        {
                            //Değişen kayıt olmuş. Veritabanına eklenmeli.
                            CustomServiceBase s = base.GetInstanceService("Security Manager Remote Recorder");
                            s.SetData(Dal, virtualhost, r);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Record Date daha büyük. Veri ataması gerçekleştirildi. : " + r.Datetime);
                        }
                        else
                        {
                            //Tarih aynı. Kayıt işlemi yapılmayacak.
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Record Date daha küçük. Kaydedilmedi!. Tarih: " + r.Datetime);
                        }
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> En büyük tarih belirleniyor.");
                maxDate = lastDate.ToString();

                foreach (Rec r in recList)
                {
                    if (Convert.ToDateTime(r.Datetime) > Convert.ToDateTime(maxDate))
                    {
                        maxDate = r.Datetime;
                    }
                }

                CustomServiceBase ser = base.GetInstanceService("Security Manager Remote Recorder");
                ser.SetReg(ID, maxDate, "", "", "");
                Local_LastPosition = maxDate;
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Yeni LastPosition atandı. LastPostion: " + Local_LastPosition);

            }
            catch (Exception ex)
            {
                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Recorder Main Exception: " + ex.ToString());
            }
            finally
            {
                if (se != null)
                {
                    if (se.Connected)
                    {
                        se.Close();
                        L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Bağlantı kesildi.");
                    }
                }

                se = null;
                tempDate = "";
                tempMac = "";
                timer1.Enabled = true;
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Timer'ın işi bitti. Time: " + DateTime.Now.ToLongTimeString());
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Bir Sonraki Tetikleme " + timer_interval / 60000 + " dakika sonra gerçekleşecektir.");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> =============================================== ");
                recList.Clear();
            }
        }
示例#2
0
        private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Timer tetiklenme ani: " + e.SignalTime.ToLongTimeString());

            timer1.Enabled = false;
            String line = "";
            Rec rec = new Rec();
            recList = new ArrayList();
            SshShell se = null;
            StringReader strR = null;

            try
            {
                L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> SshShell nesnesi icin. Host: " + remoteHost + ", User: "******", Pass: *******");

                se = new SshShell(remoteHost, user);
                if (!String.IsNullOrEmpty(password))
                    se.Password = password;

                //L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Started to connect and parse lines.");
                if (se != null)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> SshShell nesnesi Üretildi.");
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> SshShell nesnesi Üretilemedi.");
                }

                if (!se.Connected)
                {
                    try
                    {
                        se.Connect(22, 2000);
                        L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Baglanti acildi.");
                    }
                    catch (Exception ex)
                    {
                        L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Connection problem. Hata : " + ex.ToString());
                    }
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Zaten kurulu olan Baglanti ile devam ediliyor.");
                }

                if (se.Connected)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Baglanti suan acik.");
                    if (se.ShellOpened)
                    {
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Shell suan acik.");
                        if (se.ShellConnected)
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Shell Baglanti kurdu.");
                            try
                            {
                                //We can collect data.
                                strR = GetData(remoteHost, user, password, se);
                            }
                            catch (Exception ex)
                            {
                                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Data Getirirken hata ile karsilasildi.");
                                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Hata: " + ex.ToString());
                            }
                        }
                    }
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Baglanti kopuk!!!!");
                }

                while ((line = strR.ReadLine()) != null)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Line to Parse : " + line);
                    rec.Description = line;
                    //String[] arr = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    String[] arr = SpaceSplit(line, false);
                    if (line != "show ip dhcp binding")
                    {
                        try
                        {
                            rec.Description = line;
                            rec.CustomStr1 = arr[0];
                            rec.CustomStr2 = arr[1];
                            rec.CustomStr3 = arr[2];
                            rec.CustomStr4 = arr[3];
                            rec.Datetime = DateTime.Now.ToString(dateFormat);//

                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Datetime : " + rec.Datetime);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Customstr1 : " + rec.CustomStr1);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Customstr2 : " + rec.CustomStr2);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Customstr3 : " + rec.CustomStr3);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Customstr4 : " + rec.CustomStr4);

                            recList.Add(rec);
                        }
                        catch (Exception exception)
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> ERROR: " + exception.Message);
                        }
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> cekilen veriler tarihlerine bakilarak kaydedilecek.");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Kalinan LastPosition: " + Local_LastPosition);

                lastDate = new DateTime();
                bool lastPositionSetted = DateTime.TryParse(Local_LastPosition, out lastDate);
                rec.LogName = "CiscoDhcpV_1_0_0Recorder";
                foreach (Rec r in recList)
                {
                    if (!lastPositionSetted)
                    {
                        //İlk kayitlar.
                        CustomServiceBase s = base.GetInstanceService("Security Manager Remote Recorder");
                        s.SetData(Dal, virtualhost, r);
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> LastPosition is null. Veri atamasi gerceklestirildi.");
                    }
                    else
                    {
                        //Daha önce kayit alinmis.
                        if (Convert.ToDateTime(r.Datetime) > lastDate)
                        {
                            //Değisen kayit olmus. Veritabanina eklenmeli.
                            CustomServiceBase s = base.GetInstanceService("Security Manager Remote Recorder");
                            s.SetData(Dal, virtualhost, r);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Record Date daha büyük. Veri atamasi gerceklestirildi. : " + r.Datetime);
                        }
                        else
                        {
                            //Tarih ayni. Kayit islemi yapilmayacak.
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Record Date daha kücük. Kaydedilmedi!. Tarih: " + r.Datetime);
                        }
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> En büyük tarih belirleniyor.");
                maxDate = lastDate.ToString();

                foreach (Rec r in recList)
                {
                    if (Convert.ToDateTime(r.Datetime) > Convert.ToDateTime(maxDate))
                    {
                        maxDate = r.Datetime;
                    }
                }

                CustomServiceBase ser = base.GetInstanceService("Security Manager Remote Recorder");
                ser.SetReg(ID, maxDate, "", "", "");
                Local_LastPosition = maxDate;
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Yeni LastPosition atandi. LastPostion: " + Local_LastPosition);

            }
            catch (Exception ex)
            {
                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Recorder Main Exception: " + ex.ToString());
            }
            finally
            {
                if (se != null)
                {
                    if (se.Connected)
                    {
                        se.Close();
                        L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Baglanti kesildi.");
                    }
                }

                se = null;
                tempDate = "";
                tempMac = "";
                timer1.Enabled = true;
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Timer'in isi bitti. Time: " + DateTime.Now.ToLongTimeString());
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Bir Sonraki Tetikleme " + timer_interval / 60000 + " dakika sonra gerceklesecektir.");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> =============================================== ");
                recList.Clear();
            }
            se.Close();
        }
        private StringReader GetData(SshShell se)
        {
            StringReader strR = null;
            string commandToRead = "show ip dhcp binding";
            Stream st = null;
            StreamReader sr = null;
            StringBuilder strB = new StringBuilder();
            bool morePage = false;

            while (true)
            {
                se.WriteLine(commandToRead);
                se.WriteLine(commandToRead);
                L.Log(LogType.FILE, LogLevel.DEBUG, " GetData() --> Komut çalıştırıldı : " + commandToRead);

                st = se.GetStream();
                sr = new StreamReader(st);

                int cnt = 0;
                int asci = 0;
                string lineStr = "";
                string previousStr = "";

                while ((asci = sr.Read()) != 0)
                {
                    char karakter = Convert.ToChar(asci);

                    if (karakter == '#')
                    {
                        if (cnt > 0)
                        {
                            break;
                        }
                        cnt++;
                    }

                    if (karakter != '\n')
                    {
                        lineStr += karakter.ToString();
                    }
                    else
                    {
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Read Line : " + lineStr);
                        if (lineStr.ToLower().Contains("more"))
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> There is more ip to be read.");
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> We will read more page.");
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Last ip line we have got : " + previousStr);
                            morePage = true;
                        }
                        else
                        {
                            previousStr = lineStr;
                            strB.Append(lineStr);
                        }
                        lineStr = "";
                    }
                }

                if (morePage)
                {
                    commandToRead = "show ip dhcp binding | begin " + previousStr;
                    morePage = false;
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> All ip has got.");
                    break;
                }
            }

            strR = new StringReader(strB.ToString());

            if (se.Connected)
            {
                se.WriteLine("exit");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Komut çalıştırıldı. exit");

                se.Close();
                L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() | GetData() --> Bağlantı kesildi.");
            }

            st.Close();
            sr.Close();

            return strR;
        }