func BootTime() (uint64, error) { sysinfo := &syscall.Sysinfo_t{} if err := syscall.Sysinfo(sysinfo); err != nil { return 0, err } return uint64(sysinfo.Uptime), nil }
func runningStatus(w http.ResponseWriter) (res jsonObject) { sys := &syscall.Sysinfo_t{} _ = syscall.Sysinfo(sys) log := []statusEntry{} status.LogBuf.Do(func(v interface{}) { if v != nil { log = append(log, v.(statusEntry)) } }) res = jsonObject{ "status": "OK", "response": map[string]interface{}{ "uptime": sys.Uptime, "load_1": sys.Loads[0], "load_5": sys.Loads[1], "load_15": sys.Loads[2], "freeram": sys.Freeram, "log": log, "notification": status.Notifications(), }, } return }
func procUsage(pcpu *float64, rss, vss *int64) error { contents, err := ioutil.ReadFile(procStatFile) if err != nil { return err } fields := bytes.Fields(contents) *rss = (parseInt64(fields[rssPos])) << 12 *vss = parseInt64(fields[vssPos]) startTime := parseInt64(fields[startPos]) utime := parseInt64(fields[utimePos]) stime := parseInt64(fields[stimePos]) totalTime := utime + stime var sysinfo syscall.Sysinfo_t if err := syscall.Sysinfo(&sysinfo); err != nil { return err } seconds := int64(sysinfo.Uptime) - (startTime / ticks) if seconds > 0 { ipcpu := (totalTime * 1000 / ticks) / seconds *pcpu = float64(ipcpu) / 10.0 } return nil }
func main() { log.SetFlags(0) log.SetPrefix("uptime: ") flag.Parse() var info syscall.Sysinfo_t var err error syscall.Sysinfo(&info) ck(err) now := time.Now().Format("15:04:05") fmt.Printf(" %s up ", now) info.Uptime /= 60 min := info.Uptime % 60 info.Uptime /= 60 hours := info.Uptime % 24 days := info.Uptime / 24 if days != 0 { fmt.Printf("%d day", days) if days != 1 { fmt.Printf("s") } fmt.Printf(", ") } if hours != 0 { fmt.Printf("%2d:%02d, ", hours, min) } else { fmt.Printf("%d min,", min) } fmt.Printf(" load average: %.02f, %.02f, %.02f\n", float64(info.Loads[0])/65536, float64(info.Loads[1])/65536, float64(info.Loads[2])/65536) }
func main() { client := http.Client{} for { si := syscall.Sysinfo_t{} syscall.Sysinfo(&si) l := strconv.FormatFloat(float64(si.Loads[0])/65536, 'f', 2) fmt.Println(l, si.Loads[0]/65536.0, si.Loads[0]) time.Sleep(1 * time.Second) continue resp, err := client.PostForm("https://api.stathat.com/ez", url.Values{ "stat": {"loadavg"}, "ezkey": {"YLJRun7adtSFKR2u"}, "value": {l}, }) if err != nil { // Problems... fmt.Println("Error sending stat... : %v", err) os.Exit(1) } fmt.Println(resp) resp.Body.Close() time.Sleep(1 * time.Second) } }
// Uptime returns the number of seconds since the system has booted func Uptime() (int64, error) { info := &syscall.Sysinfo_t{} err := syscall.Sysinfo(info) if err != nil { return 0, err } return int64(info.Uptime), nil }
func update_sysinfo() { sysinfo_mutex.Lock() defer sysinfo_mutex.Unlock() err := syscall.Sysinfo(sysinfo) if err != nil { log.Panic("syscall.Sysinfo failed: ", err) } }
// Returns available physical memory func PhysmemAvail() uint64 { info := syscall.Sysinfo_t{} err := syscall.Sysinfo(&info) if err != nil { return 0 } return info.Freeram }
func uptime() (int64, error) { sysinfo := syscall.Sysinfo_t{} if err := syscall.Sysinfo(&sysinfo); err != nil { return 0.0, err } else { return sysinfo.Uptime, nil } }
func Load() int64 { info := &syscall.Sysinfo_t{} err := syscall.Sysinfo(info) if err != nil { fmt.Printf("%s\n", err) } return info.Uptime }
func (mem *MemStatus) fillInStatus() { //system memory usage sysInfo := new(syscall.Sysinfo_t) err := syscall.Sysinfo(sysInfo) if err == nil { mem.All = uint64(sysInfo.Totalram) mem.Free = uint64(sysInfo.Freeram) mem.Used = mem.All - mem.Free } }
// Get sysinfo func GetSysinfo() (sysinfo *syscall.Sysinfo_t, err error) { sysinfo = new(syscall.Sysinfo_t) err = syscall.Sysinfo(sysinfo) if err != nil { ALARM_MANAGER.Add(ALARM_SYSCALL, err, "Sysinfo failed") logger.Debug("Failed in syscall sysinfo, error: %s", err.Error()) return } return sysinfo, nil }
func GetSystemInfo(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) { st := &syscall.Sysinfo_t{} if err := syscall.Sysinfo(st); err != nil { cResp <- &Response{nil, tag, err} return } sinfo := &SysInfo{} sinfo.Uptime = time.Duration(st.Uptime) // float64(1<<SI_LOAD_SHIFT) == 65536.0 scale := 65536.0 sinfo.Loadavg.One = float64(st.Loads[0]) / scale sinfo.Loadavg.Five = float64(st.Loads[1]) / scale sinfo.Loadavg.Fifteen = float64(st.Loads[2]) / scale unit := uint64(st.Unit) * 1024 // kB sinfo.Ram.Total = uint64(st.Totalram) / unit sinfo.Ram.Free = uint64(st.Freeram) / unit sinfo.Ram.Buffer = uint64(st.Bufferram) / unit sinfo.Swap.Total = uint64(st.Totalswap) / unit sinfo.Swap.Free = uint64(st.Freeswap) / unit sinfo.LongBit = getLongBit() switch u, err := getUname(); { case err == nil: sinfo.Uname = u default: cResp <- &Response{nil, tag, err} return } switch u, err := getLoggedUsers(); { case err == nil: sinfo.Users = u default: cResp <- &Response{nil, tag, err} return } switch d, err := getDisksInfo(); { case err == nil: sinfo.Disks = d default: cResp <- &Response{nil, tag, err} return } cResp <- &Response{sinfo, tag, nil} }
func (self *Uptime) Get() error { sysinfo := syscall.Sysinfo_t{} if err := syscall.Sysinfo(&sysinfo); err != nil { return err } self.Length = float64(sysinfo.Uptime) return nil }
func GetSwap() (*Swap, error) { swap := &Swap{} sysinfo := syscall.Sysinfo_t{} if err := syscall.Sysinfo(&sysinfo); err != nil { return swap, err } swap.Total = uint64(sysinfo.Totalswap) swap.Free = uint64(sysinfo.Freeswap) swap.Used = swap.Total - swap.Free return swap, nil }
func (s *Uptime) Collect(c chan *Delta) { for { info := syscall.Sysinfo_t{} err := syscall.Sysinfo(&info) if err != nil { c <- NewDelta("uptime", err) continue } c <- NewDelta("uptime", time.Duration(info.Uptime)*time.Second) time.Sleep(2 * time.Second) } }
func (s *Freeram) Collect(c chan *Delta) { for { info := syscall.Sysinfo_t{} err := syscall.Sysinfo(&info) if err != nil { c <- NewDelta("freeram", err) continue } c <- NewDelta("freeram", uint(info.Freeram)) time.Sleep(2 * time.Second) } }
func (swap *Swap) Get() error { sysinfo := syscall.Sysinfo_t{} if err := syscall.Sysinfo(&sysinfo); err != nil { return err } swap.TotalSwap = uint64(sysinfo.Totalswap) swap.FreeSwap = uint64(sysinfo.Freeswap) swap.UsedSwap = swap.TotalSwap - swap.FreeSwap return nil }
func (self *Swap) Get() error { sysinfo := syscall.Sysinfo_t{} if err := syscall.Sysinfo(&sysinfo); err != nil { return err } self.Total = uint64(sysinfo.Totalswap) self.Free = uint64(sysinfo.Freeswap) self.Used = self.Total - self.Free return nil }
func (load *Load) Get() error { sysinfo := syscall.Sysinfo_t{} if err := syscall.Sysinfo(&sysinfo); err != nil { return err } load.One = float64(sysinfo.Loads[0]) / (1 << SI_LOAD_SHIFT) load.Five = float64(sysinfo.Loads[1]) / (1 << SI_LOAD_SHIFT) load.Fifteen = float64(sysinfo.Loads[2]) / (1 << SI_LOAD_SHIFT) return nil }
func (mem *Mem) Get() error { sysinfo := syscall.Sysinfo_t{} if err := syscall.Sysinfo(&sysinfo); err != nil { return err } mem.TotalRam = uint64(sysinfo.Totalram) mem.FreeRam = uint64(sysinfo.Freeram) mem.SharedRam = uint64(sysinfo.Sharedram) mem.BufferRam = uint64(sysinfo.Bufferram) return nil }
func SwapMemory() (*SwapMemoryStat, error) { sysinfo := &syscall.Sysinfo_t{} if err := syscall.Sysinfo(sysinfo); err != nil { return nil, err } ret := &SwapMemoryStat{ Total: uint64(sysinfo.Totalswap), Free: uint64(sysinfo.Freeswap), } ret.Used = ret.Total - ret.Free ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0 return ret, nil }
func (uptime *Uptime) Get() error { sysinfo := syscall.Sysinfo_t{} if err := syscall.Sysinfo(&sysinfo); err != nil { return err } uptime.Uptime = uint64(sysinfo.Uptime) /* days := sysinfo.Uptime / 86400 hours := (sysinfo.Uptime / 3600) - (days * 24) mins := (sysinfo.Uptime / 60) - (days * 1440) - (hours * 60) */ return nil }
func SysinfoGet() Sysinfo { var si syscall.Sysinfo_t syscall.Sysinfo(&si) return Sysinfo{ CpuNum: runtime.NumCPU(), Uptime: si.Uptime, Loads: si.Loads, MemTotal: si.Totalram / 1024, MemFree: si.Freeram / 1024, MemShared: si.Sharedram / 1024, MemBuffer: si.Bufferram / 1024, SwapTotal: si.Totalswap / 1024, SwapFree: si.Freeswap / 1024, Procs: si.Procs, TimeNow: time.Now().Format(time.RFC3339), } }
func uptime() string { var sysinfo syscall.Sysinfo_t errno := syscall.Sysinfo(&sysinfo) if errno == nil { seconds := sysinfo.Uptime minutes := seconds / 60 seconds -= minutes * 60 hours := minutes / 60 minutes -= hours * 60 days := hours / 24 hours -= days * 24 return fmt.Sprintf("%v days %v:%02v:%02v", days, hours, minutes, seconds) } return "unknown" }
func SwapMemory() (*SwapMemoryStat, error) { sysinfo := &syscall.Sysinfo_t{} if err := syscall.Sysinfo(sysinfo); err != nil { return nil, err } ret := &SwapMemoryStat{ Total: uint64(sysinfo.Totalswap) * uint64(sysinfo.Unit), Free: uint64(sysinfo.Freeswap) * uint64(sysinfo.Unit), } ret.Used = ret.Total - ret.Free //check Infinity if ret.Total != 0 { ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0 } else { ret.UsedPercent = 0 } filename := common.HostProc("vmstat") lines, _ := common.ReadLines(filename) for _, l := range lines { fields := strings.Fields(l) if len(fields) < 2 { continue } switch fields[0] { case "pswpin": value, err := strconv.ParseUint(fields[1], 10, 64) if err != nil { continue } ret.Sin = value * 4 * 1024 case "pswpout": value, err := strconv.ParseUint(fields[1], 10, 64) if err != nil { continue } ret.Sout = value * 4 * 1024 } } return ret, nil }
func MemStat() MemStatus { // self mem stat memStat := new(runtime.MemStats) runtime.ReadMemStats(memStat) mem := MemStatus{} mem.Self = memStat.Alloc // system mem stat sysInfo := new(syscall.Sysinfo_t) err := syscall.Sysinfo(sysInfo) if err == nil { mem.All = uint64(sysInfo.Totalram) mem.Free = uint64(sysInfo.Freeram) mem.Used = mem.All - mem.Free } fmt.Println("", "Self Alloc:", mem.Self, bytefmt.ByteSize(mem.Self), "\n", "All:", mem.All, bytefmt.ByteSize(mem.All), "\n", "Used:", mem.Used, bytefmt.ByteSize(mem.Used), "\n", "Free:", mem.Free, bytefmt.ByteSize(mem.Free), ) return mem }
// Sampling function to keep pcpu relevant. func periodic() { contents, err := ioutil.ReadFile(procStatFile) if err != nil { return } fields := bytes.Fields(contents) // PCPU pstart := parseInt64(fields[startPos]) utime := parseInt64(fields[utimePos]) stime := parseInt64(fields[stimePos]) total := utime + stime var sysinfo syscall.Sysinfo_t if err := syscall.Sysinfo(&sysinfo); err != nil { return } seconds := int64(sysinfo.Uptime) - (pstart / ticks) // Save off temps lt := lastTotal ls := lastSeconds // Update last sample lastTotal = total lastSeconds = seconds // Adjust to current time window total -= lt seconds -= ls if seconds > 0 { atomic.StoreInt64(&ipcpu, (total*1000/ticks)/seconds) } time.AfterFunc(1*time.Second, periodic) }
func Info() (*sysinfo, error) { rawsysinfo := &syscall.Sysinfo_t{} if err := syscall.Sysinfo(rawsysinfo); err != nil { return nil, err } defer lock.Unlock() lock.Lock() unit := uint64(rawsysinfo.Unit) * 1024 // kB singelton.Uptime = time.Duration(rawsysinfo.Uptime) * time.Second singelton.Loads[0] = float64(rawsysinfo.Loads[0]) / scale singelton.Loads[1] = float64(rawsysinfo.Loads[1]) / scale singelton.Loads[2] = float64(rawsysinfo.Loads[2]) / scale singelton.Procs = uint64(rawsysinfo.Procs) singelton.TotalRam = uint64(rawsysinfo.Totalram) / unit singelton.FreeRam = uint64(rawsysinfo.Freeram) / unit singelton.BufferRam = uint64(rawsysinfo.Bufferram) / unit singelton.TotalSwap = uint64(rawsysinfo.Totalswap) / unit singelton.FreeSwap = uint64(rawsysinfo.Freeswap) / unit singelton.TotalHighRam = uint64(rawsysinfo.Totalhigh) / unit singelton.FreeHighRam = uint64(rawsysinfo.Freehigh) / unit return singelton, nil }
func main() { flag.Var(unitFlag('b'), "b", "display in bytes") flag.Var(unitFlag('k'), "k", "display in kibibytes") flag.Var(unitFlag('m'), "m", "display in megabytes") flag.Var(unitFlag('g'), "g", "display in gigabytes") flag.Usage = usage flag.Parse() var info syscall.Sysinfo_t err := syscall.Sysinfo(&info) ck(err) unit = uint64(info.Unit) if unit == 0 { unit = 1 } fmt.Printf(" %13s%13s%13s%13s%13s\n", "total", "used", "free", "shared", "buffers") fmt.Printf("Mem: ") fmt.Printf("%13d%13d%13d%13d%13d\n", scale(info.Totalram), scale(info.Totalram-info.Freeram), scale(info.Freeram), scale(info.Sharedram), scale(info.Bufferram)) fmt.Printf("-/+ buffers/cache:") fmt.Printf("%13d%13d\n", scale(info.Totalram-info.Freeram-info.Bufferram), scale(info.Freeram+info.Bufferram)) fmt.Printf("Swap:") fmt.Printf("%13d%13d%13d\n", scale(info.Totalswap), scale(info.Totalswap-info.Freeswap), scale(info.Freeswap)) }