# Splunk Metrics

## Overview

{% hint style="info" %}
This page describes the Splunk Metrics check running on StackState Agent V2.

If you are currently running the Splunk Metrics check on Agent V1 (legacy), it's advised that you migrate to Agent V2.

* [Migrate to Agent V2](/5.1/setup/agent/migrate-agent-v1-to-v2.md)
* [Documentation for the Splunk Metrics check running on Agent V1 (legacy)](https://docs.stackstate.com/v/5.0/stackpacks/integrations/splunk/splunk_metrics)
  {% endhint %}

When the [Splunk StackPack](/5.1/stackpacks/integrations/splunk/splunk_stackpack.md) is installed in StackState, you can configure the Splunk Metrics check on StackState Agent V2 to begin collecting Splunk metrics data.

Metrics are collected from Splunk by executing Splunk saved searches that have been specified in the StackState Agent V2 Splunk Metrics check configuration. In order to receive Splunk metrics data in StackState, you will therefore need to add configuration to both Splunk and StackState Agent V2.

* [Splunk saved search](#splunk-saved-search) - there should be at least one saved search that generates the metrics data you want to retrieve. Each saved search can retrieve one metric.
* [StackState Agent V2 Splunk Metrics check](#agent-check) - a Splunk Metrics check should be configured to connect to your Splunk instance and execute the relevant Splunk saved searches.

The Splunk Metrics check on StackState Agent V2 will execute all configured Splunk saved searches periodically. Data will be requested from the last received metric timestamp up until now.

## Prerequisites

To run the Splunk Metrics Agent check, you need to have:

* A running Splunk instance.
* The [Splunk StackPack](/5.1/stackpacks/integrations/splunk/splunk_stackpack.md) installed on your StackState instance.
* [StackState Agent v2.18 or later](/5.1/setup/agent/about-stackstate-agent.md) must be installed on a single machine which can connect to Splunk and StackState.

## Splunk saved search

### Fields used

StackState Agent V2 executes the Splunk saved searches configured in the [Splunk Metrics Agent check configuration file](#agent-check) and pushes retrieved data to StackState as a telemetry stream. The following fields from the results of a saved search are sent to StackState:

| Field               | Description                                                              |
| ------------------- | ------------------------------------------------------------------------ |
| **\_time** (long)   | Data collection timestamp, millis since epoch.                           |
| **metric** (string) | The name of the metric. Taken from the configured `metric_name_field`.   |
| **value** (numeric) | The value of the metric. Taken from the configured `metric_value_field`. |

### Example Splunk query

{% tabs %}
{% tab title="Splunk query" %}

```
index=vms MetricId=cpu.usage.average
| table _time VMName Value    
| eval VMName = upper(VMName)
| rename VMName as metricCpuUsageAverage, Value as valueCpuUsageAverage
| eval type = "CpuUsageAverage"
```

{% endtab %}

{% tab title="Splunk Metrics Agent check configuration" %}

```
...
metric_name_field: "metricCpuUsageAverage"
metric_value_field: "valueCpuUsageAverage"
...
```

{% endtab %}
{% endtabs %}

The example Splunk saved search above would result in the following metric data in StackState:

| Field      | Data                                    |
| ---------- | --------------------------------------- |
| **\_time** | Splunk `_time` field.                   |
| **metric** | Splunk `<metricCpuUsageAverage>` field. |
| **value**  | Splunk `<valueCpuUsageAverage>` field.  |

## Agent check

### Configure the Splunk Metrics check

To enable the Splunk Metrics integration and begin collecting metrics data from your Splunk instance, the Splunk Metrics check must be configured on StackState Agent V2. The check configuration provides all details required for the Agent to connect to your Splunk instance and execute a Splunk saved search.

{% hint style="info" %}
Example Agent V2 Splunk Metrics check configuration file:\
[splunk\_metric/conf.yaml.example (github.com)](https://github.com/StackVista/stackstate-agent-integrations/blob/master/splunk_metric/stackstate_checks/splunk_metric/data/conf.yaml.example)
{% endhint %}

To configure the Splunk Metrics Agent check:

1. Edit the StackState Agent V2 configuration file `/etc/stackstate-agent/conf.d/splunk_metric.d/conf.yaml`.
2. Under **instances**, add details of your Splunk instance:
   * **url** - The URL of your Splunk instance.
   * **authentication** - How the Agent should authenticate with your Splunk instance. Choose either token-based (recommended) or basic authentication. For details, see [authentication configuration details](/5.1/stackpacks/integrations/splunk/splunk_stackpack.md#authentication).
   * **tags** - Optional. Can be used to apply specific tags to all reported metrics in StackState.
3. Under **saved\_searches**, add details of each Splunk saved search that the check should execute. Each saved search can retrieve one metric:
   * **name** - The name of the [Splunk saved search](#splunk-saved-search) to execute.
     * **metric\_name\_field** - The field in the Splunk results that will contain the metric name. Default `"metric"`.
     * **metric\_value\_field** - The field in the Splunk results that will contain numerical data. Default `"value"`.
     * **match:** - Regex used for selecting Splunk saved search queries. Default `metrics.*`.
     * **app** - The Splunk app in which the saved searches are located. Default `"search"`.
     * **request\_timeout\_seconds** - Default `10`
     * **search\_max\_retry\_count** - Default `5`
     * **search\_seconds\_between\_retries** - Default `1`
     * **batch\_size** - Default `1000`
     * **initial\_history\_time\_seconds** - Default `0`
     * **max\_restart\_history\_seconds** - Default `86400`
     * **max\_query\_chunk\_seconds** - Default `3600`
     * **unique\_key\_fields** - The fields to use to [uniquely identify a record](#uniquely-identify-a-record). Default `_bkt` and `_cd`.
     * **parameters** - Used in the Splunk API request. The default parameters provided make sure the Splunk saved search query refreshes. Default `force_dispatch: true` and `dispatch.now: true`.
4. More advanced options can be found in the [example configuration (github.com)](https://github.com/StackVista/stackstate-agent-integrations/blob/master/splunk_metric/stackstate_checks/splunk_metric/data/conf.yaml.example).
5. Save the configuration file.
6. Restart StackState Agent V2 to apply the configuration changes.
7. Once the Agent has restarted, wait for the Agent to collect data and send it to StackState.
8. Metrics retrieved from splunk are available in StackState as a metrics telemetry stream in the `stackstate-metrics` data source. This can be [mapped to associated components](/5.1/use/metrics/add-telemetry-to-element.md).

### Uniquely identify a record

To prevent sending duplicate metrics over multiple check runs, received saved search records must be uniquely identified for comparison. By default, a record is identified by the Splunk default fields `_bkt` and `_cd`. This behavior can be customized for each saved search by specifying `unique_key_fields` in the Splunk Metrics Agent check configuration. Note that the specified `unique_key_fields` fields are mandatory fields for each record returned by the Splunk saved search.

If it isn't possible to uniquely identify a record by a combination of specific fields, the whole record can be used by setting `unique_key_fields: []` (an empty list).

### Disable the Agent check

To disable the Splunk Metrics Agent check:

1. Remove or rename the Agent integration configuration file, for example:

   ```
    mv /etc/stackstate-agent/conf.d/splunk_metric.d/conf.yaml /etc/stackstate-agent/conf.d/splunk_metric.d/conf.yaml.bak
   ```
2. Restart the StackState Agent to apply the configuration changes.

## Splunk metrics in StackState

Metrics retrieved from splunk are available in StackState as a metrics telemetry stream in the `stackstate-metrics` data source. This can be [mapped to associated components](/5.1/use/metrics/add-telemetry-to-element.md).

## See also

* [StackState Splunk integration details](/5.1/stackpacks/integrations/splunk/splunk_stackpack.md)
* [Map telemetry to components](/5.1/use/metrics/add-telemetry-to-element.md)
* [Example Splunk Metrics configuration file - splunk\_metric/conf.yaml.example (github.com)](https://github.com/StackVista/stackstate-agent-integrations/blob/master/splunk_metric/stackstate_checks/splunk_metric/data/conf.yaml.example)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://archivedocs.stackstate.com/5.1/stackpacks/integrations/splunk/splunk_metrics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
