From 3981e3bd46861efbe9cebe4a0cb106faa9033daa Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Thu, 9 Oct 2008 20:28:50 -0700
Subject: Scripts to make LinHES all that it will be. sudo rules are installed
 via LinHES.install.  Currently this allows mythtv to do anything. This will
 need to change in the future, but for now its ok. -myth_mtc.py is a script
 that will check to make sure myth is idle before running optimize_mythdb.py
 along with performing a nightly backup. Currently the backup is not
 functional, as it's waiting on the knoppmyth scripts.

LinHES-start and LinHES-session start X and control whats run.  Instead of using a .xinitrc for mythtv LinHES-start is systemwide.

LinHES-profile.sh set's up the PATH for LinHES tools & scripts.
---
 abs/core-testing/LinHES-system/LinHES-profile.sh  |   3 +
 abs/core-testing/LinHES-system/LinHES-session     |  13 ++
 abs/core-testing/LinHES-system/LinHES-start       | 162 ++++++++++++++++++++++
 abs/core-testing/LinHES-system/LinHES.install     |  90 ++++++++++++
 abs/core-testing/LinHES-system/myth_mtc.py        | 139 +++++++++++++++++++
 abs/core-testing/LinHES-system/myth_mtc.sh        |  12 ++
 abs/core-testing/LinHES-system/optimize_mythdb.py |  16 +++
 7 files changed, 435 insertions(+)
 create mode 100644 abs/core-testing/LinHES-system/LinHES-profile.sh
 create mode 100755 abs/core-testing/LinHES-system/LinHES-session
 create mode 100755 abs/core-testing/LinHES-system/LinHES-start
 create mode 100644 abs/core-testing/LinHES-system/LinHES.install
 create mode 100755 abs/core-testing/LinHES-system/myth_mtc.py
 create mode 100755 abs/core-testing/LinHES-system/myth_mtc.sh
 create mode 100755 abs/core-testing/LinHES-system/optimize_mythdb.py

diff --git a/abs/core-testing/LinHES-system/LinHES-profile.sh b/abs/core-testing/LinHES-system/LinHES-profile.sh
new file mode 100644
index 0000000..830b819
--- /dev/null
+++ b/abs/core-testing/LinHES-system/LinHES-profile.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+export LinHES_ROOT=/usr/LH
+export PATH=$PATH:/usr/LH/bin
\ No newline at end of file
diff --git a/abs/core-testing/LinHES-system/LinHES-session b/abs/core-testing/LinHES-system/LinHES-session
new file mode 100755
index 0000000..05fc6d1
--- /dev/null
+++ b/abs/core-testing/LinHES-system/LinHES-session
@@ -0,0 +1,13 @@
+#!/bin/sh
+. /etc/profile
+. /etc/systemconfig
+
+xhost +local:
+xhost +127.0.0.1
+xset -dpms s off
+export MALLOC_CHECK_=0
+#start fluxbox
+fluxbox
+
+
+
diff --git a/abs/core-testing/LinHES-system/LinHES-start b/abs/core-testing/LinHES-system/LinHES-start
new file mode 100755
index 0000000..e638334
--- /dev/null
+++ b/abs/core-testing/LinHES-system/LinHES-start
@@ -0,0 +1,162 @@
+#!/bin/sh
+# $Xorg: startx.cpp,v 1.3 2000/08/17 19:54:29 cpqbld Exp $
+#
+# This is just a sample implementation of a slightly less primitive
+# interface than xinit.  It looks for user .xinitrc and .xserverrc
+# files, then system xinitrc and xserverrc files, else lets xinit choose
+# its default.  The system xinitrc should probably do things like check
+# for .Xresources files and merge them in, startup up a window manager,
+# and pop a clock and serveral xterms.
+#
+# Site administrators are STRONGLY urged to write nicer versions.
+#
+# $XFree86: xc/programs/xinit/startx.cpp,v 3.16tsi Exp $
+. /etc/profile
+
+unset DBUS_SESSION_BUS_ADDRESS
+unset SESSION_MANAGER
+
+userclientrc=/etc/X11/Sessions/LinHES
+userserverrc=$HOME/.xserverrc
+sysclientrc=/usr/X11R6/lib/X11/xinit/xinitrc
+sysserverrc=/usr/X11R6/lib/X11/xinit/xserverrc
+defaultclient=/usr/X11R6/bin/xterm
+defaultserver=/usr/X11R6/bin/X
+defaultclientargs=""
+defaultserverargs=""
+clientargs=""
+serverargs=""
+
+
+
+
+
+
+if [ -f $userclientrc ]; then
+    defaultclientargs=$userclientrc
+elif [ -f $sysclientrc ]; then
+    defaultclientargs=$sysclientrc
+fi
+
+
+
+
+if [ -f $userserverrc ]; then
+    defaultserverargs=$userserverrc
+elif [ -f $sysserverrc ]; then
+    defaultserverargs=$sysserverrc
+fi
+
+whoseargs="client"
+while [ x"$1" != x ]; do
+    case "$1" in
+    # '' required to prevent cpp from treating "/*" as a C comment.
+    /''*|\./''*)
+	if [ "$whoseargs" = "client" ]; then
+	    if [ x"$clientargs" = x ]; then
+		client="$1"
+	    else
+		clientargs="$clientargs $1"
+	    fi
+	else
+	    if [ x"$serverargs" = x ]; then
+		server="$1"
+	    else
+		serverargs="$serverargs $1"
+	    fi
+	fi
+	;;
+    --)
+	whoseargs="server"
+	;;
+    *)
+	if [ "$whoseargs" = "client" ]; then
+	    clientargs="$clientargs $1"
+	else
+	    # display must be the FIRST server argument
+	    if [ x"$serverargs" = x ] && \
+		 expr "$1" : ':[0-9][0-9]*$' > /dev/null 2>&1; then
+		display="$1"
+	    else
+		serverargs="$serverargs $1"
+	    fi
+	fi
+	;;
+    esac
+    shift
+done
+
+# process client arguments
+if [ x"$client" = x ]; then
+    # if no client arguments either, use rc file instead
+    if [ x"$clientargs" = x ]; then
+	client="$defaultclientargs"
+    else
+	client=$defaultclient
+    fi
+fi
+
+# process server arguments
+if [ x"$server" = x ]; then
+    # if no server arguments or display either, use rc file instead
+    if [ x"$serverargs" = x -a x"$display" = x ]; then
+	server="$defaultserverargs"
+    else
+	server=$defaultserver
+    fi
+fi
+
+if [ x"$XAUTHORITY" = x ]; then
+    XAUTHORITY=$HOME/.Xauthority
+    export XAUTHORITY
+fi
+
+removelist=
+
+
+# set up default Xauth info for this machine
+case `uname` in
+Linux*)
+	if [ -z "`hostname --version 2>&1 | grep GNU`" ]; then
+		hostname=`hostname -f`
+	else
+		hostname=`hostname`
+	fi
+	;;
+*)
+	hostname=`hostname`
+	;;
+esac
+
+authdisplay=${display:-:0}
+mcookie=`mcookie`
+for displayname in $authdisplay $hostname$authdisplay; do
+    if ! xauth list "$displayname" | grep "$displayname " >/dev/null 2>&1; then
+        xauth -q << EOF 
+add $displayname . $mcookie
+EOF
+	removelist="$displayname $removelist"
+    fi
+done
+
+
+xinit $client $clientargs -- $server $display $serverargs
+
+if [ x"$removelist" != x ]; then
+    xauth remove $removelist
+fi
+
+
+
+
+
+if command -v deallocvt > /dev/null 2>&1; then
+    deallocvt
+fi
+
+
+
+
+
+
+
diff --git a/abs/core-testing/LinHES-system/LinHES.install b/abs/core-testing/LinHES-system/LinHES.install
new file mode 100644
index 0000000..e327edc
--- /dev/null
+++ b/abs/core-testing/LinHES-system/LinHES.install
@@ -0,0 +1,90 @@
+# This is a default template for a post-install scriptlet.  You can
+# remove any functions you don't need (and this header).
+
+# arg 1:  the new package version
+pre_install() {
+/bin/true
+}
+
+# arg 1:  the new package version
+post_install() {
+MVDIR=/usr/LinHES/bin
+#	ln -s  $MVDIR/backup_job /etc/cron.daily/backup_cron
+#	ln -s  $MVDIR/update_system  /etc/cron.daily/xupdate_system_cron
+#	ln -s  $MVDIR/repo_sync.sh  /etc/cron.daily/zrepo_sync.sh
+	ln -s  $MVDIR/myth_mtc.sh  /etc/cron.daily/myth_mtc.sh
+
+#LINE="mythtv    ALL = NOPASSWD: /sbin/sv stop mythbackend"
+LINE="mythtv 	ALL=(ALL)       NOPASSWD: ALL"
+LINE1="%wheel 	ALL=(ALL)       NOPASSWD: ALL"
+
+
+for i in "$LINE" "$LINE1" "$LINE2" "$LINE3" "$LINE4" "$LINE5" "$LINE7" "$LINE8" "$LINE9"  "$LINE10" "$LINE11" "$LINE12" "$LINE13" "$LINE14" "$LINE15" "$LINE16"
+do
+        cp /etc/sudoers /etc/sudoers.bak
+        grep -q "$i" /etc/sudoers
+        if [ $? != 0 ]
+        then
+                echo  "$i" >> /etc/sudoers
+                visudo -cf /etc/sudoers
+                if [ ! $? = 0 ]
+                then
+                        cp /etc/sudoers.bak /etc/sudoers
+			echo $i
+                        echo "Error adding files"
+                        exit 1
+                fi
+        fi
+done
+
+REALTIME="session         required        pam_limits.so"
+grep -q "pam_limits.so" /etc/pam.d/su
+        if [ $? != 0 ]
+        then
+		echo "$REALTIME" >>  /etc/pam.d/su
+
+	fi
+
+
+
+}
+
+# arg 1:  the new package version
+# arg 2:  the old package version
+pre_upgrade() {
+	if [ -e  /etc/cron.daily/backup_cron ]
+	then
+		rm -f  /etc/cron.daily/backup_cron
+	fi
+	if [ -e /etc/cron.daily/xupdate_system_cron ]
+	then
+		rm -f /etc/cron.daily/xupdate_system_cron
+	fi
+
+	if [ -e /etc/cron.daily/myth_mtc.py ]
+	then
+		rm -f /etc/cron.daily/myth_mtc.py
+	fi
+
+
+}
+
+# arg 1:  the new package version
+# arg 2:  the old package version
+post_upgrade() {
+	post_install
+}
+
+# arg 1:  the old package version
+pre_remove() {
+  /bin/true
+}
+
+# arg 1:  the old package version
+post_remove() {
+  /bin/true
+}
+
+op=$1
+shift
+$op $*
diff --git a/abs/core-testing/LinHES-system/myth_mtc.py b/abs/core-testing/LinHES-system/myth_mtc.py
new file mode 100755
index 0000000..faeee84
--- /dev/null
+++ b/abs/core-testing/LinHES-system/myth_mtc.py
@@ -0,0 +1,139 @@
+#!/usr/bin/python
+
+
+import sys,popen2
+import optparse
+import re
+import socket
+import os
+import datetime,time
+
+
+
+try:
+    from MythTV import MythTV
+    mythtv = MythTV()
+except:
+    mythtv = None
+
+#print mythtv.db.getSetting( 'Theme', socket.gethostname())
+
+def optimize():
+    cursor = mythtv.db.cursor()
+    cursor.execute("SHOW tables")
+    result = cursor.fetchall()
+    ops=["REPAIR","OPTIMIZE","ANALYZE"]
+    for row in result:
+        ctable=row[0]
+        for op in ops:
+            print op,ctable
+            cmd= "%s  table %s" %(op,ctable)
+            cursor.execute(cmd)
+
+
+def upcoming_check():
+    upcoming = mythtv.getUpcomingRecordings()
+    try:
+        show=str(upcoming[0])
+        show=show.strip()
+        showtime=show.partition("(")[2].strip(")")
+        now=time.time()
+        rec_time=time.strptime( showtime ,"%Y-%m-%d %H:%M:%S" )
+        r=time.mktime(rec_time)
+        time_diff= ( r - now ) / 60
+    except:
+        time_diff=100
+        show="No show"
+    if ( time_diff  >  30) :
+        return True
+    else:
+        print show , "is upcoming in " , time_diff
+        return False
+
+
+def schemalock_check():
+    c = mythtv.db.cursor()
+    c.execute("select count(*) from schemalock")
+    results=c.fetchone()
+    schemalock=results[0]
+    if schemalock == 0:
+        return True
+    else:
+        print "schema is locked"
+        return False
+
+def job_check():
+    c = mythtv.db.cursor()
+    c.execute("select count(*) from jobqueue where status = 4")
+    results=c.fetchone()
+    jobs= results[0]
+    if jobs == 0 :
+        return True
+    else:
+        print " jobs are running"
+        return False
+
+
+def in_use():
+    c = mythtv.db.cursor()
+    c.execute("select count(*) from inuseprograms")
+    results=c.fetchone()
+    prginuse=results[0]
+    if prginuse == 0 :
+        return True
+    else:
+        print "programs in use"
+        return False
+
+def mfd_check():
+    ismfd=popen2.Popen3('/bin/ps cax|/bin/grep -v grep |/bin/grep mythfilldatabase')
+    ismfd.wait()
+    mfdIdle=ismfd.poll()
+    if mfdIdle == 0:
+        print "mythfilldatabase is running"
+        return False
+    else:
+        return True
+
+
+
+def idle_check():
+    if  (   upcoming_check() and schemalock_check() and job_check()  and in_use() and  mfd_check()  ):
+        idle=True
+        print "Myth is idle"
+    else:
+        idle=False
+        print "Myth is NOT idle"
+    return idle
+
+def run_stuff():
+    if idle_check():
+	print "Running optimize"
+        optimize()
+
+	print "Running backup"
+        os.system('/usr/MythVantage/bin/backup_job')
+
+	print "Running system_update"
+        os.system('/usr/MythVantage/bin/update_system')
+        continue_loop=False
+    else:
+        continue_loop=True
+    return continue_loop
+
+#---------------------------------
+starttime=time.time()
+ctin=True
+while ctin:
+    ctin=run_stuff()
+    if  ctin:
+        time.sleep(600)
+    current_time=time.time()
+    if (current_time - starttime) > 10800 :
+        ctin = False
+        print "time exceeded (3 hours)"
+        exit(1)
+
+
+
+
diff --git a/abs/core-testing/LinHES-system/myth_mtc.sh b/abs/core-testing/LinHES-system/myth_mtc.sh
new file mode 100755
index 0000000..fc81254
--- /dev/null
+++ b/abs/core-testing/LinHES-system/myth_mtc.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+. /etc/profile
+date=`date +%Y-%m-%d`
+myth_mtc.py > /var/log/$date/myth_mtc.log
+if [ $? = 0 ]
+then
+    echo "Finished" >> /var/log/$date/myth_mtc.log
+else
+    echo "Time Exceede" >> /var/log/$date/myth_mtc.log
+fi
+
+
diff --git a/abs/core-testing/LinHES-system/optimize_mythdb.py b/abs/core-testing/LinHES-system/optimize_mythdb.py
new file mode 100755
index 0000000..e7b35ed
--- /dev/null
+++ b/abs/core-testing/LinHES-system/optimize_mythdb.py
@@ -0,0 +1,16 @@
+#!/usr/bin/python
+# import MySQL module
+import MySQLdb
+import socket
+import sys
+db = MySQLdb.connect(host="localhost", user="mythtv", passwd="mythtv", db="mythconverg")
+cursor = db.cursor()
+cursor.execute("SHOW tables")
+result = cursor.fetchall()
+ops=["REPAIR","OPTIMIZE","ANALYZE"]
+for row in result:
+    ctable=row[0]
+    for op in ops:
+        print op,ctable
+        cmd= "%s  table %s" %(op,ctable)
+        cursor.execute(cmd)
-- 
cgit v0.12

