extends:
  - _system-base.yaml
  - _std-if-mib.yaml
  - _mikrotik-ipsec.yaml

selector:
  - sysobjectid:
      include:
        - 1.3.6.1.4.1.14988.1 # Router OS
        - 1.3.6.1.4.1.14988.2 # SwOS

metadata:
  device:
    fields:
      vendor:
        value: "MikroTik"
      type:
        symbols:
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            # RouterOS devices
            match_pattern: 'RouterOS (CCR.*)'
            match_value: 'Router'
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: 'RouterOS (CRS.*)'
            match_value: 'Layer 3 Switch'
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: 'RouterOS (hAP.*)'
            match_value: 'Wireless'
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: 'RouterOS ([wc]AP.*)'
            match_value: 'Access Point'
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: 'RouterOS (mAP.*)'
            match_value: 'Access Point'
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: 'RouterOS (SXT.*|LHG.*|LDF.*|Disc.*|DynaDish.*|NetBox.*|NetMetal.*|Groove.*|OmniTIK.*|BaseBox.*|Metal.*|SXTsq.*|QRT.*|Audience.*|Cube.*|Wireless Wire.*)'
            match_value: 'Wireless'
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: 'RouterOS CHR'
            match_value: 'Router'
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: 'RouterOS x86'
            match_value: 'Router'
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: 'RouterOS (RB.*)'
            match_value: 'Router'
          # SwOS devices - all are switches
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            match_pattern: '(.*) SwOS.*'
            match_value: 'Switch'
      model:
        symbols:
          # RouterOS model extraction
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            extract_value: 'RouterOS ([A-Za-z0-9-+]+)'
          # SwOS model extraction
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr
            extract_value: '([A-Za-z0-9-+]+) SwOS'
          # MikroTik generic format
          - OID: 1.3.6.1.2.1.1.1.0
            name: sysDescr

metric_tags:
  - OID: 1.3.6.1.4.1.14988.1.1.4.1.0
    symbol: mtxrLicSoftwareId
    tag: software_id
  - OID: 1.3.6.1.4.1.14988.1.1.4.4.0
    symbol: mtxrLicVersion
    tag: license_version

metrics:
  - MIB: MIKROTIK-MIB
    symbol:
      OID: 1.3.6.1.4.1.14988.1.1.3.11.0
      name: mtxrHlProcessorTemperature
      scale_factor: 0.1
      chart_meta:
        description: The current CPU temperature
        family: 'Hardware/CPU/Temperature'
        unit: "Cel"
  - MIB: MIKROTIK-MIB
    table:
      OID: 1.3.6.1.4.1.14988.1.1.19.1
      name: mtxrOpticalTable
    symbols:
      - OID: 1.3.6.1.4.1.14988.1.1.19.1.1.6
        name: mtxrOpticalTemperature
        chart_meta:
          description: Temperature at sensor chip
          family: 'Hardware/OpticalModule/Temperature/Value'
          unit: "Cel"
        transform: | # Set temperature to 0 when no SFP module present (MikroTik returns ~4294967168 for empty slots)
          {{- if gt .Metric.Value 4294967000 -}}
            {{- setValue .Metric 0 -}}
          {{- end -}}
      - OID: 1.3.6.1.4.1.14988.1.1.19.1.1.10
        name: mtxrOpticalRxPower
        chart_meta:
          description: Received optical power
          family: 'Hardware/OpticalModule/Power/In'
          unit: "mW"
      - OID: 1.3.6.1.4.1.14988.1.1.19.1.1.7
        name: mtxrOpticalSupplyVoltage
        chart_meta:
          description: Supply voltage
          family: 'Hardware/OpticalModule/Voltage/Supply'
          unit: "V"
      - OID: 1.3.6.1.4.1.14988.1.1.19.1.1.8
        name: mtxrOpticalTxBiasCurrent
        chart_meta:
          description: Transmit bias current
          family: 'Hardware/OpticalModule/Current/TxBias'
          unit: "mA"
      - OID: 1.3.6.1.4.1.14988.1.1.19.1.1.5
        name: mtxrOpticalWavelength
        chart_meta:
          description: Optical wavelength
          family: 'Hardware/OpticalModule/Wavelength'
          unit: "nm"
      - OID: 1.3.6.1.4.1.14988.1.1.19.1.1.9
        name: mtxrOpticalTxPower
        chart_meta:
          description: Transmit optical power
          family: 'Hardware/OpticalModule/Power/Out'
          unit: "mW"
      - OID: 1.3.6.1.4.1.14988.1.1.19.1.1.4
        name: mtxrOpticalTxFault
        chart_meta:
          description: Indicates whether the optical transceiver is experiencing a transmit fault
          family: 'Hardware/OpticalModule/Status/Transmit'
          unit: "{status}"
        mapping:
          0: 'ok'
          1: 'fault'
      - OID: 1.3.6.1.4.1.14988.1.1.19.1.1.3
        name: mtxrOpticalRxLoss
        chart_meta:
          description: Indicates whether there is a loss of received optical signal (Rx Loss)
          family: 'Hardware/OpticalModule/Status/Receive'
          unit: "{status}"
        mapping:
          0: 'ok'
          1: 'loss'
    metric_tags:
      - tag: optical_index
        symbol:
          OID: 1.3.6.1.4.1.14988.1.1.19.1.1.1
          name: mtxrOpticalIndex
      - tag: optical_name
        symbol:
          OID: 1.3.6.1.4.1.14988.1.1.19.1.1.2
          name: mtxrOpticalName

  - MIB: MIKROTIK-MIB
    table:
      OID: 1.3.6.1.4.1.14988.1.1.15.1
      name: mtxrPOETable
    symbols:
      - OID: 1.3.6.1.4.1.14988.1.1.15.1.1.6
        name: mtxrPOEPower
        chart_meta:
          description: Power over ethernet
          family: 'Network/Interface/PoE/Power'
          unit: "W"
      - OID: 1.3.6.1.4.1.14988.1.1.15.1.1.4
        name: mtxrPOEVoltage
        chart_meta:
          description: Power over ethernet voltage
          family: 'Network/Interface/PoE/Voltage'
          unit: "V"
      - OID: 1.3.6.1.4.1.14988.1.1.15.1.1.5
        name: mtxrPOECurrent
        chart_meta:
          description: Power over ethernet current
          family: 'Network/Interface/PoE/Current'
          unit: "mA"
      - OID: 1.3.6.1.4.1.14988.1.1.15.1.1.3
        name: mtxrPOEStatus
        chart_meta:
          description: Power over ethernet status
          family: 'Network/Interface/PoE/Status'
          unit: "{status}"
        mapping:
          1: disabled
          2: waiting_for_load
          3: powered_on
          4: overload
    metric_tags:
      - tag: poe_interface_index
        symbol:
          OID: 1.3.6.1.4.1.14988.1.1.15.1.1.1
          name: mtxrPOEInterfaceIndex
      - tag: poe_name
        symbol:
          OID: 1.3.6.1.4.1.14988.1.1.15.1.1.2
          name: mtxrPOEName

  # CPU hrProcessorTable (https://www.net-snmp.org/docs/mibs/host.html#hrProcessorTable)
  - MIB: HOST-RESOURCES-MIB
    table:
      OID: 1.3.6.1.2.1.25.3.3
      name: hrProcessorTable
    symbols:
      - OID: 1.3.6.1.2.1.25.3.3.1.2
        name: cpu.usage # hrProcessorLoad
        chart_meta:
          description: The current CPU utilization
          family: 'System/CPU/Usage'
          unit: "%"
    metric_tags:
      - tag: cpu_index
        index: 1

  # Table metrics - Extended health monitoring (multi-sensor support)
  - MIB: MIKROTIK-MIB
    table:
      OID: 1.3.6.1.4.1.14988.1.1.3.100
      name: mtxrHlTable
    symbols:
      - OID: 1.3.6.1.4.1.14988.1.1.3.100.1.3
        name: mtxrHlSensorValue
        chart_meta:
          description: Sensor value
          family: 'Hardware/Sensor'
        transform: |
          {{- $sensorType := index .Metric.Tags "rm:sensor_type" | default "" -}}
          {{- $config := get (dict 
              "1" (dict "name" "temperature" "unit" "Cel" "family" "Temperature" 
                   "desc" "Temperature reading")
              "2" (dict "name" "fan_speed" "unit" "{revolution}/min" "family" "FanSpeed" 
                   "desc" "Fan rotation speed")
              "3" (dict "name" "voltage" "unit" "V" "family" "Voltage" "divisor" 10.0 
                   "desc" "Voltage measurement")
              "4" (dict "name" "current" "unit" "A" "family" "Current" "divisor" 10.0 
                   "desc" "Current draw")
              "5" (dict "name" "power" "unit" "W" "family" "Power" "divisor" 10.0 
                   "desc" "Power consumption")
              "6" (dict "name" "sensor_status" "family" "Presence" 
                   "mapping" (i64map 0 "absent_or_faulty" 1 "present_and_ok")
                   "desc" "Component presence and operational status")
              "7" (dict "name" "sensor_state" "family" "State" 
                   "mapping" (i64map 0 "false" 1 "true")
                   "desc" "Boolean sensor state")
              "8" (dict "name" "usage_percentage" "unit" "%" "family" "Usage" 
                   "desc" "Resource utilization percentage")
          ) $sensorType -}}

          {{- if $config -}}
            {{- setName .Metric (printf "%s_%s" .Metric.Name (get $config "name")) -}}
            {{- setFamily .Metric (printf "Hardware/Sensor/%s/Value" (get $config "family")) -}}
            {{- with get $config "desc" -}}{{- setDesc $.Metric . -}}{{- end -}}
            {{- with get $config "unit" -}}{{- setUnit $.Metric . -}}{{- end -}}
            {{- with get $config "divisor" -}}{{- setValue $.Metric (int64 (div (float64 $.Value) .)) -}}{{- end -}}
            {{- with get $config "mapping" -}}{{- setMultivalue $.Metric . -}}{{- end -}}
          {{- end -}}
    metric_tags:
      - tag: sensor_name
        symbol:
          OID: 1.3.6.1.4.1.14988.1.1.3.100.1.2
          name: mtxrHlSensorName
      - tag: rm:sensor_type
        symbol:
          OID: 1.3.6.1.4.1.14988.1.1.3.100.1.4
          name: mtxrHlSensorUnit
