> ## Documentation Index
> Fetch the complete documentation index at: https://docs-xcor.paloaltonetworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Varnish

> Varnish Cache request, cache, backend, storage, and thread metrics.

The Varnish integration requires CXDOT Collector 1.19.0 or greater.

[Varnish Cache](https://www.varnish.org/) is a caching HTTP reverse proxy that
speeds up websites by serving cached content in front of your web servers. Use the Varnish
integration with the CXDOT Collector to collect request, cache, backend, storage,
thread, lock, and memory pool metrics from the Varnish Cache servers running in your
environment.

The Varnish integration supports Varnish Cache 6.0 or greater, as released by Varnish
Software in its packages and the official `varnish` container images.

## Supported telemetry types

The Varnish integration supports these telemetry types:

| Logs | Metrics | Traces | Events |
| :-: | :-: | :-: | :-: |
| No | Yes | No | No |

## Known limitations

The Varnish integration collects the statistics counters that `varnishstat` lists. It
doesn't collect the following:

* Backend health and other state that only `varnishadm` reports, such as a backend set
  sick or healthy by hand. The integration doesn't connect to the Varnish
  administration port.
* The Varnish version, which no counter carries.
* Logs, including the request records in the shared memory log.
* Counters that Varnish modules and Varnish Enterprise add. Each module defines the
  names of its counters when it loads.
* Counters for backends that a Varnish module implements by itself, because Varnish
  keeps none for them. The default configuration of the `varnish` container image uses
  such backends from version 9.0.

The Varnish integration hasn't been tested on Kubernetes nodes that enforce SELinux,
such as OpenShift nodes. On these nodes, SELinux policy likely denies the node Collector
access to the volumes of other pods, even as root, unless the Collector container runs
with an SELinux label that grants that access. The CXDOT Collector Helm chart sets no
SELinux label.

## Prerequisites

The Varnish integration has the following prerequisites:

* Run the CXDOT Collector on Linux, on the same machine as Varnish: the same host, or
  the same Kubernetes node as the Varnish pod. The Collector reads the counters from
  the shared memory files in the working directory of each Varnish server. It doesn't
  run `varnishstat` or connect to Varnish over the network.

* On Kubernetes, the node Collector must run as root, with access to every pod volume
  on its node. The Collector reads the counters of each Varnish pod from the pod's
  volume, through the kubelet pod directory on the node, which only root can read.
  The Configure on Kubernetes section describes the access that the Helm chart grants.

* Find the working directory of each Varnish server, the directory that the `-n`
  option of `varnishd` names. Without `-n`, Varnish 7.0 or later uses
  `/var/lib/varnish/varnishd`, and earlier versions use a directory under
  `/var/lib/varnish` named after the machine's host name. The official `varnish:6.0`
  container image sets `-n /var/lib/varnish`. To list the working directories under
  `/var/lib/varnish`, run the following command where Varnish runs. Each result is
  inside a working directory:

  ```shell theme={null}
  find /var/lib/varnish -maxdepth 2 -name _.vsm_mgt
  ```

## Configure

The Varnish integration is disabled by default, because in Kubernetes it requires a
node Collector that runs as root. Follow the steps for where the CXDOT Collector runs:
on Kubernetes with the CXDOT Collector Helm chart, or directly on each Varnish host.

### Configure on Kubernetes

To configure the Varnish integration on Kubernetes, follow these steps:

1. Enable the integration. For example, add the following to the `values.yaml` for your
   CXDOT Collector Helm chart:

   ```yaml theme={null}
   collection:
     cxdot:
       integrations:
         varnish:
           enabled: true
   ```

   The node Collector on each node collects from the annotated Varnish pods on that
   node. Leave `instances` out of the Helm values. The `instances` list is for a
   Collector running on a host, and the Helm chart refuses to render a configuration
   that sets it. For more information about Collector tiers, see
   [architecture](https://docs-xcor.paloaltonetworks.com/ingest/xcor/collector/install/kubernetes/architecture.md).

2. Allow the node Collector to run as root. For example, add the following to the
   `values.yaml` for your CXDOT Collector Helm chart:

   ```yaml theme={null}
   nodeCollector:
     securityContext:
       runAsRoot: true
   ```

   The Collector reads the counters of a Varnish pod from the pod's volume, through the
   kubelet pod directory on the node, which only root can read. While the Varnish
   integration is enabled, the Helm chart mounts that directory into the node
   Collector. The mount gives the Collector read access to every pod volume on the
   node. The mount also gives the Collector write access to the pod volumes that the
   kubelet keeps in memory, such as memory-backed `emptyDir` volumes, Secrets, and
   service account tokens. The chart sets the mount read-only, but Kubernetes applies
   that setting only to the top of the mount and not to the volumes that pods mount
   beneath it. The Helm chart refuses to render the Varnish integration without
   `runAsRoot`.

3. Optional: If the kubelet on your nodes uses a root directory other than
   `/var/lib/kubelet`, set `nodeCollector.kubeletRootDir` to the value of the kubelet
   `--root-dir` flag. For example, add the following to the `values.yaml` for your
   CXDOT Collector Helm chart:

   ```yaml theme={null}
   nodeCollector:
     # The kubelet root directory on k0s. On MicroK8s, it's
     # /var/snap/microk8s/common/var/lib/kubelet.
     kubeletRootDir: /var/lib/k0s/kubelet
   ```

   One value applies to every node. While the Varnish integration is enabled, a value
   that doesn't match a node keeps the node Collector pod on that node from starting,
   and the pod's events name the missing directory.

4. To collect from Varnish pods, add a CXDOT integration annotation to the pod template,
   and keep the working directory on an `emptyDir` volume. The annotation key must end
   with the container name. For example, add the following annotation and volume to
   your Varnish workload:

   ```yaml theme={null}
   spec:
     template:
       metadata:
         annotations:
           integrations.cxdot.paloaltonetworks.com/varnish: |
             schema_version: v1.19
             varnish:
               instances:
                 - endpoint: /var/lib/varnish/varnishd
       spec:
         volumes:
           - name: varnish-workdir
             emptyDir:
               medium: Memory
         containers:
           - name: varnish
             image: varnish:9.1
             volumeMounts:
               - name: varnish-workdir
                 mountPath: /var/lib/varnish
   ```

   The `endpoint` is the path of the working directory inside the `varnish` container.
   The Collector finds the working directory in the pod's `emptyDir` volume that's
   mounted at that path or at its parent directory, such as `/var/lib/varnish` in the
   example. The Collector can't read a working directory in the container's own file
   system. A memory-backed volume keeps the shared memory files in memory, where they
   count toward the pod's memory usage.

   For the official `varnish:6.0` image, mount the volume at `/var/lib/varnish` and set
   `endpoint` to `/var/lib/varnish`. For other Varnish versions earlier than 7.0, start
   `varnishd` with `-n`, such as `-n /var/lib/varnish/varnishd`. Without `-n`, the
   working directory is named after the pod's host name, which changes with each pod.

   Metrics from each annotated pod include that pod's Kubernetes metadata. For more
   information, see
   [autodiscovery](https://docs-xcor.paloaltonetworks.com/ingest/xcor/collector/discover/kubernetes.md).

### Configure on a host

To configure the Varnish integration on a host, such as a virtual machine, follow these
steps:

1. Enable the integration and list the working directory of each Varnish server. Run
   the Collector in host mode on each Varnish host and add the following to the
   Collector configuration file:

   ```yaml theme={null}
   cxdot:
     mode: host
     integrations:
       varnish:
         enabled: true
         instances:
           - endpoint: /var/lib/varnish/varnishd
           - endpoint: /var/lib/varnish/edge
             instance_name: edge
   ```

   On a host, the integration collects only from the instances in `instances`. The
   second entry in the example is a Varnish server started with `-n edge`. Give each
   Varnish server on a host its own `instance_name`. For more information about running
   the Collector on a host, see
   [install the CXDOT Collector on Linux](https://docs-xcor.paloaltonetworks.com/ingest/xcor/collector/install/linux.md).

2. Give the Collector read access to each working directory. The `varnish` group owns
   the counter files in the packages from Varnish Software, so add the account that
   runs the Collector to that group. For example:

   ```shell theme={null}
   sudo usermod -aG varnish COLLECTOR_USER
   ```

   Replace `COLLECTOR_USER` with the name of that account, then restart the Collector.
   The Collector needs no write access to the working directory and doesn't need to run
   as root. An account other than root also needs `cxdot.debug.socket_path` set to a
   path in a directory that account can write to. The default debug socket directory is
   under `/var/run`, and a Collector that can't create it doesn't start.

### Validate

To validate the Varnish integration, follow these steps:

1. In
   [Live Telemetry Analyzer](https://docs-xcor.paloaltonetworks.com/investigate/analyze/telemetry-analyzer.md),
   filter for `__name__=cxdot.integration.target.health` and
   `cxdot.integration.name=varnish`. Confirm that the metric reports `1` for each
   Varnish server. A value of `0` means that the Collector couldn't read the working
   directory, or that the uptime of the Varnish server stopped advancing, as it does
   when Varnish stops and leaves its files behind. The `cxdot.integration.target`
   attribute names the working directory. The `k8s.pod.name` attribute in Kubernetes
   and the `host.name` attribute on a host identify where Varnish runs.

2. In
   [Metrics Explorer](https://docs-xcor.paloaltonetworks.com/investigate/querying/metrics/explorer.md),
   run the following query while Varnish serves requests:

   ```text theme={null}
   sum by ("k8s.pod.name", "host.name", "varnish.instance.name") (rate({"varnish.main.client_req"}[$__rate_interval]))
   ```

   Confirm that the query returns a time series for each Varnish server that serves
   requests.

### Troubleshooting

* `cxdot.integration.target.health` reports `0` for a pod, and the Collector logs that
  no `emptyDir` volume of the pod is mounted at the working directory or at its
  parent: The pod keeps its working directory outside an `emptyDir` volume, or the
  `endpoint` in the annotation doesn't name the directory that Varnish uses. Mount an
  `emptyDir` volume at the working directory or its parent, and set `endpoint` to the
  path of the working directory inside the container.
* `cxdot.integration.target.health` reports `0` for a pod, and the Collector logs that
  more than one `emptyDir` volume of the pod holds a working directory: Two `emptyDir`
  volumes of the pod hold a working directory that matches the `endpoint`, such as two
  Varnish containers that each mount their own volume at `/var/lib/varnish`. The
  Collector can't tell which volume holds the working directory that the `endpoint`
  names, so it collects from neither. Keep the working directories of a pod on one
  `emptyDir` volume, and give each Varnish server in the pod its own working directory
  with `-n`.
* `cxdot.integration.target.health` reports `0` on a host, and the Collector logs
  `permission denied`: The account that runs the Collector can't read the working
  directory. Add the account to the group that owns the working directory, which is
  `varnish` in the packages from Varnish Software, and restart the Collector.
* No Varnish pods report metrics, and the Collector logs
  `CXDOT integration annotation target rejected by annotation restrictions`: The
  `collection.cxdot.integrations.discovery.kubernetes.annotation_restrictions.custom_pod_host`
  setting in the Helm values for your CXDOT Collector is `deny`. A Varnish target is a
  working directory, not the pod's address, so this setting rejects every Varnish
  target. The Varnish integration collects from pods only while the setting is `allow`,
  its default.
* `cxdot.integration.target.health` reports `0`, and the Collector logs that it
  `found the shared memory layout of Varnish before 6.0`: The Varnish server runs a
  version earlier than 6.0, which keeps its counters in a layout that the Collector
  can't read. The Varnish integration requires Varnish 6.0 or later, so upgrade the
  Varnish server.

## Varnish counters

Each counter that `varnishstat` lists becomes a metric named after its section, and the
object that a section's counters describe becomes an attribute. For example, the
`VBE.boot.default.req` counter is the `varnish.backend.req` metric with the
`varnish.vcl.name` attribute set to `boot` and the `varnish.backend.name` attribute set
to `default`. Every metric carries the `varnish.instance.name` attribute.

The following table lists the metrics and attributes for each counter section:

| Counter section | Metrics | Attributes |
| - | - | - |
| `MAIN` | `varnish.main.*` | none |
| `MGT` | `varnish.mgt.*` | none |
| `VBE` | `varnish.backend.*` | `varnish.vcl.name`, `varnish.backend.name` |
| `SMA`, `SMF`, `SMU`, `SSY` | `varnish.storage.*` | `varnish.storage.name`, `varnish.storage.type` |
| `LCK` | `varnish.lock.*` | `varnish.lock.name` |
| `MEMPOOL` | `varnish.mempool.*` | `varnish.mempool.name` |
| `WAITER` | `varnish.waiter.*` | `varnish.waiter.name` |
| `VCP` | `varnish.vcp.*` | none |

Counters that split one quantity by category become one metric with an attribute. For
example, the `MAIN.fetch_length` and `MAIN.fetch_chunked` counters are the
`varnish.main.fetch` metric with the `varnish.fetch.type` attribute. Which counters
Varnish publishes depends on its version and configuration. For example, the `SMF`
section exists only with file storage.

The `varnish.vcl.name` attribute of a backend metric names the Varnish Configuration
Language (VCL) configuration that defines the backend. Each VCL load therefore adds a
time series for every counter of every backend in the new configuration. Varnish keeps
the backends of an old configuration published until the configuration goes cold, 600
seconds after another configuration becomes active by default. A Varnish server whose
configuration reloads often, such as one that an ingress controller reloads on every
change, can report several copies of the time series of each backend at once. To bound
them, lower the `vcl_cooldown` parameter or discard old configurations with
`vcl.discard`. To follow a backend across reloads, aggregate its metrics by
`varnish.backend.name` without `varnish.vcl.name`.

## Configuration reference

Configure one Varnish integration instance with the following settings. In Helm values, place
these settings under `collection.cxdot.integrations.varnish`. In a Collector configuration file,
place them under `cxdot.integrations.varnish`.

### Optional settings

* **`enabled`**
  Type: `boolean`. Optional. Default: `true`.
  Whether to run the Varnish integration. If true, the Collector collects Varnish metrics. If
  false, the Collector doesn't run this integration.

* **`instances`**
  Type: `array of object`. Optional. Default: `[]`.
  Varnish instances to collect from when the Collector runs on a host, one for each working
  directory of a Varnish server on that host. When this list isn't empty, the integration
  collects from exactly these instances and discovers no Varnish pods. In Kubernetes, the
  Collector discovers Varnish pods instead, and the CXDOT Collector Helm chart refuses to render
  a configuration that sets this list. The Collector reads each instance's counters from the
  shared memory files in its working directory, so it needs read access to that directory.
  Membership of the `varnish` group grants that access on most installations.

* **`instances[].endpoint`**
  Type: `string`. Required.
  The working directory of the Varnish server, as the absolute path that Varnish uses: the
  directory that the `-n` option of `varnishd` names. A `-n` name that isn't an absolute path is
  a directory under `/var/lib/varnish` in the packages and official container images. Without
  `-n`, Varnish 7.0 or later uses `/var/lib/varnish/varnishd`, and earlier versions use a
  directory under `/var/lib/varnish` named after the machine's host name. The Collector reads
  the instance's counters from this directory.

* **`instances[].instance_name`**
  Type: `string`. Optional. Default: `default`. Minimum length: `1`.
  The instance name reported as `varnish.instance.name`: the value of the `-n` option of
  `varnishd`, or `default`. Give each instance on a host its own name.

* **`collection_interval`**
  Type: `duration`. Optional. Default: `15s`.
  Time between metric collections from each Varnish instance.

* **`timeout`**
  Type: `duration`. Optional. Default: `15s`.
  Maximum time to wait for each Varnish metric collection.


## Related topics

- [CXDOT Collector integrations](https://docs-xcor.paloaltonetworks.com/ingest/xcor/integrations/collector.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.