Monday, March 12, 2012

MythTV scripts used

mythbackup.sh (I don't use this one)
#Dumps the mythconverg database - daily backup
#Keeps the last 7 days
#!/bin/sh
LOG="/var/log/archmyth.log"
DAY=`/bin/date +%u`
DUMPFILE="~/.mythtv/mythbackup/mythdb_$DAY.sql"
DATE=`date`
echo "$DATE : Backuping up myth database to $DUMPFILE" >> $LOG
/usr/bin/mysqldump -u mythtv -pmythtv mythconverg -c > $DUMPFILE
DATE=`date`
echo "$DATE : Backup completed." >> $LOG
exit 0

mythidle.pl - courtesy of Arkay @ pcmediacenter.com.au
(be careful if cutting and pasting this, due to narrow width of the printout, some lines may have carried over to a new line, when they should not have)
Pre-shutdown-check command: /home/david/scripts/mythidle.pl
#!/usr/bin/perl -w
###############################################################################################################################
## Name: mythidle.pl
##
## Purpose: Checks to determine if the mythbackend server is idle and ready for shutdown.
## 
## (C)opyright 2008 Arksoft.
##                                    
## Author: Arkay
## 
## Ver 1.0: 14-07-2008. Initial version.
## 
###############################################################################################################################
# Require and Use Clauses.
###############################################################################################################################

use strict;    #Keeps code neat.
use Getopt::Std;   #Getopt module for option preprocessing.
use vars qw/ $opt_d $opt_h /;  #Option Processing vars.
use POSIX qw(strftime);   #Time routine we need.

###############################################################################################################################
# Prototype definitions
###############################################################################################################################

sub logmsg(@);  #Message logger so we can track what's been going on.
sub process_opts(); #Option processing.. Nothing exiting for this script.
sub do_command($); #Execute a shell command for lazy perl programmers :)
sub check_hosts(); #Are any server dependant hosts currently up?
sub check_procs(); #Check if the server is running anything that should keep us awake.
sub check_myth(); #Check if mythbackend is busy doing anything.

###############################################################################################################################
# Constant Definitions.
###############################################################################################################################

my ($TRUE) = 1;
my ($FALSE) = 0;

###############################################################################################################################
# Global vars, paths, commands to call.
###############################################################################################################################

my ($LOG) = "/var/log/archmyth.log";     #Log location.
my ($LOGSIZE) = 1024;       #Maximum log size in kbytes, self pruning.
my ($DEBUG) = $FALSE;       #Debugging default is off.
my ($BASENAME) = $0;       #How was the program called?

my ($MYTHSTATUS)="/usr/bin/mythshutdown --status;echo \$\?";  #Command to query myth status
my ($PING)="/bin/ping";       #Where is ping

###############################################################################################################################
# These are the only 2 lines in this file that should be edited.
# The first (@procs) is a list of processes that when running should keep the server awake.
# The second (@HOSTS) lists hosts that, if active, prevents the server from sleeping.
# To acertain the name of a process to see if it still running use ps -ef | grep -i 
# The names of the hosts listed below need to exist in your /etc/hosts file. You can check 
# that they are up with ping 
# Both lines consist of a comma separate list of quoted strings. i.e.
#my (@PROCS)=("dpexpress","ktorrent","shepherd","altbinz");  #Stay awake if active.
#my (@HOSTS)=("debs","htpc-lounge","quadarch");    #stay awake if any of these are up
###############################################################################################################################
my (@PROCS)=("shepherd","xbmc","chromium","vlc","smplayer","HandBrakeCLI"); #stay awake?
my (@HOSTS)=("myth-frontend");      #stay awake if any of these are up

###############################################################################################################################
# The Mainline.
###############################################################################################################################
MAIN:
{
 my ($blocked)=$FALSE;

 process_opts();

 logmsg "$BASENAME started : PID($$)";

 SWITCH: #blocked
 {
  if (check_hosts() != $FALSE) #Check it any client hosts are up
  {
   $blocked=$TRUE;
   last;
  }

  if (check_procs() != $FALSE) #Check if any processes are blocking shutdown
  {
   $blocked=$TRUE;
   last;
  }

  if (check_myth() != $FALSE) #Check if myth is busy
  {
   $blocked=$TRUE;
   last;
  }
  $blocked=$FALSE;
 }

 if ($blocked == $TRUE)
 {
  logmsg "Mythbackend is not idle - blocking Shutdown.";
 }
 else
 {
  logmsg "Mythbackend is currently idle - Allowing Shutdown.";
 }

 logmsg "$BASENAME Completed."; logmsg " ";
 exit($blocked);
}

###############################################################################################################################
# check_procs()
# Check if processes are running that should stop shutdown from occuring.
###############################################################################################################################
sub check_procs()
{
 my (@output);
 my ($proc);
 my ($command);
 my ($count)=0;
 my ($running)=$FALSE;

 logmsg "PROC  - Checking active processes that block shutdown.";

 foreach $proc (@PROCS)
 {
  $command="ps -ef | grep $proc | grep -v grep |wc -l"; 
  @output=do_command($command);

  if (@output)
  {
   $count=$output[0];
   chomp ($count);
  }

  if ($count > 0)
  {
   logmsg "PROC  - Found active process : $proc ($count running).";
   $running=$TRUE;
  }
 }
 logmsg "PROC  - No blocking processes currently running." if ($running) == $FALSE;
 return($running);
}

###############################################################################################################################
# check_myth()
# Check if we have active samba connections.
###############################################################################################################################
sub check_myth()
{
 my (@output);
 my ($status)=69;
 my ($text)="Unknown";

 logmsg "MYTH  - Checking mythbackend status.";
 @output=do_command($MYTHSTATUS);

 if (@output)
 {
  $status=$output[0]; chomp($status);

  $text="Idle."       if ($status) == 0;
  $text="Transcoding."      if ($status) == 1;
  $text="Flagging Commercials."     if ($status) == 2;
  $text="Grabbing EPG Data."     if ($status) == 4;
  $text="Recording."      if ($status) == 8;
  $text="Locked."      if ($status) == 16;
  $text="Jobs running/pending."     if ($status) == 32;
  $text="In a daily wakeup/shutdown period."   if ($status) == 64;
  $text="Less than 15 minutes to next wakeup period."  if ($status) == 128;
  $text="Setup is running."    if ($status) == 255;

  logmsg "MYTH  - Mythbackend status ($status) : $text";
 }
 else
 {
  logmsg "MYTH  - Failed to get mythbackend status.";
 }

 return($status);
}

###############################################################################################################################
# check_hosts()
# Checkif we have active samba connections.
###############################################################################################################################
sub check_hosts()
{
 my ($line);
 my ($host);
 my (@output,@ping);
 my ($up)=$FALSE;
 my ($command);

 logmsg "HOSTS - Checking for active client hosts.";

 foreach $host (@HOSTS)
 {
  $command="$PING -c2 $host | grep received | awk '{print \$4}'";
  
  @output=do_command($command);

  if ($output[0] != 0)
  {
   logmsg "HOSTS - Client host \"$host\" is still up.";
   $up=$TRUE;
   last;
  }
  else
  {
   logmsg "HOSTS - Client host \"$host\" is currently down.";
  }

 }
 logmsg "HOSTS - No active client hosts found." if ($up == $FALSE);

 return($up);
}


###############################################################################################################################
# logmsg
# Little routine to write to the log file.
# Rotates around $LOGSIZE bytes.
###############################################################################################################################
sub logmsg(@)
{ 
 my ($string)=@_;
 my $time=scalar localtime;
 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
 my (@lines,$line);

 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)=stat("$LOG");

 if (defined($size))
 {
  $size=$size/1024;    #size in kbyte

  if ($size >= $LOGSIZE)
  {
   unlink ("$LOG.old") if (-e("$LOG.old"));
   rename ($LOG,"$LOG.old");
  }
 }

 print "$time : $string\n" if ($DEBUG==$TRUE);

 if (open (LOG,">>$LOG"))
 {
  if ($string =~ /\n/)
  {
   @lines=split(/\n/,$string);
   foreach $line (@lines)
   {
    print LOG "$time : $line\n"; 
   }
  }
  else
  {
    print LOG "$time : $string\n"; 
  }
  close (LOG);
 }
 else
 {
  print "Unable to open LOG $LOG : $!";
 }
}

###############################################################################################################################
# process_opts()
# Set Global option flags dependant on command line input.
###############################################################################################################################
sub process_opts()
{
 getopts('dh');

 $DEBUG=$TRUE if ($opt_d); 
 exit(usage(1)) if ($opt_h);
}

###############################################################################################################################
# usage()
# Output Relevant Usage strings if incorrect opts are given.
###############################################################################################################################
sub usage()
{
 my($ucode)=@_;

 if ($ucode == 1) 
 {
  print "Usage: $BASENAME [-dh]\n";
  return(0);
 }
}

###############################################################################################################################
# sub do_command($)
# use system call to execute command. Returns output of command in array.
###############################################################################################################################
sub do_command($)
{
    my ($command)=@_;
    my (@output);
    my ($exit_value)=0;

    logmsg "Executing $command" if ($DEBUG == $TRUE);

    @output=`$command`; 

    $exit_value = $? >> 8;

    if ($exit_value != 0)
    {
        logmsg "Error executing $command : $!";
    }
    return(@output);
}

setwakeup.sh
sudo sh -c "/home/david/scripts/setwakeup.sh $time"
#!/bin/sh
#$1 is the first argument to the script. It is the time in seconds since 1970
#this is defined in mythtv-setup with the time_t argument

LOG="/var/log/archmyth.log"
#Set the wakeup timers.
echo 0 > /sys/class/rtc/rtc0/wakealarm      #this clears your alarm.
echo $1 > /sys/class/rtc/rtc0/wakealarm     #this writes the alarm.
date=`date "+%a %b %e %H:%M:%S %Y"`
schedutc=`date -u -d @$1 +%F" "%T`
sched=`date -d @$1 +%F" "%T`
echo "$date : Next scheduled recording : $sched ($schedutc UTC)" >>$LOG
#cat /proc/driver/rtc | head -4 >>$LOG

shutdown.sh
sudo sh -c "/home/david/scripts/shutdown.sh"
#!/bin/sh

LOG="/var/log/archmyth.log"
date=`date "+%a %b %e %H:%M:%S %Y"`
echo "$date : System Shutting down." >>$LOG
/sbin/shutdown -h now

No comments:

Post a Comment