|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | + "github.com/sysdiglabs/sysdig-mcp-server/internal/infra/sysdig" |
| 13 | +) |
| 14 | + |
| 15 | +type TroubleshootKubernetesListTopCPUConsumedByContainer struct { |
| 16 | + SysdigClient sysdig.ExtendedClientWithResponsesInterface |
| 17 | +} |
| 18 | + |
| 19 | +func NewTroubleshootKubernetesListTopCPUConsumedByContainer(sysdigClient sysdig.ExtendedClientWithResponsesInterface) *TroubleshootKubernetesListTopCPUConsumedByContainer { |
| 20 | + return &TroubleshootKubernetesListTopCPUConsumedByContainer{ |
| 21 | + SysdigClient: sysdigClient, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func (t *TroubleshootKubernetesListTopCPUConsumedByContainer) RegisterInServer(s *server.MCPServer) { |
| 26 | + tool := mcp.NewTool("troubleshoot_kubernetes_list_top_cpu_consumed_by_container", |
| 27 | + mcp.WithDescription("Identifies the Kubernetes containers consuming the most CPU (in cores)."), |
| 28 | + mcp.WithString("cluster_name", mcp.Description("The name of the cluster to filter by.")), |
| 29 | + mcp.WithString("namespace_name", mcp.Description("The name of the namespace to filter by.")), |
| 30 | + mcp.WithString("workload_type", mcp.Description("The type of the workload to filter by.")), |
| 31 | + mcp.WithString("workload_name", mcp.Description("The name of the workload to filter by.")), |
| 32 | + mcp.WithNumber("limit", |
| 33 | + mcp.Description("Maximum number of containers to return."), |
| 34 | + mcp.DefaultNumber(20), |
| 35 | + ), |
| 36 | + mcp.WithOutputSchema[map[string]any](), |
| 37 | + WithRequiredPermissions("promql.exec"), |
| 38 | + ) |
| 39 | + s.AddTool(tool, t.handle) |
| 40 | +} |
| 41 | + |
| 42 | +func (t *TroubleshootKubernetesListTopCPUConsumedByContainer) handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 43 | + clusterName := mcp.ParseString(request, "cluster_name", "") |
| 44 | + namespaceName := mcp.ParseString(request, "namespace_name", "") |
| 45 | + workloadType := mcp.ParseString(request, "workload_type", "") |
| 46 | + workloadName := mcp.ParseString(request, "workload_name", "") |
| 47 | + limit := mcp.ParseInt(request, "limit", 20) |
| 48 | + |
| 49 | + query := buildTopCPUConsumedByContainerQuery(clusterName, namespaceName, workloadType, workloadName, limit) |
| 50 | + |
| 51 | + params := &sysdig.GetQueryV1Params{ |
| 52 | + Query: query, |
| 53 | + } |
| 54 | + |
| 55 | + httpResp, err := t.SysdigClient.GetQueryV1(ctx, params) |
| 56 | + if err != nil { |
| 57 | + return mcp.NewToolResultErrorFromErr("failed to get top cpu consumed by container", err), nil |
| 58 | + } |
| 59 | + |
| 60 | + if httpResp.StatusCode != 200 { |
| 61 | + bodyBytes, _ := io.ReadAll(httpResp.Body) |
| 62 | + return mcp.NewToolResultErrorf("failed to get top cpu consumed by container: status code %d, body: %s", httpResp.StatusCode, string(bodyBytes)), nil |
| 63 | + } |
| 64 | + |
| 65 | + var queryResponse sysdig.QueryResponseV1 |
| 66 | + if err := json.NewDecoder(httpResp.Body).Decode(&queryResponse); err != nil { |
| 67 | + return mcp.NewToolResultErrorFromErr("failed to decode response", err), nil |
| 68 | + } |
| 69 | + |
| 70 | + return mcp.NewToolResultJSON(queryResponse) |
| 71 | +} |
| 72 | + |
| 73 | +func buildTopCPUConsumedByContainerQuery(clusterName, namespaceName, workloadType, workloadName string, limit int) string { |
| 74 | + filters := []string{} |
| 75 | + if clusterName != "" { |
| 76 | + filters = append(filters, fmt.Sprintf(`kube_cluster_name="%s"`, clusterName)) |
| 77 | + } |
| 78 | + if namespaceName != "" { |
| 79 | + filters = append(filters, fmt.Sprintf(`kube_namespace_name="%s"`, namespaceName)) |
| 80 | + } |
| 81 | + if workloadType != "" { |
| 82 | + filters = append(filters, fmt.Sprintf(`kube_workload_type="%s"`, workloadType)) |
| 83 | + } |
| 84 | + if workloadName != "" { |
| 85 | + filters = append(filters, fmt.Sprintf(`kube_workload_name="%s"`, workloadName)) |
| 86 | + } |
| 87 | + |
| 88 | + filterString := "" |
| 89 | + if len(filters) > 0 { |
| 90 | + filterString = fmt.Sprintf("{%s}", strings.Join(filters, ",")) |
| 91 | + } |
| 92 | + |
| 93 | + return fmt.Sprintf("topk(%d, sum by (kube_cluster_name, kube_namespace_name, kube_workload_type, kube_workload_name, container_label_io_kubernetes_container_name)(sysdig_container_cpu_cores_used%s))", limit, filterString) |
| 94 | +} |
0 commit comments