RousselTMLEARNING

TP · Splunk: from discovery to expertise

Final lab: Alert and Studio dashboard through the REST API with correction

The correction is integrated into the TP (See the green boxes)

What you will learn in this TP
  • Create a scheduled alert through the REST API
  • Tune threshold, window and throttling
  • Trigger and track an alert
  • Build a lean Studio dashboard with ds.chain
  • Troubleshoot the scheduler
Mode :

All steps and instructions are shown. Recommended for this lab: autonomous

Introduction #

Context. The shop goes to production. The team wants to be warned when payments fail and to have a shared dashboard, all created by script so it can be replayed on every environment.

Prerequisites: a machine with Docker (4 GB free memory), curl, jq and Python 3, the Lab 1 Splunk container and HEC token  ·  Estimated time: 50 min

A tp_paiement_5xx alert triggered and tracked, and a boutique_tp Studio dashboard running a single base search.

Setup #

Guided steps, with their solution.

  1. 1. Create the alert

    Why? Created through the API, the alert can be versioned and replayed identically in staging and production.

    Create tp_paiement_5xx: every 2 minutes over the last 3 minutes, triggering above 10 5xx errors on /api/paiement, tracking on, 10-minute throttle and a Log event action.
    See the correction
    API=https://localhost:8089
    curl -sk -u admin:$SPLUNK_PASSWORD $API/servicesNS/admin/search/saved/searches \
      -d name=tp_paiement_5xx \
      --data-urlencode search='index=web uri="/api/paiement" status>=500' \
      -d is_scheduled=1 -d cron_schedule='*/2 * * * *' \
      -d dispatch.earliest_time=-3m -d dispatch.latest_time=now \
      -d alert_type='number of events' -d alert_comparator='greater than' -d alert_threshold=10 \
      -d alert.track=1 -d alert.severity=4 -d alert.suppress=1 -d alert.suppress.period=10m \
      -d actions=logevent -d action.logevent.param.index=main \
      --data-urlencode action.logevent.param.event='ALERTE tp_paiement_5xx : $job.resultCount$ erreurs' \
      -o /dev/null -w '%{http_code}\n'   # 201
    curl -sk -u admin:$SPLUNK_PASSWORD "$API/servicesNS/admin/search/saved/searches/tp_paiement_5xx?output_mode=json" | jq '.entry[0].content | {cron_schedule, alert_threshold, is_scheduled}'
  2. 2. Justify the settings

    Why? Window, frequency and throttling decide noise and detection delay: you must be able to explain them.

    Why a 3-minute window for a run every 2 minutes? What would happen without throttling? Why a scheduled alert rather than real-time?
    See the correction
    Window 3 min > period 2 min: the overlap absorbs indexing delay (a late event
      does not escape the search); the trade-off is that an event can be counted twice.
    Without throttling: a 30-minute outage would send 15 identical alerts; with 10 min, at most 3.
    Real-time: one permanent search per alert, occupying a core continuously on the indexers;
      a scheduled alert every 2 minutes is enough for a payment incident.
  3. 3. Trigger the alert

    Why? An alert never triggered in testing is an alert nobody knows works.

    Send 40 payment errors now via HEC, wait 2 to 4 minutes, then find the triggered alert, the scheduler run and the event written by the action.
    See the correction
    python3 - <<'EOF' | curl -sk https://localhost:8088/services/collector/event -H "Authorization: Splunk $HEC_TOKEN" --data-binary @-
    import json, time
    now = int(time.time())
    print('\n'.join(json.dumps({'time': now, 'host': 'web-01', 'event': {'status': 500, 'uri': '/api/paiement', 'method': 'POST', 'duration_ms': 2100, 'user': 'test', 'client_ip': '10.0.0.9', 'useragent': 'curl/8.5.0'}}) for _ in range(40)))
    EOF
    # Dans Splunk / In Splunk / En Splunk :
    | rest /servicesNS/-/-/alerts/fired_alerts | table title triggered_alert_count
    index=_internal sourcetype=scheduler savedsearch_name=tp_paiement_5xx | table _time status result_count alert_actions
    index=main "ALERTE tp_paiement_5xx"
  4. 4. Create the dashboard

    Why? A base search shared by chains runs only one search for three visualizations: the platform thanks you.

    Write the Studio definition (one ds.search source, three ds.chain, a single value, a line chart, a table), wrap it in a <dashboard version="2"> and create the boutique_tp view through the API.
    See the correction
    cat > boutique.json <<'EOF'
    {
      "title": "Boutique - TP",
      "dataSources": {
        "ds_base":  { "type": "ds.search", "options": { "query": "index=web | fields _time uri status duration_ms", "queryParameters": { "earliest": "-24h", "latest": "now" } } },
        "ds_5xx":   { "type": "ds.chain", "options": { "extend": "ds_base", "query": "| where status>=500 | timechart span=30m count AS erreurs" } },
        "ds_p95":   { "type": "ds.chain", "options": { "extend": "ds_base", "query": "| stats perc95(duration_ms) AS p95 count AS requetes BY uri | sort -p95" } },
        "ds_taux":  { "type": "ds.chain", "options": { "extend": "ds_base", "query": "| stats count(eval(status>=500)) AS e count AS n | eval taux = round(100*e/n, 2) | fields taux" } }
      },
      "visualizations": {
        "viz_taux": { "type": "splunk.singlevalue", "title": "Taux d'erreur (%)", "dataSources": { "primary": "ds_taux" } },
        "viz_5xx":  { "type": "splunk.line", "title": "Erreurs 5xx", "dataSources": { "primary": "ds_5xx" } },
        "viz_p95":  { "type": "splunk.table", "title": "p95 par URL", "dataSources": { "primary": "ds_p95" } }
      },
      "inputs": {},
      "defaults": {},
      "layout": {
        "type": "absolute",
        "options": { "width": 1440, "height": 720 },
        "structure": [
          { "item": "viz_taux", "type": "block", "position": { "x": 20,  "y": 20,  "w": 300,  "h": 200 } },
          { "item": "viz_5xx",  "type": "block", "position": { "x": 340, "y": 20,  "w": 1080, "h": 320 } },
          { "item": "viz_p95",  "type": "block", "position": { "x": 20,  "y": 360, "w": 1400, "h": 340 } }
        ]
      }
    }
    EOF
    XML=$(python3 -c 'import json,sys; d=json.load(open("boutique.json")); print("<dashboard version=\"2\" theme=\"light\"><label>Boutique - TP</label><definition><![CDATA[" + json.dumps(d) + "]]></definition></dashboard>")')
    curl -sk -u admin:$SPLUNK_PASSWORD https://localhost:8089/servicesNS/admin/search/data/ui/views \
      -d name=boutique_tp --data-urlencode "eai:data=$XML" -o /dev/null -w '%{http_code}\n'   # 201
    echo "http://localhost:8000/fr-FR/app/search/boutique_tp"
  5. 5. Check and clean up

    Why? Checking that scheduled searches are not skipped is part of going live; cleaning up frees the machine.

    Check that no alert run is skipped, open the dashboard, then delete the alert, the view and the containers.
    See the correction
    # Splunk : index=_internal sourcetype=scheduler savedsearch_name=tp_paiement_5xx | stats count BY status
    #   success uniquement ; « skipped » signalerait un planificateur saturé
    curl -sk -u admin:$SPLUNK_PASSWORD -X DELETE https://localhost:8089/servicesNS/admin/search/saved/searches/tp_paiement_5xx
    curl -sk -u admin:$SPLUNK_PASSWORD -X DELETE https://localhost:8089/servicesNS/admin/search/data/ui/views/boutique_tp
    docker rm -f splunk && docker network rm splunk-tp

Validate your work

Check that each goal above is reached and that you get the result described in the introduction.

Difficulty level : ●●●●○ (4/5)

My progress

  • ⏱ Estimated time : ≈ 50 min

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

5.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Questions about this lesson

No questions yet: be the first to ask for help or share your configuration.

Ask a question about this lesson →

Recommended Articles

Dashboards, reports and alerts in Splunk

Making the most of Splunk searches: reports and scheduling, Dashboard Studio dashboards (data sou...

Administering distributed Splunk

Designing and administering a production Splunk platform: reference architectures, indexer cluste...

Splunk best practices

Best practices for running Splunk in production: data quality and sourcetypes, index design, lice...

Automating Cribl: REST API, Packs, Git and Terraform

Managing Cribl as code: REST API (authentication, group paths, commit and deploy), reusable Packs...

Alerts, connectors and SLOs in Kibana

Create alert rules in Kibana (Elasticsearch query, ES|QL, custom threshold, logs), configure conn...

Visualizing in Kibana: Lens, dashboards and saved objects

Build visualizations with Lens, assemble interactive dashboards (filters, controls, links), and m...

Splunk: the machine data platform at a glance

Discover Splunk: the platform's role, components (forwarders, indexers, search heads), ports, eve...

Using Faro data: queries, dashboards, alerts and GDPR

Query Faro data in Loki (Web Vitals, errors, events), build dashboards and alerts, and comply wit...

SPL: Splunk search fundamentals

Learning the SPL language: search structure, time range and time modifiers, search modes, filteri...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!