From a37f4c1263e867e049502aeb96a8a207424ae46c Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Thu, 12 Nov 2015 18:59:24 +0000
Subject: LinHES-system: myth_mtc.py & myth_mtc.cron: add new --check_home
 option.

The --check_home option checks how full home is and if it is over 90% it will remove caches in .mythtv and restart mythfrontend.
---
 abs/core/LinHES-system/PKGBUILD      |  6 +++---
 abs/core/LinHES-system/myth_mtc.cron | 15 +++++++++++++-
 abs/core/LinHES-system/myth_mtc.py   | 39 +++++++++++++++++++++++++++++++++---
 3 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index 7469000..e3c1700 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=8.3
-pkgrel=18
+pkgrel=19
 arch=('i686' 'x86_64')
 install=system.install
 pkgdesc="Everything that makes LinHES an automated system"
@@ -81,7 +81,7 @@ md5sums=('76842f8cff548d2c1289e0992a8b84dd'
          'de32a1c50101265dd7f6ca5037f7a26a'
          '2d7b8414ddce5f401331342ac1529c22'
          '76b2637cac0452b3acdbeeb4e8a5474b'
-         'd02ddadeb2181f90954e435842cd7f0a'
+         'cd81693e73556ff09565799516e81554'
          'dc3eef2a624754e16805d72bbe488b67'
          '617af86b901538817ebdcaf646248dc5'
          '542e670e78d117657f93141e9689f54d'
@@ -135,7 +135,7 @@ md5sums=('76842f8cff548d2c1289e0992a8b84dd'
          'eb879fee9603a05d5420d4ce8ed9e450'
          '02c810c2f47b7c4495fdacaf54189473'
          '1aac1872bf9569ea58ceb2f6f766a654'
-         'dc48a53d4e5fe27b218657ccc318d07f'
+         '3a9fb38db8ad0736d85d5f85dbb948d7'
          '4d7164800122243be6513b29c8fc498e'
          'd14fdc38878c70011754317b0f2ea59e'
          '36bdfa8d877cea20ca3870faf08e08d5'
diff --git a/abs/core/LinHES-system/myth_mtc.cron b/abs/core/LinHES-system/myth_mtc.cron
index d3dc50a..ff8e84a 100755
--- a/abs/core/LinHES-system/myth_mtc.cron
+++ b/abs/core/LinHES-system/myth_mtc.cron
@@ -16,6 +16,19 @@ fi
 #check logfile for Finished and if not run myth_mtc.py
 if ! grep -q "Finished Maintenance" $logFile
 then
+
+    if ! grep -q "Finished checking size of MythTV home" $logFile
+    then
+        MYTHCONFDIR=/usr/share/mythtv unbuffer myth_mtc.py --check_home | $log
+        if [ $? = 0 ]
+        then
+            echo "" | $log
+        else
+            echo "Time Exceeded" | $log
+            exit
+        fi
+    fi
+
     if ! grep -q "Finished Optimize" $logFile
     then
         MYTHCONFDIR=/usr/share/mythtv unbuffer myth_mtc.py --optimize | $log
@@ -52,7 +65,7 @@ then
         fi
     fi
 
-    if grep -q "Finished Optimize" $logFile && grep -q "Finished Backup" $logFile && grep -q "Finished Update" $logFile
+    if grep -q "Finished checking size of MythTV home" $logFile && grep -q "Finished Optimize" $logFile && grep -q "Finished Backup" $logFile && grep -q "Finished Update" $logFile
     then
         echo "Finished Maintenance" | $log
     fi
diff --git a/abs/core/LinHES-system/myth_mtc.py b/abs/core/LinHES-system/myth_mtc.py
index 8a08c05..ee4fde2 100755
--- a/abs/core/LinHES-system/myth_mtc.py
+++ b/abs/core/LinHES-system/myth_mtc.py
@@ -17,10 +17,35 @@ except:
 
 def get_timestamp():
     now = datetime.datetime.now()
-#date = "%s-%s-%s" %(now.year, now.month, now.day)
-    date = (now.strftime('%Y-%m-%d %H:%M')) 
+    date = (now.strftime('%Y-%m-%d %H:%M'))
     return date
 
+def getFreePercentForDir(dir):
+    stats = os.statvfs(dir)
+    total = (stats.f_blocks)
+    avail = (stats.f_bavail)
+    return (total - avail) / float(total)
+
+def check_home():
+    #get the mythtv home dir
+    sys.path.append('/usr/MythVantage/bin/')
+    config_file = "mv_config"
+    data_config = __import__(config_file,  globals(),  locals(),  [])
+
+    freePcent = getFreePercentForDir(data_config.MYTHHOME)
+    print "  Home directory percent used: " + str(freePcent * 100) + "%"
+    if float(freePcent) > .9:
+        print "    Home directory is greater than 90% used. Clearing MythTV caches..."
+        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.mythtv/{*cache,Cache-*}"
+        subprocess.call(["sh", "-c", cmd])
+        print "    Restarting mythfrontend..."
+        subprocess.call(["killall", "mythfrontend"])
+        freePcent = getFreePercentForDir(data_config.MYTHHOME)
+        print "  Home directory percent used: " + str(freePcent * 100) + "%"
+    else:
+        print "    Home directory is less than 90% used. Not clearing MythTV caches."
+    return 0
+
 def optimize():
     try:
         cursor = mythtv.db.cursor()
@@ -60,6 +85,14 @@ def bail_if_another_is_running():
 def run_stuff():
     print "\n%s" %get_timestamp()
     if not subprocess.call(["/usr/bin/python2", "/usr/LH/bin/idle.py"]):
+        if ("--check_home" in sys.argv) or (len(sys.argv) == 1):
+            print "\n#######################################"
+            print "\n%s Checking size of MythTV home" %(get_timestamp())
+            if not check_home():
+                print "\nFinished checking size of MythTV home"
+            else:
+                return True
+
         if ("--optimize" in sys.argv) or (len(sys.argv) == 1):
             print "\n#######################################"
             print "\n%s Running Optimize" %(get_timestamp())
@@ -96,7 +129,7 @@ while ctin:
     if ctin:
         print "\n%s Waiting 10 minutes before trying again." %(get_timestamp())
         time.sleep(600)
-        
+
     current_time=time.time()
     if (current_time - starttime) > 3000 :
         ctin = False
-- 
cgit v0.12

