Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Is it possible to add fore ground and back ground to any app that is down?

Is it possible to add fore ground and back ground to any app that is down?

Scheduled Pinned Locked Moved ASP.NET
csharplinqcomcollaborationtools
5 Posts 2 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samflex
    wrote on last edited by
    #1

    Greetings again, Sorry that I have to come back for help for this script. Each time this script runs, it sends email notifications to our Executive team advising them of whether any or all of the apps are either down or up. The script works very well thanks entirely to the great Richard Deeming. However, management has asked that I modify the script to add foreground color of red and background color of yellow to any URL of the app that is down along with the text indicating down. For instance, when app sends out email notifications, it lists the apps and their status as follows: Please find the status of the DMZ servers below: https://www.link1: WORKING https://www.link2.com WORKING https://www.link3.com DOWN https://www.link4.com WORKING They would like any app that is down to display as follows: Please find the status of the DMZ servers below: link 1: https://www.link1 WORKING Link 2 https://www.link2.com WORKING Link 3 https://www.link3.com DOWN Link 4 https://www.link4.com WORKING In this example, Link 3 https://www.link3.com DOWN They would like the entire row of the URL that is down to be color coded, background color of yellow and text color of red. Link 1 corresponds to the first URL, Link 2 to second URL, etc. If it is only possible to just color code just the URL and the DOWN text without the link #, that would be fine too. I am not sure if this is possible. I could not figure a way to do this. Any thought on how I could get this to work? I recognize the send mail bit has body (IsBodyHtml) set to false but I can change this to true if I can the color thing to work. Below is the working code.

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    using System.Net.NetworkInformation;
    using System.Text;
    using System.Configuration;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using System.Net.Http;
    using System.Linq;

    namespace showserverstatus
    {
    class Program
    {
    static async Task Main(string[] args)
    {
    System.Collections.Concurrent.ConcurrentDictionary urlToStatus = new();

             IEnumerable < Task > tasks = args.Select(async url =>
            {
                bool result = await ServerStatusByAsync(url);
                return urlToStatus.TryAdd(url, result ? "WORKING" : "DOWN");
            });
    
            bool\[\] results = await Task.WhenAll(tasks);
    
            StringBuilder body = new("Please find the st
    
    Richard DeemingR 1 Reply Last reply
    0
    • S samflex

      Greetings again, Sorry that I have to come back for help for this script. Each time this script runs, it sends email notifications to our Executive team advising them of whether any or all of the apps are either down or up. The script works very well thanks entirely to the great Richard Deeming. However, management has asked that I modify the script to add foreground color of red and background color of yellow to any URL of the app that is down along with the text indicating down. For instance, when app sends out email notifications, it lists the apps and their status as follows: Please find the status of the DMZ servers below: https://www.link1: WORKING https://www.link2.com WORKING https://www.link3.com DOWN https://www.link4.com WORKING They would like any app that is down to display as follows: Please find the status of the DMZ servers below: link 1: https://www.link1 WORKING Link 2 https://www.link2.com WORKING Link 3 https://www.link3.com DOWN Link 4 https://www.link4.com WORKING In this example, Link 3 https://www.link3.com DOWN They would like the entire row of the URL that is down to be color coded, background color of yellow and text color of red. Link 1 corresponds to the first URL, Link 2 to second URL, etc. If it is only possible to just color code just the URL and the DOWN text without the link #, that would be fine too. I am not sure if this is possible. I could not figure a way to do this. Any thought on how I could get this to work? I recognize the send mail bit has body (IsBodyHtml) set to false but I can change this to true if I can the color thing to work. Below is the working code.

      using System;
      using System.IO;
      using System.Net;
      using System.Net.Mail;
      using System.Net.NetworkInformation;
      using System.Text;
      using System.Configuration;
      using System.Collections.Generic;
      using System.Threading.Tasks;
      using System.Net.Http;
      using System.Linq;

      namespace showserverstatus
      {
      class Program
      {
      static async Task Main(string[] args)
      {
      System.Collections.Concurrent.ConcurrentDictionary urlToStatus = new();

               IEnumerable < Task > tasks = args.Select(async url =>
              {
                  bool result = await ServerStatusByAsync(url);
                  return urlToStatus.TryAdd(url, result ? "WORKING" : "DOWN");
              });
      
              bool\[\] results = await Task.WhenAll(tasks);
      
              StringBuilder body = new("Please find the st
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You would need to format the message body as HTML.

      static async Task SendEmailAsync(string subject, string body, bool isBodyHtml = false)
      {
      using MailMessage mm = new(ConfigurationManager.AppSettings["FromEmail"], "joeblow@gmail.com");
      mm.To.Add("janeblow@yahoo.com");
      mm.CC.Add("kevin.bruiner@hotmail.com");
      mm.Subject = subject;
      mm.Body = body;
      mm.IsBodyHtml = isBodyHtml;

      SmtpClient smtp = new()
      {
          Host = ConfigurationManager.AppSettings\["Host"\],
          Port = int.Parse(ConfigurationManager.AppSettings\["Port"\]),
          EnableSsl = true,
          UseDefaultCredentials = false,
          Credentials = new NetworkCredential(ConfigurationManager.AppSettings\["Username"\], ConfigurationManager.AppSettings\["Password"\]),
      };
      
      await smtp.SendMailAsync(mm);
      

      }

      static async Task Main(string[] args)
      {
      System.Collections.Concurrent.ConcurrentDictionary urlToStatus = new();

      IEnumerable<Task<bool>> tasks = args.Select(async url =>
      {
          bool result = await ServerStatusByAsync(url);
          return urlToStatus.TryAdd(url, result);
      });
      
      bool\[\] results = await Task.WhenAll(tasks);
      
      StringBuilder body = new("<p>Please find the status of the DMZ servers below:</p>");
      body.Append("<ul>");
      foreach (var kvp in urlToStatus)
      {
          string encodedLink = System.Net.WebUtility.HtmlEncode(kvp.Key);
          
          body.Append(kvp.Value ? "<li>" : "<li style=\\"color:red;background-color:yellow;\\">");
          body.Append(kvp.Value ? "<a href=\\"" : "<a style=\\"color:red;\\" href=\\"");
          body.Append(encodedLink);
          body.Append("\\">");
          body.Append(encodedLink);
          body.Append("</a> - ");
          body.Append(kvp.Value ? "WORKING" : "DOWN");
          body.Append("</li>");
      }
      body.Append("</ul>");
      
      await SendEmailAsync("DMZ Server Status", body.ToString(), true);
      await Task.Delay(3000);
      
      // Return the number of servers which were down:
      return results.Count(result => !result);
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      S 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You would need to format the message body as HTML.

        static async Task SendEmailAsync(string subject, string body, bool isBodyHtml = false)
        {
        using MailMessage mm = new(ConfigurationManager.AppSettings["FromEmail"], "joeblow@gmail.com");
        mm.To.Add("janeblow@yahoo.com");
        mm.CC.Add("kevin.bruiner@hotmail.com");
        mm.Subject = subject;
        mm.Body = body;
        mm.IsBodyHtml = isBodyHtml;

        SmtpClient smtp = new()
        {
            Host = ConfigurationManager.AppSettings\["Host"\],
            Port = int.Parse(ConfigurationManager.AppSettings\["Port"\]),
            EnableSsl = true,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(ConfigurationManager.AppSettings\["Username"\], ConfigurationManager.AppSettings\["Password"\]),
        };
        
        await smtp.SendMailAsync(mm);
        

        }

        static async Task Main(string[] args)
        {
        System.Collections.Concurrent.ConcurrentDictionary urlToStatus = new();

        IEnumerable<Task<bool>> tasks = args.Select(async url =>
        {
            bool result = await ServerStatusByAsync(url);
            return urlToStatus.TryAdd(url, result);
        });
        
        bool\[\] results = await Task.WhenAll(tasks);
        
        StringBuilder body = new("<p>Please find the status of the DMZ servers below:</p>");
        body.Append("<ul>");
        foreach (var kvp in urlToStatus)
        {
            string encodedLink = System.Net.WebUtility.HtmlEncode(kvp.Key);
            
            body.Append(kvp.Value ? "<li>" : "<li style=\\"color:red;background-color:yellow;\\">");
            body.Append(kvp.Value ? "<a href=\\"" : "<a style=\\"color:red;\\" href=\\"");
            body.Append(encodedLink);
            body.Append("\\">");
            body.Append(encodedLink);
            body.Append("</a> - ");
            body.Append(kvp.Value ? "WORKING" : "DOWN");
            body.Append("</li>");
        }
        body.Append("</ul>");
        
        await SendEmailAsync("DMZ Server Status", body.ToString(), true);
        await Task.Delay(3000);
        
        // Return the number of servers which were down:
        return results.Count(result => !result);
        

        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        S Offline
        S Offline
        samflex
        wrote on last edited by
        #3

        Oh wow, this is so incredible. Just too good. Many, many THANK YOU sir. I truly do appreciate your help. You are the nicest, most generous individual that I never met. Sir, do you know why they keep getting two different emails? Let's say for instance that they just wanted to get the status of one link, https://www.link1.com When they receive email, it displays like this: DMZ Server Status. Please find the status of the DMZ servers below. https://www.link1.com WORKING GET https://www.link1.com" returned OK Is there a way to remove the second email so they always get the bolded one with subject DMZ Server Status and when they open the email, they get details about the link or links? UPDATE: I *THINK* I got it! This is the part of the script that is sending the GET email ( the second email)

            static async Task ServerStatusByAsync(string url)
            {
                HttpClient http = new();
                using (HttpResponseMessage response = await http.GetAsync(url))
                {
                    Console.WriteLine("GET {0}: {1}", url, response.StatusCode);
        
                    if (response.IsSuccessStatusCode)
                    {
                      //  await SendEmailAsync($"{url} WORKING", $"GET {url} returned {response.StatusCode}");
                        return true;
                    }
                   // await SendEmailAsync($"{url} DOWN", $"GET {url} returned {response.StatusCode}");
                    return false;
                }
            }
        

        So by commenting out

        await SendEmailAsync(...)

        for both WORKING and DOWN, allows only one email: DMZ Server Status... to come through. I hope it is correct. If so, all I would need help on now is to create another copy of this same script but this time, to send email to certain group ONLY when any or all of the apps is(are) down and I swear no more changes will be needed.

        Richard DeemingR 1 Reply Last reply
        0
        • S samflex

          Oh wow, this is so incredible. Just too good. Many, many THANK YOU sir. I truly do appreciate your help. You are the nicest, most generous individual that I never met. Sir, do you know why they keep getting two different emails? Let's say for instance that they just wanted to get the status of one link, https://www.link1.com When they receive email, it displays like this: DMZ Server Status. Please find the status of the DMZ servers below. https://www.link1.com WORKING GET https://www.link1.com" returned OK Is there a way to remove the second email so they always get the bolded one with subject DMZ Server Status and when they open the email, they get details about the link or links? UPDATE: I *THINK* I got it! This is the part of the script that is sending the GET email ( the second email)

              static async Task ServerStatusByAsync(string url)
              {
                  HttpClient http = new();
                  using (HttpResponseMessage response = await http.GetAsync(url))
                  {
                      Console.WriteLine("GET {0}: {1}", url, response.StatusCode);
          
                      if (response.IsSuccessStatusCode)
                      {
                        //  await SendEmailAsync($"{url} WORKING", $"GET {url} returned {response.StatusCode}");
                          return true;
                      }
                     // await SendEmailAsync($"{url} DOWN", $"GET {url} returned {response.StatusCode}");
                      return false;
                  }
              }
          

          So by commenting out

          await SendEmailAsync(...)

          for both WORKING and DOWN, allows only one email: DMZ Server Status... to come through. I hope it is correct. If so, all I would need help on now is to create another copy of this same script but this time, to send email to certain group ONLY when any or all of the apps is(are) down and I swear no more changes will be needed.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          That's the correct change to remove the other email. If you only want to send the main email if any site is down, you'll need to check before sending it:

          bool[] results = await Task.WhenAll(tasks);
          if (results.Any(up => !up))
          {
          StringBuilder body = ...
          ...
          await SendEmailAsync("DMZ Server Status", body.ToString(), true);
          await Task.Delay(3000);
          }

          If you only want to send the message if all sites are down, use:

          if (results.All(up => !up))


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          S 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            That's the correct change to remove the other email. If you only want to send the main email if any site is down, you'll need to check before sending it:

            bool[] results = await Task.WhenAll(tasks);
            if (results.Any(up => !up))
            {
            StringBuilder body = ...
            ...
            await SendEmailAsync("DMZ Server Status", body.ToString(), true);
            await Task.Delay(3000);
            }

            If you only want to send the message if all sites are down, use:

            if (results.All(up => !up))


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            S Offline
            S Offline
            samflex
            wrote on last edited by
            #5

            Thank you so so so much sir. Your complete grasp of technology is truly amazing. No way I get this solution without your unflinching support and incredible patiences. May God continue to bless more.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups