# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

app_url: "https://www.sap.com/products/hana.html"
short_name: HANA
long_name: SAP HANA
logo_path: /stackdriver/images/integrations/saphana.png # supplied by google technical writer
description: |-
  The SAP HANA integration collects SAP HANA metrics and logs. The metrics are
  collected by querying relevant monitoring views. This integration writes
  structured trace logs.
configure_integration: |-
  To collect metrics, a monitoring user requires `SELECT` access to the relevant
  monitoring views. The following SQL script creates a monitoring role and applies
  it to a monitoring user if executed by
  a user with sufficient permissions connected to the SAP HANA instance.

  <pre>
  --Create the user
  CREATE RESTRICTED USER otel_monitoring_user PASSWORD <password>;
  --Enable user login
  ALTER USER otel_monitoring_user ENABLE CLIENT CONNECT;
  --Create the monitoring role
  CREATE ROLE OTEL_MONITORING;
  --Grant permissions to the relevant views
  GRANT CATALOG READ TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_BACKUP_CATALOG TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_BLOCKED_TRANSACTIONS TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_CONNECTIONS TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_CS_ALL_COLUMNS TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_CS_TABLES TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_DATABASE TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_DISKS TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_HOST_RESOURCE_UTILIZATION TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_LICENSES TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_RS_TABLES TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_SERVICE_COMPONENT_MEMORY TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_SERVICE_MEMORY TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_SERVICE_REPLICATION TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_SERVICE_STATISTICS TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_SERVICE_THREADS TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_SERVICES TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_VOLUME_IO_TOTAL_STATISTICS TO OTEL_MONITORING;
  GRANT SELECT ON SYS.M_WORKLOAD TO OTEL_MONITORING;
  GRANT SELECT ON _SYS_STATISTICS.STATISTICS_CURRENT_ALERTS TO OTEL_MONITORING;
  GRANT SELECT ON DUMMY TO OTEL_MONITORING;
  --Add the OTEL_MONITOR role to the monitoring user
  GRANT OTEL_MONITORING TO otel_monitoring_user;
  </pre>
supported_operating_systems: linux
supported_app_version: ["2.0"]
expected_metrics:
  - kind: GAUGE
    labels:
      - name: rating
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.alert.count
    value_type: INT64
  - kind: GAUGE
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.backup.latest
    optional: true
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: subtype
        value_regex: data|dict|index|misc
      - name: type
        value_regex: main|delta
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.column.memory.used
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: component
        value_regex: .*
      - name: host
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.component.memory.used
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: status
        value_regex: running|idle|queueing
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.connection.count
    representative: true
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: host
        value_regex: .*
      - name: type
        value_regex: user|system|io_wait|idle
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.cpu.used
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: path
        value_regex: .*
      - name: state
        value_regex: used|free
      - name: usage_type
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.disk.size.current
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: state
        value_regex: used|free
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.host.memory.current
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: state
        value_regex: used|free
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.host.swap.current
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.instance.code_size
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: state
        value_regex: used|free
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.instance.memory.current
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.instance.memory.shared.allocated
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.instance.memory.used.peak
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: product
        value_regex: .*
      - name: system
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.license.expiration.time
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: product
        value_regex: .*
      - name: system
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.license.limit
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: product
        value_regex: .*
      - name: system
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.license.peak
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.network.request.average_time
    value_type: DOUBLE
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: state
        value_regex: active|pending
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.network.request.count
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: host
        value_regex: .*
      - name: type
        value_regex: internal|external
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.network.request.finished.count
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: mode
        value_regex: .*
      - name: port
        value_regex: .*
      - name: primary
        value_regex: .*
      - name: secondary
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.replication.average_time
    optional: true
    value_type: DOUBLE
  - kind: GAUGE
    labels:
      - name: mode
        value_regex: .*
      - name: port
        value_regex: .*
      - name: primary
        value_regex: .*
      - name: secondary
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.replication.backlog.size
    optional: true
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: mode
        value_regex: .*
      - name: port
        value_regex: .*
      - name: primary
        value_regex: .*
      - name: secondary
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.replication.backlog.time
    optional: true
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: type
        value_regex: fixed|variable
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.row_store.memory.used
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: schema
        value_regex: .*
      - name: type
        value_regex: main|delta|history_main|history_delta
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.schema.memory.used.current
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: schema
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.schema.memory.used.max
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: host
        value_regex: .*
      - name: schema
        value_regex: .*
      - name: type
        value_regex: read|write|merge
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.schema.operation.count
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: schema
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.schema.record.compressed.count
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: schema
        value_regex: .*
      - name: type
        value_regex: main|delta|history_main|history_delta
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.schema.record.count
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.code_size
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: status
        value_regex: active|inactive
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.count
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.memory.compactors.allocated
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.memory.compactors.freeable
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.memory.effective_limit
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
      - name: state
        value_regex: used|free
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.memory.heap.current
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.memory.limit
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
      - name: state
        value_regex: used|free
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.memory.shared.current
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
      - name: type
        value_regex: logical|physical
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.memory.used
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: service
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.stack_size
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
      - name: status
        value_regex: active|inactive
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.service.thread.count
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: host
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.transaction.blocked
    optional: true
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: host
        value_regex: .*
      - name: type
        value_regex: update|commit|rollback
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.transaction.count
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: host
        value_regex: .*
      - name: path
        value_regex: .*
      - name: type
        value_regex: read|write
      - name: usage_type
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.volume.operation.count
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: host
        value_regex: .*
      - name: path
        value_regex: .*
      - name: type
        value_regex: read|write
      - name: usage_type
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.volume.operation.size
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: host
        value_regex: .*
      - name: path
        value_regex: .*
      - name: type
        value_regex: read|write
      - name: usage_type
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/saphana.volume.operation.time
    value_type: INT64
expected_logs:
  - log_name: saphana
    fields:
      - name: jsonPayload.component
        value_regex: "Basis"
        type: string
        description: SAP HANA component
      - name: jsonPayload.thread_id
        type: number
        description: ID of the thread logging the message
      - name: jsonPayload.connection_id
        type: number
        description: ID of the connection where the message originated, if message was logged within the scope of a connection
        optional: true
      - name: jsonPayload.transaction_id
        type: number
        description: ID of the transaction where the message originated, if message was logged within the scope of a transaction
        optional: true
      - name: jsonPayload.update_transaction_id
        type: number
        description: ID of the update transaction where the message originated, if message was logged within the scope of an update transaction
        optional: true
      - name: jsonPayload.message
        value_regex: (?s)==== Starting hdbpreprocessor, version 2.* # The (?s) part will make the . match with newline as well. See https://github.com/google/re2/blob/main/doc/syntax.txt#L65,L68
        type: string
        description: Log message
      - name: jsonPayload.severity_flag
        type: string
        description: Log entry level
        optional: true
      - name: sourceLocation.file
        value_regex: "TraceStream.cpp"
        type: string
        description: Source file name
      - name: sourceLocation.line
        type: number
        description: Line within the source file
      - name: severity
        type: string
        description: ''
configuration_options:
  metrics:
    - type: saphana
      fields:
        - default: null
          description: This value must be `saphana`.
          name: type
        - default: localhost:30015
          description: The `hostname:port` used to connect to the SAP HANA instance.
          name: endpoint
        - default: 60s
          description: A [time duration](https://pkg.go.dev/time#ParseDuration) value, such as `30s` or `5m`.
          name: collection_interval
        - default: null
          description: The username used to connect to the instance.
          name: username
        - default: null
          description: The password used to connect to the instance.
          name: password
        - default: true
          description: Sets whether or not to use a secure TLS connection. If set to `false`, then TLS is enabled.
          name: insecure
        - default: false
          description: Sets whether or not to skip verifying the certificate. If `insecure` is set to `true`, then the `insecure_skip_verify` value is not used.
          name: insecure_skip_verify
        - default: null
          description: Path to the TLS certificate to use for mTLS-required connections.
          name: cert_file
        - default: null
          description: Path to the TLS key to use for mTLS-required connections.
          name: key_file
        - default: null
          description: Path to the CA certificate. As a client, this verifies the server certificate. If empty, the receiver uses the system root CA.
          name: ca_file
  logs:
    - type: saphana
      fields:
        - name: type
          default: null
          description: The value must be `saphana`.
        - name: include_paths
          default: '[/usr/sap/*/HDB*/${HOSTNAME}/trace/*.trc]'
          description: A list of filesystem paths to read by tailing each file. A wild card (`*`) can be used in the paths.
        - name: exclude_paths
          default: '[/usr/sap/*/HDB*/${HOSTNAME}/trace/nameserver_history*.trc, /usr/sap/*/HDB*/${HOSTNAME}/trace/nameserver*loads*.trc, /usr/sap/*/HDB*/${HOSTNAME}/trace/nameserver*executed_statements*.trc]'
          description: A list of filesystem path patterns to exclude from the set matched by `include_paths`.
        - name: record_log_file_path
          default: false
          description: If set to `true`, then the path to the specific file from which the log record was obtained appears in the output log entry as the value of the `agent.googleapis.com/log_file_path` label. When using a wildcard, only the path of the file from which the record was obtained is recorded.
        - name: wildcard_refresh_interval
          default: 60s
          description: The interval at which wildcard file paths in `include_paths` are refreshed. Given as a [time duration](https://pkg.go.dev/time#ParseDuration), for example `30s` or `2m`. This property might be useful under high logging throughputs where log files are rotated faster than the default interval.
minimum_supported_agent_version:
  metrics: 2.18.1
  logging: 2.18.1
public_url: https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/third-party/saphana
