コード例 #1
0
        public bool Poll()
        {
            //if( !this._proc.)

            try {
                // Don't bother if it's empty, only null. Empty means we don't have access.
                if (null == this._proc_user)
                {
#if DEBUG
                    Trace.TraceInformation("Getting user for process #" + this._proc_id.ToString() + "...");
#endif // DEBUG
                    this._proc_user = ProcMinder.GetProcessOwner(this._proc.Id);
                }

                if (this._last_sample_time == null || this._last_sample_time == new DateTime())
                {
                    // Take first sample.
                    this._last_sample_time = DateTime.Now;
                    this._last_proc_cpu    = this._proc.TotalProcessorTime;
                }
                else
                {
                    DateTime cur_sample_time = DateTime.Now;
                    TimeSpan cur_proc_cpu    = this._proc.TotalProcessorTime;

                    this._cpu_pct = (cur_proc_cpu.TotalMilliseconds - this._last_proc_cpu.TotalMilliseconds) /
                                    cur_sample_time.Subtract(this._last_sample_time).TotalMilliseconds / Convert.ToDouble(Environment.ProcessorCount);

                    this._last_sample_time = cur_sample_time;
                    this._last_proc_cpu    = cur_proc_cpu;
                }

                this._workingset = this._proc_wset_c.NextValue();

                /*this._read_op_s_n = this._rea_op_s_c.NextValue();
                 * this._write_op_s_n = this._write_op_s_c.NextValue();
                 * this._data_op_s_n = this._data_op_s_c.NextValue();*/
                this._read_b_s_n  = this._read_bytes_s_c.NextValue();
                this._write_b_s_n = this._write_bytes_s_c.NextValue();
                //this._data_b_s_n = this._data_bytes_s_c.NextValue();
                return(true);
            } catch (Win32Exception ex) {
                if (-2147467259 == ex.ErrorCode)
                {
                    // XXX
                }
                else
                {
                    //Console.WriteLine( ex.ErrorCode );
                }
                return(true);
            } catch (Exception) {
                // Process probably exited.
                return(false);
            }
        }
コード例 #2
0
        private void ReportProcs(ProcMinder pm, object state)
        {
            string      report_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            RestRequest req         = new RestRequest("post/proc", Method.POST);

            string verify_line = String.Format(
                "{0}:{1}",
                Environment.MachineName,
                report_time
                );

            Trace.TraceInformation("Reporting process statistics...");
            StringBuilder procs_list = new StringBuilder();

            foreach (ProcObject proc in pm.Processes)
            {
                if (
                    null == this._rc ||
                    String.IsNullOrEmpty(proc.ProcName) ||
                    String.IsNullOrEmpty(proc.ProcUser) ||
                    0 >= proc.ProcId
                    )
                {
                    continue;
                }

                procs_list.Append(", ");
                procs_list.Append(proc.ToJson());
            }

            if (0 < procs_list.Length)
            {
                procs_list.Remove(0, 2);   // Remove first comma.
            }
            procs_list.Insert(0, "[");
            procs_list.Append("]");

            req.AddParameter("procs", procs_list.ToString());
            req.AddParameter("time", report_time);
            req.AddParameter("host", Environment.MachineName);

            // HMAC hashing to verify integrity.
            if (null != this._api_key)
            {
                HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(this._api_key));
                byte[]     hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(verify_line));
                req.AddParameter("hash", Convert.ToBase64String(hash));
            }

            IRestResponse res = this._rc.Execute(req);
        }
コード例 #3
0
        public void Run()
        {
            try {
                // Setup event log.
                if (!EventLog.SourceExists("ProcMinder"))
                {
                    EventLog.CreateEventSource("ProcMinder", "Application");
                    Console.WriteLine("Please restart the application to finish initializing logger.");
                    //return;
                }

                // Create an EventLog instance and assign its source.
                this._logger        = new EventLog();
                this._logger.Source = "ProcMinder";
            } catch (SecurityException ex) {
                Console.WriteLine(String.Format("Unable to setup logging: {0}", ex.Message));
            }

            if (null != this._rc_url)
            {
                this._pm = new ProcMinder(this.ReportProcs, null, this._logger);
            }
            else
            {
                this._pm = new ProcMinder();
            }

            string interval_str = RegistryGet("Interval", "No interval key found, using default: " + this._pm.Interval.ToString());

            if (null != interval_str)
            {
                this._pm.Interval = int.Parse(interval_str);
            }

            // Setup the base page.
            this._cp = new WebControlPanel("Process Minder" /*, this.UpdatePage, this._pm*/);
            this._cp.AddRenderHandler("/index.html", this.UpdatePage);

            WebDiv nav = new WebDiv("nav");

            WebLink nav_options = new WebLink("/options.html", new WebText("Options"));

            nav.Children.Add(nav_options);

            WebTable proc_table = new WebTable();

            proc_table.SetHeadRow(
                null,
                new WebText(""),
                new WebText("Process Name"),
                new WebText("Process ID"),
                new WebText("User"),
                new WebText("CPU Time"),
                new WebText("Read Bytes/s"),
                new WebText("Write Bytes/s"),
                new WebText("Working Set"),
                new WebSubmit("execute", "Execute", this.OnKillButton)
                );
            proc_table.ID = "processes";

            WebForm proc_form = new WebForm("proc_form", "/");

            proc_form.Children.Add(proc_table);

            WebPage page = (WebPage)this._cp.VFileSystem.Root.GetFile("index.html");

            page.Children.Add(nav);
            page.Children.Add(proc_form);

            this._cp.AddOption("APIKey", this.OptRenderAPIKey, this.OnOptAPIKey);
            this._cp.AddOption("ReportServer", this.OptRenderReportServer, this.OnOptReportServer);

            this.LogInfo("Service started.");

            // Start polling and serving.
            this._pm.Run();
            this._cp.Run();

            this.Running = true;

            /* while( this.Running ) {
             *  Thread.Sleep( 10000 );
             * } */
        }