zabbix_export:
  version: '7.0'
  template_groups:
    - uuid: b1312de7c348460da518bf45faebe60d
      name: Templates/Applications
  templates:
    - uuid: 264004de9f244715a84110d922786e3b
      template: 'AutoRPO by HTTP'
      name: 'AutoRPO by HTTP'
      description: |
        AutoRPO restore testing: scheduler, licence, failed and flagged tests, from the AutoRPO read-only API.
        Link it to a host that stands for the AutoRPO install. Set {$AUTORPO.URL} and {$AUTORPO.TOKEN} (secret) as global macros, or on the host.
        Read-only: Zabbix can't start, sign off or change anything in AutoRPO.
      vendor:
        name: AutoRPO
        version: 7.0-1
      groups:
        - name: Templates/Applications
      items:
        - uuid: 6c7961a6994e4e5f8ac7098b8932eb96
          name: 'AutoRPO: API status'
          type: HTTP_AGENT
          key: autorpo.status
          delay: '{$AUTORPO.INTERVAL}'
          history: '0'
          value_type: TEXT
          trends: '0'
          description: 'Raw response from the AutoRPO read-only API. Other items read from it.'
          timeout: 15s
          url: '{$AUTORPO.URL}/api/v1/status'
          preprocessing:
            - type: JAVASCRIPT
              parameters:
                - |
                  // AutoRPO /api/v1/status -> one object for the dependent items. Anything that isn't a status throws, so a broken
                  // check never reads as "0 failures".
                  var s;
                  try { s = JSON.parse(value); } catch (e) { throw 'AutoRPO did not return JSON: ' + String(value).substring(0, 120); }
                  if (!s || typeof s !== 'object' || typeof s.scheduler !== 'string') {
                    throw 'AutoRPO returned an error: ' + JSON.stringify(s).substring(0, 200);
                  }
                  function clean(t, n) {
                    return String(t == null ? '' : t).replace(/[\x00-\x1f\x7f]/g, ' ').substring(0, n || 250);
                  }
                  function num(x) {
                    if (typeof x !== 'number' || !isFinite(x) || x < 0) throw 'AutoRPO returned an unexpected count: ' + JSON.stringify(x);
                    return Math.floor(x);
                  }
                  return JSON.stringify({
                    scheduler: clean(s.scheduler),
                    error: clean(s.error),
                    blocked: s.scheduler.indexOf('Blocked:') === 0 ? 1 : 0,
                    licence_blocked: s.licence_blocked ? 1 : 0,
                    edition: clean(s.edition, 50),
                    open_failures: num(s.open_failures),
                    flagged: num(s.flagged),
                    open_tests: num(s.open_tests)
                  });
          headers:
            - name: Authorization
              value: 'Bearer {$AUTORPO.TOKEN}'
          tags:
            - tag: component
              value: autorpo
        - uuid: f35e496901f44457b5a8dc703165ebc4
          name: 'AutoRPO: Scheduler'
          type: DEPENDENT
          key: autorpo.scheduler
          delay: '0'
          value_type: CHAR
          trends: '0'
          description: 'What the scheduler is doing now, in words.'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.scheduler'
          master_item:
            key: autorpo.status
          tags:
            - tag: component
              value: autorpo
        - uuid: 4c1f5c0f679d43478eaa22e679bd9823
          name: 'AutoRPO: Scheduler error'
          type: DEPENDENT
          key: autorpo.error
          delay: '0'
          value_type: CHAR
          trends: '0'
          description: 'The last scheduler error, or empty.'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.error'
          master_item:
            key: autorpo.status
          tags:
            - tag: component
              value: autorpo
        - uuid: 63055510baba4cf8bb2c173246269618
          name: 'AutoRPO: Restores blocked'
          type: DEPENDENT
          key: autorpo.blocked
          delay: '0'
          value_type: UNSIGNED
          description: '1 when restores are blocked (the reason is in the Scheduler item).'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.blocked'
          master_item:
            key: autorpo.status
          tags:
            - tag: component
              value: autorpo
        - uuid: 438053c756c64341a7217a145aea732a
          name: 'AutoRPO: Licence blocked'
          type: DEPENDENT
          key: autorpo.licence_blocked
          delay: '0'
          value_type: UNSIGNED
          description: '1 when the licence stops restore testing.'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.licence_blocked'
          master_item:
            key: autorpo.status
          tags:
            - tag: component
              value: autorpo
        - uuid: c08391fca26d4a5481482c57de2748ee
          name: 'AutoRPO: Edition'
          type: DEPENDENT
          key: autorpo.edition
          delay: '0'
          value_type: CHAR
          trends: '0'
          description: ''
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.edition'
          master_item:
            key: autorpo.status
          tags:
            - tag: component
              value: autorpo
        - uuid: 8cd39c494e434abb9484ebf08331a2e6
          name: 'AutoRPO: Failed tests needing attention'
          type: DEPENDENT
          key: autorpo.open_failures
          delay: '0'
          value_type: UNSIGNED
          description: 'Failed tests not yet resolved.'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.open_failures'
          master_item:
            key: autorpo.status
          tags:
            - tag: component
              value: autorpo
        - uuid: 44a57651f5ef4112b05589909023177b
          name: 'AutoRPO: Flagged VMs'
          type: DEPENDENT
          key: autorpo.flagged
          delay: '0'
          value_type: UNSIGNED
          description: 'VMs that failed twice and left the queue until someone reviews them.'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.flagged'
          master_item:
            key: autorpo.status
          tags:
            - tag: component
              value: autorpo
        - uuid: c5c88b5d5b2c4e21b7f3655b9633b083
          name: 'AutoRPO: Tests in progress or waiting for sign-off'
          type: DEPENDENT
          key: autorpo.open_tests
          delay: '0'
          value_type: UNSIGNED
          description: ''
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.open_tests'
          master_item:
            key: autorpo.status
          tags:
            - tag: component
              value: autorpo
      macros:
        - macro: '{$AUTORPO.INTERVAL}'
          value: '5m'
          description: 'How often to read the AutoRPO status.'
        - macro: '{$AUTORPO.NODATA}'
          value: '30m'
          description: 'Alert when the API has not answered for this long.'
      tags:
        - tag: class
          value: application
        - tag: target
          value: autorpo
    - uuid: 19b713233d3b46ccba2a52de23281005
      template: 'AutoRPO VM by HTTP'
      name: 'AutoRPO VM by HTTP'
      description: |
        Adds AutoRPO: Last tested date, Last test result and Next test date to an existing host, by finding the VM in
        AutoRPO with the host's name (ignoring case; a short name also matches a full domain name).
        Link it to the hosts (or a host group) of servers that AutoRPO tests. Set {$AUTORPO.URL} and {$AUTORPO.TOKEN} (secret) as global macros (or on the hosts),
        and on a host whose VM has another name, {$AUTORPO.VMNAME}.
        Needs an AutoRPO version with the name lookup (see the AutoRPO release notes). Read-only.
      vendor:
        name: AutoRPO
        version: 7.0-1
      groups:
        - name: Templates/Applications
      items:
        - uuid: 5720b1e22d65499cba473873ed174f65
          name: 'AutoRPO: API lookup'
          type: HTTP_AGENT
          key: autorpo.vm
          delay: '{$AUTORPO.VM.INTERVAL}'
          history: '0'
          value_type: TEXT
          trends: '0'
          description: 'Raw response from the AutoRPO read-only API. Other items read from it.'
          timeout: 15s
          url: '{$AUTORPO.URL}/api/v1/vms'
          query_fields:
            - name: name
              value: '{$AUTORPO.VMNAME}'
            - name: name
              value: '{HOST.HOST}'
            - name: name
              value: '{HOST.NAME}'
          preprocessing:
            - type: JAVASCRIPT
              parameters:
                - |
                  // AutoRPO /api/v1/vms?name=... -> one object for the dependent items. Errors throw, so Zabbix shows the item as
                  // failing instead of a wrong answer.
                  var vms;
                  try { vms = JSON.parse(value); } catch (e) { throw 'AutoRPO did not return JSON: ' + String(value).substring(0, 120); }
                  if (!Array.isArray(vms)) {
                    throw 'AutoRPO returned an error: ' + JSON.stringify(vms).substring(0, 200);
                  }
                  function clean(t) {
                    return String(t == null ? '' : t).replace(/[\x00-\x1f\x7f]/g, ' ').substring(0, 100);
                  }
                  function when(t) {
                    if (!t) return '';
                    var d = new Date(String(t).replace(/(\.\d{3})\d+/, '$1'));
                    if (isNaN(d.getTime())) return 'Unknown';
                    function p(n) { return (n < 10 ? '0' : '') + n; }
                    return d.getUTCFullYear() + '-' + p(d.getUTCMonth() + 1) + '-' + p(d.getUTCDate()) + ' ' +
                      p(d.getUTCHours()) + ':' + p(d.getUTCMinutes()) + ' UTC';
                  }
                  function all(m, state) {
                    return JSON.stringify({match: m, state: state, last_tested: m, last_result: m, next_test: m});
                  }
                  if (!vms.length) return all('Not tested by AutoRPO', 'none');
                  for (var i = 0; i < vms.length; i++) {
                    if (!vms[i] || typeof vms[i].name !== 'string' || !('matched_by' in vms[i])) {
                      return all('AutoRPO needs updating for the name lookup', 'old');
                    }
                  }
                  if (vms.length > 1) return all('Ambiguous: ' + vms.length + ' VMs named ' + clean(vms[0].name), 'ambiguous');
                  var v = vms[0];
                  var results = {passed: 'Passed', failed: 'Failed', expired: 'Not signed off in time', error: 'Could not be tested',
                    awaiting_signoff: 'Waiting for sign-off', restoring: 'Testing now', booting: 'Testing now',
                    restored: 'Restored and checked (not started)'};
                  var result = v.last_status ? (results[v.last_status] || clean(v.last_status)) : 'Never tested';
                  if (v.state === 'testing') result = 'Testing now';
                  var due = when(v.due);
                  var next = {excluded: 'Not scheduled (excluded from testing)', archival: 'On demand (archival backup)',
                    unlicensed: 'Not scheduled (over the licence limit)', nokey: 'Not scheduled (backup key missing)',
                    never: 'Now (never passed a test)', overdue: 'Overdue since ' + due, retest: 'Retest pending',
                    testing: 'Testing now'}[v.state] || due || 'Unknown';
                  return JSON.stringify({match: 'Tested by AutoRPO: ' + clean(v.name) + (v.matched_by === 'short name' ? ' (by short name)' : ''),
                    state: clean(v.state), last_tested: v.last_tested ? when(v.last_tested) : 'Never', last_result: result, next_test: next});
          headers:
            - name: Authorization
              value: 'Bearer {$AUTORPO.TOKEN}'
          tags:
            - tag: component
              value: autorpo
        - uuid: dffa37bd7824410398d114f19de74757
          name: 'AutoRPO: Last tested date'
          type: DEPENDENT
          key: autorpo.vm.last_tested
          delay: '0'
          value_type: CHAR
          trends: '0'
          description: 'When AutoRPO last restored and tested this server (UTC).'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.last_tested'
          master_item:
            key: autorpo.vm
          tags:
            - tag: component
              value: autorpo
        - uuid: 94bf864ed7e744d7b56e3b5ef5acac44
          name: 'AutoRPO: Last test result'
          type: DEPENDENT
          key: autorpo.vm.last_result
          delay: '0'
          value_type: CHAR
          trends: '0'
          description: 'The result of the last restore test.'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.last_result'
          master_item:
            key: autorpo.vm
          tags:
            - tag: component
              value: autorpo
        - uuid: 2e411b6dbf9e4821a5c5ae41c3919da0
          name: 'AutoRPO: Next test date'
          type: DEPENDENT
          key: autorpo.vm.next_test
          delay: '0'
          value_type: CHAR
          trends: '0'
          description: 'When this server is next due for a restore test (UTC).'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.next_test'
          master_item:
            key: autorpo.vm
          tags:
            - tag: component
              value: autorpo
        - uuid: 3733ab050cbc4bc0a2b6582009256652
          name: 'AutoRPO: Matched VM'
          type: DEPENDENT
          key: autorpo.vm.match
          delay: '0'
          value_type: CHAR
          trends: '0'
          description: 'Which AutoRPO VM this host was matched to, or why not.'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.match'
          master_item:
            key: autorpo.vm
          tags:
            - tag: component
              value: autorpo
        - uuid: 2c8a1e35edb44f5587428105d8893844
          name: 'AutoRPO: State'
          type: DEPENDENT
          key: autorpo.vm.state
          delay: '0'
          value_type: CHAR
          trends: '0'
          description: 'For triggers: ok, due-soon, overdue, never, retest, testing, excluded, archival, none, ambiguous and others.'
          preprocessing:
            - type: JSONPATH
              parameters:
                - '$.state'
          master_item:
            key: autorpo.vm
          tags:
            - tag: component
              value: autorpo
      macros:
        - macro: '{$AUTORPO.VMNAME}'
          value: ''
          description: 'This host''s VM name in AutoRPO, when it differs from the host name.'
        - macro: '{$AUTORPO.VM.INTERVAL}'
          value: '1h'
          description: 'How often to look this server up in AutoRPO.'
      tags:
        - tag: class
          value: application
        - tag: target
          value: autorpo
  triggers:
        - uuid: 8b224df0b5ea40ea92b1d32aeed2ffb9
          expression: 'nodata(/AutoRPO by HTTP/autorpo.scheduler,{$AUTORPO.NODATA})=1'
          name: 'AutoRPO: no data from the API'
          priority: AVERAGE
          description: 'The AutoRPO API has not answered. Check the address, the token and that the AutoRPO service is running.'
          tags:
            - tag: scope
              value: availability
        - uuid: 89e0d2f8dd444b44a017fb4aa52bb2c1
          expression: 'last(/AutoRPO by HTTP/autorpo.blocked)=1'
          name: 'AutoRPO: restores are blocked'
          priority: HIGH
          description: 'No restore tests run until this is fixed. The reason is in the Scheduler item and on the AutoRPO dashboard.'
          tags:
            - tag: scope
              value: availability
        - uuid: f643cc4a03fb4b84989a1a00abb89ca1
          expression: 'last(/AutoRPO by HTTP/autorpo.licence_blocked)=1'
          name: 'AutoRPO: licence stops restore testing'
          priority: HIGH
          description: 'Open Settings, Licence in AutoRPO.'
          tags:
            - tag: scope
              value: availability
        - uuid: 56ec088ed4d04d5ea4e3efe4823bfda8
          expression: 'length(last(/AutoRPO by HTTP/autorpo.error))>0'
          name: 'AutoRPO: scheduler error'
          priority: AVERAGE
          description: 'See the Scheduler error item and the AutoRPO dashboard.'
          tags:
            - tag: scope
              value: availability
        - uuid: d6a14a0099714f5daecf5e8b4353b8dd
          expression: 'last(/AutoRPO by HTTP/autorpo.open_failures)>0'
          name: 'AutoRPO: failed tests need attention'
          priority: WARNING
          description: 'Open the failed tests in AutoRPO.'
          tags:
            - tag: scope
              value: availability
        - uuid: df71907e87e64519ac968bd5f440d737
          expression: 'last(/AutoRPO by HTTP/autorpo.flagged)>0'
          name: 'AutoRPO: VMs flagged for review'
          priority: WARNING
          description: 'A VM failed twice and is out of the queue until someone reviews it in AutoRPO.'
          tags:
            - tag: scope
              value: availability
        - uuid: 4dd9595236334d7d87d8fbb21a797169
          expression: 'last(/AutoRPO VM by HTTP/autorpo.vm.state)="retest"'
          name: 'AutoRPO: last restore test failed'
          priority: AVERAGE
          description: 'The last restore test of this server failed, expired or could not run. See AutoRPO: Last test result, and the test in AutoRPO.'
          tags:
            - tag: scope
              value: availability
        - uuid: ad979d4d6bd74b54b58f284239c35e33
          expression: 'last(/AutoRPO VM by HTTP/autorpo.vm.state)="overdue"'
          name: 'AutoRPO: overdue for a restore test'
          priority: WARNING
          description: 'This server has gone longer than its tier allows without a passed restore test.'
          tags:
            - tag: scope
              value: availability
        - uuid: 0d941a966f3e4bd3afff8b9d3a8288ff
          expression: 'last(/AutoRPO VM by HTTP/autorpo.vm.state)="never"'
          name: 'AutoRPO: never passed a restore test'
          priority: INFO
          description: 'AutoRPO has not yet passed a restore test of this server.'
          tags:
            - tag: scope
              value: availability
        - uuid: ffdf5de671434280998fc259641cfecc
          expression: 'last(/AutoRPO VM by HTTP/autorpo.vm.state)="ambiguous"'
          name: 'AutoRPO: host name matches more than one VM'
          priority: INFO
          description: 'Set the macro {$AUTORPO.VMNAME} on this host to the exact VM name in AutoRPO.'
          tags:
            - tag: scope
              value: availability
