예제 #1
0
        public ActionResult Get(string name, string format = "json")
        {
            if (format.ToLower() == "html")
            {
                return(Content(GetHtml(name), "text/html"));
            }

            // Note: FO produces Json output for health report messages/logs..
            ContentResult ret = Content(string.Empty);

            string networkObserverLogText = null, osObserverLogText = null, nodeObserverLogText = null, appObserverLogText = null, fabricSystemObserverLogText = null, diskObserverLogText = null;
            string logFolder;

            try
            {
                System.Fabric.Description.ConfigurationSettings configSettings = this.serviceContext.CodePackageActivationContext.GetConfigurationPackageObject("Config").Settings;
                logFolder = Utilities.GetConfigurationSetting(configSettings, "FabricObserverLogs", "ObserverLogBaseFolderPath");

                if (!Directory.Exists(logFolder))
                {
                    throw new ArgumentException($"Specified log folder, {logFolder}, does not exist.");
                }
            }
            catch (Exception e)
            {
                ret = Content(e.ToString());
                return(ret);
            }

            // Observer log paths.
            string appObserverLogPath          = Path.Combine(logFolder, "AppObserver", "AppObserver.log");
            string osObserverLogPath           = Path.Combine(logFolder, "OSObserver", "OSObserver.log");
            string diskObserverLogPath         = Path.Combine(logFolder, "DiskObserver", "DiskObserver.log");
            string networkObserverLogPath      = Path.Combine(logFolder, "NetworkObserver", "NetworkObserver.log");
            string fabricSystemObserverLogPath = Path.Combine(logFolder, "FabricSystemObserver", "FabricSystemObserver.log");
            string nodeObserverLogPath         = Path.Combine(logFolder, "NodeObserver", "NodeObserver.log");

            // Implicit retry loop. Will run only once if no exceptions arise.
            // Can only run at most MaxRetries times.
            for (int i = 0; i < MaxRetries; i++)
            {
                try
                {
                    if (System.IO.File.Exists(appObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(appObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        appObserverLogText = System.IO.File.ReadAllText(appObserverLogPath, Encoding.UTF8);
                    }

                    if (System.IO.File.Exists(diskObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(diskObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        diskObserverLogText = System.IO.File.ReadAllText(diskObserverLogPath, Encoding.UTF8);
                    }

                    if (System.IO.File.Exists(fabricSystemObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(fabricSystemObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        fabricSystemObserverLogText = System.IO.File.ReadAllText(fabricSystemObserverLogPath, Encoding.UTF8);
                    }

                    if (System.IO.File.Exists(networkObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(networkObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        networkObserverLogText = System.IO.File.ReadAllText(networkObserverLogPath, Encoding.UTF8);
                    }

                    if (System.IO.File.Exists(nodeObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(nodeObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        nodeObserverLogText = System.IO.File.ReadAllText(nodeObserverLogPath, Encoding.UTF8);
                    }

                    if (System.IO.File.Exists(osObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(osObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        osObserverLogText = System.IO.File.ReadAllText(osObserverLogPath, Encoding.UTF8);
                    }

                    string reportItems = string.Empty;

                    switch (name.ToLower())
                    {
                    case "appobserver":
                        reportItems = GetObserverErrWarnLogEntryListFromLogText(appObserverLogText);
                        break;

                    case "diskobserver":
                        reportItems = GetObserverErrWarnLogEntryListFromLogText(diskObserverLogText);
                        break;

                    case "fabricsystemobserver":
                        reportItems = GetObserverErrWarnLogEntryListFromLogText(fabricSystemObserverLogText);
                        break;

                    case "networkobserver":
                        reportItems = GetObserverErrWarnLogEntryListFromLogText(networkObserverLogText);
                        break;

                    case "nodeobserver":
                        reportItems = GetObserverErrWarnLogEntryListFromLogText(nodeObserverLogText);
                        break;

                    case "osobserver":
                        reportItems = GetObserverErrWarnLogEntryListFromLogText(osObserverLogText);
                        break;

                    default:
                        return(Json("Specified Observer, " + name + ", does not exist."));
                    }

                    ret = Content(reportItems);
                    break;
                }
                catch (IOException)
                {
                }

                // If we get here, let's wait a second before the next iteration.
                Task.Delay(1000).Wait();
            }

            return(ret);
        }
예제 #2
0
        private string GetHtml(string name)
        {
            string html = string.Empty;
            string logFolder;
            string nodeName = this.serviceContext.NodeContext.NodeName;

            System.Fabric.Description.ConfigurationSettings configSettings = this.serviceContext.CodePackageActivationContext.GetConfigurationPackageObject("Config").Settings;
            string networkObserverLogText = null, osObserverLogText = null, nodeObserverLogText = null, appObserverLogText = null, fabricSystemObserverLogText = null, diskObserverLogText = null;

            logFolder = Utilities.GetConfigurationSetting(configSettings, "FabricObserverLogs", "ObserverLogBaseFolderPath");

            // Observer log paths.
            string appObserverLogPath = Path.Combine(logFolder, "AppObserver", "AppObserver.log");
            string osObserverLogPath = Path.Combine(logFolder, "OSObserver", "OSObserver.log");
            string diskObserverLogPath = Path.Combine(logFolder, "DiskObserver", "DiskObserver.log");
            string networkObserverLogPath = Path.Combine(logFolder, "NetworkObserver", "NetworkObserver.log");
            string fabricSystemObserverLogPath = Path.Combine(logFolder, "FabricSystemObserver", "FabricSystemObserver.log");
            string nodeObserverLogPath = Path.Combine(logFolder, "NodeObserver", "NodeObserver.log");

            // Implicit retry loop. Will run only once if no exceptions arise.
            // Can only run at most MaxRetries times.
            for (int i = 0; i < MaxRetries; i++)
            {
                try
                {
                    if (System.IO.File.Exists(appObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(appObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        appObserverLogText = System.IO.File.ReadAllText(appObserverLogPath, Encoding.UTF8).Replace(Environment.NewLine, "<br/>");
                    }

                    if (System.IO.File.Exists(diskObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(diskObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        diskObserverLogText = System.IO.File.ReadAllText(diskObserverLogPath, Encoding.UTF8).Replace(Environment.NewLine, "<br/>");
                    }

                    if (System.IO.File.Exists(fabricSystemObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(fabricSystemObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        fabricSystemObserverLogText = System.IO.File.ReadAllText(fabricSystemObserverLogPath, Encoding.UTF8).Replace(Environment.NewLine, "<br/>");
                    }

                    if (System.IO.File.Exists(networkObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(networkObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        networkObserverLogText = System.IO.File.ReadAllText(networkObserverLogPath, Encoding.UTF8).Replace(Environment.NewLine, "<br/>");
                    }

                    if (System.IO.File.Exists(nodeObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(nodeObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        nodeObserverLogText = System.IO.File.ReadAllText(nodeObserverLogPath, Encoding.UTF8).Replace(Environment.NewLine, "<br/>");
                    }

                    if (System.IO.File.Exists(osObserverLogPath) &&
                        System.IO.File.GetCreationTimeUtc(osObserverLogPath).ToShortDateString() == DateTime.UtcNow.ToShortDateString())
                    {
                        osObserverLogText = System.IO.File.ReadAllText(osObserverLogPath, Encoding.UTF8).Trim();
                    }

                    string host = Request.Host.Value;

                    string nodeLinks = string.Empty;

                    // Request originating from ObserverWeb node hyperlinks.
                    if (Request.QueryString.HasValue && Request.Query.ContainsKey("fqdn"))
                    {
                        host = Request.Query["fqdn"];

                        // Node links.
                        System.Fabric.Query.NodeList nodeList = this.fabricClient.QueryManager.GetNodeListAsync().Result;
                        IOrderedEnumerable <System.Fabric.Query.Node> ordered = nodeList.OrderBy(node => node.NodeName);

                        foreach (System.Fabric.Query.Node node in ordered)
                        {
                            nodeLinks += "| <a href='" + Request.Scheme + "://" + host + "/api/ObserverLog/" + name + "/" + node.NodeName + "/html'>" + node.NodeName + "</a> | ";
                        }
                    }

                    this.sb = new StringBuilder();

                    _ = this.sb.AppendLine("<html>\n\t<head>");
                    _ = this.sb.AppendLine("\n\t\t<title>FabricObserver Observer Report: Errors and Warnings</title>");
                    _ = this.sb.AppendLine("\n\t\t" + this.script);
                    _ = this.sb.AppendLine("\n\t\t<style type=\"text/css\">\n" +
                                           "\t\t\t.container {\n" +
                                           "\t\t\t\tfont-family: Consolas; font-size: 14px; background-color: lightblue; padding: 5px; border: 1px solid grey; " +
                                           "width: 98%;\n" +
                                           "\t\t\t}\n" +
                                           "\t\t\t.header {\n" +
                                           "\t\t\t\tfont-size: 25px; text-align: center; background-color: lightblue; " +
                                           "padding 5px; margin-bottom: 10px;\n" +
                                           "\t\t\t}\n" +
                                           "\t\t\tpre {\n" +
                                           "\t\t\t\toverflow-x: auto; white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;" +
                                           "\t\t\t}\n" +
                                           "\t\t\t a:link { text-decoration: none; }" +
                                           "\n\t\t</style>");
                    _ = this.sb.AppendLine("\n\t</head>");
                    _ = this.sb.AppendLine("\n\t<body>");
                    _ = this.sb.AppendLine("\n\t\t\t <br/>");
                    _ = this.sb.AppendLine("\n\t\t\t\t<div class=\"container\"><div style=\"position: relative; width: 100%; margin-left: auto; margin-right: auto;\"><br/><strong>Errors and Warnings for " + name + " on " + nodeName + "</strong><br/><br/>" + nodeLinks);

                    switch (name.ToLower())
                    {
                    case "appobserver":
                        if (!string.IsNullOrEmpty(appObserverLogText))
                        {
                            _ = this.sb.AppendLine("\n\t\t\t<br/><br/>" + "\n\t\t\t" + appObserverLogText + "<br/><br/>");
                        }

                        break;

                    case "diskobserver":
                        if (!string.IsNullOrEmpty(diskObserverLogText))
                        {
                            _ = this.sb.AppendLine("\n\t\t\t<br/><br/>" + "\n\t\t\t" + diskObserverLogText + "<br/><br/>");
                        }

                        break;

                    case "fabricsystemobserver":
                        if (!string.IsNullOrEmpty(fabricSystemObserverLogText))
                        {
                            _ = this.sb.AppendLine("\n\t\t\t<br/><br/>" + "\n\t\t\t" + fabricSystemObserverLogText + "<br/><br/>");
                        }

                        break;

                    case "networkobserver":
                        if (!string.IsNullOrEmpty(networkObserverLogText))
                        {
                            _ = this.sb.AppendLine("\n\t\t\t<br/><br/>" + "\n\t\t\t" + networkObserverLogText + "<br/><br/>");
                        }

                        break;

                    case "nodeobserver":
                        if (!string.IsNullOrEmpty(nodeObserverLogText))
                        {
                            _ = this.sb.AppendLine("\n\t\t\t<br/><br/>" + "\n\t\t\t" + nodeObserverLogText + "<br/><br/>");
                        }

                        break;

                    case "osobserver":
                        if (!string.IsNullOrEmpty(osObserverLogText))
                        {
                            _ = this.sb.AppendLine("\n\t\t\t<br/><br/>" + "\n\t\t\t" + osObserverLogText + "<br/><br/>");
                        }

                        break;

                    default:
                        _ = this.sb.AppendLine("\n\t\t\t<br/>Specified Observer, " + name + ", does not exist.");
                        break;
                    }

                    _    = this.sb.AppendLine("\n\t\t\t</div>");
                    _    = this.sb.AppendLine("\n\t</body>");
                    _    = this.sb.AppendLine("</html>");
                    html = this.sb.ToString();
                    _    = this.sb.Clear();
                    break;
                }
                catch (IOException ie)
                {
                    html = ie.ToString();
                }

                // If we get here, let's wait a few seconds before the next iteration.
                Task.Delay(2000).Wait();
            }

            return(html);
        }