2017-07-12 3 views
0

Ich bin neu in C# und objektorientierte Programmierung im Allgemeinen. Ich erstelle einen Windows-Dienst, der eine IP-Adresse 10 Mal alle 10 Minuten pingt.C# - Windows Service - Kontinuierliche Ping Anfrage Timeouts

Wenn 7 von 10 Anfragen Timeout (Isolate Network Blips), würde es eine E-Mail senden, die benachrichtigt, dass das System nicht verfügbar ist. Ich habe diesen Teil richtig gemacht.

Das Problem, mit dem ich konfrontiert bin, ist mit der Meldung, dass das System läuft.

Das folgende ist mein Code:

protected override void OnStart(string[] args) 
     { 
       eventLog.WriteEntry("Source: Service Started",EventLogEntryType.SuccessAudit); 
       timer.Enabled = true; 
       timer.Interval = (10 * 60 * 1000); 
       timer.Elapsed += new System.Timers.ElapsedEventHandler(methodStart); 


     } 

public void methodStart(object sender, ElapsedEventArgs e) 
     { 

      Ping p = new Ping(); 
      PingReply r; 
      string s = "SYSTEM-IP-ADDRESS"; 
      int upCounter=0; 
      int downCounter = 0; 

      bool sysDown = false; 
      try 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        r = p.Send(s); 
        if (r.Status == IPStatus.Success) 
        { 
         eventLog.WriteEntry("Ping to " + s.ToString() + "[" + r.Address.ToString() + "]" + " Successful" 
         + " Response delay = " + r.RoundtripTime.ToString() + " ms" + "\n", EventLogEntryType.SuccessAudit); 
         upCounter++; 

        } 
        else 
        { 

         downCounter++; 
        } 
       } 

       if(downCounter>=7) 
       { 
        //LOG ENTRY 
        eventLog.WriteEntry("Unable to reach the system after 7 Timeouts! Email notification Sent.", EventLogEntryType.FailureAudit); 

        // EMAIL NOTIFICATION 


        downCounter = 0; 
       } 
       else 
       { 
        sysDown = false; 

       } 

      } 
      catch (Exception ex) 
      { 
       //EXCEPTION HANDLING 

      } 
      loopCounter++; 

      if(sysDown==false && loopCounter>2) 
      { 
       eventLog.WriteEntry("The Tool Is Up Email Notification Sent", EventLogEntryType.SuccessAudit); 

       // EMAIL NOTIFICATION 

       loopCounter = 0; 
      } 

     } 

Was ich versuche zu erreichen, die Ping-Zeiten out 7 => Zeit (en) und sendet eine E-Mail unter Angabe es ist nach unten. Wenn das System während der nächsten 2 Ausführungen aktiv ist, senden Sie eine E-Mail, die besagt, dass das System betriebsbereit ist (Mein Code sendet alle 2 Ausführungen eine E-Mail, die angibt, dass das System betriebsbereit ist).

Wie kann dies erreicht werden?

Update 1: Ich habe die E-Mail-Logik.

Update 2: Die Lösung von Vibhav Ramcharan löst eine Systemup-Benachrichtigung bei jeder Ausführung von startMethod() aus.

Der Schwellenwert für die System-Down-Benachrichtigung beträgt 70%. Dies sind 7 Ping-Fehler während einer einzigen Ausführung.

Angenommen, wenn das System ausfällt. Eine E-Mail wird ausgelöst, die einen Systemfehler meldet.

Wenn das System betriebsbereit ist, wird die Ausführung zweimal erfolgreich ausgeführt. Senden Sie eine E-Mail, die benachrichtigt, dass das System betriebsbereit ist.

Der obige Code löst eine System-E-Mail auf jedem methodStart(). Schließlich, Spamming.

Antwort

0

Der folgende Code funktioniert und tut das Nötige.

public void methodStart(object sender, ElapsedEventArgs e) 
      { 
       Ping p = new Ping(); 
       PingReply r; 
       string s = "SYSTEM-IP-ADDRESS"; 
       try 
       { 
        for (int i = 0; i < 10; i++) 
        { 
         r = p.Send(s); 
         if (r.Status == IPStatus.Success) 
         { 
          SuccessNoti(); 

         } 
         else 
         { 

          UnsuccessfulNoti(); 
         } 
        } 
       } 
       catch (Exception ex) 
       { 
        UnsuccessfulNoti(); 
       } 

       } 

      } 

      public void SuccessNoti() 
      { 
       if ((string.Compare(status, "Down", false) == 0) && Successcount >= 7) 
       { 
        using (MailMessage mail = new MailMessage()) 
        { 
         using (SmtpClient SmtpServer = new SmtpClient(smtp)) 
         { 
         // EMAIL NOTIFICATION 

          Successcount = 0; 
          status = null; 
         } 
        } 
       } 
       else 
       { 
        if (string.Compare(status, "Down", false) == 0) 
        { 
         Successcount = Successcount + 1; 
        } 
       } 
      } 

      public void sendfailureNotofication() 
      { 
       if (failureCount >= 7 && !(string.Compare(status, "Down", false) == 0)) 
       { 

        status = "Down"; 
        using (MailMessage mail = new MailMessage()) 
        { 
         using (SmtpClient SmtpServer = new SmtpClient(smtp)) 
         { 
         // EMAIL NOTIFICATION 

          failureCount = 0; 
          status = "Down"; 
         } 
        } 
       } 
       else 
       { 
        if (!(string.Compare(status, "Down", false) == 0)) 
        { 
         failureCount = failureCount + 1; 
        } 
       } 


      } 
0

Ok, ich lese Ihren Kommentar Ich denke, Sie brauchen einen statischen int außerhalb der Methode deklariert. Beispiel:

class Program{ 

    private static int loopCounter = 0; 
    static void Main(string[] args) 
    { 
      // code 
    } 

    public void methodStart(object sender, ElapsedEventArgs e){ 
     // code 
    } 

} 

Wenn Sie die Methode zweimal ausführen müssen, sollten Sie alle Verfahren aus methodStart extrahieren (zB Name: startUpMethod(), danach in methodStart Sie rufen startUpMethod()); Wenn Sie die Methode erneut aufrufen möchten (egal, ob sie innerhalb der gleichen Methode ist), rufen Sie startUpMethod() erneut auf. Dies wird als rekursiver Aufruf bezeichnet. Beispiel.

public void public void methodStart(object sender, ElapsedEventArgs e){ 
    startUpMethod(); 
} 

public void startUpMethod() 
{ 
     //do something 
     if(repeat) 
     startUpMethod() 
} 

Sie sollten Endlosschleife kümmern

+0

Nein, ich habe die E-Mail-Logik, die funktioniert.Was ich möchte, ist die Logik, die Methode zweimal auszuführen, nachdem das System ausfällt. – Tango

0

Von meinem Verständnis, müssen Sie zwei erfolgreiche Ausführungen verfolgen und eine E-Mail nach einem Fehler senden. Ich habe Kommentare im folgenden Code hinzugefügt.

protected override void OnStart(string[] args){ 

     var timer = new Timer 
     { 
      Enabled = true, 
      Interval = (10 * 60 * 1000) 
     }; 
     timer.Elapsed += new System.Timers.ElapsedEventHandler(methodStart); 
    } 

    private int loopCounter = 0; 
    bool sysDown = false; 

    // Used to count the number of successful executions after a failure. 
    int systemUpAfterFailureCount = 0; 

    public void methodStart(object sender, ElapsedEventArgs e) 
    { 

     Ping p = new Ping(); 
     PingReply r; 
     string s = "SYSTEM-IP-ADDRESS"; 
     int upCounter = 0; 
     int downCounter = 0; 


     try 
     { 
      for (int i = 0; i < 10; i++) 
      { 
       r = p.Send(s); 

       if (r.Status == IPStatus.Success) 
        upCounter++; 
       else 
        downCounter++; 
      } 

      if (downCounter >= 7) 
      { 
       // LOG ENTRY 
       // EMAIL NOTIFICATION 
       downCounter = 0; 
       // The system has failed 
       sysDown = true; 
      } 
      else 
      { 
       // Before changing the sysDown flag, check if the system was previously down 
       // This is the first successful execution after the failure 
       if (sysDown) 
        systemUpAfterFailureCount++; 

       // This will allow systemUpAfterFailureCount to increase if it is Ex: 1 and sysDown = false (Your success execution limit is 2, which we control at the IF block below) 
       if (systemUpAfterFailureCount > 1) 
        systemUpAfterFailureCount++; 

       sysDown = false; 
      } 

     } 
     catch (Exception ex) 
     { 
      //EXCEPTION HANDLING 
     } 

     loopCounter++; 

     // Check if the system is down, if it is up execute the following code for a maximum of 2 executions. 
     if (sysDown==false && systemUpAfterFailureCount <= 2) 
     { 
      // LOG ENTRY 
      loopCounter = 0; 

      // Send email for successful executions after a failure, limited to 2. 
      if (systemUpAfterFailureCount <= 2) 
      { 
       // EMAIL NOTIFICATION 
      } 

      // After two successful executions have occured, reset the counter 
      if (systemUpAfterFailureCount == 2) 
      { 
       systemUpAfterFailureCount = 0; 
      } 
     } 
    } 
+0

Leider sendet diese Lösung nach jeder Ausführung von startMethod eine E-Mail. – Tango