From d5ef6c6e5e06d2e1b0d3a823e0ba11ebc8c0942e Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Wed, 1 Feb 2023 18:03:36 -0500
Subject: linhes-system: myth_mtc.py: add

add deps and remove from lh_system_start.sh
---
 linhes/linhes-system/PKGBUILD            |  25 +++--
 linhes/linhes-system/lh_system_start.sh  |  12 +--
 linhes/linhes-system/myth_mtc.py         | 155 +++++++++++++++++++++++++++++++
 linhes/linhes-system/rsyslog.hook        |   9 ++
 linhes/linhes-system/rsyslog.mythtv.conf |  63 +++++++++++++
 5 files changed, 250 insertions(+), 14 deletions(-)
 create mode 100755 linhes/linhes-system/myth_mtc.py
 create mode 100644 linhes/linhes-system/rsyslog.hook
 create mode 100644 linhes/linhes-system/rsyslog.mythtv.conf

diff --git a/linhes/linhes-system/PKGBUILD b/linhes/linhes-system/PKGBUILD
index 4dc0624..e6378b5 100755
--- a/linhes/linhes-system/PKGBUILD
+++ b/linhes/linhes-system/PKGBUILD
@@ -1,16 +1,18 @@
 pkgname=linhes-system
 pkgver=9.0.0
-pkgrel=30
+pkgrel=32
 arch=('x86_64')
 #install=$pkgname.install
 pkgdesc="Everything that makes LinHES a system"
 license=('GPL2')
-depends=('cronie' 'dbus-python' 'kdialog' 'libnotify' 'linhes-templates' 'linhes-theme' 'pacman-contrib'
-         'openssh' 'ttf-overlock')
+depends=('cronie' 'dbus-python' 'flatpak' 'firefox' 'glances' 'kdialog' 'libnotify'
+         'logrotate' 'linhes-templates' 'linhes-theme' 'linhes-web'
+         'mythtv' 'mythplugins-mytharchive' 'mythplugins-mythmusic' 'mythplugins-mythweb' 'ncdu'
+         'openssh' 'pacman-contrib' 'rsyslog' 'ttf-overlock' 'x11vnc')
 binfiles="add_storage.py balance_storage_groups.py empty_storage_groups.py remove_storage.py
           checkXFSfrag.sh enableIRWake.sh idle.py lh_system_start.sh lh_notify-send
           lh_home_check.sh jobqueue_helper.py gen_lib_xml.py
-          diskspace.sh find_orphans.py optimize_mythdb.py
+          diskspace.sh find_orphans.py optimize_mythdb.py myth_mtc.py
           misc_recent_recordings.pl misc_status_config.py misc_status_info.sh
           misc_upcoming_recordings.pl misc_which_recorder.pl
           create_media_dirs.sh be_check.py
@@ -18,10 +20,10 @@ binfiles="add_storage.py balance_storage_groups.py empty_storage_groups.py remov
 source=($binfiles
     'myth_mtc.cron' 'paccache.cron' 'flatpak_update.cron' 'xfs_defrag.cron'
     'readme_is_xml' 'add_storage.readme' 'LinHES-release'
-    '79-cronie.hook' 'fstrim.hook' 'openssh.hook' 'plex_lib.conf'
+    '79-cronie.hook' 'fstrim.hook' 'openssh.hook' 'plex_lib.conf' 'rsyslog.hook'
     '10-monitor.conf' 'x11vnc.override.conf' 'lh_lighttpd.conf' 'lh_php.ini'
     'system-sudo.rules' 'linhes-profile.sh' 'lh_sqlserver.cnf'
-    'lh_system_start.sh.desktop' 'rc6_mce.toml')
+    'lh_system_start.sh.desktop' 'rc6_mce.toml' 'rsyslog.mythtv.conf')
 sha256sums=('7f91d2afcb76e8e9063c6bbe05f5c3d134a6f67541aead8894d342c32d34ad98'
             '1ec3c266cafb0c1b231e88df88b97fec1a1b7465b5d95a0c1e64c8cb727b7c47'
             '97fe9e851c782fa9f85c5b69b110ccff2817dd4fa2a6d9ff6ee225dc558677e4'
@@ -29,7 +31,7 @@ sha256sums=('7f91d2afcb76e8e9063c6bbe05f5c3d134a6f67541aead8894d342c32d34ad98'
             'c7a2b2de44645e3a905b39d7411e5b8b27b60bd2944533fd4655c3b175755da1'
             'ae34515e144830f424d3bd3f6b1b446892d62beed20bca6f0fb19b0bbb779f27'
             '23358a7bff4968eccd469613b81b1415c2ae0ebe77f14f74426697333e4d88d7'
-            '7c6485d07ca67d2a4f089ed885c8ae3a9205d6f43f373ae8a32c2da55daa6de8'
+            '721c698a2478edd8dad2bc52422bc2c225d14fc3a4c30823cb4a2afe52b82fc4'
             '6d4fb0ed1a5ed961b3a3884dce093118e50c2981a9cd5837d20abc5a6d4fd8aa'
             '87875d9e5f5ce18208f419698ce69b6bcbcd08955a57a4a13940e715af58b787'
             '91bdec992bb2c933e15625c181f2195c402060b879168ebf35944cb064c904b9'
@@ -37,6 +39,7 @@ sha256sums=('7f91d2afcb76e8e9063c6bbe05f5c3d134a6f67541aead8894d342c32d34ad98'
             'ebdb3ee0212e0cc72526bb5e50a032573e1894acb7bf75617243b0b49aa1f8f2'
             '4d006f0fe3b13e67de1b961d611e81911905a30d140849dfdb8e5c0dc4da2f7c'
             'e371c6a289c68fe200d7da856c20a8c579efa23178f4d62235f7359d7f6e49a1'
+            'd63ce11809600e77ff187eee3751a8635045ef14c6333f1584f5e35f15a679a1'
             'd2d69b2bf6315bd37ff5f5b2f0cde8ab2fb89bae18f8796dc5208ffc1a9d743e'
             'a3f8ba840853e4a189dd52520a6958f4030e1cc3391200a6aeef055fb469f0b4'
             '1819085bd2c9106482c5f243b95fddf3dae69212330ab76cb493add5c26a45a4'
@@ -57,6 +60,7 @@ sha256sums=('7f91d2afcb76e8e9063c6bbe05f5c3d134a6f67541aead8894d342c32d34ad98'
             '4c29e0b71071ae9556cf2dbd75de560d028577fe5eb993113105112c4b445eac'
             '890482242434e333024c7819e8bf3c889dc16548d0a1745479c8523930fb32f7'
             '71c564a12d9a8e2814a2bf67a1a3d70c1e9d3b50bc108f7043ed8c958c067b01'
+            '6c42b2920c6a37bf3dd05755b9e3fdd80137708cc55a7d1bef2234c17dff0349'
             'dead17906b33a7f9d66ad13bb1c083a23438f45ece9bd5ec41ff86eda01c132a'
             '2c9152ed542bb9c3aa516c6009125a757065100a060e0fa597243d4cbd92844f'
             '73aae13c47223c4ebb45c5cb2aff19a72b6b32cc0d9e7c83de9f97ada593b411'
@@ -65,7 +69,8 @@ sha256sums=('7f91d2afcb76e8e9063c6bbe05f5c3d134a6f67541aead8894d342c32d34ad98'
             '47ace62d7e7e651f25dc7c0a2e96d0862f5efe1a583eeda2843b1fc1499141fe'
             '3ed91fb5a7894f82fb4895e06d2e3f1df3ac4f82e46c970d4a85aaa4edc24cf1'
             '0b9868a563036c81f8fdb8ab8bbad51934aca2a07e9d7634e24214791afda8e2'
-            '1b965b5e7eeafdf3815c8f2722587a560693dd780327cca9910dc47fba0f1aef')
+            '1b965b5e7eeafdf3815c8f2722587a560693dd780327cca9910dc47fba0f1aef'
+            '2b91f6eb8c010a0dce1f41149c0549d067915fba93251c7af7e5328a05977f0c')
 
 package() {
     cd $srcdir
@@ -102,6 +107,9 @@ package() {
     #gen_lib_xml.py files
     install -Dm644 ${srcdir}/plex_lib.conf ${pkgdir}/etc/gen_lib_xml.d/plex_lib.conf
 
+    #rsyslog files
+    install -Dm644 $srcdir/rsyslog.mythtv.conf $pkgdir/etc/rsyslog.d/mythtv.conf
+
     #remote files
     install -Dm644 $srcdir/rc6_mce.toml $pkgdir/etc/rc_keymaps/rc6_mce.toml
 
@@ -121,6 +129,7 @@ package() {
 	install -Dm0644 $srcdir/79-cronie.hook "${pkgdir}"/usr/share/libalpm/hooks/79-cronie.hook
 	install -Dm0644 $srcdir/fstrim.hook "${pkgdir}"/usr/share/libalpm/hooks/fstrim.hook
 	install -Dm0644 $srcdir/openssh.hook "${pkgdir}"/usr/share/libalpm/hooks/openssh.hook
+	install -Dm0644 $srcdir/rsyslog.hook "${pkgdir}"/usr/share/libalpm/hooks/rsyslog.hook
 
     #sudo rules
     mkdir -p $pkgdir/etc/sudoers.d/
diff --git a/linhes/linhes-system/lh_system_start.sh b/linhes/linhes-system/lh_system_start.sh
index d5a7646..257f800 100755
--- a/linhes/linhes-system/lh_system_start.sh
+++ b/linhes/linhes-system/lh_system_start.sh
@@ -64,11 +64,11 @@ function storage_scan(){
 
 function install_lh_apps(){
     #install programs that are not needed on the iso
-    /usr/bin/lh_notify-send --app-name="LinHES" --icon=dialog-information --expire-time=40000 "Installing apps." "This could take a few minutes."
-    sudo pacman -Syyy --noconfirm archlinux-keyring
-    sudo pacman -Syyy --noconfirm flatpak firefox glances mlocate mythtv mythplugins-mytharchive mythplugins-mythmusic mythplugins-mythweb ncdu x11vnc
-    status=$?
-    [ $status -eq 1 ] && msg "Could not install apps. Check internet connection. Cancelling Setup." && exit 1
+    msg "Installing apps."
+    #sudo pacman -Syyy --noconfirm archlinux-keyring
+    #sudo pacman -Syyy --noconfirm flatpak firefox glances mlocate mythtv mythplugins-mytharchive mythplugins-mythmusic mythplugins-mythweb ncdu x11vnc
+    #status=$?
+    #[ $status -eq 1 ] && msg "Could not install apps. Check internet connection. Cancelling Setup." && exit 1
     flatpak install tv.plex.PlexHTPC --noninteractive --assumeyes
     gen_lib_xml.py
 }
@@ -85,7 +85,7 @@ function sql_setup(){
 }
 
 function localweb_setup(){
-    sudo pacman -Syyy --noconfirm linhes-web
+    #sudo pacman -Syyy --noconfirm linhes-web
     sudo cp /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.orig
     sudo cp /usr/share/linhes/templates/lighttpd.conf.template /etc/lighttpd/lighttpd.conf
     sudo systemctl enable --now lighttpd.service
diff --git a/linhes/linhes-system/myth_mtc.py b/linhes/linhes-system/myth_mtc.py
new file mode 100755
index 0000000..953fb96
--- /dev/null
+++ b/linhes/linhes-system/myth_mtc.py
@@ -0,0 +1,155 @@
+#!/usr/bin/python
+import sys, subprocess
+import re
+import socket
+import os
+import datetime,time
+import shlex
+sys.dont_write_bytecode = True
+
+try:
+    from MythTV import MythBE
+    mythtv = MythBE()
+except:
+    mythtv = None
+
+#print mythtv.db.getSetting( 'Theme', socket.gethostname())
+
+def get_timestamp():
+    now = datetime.datetime.now()
+    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 caches...")
+        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.mythtv/{*cache,Cache-*,tmp/*,MythMusic/AlbumArt/*}"
+        subprocess.call(["sh", "-c", cmd])
+        print("    Restarting mythfrontend...")
+        subprocess.call(["killall", "mythfrontend"])
+        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.cache/*"
+        subprocess.call(["sh", "-c", cmd])
+        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.plexht/userdata/Thumbnails/*"
+        subprocess.call(["sh", "-c", cmd])
+        cmd = "/usr/bin/rm -rf " + data_config.MYTHHOME + "/.plexht/userdata/ThemeMusicCache/*"
+        subprocess.call(["sh", "-c", cmd])
+        freePcent = getFreePercentForDir(data_config.MYTHHOME)
+        print("  Home directory percent used: " + str(freePcent * 100) + "%")
+    else:
+        print("    Home directory is less than 90% used. Not clearing caches.")
+    return 0
+
+def optimize():
+    try:
+        cursor = mythtv.db.cursor()
+        cursor.execute("SHOW tables")
+        result = cursor.fetchall()
+    except:
+        print("\n%s Problem getting tables from the database" %(get_timestamp()))
+        return 1
+    ops=["REPAIR","OPTIMIZE","ANALYZE"]
+    for row in result:
+        ctable=row[0]
+        for op in ops:
+            print("    %s %s" %(op,ctable))
+            cmd= "%s  table %s" %(op,ctable)
+            cursor.execute(cmd)
+    return 0
+
+def cleanup_inuseprograms():
+    fourHoursAgo=datetime.datetime.today() - datetime.timedelta(hours=4)
+    cmd="DELETE FROM inuseprograms WHERE lastupdatetime < '%s';" %fourHoursAgo
+    try:
+        cursor = mythtv.db.cursor()
+        cursor.execute(cmd)
+    except:
+        print("\n%s Problem cleaning inuseprograms in database" %(get_timestamp()))
+
+
+def bail_if_another_is_running():
+    cmd = shlex.split("pgrep -u {} -f {}".format(os.getuid(), __file__))
+    pids = subprocess.check_output(cmd).strip().split('\n')
+    if len(pids) > 1:
+        pids.remove("{}".format(os.getpid()))
+        print("Exiting! Found {} is already running (pids): {}".format(
+            __file__, " ".join(pids)))
+        raise SystemExit(1)
+
+def run_stuff():
+    print("\n%s" %get_timestamp())
+
+    if (len(sys.argv) == 1) or ("--noidlecheck" in sys.argv) and (len(sys.argv) == 2):
+        runall = True
+    else:
+        runall = False
+
+    if ("--noidlecheck" in sys.argv):
+        print("No system idle check will be done.")
+        idle = 0
+    else:
+        idle = subprocess.call(["/usr/bin/python2", "/usr/LH/bin/idle.py"])
+
+    if not idle:
+        if ("--check_home" in sys.argv) or runall:
+            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 runall:
+            print("\n#######################################")
+            print("\n%s Running Optimize" %(get_timestamp()))
+            if not optimize():
+                print("\nFinished Optimize")
+            else:
+                return True
+
+        if ("--backup" in sys.argv) or runall:
+            print("\n#######################################")
+            print("\n%s Running Backup" %(get_timestamp()))
+            if not os.system('/usr/LH/bin/lh_system_backup_job'):
+                print("\nFinished Backup")
+
+        if ("--update" in sys.argv) or runall:
+            print("\n#######################################")
+            print("\n%s Running System Update" %(get_timestamp()))
+            if not os.system('/usr/LH/bin/lh_system_host_update'):
+                print("\nFinished Update")
+
+        print("\n#######################################")
+        continue_loop=False
+    else:
+        continue_loop=True
+    return continue_loop
+
+#---------------------------------
+bail_if_another_is_running()
+starttime=time.time()
+ctin=True
+while ctin:
+    cleanup_inuseprograms()
+    ctin=run_stuff()
+    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
+        print("\n%s Time Exceeded 50 minutes. Quitting.)" %(get_timestamp()))
+        exit(1)
diff --git a/linhes/linhes-system/rsyslog.hook b/linhes/linhes-system/rsyslog.hook
new file mode 100644
index 0000000..00e3b5f
--- /dev/null
+++ b/linhes/linhes-system/rsyslog.hook
@@ -0,0 +1,9 @@
+[Trigger]
+Operation = Install
+Type = Package
+Target = linhes-system
+
+[Action]
+Description = Enable and start rsyslog...
+When = PostTransaction
+Exec = /usr/bin/systemctl enable --now rsyslog.service
diff --git a/linhes/linhes-system/rsyslog.mythtv.conf b/linhes/linhes-system/rsyslog.mythtv.conf
new file mode 100644
index 0000000..9f3efe7
--- /dev/null
+++ b/linhes/linhes-system/rsyslog.mythtv.conf
@@ -0,0 +1,63 @@
+# MythTV 0.26 and later rsyslog.d conf
+
+# Control-code conversion. The default is on.
+#$EscapeControlCharactersOnReceive off
+
+$template DynMythFormat,"%TIMESTAMP:::date-rfc3339%%msg%\n"
+$template DynMythBackend,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythbackend.log"
+$template DynMythFrontend,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythfrontend.log"
+$template DynMythJobQueue,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythjobqueue.log"
+$template DynMythMediaServer,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythmediaserver.log"
+$template DynMythSetup,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythtv_setup.log"
+$template DynMythFillDatabase,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythfilldatabase.log"
+$template DynMythCommflag,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythcommflag.log"
+$template DynMythPreviewGen,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythpreviewgen.log"
+$template DynMythTranscode,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythtranscode.log"
+$template DynMythMetadataLookup,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythmetadatalookup.log"
+$template DynMythUtil,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythutil.log"
+$template DynMythWelcome,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythwelcome.log"
+$template DynMythShutdown,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythshutdown.log"
+$template DynMythLCDServer,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythlcdserver.log"
+$template DynMythccExtractor,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythccextractor.log"
+$template DynMythAVTest,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythavtest.log"
+$template DynMythLogServer,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_mythlogserver.log"
+$template DynMythNoLogServer,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%HOSTNAME%_%programname%.log"
+
+
+
+if $syslogfacility-text == 'local6' and $msg startswith ' mythbackend' then ?DynMythBackend;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythfrontend' then ?DynMythFrontend;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythjobqueue' then ?DynMythJobQueue;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythmediaserver' then ?DynMythMediaServer;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythtv-setup' then ?DynMythSetup;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythfilldatabase' then ?DynMythFillDatabase;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythcommflag' then ?DynMythCommflag;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythpreviewgen' then ?DynMythPreviewGen;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythtranscode' then ?DynMythTranscode;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythmetadatalookup' then ?DynMythMetadataLookup;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythutil' then ?DynMythUtil;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythwelcome' then ?DynMythWelcome;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythshutdown' then ?DynMythShutdown;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythlcdserver' then ?DynMythLCDServer;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythccextractor' then ?DynMythccExtractor;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythavtest' then ?DynMythAVTest;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $msg startswith ' mythlogserver' then ?DynMythLogServer;DynMythFormat
+& stop
+if $syslogfacility-text == 'local6' and $programname startswith 'myth' then ?DynMythNoLogServer
+& stop
-- 
cgit v0.12

