Exemplo n.º 1
0
        public Log(DSCReport dscReport, StatusData statusData)
        {
            if (dscReport != null)
            {
                // If there is no status in DSC Report, Job is InProgress
                if (dscReport.Status == null)
                {
                    this.status = "InProgress";
                }
                else
                {
                    this.status = dscReport.Status;
                }

                // Get data from DSC Report
                this.jobId           = dscReport.JobId;
                this.operationType   = dscReport.OperationType;
                this.refreshMode     = dscReport.RefreshMode;
                this.rebootRequested = dscReport.RebootRequested;
                this.timestamp       = dscReport.EndTime;

                // Get data from DSC StatusReport, if available
                if (statusData != null)
                {
                    this.durationInSeconds = statusData.DurationInSeconds;
                    this.numberOfResources = statusData.NumberOfResources;
                    this.hostname          = statusData.Hostname;

                    if (statusData.Error != null)
                    {
                        this.error = statusData.Error;
                    }

                    if (statusData.ResourcesInDesiredState != null)
                    {
                        foreach (Resource resource in statusData.ResourcesInDesiredState)
                        {
                            if (this.resourcesInDesiredState != "")
                            {
                                this.resourcesInDesiredState += ", ";
                            }

                            this.resourcesInDesiredState += resource.InstanceName;
                        }
                        if (statusData.ResourcesInDesiredState.Length < this.numberOfResources)
                        {
                            this.compliant = false;
                        }
                    }

                    if (statusData.ResourcesNotInDesiredState != null)
                    {
                        foreach (Resource resource in statusData.ResourcesNotInDesiredState)
                        {
                            if (this.resourcesNotInDesiredState != "")
                            {
                                this.resourcesNotInDesiredState += ", ";
                            }

                            this.resourcesNotInDesiredState += resource.InstanceName;

                            if (resource.Error != null)
                            {
                                if (this.error != "")
                                {
                                    this.error += "\r\n";
                                }

                                this.error += resource.InstanceName + @" """ + resource.Error + @"""";
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void Application_BeginRequest(Object source, EventArgs e)
        {
            // Create HttpApplication and HttpContext objects to access
            // request and response properties.
            HttpApplication application = (HttpApplication)source;
            HttpContext     context     = application.Context;

            var urlMatch = SendReportRegex.Match(context.Request.Path);

            if (urlMatch.Success)
            {
                var agentId = urlMatch.Groups["AgentId"];

                // Loading POST data
                byte[] postData = new byte[context.Request.InputStream.Length];

                // save the position so that further processing of the request happens normally
                var originalPosition = context.Request.InputStream.Position;

                try
                {
                    context.Request.InputStream.Read(postData, 0, (int)context.Request.InputStream.Length);
                }
                catch
                {
                    postData = null;
                }
                finally
                {
                    // reset the position for normal request execution
                    context.Request.InputStream.Position = originalPosition;
                }

                // Decoding POST data as JSON
                DSCReport  dscReport  = new DSCReport();
                StatusData statusData = new StatusData();

                if (postData != null && postData.Length > 0)
                {
                    // Extract data from main report
                    try
                    {
                        string postDataStr = System.Text.Encoding.ASCII.GetString(postData);
                        JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                        dscReport = json_serializer.Deserialize <DSCReport>(postDataStr);
                    }
                    catch
                    {
                        dscReport = null;
                    }

                    // Extract data from StatusData part of main report
                    if (dscReport != null && dscReport.StatusData.Length > 0)
                    {
                        try
                        {
                            string statusDataStr = dscReport.StatusData[0];
                            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                            statusData = json_serializer.Deserialize <StatusData>(statusDataStr);
                        }
                        catch
                        {
                            statusData = null;
                        }
                    }
                    else
                    {
                        statusData = null;
                    }

                    // Write report on disk
                    if (dscReport.Status != null || ReportInProgress)
                    {
                        if (Directory.Exists(ReportsFolder))
                        {
                            var fileName = $"{agentId}.json";
                            // compute file name based on AgentId
                            var fileFullPath = Path.Combine(ReportsFolder, fileName);

                            try
                            {
                                // write to file request content
                                using (var fileWriter = new FileStream(fileFullPath, FileMode.Create))
                                {
                                    using (var binaryWriter = new BinaryWriter(fileWriter))
                                    {
                                        binaryWriter.Write(postData);
                                        binaryWriter.Flush();
                                        binaryWriter.Close();
                                    }
                                }
                            }
                            catch
                            {
                                // TODO if needed
                            }
                        }
                    }

                    // Write new log line in log file
                    Log log = new Log(dscReport, statusData);
                    log.Flush();
                }
            }
        }