diff --git a/tasks/getval.json b/tasks/getval.json index 7c99b1fdf..46545a215 100644 --- a/tasks/getval.json +++ b/tasks/getval.json @@ -3,6 +3,10 @@ "input_method": "stdin", "files": ["python_task_helper/files/task_helper.py"], "parameters": { + "hostname": { + "description": "Hostname of metric, as configured with Hostname in collectd", + "type": "Optional[String[1]]" + }, "metric": { "description": "Name of metric, e.g. load/load-relative", "type": "String[1]" diff --git a/tasks/getval.py b/tasks/getval.py index 3ffb86086..8284fe886 100644 --- a/tasks/getval.py +++ b/tasks/getval.py @@ -8,11 +8,24 @@ from task_helper import TaskHelper -HOSTNAME = socket.gethostname() class GetVal(TaskHelper): + def get_metric(self, hostname, metric): + fullmetric = hostname+'/'+metric + return subprocess.check_output(['/usr/bin/collectdctl', 'getval',fullmetric]).rstrip().decode().split('\n') + def task(self, args): - fullmetric = HOSTNAME+'/'+args['metric'] - results = subprocess.check_output(['/usr/bin/collectdctl', 'getval',fullmetric]).rstrip().decode().split('\n') + if 'hostname' in args: + hosts = [args['hostname']] + else: + hosts = [socket.gethostname(), socket.getfqdn()] + + for host in hosts: + try: + results = self.get_metric(host, args['metric']) + break + except: + continue + values = { k:v for k,v in (x.split('=') for x in results)} return { 'metric': args['metric'],