Skip to content

Commit 4f1d979

Browse files
committed
cephadm: configuring prometheus endpoint for smb
Allow Prometheus to scrap samba-server metrics via default port 9922[1]. Uses the same conventions as used by NFS-Ganesha Prometheus metrics exporter. [1] https://github.com/prometheus/prometheus/wiki/Default-port-allocations Signed-off-by: Shachar Sharon <[email protected]>
1 parent 2c40969 commit 4f1d979

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

src/pybind/mgr/cephadm/service_discovery.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Server: # type: ignore
1313
from mgr_util import build_url
1414
from typing import Dict, List, TYPE_CHECKING, cast, Collection, Callable, NamedTuple, Optional, IO
1515
from cephadm.services.nfs import NFSService
16+
from cephadm.services.smb import SMBService
1617
from cephadm.services.monitoring import AlertmanagerService, NodeExporterService, PrometheusService
1718
import secrets
1819
from mgr_util import verify_tls_files
@@ -149,6 +150,7 @@ def index(self) -> str:
149150
<p><a href='prometheus/sd-config?service=ceph-exporter'>Ceph exporter http sd-config</a></p>
150151
<p><a href='prometheus/sd-config?service=nvmeof'>NVMeoF http sd-config</a></p>
151152
<p><a href='prometheus/sd-config?service=nfs'>NFS http sd-config</a></p>
153+
<p><a href='prometheus/sd-config?service=smb'>SMB http sd-config</a></p>
152154
<p><a href='prometheus/rules'>Prometheus rules</a></p>
153155
</body>
154156
</html>'''
@@ -171,6 +173,8 @@ def get_sd_config(self, service: str) -> List[Dict[str, Collection[str]]]:
171173
return self.nvmeof_sd_config()
172174
elif service == 'nfs':
173175
return self.nfs_sd_config()
176+
elif service == 'smb':
177+
return self.smb_sd_config()
174178
else:
175179
return []
176180

@@ -265,6 +269,19 @@ def nfs_sd_config(self) -> List[Dict[str, Collection[str]]]:
265269
})
266270
return srv_entries
267271

272+
def smb_sd_config(self) -> List[Dict[str, Collection[str]]]:
273+
"""Return <http_sd_config> compatible prometheus config for smb service."""
274+
srv_entries = []
275+
for dd in self.mgr.cache.get_daemons_by_type('smb'):
276+
assert dd.hostname is not None
277+
addr = dd.ip if dd.ip else self.mgr.inventory.get_addr(dd.hostname)
278+
port = SMBService.DEFAULT_EXPORTER_PORT
279+
srv_entries.append({
280+
'targets': [build_url(host=addr, port=port).lstrip('/')],
281+
'labels': {'instance': dd.hostname}
282+
})
283+
return srv_entries
284+
268285
@cherrypy.expose(alias='prometheus/rules')
269286
def get_prometheus_rules(self) -> str:
270287
"""Return currently configured prometheus rules as Yaml."""

src/pybind/mgr/cephadm/services/monitoring.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ def generate_config(
503503
nvmeof_sd_url = f'{srv_end_point}service=nvmeof' # always included
504504
mgmt_gw_enabled = len(self.mgr.cache.get_daemons_by_service('mgmt-gateway')) > 0
505505
nfs_sd_url = f'{srv_end_point}service=nfs' # always included
506+
smb_sd_url = f'{srv_end_point}service=smb' # always included
506507

507508
alertmanager_user, alertmanager_password = self.mgr._get_alertmanager_credentials()
508509
prometheus_user, prometheus_password = self.mgr._get_prometheus_credentials()
@@ -524,7 +525,8 @@ def generate_config(
524525
'nvmeof_sd_url': nvmeof_sd_url,
525526
'external_prometheus_targets': targets,
526527
'cluster_fsid': FSID,
527-
'nfs_sd_url': nfs_sd_url
528+
'nfs_sd_url': nfs_sd_url,
529+
'smb_sd_url': smb_sd_url
528530
}
529531

530532
ip_to_bind_to = ''

src/pybind/mgr/cephadm/templates/services/prometheus/prometheus.yml.j2

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ scrape_configs:
181181
{% endif %}
182182
{% endif %}
183183

184+
{% if smb_sd_url %}
185+
- job_name: 'smb'
186+
{% if security_enabled %}
187+
honor_labels: true
188+
scheme: https
189+
tls_config:
190+
ca_file: root_cert.pem
191+
http_sd_configs:
192+
- url: {{ smb_sd_url }}
193+
basic_auth:
194+
username: {{ service_discovery_username }}
195+
password: {{ service_discovery_password }}
196+
tls_config:
197+
ca_file: root_cert.pem
198+
{% else %}
199+
http_sd_configs:
200+
- url: {{ smb_sd_url }}
201+
{% endif %}
202+
{% endif %}
203+
184204
{% if not security_enabled %}
185205
- job_name: 'federate'
186206
scrape_interval: 15s

src/pybind/mgr/cephadm/tests/test_service_discovery.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def get_daemons_by_service(self, service_type):
2727
return [FakeDaemonDescription('1.2.3.4', [9587], 'node0'),
2828
FakeDaemonDescription('1.2.3.5', [9587], 'node1')]
2929

30+
if service_type == 'smb':
31+
return [FakeDaemonDescription('1.2.3.4', [9922], 'node0'),
32+
FakeDaemonDescription('1.2.3.5', [9922], 'node1')]
33+
3034
return [FakeDaemonDescription('1.2.3.4', [9100], 'node0'),
3135
FakeDaemonDescription('1.2.3.5', [9200], 'node1')]
3236

@@ -206,6 +210,20 @@ def test_get_sd_config_nfs(self):
206210
# check content
207211
assert cfg[0]['targets'] == ['1.2.3.4:9587']
208212

213+
def test_get_sd_config_smb(self):
214+
mgr = FakeMgr()
215+
root = Root(mgr, 5000, '0.0.0.0')
216+
cfg = root.get_sd_config('smb')
217+
218+
# check response structure
219+
assert cfg
220+
for entry in cfg:
221+
assert 'labels' in entry
222+
assert 'targets' in entry
223+
224+
# check content
225+
assert cfg[0]['targets'] == ['1.2.3.4:9922']
226+
209227
def test_get_sd_config_invalid_service(self):
210228
mgr = FakeMgr()
211229
root = Root(mgr, 5000, '0.0.0.0')

src/pybind/mgr/cephadm/tests/test_services.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,10 @@ def test_prometheus_config_security_disabled(self, _run_cephadm, cephadm_module:
828828
http_sd_configs:
829829
- url: http://[::1]:8765/sd/prometheus/sd-config?service=nfs
830830
831+
- job_name: 'smb'
832+
http_sd_configs:
833+
- url: http://[::1]:8765/sd/prometheus/sd-config?service=smb
834+
831835
- job_name: 'federate'
832836
scrape_interval: 15s
833837
honor_labels: true
@@ -1038,6 +1042,19 @@ def gen_cert(host, addr):
10381042
tls_config:
10391043
ca_file: root_cert.pem
10401044
1045+
- job_name: 'smb'
1046+
honor_labels: true
1047+
scheme: https
1048+
tls_config:
1049+
ca_file: root_cert.pem
1050+
http_sd_configs:
1051+
- url: https://[::1]:8765/sd/prometheus/sd-config?service=smb
1052+
basic_auth:
1053+
username: sd_user
1054+
password: sd_password
1055+
tls_config:
1056+
ca_file: root_cert.pem
1057+
10411058
""").lstrip()
10421059

10431060
_run_cephadm.assert_called_with(

0 commit comments

Comments
 (0)