Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions roles/jmx_exporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Ansible Role: jmx exporter

Deploy prometheus [jmx exporter](https://github.com/prometheus/jmx_exporter/tree/main) using ansible.

## Requirements

- gnu-tar on Mac deployer host (`brew install gnu-tar`)
- Passlib is required when using the basic authentication feature (`python -m pip install passlib bcrypt==4.0.1`)

## Role Variables
All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml).
Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/jmx_exporter_role.html) for description and default values of the variables.

## Example

### Playbook

Use it in a playbook as follows:
```yaml
- hosts: all
collections:
- prometheus.prometheus
roles:
- prometheus.prometheus.jmx_exporter
```

### TLS config

Before running jmx_exporter role, the user needs to provision their own certificate and key.
```yaml
- hosts: all
pre_tasks:
- name: Create jmx_exporter cert dir
ansible.builtin.file:
path: "/etc/jmx_exporter"
state: directory
owner: root
group: root

- name: Create cert and key
community.crypto.x509_certificate:
path: /etc/jmx_exporter/tls.cert
csr_path: /etc/jmx_exporter/tls.csr
privatekey_path: /etc/jmx_exporter/tls.key
provider: selfsigned
collections:
- prometheus.prometheus
roles:
- prometheus.prometheus.jmx_exporter
vars:
jmx_exporter_tls_server_config:
cert_file: /etc/jmx_exporter/tls.cert
key_file: /etc/jmx_exporter/tls.key
jmx_exporter_basic_auth_users:
randomuser: examplepassword
```

### Demo site

We provide an example site that demonstrates a full monitoring solution based on prometheus and grafana. The repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and the site is hosted on [DigitalOcean](https://digitalocean.com).

## Local Testing

The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable for your system. Running your tests is as simple as executing `molecule test`.

## Continuous Integration

Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have quite a large test matrix which can take more time than local testing, so please be patient.

## Contributing

See [contributor guideline](CONTRIBUTING.md).

## Troubleshooting

See [troubleshooting](TROUBLESHOOTING.md).

## License

This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details.
20 changes: 20 additions & 0 deletions roles/jmx_exporter/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
jmx_exporter_version: 1.6.0
jmx_exporter_binary_url: "https://github.com/{{ _jmx_exporter_repo }}/releases/download/v{{ jmx_exporter_version }}/\
jmx_prometheus_javaagent-{{ jmx_exporter_version }}.jar"
jmx_exporter_checksums_url: "https://github.com/{{ _jmx_exporter_repo }}/releases/download/v{{ jmx_exporter_version }}/jmx_prometheus_javaagent-{{ jmx_exporter_version }}.jar.sha256"

jmx_exporter_web_listen_address: "0.0.0.0:9404"

jmx_exporter_tls_server_config: {}
jmx_exporter_http_server_config: {}
jmx_exporter_basic_auth_users: {}

jmx_exporter_binary_install_dir: "/usr/local/bin"
jmx_exporter_system_user: "jmx-exp"
jmx_exporter_system_group: "{{ jmx_exporter_system_user }}"
jmx_exporter_config_dir: "/etc/jmx_exporter"
jmx_exporter_config_file: "{{ jmx_exporter_config_dir }}/jmx_exporter.yml"

# Local path to stash the archive and its extraction
jmx_exporter_local_cache_path: "/tmp/jmx_exporter/{{ jmx_exporter_version }}"
10 changes: 10 additions & 0 deletions roles/jmx_exporter/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Restart jmx_exporter
listen: "restart jmx_exporter"
become: true
ansible.builtin.systemd:
daemon_reload: true
name: jmx_exporter
state: restarted
when:
- not ansible_check_mode
65 changes: 65 additions & 0 deletions roles/jmx_exporter/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
# yamllint disable rule:line-length
argument_specs:
main:
short_description: "Prometheus jmx_exporter"
description:
- "Deploy prometheus L(jmx exporter,https://github.com/prometheus/jmx_exporter) using ansible"
author:
- "Prometheus Community"
options:
jmx_exporter_version:
description: "jmx_exporter package version. Also accepts latest as parameter."
default: "1.6.0"
jmx_exporter_binary_url:
description: "URL of the jmx_exporter binaries .jar file"
default: "https://github.com/{{ _jmx_exporter_repo }}/releases/download/v{{ jmx_exporter_version }}/jmx_prometheus_javaagent-{{ jmx_exporter_version }}.jar"
jmx_exporter_checksums_url:
description: "URL of the jmx_exporter checksums file"
default: "https://github.com/{{ _jmx_exporter_repo }}/releases/download/v{{ jmx_exporter_version }}/jmx_prometheus_javaagent-{{ jmx_exporter_version }}.jar.sha256"
jmx_exporter_web_listen_address:
description: "Address on which jmx exporter will listen"
default: "0.0.0.0:9404"
jmx_exporter_tls_server_config:
description:
- "Configuration for TLS authentication."
- "Keys and values are the same as in L(jmx_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
type: "dict"
jmx_exporter_http_server_config:
description:
- "Config for HTTP/2 support."
- "Keys and values are the same as in L(jmx_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
type: "dict"
jmx_exporter_basic_auth_users:
description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt."
type: "dict"
jmx_exporter_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install jmx_exporter binary"
default: "/usr/local/bin"
jmx_exporter_config_file:
description:
- "I(Advanced)"
- "Path to the JMX Exporter configuration file"
default: "/etc/jmx_exporter/jmx_exporter.yml"
jmx_exporter_system_user:
description:
- "I(Advanced)"
- "System user for jmx_exporter"
default: "jmx-exp"
jmx_exporter_system_group:
description:
- "I(Advanced)"
- "System group for jmx_exporter"
default: "jmx-exp"
jmx_exporter_config_dir:
description:
- "I(Advanced)"
- "Directory to store jmx_exporter configuration"
default: "/etc/jmx_exporter"
jmx_exporter_local_cache_path:
description:
- "I(Advanced)"
- "Local path to stash the archive and its extraction"
default: "/tmp/jmx_exporter/{{ jmx_exporter_version }}"
26 changes: 26 additions & 0 deletions roles/jmx_exporter/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
galaxy_info:
author: "Prometheus Community"
description: "Prometheus jmx_exporter"
license: "jmx"
min_ansible_version: "2.14"
platforms:
- name: "Ubuntu"
versions:
- "jammy"
- "noble"
- name: "Debian"
versions:
- "bullseye"
- "bookworm"
- name: "EL"
versions:
- "9"
- "10"
galaxy_tags:
- "monitoring"
- "prometheus"
- "exporter"
- "metrics"
- "system"
- "jmx"
16 changes: 16 additions & 0 deletions roles/jmx_exporter/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
provisioner:
playbooks:
prepare: "${MOLECULE_PROJECT_DIRECTORY}/../../.config/molecule/alternative/prepare.yml"
inventory:
group_vars:
all:
jmx_exporter_web_listen_address: "127.0.0.1:9118"
jmx_exporter_tls_server_config:
cert_file: /etc/jmx_exporter/tls.cert
key_file: /etc/jmx_exporter/tls.key
jmx_exporter_http_server_config:
http2: true
jmx_exporter_basic_auth_users:
randomuser: examplepassword
jmx_exporter_version: 1.0.8
61 changes: 61 additions & 0 deletions roles/jmx_exporter/molecule/alternative/tests/test_alternative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from testinfra_helpers import get_target_hosts

testinfra_hosts = get_target_hosts()


def test_directories(host):
dirs = [
"/etc/jmx_exporter"
]
for dir in dirs:
d = host.file(dir)
assert d.is_directory
assert d.exists


def test_files(host):
files = [
"/etc/systemd/system/jmx_exporter.service",
"//usr/local/bin/jmx_exporter"
]
for file in files:
f = host.file(file)
assert f.exists
assert f.is_file


def test_user(host):
assert host.group("jmx-exp").exists
assert "jmx-exp" in host.user("jmx-exp").groups
assert host.user("jmx-exp").shell == "/usr/sbin/nologin"


def test_service(host):
s = host.service("jmx_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u jmx_exporter --since "1 hour ago"')
print("\n==== journalctl -u jmx_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("jmx_exporter")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


def test_socket(host):
sockets = [
"tcp://127.0.0.1:9118"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening
6 changes: 6 additions & 0 deletions roles/jmx_exporter/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
jmx_exporter_web_listen_address: "127.0.0.1:9117"
61 changes: 61 additions & 0 deletions roles/jmx_exporter/molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from testinfra_helpers import get_target_hosts

testinfra_hosts = get_target_hosts()


def test_directories(host):
dirs = [
"/etc/jmx_exporter"
]
for dir in dirs:
d = host.file(dir)
assert d.is_directory
assert d.exists


def test_files(host):
files = [
"/etc/systemd/system/jmx_exporter.service",
"//usr/local/bin/jmx_exporter"
]
for file in files:
f = host.file(file)
assert f.exists
assert f.is_file


def test_user(host):
assert host.group("jmx-exp").exists
assert "jmx-exp" in host.user("jmx-exp").groups
assert host.user("jmx-exp").shell == "/usr/sbin/nologin"


def test_service(host):
s = host.service("jmx_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u jmx_exporter --since "1 hour ago"')
print("\n==== journalctl -u jmx_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("jmx_exporter")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


def test_socket(host):
sockets = [
"tcp://127.0.0.1:9117"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening
6 changes: 6 additions & 0 deletions roles/jmx_exporter/molecule/latest/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
jmx_exporter_version: latest
Loading
Loading