//Returns the command line slice for a given process name func getProcCmdline(procname string) (cmd []string, err error) { var proc *process.Process pid := getProcPID(procname) proc, err = process.NewProcess(int32(pid)) cmd, err = proc.CmdlineSlice() return cmd, err }
func NewSpecProcessor( processName string, prefix string, pid int32, acc telegraf.Accumulator, p *process.Process, tags map[string]string, ) *SpecProcessor { if processName != "" { tags["process_name"] = processName } else { name, err := p.Name() if err == nil { tags["process_name"] = name } } return &SpecProcessor{ Prefix: prefix, pid: pid, tags: tags, fields: make(map[string]interface{}), acc: acc, proc: p, } }
func NewSpecProcessor( prefix string, acc plugins.Accumulator, p *process.Process, ) *SpecProcessor { tags := make(map[string]string) tags["pid"] = fmt.Sprintf("%v", p.Pid) if name, err := p.Name(); err == nil { tags["name"] = name } return &SpecProcessor{ Prefix: prefix, tags: tags, acc: acc, proc: p, } }
func NewSpecProcessor( prefix string, acc telegraf.Accumulator, p *process.Process, tags map[string]string, ) *SpecProcessor { if name, err := p.Name(); err == nil { tags["process_name"] = name } return &SpecProcessor{ Prefix: prefix, tags: tags, fields: make(map[string]interface{}), acc: acc, proc: p, } }
func hasPort(proc *process.Process, connections []net.ConnectionStat) bool { for _, connection := range connections { if connection.Status == "LISTEN" && connection.Pid == int32(proc.Pid) { return true } } children, err := proc.Children() if err == nil { for _, child := range children { if hasPort(child, connections) { return true } } } return false }
func (sc *ServiceConfig) doGetPorts(proc *process.Process) ([]string, error) { var err error if len(connectionsCache) == 0 { connectionsCache, err = net.Connections("all") if err != nil { return nil, errors.WithStack(err) } } var ports []string var knownPorts = make(map[int]struct{}) if sc.LaunchChecks != nil { for _, port := range sc.LaunchChecks.Ports { knownPorts[port] = struct{}{} } } for _, connection := range connectionsCache { if connection.Status == "LISTEN" { if _, ok := knownPorts[int(connection.Laddr.Port)]; connection.Pid == proc.Pid && !ok { ports = append(ports, strconv.Itoa(int(connection.Laddr.Port))) } } } children, err := proc.Children() // This will error out if the process has finished or has no children if err != nil { return ports, nil } for _, child := range children { childPorts, err := sc.doGetPorts(child) if err == nil { ports = append(ports, childPorts...) } } return ports, nil }
func getProcStatus(p *process.Process) (*ProcessStatus, error) { n, err := p.Name() if err != nil { return nil, err } c, err := p.Percent(0) if err != nil { return nil, err } m, err := p.MemoryInfo() if err != nil { return nil, err } return &ProcessStatus{ Pid: p.Pid, Name: n, CPU: c, VMS: m.VMS, RSS: m.RSS, Swap: m.Swap, }, nil }
func (r region) communicate() { //collect region statistics ticker := time.NewTicker(5 * time.Second) //object holding process reference var exe *exec.Cmd exe = nil var start time.Time var proc *process.Process //process communication terminated := make(chan bool) for { select { case <-terminated: //the process exited for some Reason exe = nil case cmd := <-r.cmds: switch cmd.command { case "start": //if already running, exit if exe != nil { r.log.Error("Region is already running", r.UUID) continue } //execute binaries os.Chdir(r.dir) cmdName := "/usr/bin/mono" cmdArgs := []string{"OpenSim.exe", "-console", "rest"} exe = exec.Command(cmdName, cmdArgs...) err := exe.Start() if err != nil { errMsg := fmt.Sprintf("Error starting process: %s", err.Error()) r.log.Error(errMsg) continue } r.log.Info("Started Successfully") start = time.Now() proc, _ = process.NewProcess(int32(exe.Process.Pid)) go func() { //wait for process, ignoring process-specific errors _ = exe.Wait() r.log.Error("Terminated") exe = nil terminated <- true }() case "kill": //if not running, exit if exe == nil { errMsg := fmt.Sprintf("Kill region %v failed, region is not running", r.UUID.String()) r.log.Error(errMsg) continue } if err := exe.Process.Kill(); err != nil { errMsg := fmt.Sprintf("Error killing process: %s", err.Error()) r.log.Error(errMsg) } default: r.log.Info("Received unexpected command: %v", cmd.command) } case <-ticker.C: stat := mgm.RegionStat{UUID: r.UUID} if exe == nil { //trivially halted if we never started r.rStat <- stat continue } stat.Running = true cpuPercent, err := proc.CPUPercent(0) if err != nil { errMsg := fmt.Sprintf("Error getting cpu for pid: %s", err.Error()) r.log.Error(errMsg) } else { stat.CPUPercent = cpuPercent } //memory info from this module may not be correct.... memInfo, err := proc.MemoryInfo() if err != nil { errMsg := fmt.Sprintf("Error getting memory for pid: %s", err.Error()) r.log.Error(errMsg) } else { stat.MemKB = (float64(memInfo.RSS) / 1024.0) } elapsed := time.Since(start) stat.Uptime = elapsed r.rStat <- stat } } }