|
| 1 | +/*___INFO__MARK_BEGIN__*/ |
| 2 | +/************************************************************************* |
| 3 | +* Copyright 2025 HPC-Gridware GmbH |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +* |
| 17 | +************************************************************************/ |
| 18 | +/*___INFO__MARK_END__*/ |
| 19 | + |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "encoding/json" |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + "strconv" |
| 28 | + |
| 29 | + qacct "github.com/hpc-gridware/go-clusterscheduler/pkg/qacct/v9.0" |
| 30 | + "github.com/mark3labs/mcp-go/mcp" |
| 31 | +) |
| 32 | + |
| 33 | +// registerAccountingTools registers all job accounting related tools |
| 34 | +func registerAccountingTools(s *SchedulerServer, config SchedulerServerConfig) error { |
| 35 | + // Add qacct tool |
| 36 | + s.server.AddTool(mcp.NewTool( |
| 37 | + "qacct", |
| 38 | + mcp.WithDescription("Retrieves accounting information about finished jobs in the Gridware Cluster Scheduler. This tool allows querying job history, resource usage, and execution details for completed jobs. Use with various options or specify job IDs to get detailed information about specific jobs."), |
| 39 | + mcp.WithArray("arguments", |
| 40 | + mcp.Description("Command line arguments for qacct (e.g., '-j 123' for job information, '-u username' for user jobs, '-help' for help documentation)."), |
| 41 | + ), |
| 42 | + ), func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 43 | + log.Printf("Executing qacct command") |
| 44 | + |
| 45 | + // Get optional arguments |
| 46 | + var arguments []string |
| 47 | + if args, ok := req.Params.Arguments["arguments"].([]interface{}); ok { |
| 48 | + for _, arg := range args { |
| 49 | + if strArg, ok := arg.(string); ok { |
| 50 | + arguments = append(arguments, strArg) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Execute qacct command |
| 56 | + output, err := getAccountingInfo(ctx, arguments) |
| 57 | + if err != nil { |
| 58 | + log.Printf("Failed to execute qacct: %v", err) |
| 59 | + return &mcp.CallToolResult{ |
| 60 | + Content: []mcp.Content{ |
| 61 | + mcp.TextContent{ |
| 62 | + Type: "text", |
| 63 | + Text: fmt.Sprintf("Failed to execute qacct command: %v", err), |
| 64 | + }, |
| 65 | + }, |
| 66 | + IsError: true, |
| 67 | + }, nil |
| 68 | + } |
| 69 | + |
| 70 | + log.Printf("Successfully executed qacct command") |
| 71 | + |
| 72 | + return &mcp.CallToolResult{ |
| 73 | + Content: []mcp.Content{ |
| 74 | + mcp.TextContent{ |
| 75 | + Type: "text", |
| 76 | + Text: output, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, nil |
| 80 | + }) |
| 81 | + |
| 82 | + // Add job_details tool |
| 83 | + s.server.AddTool(mcp.NewTool( |
| 84 | + "job_details", |
| 85 | + mcp.WithDescription("Retrieves detailed accounting information about finished jobs in a structured format. This tool returns comprehensive data about job execution, including resource usage, submission parameters, and execution timelines. Specify job IDs to get information about specific jobs or leave empty to get details for all finished jobs."), |
| 86 | + mcp.WithArray("job_ids", |
| 87 | + mcp.Description("List of job IDs to retrieve details for. If omitted, details for all finished jobs will be returned."), |
| 88 | + ), |
| 89 | + ), func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 90 | + log.Printf("Retrieving job details") |
| 91 | + |
| 92 | + // Parse job IDs if provided |
| 93 | + var jobIDs []int64 |
| 94 | + if ids, ok := req.Params.Arguments["job_ids"].([]interface{}); ok && len(ids) > 0 { |
| 95 | + for _, id := range ids { |
| 96 | + // Handle different possible formats (string, number) |
| 97 | + switch v := id.(type) { |
| 98 | + case string: |
| 99 | + if jobID, err := strconv.ParseInt(v, 10, 64); err == nil { |
| 100 | + jobIDs = append(jobIDs, jobID) |
| 101 | + } else { |
| 102 | + log.Printf("Invalid job ID format: %s", v) |
| 103 | + } |
| 104 | + case float64: |
| 105 | + jobIDs = append(jobIDs, int64(v)) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Get job details |
| 111 | + output, err := getStructuredJobDetails(ctx, jobIDs) |
| 112 | + if err != nil { |
| 113 | + log.Printf("Failed to get job details: %v", err) |
| 114 | + return &mcp.CallToolResult{ |
| 115 | + Content: []mcp.Content{ |
| 116 | + mcp.TextContent{ |
| 117 | + Type: "text", |
| 118 | + Text: fmt.Sprintf("Failed to retrieve job details: %v", err), |
| 119 | + }, |
| 120 | + }, |
| 121 | + IsError: true, |
| 122 | + }, nil |
| 123 | + } |
| 124 | + |
| 125 | + log.Printf("Successfully retrieved job details") |
| 126 | + |
| 127 | + return &mcp.CallToolResult{ |
| 128 | + Content: []mcp.Content{ |
| 129 | + mcp.TextContent{ |
| 130 | + Type: "text", |
| 131 | + Text: output, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, nil |
| 135 | + }) |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +// Helper functions for job accounting |
| 141 | + |
| 142 | +// getAccountingInfo executes qacct with the given arguments |
| 143 | +func getAccountingInfo(ctx context.Context, args []string) (string, error) { |
| 144 | + qa, err := qacct.NewCommandLineQAcct(qacct.CommandLineQAcctConfig{ |
| 145 | + Executable: "qacct", |
| 146 | + }) |
| 147 | + if err != nil { |
| 148 | + return "", fmt.Errorf("internal error: failed to initialize qacct command line tool: %v", err) |
| 149 | + } |
| 150 | + |
| 151 | + output, err := qa.NativeSpecification(args) |
| 152 | + if err != nil { |
| 153 | + return "", fmt.Errorf("internal error: failed to execute qacct command: %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + return output, nil |
| 157 | +} |
| 158 | + |
| 159 | +// getStructuredJobDetails retrieves job details using qacct |
| 160 | +func getStructuredJobDetails(ctx context.Context, jobIDs []int64) (string, error) { |
| 161 | + qa, err := qacct.NewCommandLineQAcct(qacct.CommandLineQAcctConfig{ |
| 162 | + Executable: "qacct", |
| 163 | + }) |
| 164 | + if err != nil { |
| 165 | + return "", fmt.Errorf("internal error: failed to initialize qacct command line tool: %v", err) |
| 166 | + } |
| 167 | + |
| 168 | + jobDetails, err := qa.ShowJobDetails(jobIDs) |
| 169 | + if err != nil { |
| 170 | + return "", fmt.Errorf("internal error: failed to show job details: %v", err) |
| 171 | + } |
| 172 | + |
| 173 | + // Convert job details to JSON for structured output |
| 174 | + data, err := json.MarshalIndent(jobDetails, "", " ") |
| 175 | + if err != nil { |
| 176 | + return "", fmt.Errorf("internal error: failed to format job details: %v", err) |
| 177 | + } |
| 178 | + |
| 179 | + return string(data), nil |
| 180 | +} |
0 commit comments