monitoring.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. .. _guide-monitoring:
  2. ==================
  3. Monitoring Guide
  4. ==================
  5. .. contents::
  6. :local:
  7. Introduction
  8. ============
  9. There are several tools available to monitor and inspect Celery clusters.
  10. This document describes some of these, as as well as
  11. features related to monitoring, like events and broadcast commands.
  12. .. _monitoring-workers:
  13. Workers
  14. =======
  15. .. _monitoring-celeryctl:
  16. celeryctl: Management Utility
  17. -----------------------------
  18. .. versionadded:: 2.1
  19. :mod:`~celery.bin.celeryctl` is a command line utility to inspect
  20. and manage worker nodes (and to some degree tasks).
  21. To list all the commands from the command line do::
  22. $ celeryctl help
  23. or to get help for a specific command do::
  24. $ celeryctl <command> --help
  25. Commands
  26. ~~~~~~~~
  27. * **status**: List active nodes in this cluster
  28. ::
  29. $ celeryctl status
  30. * **result**: Show the result of a task
  31. ::
  32. $ celeryctl result -t tasks.add 4e196aa4-0141-4601-8138-7aa33db0f577
  33. Note that you can omit the name of the task as long as the
  34. task doesn't use a custom result backend.
  35. * **inspect active**: List active tasks
  36. ::
  37. $ celeryctl inspect active
  38. These are all the tasks that are currently being executed.
  39. * **inspect scheduled**: List scheduled ETA tasks
  40. ::
  41. $ celeryctl inspect scheduled
  42. These are tasks reserved by the worker because they have the
  43. ``eta`` or ``countdown`` argument set.
  44. * **inspect reserved**: List reserved tasks
  45. ::
  46. $ celeryctl inspect reserved
  47. This will list all tasks that have been prefetched by the worker,
  48. and is currently waiting to be executed (does not include tasks
  49. with an eta).
  50. * **inspect revoked**: List history of revoked tasks
  51. ::
  52. $ celeryctl inspect revoked
  53. * **inspect registered_tasks**: List registered tasks
  54. ::
  55. $ celeryctl inspect registered_tasks
  56. * **inspect states**: Show worker statistics
  57. ::
  58. $ celeryctl inspect stats
  59. * **inspect diagnose**: Diagnose the pool processes.
  60. ::
  61. $ celeryctl inspect diagnose
  62. This will verify that the workers pool processes are available
  63. to do work. Note that this will not work if the worker is busy.
  64. * **inspect enable_events**: Enable events
  65. ::
  66. $ celeryctl inspect enable_events
  67. * **inspect disable_events**: Disable events
  68. ::
  69. $ celeryctl inspect disable_events
  70. .. note::
  71. All ``inspect`` commands supports a ``--timeout`` argument,
  72. This is the number of seconds to wait for responses.
  73. You may have to increase this timeout if you're getting empty responses
  74. due to latency.
  75. .. _celeryctl-inspect-destination:
  76. Specifying destination nodes
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. By default the inspect commands operates on all workers.
  79. You can specify a single, or a list of workers by using the
  80. ``--destination`` argument::
  81. $ celeryctl inspect -d w1,w2 reserved
  82. .. _monitoring-django-admin:
  83. Django Admin Monitor
  84. --------------------
  85. .. versionadded:: 2.1
  86. When you add `django-celery`_ to your Django project you will
  87. automatically get a monitor section as part of the Django admin interface.
  88. This can also be used if you're not using Celery with a Django project.
  89. *Screenshot*
  90. .. image:: http://celeryproject.org/beta/djangoceleryadmin2.jpg
  91. .. _`django-celery`: http://pypi.python.org/pypi/django-celery
  92. .. _monitoring-django-starting:
  93. Starting the monitor
  94. ~~~~~~~~~~~~~~~~~~~~
  95. The Celery section will already be present in your admin interface,
  96. but you won't see any data appearing until you start the snapshot camera.
  97. The camera takes snapshots of the events your workers sends at regular
  98. intervals, storing them in your database (See :ref:`monitoring-snapshots`).
  99. To start the camera run::
  100. $ python manage.py celerycam
  101. If you haven't already enabled the sending of events you need to do so::
  102. $ python manage.py celeryctl inspect enable_events
  103. :Tip: You can enable events when the worker starts using the ``-E`` argument
  104. to :mod:`~celery.bin.celeryd`.
  105. Now that the camera has been started, and events have been enabled
  106. you should be able to see your workers and the tasks in the admin interface
  107. (it may take some time for workers to show up).
  108. .. _monitoring-django-frequency:
  109. Shutter frequency
  110. ~~~~~~~~~~~~~~~~~
  111. By default the camera takes a snapshot every second, if this is too frequent
  112. or you want higher precision then you can change this using the
  113. ``--frequency`` argument. This is a float describing how often, in seconds,
  114. it should wake up to check if there are any new events::
  115. $ python manage.py celerycam --frequency=3.0
  116. The camera also supports rate limiting using the ``--maxrate`` argument.
  117. While the frequency controls how often the camera thread wakes up,
  118. the rate limit controls how often it will actually take a snapshot.
  119. The rate limits can be specified in seconds, minutes or hours
  120. by appending ``/s``, ``/m`` or ``/h`` to the value.
  121. Example: ``--maxrate=100/m``, means "hundred writes a minute".
  122. The rate limit is off by default, which means it will take a snapshot
  123. for every ``--frequency`` seconds.
  124. The events also expire after some time, so the database doesn't fill up.
  125. Successful tasks are deleted after 1 day, failed tasks after 3 days,
  126. and tasks in other states after 5 days.
  127. .. _monitoring-nodjango:
  128. Using outside of Django
  129. ~~~~~~~~~~~~~~~~~~~~~~~
  130. TODO
  131. .. _monitoring-celeryev:
  132. celeryev: Curses Monitor
  133. ------------------------
  134. .. versionadded:: 2.0
  135. :mod:`~celery.bin.celeryev` is a simple curses monitor displaying
  136. task and worker history. You can inspect the result and traceback of tasks,
  137. and it also supports some management commands like rate limiting and shutdown
  138. of workers.
  139. .. image:: http://celeryproject.org/img/celeryevshotsm.jpg
  140. :mod:`~celery.bin.celeryev` is also used to start snapshot cameras (see
  141. :ref:`monitoring-snapshots`::
  142. $ celeryev --camera=<camera-class> --frequency=1.0
  143. and it includes a tool to dump events to stdout::
  144. $ celeryev --dump
  145. For a complete list of options use ``--help``::
  146. $ celeryev --help
  147. .. _monitoring-celerymon:
  148. celerymon: Web monitor
  149. ----------------------
  150. `celerymon`_ is the ongoing work to create a web monitor.
  151. It's far from complete yet, and does currently only support
  152. a JSON API. Help is desperately needed for this project, so if you,
  153. or someone you knowi, would like to contribute templates, design, code
  154. or help this project in any way, please get in touch!
  155. :Tip: The Django admin monitor can be used even though you're not using
  156. Celery with a Django project. See :ref:`monitoring-nodjango`.
  157. .. _`celerymon`: http://github.com/ask/celerymon/
  158. .. _monitoring-rabbitmq:
  159. RabbitMQ
  160. ========
  161. To manage a Celery cluster it is important to know how
  162. RabbitMQ can be monitored.
  163. RabbitMQ ships with the `rabbitmqctl(1)`_ command,
  164. with this you can list queues, exchanges, bindings,
  165. queue lenghts, the memory usage of each queue, as well
  166. as manage users, virtual hosts and their permissions.
  167. .. note::
  168. The default virtual host (``"/"``) is used in these
  169. examples, if you use a custom virtual host you have to add
  170. the ``-p`` argument to the command, e.g:
  171. ``rabbitmqctl list_queues -p my_vhost ....``
  172. .. _`rabbitmqctl(1)`: http://www.rabbitmq.com/man/rabbitmqctl.1.man.html
  173. .. _monitoring-rmq-queues:
  174. Inspecting queues
  175. -----------------
  176. Finding the number of tasks in a queue::
  177. $ rabbitmqctl list_queues name messages messages_ready \
  178. messages_unacknowlged
  179. Here ``messages_ready`` is the number of messages ready
  180. for delivery (sent but not received), ``messages_unacknowledged``
  181. is the number of messages that has been received by a worker but
  182. not acknowledged yet (meaning it is in progress, or has been reserved).
  183. ``messages`` is the sum of ready and unacknowledged messages combined.
  184. Finding the number of workers currently consuming from a queue::
  185. $ rabbitmqctl list_queues name consumers
  186. Finding the amount of memory allocated to a queue::
  187. $ rabbitmqctl list_queues name memory
  188. :Tip: Adding the ``-q`` option to `rabbitmqctl(1)`_ makes the output
  189. easier to parse.
  190. .. _monitoring-munin:
  191. Munin
  192. =====
  193. This is a list of known Munin plugins that can be useful when
  194. maintaining a Celery cluster.
  195. * rabbitmq-munin: Munin-plugins for RabbitMQ.
  196. http://github.com/ask/rabbitmq-munin
  197. * celery_tasks: Monitors the number of times each task type has
  198. been executed (requires ``celerymon``).
  199. http://exchange.munin-monitoring.org/plugins/celery_tasks-2/details
  200. * celery_task_states: Monitors the number of tasks in each state
  201. (requires ``celerymon``).
  202. http://exchange.munin-monitoring.org/plugins/celery_tasks/details
  203. .. _monitoring-events:
  204. Events
  205. ======
  206. The worker has the ability to send a message whenever some event
  207. happens. These events are then captured by tools like ``celerymon`` and
  208. ``celeryev`` to monitor the cluster.
  209. .. _monitoring-snapshots:
  210. Snapshots
  211. ---------
  212. .. versionadded: 2.1
  213. Even a single worker can produce a huge amount of events, so storing
  214. history of events on disk may be very expensive.
  215. A sequence of events describes the cluster state in that time period,
  216. by taking periodic snapshots of this state we can keep all history, but
  217. still only periodically write it to disk.
  218. To take snapshots you need a Camera class, with this you can define
  219. what should happen every time the state is captured. You can
  220. write it to a database, send it by e-mail or something else entirely).
  221. ``celeryev`` is then used to take snapshots with the camera,
  222. for example if you want to capture state every 2 seconds using the
  223. camera ``myapp.Camera`` you run ``celeryev`` with the following arguments::
  224. $ celeryev -c myapp.Camera --frequency=2.0
  225. .. _monitoring-camera:
  226. Custom Camera
  227. ~~~~~~~~~~~~~
  228. Here is an example camera, dumping the snapshot to the screen:
  229. .. code-block:: python
  230. from pprint import pformat
  231. from celery.events.snapshot import Polaroid
  232. class DumpCam(Polaroid):
  233. def shutter(self, state):
  234. if not state.event_count:
  235. # No new events since last snapshot.
  236. return
  237. print("Workers: %s" % (pformat(state.workers, indent=4), ))
  238. print("Tasks: %s" % (pformat(state.tasks, indent=4), ))
  239. print("Total: %s events, %s tasks" % (
  240. state.event_count, state.task_count))
  241. Now you can use this cam with ``celeryev`` by specifying
  242. it with the ``-c`` option::
  243. $ celeryev -c myapp.DumpCam --frequency=2.0
  244. Or you can use it programatically like this::
  245. from celery.events import EventReceiver
  246. from celery.messaging import establish_connection
  247. from celery.events.state import State
  248. from myapp import DumpCam
  249. def main():
  250. state = State()
  251. with establish_connection() as connection:
  252. recv = EventReceiver(connection, handlers={"*": state.event})
  253. with DumpCam(state, freq=1.0):
  254. recv.capture(limit=None, timeout=None)
  255. if __name__ == "__main__":
  256. main()
  257. .. _event-reference:
  258. Event Reference
  259. ---------------
  260. This list contains the events sent by the worker, and their arguments.
  261. .. _event-reference-task:
  262. Task Events
  263. ~~~~~~~~~~~
  264. * ``task-received(uuid, name, args, kwargs, retries, eta, hostname,
  265. timestamp)``
  266. Sent when the worker receives a task.
  267. * ``task-started(uuid, hostname, timestamp)``
  268. Sent just before the worker executes the task.
  269. * ``task-succeeded(uuid, result, runtime, hostname, timestamp)``
  270. Sent if the task executed successfully.
  271. Runtime is the time it took to execute the task using the pool.
  272. (Time starting from the task is sent to the pool, and ending when the
  273. pool result handlers callback is called).
  274. * ``task-failed(uuid, exception, traceback, hostname, timestamp)``
  275. Sent if the execution of the task failed.
  276. * ``task-revoked(uuid)``
  277. Sent if the task has been revoked (Note that this is likely
  278. to be sent by more than one worker)
  279. * ``task-retried(uuid, exception, traceback, hostname, delay, timestamp)``
  280. Sent if the task failed, but will be retried in the future.
  281. (**NOT IMPLEMENTED**)
  282. .. _event-reference-worker:
  283. Worker Events
  284. ~~~~~~~~~~~~~
  285. * ``worker-online(hostname, timestamp)``
  286. The worker has connected to the broker and is online.
  287. * ``worker-heartbeat(hostname, timestamp)``
  288. Sent every minute, if the worker has not sent a heartbeat in 2 minutes,
  289. it is considered to be offline.
  290. * ``worker-offline(hostname, timestamp)``
  291. The worker has disconnected from the broker.