Created
March 29, 2016 12:46
-
-
Save chocobn69/eeeb370255180279cb0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
from django.core.management.base import BaseCommand | |
from django.db import connection | |
from django.core import mail | |
import logging | |
logger = logging.getLogger(__name__) | |
class Command(BaseCommand): | |
args = '' | |
help = "Verifie l'etat des tables dans postgresql" | |
def handle(self, *args, **options): | |
""" | |
:param args: | |
:param options: | |
:return: | |
""" | |
cursor = connection.cursor() | |
try: | |
cursor.execute(""" | |
select reltuples, relpages, pg_relation_size(oid),oid::regclass | |
from pg_class | |
where reltuples=0 and relpages > 0 and relkind = 'r' | |
""") | |
problems = cursor.fetchall() | |
if problems: | |
logger.error(problems) | |
try: | |
mail.send_mail(subject='database probleme', | |
message=""" | |
we have some problems : | |
%s""" % problems, | |
from_email='', | |
recipient_list=['YOURMAIL'], | |
fail_silently=False) | |
except Exception as e: | |
logger.exception(e) | |
except Exception as e: | |
raise e | |
finally: | |
cursor.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment