×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How do I modify my script to add foreground and background colors to URL with DOWN message?

How do I modify my script to add foreground and background colors to URL with DOWN message?

How do I modify my script to add foreground and background colors to URL with DOWN message?

(OP)
Greetings mates,

I have not been back here for a long time.

I have written a script that monitors six (6) apps on our DMZ servers.

The script is scheduled to run three times a day.

Each time it 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.

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:
link1: AttachMate solutions Links.com" target="_blank">https://www.link1: AttachMate solutions Links.com 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:
link1: AttachMate solutions Links.com" target="_blank">https://www.link1: AttachMate solutions Links.com WORKING
https://www.link2.com WORKING
https://www.link3.com DOWN
https://www.link4.com WORKING

It's weird but I could not get this to work.

Any thought on 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.

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<int> Main(string[] args)
        {
            System.Collections.Concurrent.ConcurrentDictionary<string, string> urlToStatus = new();

             IEnumerable < Task<bool> > 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 status of the DMZ servers below:");
            foreach (var kvp in urlToStatus)
            {
                body.AppendLine();
                body.AppendFormat("{0}: {1}", kvp.Key, kvp.Value);
            }

            await SendEmailAsync("DMZ Server Status", body.ToString());
            await Task.Delay(3000);

            // Return the number of servers which were down:
            return results.Count(result => !result);
        }
        static async Task<bool> 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;
            }
        }

        static async Task SendEmailAsync(string subject, string body)
        {
            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 = false;

            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);
        }
    }
} 

Thanks much in advance.

RE: How do I modify my script to add foreground and background colors to URL with DOWN message?

Not sure about the proper way of doing it. To get a red background with yellow text, the HTML needs to be

CODE

<span style="background-color:red; color:yellow">DOWN</style> 
I don't know whether just changing the text will work or whether you need to add an HtmlGenericControl("span"). Anyway, something for you to try out.

RE: How do I modify my script to add foreground and background colors to URL with DOWN message?

(OP)
Thanks for your response.

However, I have already resolved this.

I actually came here last night to delete this thread because even though what guys provide here is free service, this one took way too long to get a response but could not see an option to delete.

BTW: It took a whole lot more than that solution you provided to get it to work.

Also, while I am at it, how on earth does someone reply to a thread?

I tried reply to a thread with solution but could not get the screen to respond.

I know this is not related to my thread but I thought one of the moderators could see this and perhaps let me know why I could not provide solution any OP's post.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close