Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add procfd probe for socketmemleaks and update bundle file #44

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions deploy/skoopbundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ data:
event_config:
port: 19102
loki_enable: true
loki_address: loki-service.kubeskoop.svc.cluster.local
loki_address: loki-service
probes:
- tcpreset
- packetloss
Expand Down Expand Up @@ -332,7 +332,7 @@ data:
"name": "prometheus",
"orgId": 1,
"type": "prometheus",
"url": "http://prometheus-service.kubeskoop.svc",
"url": "http://prometheus-service",
"version": 1
}
]
Expand All @@ -358,8 +358,11 @@ spec:
- name: grafana
image: grafana/grafana:latest
ports:
- name: grafana
containerPort: 3000
- name: grafana
containerPort: 3000
env:
- name: GF_SECURITY_ADMIN_PASSWORD
value: "kubeskoop"
resources:
limits:
memory: "1Gi"
Expand Down
24 changes: 12 additions & 12 deletions pkg/exporter/cmd/diag.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// diagCmd represents the diag command
var diagCmd = &cobra.Command{
Use: "diag",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("diag called")
},
}
var (
diagCmd = &cobra.Command{
Use: "diag",
Short: "Run command in the command line to probe metrics and events.",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help() // nolint
},
}

podname string
)

func init() {
rootCmd.AddCommand(diagCmd)
diagCmd.PersistentFlags().StringVarP(&podname, "pod", "i", "", "specified pod")
}
81 changes: 42 additions & 39 deletions pkg/exporter/cmd/diag_metric.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
Expand All @@ -20,57 +17,63 @@ import (
var (
metricCmd = &cobra.Command{
Use: "metric",
Short: "A brief description of your command",
Short: "get metric data in cli",
Run: func(cmd *cobra.Command, args []string) {
if len(probeName) > 0 {
ctx := slog.NewContext(context.Background(), slog.Default())
err := nettop.SyncNetTopology()
if err != nil {
slog.Ctx(ctx).Info("sync nettop", "err", err)
return
}
texts := pterm.TableData{
{"METRIC", "VALUE", "NETNS", "POD", "NAMESPACE", "PROBE"},
if len(probeName) == 0 {
cmd.Help() // nolint
return
}
ctx := slog.NewContext(context.Background(), slog.Default())
err := nettop.SyncNetTopology()
if err != nil {
slog.Ctx(ctx).Info("sync nettop", "err", err)
return
}
texts := pterm.TableData{
{"METRIC", "VALUE", "NETNS", "POD", "NAMESPACE", "PROBE"},
}
for _, p := range probeName {
data, err := probe.CollectOnce(ctx, p)
if err != nil && data == nil {
slog.Ctx(ctx).Info("collect metric", "err", err)
continue
}
for _, p := range probeName {
data, err := probe.CollectOnce(ctx, p)
if err != nil && data == nil {
slog.Ctx(ctx).Info("collect metric", "err", err)
for m, d := range data {
slog.Ctx(ctx).Debug("raw metric msg", "metric", m, "data", d)
// if a probe provide multi subject, only fetch relevant metric data
if !strings.HasPrefix(m, p) {
continue
}
for m, d := range data {
slog.Ctx(ctx).Debug("raw metric msg", "metric", m, "data", d)
// if a probe provide multi subject, only fetch relevant metric data
if !strings.HasPrefix(m, p) {
for nsinum, v := range d {
et, err := nettop.GetEntityByNetns(int(nsinum))
if err != nil {
slog.Ctx(ctx).Info("get entity failed", "netns", nsinum, "err", err)
continue
}
for nsinum, v := range d {
et, err := nettop.GetEntityByNetns(int(nsinum))
if err != nil {
slog.Ctx(ctx).Info("get entity failed", "netns", nsinum, "err", err)
continue
}
texts = append(texts, []string{
m,
fmt.Sprintf("%d", v),
fmt.Sprintf("%d", nsinum),
et.GetPodName(),
et.GetPodNamespace(),
p,
})
}

texts = append(texts, []string{
m,
fmt.Sprintf("%d", v),
fmt.Sprintf("%d", nsinum),
et.GetPodName(),
et.GetPodNamespace(),
p,
})
}

}
pterm.DefaultTable.WithHasHeader().WithData(texts).Render() // nolint
}
pterm.DefaultTable.WithHasHeader().WithData(texts).Render() // nolint

},
}

probeName []string
probeName []string
metricName []string
)

func init() {
diagCmd.AddCommand(metricCmd)

metricCmd.PersistentFlags().StringSliceVarP(&probeName, "probe", "p", []string{}, "probe name to diag")
metricCmd.PersistentFlags().StringSliceVarP(&metricName, "metric", "m", []string{}, "metric name to diag")
}
4 changes: 4 additions & 0 deletions pkg/exporter/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ var (
cmd.Help() // nolint
},
}

output string
)

func init() {
rootCmd.AddCommand(listCmd)

listCmd.PersistentFlags().StringVarP(&output, "output", "o", "text", "output format, support text/json/file")
}
2 changes: 1 addition & 1 deletion pkg/exporter/cmd/list_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var (
entityCmd = &cobra.Command{
Use: "entity",
Short: "list network entity, aka no-hostNetwork pod",
Short: "List all network entities, including all non-hostnetwork pods and the host itself.",
Run: func(cmd *cobra.Command, args []string) {
if LabelSelector != "" {
slct, err := parseLabelSelector(LabelSelector)
Expand Down
30 changes: 21 additions & 9 deletions pkg/exporter/cmd/list_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,41 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"context"

"github.com/alibaba/kubeskoop/pkg/exporter/probe"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"golang.org/x/exp/slog"
)

// eventCmd represents the event command
var eventCmd = &cobra.Command{
Use: "event",
Short: "list all available metrics",
Run: func(cmd *cobra.Command, args []string) {
ctx := slog.NewContext(context.Background(), slog.Default())
if len(listprobe) == 0 {
listprobe = probe.ListMetricProbes(ctx, true)
}

events := probe.ListEvents()
pterm.Print(events)

sliceMapTextOutput("events", events)
},
}

func init() {
listCmd.AddCommand(eventCmd)
}

func sliceMapTextOutput(title string, data map[string][]string) {
tree := pterm.TreeNode{
Text: title,
Children: []pterm.TreeNode{},
}

for p, unit := range data {
parent := pterm.TreeNode{
Text: p,
Children: []pterm.TreeNode{},
}
for i := range unit {
parent.Children = append(parent.Children, pterm.TreeNode{Text: unit[i]})
}
tree.Children = append(tree.Children, parent)
}
pterm.DefaultTree.WithRoot(tree).Render() // nolint
}
46 changes: 36 additions & 10 deletions pkg/exporter/cmd/list_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,59 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"context"
"strings"

"github.com/alibaba/kubeskoop/pkg/exporter/probe"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"golang.org/x/exp/slog"
)

// metricCmd represents the metric command
var (
listmetricCmd = &cobra.Command{
Use: "metric",
Short: "list all available metrics",
Short: "list available metrics of probe",
Run: func(cmd *cobra.Command, args []string) {
ctx := slog.NewContext(context.Background(), slog.Default())
if len(listprobe) == 0 {
listprobe = probe.ListMetricProbes(ctx, true)
showprobes := []string{}
allprobes := probe.ListMetricProbes()
for idx := range listprobe {
for _, probe := range allprobes {
if strings.Contains(probe, listprobe[idx]) {
showprobes = append(showprobes, probe)
break
}
}
}

if len(showprobes) == 0 {
showprobes = allprobes
}

metriclist := []string{}
tree := pterm.TreeNode{
Text: "metrics",
Children: []pterm.TreeNode{},
}
metrics := probe.ListMetrics()
for _, p := range listprobe {
for _, p := range showprobes {
if mnames, ok := metrics[p]; ok {
metriclist = append(metriclist, mnames...)
parent := pterm.TreeNode{
Text: p,
Children: []pterm.TreeNode{},
}
for i := range mnames {
if !strings.HasPrefix(mnames[i], p) {
continue
}
children := pterm.TreeNode{
Text: mnames[i],
}
parent.Children = append(parent.Children, children)
}
tree.Children = append(tree.Children, parent)
}
}
pterm.Print(metriclist)

pterm.DefaultTree.WithRoot(tree).Render() // nolint

},
}
Expand Down
34 changes: 25 additions & 9 deletions pkg/exporter/cmd/list_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"context"
"encoding/json"
"fmt"

"github.com/alibaba/kubeskoop/pkg/exporter/probe"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"golang.org/x/exp/slog"
)
Expand All @@ -16,18 +16,34 @@ import (
var (
probeCmd = &cobra.Command{
Use: "probe",
Short: "A brief description of your command",
Short: "list supported probe with metric exporting",
Run: func(cmd *cobra.Command, args []string) {
ctx := slog.NewContext(context.Background(), slog.Default())
pls := probe.ListMetricProbes(ctx, avail)
pterm.Println(pls)
res := map[string][]string{
"metric": {},
"event": {},
}
res["metric"] = probe.ListMetricProbes()

els := probe.ListEvents()
for ep := range els {
res["event"] = append(res["event"], ep)
}

switch output {
case "json":
text, err := json.MarshalIndent(res, "", " ")
if err != nil {
slog.Warn("json marshal failed", "err", err)
return
}
fmt.Println(string(text))
default:
sliceMapTextOutput("probes", res)
}
},
}

avail bool
)

func init() {
listCmd.AddCommand(probeCmd)
probeCmd.PersistentFlags().BoolVarP(&avail, "avail", "a", false, "list all available probes")
}
2 changes: 1 addition & 1 deletion pkg/exporter/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func status(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
res := make(map[string]bool)
for _, pn := range probe.ListMetricProbes(context.Background(), true) {
for _, pn := range probe.ListMetricProbes() {
p := probe.GetProbe(pn)
res[p.Name()] = p.Ready()
}
Expand Down
Loading