forked from jumpserver/koko
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
155 additions
and
9 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
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,12 @@ | ||
package model | ||
|
||
const ( | ||
StateKeyCpuLoad1 = "system_cpu_load_1" | ||
StateKeyMemoryUsed = "system_memory_used_percent" | ||
StateKeyDiskUsed = "system_disk_used_percent" | ||
StateKeyActiveSessions = "session_active_count" | ||
) | ||
|
||
const ( | ||
ComponentName = "koko" | ||
) |
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,38 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/jumpserver/koko/pkg/logger" | ||
"github.com/jumpserver/koko/pkg/model" | ||
"github.com/jumpserver/koko/pkg/state" | ||
) | ||
|
||
type StatOption func(states map[string]interface{}) | ||
|
||
func SessionActiveCount(count int) StatOption { | ||
return func(states map[string]interface{}) { | ||
states[model.StateKeyActiveSessions] = count | ||
} | ||
} | ||
|
||
func ReportStat(opts ...StatOption) { | ||
stateData := make(map[string]interface{}) | ||
if cpuLoad := state.CpuLoad1Usage(); cpuLoad > 0 { | ||
stateData[model.StateKeyCpuLoad1] = cpuLoad | ||
} | ||
if diskUsagePercent := state.DiskUsagePercent(); diskUsagePercent > 0 { | ||
stateData[model.StateKeyDiskUsed] = diskUsagePercent | ||
} | ||
if memUsagePercent := state.MemoryUsagePercent(); memUsagePercent > 0 { | ||
stateData[model.StateKeyMemoryUsed] = memUsagePercent | ||
} | ||
for _, setter := range opts { | ||
setter(stateData) | ||
} | ||
var resp interface{} | ||
_, err := authClient.Post(StatURL, stateData, &resp) | ||
if err != nil { | ||
logger.Errorf("Report stat err: %s", err) | ||
return | ||
} | ||
logger.Debugf("Report stat success %v",resp) | ||
} |
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,64 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
|
||
"github.com/shirou/gopsutil/v3/cpu" | ||
"github.com/shirou/gopsutil/v3/disk" | ||
"github.com/shirou/gopsutil/v3/load" | ||
"github.com/shirou/gopsutil/v3/mem" | ||
|
||
"github.com/jumpserver/koko/pkg/config" | ||
"github.com/jumpserver/koko/pkg/logger" | ||
) | ||
|
||
func CpuLoad1Usage() float64 { | ||
var ( | ||
err error | ||
cpuCount int | ||
avgLoadStat *load.AvgStat | ||
) | ||
cpuCount, err = cpu.Counts(true) | ||
avgLoadStat, err = load.Avg() | ||
if err != nil { | ||
logger.Errorf("Get cpu load 1min err: %s", err) | ||
return -1 | ||
} | ||
return convertFloatDecimal(avgLoadStat.Load1 / float64(cpuCount)) | ||
} | ||
|
||
func DiskUsagePercent() float64 { | ||
rootPath := config.GetConf().RootPath | ||
usage, err := disk.Usage(rootPath) | ||
if err != nil { | ||
logger.Errorf("Get disk usage err: %s", err) | ||
return -1 | ||
} | ||
return convertFloatDecimal(usage.UsedPercent) | ||
} | ||
|
||
func MemoryUsagePercent() float64 { | ||
vmStatus, err := mem.VirtualMemory() | ||
if err != nil { | ||
logger.Errorf("Get memory usage err: %s", err) | ||
return -1 | ||
} | ||
return convertFloatDecimal(vmStatus.UsedPercent) | ||
} | ||
|
||
func CurrentLocalIP() string { | ||
conn, err := net.Dial("udp", "8.8.8.8:80") | ||
if err != nil { | ||
logger.Errorf("Get local IP err: %s", err) | ||
} | ||
defer conn.Close() | ||
localAddr := conn.LocalAddr().(*net.UDPAddr) | ||
return localAddr.IP.String() | ||
} | ||
|
||
func convertFloatDecimal(value float64) float64 { | ||
result, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64) | ||
return result | ||
} |
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,12 @@ | ||
package state | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestBasic(t *testing.T) { | ||
t.Log("load cpu==> ", CpuLoad1Usage()) | ||
t.Log("men==> ", MemoryUsagePercent()) | ||
t.Log("disk==> ", DiskUsagePercent()) | ||
t.Log("local ip ==> ", CurrentLocalIP()) | ||
} |