Exemple #1
0
        public LinuxProcessList GetProcessStatus(List <UInt32> pids)
        {
            string stdout, stderr;

            SendRemoteSshCommand(CreateProcessInformation(CreateProcessListCommand(pids)), "y\n", false, false, out stdout, out stderr);
            SendRemoteSshCommand(CreateProcessInformation(CreateReadProcessListCommand()), "y\n", true, true, out stdout, out stderr);

            LinuxProcessList processes = new LinuxProcessList();

            using (TextReader tr = new StreamReader(new MemoryStream(ASCIIEncoding.UTF8.GetBytes(stdout))))
            {
                String line;
                while ((line = tr.ReadLine()) != null)
                {
                    LinuxProcess info;
                    if (LinuxProcess.TryParse(line, out info))
                    {
                        processes.Add(info);
                    }
                }
            }

            return(processes);
        }
        public static bool TryParse(String line, out LinuxProcess info)
        {
            info = new LinuxProcess();
            bool result = false;

            try
            {
                List <LinePart> parts = LineSplitter.Split(line);
                UInt32          test;
                if (parts.Count > 5 && UInt32.TryParse(parts[0].Text, out test) == true)
                {
                    for (int x = 0; parts.Count >= 10 && x < parts.Count && result == false; x++)
                    {
                        switch (x)
                        {
                        case 0:
                            info.Pid = UInt32.Parse(parts[x].Text);
                            break;

                        case 1:
                            info.User = parts[x].Text;
                            break;

                        case 2:
                            info.Cpu = Double.Parse(parts[x].Text);
                            break;

                        case 3:
                            info.Memory = Double.Parse(parts[x].Text);
                            break;

                        case 4:
                            info.VirtualSize = UInt32.Parse(parts[x].Text);
                            break;

                        case 5:
                            info.ResidentSize = UInt32.Parse(parts[x].Text);
                            break;

                        case 6:
                            info.Tty = parts[x].Text;
                            break;

                        case 7:
                            foreach (Char c in parts[x].Text)
                            {
                                LinuxProcessStates state;
                                if (_processStateIndex.TryGetValue(c, out state))
                                {
                                    info.ProcessStates.Add(state);
                                }
                            }
                            break;

                        case 8:
                        {
                            TimeSpan elapsed;
                            if (TryParseTimeSpan(parts[x].Text, out elapsed) == false)
                            {
                                elapsed = TimeSpan.Zero;
                            }

                            info.StartTime = DateTime.Now - elapsed;
                        }
                        break;

                        case 9:
                            info.Command = line.Substring(parts[x].Index).Trim();
                            info.Name    = info.Command.Split(' ')[0];
                            result       = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.SysLogText(LogLevel.DEBUG, "Parse error: {0}", e.Message);
            }


            return(result);
        }