]> git.proxmox.com Git - ceph.git/blame - ceph/doc/mgr/plugins.rst
update sources to v12.1.1
[ceph.git] / ceph / doc / mgr / plugins.rst
CommitLineData
7c673cae
FG
1
2ceph-mgr plugin author guide
3============================
4
5Creating a plugin
6-----------------
7
8In pybind/mgr/, create a python module. Within your module, create a class
9named ``Module`` that inherits from ``MgrModule``.
10
11The most important methods to override are:
12
13* a ``serve`` member function for server-type modules. This
14 function should block forever.
15* a ``notify`` member function if your module needs to
16 take action when new cluster data is available.
17* a ``handle_command`` member function if your module
18 exposes CLI commands.
19
20Installing a plugin
21-------------------
22
23Once your module is present in the location set by the
224ce89b
WB
24``mgr module path`` configuration setting, you can enable it
25via the ``ceph mgr module enable`` command::
7c673cae 26
224ce89b 27 ceph mgr module enable mymodule
7c673cae
FG
28
29Note that the MgrModule interface is not stable, so any modules maintained
30outside of the Ceph tree are liable to break when run against any newer
31or older versions of Ceph.
32
33Logging
34-------
35
36MgrModule instances have a ``log`` property which is a logger instance that
37sends log messages into the Ceph logging layer where they will be recorded
38in the mgr daemon's log file.
39
40Use it the same way you would any other python logger. The python
41log levels debug, info, warn, err are mapped into the Ceph
42severities 20, 4, 1 and 0 respectively.
43
44Exposing commands
45-----------------
46
47Set the ``COMMANDS`` class attribute of your plugin to a list of dicts
48like this::
49
50 COMMANDS = [
51 {
52 "cmd": "foobar name=myarg,type=CephString",
53 "desc": "Do something awesome",
54 "perm": "rw"
55 }
56 ]
57
58The ``cmd`` part of each entry is parsed in the same way as internal
59Ceph mon and admin socket commands (see mon/MonCommands.h in
60the Ceph source for examples)
61
62Config settings
63---------------
64
65Modules have access to a simple key/value store (keys and values are
66byte strings) for storing configuration. Don't use this for
67storing large amounts of data.
68
69Config values are stored using the mon's config-key commands.
70
71Hints for using these:
72
73* Reads are fast: ceph-mgr keeps a local in-memory copy
74* Don't set things by hand with "ceph config-key", the mgr doesn't update
75 at runtime (only set things from within modules).
76* Writes block until the value is persisted, but reads from another
77 thread will see the new value immediately.
78
79Any config settings you want to expose to users from your module will
80need corresponding hooks in ``COMMANDS`` to expose a setter.
81
82Accessing cluster data
83----------------------
84
85Modules have access to the in-memory copies of the Ceph cluster's
86state that the mgr maintains. Accessor functions as exposed
87as members of MgrModule.
88
89Calls that access the cluster or daemon state are generally going
90from Python into native C++ routines. There is some overhead to this,
91but much less than for example calling into a REST API or calling into
92an SQL database.
93
94There are no consistency rules about access to cluster structures or
95daemon metadata. For example, an OSD might exist in OSDMap but
96have no metadata, or vice versa. On a healthy cluster these
97will be very rare transient states, but plugins should be written
98to cope with the possibility.
99
100``get(self, data_name)``
101
102Fetch named cluster-wide objects such as the OSDMap. Valid things
103to fetch are osd_crush_map_text, osd_map, osd_map_tree,
104osd_map_crush, config, mon_map, fs_map, osd_metadata, pg_summary,
105df, osd_stats, health, mon_status.
106
107All these structures have their own JSON representations: experiment
108or look at the C++ dump() methods to learn about them.
109
110``get_server(self, hostname)``
111
112Fetch metadata about a particular hostname. This is information
113that ceph-mgr has gleaned from the daemon metadata reported
114by daemons running on a particular server.
115
116``list_servers(self)``
117
118Like ``get_server``, but gives information about all servers (i.e. all
119unique hostnames that have been mentioned in daemon metadata)
120
121``get_metadata(self, svc_type, svc_id)``
122
123Fetch the daemon metadata for a particular service. svc_type is one
124of osd or mds, and svc_id is a string (convert OSD integer IDs to strings
125when calling this).
126
127``get_counter(self, svc_type, svc_name, path)``
128
129Fetch the latest performance counter data for a particular counter. The
130path is a period-separated concatenation of the subsystem and the counter
131name, for example "mds.inodes".
132
133A list of two-tuples of (timestamp, value) is returned. This may be
134empty if no data is available.
135
136Sending commands
137----------------
138
139A non-blocking facility is provided for sending monitor commands
140to the cluster.
141
142``send_command(self, result, command_str, tag)``
143
144The ``result`` parameter should be an instance of the CommandResult
145class, defined in the same module as MgrModule. This acts as a
146completion and stores the output of the command. Use CommandResult.wait()
147if you want to block on completion.
148
149The ``command_str`` parameter is a JSON-serialized command. This
150uses the same format as the ceph command line, which is a dictionary
151of command arguments, with the extra ``prefix`` key containing the
152command name itself. Consult MonCommands.h for available commands
153and their expected arguments.
154
155The ``tag`` parameter is used for nonblocking operation: when
156a command completes, the ``notify()`` callback on the MgrModule
157instance is triggered, with notify_type set to "command", and
158notify_id set to the tag of the command.
159
160
161Logging
162-------
163
164Use your module's ``log`` attribute as your logger. This is a logger
165configured to output via the ceph logging framework, to the local ceph-mgr
166log files.
167
168Python log severities are mapped to ceph severities as follows:
169
170* DEBUG is 20
171* INFO is 4
172* WARN is 1
173* ERR is 0
174
175Shutting down cleanly
176---------------------
177
178If a module implements the ``serve()`` method, it should also implement
179the ``shutdown()`` method to shutdown cleanly: misbehaving modules
180may otherwise prevent clean shutdown of ceph-mgr.
181
182Is something missing?
183---------------------
184
185The ceph-mgr python interface is not set in stone. If you have a need
186that is not satisfied by the current interface, please bring it up
187on the ceph-devel mailing list. While it is desired to avoid bloating
188the interface, it is not generally very hard to expose existing data
189to the Python code when there is a good reason.
190