Exemplo n.º 1
0
func printUptime(us []utmp.Utmp) {

	var (
		bootTime int32
		entries  int64
		now      utmp.TimeVal

		days, hours, mins int
		uptime            float64
	)

	request := "kern.boottime"
	secs, err := syscall.SysctlUint32(request)
	if err != nil {
		panic(err)
	}
	secs = float64(secs)

	if 0 <= secs || secs < math.MaxFloat64 {
		uptime = secs
	} else {
		uptime = -1
	}

	for _, v := range us {

		if v.IsUserProcess() {
			entries++
		}

		if v.Type == utmp.BootTime {
			bootTime = v.Time.Sec
		}
	}

	now.GetTimeOfDay()
	if uptime == 0 {
		if bootTime == 0 {
			fatal.Fatalln("couldn't get boot time")
		}

		uptime = float64(now.Sec - bootTime)
	}

	days = int(uptime) / 86400
	hours = (int(uptime) - (days * 86400)) / 3600
	mins = (int(uptime) - (days * 86400) - (hours * 3600)) / 60

	fmt.Print(time.Now().Local().Format(" 15:04pm  "))

	if uptime == -1 {
		fmt.Print("up ???? days ??:??,  ")
	} else {
		if 0 < days {
			if days > 1 {
				fmt.Printf("up %d days %2d:%02d,  ", days, hours, mins)
			} else {
				fmt.Printf("up %d day %2d:%02d,  ", days, hours, mins)
			}
		} else {
			fmt.Printf("up  %2d:%02d,  ", hours, mins)
		}
	}

	if len(us) > 1 || len(us) == 0 {
		fmt.Printf("%d users", entries)
	} else {
		fmt.Printf("%d user", entries)
	}

	var avg [3]float64
	loads := stdlib.GetLoadAvg(&avg)

	if loads == -1 {
		fmt.Printf("%s", "\n")
	} else {
		if loads > 0 {
			fmt.Printf(",  load average: %.2f", avg[0])
		}

		if loads > 1 {
			fmt.Printf(", %.2f", avg[1])
		}

		if loads > 2 {
			fmt.Printf(", %.2f", avg[2])
		}

		if loads > 0 {
			fmt.Printf("%s", "\n")
		}
	}
}
Exemplo n.º 2
0
func printUptime(us []utmp.Utmp) {

	var (
		bootTime int32
		entries  int64
		now      utmp.TimeVal

		days, hours, mins int
		uptime            float64
	)

	file, err := os.Open("/proc/uptime")
	if err != nil {
		fatal.Fatalln(err)
	}
	defer file.Close()

	buf := make([]byte, 256)

	n, err := file.Read(buf)
	if err != nil && err != io.EOF {
		fatal.Fatalln(err)
	}

	// /proc/uptime's output is in the format of "%f %f\n"
	// The first space in the buffer will be the end of the first number
	line := string(buf[:bytes.IndexByte(buf[:n], ' ')])

	secs, err := strconv.ParseFloat(line, 64)
	if err != nil {
		fatal.Fatalln(err)
	}

	if 0 <= secs || secs < math.MaxFloat64 {
		uptime = secs
	} else {
		uptime = -1
	}

	for _, v := range us {

		if v.IsUserProcess() {
			entries++
		}

		if v.Type == utmp.BootTime {
			bootTime = v.Time.Sec
		}
	}

	now.GetTimeOfDay()
	if uptime == 0 {
		if bootTime == 0 {
			fatal.Fatalln("can't get boot time")
		}

		uptime = float64(now.Sec - bootTime)
	}

	days = int(uptime) / 86400
	hours = (int(uptime) - (days * 86400)) / 3600
	mins = (int(uptime) - (days * 86400) - (hours * 3600)) / 60

	fmt.Print(time.Now().Local().Format(" 15:04pm  "))

	if uptime == -1 {
		fmt.Print("up ???? days ??:??,  ")
	} else {
		if 0 < days {
			if days > 1 {
				fmt.Printf("up %d days %2d:%02d,  ", days, hours, mins)
			} else {
				fmt.Printf("up %d day %2d:%02d,  ", days, hours, mins)
			}
		} else {
			fmt.Printf("up  %2d:%02d,  ", hours, mins)
		}
	}

	if len(us) > 1 || len(us) == 0 {
		fmt.Printf("%d users", entries)
	} else {
		fmt.Printf("%d user", entries)
	}

	var avg [3]float64
	loads := stdlib.GetLoadAvg(&avg)

	if loads == -1 {
		fmt.Printf("%s", "\n")
	} else {
		if loads > 0 {
			fmt.Printf(",  load average: %.2f", avg[0])
		}

		if loads > 1 {
			fmt.Printf(", %.2f", avg[1])
		}

		if loads > 2 {
			fmt.Printf(", %.2f", avg[2])
		}

		if loads > 0 {
			fmt.Printf("%s", "\n")
		}
	}
}