示例#1
0
        public async Task <ActionResult <List <NodeStatusVM> > > Status()
        {
            var lastStatus = (await nodeStatusCol.Aggregate()
                              .SortByDescending(s => s.Id)
                              .Group("{_id: \"$" + nameof(NodeStatusRange.NodeId) + "\", Last: {$first: \"$$ROOT\"}}")
                              .As <LastNodeStatusRange>().ToListAsync())
                             .Select(x => Mapper.Map <NodeStatusVM>(x.Last))
                             .ToList();

            var nodes = (await nodesCol.AllAsync()).ToDictionary(k => k.Id);

            foreach (var item in lastStatus)
            {
                if (nodes.ContainsKey(item.NodeId))
                {
                    item.Address = nodes[item.NodeId].Address;
                    item.Name    = nodes[item.NodeId].Name;
                }
            }
            return(lastStatus.Where(i => i.Name != null).OrderBy(i => i.NodeId).ToList());
        }
示例#2
0
 public async Task <ActionResult <List <NetworkNode> > > List()
 {
     return((await nodesCol.AllAsync()).ToList());
 }
示例#3
0
 public async Task <ActionResult <List <string> > > GlobalAlertReceivers()
 {
     return((await alertReceiversCol.AllAsync()).Select(a => a.Address).ToList());
 }
示例#4
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(1000, stoppingToken);

                var nodes = nodesCol.All();
                foreach (var node in nodes)
                {
                    if (stoppingToken.IsCancellationRequested)
                    {
                        break;
                    }

                    var lastStatus = statusCol.Find(s => s.NodeId == node.Id).SortByDescending(s => s.LastTime).FirstOrDefault(cancellationToken: stoppingToken);

                    if (node.CheckMechanism == NetworkNode.CheckMechanismEnum.Ping)
                    {
                        Ping p = new Ping();
                        try
                        {
                            if (lastStatus == null)
                            {
                                lastStatus = NewStatus(node.Id);
                            }
                            var reply = await p.SendPingAsync(node.Address, 2000);

                            if (lastStatus.IpStatus != reply.Status)
                            {
                                lastStatus = NewStatus(node.Id);
                            }
                            lastStatus.IpStatus = reply.Status;
                        }
                        catch (Exception ex)
                        {
                            lastStatus.ErrorMessage = GetErrorMessages(ex);
                        }
                    }
                    else if (node.CheckMechanism == NetworkNode.CheckMechanismEnum.Http)
                    {
                        var httpClient = httpClientFactory.CreateClient();
                        try
                        {
                            string address = node.Address;
                            if (!address.StartsWith("http"))
                            {
                                address = "http://" + node.Address;
                            }
                            if (lastStatus == null)
                            {
                                lastStatus = NewStatus(node.Id);
                            }
                            var reply = await httpClient.GetAsync(address, stoppingToken);

                            if (lastStatus.HttpStatus != reply.StatusCode)
                            {
                                lastStatus = NewStatus(node.Id);
                            }
                            lastStatus.HttpStatus = reply.StatusCode;
                        }
                        catch (Exception ex)
                        {
                            lastStatus.ErrorMessage = GetErrorMessages(ex);
                        }
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }

                    // Send alert if required:

                    if (!lastStatus.IsSuccess &&
                        (lastStatus.AlertSendTime == null || DateTime.Now - lastStatus.AlertSendTime > TimeSpan.FromMinutes(alertResendMinutes)) &&
                        DateTime.Now - lastStatus.StartTime > TimeSpan.FromSeconds(errorWaitSeconds))
                    {
                        var content   = alertContent.Replace("{nodeName}", node.Name);
                        var receivers = new HashSet <string>();
                        if (node.AlertReceivers != null)
                        {
                            foreach (var dest in node.AlertReceivers)
                            {
                                if (!receivers.Contains(dest))
                                {
                                    await alertSender.Send(node.Id, dest, content);

                                    receivers.Add(dest);
                                }
                            }
                        }
                        foreach (var dest in (await alertReceiversCol.AllAsync()).Select(r => r.Address))
                        {
                            if (!receivers.Contains(dest))
                            {
                                await alertSender.Send(node.Id, dest, content);

                                receivers.Add(dest);
                            }
                        }
                        lastStatus.AlertSendTime = DateTime.Now;
                    }

                    lastStatus.LastTime = DateTime.Now;
                    await statusCol.SaveAsync(lastStatus);
                }
            }
        }