Skip to content

Commit e73f55c

Browse files
author
redanthrax
committed
Merge branch 'FreeBSD' of github.com:redanthrax/go-sysinfo into FreeBSD
2 parents e65df80 + e0d9718 commit e73f55c

File tree

15 files changed

+677
-25
lines changed

15 files changed

+677
-25
lines changed

providers/freebsd/arch.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package freebsd
19+
20+
import (
21+
"syscall"
22+
23+
"github.com/pkg/errors"
24+
)
25+
26+
const hardwareMIB = "hw.machine"
27+
28+
func Architecture() (string, error) {
29+
arch, err := syscall.Sysctl(hardwareMIB)
30+
if err != nil {
31+
return "", errors.Wrap(err, "failed to get architecture")
32+
}
33+
34+
return arch, nil
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package freebsd
19+
20+
import (
21+
"testing"
22+
23+
"github.com/elastic/go-sysinfo/internal/registry"
24+
)
25+
26+
var _ registry.HostProvider = freebsdSystem{}
27+
28+
func TestBootTime(t *testing.T) {
29+
boottime, err := BootTime()
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
t.Log(boottime)
35+
}

providers/freebsd/host_freebsd.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
// +build,freebsd,cgo
19-
2018
package freebsd
2119

2220
// #cgo LDFLAGS: -lkvm
@@ -33,9 +31,9 @@ import (
3331
"github.com/pkg/errors"
3432
"github.com/prometheus/procfs"
3533

36-
"github.com/redanthrax/go-sysinfo/internal/registry"
37-
"github.com/redanthrax/go-sysinfo/providers/shared"
38-
"github.com/redanthrax/go-sysinfo/types"
34+
"github.com/elastic/go-sysinfo/internal/registry"
35+
"github.com/elastic/go-sysinfo/providers/shared"
36+
"github.com/elastic/go-sysinfo/types"
3937
)
4038

4139
func init() {
@@ -59,6 +57,7 @@ func (s freebsdSystem) Host() (types.Host, error) {
5957
}
6058

6159
type host struct {
60+
procFS procFS
6261
info types.HostInfo
6362
}
6463

@@ -244,3 +243,8 @@ type procFS struct {
244243
procfs.FS
245244
mountPoint string
246245
}
246+
247+
func (fs *procFS) path(p ...string) string {
248+
elem := append([]string{fs.mountPoint}, p...)
249+
return filepath.Join(elem...)
250+
}

providers/freebsd/host_freebsd_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
// +build,freebsd,cgo
19-
2018
package freebsd
2119

2220
import (
2321
"encoding/json"
2422
"testing"
2523

26-
"github.com/redanthrax/go-sysinfo/internal/registry"
27-
"github.com/stretchr/testify/assert"
24+
"github.com/elastic/go-sysinfo/internal/registry"
2825
)
2926

3027
var _ registry.HostProvider = freebsdSystem{}
3128

3229
func TestHost(t *testing.T) {
33-
host, err := freebsdSystem{}.Host()
30+
host, err := newFreeBSDSystem("").Host()
3431
if err != nil {
3532
t.Fatal(err)
3633
}
@@ -45,11 +42,8 @@ func TestHostMemoryInfo(t *testing.T) {
4542
if err != nil {
4643
t.Fatal(err)
4744
}
48-
m, err := host.Memory()
45+
_, err = host.Memory()
4946
if err != nil {
5047
t.Fatal(err)
5148
}
52-
53-
assert.EqualValues(t, 8554647552, int64(m.Total))
54-
assert.NotContains(t, m.Metrics, "MemTotal")
5549
}

providers/freebsd/kernel.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package freebsd
19+
20+
import (
21+
"syscall"
22+
23+
"github.com/pkg/errors"
24+
)
25+
26+
const kernelReleaseMIB = "kern.osrelease"
27+
28+
func KernelVersion() (string, error) {
29+
version, err := syscall.Sysctl(kernelReleaseMIB)
30+
if err != nil {
31+
return "", errors.Wrap(err, "failed to get kernel version")
32+
}
33+
34+
return version, nil
35+
}

providers/freebsd/machineid.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package freebsd
19+
20+
import (
21+
"syscall"
22+
23+
"github.com/pkg/errors"
24+
)
25+
26+
const kernelHostUUIDMIB = "kern.hostuuid"
27+
28+
func MachineID() (string, error) {
29+
uuid, err := syscall.Sysctl(kernelHostUUIDMIB)
30+
if err != nil {
31+
return "", errors.Wrap(err, "failed to get machine id")
32+
}
33+
34+
return uuid, nil
35+
}

providers/freebsd/os_freebsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"strings"
2323
"syscall"
2424

25-
"github.com/redanthrax/go-sysinfo/types"
25+
"github.com/elastic/go-sysinfo/types"
2626
)
2727

2828
const ostypeMIB = "kern.ostype"

providers/freebsd/os_freebsd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
"github.com/stretchr/testify/assert"
2424

25-
"github.com/redanthrax/go-sysinfo/types"
25+
"github.com/elastic/go-sysinfo/types"
2626
)
2727

2828
func TestOperatingSystem(t *testing.T) {

providers/freebsd/process_freebsd.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
//+build,freebsd,cgo
19-
2018
package freebsd
2119

2220
// #cgo LDFLAGS: -lkvm -lprocstat
@@ -67,14 +65,15 @@ import "C"
6765

6866
import (
6967
"os"
68+
"io/ioutil"
7069
"strconv"
7170
"strings"
7271
"syscall"
7372
"time"
7473

7574
"github.com/pkg/errors"
7675

77-
"github.com/redanthrax/go-sysinfo/types"
76+
"github.com/elastic/go-sysinfo/types"
7877
)
7978

8079
func getProcInfo(op int, arg int) ([]process, error) {
@@ -103,6 +102,10 @@ func getProcInfo(op int, arg int) ([]process, error) {
103102
return procs, nil
104103
}
105104

105+
func (p *process) path(pa ...string) string {
106+
return p.fs.path(append([]string{strconv.Itoa(p.PID())}, pa...)...)
107+
}
108+
106109
func copyArray(from **C.char) []string {
107110
if from == nil {
108111
return nil
@@ -215,6 +218,7 @@ func getProcCWD(p *process) (string, error) {
215218

216219
type process struct {
217220
pid int
221+
fs procFS
218222
kinfo C.struct_kinfo_proc
219223
}
220224

@@ -300,6 +304,30 @@ func (p *process) User() (types.UserInfo, error) {
300304
}, nil
301305
}
302306

307+
308+
// NetworkStats reports network stats for an individual PID.
309+
func (p *process) NetworkCounters() (*types.NetworkCountersInfo, error) {
310+
snmpRaw, err := ioutil.ReadFile(p.path("net/snmp"))
311+
if err != nil {
312+
return nil, err
313+
}
314+
snmp, err := getNetSnmpStats(snmpRaw)
315+
if err != nil {
316+
return nil, err
317+
}
318+
319+
netstatRaw, err := ioutil.ReadFile(p.path("net/netstat"))
320+
if err != nil {
321+
return nil, err
322+
}
323+
netstat, err := getNetstatStats(netstatRaw)
324+
if err != nil {
325+
return nil, err
326+
}
327+
328+
return &types.NetworkCountersInfo{SNMP: snmp, Netstat: netstat}, nil
329+
}
330+
303331
func (p *process) PID() int {
304332
return p.pid
305333
}

0 commit comments

Comments
 (0)