Skip to content

Commit

Permalink
Merge pull request #504 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 13.5.0
  • Loading branch information
andyone authored Sep 15, 2024
2 parents d64e7ab + b9bb86c commit 8afd234
Show file tree
Hide file tree
Showing 22 changed files with 913 additions and 90 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
## Changelog

### [13.5.0](https://kaos.sh/ek/13.5.0)

- `[support/resources]` Added package for collecting info about CPU and memory
- `[support/kernel]` Added package for collecting OS kernel parameters
- `[system/sysctl]` Added method `All` to get all kernel parameters
- `[system]` Added macOS support for `GetCPUInfo`
- `[system]` Added macOS support for `GetMemUsage`
- `[support]` Added locale to default OS info output
- `[mathutil]` Added methods `IsInt`, `IsFloat`, and `IsNumber`
- `[support]` Added info if CGO is used for build

### [13.4.0](https://kaos.sh/ek/13.4.0)

- `[system/sysctl]` Added new package for reading kernel parameters
Expand Down
30 changes: 30 additions & 0 deletions mathutil/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

func ExampleIsNumber() {
fmt.Println(IsNumber("test"))
fmt.Println(IsNumber("746131"))
fmt.Println(IsNumber("-10.431"))
// Output:
// false
// true
// true
}

func ExampleIsInt() {
fmt.Println(IsInt("test"))
fmt.Println(IsInt("746131"))
fmt.Println(IsInt("-194"))
// Output:
// false
// true
// true
}

func ExampleIsFloat() {
fmt.Println(IsFloat("test"))
fmt.Println(IsFloat("74.6131"))
fmt.Println(IsFloat("-10.4"))
// Output:
// false
// true
// true
}

func ExampleBetween() {
fmt.Println(Between(10, 1, 5))
fmt.Println(Between(-3, 1, 5))
Expand Down
51 changes: 51 additions & 0 deletions mathutil/mathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,57 @@ type NumericNeg interface {

// ////////////////////////////////////////////////////////////////////////////////// //

// IsInt returns true if given string contains int symbols.
//
// Note that this method does not validate the given value.
func IsInt(s string) bool {
if s == "" {
return false
}

for _, r := range s {
switch r {
// - , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
case 45, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57:
// continue
default:
return false
}
}

return true
}

// IsFloat returns true if given string contains float symbols.
//
// Note that this method does not validate the given value.
func IsFloat(s string) bool {
if s == "" {
return false
}

for _, r := range s {
switch r {
// - , . , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
case 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57:
// continue
default:
return false
}
}

return true
}

// IsNumber returns true if given string contains number symbols (int or float).
//
// Note that this method does not validate the given value.
func IsNumber(s string) bool {
return IsInt(s) || IsFloat(s)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Between returns value between min and max values
func Between[N Numeric](val, min, max N) N {
switch {
Expand Down
9 changes: 9 additions & 0 deletions mathutil/mathutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ var _ = Suite(&MathUtilSuite{})

// ////////////////////////////////////////////////////////////////////////////////// //

func (s *MathUtilSuite) TestChecks(c *C) {
c.Assert(IsNumber("1234567890.1"), Equals, true)
c.Assert(IsNumber("1234567890"), Equals, true)
c.Assert(IsInt(""), Equals, false)
c.Assert(IsFloat(""), Equals, false)
c.Assert(IsInt("1234567890a"), Equals, false)
c.Assert(IsFloat("1234567890.1a"), Equals, false)
}

func (s *MathUtilSuite) TestBetween(c *C) {
c.Assert(Between(5, 0, 100), Equals, 5)
c.Assert(Between(10, 0, 5), Equals, 5)
Expand Down
51 changes: 51 additions & 0 deletions support/kernel/kernel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//go:build linux || darwin
// +build linux darwin

// Package kernel provides methods for collecting information from OS kernel
package kernel

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"strings"

"github.com/essentialkaos/ek/v13/strutil"
"github.com/essentialkaos/ek/v13/support"
"github.com/essentialkaos/ek/v13/system/sysctl"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Collect collects info from OS kernel
func Collect(params ...string) []support.KernelParam {
kernelParams, err := sysctl.All()

if err != nil {
return nil
}

var result []support.KernelParam

for _, param := range params {
for k, v := range kernelParams {
if !strings.HasPrefix(k, param) {
continue
}

value := strings.ReplaceAll(v, "\t", " ")
value = strutil.SqueezeRepeats(value, " ")

result = append(result, support.KernelParam{
Key: k,
Value: value,
})
}
}

return result
}
18 changes: 18 additions & 0 deletions support/kernel/kernel_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Package kernel provides methods for collecting information from OS kernel
package kernel

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import "github.com/essentialkaos/ek/v13/support"

// ////////////////////////////////////////////////////////////////////////////////// //

// Collect collects info from OS kernel
func Collect(params ...string) []support.KernelParam {
return nil
}
54 changes: 54 additions & 0 deletions support/resources/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build linux || darwin
// +build linux darwin

// Package resources provides methods for collecting information about system
// resources (cpu/memory)
package resources

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"github.com/essentialkaos/ek/v13/support"
"github.com/essentialkaos/ek/v13/system"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Collect collects info about CPU and memory
func Collect() *support.ResourcesInfo {
result := &support.ResourcesInfo{}

cpuInfo, err1 := system.GetCPUInfo()

if err1 == nil {
for _, p := range cpuInfo {
result.CPU = append(result.CPU, support.CPUInfo{
Model: p.Model,
Cores: p.Cores,
Threads: p.Siblings / p.Cores,
})
}
}

memInfo, err2 := system.GetMemUsage()

if err2 == nil {
result.MemTotal = memInfo.MemTotal
result.MemFree = memInfo.MemFree
result.MemUsed = memInfo.MemUsed
result.SwapTotal = memInfo.SwapTotal
result.SwapFree = memInfo.SwapFree
result.SwapUsed = memInfo.SwapUsed
}

if err1 != nil && err2 != nil {
return nil
}

return result
}
21 changes: 21 additions & 0 deletions support/resources/resources_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Package resources provides methods for collecting information about system
// resources (cpu/memory)
package resources

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"github.com/essentialkaos/ek/v13/support"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Collect collects info about CPU and memory
func Collect() *support.ResourcesInfo {
return nil
}
2 changes: 1 addition & 1 deletion support/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

// Collect collect info about services
// Collect collects info about services
func Collect(services ...string) []support.Service {
var result []support.Service

Expand Down
Loading

0 comments on commit 8afd234

Please sign in to comment.