Click here to Skip to main content
15,867,453 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming25-Aug-21 6:18
mveRichard Deeming25-Aug-21 6:18 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex25-Aug-21 8:23
samflex25-Aug-21 8:23 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming25-Aug-21 22:12
mveRichard Deeming25-Aug-21 22:12 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex26-Aug-21 3:04
samflex26-Aug-21 3:04 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming26-Aug-21 3:52
mveRichard Deeming26-Aug-21 3:52 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex26-Aug-21 4:04
samflex26-Aug-21 4:04 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex26-Aug-21 5:08
samflex26-Aug-21 5:08 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming26-Aug-21 5:19
mveRichard Deeming26-Aug-21 5:19 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex26-Aug-21 6:41
samflex26-Aug-21 6:41 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming26-Aug-21 21:28
mveRichard Deeming26-Aug-21 21:28 
Looks like the reply.Options isn't set. You will need to test for null, or use a null-conditional operator:
C#
if (reply.Options != null)
{
    Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
    Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
}
...
SendEmail($"{site} Up", $@"Ping {site}
IP Address: {reply.Address}
RoundTrip time: {reply.RoundtripTime}
Time to live: {reply.Options?.Ttl}
Don't fragment: {reply.Options?.DontFragment}
Buffer size: {reply.Buffer?.Length}");


samflex wrote:
For instance, none of these worked:

https://gis.massdot.state.ma.us/arcgis/rest
gis.massdot.state.ma.us/arcgis/rest/
gis.massdot.state.ma.us/arcgis/rest
You're pinging a server, not a URL. It looks like gis.massdot.state.ma.us either isn't up, or is blocking pings.

If you actually want to test a URL, you'd need to use a different approach. For example:
C#
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);
        urlToStatus.TryAdd(url, result ? "UP" : "DOWN");
    });
    
    await Task.WhenAll(tasks);
    
    StringBuilder body = new("Please find the status of the servers below:");
    foreach (var kvp in urlToStatus)
    {
        body.AppendLine();
        body.AppendFormat("{0}: {1}", kvp.Key, kvp.Value);
    }
    
    await SendEmailAsync("Server Status", body.ToString());
    await Task.Delay(3000);
}

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@yahoo.com");
    mm.To.Add("johndoe@gmail.com");
    mm.CC.Add("joesixpack@yahoo.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);
}
This has the added advantage of testing all URLs at once, rather than one-by-one, which should reduce the time it takes to test multiple URLs.



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

GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex27-Aug-21 3:21
samflex27-Aug-21 3:21 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming27-Aug-21 4:10
mveRichard Deeming27-Aug-21 4:10 
General(SOLVED) Re: I am having problem creating a script to monitor Rest/API services. Pin
samflex27-Aug-21 5:27
samflex27-Aug-21 5:27 
QuestionBest, cheap, ASP.NET Core hosting? Pin
Chris Maunder4-Aug-21 8:36
cofounderChris Maunder4-Aug-21 8:36 
AnswerRe: Best, cheap, ASP.NET Core hosting? Pin
jkirkerx11-Aug-21 11:15
professionaljkirkerx11-Aug-21 11:15 
GeneralRe: Best, cheap, ASP.NET Core hosting? Pin
Chris Maunder11-Aug-21 11:56
cofounderChris Maunder11-Aug-21 11:56 
GeneralRe: Best, cheap, ASP.NET Core hosting? Pin
jkirkerx11-Aug-21 12:50
professionaljkirkerx11-Aug-21 12:50 
AnswerRe: Best, cheap, ASP.NET Core hosting? Pin
Deepak Vasudevan13-Sep-21 21:42
Deepak Vasudevan13-Sep-21 21:42 
GeneralRe: Best, cheap, ASP.NET Core hosting? Pin
Chris Maunder14-Sep-21 4:38
cofounderChris Maunder14-Sep-21 4:38 
AnswerRe: Best, cheap, ASP.NET Core hosting? Pin
Moo v This17-Nov-21 2:53
Moo v This17-Nov-21 2:53 
Questionhow to run default page as login page in web app blazor Pin
Member 1129177422-Jul-21 2:54
Member 1129177422-Jul-21 2:54 
Questionncaught ReferenceError:System is not defined at (index):18 dx.light.css:1 Failed to load resource:the server responded with a status of 404(Not Found) Pin
Member 146901009-Jul-21 7:15
Member 146901009-Jul-21 7:15 
AnswerRe: ncaught ReferenceError:System is not defined at (index):18 dx.light.css:1 Failed to load resource:the server responded with a status of 404(Not Found) Pin
SeeSharp29-Jul-21 7:38
SeeSharp29-Jul-21 7:38 
JokeEste site aceita link? Pin
Ronaldo Luis Gonçalves6-Jul-21 3:37
Ronaldo Luis Gonçalves6-Jul-21 3:37 
AnswerRe: Este site aceita link? Pin
SeeSharp29-Jul-21 8:05
SeeSharp29-Jul-21 8:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.