Ejemplo n.º 1
0
        public static IEnumerable <Url> GetUrls(
            this AppCmd appCmd)
        {
            foreach (var site in appCmd.GetLines("list sites /text:name"))
            {
                foreach (var binding in appCmd.GetLines($"list site \"{site}\" /text:bindings").SelectMany(s => s.Split(',')))
                {
                    foreach (var app in appCmd.GetLines($"list app /site.name:\"{site}\" /text:path"))
                    {
                        Url url;

                        try
                        {
                            url = new Url(site, binding, app);
                        }

                        catch
                        {
                            continue;
                        }

                        yield return(url);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static IEnumerable <string> GetHosts(
            this AppCmd appCmd)
        {
            foreach (var binding in appCmd.GetLines("list sites /text:bindings")
                     .SelectMany(s => s.Split(',')))
            {
                var match = Regex.Match(binding, @"(https?)/(.*):(\d+):(?<host>.*)");

                yield return(match.Groups["host"].Value);
            }
        }
Ejemplo n.º 3
0
        static void Main()
        {
            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

            var appCmd = new AppCmd(ConfigurationManager.AppSettings["AppCmd"]);

            var skip = ((MonitorConfigurationSection)ConfigurationManager.GetSection("monitor")).Skip
                       .OfType <MonitorSkip>()
                       .Select(s => new Uri(s.Uri))
                       .ToList();

            var errors = File.Exists("Url.err")
                ? File.ReadAllLines("Url.err").Select(s => new Uri(s)).ToList()
                : new List <Uri>();

            var errorWriter = new StreamWriter("Url.err", false);
            var outWriter   = new StreamWriter("Out.log", true);

            var parallelOptions = new ParallelOptions
            {
                MaxDegreeOfParallelism = int.Parse(ConfigurationManager.AppSettings["MaxDegreeOfParallelism"])
            };

            var hosts = appCmd.GetHosts().ToList();

            Parallel.ForEach(appCmd.GetUrls().Where(w => !skip.Contains(w.RemoteUri)), parallelOptions, url =>
            {
                try
                {
                    var status = url.GetStatus(hosts);

                    Console.WriteLine("{0}\t{1}\t{2}\t{3}({4})", DateTime.Now, url.Name, url.RemoteUri, status.Description, status.Code);

                    if (!status.Success)
                    {
                        lock (outWriter)
                        {
                            outWriter.WriteLine("{0}\t{1}\t{2}\t{3}({4})", DateTime.Now, url.Name, url.RemoteUri, status.Description, status.Code);
                            outWriter.Flush();
                        }

                        lock (errorWriter)
                        {
                            errorWriter.WriteLine(url.RemoteUri);
                            errorWriter.Flush();
                        }

                        if (!errors.Contains(url.RemoteUri))
                        {
                            SendNotification(status);
                        }
                    }
                }

                catch (Exception e)
                {
                    var status = e.ToSiteStatus(url);

                    Console.WriteLine("{0}\t{1}\t{2}\t{3}({4})", DateTime.Now, url.Name, url.RemoteUri, status.Description, status.Code);

                    lock (outWriter)
                    {
                        outWriter.WriteLine("{0}\t{1}\t{2}\t{3}({4})", DateTime.Now, url.Name, url.RemoteUri, status.Description, status.Code);
                        outWriter.Flush();
                    }

                    lock (errorWriter)
                    {
                        errorWriter.WriteLine(url.RemoteUri);
                        errorWriter.Flush();
                    }

                    if (!errors.Contains(url.RemoteUri))
                    {
                        SendNotification(status);
                    }
                }
            });

            outWriter.Close();
            errorWriter.Close();
        }