-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
15 changed files
with
1,012 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.