func init() { // Seems like kern.osreldate is veiled on latest OS X. We use // kern.osrelease instead. osver, err := syscall.Sysctl("kern.osrelease") if err != nil { return } var i int for i = range osver { if osver[i] != '.' { continue } } // The IPV6_RECVPATHMTU and IPV6_PATHMTU options were // introduced in OS X 10.7 (Darwin 11.0.0). // See http://support.apple.com/kb/HT1633. if i > 2 || i == 2 && osver[0] >= '1' && osver[1] >= '1' { ctlOpts[ctlTrafficClass].name = sysIPV6_TCLASS ctlOpts[ctlTrafficClass].length = 4 ctlOpts[ctlTrafficClass].marshal = marshalTrafficClass ctlOpts[ctlTrafficClass].parse = parseTrafficClass ctlOpts[ctlHopLimit].name = sysIPV6_HOPLIMIT ctlOpts[ctlHopLimit].marshal = marshalHopLimit ctlOpts[ctlPacketInfo].name = sysIPV6_PKTINFO ctlOpts[ctlPacketInfo].marshal = marshalPacketInfo sockOpts[ssoReceiveTrafficClass].level = ianaProtocolIPv6 sockOpts[ssoReceiveTrafficClass].name = sysIPV6_RECVTCLASS sockOpts[ssoReceiveTrafficClass].typ = ssoTypeInt sockOpts[ssoReceiveHopLimit].name = sysIPV6_RECVHOPLIMIT sockOpts[ssoReceivePacketInfo].name = sysIPV6_RECVPKTINFO sockOpts[ssoReceivePathMTU].level = ianaProtocolIPv6 sockOpts[ssoReceivePathMTU].name = sysIPV6_RECVPATHMTU sockOpts[ssoReceivePathMTU].typ = ssoTypeInt } }
func getInfo() ([]Info, error) { cpu, err := syscall.Sysctl("machdep.cpu.vendor") if err != nil { return nil, err } fmt.Println(cpu) /* type Info struct { CPU int32 `json:"cpu"` VendorId string `json:"vendorId"` Family string `json:"family"` Model string `json:"model"` Stepping int32 `json:"stepping"` PhysicalId string `json:"physicalId"` CoreId string `json:"coreId"` Cores int32 `json:"cores"` ModelName string `json:"modelName"` Mhz float64 `json:"mhz"` CacheSize int32 `json:"cacheSize"` Flags []string `json:"flags"` }*/ return nil, nil }
// osVersion returns the OS version. func osVersion() string { release, err := syscall.Sysctl("kern.osrelease") if err != nil { return "0.0" } return release }
func supportsIPv6MulticastDeliveryOnLoopback() bool { switch runtime.GOOS { case "freebsd": // See http://www.freebsd.org/cgi/query-pr.cgi?pr=180065. // Even after the fix, it looks like the latest // kernels don't deliver link-local scoped multicast // packets correctly. return false case "darwin": // See http://support.apple.com/kb/HT1633. s, err := syscall.Sysctl("kern.osrelease") if err != nil { return false } ss := strings.Split(s, ".") if len(ss) == 0 { return false } // OS X 10.9 (Darwin 13) or above seems to do the // right thing; preserving the packet header as it's // needed for the checksum calcuration with pseudo // header on loopback multicast delivery process. // If not, you'll probably see what is the slow-acting // kernel crash caused by lazy mbuf corruption. // See ip6_mloopback in netinet6/ip6_output.c. if mjver, err := strconv.Atoi(ss[0]); err != nil || mjver < 13 { return false } return true default: return true } }
func LoadAvg() (s []string) { v, err := syscall.Sysctl(sysctl) t := time.Now() etime := strconv.FormatInt(t.Unix(), 10) if err != nil { fmt.Println(err) } b := []byte(v) var l loadavg = *(*loadavg)(unsafe.Pointer(&b[0])) scale := float64(l.scale) c := strconv.FormatFloat(float64(l.ldavg[0])/scale, 'f', 2, 64) d := strconv.FormatFloat(float64(l.ldavg[1])/scale, 'f', 2, 64) e := strconv.FormatFloat(float64(l.ldavg[2])/scale, 'f', 2, 64) // returning as load.load.metric because that's what collectd does f := fmt.Sprintf("load.load.shortterm %s %s", c, etime) s = append(s, f) g := fmt.Sprintf("load.load.midterm %s %s", d, etime) s = append(s, g) h := fmt.Sprintf("load.load.longterm %s %s", e, etime) s = append(s, h) return s }
func init() { // Seems like kern.osreldate is veiled on latest OS X. We use // kern.osrelease instead. osver, err := syscall.Sysctl("kern.osrelease") if err != nil { return } var i int for i = range osver { if osver[i] != '.' { continue } } // The IP_PKTINFO was introduced in OS X 10.7 (Darwin // 11.0.0). See http://support.apple.com/kb/HT1633. if i > 2 || i == 2 && osver[0] >= '1' && osver[1] >= '1' { ctlOpts[ctlPacketInfo].name = sysIP_PKTINFO ctlOpts[ctlPacketInfo].length = sysSizeofInetPktinfo ctlOpts[ctlPacketInfo].marshal = marshalPacketInfo ctlOpts[ctlPacketInfo].parse = parsePacketInfo sockOpts[ssoPacketInfo].name = sysIP_RECVPKTINFO sockOpts[ssoPacketInfo].typ = ssoTypeInt sockOpts[ssoMulticastInterface].typ = ssoTypeIPMreqn } }
func hostname() (name string, err error) { name, err = syscall.Sysctl("kern.hostname") if err != nil { return "", NewSyscallError("sysctl kern.hostname", err) } return name, nil }
// osName returns the name of the OS. func osName() string { name, err := syscall.Sysctl("kern.ostype") if err != nil { return runtime.GOOS } return name }
// IsVTXDisabled checks if VT-X is disabled in the BIOS. If it is, the vm will fail to start. // If we can't be sure it is disabled, we carry on and will check the vm logs after it's started. func (d *Driver) IsVTXDisabled() bool { features, err := syscall.Sysctl("machdep.cpu.features") if err != nil { log.Debugf("Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v", err) return false } return isVTXDisabled(features) }
func Hostname() (name string, err Error) { var errno int name, errno = syscall.Sysctl("kern.hostname") if errno != 0 { return "", NewSyscallError("sysctl kern.hostname", errno) } return name, nil }
func trySysctl() (string, error) { v, err := syscall.Sysctl("osxfuse.version.number") if err != nil { log.Debug("mount: sysctl osxfuse.version.number:", "failed") return "", err } log.Debug("mount: sysctl osxfuse.version.number:", v) return v, nil }
func getOSInfo() (*OSInfo, error) { osinfo := &OSInfo{} var err error osinfo.Name, err = syscall.Sysctl("kern.ostype") if err != nil { return nil, err } osinfo.Version, err = syscall.Sysctl("kern.osrelease") if err != nil { return nil, err } osinfo.Id = osinfo.Name osinfo.IdLike = osinfo.Name osinfo.PrettyName = osinfo.Name + " Kernel Version " + osinfo.Version osinfo.VersionId = osinfo.Version osinfo.HomeURL = "www.apple.com" return osinfo, nil }
// Returns system uptime as time.Duration. func getSystemUptime() (uptime time.Duration, err error) { val, err := syscall.Sysctl("kern.boottime") if err != nil { return 0, err } buf := []byte(val) tv := *syscall.Timeval(unsafe.Pointer(&buf[0])) return time.Since(time.Unix(tv.Unix())), nil }
// GetSystemInfo return system info func GetSystemInfo() (*SystemInfo, error) { hostname, err := syscall.Sysctl("kern.hostname") if err != nil || hostname == "" { return nil, errors.New("Can't read hostname info") } os, err := syscall.Sysctl("kern.ostype") if err != nil || os == "" { return nil, errors.New("Can't read os info") } kernel, err := syscall.Sysctl("kern.osrelease") if err != nil || kernel == "" { return nil, errors.New("Can't read kernel info") } arch, err := syscall.Sysctl("kern.version") if err != nil || arch == "" { return nil, errors.New("Can't read arch info") } archSlice := strings.Split(arch, "/") if len(archSlice) != 2 { return nil, errors.New("Can't read arch info") } cleanArch := strings.ToLower(strings.Replace(archSlice[len(archSlice)-1], "RELEASE_", "", -1)) return &SystemInfo{ Hostname: hostname, OS: os, Distribution: DARWIN_OSX, Version: getOSXVersion(), Kernel: kernel, Arch: cleanArch, }, nil }
func init() { if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" { archs, _ := syscall.Sysctl("kern.supported_archs") for _, s := range strings.Split(archs, " ") { if strings.TrimSpace(s) == "amd64" { freebsd32o64 = true break } } } }
// IsVTXDisabled checks if VT-X is disabled in the BIOS. If it is, the vm will fail to start. // If we can't be sure it is disabled, we carry on and will check the vm logs after it's started. func (d *Driver) IsVTXDisabled() bool { errmsg := "Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v" features, err := syscall.Sysctl("machdep.cpu.features") if err != nil { log.Debugf(errmsg, err) return false } disabled := !strings.Contains(features, "VMX") return disabled }
// Distrib returns system distribution and version string. func Distrib() (string, error) { uname, err := unix.Sysctl("kern.version") if err != nil { return "", err } split := strings.Split(uname, " ") if len(split) > 1 { return strings.Join(split[:2], " "), nil } return "FreeBSD", nil }
func init() { if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" { archs, _ := syscall.Sysctl("kern.supported_archs") for _, s := range strings.Fields(archs) { if s == "amd64" { freebsd32o64 = true break } } } }
func getHwMemsize() (uint64, error) { totalString, err := syscall.Sysctl("hw.memsize") if err != nil { return 0, err } // syscall.sysctl() helpfully assumes the result is a null-terminated string and // removes the last byte of the result if it's 0 :/ totalString += "\x00" total := uint64(binary.LittleEndian.Uint64([]byte(totalString))) return total, nil }
func kernelInfo() (map[string]interface{}, error) { info := make(map[string]interface{}) // still need modules kernKeys := map[string]string{"ostype": "name", "osrelease": "release", "version": "version"} for sysctlName, gohaiName := range kernKeys { k, err := syscall.Sysctl("kern." + sysctlName) if err != nil { return nil, err } info[gohaiName] = k } info["os"] = info["name"] return info, nil }
func init() { if runtime.GOOS == "darwin" { // See http://support.apple.com/kb/HT1633. s, err := syscall.Sysctl("kern.osrelease") if err != nil { return } ss := strings.Split(s, ".") if len(ss) == 0 { return } darwinVersion, _ = strconv.Atoi(ss[0]) } }
func cpuInfo() (map[string]interface{}, error) { info := make(map[string]interface{}) cpuKeys := map[string]string{"vendor": "vendor_id", "brand_string": "model_name"} for sysctlName, gohaiName := range cpuKeys { k, err := syscall.Sysctl("machdep.cpu." + sysctlName) if err != nil { return nil, err } info[gohaiName] = k } cpuKeyInts := map[string]string{"model": "model", "family": "family", "stepping": "stepping"} for sysctlName, gohaiName := range cpuKeyInts { k, err := syscall.SysctlUint32("machdep.cpu." + sysctlName) if err != nil { return nil, err } info[gohaiName] = k } hwKeyInts := map[string]string{"physicalcpu": "real", "logicalcpu": "total", "cpufrequency": "mhz"} for sysctlName, gohaiName := range hwKeyInts { k, err := syscall.SysctlUint32("hw." + sysctlName) if err != nil { return nil, err } info[gohaiName] = k } info["mhz"] = info["mhz"].(uint32) / 1000000 cpuFlags, err := syscall.Sysctl("machdep.cpu.features") if err != nil { return nil, err } info["flags"] = strings.Split(strings.ToLower(cpuFlags), " ") return info, nil }
// SetTotal returns the number of bytes from hw.memsize on osx func (m *Mem) SetTotal() { var data interface{} data = &m.Total val, err := syscall.Sysctl("hw.memsize") if err != nil { m.Total = 0 } buf := []byte(val) v := data.(*uint64) *v = *(*uint64)(unsafe.Pointer(&buf[0])) }
func init() { freebsdVersion, _ = syscall.SysctlUint32("kern.osreldate") if freebsdVersion >= 1000000 { sockOpts[ssoMulticastInterface].typ = ssoTypeIPMreqn } if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" { archs, _ := syscall.Sysctl("kern.supported_archs") for _, s := range strings.Fields(archs) { if s == "amd64" { freebsd32o64 = true break } } } }
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { k, err := p.getKProc() if err != nil { return nil, err } v, err := syscall.Sysctl("vm.stats.vm.v_page_size") if err != nil { return nil, err } pageSize := common.LittleEndian.Uint16([]byte(v)) return &MemoryInfoStat{ RSS: uint64(k.KiRssize) * uint64(pageSize), VMS: uint64(k.KiSize), }, nil }
// generic Sysctl buffer unmarshalling func sysctlbyname(name string, data interface{}) (err error) { val, err := syscall.Sysctl(name) if err != nil { return err } buf := []byte(val) switch v := data.(type) { case *uint64: *v = *(*uint64)(unsafe.Pointer(&buf[0])) return } bbuf := bytes.NewBuffer([]byte(val)) return binary.Read(bbuf, binary.LittleEndian, data) }
// Get system load averages. func GetLoadAvg(avg *[3]float64) int { v, err := syscall.Sysctl("vm.loadavg") if err != nil { return -1 } b := []byte(v) var l loadavg = *(*loadavg)(unsafe.Pointer(&b[0])) scale := float64(l.scale) i := 0 for ; i < 3; i++ { avg[i] = float64(l.ldavg[i]) / scale } return i }
func darwinFuseCheckVersion() error { // on OSX, check FUSE version. if runtime.GOOS != "darwin" { return nil } ov, err := syscall.Sysctl("osxfuse.version.number") if err != nil { return err } if strings.HasPrefix(ov, "2.7.") || strings.HasPrefix(ov, "2.8.") { return nil } return fmt.Errorf("osxfuse version %s not supported.\n%s\n%s", ov, "Older versions of osxfuse have kernel panic bugs; please upgrade!", "https://github.com/jbenet/go-ipfs/issues/177") }
func init() { // Seems like kern.osreldate is veiled on latest OS X. We use // kern.osrelease instead. osver, err := syscall.Sysctl("kern.osrelease") if err != nil { return } var i int for i = range osver { if osver[i] != '.' { continue } } // The IP_PKTINFO was introduced in OS X 10.7 (Darwin // 11.0.0). See http://support.apple.com/kb/HT1633. if i > 2 || i == 2 && osver[0] >= '1' && osver[1] >= '1' { supportsPacketInfo = true } }
func loadAvg() ([3]float64, error) { v, err := syscall.Sysctl(sysctl) if err != nil { return [...]float64{-1, -1, -1}, fmt.Errorf("loadavg: sysctl failed: %v", err) } b := []byte(v) var l loadavg = *(*loadavg)(unsafe.Pointer(&b[0])) // BUG(dfc) syscall.Sysctl truncates the last byte (expecting a null terminated string) // so we have no access to the last byte returned. However it looks like on 64bit kernels // this byte is padding, so it all works anyway. scale := float64(l.scale) return [...]float64{ float64(l.ldavg[0]) / scale, float64(l.ldavg[1]) / scale, float64(l.ldavg[2]) / scale, }, nil }