func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, int32, error) { pid := p.Pid statPath := common.HostProc(strconv.Itoa(int(pid)), "stat") contents, err := ioutil.ReadFile(statPath) if err != nil { return "", 0, nil, 0, 0, err } fields := strings.Fields(string(contents)) i := 1 for !strings.HasSuffix(fields[i], ")") { i++ } termmap, err := getTerminalMap() terminal := "" if err == nil { t, err := strconv.ParseUint(fields[i+5], 10, 64) if err != nil { return "", 0, nil, 0, 0, err } terminal = termmap[t] } ppid, err := strconv.ParseInt(fields[i+2], 10, 32) if err != nil { return "", 0, nil, 0, 0, err } utime, err := strconv.ParseFloat(fields[i+12], 64) if err != nil { return "", 0, nil, 0, 0, err } stime, err := strconv.ParseFloat(fields[i+13], 64) if err != nil { return "", 0, nil, 0, 0, err } cpuTimes := &cpu.TimesStat{ CPU: "cpu", User: float64(utime / ClockTicks), System: float64(stime / ClockTicks), } bootTime, _ := host.BootTime() t, err := strconv.ParseUint(fields[i+20], 10, 64) if err != nil { return "", 0, nil, 0, 0, err } ctime := (t / uint64(ClockTicks)) + uint64(bootTime) createTime := int64(ctime * 1000) // p.Nice = mustParseInt32(fields[18]) // use syscall instead of parse Stat file snice, _ := syscall.Getpriority(PrioProcess, int(pid)) nice := int32(snice) // FIXME: is this true? return terminal, int32(ppid), cpuTimes, createTime, nice, nil }
// Uptime - returns uptime string // uptime = "{0} days {1} hours {2} minutes".format(days, hours, minutes) func Uptime() string { boot, _ := pshost.BootTime() secondsFromBoot := uint64(time.Now().Unix()) - boot days := secondsFromBoot / DAY hours := (secondsFromBoot % DAY) / HOUR minutes := (secondsFromBoot % HOUR) / MINUTE s := fmt.Sprintf("%v days %v hours %v minutes", days, hours, minutes) return s }
// Run gathers host information from gopsutil. func (h *HostInfo) Run() error { data, err := gopsutil_host.HostInfo() if err != nil { return err } h.Data["Hostname"] = data.Hostname h.Data["Uptime"] = data.Uptime h.Data["Procs"] = data.Procs h.Data["OS"] = data.OS h.Data["Platform"] = data.Platform h.Data["PlatformFamily"] = data.PlatformFamily h.Data["PlatformVersion"] = data.PlatformVersion h.Data["VirtualizationSystem"] = data.VirtualizationSystem h.Data["VirtualizationRole"] = data.VirtualizationRole bootTime, err := gopsutil_host.BootTime() if err == nil { h.Data["BootTime"] = bootTime } return nil }