r/collectd-version: Add OS version plugin

The `collectd-version` script uses the *collectd* UNIX socket to send
custom values to *collectd* to track the OS version.  Since these values
obviously cannot change while the system is running, the values are
specified with a very long interval.  This avoids having to continuously
insert the values, either with a long-running process or by repeatedly
running a script.  The values only need to be inserted once when
*collectd* starts.
ntfy
Dustin 2021-10-30 16:50:37 -05:00
parent 12c3fb950b
commit a178dc4af9
5 changed files with 101 additions and 0 deletions

View File

@ -3,6 +3,9 @@
- role: collectd
tags:
- collectd
- role: collectd-version
tags:
- collectd-version
- hosts: collectd-prometheus
roles:

View File

@ -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()

View File

@ -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

View File

@ -0,0 +1,4 @@
- name: restart collectd-version
service:
name: collectd-version
state: restarted

View File

@ -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