How to set up alerting based on logs

VMRule and VMAlert for VictoriaLogs.

In the Shturval platform, logs for client clusters and the management cluster are stored in VictoriaLogs. When needed, configure alerting based on cluster logs using custom resources VMRule and VMAlert. Step-by-step instructions with examples follow.


Prerequisites


Step 1: Create a VMRule

VMRule sets the rule: under what condition in the logs the alert is triggered.

apiVersion: operator.victoriametrics.com/v1beta1
kind: VMRule
metadata:
  labels:
    cluster: clustername
    role: user
    vmalert-type: vlogs
  name: vlogs-rule
  namespace: clustername
spec:
  groups:
    - headers:
        - 'ProjectID: 4'
      interval: 5m
      name: vlogsgroup
      type: vlogs
      rules:
        - alert: vlogsrule
          annotations:
            description: 'SSC {{ index $labels "objectRef.name" }} was updated by {{ index $labels "user.username" }} in the last {{$for}}'
          expr: objectRef.resource:"shturvalserviceconfigs" verb:~"patch|update" | stats by(objectRef.name, objectRef.resource, user.username) count() as updateSSC | filter updateSSC:>1
          labels:
            source: vlogs
ParameterDescription
metadata.labels.clusterName of the cluster where log alerting is required
metadata.nameRule name
metadata.namespaceNamespace of the cluster (cluster name)
groups.nameRule group name
groups.headersProjectID in VictoriaLogs. Only one project can be specified
groups.typevlogs
rules.alertAlert name
rules.exprExpression for notification based on logs (field expr in the rule)

According to the established rule in expr, notifications will be sent when services (SSC) change in the cluster.


Step 2: Create VMAlert

VMAlert evaluates rules and sends alerts to Alertmanager.

apiVersion: operator.victoriametrics.com/v1beta1
kind: VMAlert
metadata:
  labels:
    cluster: clustername
  name: vlogs
  namespace: clustername
spec:
  datasource:
    url: http://vlogs-logs.clustername:9428
  evaluationInterval: 30s
  externalLabels:
    cluster: clustername
  extraArgs:
    rule.defaultRuleType: vlogs
  notifiers:
    - url: http://vmalertmanager-shturval-metrics-collector.victoria-metrics:9093
  remoteRead:
    url: http://shturval-monitoring-vmselect.victoria-metrics:8481/select/monitoring_id:3/prometheus
  remoteWrite:
    url: http://shturval-monitoring-vminsert.victoria-metrics:8480/insert/monitoring_id:3/prometheus
  resources:
    limits:
      cpu: 200m
      memory: 256Mi
    requests:
      cpu: 100m
      memory: 128Mi
  ruleSelector:
    matchLabels:
      vmalert-type: vlogs
      cluster: clustername
  ruleNamespaceSelector: {}
  selectAllByDefault: false
ParameterDescription
metadata.namespaceNamespace of the cluster in which alerting is configured based on logs
metadata.labels.clusterName of the cluster in which log alerting is configured
spec.datasource.urlCluster log source URL: http://vlogs-logs.<cluster_name>:9428
externalLabels.clusterName of the cluster in which log alerting is configured
spec.remoteRead.urlVictoriaMetricsSelect; instead of monitoring_id specify the value of the cluster annotation with this key
spec.remoteWrite.urlVictoriaMetricsInsert; similar to monitoring_id
spec.resourcesResources requests and limits (for example: limits cpu 200m, memory 256Mi; requests cpu 100m, memory 128Mi)

Step 3: Upload to the platform

In the management cluster, import VMRule and VMAlert with manifest import.

Screenshot

Importing alert manifests from logs


Checking alerts

You can check alerts based on logs using a notification on maildev. Set up maildev: create a recipient of type email in the Notifications → Recipients section. More details: example of setting up sending a notification to maildev.