Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created March 2, 2011 12:36
Show Gist options
  • Save revolunet/850858 to your computer and use it in GitHub Desktop.
Save revolunet/850858 to your computer and use it in GitHub Desktop.
graphs your django model instances with munin
#!/usr/bin/python
#
# Author : Julien Bouquillon (julien@bouquillon.com) - revolunet team
#
# this munin plugin generate graphs with evolution of all registered django models
# install : copy, chmod +x then ln -s in /etc/munin/plugins
#
# project root
import sys
BASE_DIR = '/home/django/quickprez_vhosts/beta/djangoapp'
sys.path.insert(0, BASE_DIR)
import settings
sys.path.insert(0, settings.DJANGO_SOURCE)
# setup environ
from django.core.management import setup_environ
setup_environ(settings)
def iterateModels():
from django.db.models.loading import cache
for app in cache.get_apps():
app_label = app.__name__.split('.')[-2]
for model in cache.get_models( app ):
#print app_label, model.__name__
yield app, model
def run():
apps = {}
for app, model in iterateModels():
app_label = app.__name__.split('.')[-2]
if not apps.has_key( app_label ):
apps[app_label] = {}
apps[app_label][model.__name__ ] = 0
nb = model.objects.count()
print "%s_%s.value %s" % (app_label, model.__name__ , nb)
run()
if len(sys.argv)==2 and sys.argv[1] == 'config':
import os
bname = os.path.basename(sys.argv[0])
print """graph_title Django models %s
graph_vlabel count
graph_category Django
graph_info Stats Django models %s""" % (bname, bname)
for app, model in iterateModels():
app_label = app.__name__.split('.')[-2]
print "%s_%s.label %s_%s" % (app_label, model.__name__, app_label, model.__name__)
print "%s_%s.info %s_%s count" % (app_label, model.__name__, app_label, model.__name__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment