diff --git a/bundles/telegraf/items.py b/bundles/telegraf/items.py index 35f17ad..9c58567 100644 --- a/bundles/telegraf/items.py +++ b/bundles/telegraf/items.py @@ -59,13 +59,13 @@ telegraf_config = { # is a dict, of which we only use the value of it. This also allows us # to overwrite values set by metadata defaults/reactors in node and group # metadata, if needed. -for config in node.metadata.get('telegraf/input_plugins/exec', {}).values(): +for config in sorted(node.metadata.get('telegraf/input_plugins/exec', {}).values(), key=lambda c: ''.join(c['commands'])): if 'exec' not in telegraf_config['inputs']: telegraf_config['inputs']['exec'] = [] telegraf_config['inputs']['exec'].append(config) -for config in node.metadata.get('telegraf/input_plugins/execd', {}).values(): +for config in sorted(node.metadata.get('telegraf/input_plugins/execd', {}).values(), key=lambda c: ''.join(c['commands'])): if 'execd' not in telegraf_config['inputs']: telegraf_config['inputs']['execd'] = [] diff --git a/bundles/transmission/files/telegraf-transmission-plugin b/bundles/transmission/files/telegraf-transmission-plugin new file mode 100644 index 0000000..ac52142 --- /dev/null +++ b/bundles/transmission/files/telegraf-transmission-plugin @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +from transmission_rpc import Client + +c = Client(host='127.0.0.1', port=${rpc_port}) +torrents = c.get_torrents() + +total_progress = 0 +total_available = 0 +total_upload = 0 +total_download = 0 + +for torrent in torrents: + name = torrent.name.replace(' ', '\\ ') + print(f'transmission,type=detail,name={name} percent_done={torrent.progress}') + print(f'transmission,type=detail,name={name} percent_avail={torrent.available}') + print(f'transmission,type=detail,name={name} ratio={torrent.ratio}') + print(f'transmission,type=detail,name={name} size={torrent.total_size}') + print(f'transmission,type=detail,name={name} upload={torrent.rateUpload}') + print(f'transmission,type=detail,name={name} download={torrent.rateDownload}') + + total_progress += torrent.progress + total_available += torrent.available + total_upload += torrent.rateUpload + total_download += torrent.rateDownload + +total_progress_pct = round((total_progress/(len(torrents)*100))*100, 3) +total_available_pct = round((total_available/(len(torrents)*100))*100, 3) + +print(f'transmission,type=global num_torrents={len(torrents)}') +print(f'transmission,type=global percent_done={total_progress_pct}') +print(f'transmission,type=global percent_avail={total_available_pct}') +print(f'transmission,type=global upload={total_upload}') +print(f'transmission,type=global download={total_download}') diff --git a/bundles/transmission/items.py b/bundles/transmission/items.py index 42a9f18..0306f6c 100644 --- a/bundles/transmission/items.py +++ b/bundles/transmission/items.py @@ -1,5 +1,13 @@ from bundlewrap.metadata import metadata_to_json +pkg_pip = { + 'transmission-rpc': { + 'needed_by': { + 'svc_systemd:telegraf', + }, + }, +} + files = { # XXX Manage settings using bundlewrap once transmission has an # option to disable config rewriting. @@ -14,6 +22,13 @@ files = { 'svc_systemd:transmission-daemon:restart', }, }, + '/usr/local/bin/telegraf-transmission-plugin': { + 'content_type': 'mako', + 'context': { + 'rpc_port': node.metadata['transmission']['config']['rpc-port'], + }, + 'mode': '0755', + }, } actions = { diff --git a/bundles/transmission/metadata.py b/bundles/transmission/metadata.py index cc29ccf..3ee0ad9 100644 --- a/bundles/transmission/metadata.py +++ b/bundles/transmission/metadata.py @@ -34,6 +34,20 @@ defaults = { }, } +if node.has_bundle('telegraf'): + defaults['telegraf'] = { + 'input_plugins': { + 'exec': { + 'transmission': { + 'commands': ['telegraf-transmission-plugin'], + 'interval': '10s', + 'data_format': 'influx', + 'timeout': '5s', + }, + }, + }, + } + @metadata_reactor.provides( 'iptables/port_rules',