Skip to content

Instantly share code, notes, and snippets.

@larsbutler
Created April 4, 2013 11:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save larsbutler/5309600 to your computer and use it in GitHub Desktop.
Save larsbutler/5309600 to your computer and use it in GitHub Desktop.
Script for monitoring celery worker usage, for https://github.com/gem/oq-engine
#!/usr/bin/env python
import sys
from celery.task.control import inspect
def ping_celery():
ins = inspect()
# Count the number of total worker processes
total_workers = 0
# ... and active tasks
num_active_tasks = 0
all_stats = ins.stats()
if all_stats is None:
print "No active workers"
sys.exit(0)
hostnames = []
for hostname, stats in all_stats.items():
num_procs = len(stats['pool']['processes'])
total_workers += num_procs
hostnames.append(hostname)
# Now ping the workers
# and show individual stats:
ping = ins.ping()
active = ins.active()
for host in hostnames:
print '=========='
print 'Host: %s' % host
if ping[host] == 'pong':
print 'Status: Online'
else:
print 'Status: Not Responding'
print 'Worker processes: %s' % len(
all_stats[host]['pool']['processes'])
worker_activity = active.get(host)
if worker_activity is not None:
print 'Active tasks: %s' % len(worker_activity)
num_active_tasks += len(worker_activity)
print '=========='
print
print 'Total workers: %s' % total_workers
print 'Active tasks: %s' % num_active_tasks
print 'Cluster utilization: %.2f%%' % (
(float(num_active_tasks) / total_workers) * 100
)
if __name__ == "__main__":
ping_celery()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment