Skip to content

Commit

Permalink
all: add AIX provider
Browse files Browse the repository at this point in the history
Add a implementation with CGO for AIX.

Process information are retrieved by /proc folder or with libperfstat,
getprocs syscalls.
  • Loading branch information
Clément Chigot committed Jul 17, 2020
1 parent 2e4263c commit a8b20c1
Show file tree
Hide file tree
Showing 15 changed files with 1,012 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added detection of containerized cgroup in Kubernetes [#80](https://github.com/elastic/go-sysinfo/pull/80)
- Add AIX support [#77](https://github.com/elastic/go-sysinfo/pull/77)

### Changed

Expand Down
42 changes: 20 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,23 @@ if handleCounter, ok := process.(types.OpenHandleCounter); ok {
These tables show what methods are implemented as well as the extra interfaces
that are implemented.

| `Host` Features | Darwin | Linux | Windows |
|------------------|--------|-------|---------|
| `Info()` | x | x | x |
| `Memory()` | x | x | x |
| `CPUTimer` | x | x | x |
| `VMStat` | | x | |
| `NetworkCounters`| | x | |

| `Process` Features | Darwin | Linux | Windows |
|------------------------|--------|-------|---------|
| `Info()` | x | x | x |
| `Memory()` | x | x | x |
| `User()` | x | x | x |
| `Parent()` | x | x | x |
| `CPUTimer` | x | x | x |
| `Environment` | x | x | |
| `OpenHandleEnumerator` | | x | |
| `OpenHandleCounter` | | x | |
| `Seccomp` | | x | |
| `Capabilities` | | x | |


| `Host` Features | Darwin | Linux | Windows | AIX/ppc64 |
|------------------|--------|-------|---------|-----------|
| `Info()` | x | x | x | x |
| `Memory()` | x | x | x | x |
| `CPUTimer` | x | x | x | x |
| `VMStat` | | x | | |
| `NetworkCounters`| | x | | |

| `Process` Features | Darwin | Linux | Windows | AIX/ppc64 |
|------------------------|--------|-------|---------|-----------|
| `Info()` | x | x | x | x |
| `Memory()` | x | x | x | x |
| `User()` | x | x | x | x |
| `Parent()` | x | x | x | x |
| `CPUTimer` | x | x | x | x |
| `Environment` | x | x | | x |
| `OpenHandleEnumerator` | | x | | |
| `OpenHandleCounter` | | x | | |
| `Seccomp` | | x | | |
| `Capabilities` | | x | | |
78 changes: 78 additions & 0 deletions providers/aix/boottime_aix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package aix

import (
"encoding/binary"
"os"
"time"

"github.com/pkg/errors"
)

// utmp can't be used by "encoding/binary" if generated by cgo,
// some pads will be missing.
type utmp struct {
User [256]uint8
Id [14]uint8
Line [64]uint8
XPad1 int16
Pid int32
Type int16
XPad2 int16
Time int64
Termination int16
Exit int16
Host [256]uint8
Xdblwordpad int32
XreservedA [2]int32
XreservedV [6]int32
}

const (
typeBootTime = 2
)

// BootTime returns the time at which the machine was started, truncated to the nearest second
func BootTime() (time.Time, error) {
return bootTime("/etc/utmp")
}

func bootTime(filename string) (time.Time, error) {
// Get boot time from /etc/utmp
file, err := os.Open(filename)
if err != nil {
return time.Time{}, errors.Wrap(err, "failed to get host uptime: cannot open /etc/utmp")
}

defer file.Close()

for {
var utmp utmp
if err := binary.Read(file, binary.BigEndian, &utmp); err != nil {
break
}

if utmp.Type == typeBootTime {
return time.Unix(utmp.Time, 0), nil
}
}

return time.Time{}, errors.Wrap(err, "failed to get host uptime: no utmp record")

}
34 changes: 34 additions & 0 deletions providers/aix/boottime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package aix

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBootTime(t *testing.T) {
bt, err := bootTime("testdata/utmp")
if err != nil {
t.Fatal(err)
}

assert.EqualValues(t, bt.Unix(), 1585726535)

}
41 changes: 41 additions & 0 deletions providers/aix/defs_aix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// +build ignore

package aix

/*
#include <sys/types.h>
#include <utmp.h>
#include <sys/procfs.h>
*/
import "C"

type prcred C.prcred_t

type pstatus C.pstatus_t
type prTimestruc64 C.pr_timestruc64_t
type prSigset C.pr_sigset_t
type fltset C.fltset_t
type lwpstatus C.lwpstatus_t
type prSiginfo64 C.pr_siginfo64_t
type prStack64 C.pr_stack64_t
type prSigaction64 C.struct_pr_sigaction64
type prgregset C.prgregset_t
type prfpregset C.prfpregset_t
type pfamily C.pfamily_t
20 changes: 20 additions & 0 deletions providers/aix/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// Package aix implements the HostProvider and ProcessProvider interfaces
// for providing information about MacOS.
package aix
Loading

0 comments on commit a8b20c1

Please sign in to comment.