-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: collect pids/threads count (#105)
- Loading branch information
1 parent
3b36c57
commit 7ca97a9
Showing
5 changed files
with
143 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0. | ||
*/ | ||
|
||
package handlers | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/shirou/gopsutil/v3/process" | ||
"github.com/traas-stack/holoinsight-agent/cmd/containerhelper/model" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Count the number of thread | ||
func countThreadHandler(_ string, resp *model.Resp) error { | ||
pids, err := process.Pids() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
values := make(map[string]interface{}) | ||
totalThreads := readTotalThreads(pids) | ||
values["process_pids"] = len(pids) | ||
values["process_threads"] = totalThreads | ||
resp.Data = values | ||
return nil | ||
} | ||
|
||
// Only runs on Linux | ||
// Get total threads count of OS | ||
func readTotalThreads(pids []int32) int { | ||
totalThreads := int64(0) | ||
for _, pid := range pids { | ||
path := fmt.Sprintf("/proc/%d/status", pid) | ||
file, err := os.Open(path) | ||
if err != nil { | ||
continue | ||
} else { | ||
scanner := bufio.NewScanner(file) | ||
scanner.Split(bufio.ScanLines) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.HasPrefix(line, "Threads:\t") { | ||
x, err := strconv.ParseInt(line[len("Threads:\t"):], 10, 64) | ||
if err != nil { | ||
continue | ||
} else { | ||
totalThreads += x | ||
} | ||
} | ||
} | ||
file.Close() | ||
} | ||
} | ||
return int(totalThreads) | ||
} |
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,19 @@ | ||
/* | ||
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0. | ||
*/ | ||
|
||
package thread | ||
|
||
import ( | ||
"github.com/traas-stack/holoinsight-agent/pkg/collecttask" | ||
"github.com/traas-stack/holoinsight-agent/pkg/plugin/api" | ||
"github.com/traas-stack/holoinsight-agent/pkg/plugin/input/standard/providers" | ||
) | ||
|
||
func init() { | ||
providers.RegisterInputProvider("countthreadtask", func(task *collecttask.CollectTask) (api.Input, error) { | ||
return &input{ | ||
task: task, | ||
}, nil | ||
}) | ||
} |
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,63 @@ | ||
/* | ||
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0. | ||
*/ | ||
|
||
package thread | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/spf13/cast" | ||
cm "github.com/traas-stack/holoinsight-agent/cmd/containerhelper/model" | ||
"github.com/traas-stack/holoinsight-agent/pkg/collecttask" | ||
"github.com/traas-stack/holoinsight-agent/pkg/core" | ||
"github.com/traas-stack/holoinsight-agent/pkg/cri" | ||
"github.com/traas-stack/holoinsight-agent/pkg/cri/criutils" | ||
"github.com/traas-stack/holoinsight-agent/pkg/ioc" | ||
"github.com/traas-stack/holoinsight-agent/pkg/logger" | ||
"github.com/traas-stack/holoinsight-agent/pkg/model" | ||
"github.com/traas-stack/holoinsight-agent/pkg/plugin/api" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ( | ||
input struct { | ||
task *collecttask.CollectTask | ||
} | ||
) | ||
|
||
func (i *input) GetDefaultPrefix() string { | ||
return "" | ||
} | ||
|
||
func (i *input) Collect(a api.Accumulator) error { | ||
if !i.task.Target.IsTypePod() { | ||
return nil | ||
} | ||
|
||
biz, err := criutils.GetMainBizContainerE(ioc.Crii, i.task.Target.GetNamespace(), i.task.Target.GetPodName()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r, err := ioc.Crii.Exec(context.Background(), biz, cri.ExecRequest{ | ||
Cmd: []string{core.HelperToolPath, "countThread"}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
resp := &cm.Resp{} | ||
if err := json.NewDecoder(r.Stdout).Decode(resp); err != nil { | ||
return err | ||
} | ||
data := resp.Data.(map[string]interface{}) | ||
logger.Infoz("test thread count ", zap.Any("result", data)) | ||
for metric, value := range data { | ||
a.AddMetric(&model.Metric{ | ||
Name: metric, | ||
Tags: make(map[string]string), | ||
Value: cast.ToFloat64(value), | ||
}) | ||
} | ||
return nil | ||
} |