
# Configure JMX Exporter HBase Master

    module.exports = (service) ->
      options = service.options

## Identities

      options.hadoop_group = service.deps.hadoop_core.options.hadoop_group
      # Group
      options.group ?= merge {}, service.deps.prometheus_monitor[0].options.group, options.group
      options.user ?= merge {}, service.deps.prometheus_monitor[0].options.user, options.user
      options.hbase_user ?= merge {}, service.deps.hbase_master.options.user
      options.hbase_group ?= merge {}, service.deps.hbase_master.options.group

## Package
    
      options.version ?= '0.1.0'
      #standalone server
      options.jar_source ?= "https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_httpserver/#{options.version}/jmx_prometheus_httpserver-#{options.version}-jar-with-dependencies.jar"
      # java agent
      # options.agent_source ?= "https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/#{options.version}/jmx_prometheus_javaagent-#{options.version}.jar"
      options.download = service.deps.jmx_exporter[0].node.fqdn is service.node.fqdn
      options.install_dir ?= "/usr/prometheus/#{options.version}/jmx_exporter"
      options.opts ?= {}
      options.opts.base ?= ''
      options.opts.java_properties ?= {}
      options.opts.jvm ?= {}
      options.conf_dir ?= "/etc/prometheus-exporter-jmx/conf"

## Enable JMX Server
JMX options will be configured using a properties file, more readable for administrators.
There is a difference between  -Dcom.sun.management.config.file=<file>. and
com.sun.management.jmxremote.ssl.config.file=<file>.

      options.jmx_config_file ?= "#{service.deps.hbase_master.options.conf_dir}/jmx.properties"
      service.deps.hbase_master.options.opts.java_properties['com.sun.management.config.file'] ?= options.jmx_config_file
      options.jmx_config ?= {}
      options.jmx_config['com.sun.management.jmxremote'] ?= 'true'
      options.jmx_config['com.sun.management.jmxremote.port'] ?= '9017'
      options.jmx_config['com.sun.management.jmxremote.ssl.config.file'] ?= "#{service.deps.hbase_master.options.conf_dir}/jmx-ssl.properties"
      
## Enable JMX SSL

      options.ssl = merge {}, service.deps.ssl, service.deps.hadoop_core.options.ssl
      if !!options.ssl
        options.jmx_ssl_file ?= options.jmx_config['com.sun.management.jmxremote.ssl.config.file']
        options.jmx_ssl_config ?= {}
        service.deps.hbase_master.options.opts.java_properties['com.sun.management.jmxremote.ssl'] ?= 'true'
        service.deps.hbase_master.options.opts.java_properties['com.sun.management.jmxremote.ssl.need.client.auth'] ?= 'false'
        options.jmx_ssl_config['javax.net.ssl.keyStore'] ?= options.ssl.keystore.target
        throw Error 'Missing RS Keystore Password' unless options.ssl?.keystore?.password
        options.jmx_ssl_config['javax.net.ssl.keyStorePassword'] ?= options.ssl.keystore.password
        #jmx_exporter client truststore
        options.conf_dir ?= "/etc/prometheus-exporter-jmx/conf"
        options.opts.java_properties['javax.net.ssl.trustStore'] ?=  "#{options.conf_dir}/truststore"
        throw Error 'Missing RS Truststore Password' unless options.ssl?.truststore?.password
        options.opts.java_properties['javax.net.ssl.trustStorePassword'] ?=  options.ssl.truststore.password
      else
        options.jmx_config['com.sun.management.jmxremote.ssl'] ?= 'false'

## Enable JMX Authentication

      options.authenticate ?= 'false'
      if options.authenticate
        options.username ?= 'monitorRole'# be careful if changing , should configure access file
        options.jmx_auth_file ?=  '/etc/security/jmxPasswords/hbase-master.password'
        options.jmx_config['com.sun.management.jmxremote.authenticate'] ?= 'true'
        throw Error 'Missing options.password' unless options.password
        options.jmx_config['com.sun.management.jmxremote.password.file'] ?= options.jmx_auth_file
        # user password file for authentication

## Configuration
configure JMX Exporter to scrape HBase master metrics.

      options.java_home ?= service.deps.java.options.java_home
      options.conf_file ?= "#{options.conf_dir}/hbase_master.yaml"
      # Misc
      options.fqdn ?= service.node.fqdn
      options.hostname = service.node.hostname
      options.iptables ?= service.deps.iptables and service.deps.iptables.options.action is 'start'
      options.clean_logs ?= false
      options.port ?= 5562
      options.cluster_name ?= "ryba-env-metal"
      options.config ?= {}
      options.config['startDelaySeconds'] ?= 0
      options.config['ssl'] ?= false
      options.config['hostPort'] ?= "#{service.deps.hbase_master.node.fqdn}:#{options.jmx_config['com.sun.management.jmxremote.port']}"
      options.config['lowercaseOutputName'] ?= true
      options.config['rules'] ?= []
      options.config['rules'].push 'pattern': '.*'
      #authentication
      options.config['username'] ?= options.username
      options.config['password'] ?= options.password
      options.config['ssl'] ?= false
      # options.config['ssl'] ?= service.deps.hadoop_core.options.opts.java_properties['com.sun.management.jmxremote.ssl'] is 'true'

## Register Prometheus Scrapper
configure by default two new label, one cluster and the other service
Note: cluster name shoul not contain other character than ([a-zA-Z0-9\-\_]*)

      options.relabel_configs ?= [
          source_labels: ['job']
          regex: "([a-zA-Z0-9\\-\\_]*).([a-zA-Z0-9]*)"
          target_label: "cluster"
          replacement: "$1"
        ,
          source_labels: ['job']
          regex: "([a-zA-Z0-9\\-\\_]*).([a-zA-Z0-9]*)"
          target_label: "service"
          replacement: "$2"
        ,
          source_labels: ['__address__']
          regex: "([a-zA-Z0-9\\-\\_\\.]*):([a-zA-Z0-9]*)"
          target_label: "hostname"
          replacement: "$1"
        ]
      for srv in service.deps.prometheus_monitor
        srv.options.config ?= {}
        srv.options.config['scrape_configs'] ?= []
        scrape_config = null
        # iterate through configuration to find zookeeper's one
        # register current fqdn if not already existing
        for conf in srv.options.config['scrape_configs']
          scrape_config = conf if conf.job_name is "#{options.cluster_name}.hbasemaster"
        exist = scrape_config?
        scrape_config ?=
          job_name: "#{options.cluster_name}.hbasemaster"
          static_configs:
            [
              targets: []
            ]
          relabel_configs: options.relabel_configs
        for static_config in scrape_config.static_configs
          hostPort = "#{service.node.fqdn}:#{options.port}"
          static_config.targets ?= []
          static_config.targets.push hostPort unless static_config.targets.indexOf(hostPort) isnt -1
        srv.options.config['scrape_configs'].push scrape_config unless exist

## Wait

      options.wait ?= {}
      options.wait.jmx ?=
        host: service.node.fqdn
        port: options.port or '5563'

## Dependencies

    {merge} = require '@nikitajs/core/lib/misc'

[jmx_exporter]:(https://github.com/prometheus/jmx_exporter)
