2010-06-13 21:45:08 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""=cut
|
|
|
|
=head1 NAME
|
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
celery_tasks_states - Munin plugin to monitor the number of Celery tasks in each state.
|
2010-06-13 21:45:08 +02:00
|
|
|
|
|
|
|
=head1 REQUIREMENTS
|
|
|
|
|
|
|
|
- Python
|
|
|
|
- celery (http://celeryproject.org/)
|
|
|
|
- celerymon (http://github.com/ask/celerymon)
|
|
|
|
|
|
|
|
Note: don't forget to enable sending of the events on the celery daemon - run it with the --events option
|
|
|
|
|
|
|
|
=head1 CONFIGURATION
|
|
|
|
|
|
|
|
Default configuration:
|
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
[celery_tasks_states]
|
|
|
|
env.api_url http://localhost:8989
|
|
|
|
env.workers all
|
|
|
|
|
|
|
|
If workers variable is not set or set to "all", task number for all the workers is monitored.
|
2010-06-13 21:45:08 +02:00
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
You can optionally set the workers variable to the string of hostnames you want to monitor separated by a comma.
|
2010-06-13 21:45:08 +02:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
[celery_tasks]
|
2012-07-18 00:30:54 +02:00
|
|
|
env.workers localhost,foo.bar.net,bar.foo.net
|
2010-06-13 21:45:08 +02:00
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
This would only monitor the number of tasks for the workers with the hostnames "localhost", "foo.bar.net" and "bar.foo.net"
|
2010-06-13 21:45:08 +02:00
|
|
|
|
|
|
|
=head1 MAGIC MARKERS
|
|
|
|
|
|
|
|
#%# family=manual
|
|
|
|
#%# capabilities=autoconf
|
|
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
|
|
|
|
Tomaz Muraus (http://github.com/Kami/munin-celery)
|
|
|
|
|
|
|
|
=head1 LICENSE
|
|
|
|
|
|
|
|
GPLv2
|
|
|
|
|
|
|
|
=cut"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except:
|
|
|
|
import simplejson as json
|
|
|
|
|
|
|
|
API_URL = 'http://localhost:8989'
|
|
|
|
URL_ENDPOINTS = {
|
|
|
|
'workers': '/api/worker/',
|
|
|
|
'worker_tasks': '/api/worker/%s/tasks',
|
|
|
|
'tasks': '/api/task/',
|
|
|
|
'task_names': '/api/task/name/',
|
|
|
|
'task_details': '/api/task/name/%s',
|
|
|
|
}
|
|
|
|
TASK_STATES = (
|
2012-07-17 18:19:22 +02:00
|
|
|
'PENDING',
|
|
|
|
'RECEIVED',
|
|
|
|
'STARTED',
|
|
|
|
'SUCCESS',
|
|
|
|
'FAILURE',
|
|
|
|
'REVOKED',
|
|
|
|
'RETRY'
|
2010-06-13 21:45:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_data(what, api_url, *args):
|
|
|
|
try:
|
|
|
|
request = urllib.urlopen('%s%s' % (api_url, \
|
|
|
|
URL_ENDPOINTS[what] % (args)))
|
|
|
|
response = request.read()
|
|
|
|
return json.loads(response)
|
|
|
|
except IOError:
|
|
|
|
print 'Could not connect to the celerymon webserver'
|
|
|
|
sys.exit(-1)
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2010-06-13 21:45:08 +02:00
|
|
|
def check_web_server_status(api_url):
|
|
|
|
try:
|
|
|
|
request = urllib.urlopen(api_url)
|
|
|
|
response = request.read()
|
|
|
|
except IOError:
|
|
|
|
print 'Could not connect to the celerymon webserver'
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
def clean_state_name(state_name):
|
|
|
|
return state_name.lower()
|
2010-06-13 21:45:08 +02:00
|
|
|
|
|
|
|
# Config
|
2012-07-18 00:30:54 +02:00
|
|
|
def print_config(workers = None):
|
|
|
|
if workers:
|
|
|
|
print 'graph_title Celery tasks in each state [workers = %s]' % (', ' . join(workers))
|
|
|
|
else:
|
|
|
|
print 'graph_title Celery tasks in each state'
|
2010-06-13 21:45:08 +02:00
|
|
|
print 'graph_args --lower-limit 0'
|
|
|
|
print 'graph_scale no'
|
|
|
|
print 'graph_vlabel tasks per ${graph_period}'
|
2018-03-28 04:30:14 +02:00
|
|
|
print 'graph_category cloud'
|
2010-06-13 21:45:08 +02:00
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
for name in TASK_STATES:
|
|
|
|
name = clean_state_name(name)
|
|
|
|
print '%s.label %s' % (name, name)
|
|
|
|
print '%s.type DERIVE' % (name)
|
|
|
|
print '%s.min 0' % (name)
|
|
|
|
print '%s.info number of %s tasks' % (name, name)
|
|
|
|
|
2010-06-13 21:45:08 +02:00
|
|
|
# Values
|
2012-07-18 00:30:54 +02:00
|
|
|
def print_values(workers = None, api_url = None):
|
|
|
|
data = get_data('tasks', api_url)
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
counters = dict([(key, 0) for key in TASK_STATES])
|
|
|
|
for task_name, task_data in data:
|
|
|
|
state = task_data['state']
|
|
|
|
hostname = task_data['worker']['hostname']
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
if workers and hostname not in workers:
|
|
|
|
continue
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
counters[state] += 1
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
for name in TASK_STATES:
|
|
|
|
name_cleaned = clean_state_name(name)
|
|
|
|
value = counters[name]
|
|
|
|
print '%s.value %d' % (name_cleaned, value)
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2010-06-13 21:45:08 +02:00
|
|
|
if __name__ == '__main__':
|
2012-07-18 00:30:54 +02:00
|
|
|
workers = os.environ.get('workers', 'all')
|
2010-06-13 21:45:08 +02:00
|
|
|
api_url = os.environ.get('api_url', API_URL)
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2010-06-13 21:45:08 +02:00
|
|
|
check_web_server_status(api_url)
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2012-07-18 00:30:54 +02:00
|
|
|
if workers in [None, '', 'all']:
|
|
|
|
workers = None
|
|
|
|
else:
|
|
|
|
workers = workers.split(',')
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2010-06-13 21:45:08 +02:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
if sys.argv[1] == 'config':
|
2012-07-18 00:30:54 +02:00
|
|
|
print_config(workers)
|
2010-06-13 21:45:08 +02:00
|
|
|
elif sys.argv[1] == 'autoconf':
|
|
|
|
print 'yes'
|
|
|
|
else:
|
2012-07-18 00:30:54 +02:00
|
|
|
print_values(workers, api_url)
|
2018-08-02 02:03:42 +02:00
|
|
|
|