3

I have the following Azure Function that fails with Access is Denied (ignore the fact that the logic is all weird, I'm just doing a first test)

public static void Run(TimerInfo myTimer, ILogger log)
{
    List<string> servers = new List<string>()
    {
        "server1"
    };

    foreach(string server in servers)
    {
        if (!Ping(server))
        {
            SendEmail($"Server {server} seems down.", log);
        }
    }
}

static bool Ping(string hostName)
{
    Ping pingSender = new Ping();
    int timeout = 120;
    PingReply reply = pingSender.Send(hostName, timeout);
    return reply.Status == IPStatus.Success;
}

static void SendEmail(string message, ILogger log)
{
    log.LogInformation(message);
}

If I change the lines

PingReply reply = pingSender.Send(hostName, timeout);
return reply.Status == IPStatus.Success;

to return true; for the sake of testing, the function runs well.

What do I need to configure so the function can do the ping?

PedroC88
  • 3,708
  • 7
  • 43
  • 77
  • Are you sure the server you are pinging responds to ping packets? – Scott Chamberlain Aug 22 '19 at 03:27
  • Part of the testing is to ensure they respond from Azure but, if they don't respond, the function should still run and log it in the `SendEmail` method instead of giving me an `Access is Denied` error. Shouldn't it? – PedroC88 Aug 22 '19 at 03:49

1 Answers1

6

As far as I know, we can't do ping operation in Azure function successful because ICMP protocol is not permitted in Azure. But we can do tcpping in it. You can test it in Azure function console(shown as screenshot below):

enter image description here We can also install some tools to do ping operation, such as PsPing, Nmap, or Telnet.

Here is the updating:

According to some research, I think Azure Function can meet your requirements.

First, we should install psping. You can download it on this page:https://learn.microsoft.com/zh-cn/sysinternals/downloads/psping#installation

Then unzip the psping file and open Kudu in your Azure function.

enter image description here

Then click "Debug console" --> "CMD" --> "site", new a folder named "tools", click "tools" and drag your psping file(PSTools) to "tools" folder.

After that, please refer to the code I post below

enter image description here

If ping success, the variable "err" in my code will show nothing. If ping fail, it will show the error. So you can judge success based on it.

enter image description here

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • I'm new to Azure Functions and I'm unaware how I could set up console commands to run on a schedule. Would that be possible? The goal is to have the function ping a list of servers and send me an email if the server doesn't respond. – PedroC88 Aug 22 '19 at 03:46
  • @PedroC88 Have a look at TimeTriggered functions and use all nuget Hury mentioned to perform the task https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer – HariHaran Aug 22 '19 at 06:01
  • Hi @PedroC88, I have updated my answer, please refer to it. – Hury Shen Aug 22 '19 at 08:48
  • @HuryShen thanks, will take a look at it as soon as I have the chance today! – PedroC88 Aug 22 '19 at 13:33
  • Thanks @HuryShen it worked. Can I ask you to make two changes to your answer? 1.- Explain how to get the tool on the function directory (I got this working with a lot of trouble, perhaps you have an easier approach). and 2.- Edit the answer to clarify that `err` is never null, I used string.IsNullOrEmpty() to check for errors – PedroC88 Aug 22 '19 at 17:43