Exemplo n.º 1
0
func getSysctl(sysctlName string) string {
	if val, err := sysctl.ByName(sysctlName); err != nil {
		log.Fatalf("%s - %v\n", sysctlName, err)
		return "" //NOTREACHED
	} else {
		return val
	}
}
Exemplo n.º 2
0
func main() {
	flag.Parse()

	msgbuf, err := sysctl.ByName("kern.msgbuf")
	if err != nil {
		log.Fatalf("sysctl kern.msgbuf: %v", err)
	}

	buf := []byte(msgbuf)
	//trim leading \0 chars ( \0 == \x00 )
	for i := 0; i < len(buf); i++ {
		if buf[i] != '\x00' {
			buf = buf[i:]
			break
		}
	}

	if buf[len(buf)-1] == '\n' {
		buf = buf[0 : len(buf)-1]
	}

	lines := strings.Split(string(buf), "\n")
	for _, line := range lines {
		re := regexp.MustCompile(`^<(\d+)>`) // search for syslog entries
		if matches := re.FindStringSubmatch(line); matches != nil {
			fac, err := strconv.ParseInt(matches[1], 10, 32)
			if err == nil {
				if !isKernSyslogEntry(fac) {
					continue
				}
				line = re.ReplaceAllString(line, "") // remove syslog facility
			}
		}
		fmt.Println(line)
	}

	if *cflag {
		if err := sysctl.SetUint32("kern.msgbuf_clear", 1); err != nil {
			log.Fatalf("sysctl kern.msgbuf_clear: %v", err)
		}
	}
}
Exemplo n.º 3
0
func getHostname() (string, error) {
	return sysctl.ByName("kern.hostname")
}