monitoring.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. The admin interface shows tasks, worker nodes, and even
  109. lets you perform some actions, like revoking and rate limiting tasks,
  110. and shutting down worker nodes.
  111. .. _monitoring-django-frequency:
  112. Shutter frequency
  113. ~~~~~~~~~~~~~~~~~
  114. By default the camera takes a snapshot every second, if this is too frequent
  115. or you want higher precision then you can change this using the
  116. ``--frequency`` argument. This is a float describing how often, in seconds,
  117. it should wake up to check if there are any new events::
  118. $ python manage.py celerycam --frequency=3.0
  119. The camera also supports rate limiting using the ``--maxrate`` argument.
  120. While the frequency controls how often the camera thread wakes up,
  121. the rate limit controls how often it will actually take a snapshot.
  122. The rate limits can be specified in seconds, minutes or hours
  123. by appending ``/s``, ``/m`` or ``/h`` to the value.
  124. Example: ``--maxrate=100/m``, means "hundred writes a minute".
  125. The rate limit is off by default, which means it will take a snapshot
  126. for every ``--frequency`` seconds.
  127. The events also expire after some time, so the database doesn't fill up.
  128. Successful tasks are deleted after 1 day, failed tasks after 3 days,
  129. and tasks in other states after 5 days.
  130. .. _monitoring-nodjango:
  131. Using outside of Django
  132. ~~~~~~~~~~~~~~~~~~~~~~~
  133. ``django-celery`` also installs the :program:`djcelerymon` program. This
  134. can be used by non-Django users, and runs both a webserver and a snapshot
  135. camera in the same process.
  136. **Installing**
  137. Using :program:`pip`::
  138. $ pip install -U django-celery
  139. or using :program:`easy_install`::
  140. $ easy_install -U django-celery
  141. **Running**
  142. :program:`djcelerymon` reads configuration from your Celery config module,
  143. and sets up the Django environment using the same settings::
  144. $ djcelerymon
  145. Database tables will be created the first time the monitor is run.
  146. By default an ``sqlite3`` database file named
  147. :file:`djcelerymon.db` is used, so make sure this file is writeable by the
  148. user running the monitor.
  149. If you want to store the events in a different database, e.g. MySQL,
  150. then you can configure the ``DATABASE*`` settings in your Celery
  151. config module. See http://docs.djangoproject.com/en/dev/ref/settings/#databases.
  152. You will also be asked to create a superuser (and you need to create one
  153. to be able to log into the admin later)::
  154. Creating table auth_permission
  155. Creating table auth_group_permissions
  156. [...]
  157. You just installed Django's auth system, which means you don't
  158. have any superusers defined. Would you like to create
  159. one now? (yes/no): yes
  160. Username (Leave blank to use 'username'): username
  161. E-mail address: me@example.com
  162. Password: ******
  163. Password (again): ******
  164. Superuser created successfully.
  165. [...]
  166. Django version 1.2.1, using settings 'celeryconfig'
  167. Development server is running at http://127.0.0.1:8000/
  168. Quit the server with CONTROL-C.
  169. Now that the service is started you can visit the monitor
  170. at http://127.0.0.1:8000, and log in using the user you created.
  171. For a list of the command line options supported by :program:`djcelerymon`,
  172. please see ``djcelerymon --help``.
  173. .. _monitoring-celeryev:
  174. celeryev: Curses Monitor
  175. ------------------------
  176. .. versionadded:: 2.0
  177. :mod:`~celery.bin.celeryev` is a simple curses monitor displaying
  178. task and worker history. You can inspect the result and traceback of tasks,
  179. and it also supports some management commands like rate limiting and shutdown
  180. of workers.
  181. .. image:: http://celeryproject.org/img/celeryevshotsm.jpg
  182. :mod:`~celery.bin.celeryev` is also used to start snapshot cameras (see
  183. :ref:`monitoring-snapshots`::
  184. $ celeryev --camera=<camera-class> --frequency=1.0
  185. and it includes a tool to dump events to stdout::
  186. $ celeryev --dump
  187. For a complete list of options use ``--help``::
  188. $ celeryev --help
  189. .. _monitoring-celerymon:
  190. celerymon: Web monitor
  191. ----------------------
  192. `celerymon`_ is the ongoing work to create a web monitor.
  193. It's far from complete yet, and does currently only support
  194. a JSON API. Help is desperately needed for this project, so if you,
  195. or someone you knowi, would like to contribute templates, design, code
  196. or help this project in any way, please get in touch!
  197. :Tip: The Django admin monitor can be used even though you're not using
  198. Celery with a Django project. See :ref:`monitoring-nodjango`.
  199. .. _`celerymon`: http://github.com/ask/celerymon/
  200. .. _monitoring-rabbitmq:
  201. RabbitMQ
  202. ========
  203. To manage a Celery cluster it is important to know how
  204. RabbitMQ can be monitored.
  205. RabbitMQ ships with the `rabbitmqctl(1)`_ command,
  206. with this you can list queues, exchanges, bindings,
  207. queue lenghts, the memory usage of each queue, as well
  208. as manage users, virtual hosts and their permissions.
  209. .. note::
  210. The default virtual host (``"/"``) is used in these
  211. examples, if you use a custom virtual host you have to add
  212. the ``-p`` argument to the command, e.g:
  213. ``rabbitmqctl list_queues -p my_vhost ....``
  214. .. _`rabbitmqctl(1)`: http://www.rabbitmq.com/man/rabbitmqctl.1.man.html
  215. .. _monitoring-rmq-queues:
  216. Inspecting queues
  217. -----------------
  218. Finding the number of tasks in a queue::
  219. $ rabbitmqctl list_queues name messages messages_ready \
  220. messages_unacknowlged
  221. Here ``messages_ready`` is the number of messages ready
  222. for delivery (sent but not received), ``messages_unacknowledged``
  223. is the number of messages that has been received by a worker but
  224. not acknowledged yet (meaning it is in progress, or has been reserved).
  225. ``messages`` is the sum of ready and unacknowledged messages combined.
  226. Finding the number of workers currently consuming from a queue::
  227. $ rabbitmqctl list_queues name consumers
  228. Finding the amount of memory allocated to a queue::
  229. $ rabbitmqctl list_queues name memory
  230. :Tip: Adding the ``-q`` option to `rabbitmqctl(1)`_ makes the output
  231. easier to parse.
  232. .. _monitoring-munin:
  233. Munin
  234. =====
  235. This is a list of known Munin plugins that can be useful when
  236. maintaining a Celery cluster.
  237. * rabbitmq-munin: Munin-plugins for RabbitMQ.
  238. http://github.com/ask/rabbitmq-munin
  239. * celery_tasks: Monitors the number of times each task type has
  240. been executed (requires ``celerymon``).
  241. http://exchange.munin-monitoring.org/plugins/celery_tasks-2/details
  242. * celery_task_states: Monitors the number of tasks in each state
  243. (requires ``celerymon``).
  244. http://exchange.munin-monitoring.org/plugins/celery_tasks/details
  245. .. _monitoring-events:
  246. Events
  247. ======
  248. The worker has the ability to send a message whenever some event
  249. happens. These events are then captured by tools like ``celerymon`` and
  250. ``celeryev`` to monitor the cluster.
  251. .. _monitoring-snapshots:
  252. Snapshots
  253. ---------
  254. .. versionadded: 2.1
  255. Even a single worker can produce a huge amount of events, so storing
  256. history of events on disk may be very expensive.
  257. A sequence of events describes the cluster state in that time period,
  258. by taking periodic snapshots of this state we can keep all history, but
  259. still only periodically write it to disk.
  260. To take snapshots you need a Camera class, with this you can define
  261. what should happen every time the state is captured. You can
  262. write it to a database, send it by e-mail or something else entirely).
  263. ``celeryev`` is then used to take snapshots with the camera,
  264. for example if you want to capture state every 2 seconds using the
  265. camera ``myapp.Camera`` you run ``celeryev`` with the following arguments::
  266. $ celeryev -c myapp.Camera --frequency=2.0
  267. .. _monitoring-camera:
  268. Custom Camera
  269. ~~~~~~~~~~~~~
  270. Here is an example camera, dumping the snapshot to the screen:
  271. .. code-block:: python
  272. from pprint import pformat
  273. from celery.events.snapshot import Polaroid
  274. class DumpCam(Polaroid):
  275. def shutter(self, state):
  276. if not state.event_count:
  277. # No new events since last snapshot.
  278. return
  279. print("Workers: %s" % (pformat(state.workers, indent=4), ))
  280. print("Tasks: %s" % (pformat(state.tasks, indent=4), ))
  281. print("Total: %s events, %s tasks" % (
  282. state.event_count, state.task_count))
  283. Now you can use this cam with ``celeryev`` by specifying
  284. it with the ``-c`` option::
  285. $ celeryev -c myapp.DumpCam --frequency=2.0
  286. Or you can use it programatically like this::
  287. from celery.events import EventReceiver
  288. from celery.messaging import establish_connection
  289. from celery.events.state import State
  290. from myapp import DumpCam
  291. def main():
  292. state = State()
  293. with establish_connection() as connection:
  294. recv = EventReceiver(connection, handlers={"*": state.event})
  295. with DumpCam(state, freq=1.0):
  296. recv.capture(limit=None, timeout=None)
  297. if __name__ == "__main__":
  298. main()
  299. .. _event-reference:
  300. Event Reference
  301. ---------------
  302. This list contains the events sent by the worker, and their arguments.
  303. .. _event-reference-task:
  304. Task Events
  305. ~~~~~~~~~~~
  306. * ``task-received(uuid, name, args, kwargs, retries, eta, hostname,
  307. timestamp)``
  308. Sent when the worker receives a task.
  309. * ``task-started(uuid, hostname, timestamp)``
  310. Sent just before the worker executes the task.
  311. * ``task-succeeded(uuid, result, runtime, hostname, timestamp)``
  312. Sent if the task executed successfully.
  313. Runtime is the time it took to execute the task using the pool.
  314. (Time starting from the task is sent to the pool, and ending when the
  315. pool result handlers callback is called).
  316. * ``task-failed(uuid, exception, traceback, hostname, timestamp)``
  317. Sent if the execution of the task failed.
  318. * ``task-revoked(uuid)``
  319. Sent if the task has been revoked (Note that this is likely
  320. to be sent by more than one worker)
  321. * ``task-retried(uuid, exception, traceback, hostname, delay, timestamp)``
  322. Sent if the task failed, but will be retried in the future.
  323. (**NOT IMPLEMENTED**)
  324. .. _event-reference-worker:
  325. Worker Events
  326. ~~~~~~~~~~~~~
  327. * ``worker-online(hostname, timestamp)``
  328. The worker has connected to the broker and is online.
  329. * ``worker-heartbeat(hostname, timestamp)``
  330. Sent every minute, if the worker has not sent a heartbeat in 2 minutes,
  331. it is considered to be offline.
  332. * ``worker-offline(hostname, timestamp)``
  333. The worker has disconnected from the broker.