Skip to content

Commit 34ef566

Browse files
authored
Merge pull request #1 from kitproj/copilot/add-splunk-cli-functionality
Implement Splunk CLI & MCP server for ad-hoc queries following jira-cli pattern
2 parents 63cb8b7 + 7499bec commit 34ef566

12 files changed

Lines changed: 1178 additions & 2 deletions

File tree

.github/workflows/go.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
jobs:
13+
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Set up Go
20+
uses: actions/setup-go@v4
21+
with:
22+
go-version: '1.24'
23+
24+
- name: Build
25+
run: go build -v ./...
26+
27+
- name: Test
28+
run: go test -v ./...

.github/workflows/release.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: release
2+
3+
on:
4+
push:
5+
# run only against tags
6+
tags:
7+
- "*"
8+
9+
permissions:
10+
contents: write
11+
12+
jobs:
13+
release:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 0
19+
- run: git fetch --force --tags
20+
- name: Set up Go
21+
uses: actions/setup-go@v3
22+
with:
23+
go-version: ">=1.22.0"
24+
cache: true
25+
26+
- run: go generate -v ./...
27+
- run: go vet -v ./...
28+
- run: go test -v ./...
29+
30+
# https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63
31+
- run: CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o splunk_${{ github.ref_name }}_darwin_amd64 .
32+
- run: CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o splunk_${{ github.ref_name }}_darwin_arm64 .
33+
- run: CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -o splunk_${{ github.ref_name }}_linux_386 .
34+
- run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o splunk_${{ github.ref_name }}_linux_amd64 .
35+
- run: CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o splunk_${{ github.ref_name }}_linux_arm64 .
36+
37+
# create checksums.txt
38+
- run: shasum -a 256 splunk_* > checksums.txt
39+
40+
- name: Create a Release in a GitHub Action
41+
uses: softprops/action-gh-release@v2
42+
with:
43+
files: |
44+
splunk_${{ github.ref_name }}_darwin_amd64
45+
splunk_${{ github.ref_name }}_darwin_arm64
46+
splunk_${{ github.ref_name }}_linux_386
47+
splunk_${{ github.ref_name }}_linux_amd64
48+
splunk_${{ github.ref_name }}_linux_arm64
49+
checksums.txt

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
*.so
99
*.dylib
1010

11+
# Binary names (specific files in root only)
12+
/splunk
13+
/splunk-cli
14+
15+
# Build output directory
16+
/dist/
17+
dist/
18+
1119
# Test binary, built with `go test -c`
1220
*.test
1321

Makefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.PHONY: build test clean install
2+
3+
# Build the binary
4+
build:
5+
go build -o splunk .
6+
7+
# Run tests
8+
test:
9+
go test -v ./...
10+
11+
# Clean build artifacts
12+
clean:
13+
rm -f splunk
14+
15+
# Install to /usr/local/bin
16+
install: build
17+
sudo cp splunk /usr/local/bin/splunk
18+
sudo chmod +x /usr/local/bin/splunk
19+
20+
# Build for all platforms
21+
build-all:
22+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o dist/splunk_darwin_amd64 .
23+
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o dist/splunk_darwin_arm64 .
24+
CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -o dist/splunk_linux_386 .
25+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o dist/splunk_linux_amd64 .
26+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o dist/splunk_linux_arm64 .
27+
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o dist/splunk_windows_amd64.exe .
28+
29+
# Run linter
30+
lint:
31+
go vet ./...
32+
go fmt ./...
33+
34+
# Run the binary
35+
run: build
36+
./splunk
37+
38+
# Show help
39+
help:
40+
@echo "Available targets:"
41+
@echo " build - Build the splunk binary"
42+
@echo " test - Run tests"
43+
@echo " clean - Remove build artifacts"
44+
@echo " install - Install to /usr/local/bin"
45+
@echo " build-all - Build for all platforms"
46+
@echo " lint - Run go vet and go fmt"
47+
@echo " run - Build and run the binary"
48+
@echo " help - Show this help message"

README.md

Lines changed: 211 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,211 @@
1-
# splunk-cli
2-
Splunk CLI
1+
# Splunk CLI & MCP Server
2+
3+
A Splunk CLI and MCP server that allows you and your coding agents to interact with Splunk. Inspired by the GitHub CLI and following the same concept as jira-cli, it aims to provide a simple and efficient way for humans and agents to interact with Splunk from the command line.
4+
5+
Being both a CLI and an MCP server means you get the best of both worlds. Agents can be directed to perform specific commands (e.g., `Run a search for errors in the last hour by running splunk search 'error' '-1h' 'now'`), or they can use the MCP server to interact with Splunk directly.
6+
7+
Like `jq`, it is a single tiny binary, without the overhead of installing a Node runtime, and without the need to put your Splunk token in plain text file (it uses the system key-ring).
8+
9+
## Installation
10+
11+
### Supported Platforms
12+
13+
Binaries are available for:
14+
- **Linux**: amd64, arm64
15+
- **macOS**: amd64 (Intel), arm64 (Apple Silicon)
16+
- **Windows**: amd64
17+
18+
### Download and Install
19+
20+
Download the binary for your platform from the [release page](https://github.com/kitproj/splunk-cli/releases).
21+
22+
#### Linux
23+
24+
**For Linux (amd64):**
25+
```bash
26+
sudo curl -fsL -o /usr/local/bin/splunk https://github.com/kitproj/splunk-cli/releases/download/v0.0.1/splunk_v0.0.1_linux_amd64
27+
sudo chmod +x /usr/local/bin/splunk
28+
```
29+
30+
**For Linux (arm64):**
31+
```bash
32+
sudo curl -fsL -o /usr/local/bin/splunk https://github.com/kitproj/splunk-cli/releases/download/v0.0.1/splunk_v0.0.1_linux_arm64
33+
sudo chmod +x /usr/local/bin/splunk
34+
```
35+
36+
#### macOS
37+
38+
**For macOS (Apple Silicon/arm64):**
39+
```bash
40+
sudo curl -fsL -o /usr/local/bin/splunk https://github.com/kitproj/splunk-cli/releases/download/v0.0.1/splunk_v0.0.1_darwin_arm64
41+
sudo chmod +x /usr/local/bin/splunk
42+
```
43+
44+
**For macOS (Intel/amd64):**
45+
```bash
46+
sudo curl -fsL -o /usr/local/bin/splunk https://github.com/kitproj/splunk-cli/releases/download/v0.0.1/splunk_v0.0.1_darwin_amd64
47+
sudo chmod +x /usr/local/bin/splunk
48+
```
49+
50+
#### Verify Installation
51+
52+
After installing, verify the installation works:
53+
```bash
54+
splunk -h
55+
```
56+
57+
## Usage
58+
59+
### Configuration
60+
61+
#### Getting a Splunk API Token
62+
63+
Before configuring, you'll need to create a Splunk authentication token:
64+
65+
1. Log in to your Splunk instance: `https://your-splunk-host:8000`
66+
2. Go to Settings > Tokens
67+
3. Click "New Token" or "Enable Token Authentication" if not already enabled
68+
4. Generate and copy the token (you won't be able to see it again)
69+
70+
#### Configure the CLI
71+
72+
The `splunk` CLI can be configured in two ways:
73+
74+
1. **Using the configure command (recommended, secure)**:
75+
```bash
76+
echo "your-api-token" | splunk configure your-splunk-host
77+
```
78+
This stores the host in `~/.config/splunk-cli/config.json` and the token securely in your system's keyring.
79+
80+
2. **Using environment variables**:
81+
```bash
82+
export SPLUNK_HOST=your-splunk-host
83+
export SPLUNK_TOKEN=your-api-token
84+
```
85+
Note: The SPLUNK_TOKEN environment variable is still supported for backward compatibility, but using the keyring (via `splunk configure`) is more secure on multi-user systems.
86+
87+
## Usage
88+
89+
### Direct CLI Usage
90+
91+
```bash
92+
Usage:
93+
splunk configure <host> - Configure Splunk host and token (reads token from stdin)
94+
splunk search <query> [earliest-time] [latest-time] - Run a Splunk search query
95+
splunk mcp-server - Start MCP server (stdio transport)
96+
```
97+
98+
#### Examples
99+
100+
**Run a search:**
101+
```bash
102+
splunk search "error" "-1h" "now"
103+
# Search for "error" in the last hour
104+
105+
splunk search "index=main sourcetype=access_combined | stats count by status"
106+
# Search with SPL query
107+
```
108+
109+
### MCP Server Mode
110+
111+
The MCP (Model Context Protocol) server allows AI assistants and other tools to interact with Splunk through a standardized JSON-RPC protocol over stdio. This enables seamless integration with AI coding assistants and other automation tools.
112+
113+
Learn more about MCP: https://modelcontextprotocol.io
114+
115+
**Setup:**
116+
117+
1. First, configure your Splunk host and token (stored securely in the system keyring):
118+
```bash
119+
echo "your-api-token" | splunk configure your-splunk-host
120+
```
121+
122+
2. Add the MCP server configuration to your MCP client (e.g., Claude Desktop, Cline):
123+
```json
124+
{
125+
"mcpServers": {
126+
"splunk": {
127+
"command": "splunk",
128+
"args": ["mcp-server"]
129+
}
130+
}
131+
}
132+
```
133+
134+
For **Claude Desktop**, add this to:
135+
- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
136+
- Windows: `%APPDATA%\Claude\claude_desktop_config.json`
137+
138+
The server exposes the following tool:
139+
- `search` - Run a Splunk search query and return results
140+
141+
**Example usage from an AI assistant:**
142+
> "Search Splunk for errors in the main index in the last hour and show me the top 10 results."
143+
144+
## Development
145+
146+
### Built With
147+
148+
This CLI uses the following Go libraries:
149+
- **[github.com/mark3labs/mcp-go](https://github.com/mark3labs/mcp-go)** - Model Context Protocol server library
150+
- **[github.com/zalando/go-keyring](https://github.com/zalando/go-keyring)** - Cross-platform keyring library for secure token storage
151+
152+
The Splunk API client is a custom implementation using the Splunk REST API, as there is no official Go SDK for Splunk Enterprise.
153+
154+
### Building from Source
155+
156+
```bash
157+
# Clone the repository
158+
git clone https://github.com/kitproj/splunk-cli.git
159+
cd splunk-cli
160+
161+
# Build the binary
162+
go build -o splunk
163+
164+
# Run tests
165+
go test ./...
166+
```
167+
168+
### Project Structure
169+
170+
```
171+
splunk-cli/
172+
├── internal/
173+
│ ├── config/ # Configuration management (host, token storage)
174+
│ └── splunk/ # Splunk REST API client
175+
├── main.go # CLI entry point and command handlers
176+
├── mcp.go # MCP server implementation
177+
├── mcp_test.go # MCP server tests
178+
└── README.md # This file
179+
```
180+
181+
## Troubleshooting
182+
183+
### Common Issues
184+
185+
**"Splunk host must be configured" error**
186+
- Make sure you've run `splunk configure <host>` or set the `SPLUNK_HOST` environment variable
187+
- Check that the config file exists: `cat ~/.config/splunk-cli/config.json`
188+
189+
**"Failed to execute request" or authentication errors**
190+
- Verify your API token is still valid (tokens can expire)
191+
- Re-run the configure command to update the token: `echo "new-token" | splunk configure your-splunk-host`
192+
- Make sure your Splunk user has permission to access the requested resources
193+
194+
**Keyring issues on Linux**
195+
- Some Linux systems may not have a keyring service installed
196+
- Install `gnome-keyring` or `kwallet` for your desktop environment
197+
- Alternatively, use environment variables: `export SPLUNK_TOKEN=your-token`
198+
199+
**MCP server not appearing in Claude Desktop**
200+
- Restart Claude Desktop after editing the config file
201+
- Check the config file syntax is valid JSON
202+
- Verify the `splunk` binary is in your PATH: `which splunk`
203+
204+
### Getting Help
205+
206+
- Report issues: https://github.com/kitproj/splunk-cli/issues
207+
- Check existing issues for solutions and workarounds
208+
209+
## License
210+
211+
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.

go.mod

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module github.com/kitproj/splunk-cli
2+
3+
go 1.24.10
4+
5+
require (
6+
github.com/mark3labs/mcp-go v0.43.0
7+
github.com/zalando/go-keyring v0.2.6
8+
golang.org/x/term v0.37.0
9+
)
10+
11+
require (
12+
al.essio.dev/pkg/shellescape v1.5.1 // indirect
13+
github.com/bahlo/generic-list-go v0.2.0 // indirect
14+
github.com/buger/jsonparser v1.1.1 // indirect
15+
github.com/danieljoos/wincred v1.2.2 // indirect
16+
github.com/godbus/dbus/v5 v5.1.0 // indirect
17+
github.com/google/uuid v1.6.0 // indirect
18+
github.com/invopop/jsonschema v0.13.0 // indirect
19+
github.com/mailru/easyjson v0.7.7 // indirect
20+
github.com/spf13/cast v1.7.1 // indirect
21+
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
22+
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
23+
golang.org/x/sys v0.38.0 // indirect
24+
gopkg.in/yaml.v3 v3.0.1 // indirect
25+
)

0 commit comments

Comments
 (0)