示例#1
0
        private void SyncResources()
        {
            List <MonitoredResource> currentResources = GetCurrentResources();

            foreach (MonitoredResource resource in currentResources)
            {
                if (GetMonitoredResource(resource, false) == null)
                {
                    resource.FirstDiscovered = DateTime.Now;
                    _monitoredResources.Add(resource);
                }
            }

            foreach (MonitoredResource resource in new List <MonitoredResource>(_monitoredResources))
            {
                if (currentResources.Any(x => x.ResourceHash.Equals(resource.ResourceHash)))
                {
                    MonitoredResource currentResource = GetMonitoredResource(resource);
                    currentResource.LastSeen = DateTime.Now;
                    AddOrUpdateResourceList(currentResource);
                }
                else
                {
                    UpdateResourcePoweredOff(resource);
                }
            }
        }
示例#2
0
        private void UpdateResourcePoweredOff(MonitoredResource resource)
        {
            MonitoredResource r = GetMonitoredResource(resource);

            r.CurrentlyPoweredOn   = false;
            r.LastSeen             = DateTime.Now;
            r.ConsecutivePoweredOn = 0;
            r.SendEmail            = false;
            r.ExecuteAction        = false;

            AddOrUpdateResourceList(r);
        }
示例#3
0
        private MonitoredResource GetMonitoredResource(MonitoredResource resource, bool create = true)
        {
            MonitoredResource realResource = _monitoredResources.FirstOrDefault(x => x.ResourceHash.Equals(resource.ResourceHash));

            if (create && realResource == null)
            {
                AddOrUpdateResourceList(resource);
                return(GetMonitoredResource(resource, false));
            }

            return(realResource);
        }
示例#4
0
        private void AddOrUpdateResourceList(MonitoredResource resource)
        {
            if (resource.FirstDiscovered == DateTime.MinValue)
            {
                resource.FirstDiscovered = DateTime.Now;
            }

            resource.TotalDiscoveries++;

            if (_monitoredResources.Contains(resource))
            {
                _monitoredResources.Remove(resource);
            }

            _monitoredResources.Add(resource);
        }
示例#5
0
        private bool IncludeResource(MonitoredResource r, string includeFilter, string excludeFilter)
        {
            bool include = false;

            if (!string.IsNullOrEmpty(includeFilter) && Regex.IsMatch(r.Name, includeFilter, RegexOptions.IgnoreCase))
            {
                include = true;
            }

            if (!string.IsNullOrEmpty(excludeFilter) && Regex.IsMatch(r.Name, excludeFilter, RegexOptions.IgnoreCase))
            {
                include = false;
            }

            r.CurrentlyMonitored = include;
            _log.LogInformation($"IncludeResource:{r.Name} include?:{include}");
            return(include);
        }
示例#6
0
        private static string BuildMessage(MonitoredResource result)
        {
            string action = ": none";

            if (result.SendEmail)
            {
                result.SendEmail = false;
                action           = $": executed email action:{result.LastEmailSent}";
            }

            if (result.ExecuteAction)
            {
                result.ExecuteAction = false;
                action = $": executed power action:{result.LastActionExecuted}";
            }

            return(action);
        }
示例#7
0
        private List <MonitoredResource> GetCurrentResources()
        {
            List <MonitoredResource> mResources = new List <MonitoredResource>();

            foreach (GenericResource gResource in _resourceClient.Resources.List().ToList())
            {
                mResources.Add(new MonitoredResource(gResource));

                if (gResource.Type == "Microsoft.Compute/virtualMachineScaleSets")
                {
                    // add instance id resource to list as it is not enumerated by default
                    List <VmssResults> list = new List <VmssResults>();
                    list.Add((new VmssResults()
                    {
                        id = gResource.Id
                    }));
                    List <VMResults> vms = GetVmssVmResults(list);

                    foreach (VMResults result in vms)
                    {
                        MonitoredResource resource = new MonitoredResource(new GenericResource(
                                                                               result.id,
                                                                               result.name,
                                                                               result.type,
                                                                               result.location,
                                                                               null,
                                                                               null,
                                                                               null,
                                                                               null,
                                                                               null,
                                                                               null,
                                                                               null));

                        resource.InstanceId = Convert.ToInt32(Regex.Replace(result.id, ".+/", ""));
                        mResources.Add(resource);
                    }
                }
            }

            return(mResources);
        }
示例#8
0
        private MonitoredResource UpdateResourcePoweredOn(MonitoredResource resource)
        {
            MonitoredResource r = GetMonitoredResource(resource);

            _log.LogInformation($"UpdateResourcePoweredOn:{JsonConvert.SerializeObject(r, Formatting.Indented)}");
            bool wasPoweredOn = r.CurrentlyPoweredOn;

            r.CurrentlyPoweredOn = true;
            r.LastSeen           = DateTime.Now;
            r.LastSeenPoweredOn  = DateTime.Now;
            r.TotalPoweredOn++;

            if (wasPoweredOn)
            {
                r.ConsecutivePoweredOn++;
            }
            else
            {
                r.ConsecutivePoweredOn = 1;
            }

            if (r.ConsecutivePoweredOn >= _consecutivePoweredOnEmailCount)
            {
                r.LastEmailSent = DateTime.Now;
                r.SendEmail     = true;
                _log.LogWarning($"resource powered on. sending email:\r\n{JsonConvert.SerializeObject(r, Formatting.Indented)}");
            }

            if (r.ConsecutivePoweredOn >= _consecutivePoweredOnActionCount)
            {
                r.LastActionExecuted = DateTime.Now;
                r.ExecuteAction      = true;
                _log.LogWarning($"resource powered on. executing action:\r\n{JsonConvert.SerializeObject(r, Formatting.Indented)}");
            }

            AddOrUpdateResourceList(r);
            return(r);
        }
示例#9
0
        private bool CheckVmssPowerStates()
        {
            bool retval = false;
            List <VmssResults>       allResults         = GetVirtualMachineScaleSets();
            List <VMResults>         vmssVmResults      = GetVmssVmResults(allResults);
            List <MonitoredResource> vmssRunningResults = new List <MonitoredResource>();

            foreach (VMResults result in vmssVmResults)
            {
                //GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/virtualmachines/{instanceId}/instanceView?api-version=2017-12-01
                Uri    instanceUri               = new Uri($"{_baseUri}{result.id}/instanceView?api-version={_virtualMachineApiVersion}");
                string instanceResponse          = GET(instanceUri.ToString());
                VmssVMInstanceResults vmInstance = JsonConvert.DeserializeObject <VmssVMInstanceResults>(instanceResponse);
                vmInstance.id = instanceUri.AbsolutePath;

                _log.LogInformation($"vm instance view: {JsonConvert.SerializeObject(vmInstance, Formatting.Indented)}");
                MonitoredResource mresource = new MonitoredResource(new GenericResource(result.id, result.name, result.type));

                string pattern = @".+/(\d+)$";
                if (Regex.IsMatch(result.id, pattern))
                {
                    mresource.InstanceId = Convert.ToInt32(Regex.Match(result.id, pattern).Groups[1].Value);
                }

                if (vmInstance.statuses.Count(x => x.code.Contains("running")) > 0)
                {
                    MonitoredResource r = UpdateResourcePoweredOn(mresource);

                    if (IncludeResource(r, _vmssIncludeFilter, _vmssExcludeFilter))
                    {
                        if (r.SendEmail)
                        {
                            vmssRunningResults.Add(r);
                        }

                        if (r.ExecuteAction)
                        {
                            // send post to turn off vm
                            // POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/virtualmachines/{instanceId}/deallocate?api-version=2017-12-01
                            string aresult = POST($"{_baseUri}{result.id}/deallocate", null, _virtualMachineApiVersion);
                        }
                    }
                }
                else
                {
                    UpdateResourcePoweredOff(mresource);
                }
            }

            if (vmssRunningResults.Count > 0)
            {
                retval = true;
                _msgBuilder.AppendLine($"<b>there are running vm scaleset instances in subscription:</b>");
                _msgBuilder.AppendLine();

                foreach (MonitoredResource result in vmssRunningResults)
                {
                    string action = BuildMessage(result);

                    _msgBuilder.AppendLine($"<p>resource: {result.Id}</p>");
                    _msgBuilder.AppendLine($"<p>instance: {result.InstanceId}{action}</p>");
                }

                _log.LogWarning(_msgBuilder.ToString());
            }

            return(retval);
        }
示例#10
0
        private bool CheckVmPowerStates()
        {
            //GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Compute/virtualMachines?api-version=2017-12-01
            //GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/instanceView?api-version=2017-12-01
            bool                     retval           = false;
            List <VMResults>         allResults       = new List <VMResults>();
            List <MonitoredResource> vmRunningResults = new List <MonitoredResource>();

            string response = GET("providers/Microsoft.Compute/virtualMachines", null, _virtualMachineApiVersion);

            while (true)
            {
                if (string.IsNullOrEmpty(response))
                {
                    _log.LogError("CheckVmPowerStates:ERROR:null response");
                    return(false);
                }

                allResults.AddRange(JsonConvert.DeserializeObject <VMResults[]>(JObject.Parse(response)["value"].ToString()));

                if (Regex.IsMatch(response, "nextLink"))
                {
                    response = GET(JObject.Parse(response)["nextLink"].ToString());
                }
                else
                {
                    break;
                }
            }

            _log.LogInformation($"vm results count: {allResults.Count}");

            foreach (VMResults result in allResults)
            {
                response = GET($"{_baseUri}{result.id}/instanceView?api-version={_virtualMachineApiVersion}");
                VMInstanceResults instance  = JsonConvert.DeserializeObject <VMInstanceResults>(response);
                MonitoredResource mresource = new MonitoredResource(new GenericResource(result.id, result.name, result.type));

                if (instance.statuses.Count(x => x.code.Contains("running")) > 0)
                {
                    MonitoredResource r = UpdateResourcePoweredOn(mresource);

                    if (IncludeResource(r, _vmIncludeFilter, _vmExcludeFilter))
                    {
                        if (r.SendEmail)
                        {
                            vmRunningResults.Add(r);
                        }

                        if (r.ExecuteAction)
                        {
                            // send post to turn off vm
                            string aresult = POST($"{_baseUri}{result.id}/deallocate", null, _virtualMachineApiVersion);
                        }
                    }
                }
                else
                {
                    UpdateResourcePoweredOff(mresource);
                }

                _log.LogInformation($"vm instance view: {JsonConvert.SerializeObject(instance, Formatting.Indented)}");
            }

            if (vmRunningResults.Count > 0)
            {
                _msgBuilder.AppendLine($"<b>there are running vm's in subscription</b>");
                retval = true;

                foreach (MonitoredResource result in vmRunningResults)
                {
                    _msgBuilder.AppendLine($"<p>{result.Name}{BuildMessage(result)}</p>");
                }

                _log.LogWarning($"{_msgBuilder.ToString()}");
            }

            return(retval);
        }
示例#11
0
        private bool CheckKustoPowerStates()
        {
            bool retval = false;
            List <KustoClusterResults> allResults          = new List <KustoClusterResults>();
            List <MonitoredResource>   kustoRunningResults = new List <MonitoredResource>();
            List <string> kustoClusterIds = new List <string>();

            string response = GET("providers/Microsoft.Kusto/clusters", null, _kustoApiVersion);

            while (true)
            {
                if (string.IsNullOrEmpty(response))
                {
                    _log.LogError("CheckKustoPowerStates:ERROR:null response");
                    return(false);
                }

                KustoClusterResults[] clusterResults = JsonConvert.DeserializeObject <KustoClusterResults[]>(JObject.Parse(response)["value"].ToString());
                _log.LogInformation($"{JsonConvert.SerializeObject(clusterResults, Formatting.Indented)}");

                allResults.AddRange(clusterResults);

                if (Regex.IsMatch(response, "nextLink"))
                {
                    response = GET(JObject.Parse(response)["nextLink"].ToString());
                }
                else
                {
                    break;
                }
            }

            _log.LogInformation($"kusto cluster results count: {allResults.Count}");

            // check properties/state to see if stopped or started
            foreach (KustoClusterResults result in allResults)
            {
                MonitoredResource mresource = new MonitoredResource(new GenericResource(result.id, result.name, result.type));

                if (!result.properties.state.Contains("Stopped"))
                {
                    MonitoredResource r = UpdateResourcePoweredOn(mresource);

                    if (IncludeResource(r, _kustoIncludeFilter, _kustoExcludeFilter))
                    {
                        if (r.SendEmail)
                        {
                            kustoRunningResults.Add(r);
                        }

                        if (r.ExecuteAction)
                        {
                            // send post to turn off cluster
                            string aresult = POST($"{_baseUri}{r.Id}/stop", null, _kustoApiVersion);
                        }
                    }

                    _log.LogWarning($"running kusto cluster {result}");
                }
                else
                {
                    UpdateResourcePoweredOff(mresource);
                }
            }

            if (kustoRunningResults.Count > 0)
            {
                _msgBuilder.AppendLine($"<b>there are running kusto clusters in subscription</b>");

                retval = true;

                foreach (MonitoredResource result in kustoRunningResults)
                {
                    _msgBuilder.AppendLine($"<p>{result.Name}{BuildMessage(result)}</p>");
                }

                _log.LogWarning($"{_msgBuilder.ToString()}");
            }
            else
            {
                _log.LogInformation("there are *no* running kusto clusters in subscription");
            }

            return(retval);
        }