Projet

Général

Profil

Révision 0ed36d00

ID0ed36d00e1253c3ae24919ea415657b7200947d4
Parent 6f28e159
Enfant 2b6d4bc4

Ajouté par Toma? Muraus il y a presque 14 ans

Initial version

Voir les différences:

plugins/other/celery_tasks-2
1
#!/usr/bin/env python
2

  
3
"""=cut
4
=head1 NAME
5

  
6
celery_tasks - Munin plugin to monitor the number of Celery tasks with specified names.
7

  
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
None
21

  
22
You must set the name of at least one task you want to monitor (multiple names are separated by a comma).
23

  
24
For example:
25

  
26
  [celery_tasks]
27
	 env.tasks myapp.tasks.SendEmailTask,myapp2.tasks.FetchUserDataTask
28

  
29
This would monitor the number of task for a task with name "myapp.tasks.SendEmailTask" and "myapp2.tasks.FetchUserDataTask".
30

  
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
			'task-accepted',
65
			'task-received',
66
			'task-succeeded',
67
			'task-failed',
68
			'task-retried',
69
)
70

  
71
def get_data(what, api_url, *args):
72
	try:
73
		request = urllib.urlopen('%s%s' % (api_url, \
74
										   URL_ENDPOINTS[what] % (args)))
75
		response = request.read()
76
		return json.loads(response)
77
	except IOError:
78
		print 'Could not connect to the celerymon webserver'
79
		sys.exit(-1)
80
		
81
def check_web_server_status(api_url):
82
	try:
83
		request = urllib.urlopen(api_url)
84
		response = request.read()
85
	except IOError:
86
		print 'Could not connect to the celerymon webserver'
87
		sys.exit(-1)
88

  
89
def clean_task_name(task_name):
90
	return task_name.replace('.', '_')
91

  
92
# Config
93
def print_config(task_names):
94
	print 'graph_title Celery tasks'
95
	print 'graph_args --lower-limit 0'
96
	print 'graph_scale no'
97
	print 'graph_vlabel tasks per ${graph_period}'
98
	print 'graph_category celery'
99

  
100
	for name in task_names:
101
		print '%s.label %s' % (clean_task_name(name), name)
102
		print '%s.type DERIVE' % (clean_task_name(name))
103
		print '%s.min 0' % (clean_task_name(name))
104
		print '%s.info number of %s tasks' % (clean_task_name(name), name)
105
		
106
# Values
107
def print_values(task_names = None, api_url = None):
108
	for task_name in task_names:
109
		count = len(get_data('task_details', api_url, task_name))
110
		print '%s.value %d' % (clean_task_name(task_name), count)
111
		
112
if __name__ == '__main__':
113
	task_names = os.environ.get('tasks', None)
114
	api_url = os.environ.get('api_url', API_URL)
115
	
116
	check_web_server_status(api_url)
117
	
118
	if not task_names:
119
		print 'You need to define at least one task name'
120
		sys.exit(-1)
121
		
122
	task_names = task_names.split(',')
123
			
124
	if len(sys.argv) > 1:
125
		if sys.argv[1] == 'config':
126
			print_config(task_names)
127
		elif sys.argv[1] == 'autoconf':
128
			print 'yes'
129
	else:
130
		print_values(task_names, api_url)
131

  

Formats disponibles : Unified diff