]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # -*- coding: utf-8 -*- |
2 | from __future__ import absolute_import | |
3 | ||
f67539c2 TL |
4 | from typing import Any, Dict |
5 | ||
11fdf7f2 TL |
6 | import cherrypy |
7 | ||
11fdf7f2 TL |
8 | from .. import mgr |
9 | from ..security import Scope | |
10 | from ..services.ceph_service import CephService | |
f67539c2 TL |
11 | from . import ApiController, ControllerDoc, EndpointDoc, RESTController |
12 | ||
13 | PERF_SCHEMA = { | |
14 | "mon.a": ({ | |
15 | ".cache_bytes": ({ | |
16 | "description": (str, ""), | |
17 | "nick": (str, ""), | |
18 | "type": (int, ""), | |
19 | "priority": (int, ""), | |
20 | "units": (int, ""), | |
21 | "value": (int, "") | |
22 | }, ""), | |
23 | }, "Service ID"), | |
24 | } | |
11fdf7f2 TL |
25 | |
26 | ||
27 | class PerfCounter(RESTController): | |
28 | service_type = None # type: str | |
29 | ||
30 | def get(self, service_id): | |
31 | schema_dict = mgr.get_perf_schema(self.service_type, str(service_id)) | |
32 | try: | |
33 | schema = schema_dict["{}.{}".format(self.service_type, service_id)] | |
34 | except KeyError as e: | |
35 | raise cherrypy.HTTPError(404, "{0} not found".format(e)) | |
36 | counters = [] | |
37 | ||
38 | for key, value in sorted(schema.items()): | |
39 | counter = dict() | |
40 | counter['name'] = str(key) | |
41 | counter['description'] = value['description'] | |
42 | # pylint: disable=W0212 | |
43 | if mgr._stattype_to_str(value['type']) == 'counter': | |
44 | counter['value'] = CephService.get_rate( | |
45 | self.service_type, service_id, key) | |
46 | counter['unit'] = mgr._unit_to_str(value['units']) | |
47 | else: | |
48 | counter['value'] = mgr.get_latest( | |
49 | self.service_type, service_id, key) | |
50 | counter['unit'] = '' | |
51 | counters.append(counter) | |
52 | ||
53 | return { | |
54 | 'service': { | |
55 | 'type': self.service_type, | |
56 | 'id': str(service_id) | |
57 | }, | |
58 | 'counters': counters | |
59 | } | |
60 | ||
61 | ||
62 | @ApiController('perf_counters/mds', Scope.CEPHFS) | |
f67539c2 | 63 | @ControllerDoc("Mds Perf Counters Management API", "MdsPerfCounter") |
11fdf7f2 TL |
64 | class MdsPerfCounter(PerfCounter): |
65 | service_type = 'mds' | |
66 | ||
67 | ||
68 | @ApiController('perf_counters/mon', Scope.MONITOR) | |
f67539c2 | 69 | @ControllerDoc("Mon Perf Counters Management API", "MonPerfCounter") |
11fdf7f2 TL |
70 | class MonPerfCounter(PerfCounter): |
71 | service_type = 'mon' | |
72 | ||
73 | ||
74 | @ApiController('perf_counters/osd', Scope.OSD) | |
f67539c2 | 75 | @ControllerDoc("OSD Perf Counters Management API", "OsdPerfCounter") |
11fdf7f2 TL |
76 | class OsdPerfCounter(PerfCounter): |
77 | service_type = 'osd' | |
78 | ||
79 | ||
80 | @ApiController('perf_counters/rgw', Scope.RGW) | |
f67539c2 | 81 | @ControllerDoc("Rgw Perf Counters Management API", "RgwPerfCounter") |
11fdf7f2 TL |
82 | class RgwPerfCounter(PerfCounter): |
83 | service_type = 'rgw' | |
84 | ||
f67539c2 TL |
85 | def get(self, service_id: str) -> Dict[str, Any]: |
86 | svc_data = CephService.get_service_data_by_metadata_id(self.service_type, service_id) | |
87 | service_map_id = svc_data['service_map_id'] | |
88 | schema_dict = mgr.get_perf_schema(self.service_type, service_map_id) | |
89 | try: | |
90 | schema = schema_dict["{}.{}".format(self.service_type, service_map_id)] | |
91 | except KeyError as e: | |
92 | raise cherrypy.HTTPError(404, "{0} not found".format(e)) | |
93 | counters = [] | |
94 | ||
95 | for key, value in sorted(schema.items()): | |
96 | counter = dict() | |
97 | counter['name'] = str(key) | |
98 | counter['description'] = value['description'] | |
99 | # pylint: disable=W0212 | |
100 | if mgr._stattype_to_str(value['type']) == 'counter': | |
101 | counter['value'] = CephService.get_rate( | |
102 | self.service_type, service_map_id, key) | |
103 | counter['unit'] = mgr._unit_to_str(value['units']) | |
104 | else: | |
105 | counter['value'] = mgr.get_latest( | |
106 | self.service_type, service_map_id, key) | |
107 | counter['unit'] = '' | |
108 | counters.append(counter) | |
109 | ||
110 | return { | |
111 | 'service': { | |
112 | 'type': self.service_type, | |
113 | 'id': svc_data['id'] | |
114 | }, | |
115 | 'counters': counters | |
116 | } | |
117 | ||
11fdf7f2 TL |
118 | |
119 | @ApiController('perf_counters/rbd-mirror', Scope.RBD_MIRRORING) | |
f67539c2 | 120 | @ControllerDoc("Rgw Mirroring Perf Counters Management API", "RgwMirrorPerfCounter") |
11fdf7f2 TL |
121 | class RbdMirrorPerfCounter(PerfCounter): |
122 | service_type = 'rbd-mirror' | |
123 | ||
124 | ||
125 | @ApiController('perf_counters/mgr', Scope.MANAGER) | |
f67539c2 | 126 | @ControllerDoc("Mgr Perf Counters Management API", "MgrPerfCounter") |
11fdf7f2 TL |
127 | class MgrPerfCounter(PerfCounter): |
128 | service_type = 'mgr' | |
129 | ||
130 | ||
81eedcae | 131 | @ApiController('perf_counters/tcmu-runner', Scope.ISCSI) |
f67539c2 | 132 | @ControllerDoc("Tcmu Runner Perf Counters Management API", "TcmuRunnerPerfCounter") |
81eedcae TL |
133 | class TcmuRunnerPerfCounter(PerfCounter): |
134 | service_type = 'tcmu-runner' | |
135 | ||
136 | ||
11fdf7f2 | 137 | @ApiController('perf_counters') |
f67539c2 | 138 | @ControllerDoc("Perf Counters Management API", "PerfCounters") |
11fdf7f2 | 139 | class PerfCounters(RESTController): |
f67539c2 TL |
140 | @EndpointDoc("Display Perf Counters", |
141 | responses={200: PERF_SCHEMA}) | |
11fdf7f2 TL |
142 | def list(self): |
143 | return mgr.get_all_perf_counters() |