The correction is integrated into the TP (See the green boxes)
What you will learn in this TP
- Run Splunk Enterprise in a container
- Create an index and set its retention
- Create an HEC token limited to one index
- Send events and find them with SPL
All steps and instructions are shown. Recommended for this lab: guided
Introduction #
Context. Your team wants to centralize the online shop's logs. You set up a test Splunk, create a dedicated index and connect the application through the HTTP Event Collector.
Prerequisites: a machine with Docker (4 GB free memory), curl, jq and Python 3 · Estimated time: 40 min
A local Splunk 10.6 with a 30-day retentionweb index holding 3,000 events received via HEC.Setup #
Guided steps, with their solution.
1. Run Splunk
Why? The official container configures Splunk at startup: license, general terms, password and HEC enabled.
Runsplunk/splunk:10.6.2with ports 8000, 8088, 8089 and 9997, wait for the healthy state (2 to 3 minutes) and print the version via the API.See the correction
read -rsp 'Splunk admin password (8 characters minimum): ' SPLUNK_PASSWORD && export SPLUNK_PASSWORD docker run -d --name splunk -p 8000:8000 -p 8088:8088 -p 8089:8089 -p 9997:9997 \ -e SPLUNK_START_ARGS=--accept-license \ -e SPLUNK_GENERAL_TERMS=--accept-sgt-current-at-splunk-com \ -e SPLUNK_PASSWORD -e SPLUNK_HEC_TOKEN=$(python3 -c "import uuid; print(uuid.uuid4())") -e SPLUNK_ENABLE_LISTEN=9997 \ splunk/splunk:10.6.2 until [ "$(docker inspect -f '{{.State.Health.Status}}' splunk)" = healthy ]; do sleep 10; done; echo ready curl -sk -u admin:$SPLUNK_PASSWORD https://localhost:8089/services/server/info?output_mode=json | jq -r '.entry[0].content.version'2. Create the index and token
Why? A dedicated index carries its own permissions and retention; a token limited to that index prevents an application from writing elsewhere.
Through the REST API, create thewebindex (30-day retention) then an HEC tokentp_webwhose default and only allowed index isweb.See the correction
API=https://localhost:8089 curl -sk -u admin:$SPLUNK_PASSWORD $API/services/data/indexes -d name=web -d frozenTimePeriodInSecs=2592000 -o /dev/null -w '%{http_code}\n' # 201 HEC_TOKEN=$(curl -sk -u admin:$SPLUNK_PASSWORD "$API/servicesNS/admin/splunk_httpinput/data/inputs/http?output_mode=json" \ -d name=tp_web -d index=web -d indexes=web -d sourcetype=_json | jq -r '.entry[0].content.token') echo $HEC_TOKEN3. Send events
Why? HEC accepts several JSON events in one request: batch sending is much more efficient.
Generate 3,000 access events spread over the last 24 hours and send them in batches of 500. Each response must be{"text":"Success","code":0}.See the correction
cat > gen_web.py <<'EOF' import json, random, sys, time uris = ['/', '/produits', '/panier', '/api/paiement', '/connexion', '/api/stock'] users = ['alice', 'bruno', 'chloe', 'driss', 'emma', 'farid', 'gaelle'] now = int(time.time()) out = [] for i in range(3000): uri = random.choices(uris, weights=[30, 25, 15, 10, 12, 8])[0] status = random.choices([200, 302, 404, 500, 503], weights=[85, 5, 5, 4, 1])[0] if uri == '/api/paiement' and random.random() < 0.15: status = 500 duration = int(random.lognormvariate(4.5, 0.6)) + (1500 if uri == '/api/paiement' and status == 500 else 0) ev = {'status': status, 'uri': uri, 'method': 'POST' if uri.startswith('/api') else 'GET', 'duration_ms': duration, 'user': random.choice(users), 'client_ip': '10.0.%d.%d' % (random.randint(0, 3), random.randint(1, 254)), 'useragent': random.choice(['Mozilla/5.0 Firefox/133.0', 'Mozilla/5.0 Chrome/131.0', 'Mozilla/5.0 Safari/605.1', 'curl/8.5.0'])} out.append(json.dumps({'time': now - random.randint(0, 86400), 'host': 'web-0%d' % random.randint(1, 3), 'event': ev})) sys.stdout.write('\n'.join(out)) EOF python3 gen_web.py | split -l 500 - lot_ for f in lot_*; do curl -sk https://localhost:8088/services/collector/event -H "Authorization: Splunk $HEC_TOKEN" --data-binary @$f; echo; done rm lot_*4. Search
Why? Checking the event count and fields validates the whole chain: token, index, timestamp and JSON extraction.
Log in tohttp://localhost:8000(admin). Count events in thewebindex over 24 hours, per host, and display the list of extracted fields.See the correction
index=web earliest=-24h | stats count BY host index=web earliest=-24h | fieldsummary | table field count distinct_count # 3 000 événements au total ; champs status, uri, method, duration_ms, user, client_ip, useragent5. Check via the API
Why? Automating a check through the API is the basis of platform acceptance tests.
Run the same search through/services/search/jobs/exportas CSV, then check the index size with| rest.See the correction
curl -sk -u admin:$SPLUNK_PASSWORD https://localhost:8089/services/search/jobs/export \ --data-urlencode search='search index=web earliest=-24h | stats count BY status' -d output_mode=csv # Dans Splunk / In Splunk / En Splunk : | rest /services/data/indexes/web | table title totalEventCount currentDBSizeMB frozenTimePeriodInSecs
Validate your work
Check that each goal above is reached and that you get the result described in the introduction.
Difficulty level : ●●○○○ (2/5)
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Splunk: the machine data platform at a glance
Discover Splunk: the platform's role, components (forwarders, indexers, search heads), ports, eve...
Installing Splunk and ingesting data
Installing Splunk Enterprise (package or Docker), understanding $SPLUNK_HOME and configuration fi...
Dashboards, reports and alerts in Splunk
Making the most of Splunk searches: reports and scheduling, Dashboard Studio dashboards (data sou...
Splunk best practices
Best practices for running Splunk in production: data quality and sourcetypes, index design, lice...
Administering distributed Splunk
Designing and administering a production Splunk platform: reference architectures, indexer cluste...
SPL: Splunk search fundamentals
Learning the SPL language: search structure, time range and time modifiers, search modes, filteri...
Sources, destinations and routes in Cribl Stream
Receiving and sending data with Cribl Stream: push, pull, collector and internal sources, event b...
Security and observability with Splunk
Splunk solutions beyond search: Enterprise Security (detections, findings, risk-based alerting, a...
Kibana: role, installation and first steps
What Kibana is in the Elastic suite, how to install it with Elasticsearch (start-local, Docker), ...