Skip to content

Commit

Permalink
feat: 上报监控指标
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Dec 10, 2020
1 parent 49b0066 commit 56ec450
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/pkg/sftp v1.12.0
github.com/satori/go.uuid v1.2.0
github.com/sevlyar/go-daemon v0.1.5
github.com/shirou/gopsutil/v3 v3.20.11
github.com/sirupsen/logrus v1.4.2
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c
Expand Down
12 changes: 7 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/LeeEirc/ssh v0.1.2-0.20201111074515-e8272f1a6534 h1:x4gr2rn068pHL5Nvd
github.com/LeeEirc/ssh v0.1.2-0.20201111074515-e8272f1a6534/go.mod h1:Y4MILF5d5xIGmsN5GLJryvyyLRq1kYkbA1V10UWCszs=
github.com/LeeEirc/tclientlib v0.0.0-20201208031857-c210dd977a04 h1:j2XxH6wbtZUHMc10CUIqWNPw4Z8COMfzWNlMnsvZmUM=
github.com/LeeEirc/tclientlib v0.0.0-20201208031857-c210dd977a04/go.mod h1:TF2v0XZYyRcZfx4NmA/EEFRkdKZLsQd8YnlhGKl1KUA=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/aliyun/aliyun-oss-go-sdk v1.9.8 h1:BOflvK0Zs/zGmoabyFIzTg5c3kguktWTXEwewwbuba0=
github.com/aliyun/aliyun-oss-go-sdk v1.9.8/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
Expand All @@ -32,6 +34,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/form v3.1.4+incompatible h1:lvKiHVxE2WvzDIoyMnWcjyiBxKt2+uFJyZcPYWsLnjI=
Expand Down Expand Up @@ -88,13 +92,14 @@ github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sevlyar/go-daemon v0.1.5 h1:Zy/6jLbM8CfqJ4x4RPr7MJlSKt90f00kNM1D401C+Qk=
github.com/sevlyar/go-daemon v0.1.5/go.mod h1:6dJpPatBT9eUwM5VCw9Bt6CdX9Tk6UWvhW3MebLDRKE=
github.com/shirou/gopsutil/v3 v3.20.11 h1:NeVf1K0cgxsWz+N3671ojRptdgzvp7BXL3KV21R0JnA=
github.com/shirou/gopsutil/v3 v3.20.11/go.mod h1:igHnfak0qnw1biGeI2qKQvu0ZkwvEkUcCLlYhZzdr/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand All @@ -106,16 +111,14 @@ github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mB
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d h1:MiWWjyhUzZ+jvhZvloX6ZrUsdEghn8a64Upd8EMHglE=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -133,7 +136,6 @@ gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXa
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 h1:AFxeG48hTWHhDTQDk/m2gorfVHUEa9vo3tp3D7TzwjI=
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
11 changes: 10 additions & 1 deletion pkg/koko/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Initial() {
}

go keepHeartbeat()
go keepReportState()
}

// uploadRemainReplay 上传遗留的录像
Expand Down Expand Up @@ -81,7 +82,7 @@ func uploadRemainReplay(rootPath string) {
// keepHeartbeat 保持心跳
func keepHeartbeat() {
for {
time.Sleep(config.GetConf().HeartbeatDuration * time.Second)
time.Sleep(30 * time.Second)
data := proxy.GetAliveSessions()
tasks := service.TerminalHeartBeat(data)
if len(tasks) != 0 {
Expand All @@ -92,6 +93,14 @@ func keepHeartbeat() {
}
}

func keepReportState() {
for {
time.Sleep(30 * time.Second)
service.ReportStat(service.SessionActiveCount(
proxy.GetAliveSessionCount()))
}
}

func ValidateRemainReplayFile(path string) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND, os.ModePerm)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/model/state.go
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"
)
6 changes: 5 additions & 1 deletion pkg/proxy/sessmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ func GetAliveSessions() []string {
return sids
}

func GetAliveSessionCount() int {
lock.RLock()
defer lock.RUnlock()
return len(sessionMap)
}

func AddSession(sw Session) {
lock.Lock()
defer lock.Unlock()
sessionMap[sw.SessionID()] = sw
}


func postSession(data map[string]interface{}) bool {
for i := 0; i < 5; i++ {
if service.CreateSession(data) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func KeepSyncConfigWithServer(ctx context.Context) {
case <-ticker.C:
err := LoadConfigFromServer()
if err != nil {
logger.Warn("Sync config with server error: ", err)
logger.Errorf("Sync config with server error: %s", err)
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/service/state.go
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)
}
2 changes: 1 addition & 1 deletion pkg/service/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func RegisterTerminal(name, token, comment string) (res model.Terminal) {
client := newClient()
client.Headers["Authorization"] = fmt.Sprintf("BootstrapToken %s", token)
data := map[string]string{"name": name, "comment": comment}
data := map[string]string{"name": name, "comment": comment, "type": model.ComponentName}
_, err := client.Post(TerminalRegisterURL, data, &res)
if err != nil {
logger.Error(err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/service/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ const (
const (
NotificationCommandURL = "/api/v1/terminal/commands/insecure-command/"
)

const (
StatURL = "/api/v1/terminal/components/state/"
)
64 changes: 64 additions & 0 deletions pkg/state/basic.go
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
}
12 changes: 12 additions & 0 deletions pkg/state/basic_test.go
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())
}

0 comments on commit 56ec450

Please sign in to comment.