diff --git a/collectd.yml b/collectd.yml index 1ce7e41..102a389 100644 --- a/collectd.yml +++ b/collectd.yml @@ -3,6 +3,9 @@ - role: collectd tags: - collectd + - role: collectd-version + tags: + - collectd-version - hosts: collectd-prometheus roles: diff --git a/roles/collectd-version/files/collectd-version.py b/roles/collectd-version/files/collectd-version.py new file mode 100644 index 0000000..4b44605 --- /dev/null +++ b/roles/collectd-version/files/collectd-version.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 +import os +import shlex +import socket + + +SECONDS = 1 +MINUTES = 60 * SECONDS +HOURS = 60 * MINUTES +DAYS = 24 * HOURS + +COLLECTD_SOCKET = os.environ.get('COLLECTD_SOCKET', '/run/collectd/socket') +COLLECTD_INTERVAL = int(os.environ.get('COLLECTD_INTERVAL', 365 * DAYS)) + + +def parse_os_release(): + data = {} + with open('/etc/os-release', encoding='utf-8') as f: + for line in f: + line = line.split('#')[0].strip() + if not line: + continue + key, __, value = shlex.split(line)[0].partition('=') + if not key or not value: + continue + data[key] = value + return data + + +def get_values(): + osinfo = parse_os_release() + if 'PRETTY_NAME' in osinfo: + yield 'name', osinfo['PRETTY_NAME'] + if 'VERSION_ID' in osinfo and 'ID' in osinfo: + yield osinfo['ID'], osinfo['VERSION_ID'] + yield 'kernel', os.uname().release + + +def main(): + hostname = socket.getfqdn() + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect(COLLECTD_SOCKET) + with sock: + for valtype, value in get_values(): + identifier = f'{hostname}/version-{value}/version-{valtype}' + line = f'PUTVAL "{identifier}" interval={COLLECTD_INTERVAL} N:1\n' + sock.send(line.encode('utf-8')) + + +if __name__ == '__main__': + main() diff --git a/roles/collectd-version/files/collectd-version.service b/roles/collectd-version/files/collectd-version.service new file mode 100644 index 0000000..c711b5d --- /dev/null +++ b/roles/collectd-version/files/collectd-version.service @@ -0,0 +1,11 @@ +[Unit] +Description=collectd version plugin +BindsTo=collectd.service +After=collectd.service + +[Service] +ExecStart=/usr/local/libexec/collectd-version +RemainAfterExit=true + +[Install] +WantedBy=collectd.service diff --git a/roles/collectd-version/handlers/main.yml b/roles/collectd-version/handlers/main.yml new file mode 100644 index 0000000..289be42 --- /dev/null +++ b/roles/collectd-version/handlers/main.yml @@ -0,0 +1,4 @@ +- name: restart collectd-version + service: + name: collectd-version + state: restarted diff --git a/roles/collectd-version/tasks/main.yml b/roles/collectd-version/tasks/main.yml new file mode 100644 index 0000000..3394937 --- /dev/null +++ b/roles/collectd-version/tasks/main.yml @@ -0,0 +1,32 @@ +- name: ensure collectd-version script is installed + copy: + src: collectd-version.py + dest: /usr/local/libexec/collectd-version + mode: 'u=rwx,go=rx' + notify: restart collectd + tags: + - install + +- name: ensure collectd-version.service systemd unit exists + copy: + src: collectd-version.service + dest: /etc/systemd/system/ + mode: 'u=rw,go=rx' + notify: + - reload systemd + - restart collectd-version + tags: + - systemd + +- name: ensure collectd-version service starts automatically + service: + name: collectd-version + enabled: true + tags: + - service +- name: ensure collectd-version service is started + service: + name: collectd-version + state: started + tags: + - service