C# 检查ubuntu服务是否已在c中停止的最佳方法#

C# 检查ubuntu服务是否已在c中停止的最佳方法#,c#,azure,ubuntu-16.04,C#,Azure,Ubuntu 16.04,我想在Azure虚拟机(Ubuntu 16.04)中的服务(grafana或XDB)停止时收到警报。我想使用c#连接到VM并检查grafana和XDB服务的状态。任何人都可以共享实现此功能的代码示例吗?这里有一些东西可以用来在c中使用SSH连接到Azure linux# 使用(var client=new SshClient(“my vm.cloudapp.net”,22,“用户名”,“密码​")) { client.Connect(); Console.WriteLine(“成功了!”);

我想在Azure虚拟机(Ubuntu 16.04)中的服务(grafana或XDB)停止时收到警报。我想使用c#连接到VM并检查grafana和XDB服务的状态。任何人都可以共享实现此功能的代码示例吗?

这里有一些东西可以用来在c中使用SSH连接到Azure linux#

使用(var client=new SshClient(“my vm.cloudapp.net”,22,“用户名”,“密码​"))
{
client.Connect();
Console.WriteLine(“成功了!”);
client.Disconnect();
Console.ReadLine();

}
这两种服务都提供运行状况端点,可用于从远程服务器检查其状态。无需打开远程shell连接。事实上,如果必须通过SSH连接到每个服务器场,则无法监控大型服务器场

在最简单的情况下,忽略网络问题,只需点击健康端点即可检查两个服务的状态。粗略的实现可能如下所示:

public async Task<bool> CheckBoth()
{
    var client = new HttpClient
    {
        Timeout = TimeSpan.FromSeconds(30)
    };

    const string grafanaHealthUrl = "https://myGrafanaURL/api/health";
    const string influxPingUrl = "https://myInfluxURL/ping";

    var (grafanaOK, grafanaError) = await CheckAsync(client, grafanaHealthUrl,
                                                     HttpStatusCode.OK, "Grafana error");
    var (influxOK, influxError) = await CheckAsync(client, influxPingUrl, 
                                                   HttpStatusCode.NoContent,"InfluxDB error");

    if (!influxOK || !grafanaOK)
    {
                //Do something with the errors
                return false;
    }
    return true;

}

public async Task<(bool ok, string result)> CheckAsync(HttpClient client,
                                                       string healthUrl, 
                                                       HttpStatusCode expected,
                                                       string errorMessage)
{
    try
    {
        var status = await client.GetAsync(healthUrl);
        if (status.StatusCode != expected)
        {
            //Failure message, get it and log it
            var statusBody = await status.Content.ReadAsStringAsync();
            //Possibly log it ....
            return (ok: false, result: $"{errorMessage}: {statusBody}");
        }
    }
    catch (TaskCanceledException)
    {
        return (ok: false, result: $"{errorMessage}: Timeout");
    }
    return (ok: true, "");
}
public异步任务CheckBoth()
{
var client=新的HttpClient
{
超时=时间跨度。从秒(30)
};
常量字符串grafanahealthull=”https://myGrafanaURL/api/health";
常量字符串infloxpingurl=”https://myInfluxURL/ping";
var(grafanaOK,grafanaError)=等待CheckAsync(客户端,grafanahealthull,
HttpStatusCode.OK,“Grafana错误”);
var(influxOK,influxError)=等待CheckAsync(客户端,influxPingUrl,
HttpStatusCode.NoContent,“InfluxDB错误”);
如果(!influxOK | | |!grafanaOK)
{
//对这些错误做点什么
返回false;
}
返回true;
}
公共异步任务CheckAsync(HttpClient客户端,
字符串healthUrl,
应为HttpStatusCode,
字符串错误消息)
{
尝试
{
var status=await client.GetAsync(healthUrl);
如果(status.StatusCode!=预期)
{
//失败消息,获取并记录它
var statusBody=await status.Content.ReadAsStringAsync();
//可能会记录下来。。。。
返回(确定:false,结果:$“{errorMessage}:{statusBody}”);
}
}
捕获(TaskCanceledException)
{
返回(ok:false,结果:$“{errorMessage}:Timeout”);
}
返回(ok:true,“”);
}

也许更好的解决方案是使用Azure Monitor定期发送警报,如果它们停机。

请告诉我们您尝试了哪些选项您不需要自己连接和检查。Azure Monitoring可以从任何Linux虚拟机收集度量、服务状态等。Grafana具有可用于监视hea的插件lth及其行为。Azure Monitor可以和InfluxDB提供。如果没有更好的解决方案,您可以添加一个通用的ping测试。您可以配置警报并在服务出现故障时立即收到通知,您可以调用Azure Monitor查看服务的状态,或者,在最坏的情况下,您可以从代码中调用
ping
端点。Thi必须以某种方式保护s,以防止黑客将服务ping死,例如只允许从特定IP访问。哦,Grafana插件用于使用Azure Monitor作为源。Grafana。因此,在最简单的情况下,可以编写一个C程序,只定期调用两个服务的健康URL。这不是打赌吗ter使用Azure healtcheck服务来检查grafana和influxdb的状态?是的,这是可行的,但用户需要c#代码来检查状态。这些服务可以使用任何语言通过HTTP进行检查。在任何情况下,连接到VM都不会检查服务的状态。是的,这就是我在替代解决方案中提到的:)例如,当日志分析已经有Linux代理时,为什么要编写和部署代理呢?用户可以通过HTTP查询日志数据以查看发生了什么,尽管最好配置一个在服务停止时触发的警报。我仍然需要检查在我的情况下,使用Azure Monitor是否是一个更好的解决方案,但代码示例可以工作这是给我的。非常感谢你的帮助!!!