-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a role to deploy https://github.com/google/cadvisor. Signed-off-by: SuperQ <[email protected]>
- Loading branch information
Showing
24 changed files
with
641 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<p><img src="https://www.circonus.com/wp-content/uploads/2015/03/sol-icon-itOps.png" alt="graph logo" title="graph" align="right" height="60" /></p> | ||
|
||
# Ansible Role: cadvisor | ||
|
||
## Description | ||
|
||
Deploy [cadvisor](https://github.com/google/cadvisor) using ansible. | ||
|
||
## Requirements | ||
|
||
- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it) | ||
- gnu-tar on Mac deployer host (`brew install gnu-tar`) | ||
- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`) | ||
|
||
## 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/cadvisor_role.html) for description and default values of the variables. | ||
|
||
## Example | ||
|
||
### Playbook | ||
|
||
Use it in a playbook as follows: | ||
```yaml | ||
- hosts: all | ||
roles: | ||
- prometheus.prometheus.cadvisor | ||
``` | ||
### 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/superq/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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Troubleshooting | ||
|
||
## Bad requests (HTTP 400) | ||
|
||
This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur. | ||
|
||
This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers. | ||
|
||
Unless netrc is needed by your playbook and ansible roles, please unset the var like so: | ||
|
||
``` | ||
$ NETRC= ansible-playbook ... | ||
``` | ||
|
||
Or: | ||
|
||
``` | ||
$ export NETRC= | ||
$ ansible-playbook ... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
cadvisor_version: 0.47.2 | ||
cadvisor_binary_local_dir: "" | ||
cadvisor_binary_url: "https://github.com/{{ _cadvisor_repo }}/releases/download/v{{ cadvisor_version }}/\ | ||
cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}" | ||
cadvisor_skip_install: false | ||
|
||
cadvisor_listen_ip: "0.0.0.0" | ||
cadvisor_port: "8080" | ||
cadvisor_prometheus_endpoint: "/metrics" | ||
|
||
cadvisor_binary_install_dir: "/usr/local/bin" | ||
cadvisor_system_group: "root" | ||
cadvisor_system_user: "{{ cadvisor_system_group }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
- name: Restart cadvisor | ||
listen: "restart cadvisor" | ||
become: true | ||
ansible.builtin.systemd: | ||
daemon_reload: true | ||
name: cadvisor | ||
state: restarted | ||
when: | ||
- not ansible_check_mode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
# yamllint disable rule:line-length | ||
argument_specs: | ||
main: | ||
short_description: "cAdvisor" | ||
description: | ||
- "Deploy L(cadvisor,https://github.com/google/cadvisor) using ansible" | ||
author: | ||
- "Prometheus Community" | ||
options: | ||
cadvisor_version: | ||
description: "cAdvisor package version. Also accepts latest as parameter." | ||
default: "0.47.2" | ||
cadvisor_skip_install: | ||
description: "cAdvisor installation tasks gets skipped when set to true." | ||
type: bool | ||
default: false | ||
cadvisor_binary_local_dir: | ||
description: | ||
- "Enables the use of local packages instead of those distributed on github." | ||
- "The parameter may be set to a directory where the C(cadvisor) binary is stored on the host where ansible is run." | ||
- "This overrides the I(cadvisor_version) parameter" | ||
cadvisor_binary_url: | ||
description: "URL of the cadvisor binary file" | ||
default: "https://github.com/{{ _cadvisor_repo }}/releases/download/v{{ cadvisor_version }}/cadvisor-{{ cadvisor_version }}-linux-{{ go_arch }}" | ||
cadvisor_listen_ip: | ||
description: "Address on which cadvisor will listen" | ||
default: "0.0.0.0" | ||
cadvisor_port: | ||
description: "Port on which cadvisor will listen" | ||
default: "8080" | ||
cadvisor_prometheus_endpoint: | ||
description: "Path under which to expose metrics" | ||
default: "/metrics" | ||
cadvisor_binary_install_dir: | ||
description: | ||
- "I(Advanced)" | ||
- "Directory to install cadvisor binary" | ||
default: "/usr/local/bin" | ||
cadvisor_system_group: | ||
description: | ||
- "I(Advanced)" | ||
- "System group for cadvisor" | ||
default: "root" | ||
cadvisor_system_user: | ||
description: | ||
- "I(Advanced)" | ||
- "cAdvisor user" | ||
default: "root" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
galaxy_info: | ||
author: "cAdvisor Authors" | ||
description: "cAdvisor" | ||
license: "Apache" | ||
min_ansible_version: "2.9" | ||
platforms: | ||
- name: "Ubuntu" | ||
versions: | ||
- "focal" | ||
- "jammy" | ||
- name: "Debian" | ||
versions: | ||
- "bullseye" | ||
- "buster" | ||
- name: "EL" | ||
versions: | ||
- "7" | ||
- "8" | ||
- "9" | ||
- name: "Fedora" | ||
versions: | ||
- "37" | ||
- '38' | ||
galaxy_tags: | ||
- "monitoring" | ||
- "prometheus" | ||
- "exporter" | ||
- "metrics" | ||
- "system" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
provisioner: | ||
inventory: | ||
group_vars: | ||
all: | ||
cadvisor_binary_local_dir: "/tmp" | ||
cadvisor_port: "8000" | ||
go_arch: amd64 | ||
cadvisor_version: 0.47.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
- name: Run local preparation | ||
hosts: localhost | ||
vars_files: | ||
- ../defaults/main.yml | ||
gather_facts: false | ||
tasks: | ||
- name: Download cadvisor binary to local folder | ||
become: false | ||
ansible.builtin.get_url: | ||
url: "{{ cadvisor_binary_url }}" | ||
dest: "/tmp/cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}" | ||
mode: 0644 | ||
register: _download_binary | ||
until: _download_binary is succeeded | ||
retries: 5 | ||
delay: 2 | ||
check_mode: false | ||
|
||
- name: Link to cadvisor binaries directory | ||
become: false | ||
ansible.builtin.file: | ||
src: "/tmp/cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}" | ||
dest: "/tmp/cadvisor" | ||
state: link | ||
check_mode: false |
35 changes: 35 additions & 0 deletions
35
roles/cadvisor/molecule/alternative/tests/test_alternative.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
import os | ||
import testinfra.utils.ansible_runner | ||
import pytest | ||
|
||
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( | ||
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') | ||
|
||
|
||
def test_service(host): | ||
s = host.service("cadvisor") | ||
try: | ||
assert s.is_running | ||
except AssertionError: | ||
# Capture service logs | ||
journal_output = host.run('journalctl -u cadvisor --since "1 hour ago"') | ||
print("\n==== journalctl -u cadvisor Output ====\n") | ||
print(journal_output) | ||
print("\n============================================\n") | ||
raise # Re-raise the original assertion error | ||
|
||
|
||
def test_protecthome_property(host): | ||
s = host.service("cadvisor") | ||
p = s.systemd_properties | ||
assert p.get("ProtectHome") == "yes" | ||
|
||
|
||
@pytest.mark.parametrize("sockets", [ | ||
"tcp://127.0.0.1:8000", | ||
]) | ||
def test_socket(host, sockets): | ||
assert host.socket(sockets).is_listening |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
provisioner: | ||
inventory: | ||
group_vars: | ||
all: | ||
cadvisor_port: "8080" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
import os | ||
import testinfra.utils.ansible_runner | ||
|
||
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( | ||
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') | ||
|
||
|
||
def test_files(host): | ||
files = [ | ||
"/etc/systemd/system/cadvisor.service", | ||
"/usr/local/bin/cadvisor" | ||
] | ||
for file in files: | ||
f = host.file(file) | ||
assert f.exists | ||
assert f.is_file | ||
|
||
|
||
def test_permissions_didnt_change(host): | ||
dirs = [ | ||
"/etc", | ||
"/root", | ||
"/usr", | ||
"/var" | ||
] | ||
for file in dirs: | ||
f = host.file(file) | ||
assert f.exists | ||
assert f.is_directory | ||
assert f.user == "root" | ||
assert f.group == "root" | ||
|
||
|
||
def test_user(host): | ||
assert host.group("root").exists | ||
assert "root" in host.user("root").groups | ||
assert host.user("root").shell == "/bin/bash" | ||
assert host.user("root").home == "/root" | ||
|
||
|
||
def test_service(host): | ||
s = host.service("cadvisor") | ||
try: | ||
assert s.is_running | ||
except AssertionError: | ||
# Capture service logs | ||
journal_output = host.run('journalctl -u cadvisor --since "1 hour ago"') | ||
print("\n==== journalctl -u cadvisor Output ====\n") | ||
print(journal_output) | ||
print("\n============================================\n") | ||
raise # Re-raise the original assertion error | ||
|
||
|
||
def test_protecthome_property(host): | ||
s = host.service("cadvisor") | ||
p = s.systemd_properties | ||
assert p.get("ProtectHome") == "yes" | ||
|
||
|
||
def test_socket(host): | ||
sockets = [ | ||
"tcp://127.0.0.1:8080" | ||
] | ||
for socket in sockets: | ||
s = host.socket(socket) | ||
assert s.is_listening |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
provisioner: | ||
inventory: | ||
group_vars: | ||
all: | ||
cadvisor_version: latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
import os | ||
import testinfra.utils.ansible_runner | ||
import pytest | ||
|
||
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( | ||
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') | ||
|
||
|
||
@pytest.mark.parametrize("files", [ | ||
"/etc/systemd/system/cadvisor.service", | ||
"/usr/local/bin/cadvisor" | ||
]) | ||
def test_files(host, files): | ||
f = host.file(files) | ||
assert f.exists | ||
assert f.is_file | ||
|
||
|
||
def test_service(host): | ||
s = host.service("cadvisor") | ||
try: | ||
assert s.is_running | ||
except AssertionError: | ||
# Capture service logs | ||
journal_output = host.run('journalctl -u cadvisor --since "1 hour ago"') | ||
print("\n==== journalctl -u cadvisor Output ====\n") | ||
print(journal_output) | ||
print("\n============================================\n") | ||
raise # Re-raise the original assertion error | ||
|
||
|
||
def test_protecthome_property(host): | ||
s = host.service("cadvisor") | ||
p = s.systemd_properties | ||
assert p.get("ProtectHome") == "yes" | ||
|
||
|
||
def test_socket(host): | ||
s = host.socket("tcp://0.0.0.0:8080") | ||
assert s.is_listening |
Oops, something went wrong.