Skip to content

Commit

Permalink
Merge pull request #1 from LokeshOpsramp/master
Browse files Browse the repository at this point in the history
merge upstream chanages
  • Loading branch information
opsrampdeveloper authored Oct 18, 2023
2 parents 045c748 + 076d8d7 commit 70526ce
Show file tree
Hide file tree
Showing 79 changed files with 1,490 additions and 471 deletions.
13 changes: 13 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Security Policy

## Supported Versions

Security updates are applied only to the latest release.

## Reporting a Vulnerability

If you have discovered a security vulnerability in this project, please report it privately. **Do not disclose it as a public issue.** This gives us time to work with you to fix the issue before public exposure, reducing the chance that the exploit will be used before a patch is released.

Please disclose it at [Security Advisories](https://github.com/shirou/gopsutil/security/advisories/new).

This project is maintained by a team of volunteers on a reasonable-effort basis. As such, vulnerability reports will be investigated and fixed or disclosed as soon as possible.
8 changes: 4 additions & 4 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ jobs:
fail-fast: false
matrix:
go-version: ${{fromJson(needs.go-versions.outputs.versions)}}
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- id: cache-paths
run: |
echo "::set-output name=cache::$(go env GOCACHE)"
echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
- name: Cache go modules
uses: actions/cache@v3
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
with:
path: |
${{ steps.cache-paths.outputs.cache }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
pull-requests: write # for actions/labeler to add labels to PRs
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v4
- uses: actions/labeler@ac9175f8a1f3625fd0d4fb234536d26811351594 # v4.3.0
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
7 changes: 4 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Setup go
uses: actions/setup-go@v3
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: 1.17
cache: false
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- name: Setup golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0
with:
args: --verbose
version: latest
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- name: Release
run: make release
25 changes: 25 additions & 0 deletions .github/workflows/sbom_generator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: SBOM Generator

on:
push:
branches: [ "master" ]

workflow_dispatch:

permissions: read-all

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- uses: advanced-security/sbom-generator-action@375dee8e6144d9fd0ec1f5667b4f6fb4faacefed # v0.0.1
id: sbom
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
path: ${{steps.sbom.outputs.fileName }}
name: "SBOM"
4 changes: 2 additions & 2 deletions .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ jobs:
name: Shellcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- name: Run ShellCheck
uses: ludeeus/action-shellcheck@master
uses: ludeeus/action-shellcheck@00cae500b08a931fb5698e11e79bfbd38e612a38 # v2.0.0
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ jobs:
fail-fast: false
matrix:
go-version: ${{fromJson(needs.go-versions.outputs.versions)}}
os: [ubuntu-20.04, ubuntu-18.04, windows-2019, macos-11]
os: [ubuntu-22.04, ubuntu-20.04, windows-2022, windows-2019, macos-11, macos-12]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- id: go-env
run: |
echo "::set-output name=cache::$(go env GOCACHE)"
echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
- name: Cache go modules
uses: actions/cache@v3
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
with:
path: |
${{ steps.go-env.outputs.cache }}
Expand Down
40 changes: 30 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
issues:
max-same-issues: 0
exclude-rules:
- linters:
- gosec
text: "G204"
- linters:
- revive
text: "var-naming"
- linters:
- revive
text: "exported"
exclude-rules:
- linters:
- gosec
text: "G204"
- linters:
- revive
text: "var-naming"
- linters:
- revive
text: "exported"
- linters:
- revive
text: "empty-block"
- linters:
- revive
text: "unused-parameter"
linters:
enable:
- asciicheck
Expand All @@ -20,6 +26,7 @@ linters:
- gofmt
- gofumpt
- goimports
- gomodguard
- gosec
- gosimple
- importas
Expand All @@ -40,3 +47,16 @@ linters:
- structcheck
- unused
- varcheck
linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/shirou)
gomodguard:
blocked:
modules:
- io/ioutil:
recommandations:
- io
- os
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ environment variable.
You can set an alternative location to `/proc/N/mountinfo` by setting the
`HOST_PROC_MOUNTINFO` environment variable.

### Adding settings using `context` (from v3.23.6)

As of v3.23.6, it is now possible to pass a path location using `context`: import `"github.com/shirou/gopsutil/v3/common"` and pass a context with `common.EnvMap` set to `common.EnvKey`, and the location will be used within each function.

```
ctx := context.WithValue(context.Background(),
common.EnvKey, common.EnvMap{common.HostProcEnvKey: "/myproc"},
)
v, err := mem.VirtualMemoryWithContext(ctx)
```

First priority is given to the value set in `context`, then the value from the environment variable, and finally the default location.

## Documentation

See https://pkg.go.dev/github.com/shirou/gopsutil/v3 or https://godocs.io/github.com/shirou/gopsutil/v3
Expand Down Expand Up @@ -154,7 +167,7 @@ will provide useful information.
- system wide stats on netfilter conntrack module
- sourced from /proc/sys/net/netfilter/nf_conntrack_count

Some code is ported from Ohai. many thanks.
Some code is ported from Ohai. Many thanks.

## Current Status

Expand Down Expand Up @@ -219,9 +232,9 @@ Some code is ported from Ohai. many thanks.
|rlimit |x | | | | |
|num\_handlers | | | | | |
|threads |x | | | | |
|cpu\_percent |x | |x |x | |
|cpu\_percent |x | |x |x |x |
|cpu\_affinity | | | | | |
|memory\_percent | | | | | |
|memory\_percent |x | | | |x |
|parent |x | |x |x |x |
|children |x |x |x |x |x |
|connections |x | |x |x | |
Expand Down
23 changes: 23 additions & 0 deletions common/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package common

type EnvKeyType string

// EnvKey is a context key that can be used to set programmatically the environment
// gopsutil relies on to perform calls against the OS.
// Example of use:
//
// ctx := context.WithValue(context.Background(), common.EnvKey, EnvMap{common.HostProcEnvKey: "/myproc"})
// avg, err := load.AvgWithContext(ctx)
var EnvKey = EnvKeyType("env")

const (
HostProcEnvKey EnvKeyType = "HOST_PROC"
HostSysEnvKey EnvKeyType = "HOST_SYS"
HostEtcEnvKey EnvKeyType = "HOST_ETC"
HostVarEnvKey EnvKeyType = "HOST_VAR"
HostRunEnvKey EnvKeyType = "HOST_RUN"
HostDevEnvKey EnvKeyType = "HOST_DEV"
HostRootEnvKey EnvKeyType = "HOST_ROOT"
)

type EnvMap map[EnvKeyType]string
15 changes: 10 additions & 5 deletions cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

"github.com/shoenig/go-m1cpu"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -85,11 +86,15 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
c.CacheSize = int32(cacheSize)
c.VendorID, _ = unix.Sysctl("machdep.cpu.vendor")

// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
if err == nil {
c.Mhz = float64(cpuFrequency) / 1000000.0
if m1cpu.IsAppleSilicon() {
c.Mhz = float64(m1cpu.PCoreHz() / 1_000_000)
} else {
// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
if err == nil {
c.Mhz = float64(cpuFrequency) / 1000000.0
}
}

return append(ret, c), nil
Expand Down
33 changes: 33 additions & 0 deletions cpu/cpu_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build darwin
// +build darwin

package cpu

import (
"testing"

"github.com/shoenig/go-m1cpu"
)

func Test_CpuInfo_AppleSilicon(t *testing.T) {
if !m1cpu.IsAppleSilicon() {
t.Skip("wrong cpu type")
}

v, err := Info()
if err != nil {
t.Errorf("cpu info should be implemented on darwin systems")
}

for _, vv := range v {
if vv.ModelName == "" {
t.Errorf("could not get CPU info: %v", vv)
}
if vv.Mhz <= 0 {
t.Errorf("could not get frequency of: %s", vv.ModelName)
}
if vv.Mhz > 6000 {
t.Errorf("cpu frequency is absurdly high value: %f MHz", vv.Mhz)
}
}
}
4 changes: 2 additions & 2 deletions cpu/cpu_fallback.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows && !dragonfly && !plan9 && !aix
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9,!aix
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows && !dragonfly && !plan9 && !aix
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows,!dragonfly,!plan9,!aix

package cpu

Expand Down
Loading

0 comments on commit 70526ce

Please sign in to comment.