pythontr.com
#!/usr/bin/python #-*- coding:utf-8 -*- #------------------------------------------------------------------------------ # Hüseyin ÖZDEMİR # 28.08.2011 # husonet # Yük durumunu kontrol eder yük durumu 20 seviyesine geldiği yada geçtiği zaman # Apache ve Mysql servislerini restart eder. # Debian 6.0 Squueze de test edilmiştir. #------------------------------------------------------------------------------ import os from subprocess import call import subprocess from datetime import datetime LOAD = 20 KOMUT = "top -b -n 1 | head -n 20 | grep average | awk '{print $12}' |" KOMUT += " cut -d, -f1 | cut -d. -f1" LOG_DOSYA = "/var/log/loadinfo.load" APACHE_SERVIS = "/etc/init.d/apache2 restart" MYSQL_SERVIS = "/etc/init.d/mysql restart" # Debug yapilacak mi? Test ortami icin 1, gercek calisma ortami icin 0 DEBUG = 0 #------------------------------------------------------------------------------ # log dosyası def log_dosya(sVal): try: dosya = open(LOG_DOSYA , 'wb') dosya.write(sVal) except Exception, err: if DEBUG: raise else: print(str(err)) finally: try: dosya.close() except: pass #------------------------------------------------------------------------------ # yük durumu kontrol et uygun değilse apache servisini ve mysql servisini· # yeniden başlat def yuk_kontrol(): try: pid = subprocess.Popen(KOMUT, shell=True, stdin=subprocess.PIPE,· stdout=subprocess.PIPE, stderr=subprocess.PIPE,) pid.wait() load_info = pid.communicate()[0].strip() if int(load_info) >= LOAD: tarih = datetime.now() paid = subprocess.Popen(APACHE_SERVIS, shell=True) paid = paid.wait() #pmid = subprocess.Popen(MYSQL_SERVIS, shell=True) #pmid = pmid.wait() log_dosya('Sistemin son çalışma tarihi:' +· tarih.strftime("%d-%m-%Y %H:%M:%S")) except Exception, err: if DEBUG: raise else: print(str(err)) if __name__ == "__main__": try: yuk_kontrol() except Exception, err: print(str(err))
Yorumlar