Skip to content

Commit

Permalink
Merge pull request #21061 from jan--f/jan-prometheus-file-sd-command
Browse files Browse the repository at this point in the history
pybind/mgr/prometheus: add file_sd_config command

Reviewed-by: Boris Ranto <branto@redhat.com>
Reviewed-by: John Spray <john.spray@redhat.com>
  • Loading branch information
liewegas committed Apr 26, 2018
2 parents 2332e63 + 2eb0b41 commit 510762f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
12 changes: 12 additions & 0 deletions qa/tasks/mgr/test_prometheus.py
Expand Up @@ -2,6 +2,7 @@

from mgr_test_case import MgrTestCase

import json
import logging
import requests

Expand All @@ -15,6 +16,17 @@ class TestPrometheus(MgrTestCase):
def setUp(self):
self.setup_mgrs()

def test_file_sd_command(self):
self._assign_ports("prometheus", "server_port")
self._load_module("prometheus")

result = json.loads(self.mgr_cluster.mon_manager.raw_cluster_cmd(
"prometheus", "file_sd_config"))
mgr_map = self.mgr_cluster.get_mgr_map()
self.assertEqual(len(result[0]['targets']), len(mgr_map['standbys']) + 1)



def test_standby(self):
self._assign_ports("prometheus", "server_port")
self._load_module("prometheus")
Expand Down
45 changes: 44 additions & 1 deletion src/pybind/mgr/prometheus/module.py
Expand Up @@ -5,7 +5,7 @@
import os
import socket
from collections import OrderedDict
from mgr_module import MgrModule, MgrStandbyModule
from mgr_module import MgrModule, MgrStandbyModule, CommandResult

# Defaults for the Prometheus HTTP server. Can also set in config-key
# see https://github.com/prometheus/prometheus/wiki/Default-port-allocations
Expand Down Expand Up @@ -335,6 +335,11 @@ class Module(MgrModule):
"desc": "Run a self test on the prometheus module",
"perm": "rw"
},
{
"cmd": "prometheus file_sd_config",
"desc": "Return file_sd compatible prometheus config for mgr cluster",
"perm": "r"
},
]

OPTIONS = [
Expand Down Expand Up @@ -565,10 +570,48 @@ def collect(self):

return self.metrics.metrics

def get_file_sd_config(self):
servers = self.list_servers()
targets = []
for server in servers:
hostname = server.get('hostname', '')
for service in server.get('services', []):
if service['type'] != 'mgr':
continue
id_ = service['id']
# get port for prometheus module at mgr with id_
# TODO use get_config_prefix or get_config here once
# https://github.com/ceph/ceph/pull/20458 is merged
result = CommandResult("")
global_instance().send_command(
result, "mon", '',
json.dumps({
"prefix": "config-key get",
'key': "config/mgr/mgr/prometheus/{}/server_port".format(id_),
}),
"")
r, outb, outs = result.wait()
if r != 0:
global_instance().log.error("Failed to retrieve port for mgr {}: {}".format(id_, outs))
else:
port = json.loads(outb)
targets.append('{}:{}'.format(hostname, port))

ret = [
{
"targets": targets,
"labels": {}
}
]
return 0, json.dumps(ret), ""

def handle_command(self, cmd):
if cmd['prefix'] == 'prometheus self-test':
self.collect()
self.get_file_sd_config()
return 0, '', 'Self-test OK'
elif cmd['prefix'] == 'prometheus file_sd_config':
return self.get_file_sd_config()
else:
return (-errno.EINVAL, '',
"Command not found '{0}'".format(cmd['prefix']))
Expand Down

0 comments on commit 510762f

Please sign in to comment.