root / plugins / celery / celery_tasks @ 17f78427
Historique | Voir | Annoter | Télécharger (2,94 ko)
| 1 | cc0e3027 | Toma? Muraus | #!/usr/bin/env python |
|---|---|---|---|
| 2 | b7d577ce | Alisson Patricio | |
| 3 | cc0e3027 | Toma? Muraus | """=cut |
| 4 | =head1 NAME |
||
| 5 | |||
| 6 | b7d577ce | Alisson Patricio | celery_tasks - Munin plugin to monitor the number of Celery tasks with specified names. |
| 7 | cc0e3027 | Toma? Muraus | |
| 8 | =head1 REQUIREMENTS |
||
| 9 | |||
| 10 | - Python |
||
| 11 | - celery (http://celeryproject.org/) |
||
| 12 | - celerymon (http://github.com/ask/celerymon) |
||
| 13 | |||
| 14 | Note: don't forget to enable sending of the events on the celery daemon - run it with the --events option |
||
| 15 | |||
| 16 | =head1 CONFIGURATION |
||
| 17 | |||
| 18 | Default configuration: |
||
| 19 | |||
| 20 | b7d577ce | Alisson Patricio | None |
| 21 | cc0e3027 | Toma? Muraus | |
| 22 | b7d577ce | Alisson Patricio | You must set the name of at least one task you want to monitor (multiple names are separated by a comma). |
| 23 | cc0e3027 | Toma? Muraus | |
| 24 | For example: |
||
| 25 | |||
| 26 | [celery_tasks] |
||
| 27 | b7d577ce | Alisson Patricio | env.tasks myapp.tasks.SendEmailTask,myapp2.tasks.FetchUserDataTask |
| 28 | cc0e3027 | Toma? Muraus | |
| 29 | b7d577ce | Alisson Patricio | This would monitor the number of task for a task with name "myapp.tasks.SendEmailTask" and "myapp2.tasks.FetchUserDataTask". |
| 30 | cc0e3027 | Toma? Muraus | |
| 31 | =head1 MAGIC MARKERS |
||
| 32 | |||
| 33 | #%# family=manual |
||
| 34 | #%# capabilities=autoconf |
||
| 35 | |||
| 36 | =head1 AUTHOR |
||
| 37 | |||
| 38 | Tomaz Muraus (http://github.com/Kami/munin-celery) |
||
| 39 | |||
| 40 | =head1 LICENSE |
||
| 41 | |||
| 42 | GPLv2 |
||
| 43 | |||
| 44 | =cut""" |
||
| 45 | |||
| 46 | import os |
||
| 47 | import sys |
||
| 48 | import urllib |
||
| 49 | |||
| 50 | try: |
||
| 51 | import json |
||
| 52 | except: |
||
| 53 | import simplejson as json |
||
| 54 | |||
| 55 | API_URL = 'http://localhost:8989' |
||
| 56 | URL_ENDPOINTS = {
|
||
| 57 | 'workers': '/api/worker/', |
||
| 58 | 'worker_tasks': '/api/worker/%s/tasks', |
||
| 59 | 'tasks': '/api/task/', |
||
| 60 | 'task_names': '/api/task/name/', |
||
| 61 | 'task_details': '/api/task/name/%s', |
||
| 62 | } |
||
| 63 | TASK_STATES = ( |
||
| 64 | 3ec624e8 | Alisson Patricio | 'PENDING', |
| 65 | 'RECEIVED', |
||
| 66 | 'STARTED', |
||
| 67 | 'SUCCESS', |
||
| 68 | 'FAILURE', |
||
| 69 | 'REVOKED', |
||
| 70 | 'RETRY' |
||
| 71 | cc0e3027 | Toma? Muraus | ) |
| 72 | |||
| 73 | def get_data(what, api_url, *args): |
||
| 74 | try: |
||
| 75 | request = urllib.urlopen('%s%s' % (api_url, \
|
||
| 76 | URL_ENDPOINTS[what] % (args))) |
||
| 77 | response = request.read() |
||
| 78 | return json.loads(response) |
||
| 79 | except IOError: |
||
| 80 | print 'Could not connect to the celerymon webserver' |
||
| 81 | sys.exit(-1) |
||
| 82 | 17f78427 | Lars Kruse | |
| 83 | cc0e3027 | Toma? Muraus | def check_web_server_status(api_url): |
| 84 | try: |
||
| 85 | request = urllib.urlopen(api_url) |
||
| 86 | response = request.read() |
||
| 87 | except IOError: |
||
| 88 | print 'Could not connect to the celerymon webserver' |
||
| 89 | sys.exit(-1) |
||
| 90 | |||
| 91 | b7d577ce | Alisson Patricio | def clean_task_name(task_name): |
| 92 | return task_name.replace('.', '_')
|
||
| 93 | cc0e3027 | Toma? Muraus | |
| 94 | # Config |
||
| 95 | b7d577ce | Alisson Patricio | def print_config(task_names): |
| 96 | print 'graph_title Celery tasks' |
||
| 97 | cc0e3027 | Toma? Muraus | print 'graph_args --lower-limit 0' |
| 98 | print 'graph_scale no' |
||
| 99 | print 'graph_vlabel tasks per ${graph_period}'
|
||
| 100 | 9995f46c | Lars Kruse | print 'graph_category cloud' |
| 101 | cc0e3027 | Toma? Muraus | |
| 102 | b7d577ce | Alisson Patricio | for name in task_names: |
| 103 | print '%s.label %s' % (clean_task_name(name), name) |
||
| 104 | print '%s.type DERIVE' % (clean_task_name(name)) |
||
| 105 | print '%s.min 0' % (clean_task_name(name)) |
||
| 106 | print '%s.info number of %s tasks' % (clean_task_name(name), name) |
||
| 107 | 17f78427 | Lars Kruse | |
| 108 | b7d577ce | Alisson Patricio | # Values |
| 109 | def print_values(task_names = None, api_url = None): |
||
| 110 | for task_name in task_names: |
||
| 111 | count = len(get_data('task_details', api_url, task_name))
|
||
| 112 | print '%s.value %d' % (clean_task_name(task_name), count) |
||
| 113 | 17f78427 | Lars Kruse | |
| 114 | cc0e3027 | Toma? Muraus | if __name__ == '__main__': |
| 115 | b7d577ce | Alisson Patricio | task_names = os.environ.get('tasks', None)
|
| 116 | cc0e3027 | Toma? Muraus | api_url = os.environ.get('api_url', API_URL)
|
| 117 | 17f78427 | Lars Kruse | |
| 118 | cc0e3027 | Toma? Muraus | check_web_server_status(api_url) |
| 119 | 17f78427 | Lars Kruse | |
| 120 | b7d577ce | Alisson Patricio | if not task_names: |
| 121 | print 'You need to define at least one task name' |
||
| 122 | sys.exit(-1) |
||
| 123 | 17f78427 | Lars Kruse | |
| 124 | b7d577ce | Alisson Patricio | task_names = task_names.split(',')
|
| 125 | 17f78427 | Lars Kruse | |
| 126 | cc0e3027 | Toma? Muraus | if len(sys.argv) > 1: |
| 127 | if sys.argv[1] == 'config': |
||
| 128 | b7d577ce | Alisson Patricio | print_config(task_names) |
| 129 | cc0e3027 | Toma? Muraus | elif sys.argv[1] == 'autoconf': |
| 130 | print 'yes' |
||
| 131 | else: |
||
| 132 | b7d577ce | Alisson Patricio | print_values(task_names, api_url) |
