1 | #!/bin/bash
|
2 |
|
3 | usage () {
|
4 | echo "Usage: $0 [-h] [-s] [-t <minutes_needed>]"
|
5 | echo
|
6 | echo "-h - Print this help/usage message and quit"
|
7 | echo "-s - Run silently (default is verbose)"
|
8 | echo "-t - Minutes of idle time needed (default is 20)"
|
9 | echo "-v - Be more verbose for debugging"
|
10 | echo
|
11 | echo "Silent mode is recommended for use in cron jobs or scripts."
|
12 | exit $1
|
13 | }
|
14 |
|
15 | msg () { # A status reporting function
|
16 | [ "$VERBOSE" -ne 0 ] && echo "$*"
|
17 | }
|
18 |
|
19 | mysql_cmd () {
|
20 | /usr/bin/mysql -u root mythconverg -sBe "$*"
|
21 | }
|
22 |
|
23 | # Command line argument handling
|
24 | VERBOSE=1
|
25 | TIME_BEFORE=20
|
26 | TIME_AFTER=5 # Only adjustable by editing here
|
27 |
|
28 | while getopts "hst:v" OPT ; do
|
29 | case $OPT in
|
30 | h) usage 0 ;;
|
31 | s) VERBOSE=0 ;;
|
32 | t) TIME_BEFORE=$OPTARG ;;
|
33 | v) VERBOSE=2 ;;
|
34 | *) usage 1 ;;
|
35 | esac
|
36 | done
|
37 | # Check for extra cruft on the command line...
|
38 | shift $(($OPTIND - 1))
|
39 | [ -n "$*" ] && usage 1
|
40 |
|
41 | msg "Checking what MythTV is doing now or plans within $TIME_BEFORE minutes..."
|
42 | msg
|
43 |
|
44 | /usr/bin/mythshutdown -s 1
|
45 | BUSY="$?"
|
46 | msg "mythshutdown returned $BUSY"
|
47 | # Ignore certain non-zero flag values
|
48 | BUSY=$(($BUSY & 0x2F))
|
49 |
|
50 | SCHEMALOCK=$(mysql_cmd "select count(*) from schemalock")
|
51 | msg "schemalock $SCHEMALOCK"
|
52 |
|
53 | JOBS=$(mysql_cmd "select count(*) from jobqueue where status = 4")
|
54 | msg "running jobs $JOBS"
|
55 |
|
56 | INUSE=$(mysql_cmd "select count(*) from inuseprograms")
|
57 | msg "inuse programs $INUSE"
|
58 |
|
59 | POTENTIAL=$(mysql_cmd "select count(*) from recordmatch as rm, program as p
|
60 | where rm.chanid = p.chanid and rm.starttime = p.starttime
|
61 | and rm.starttime < now() + interval $TIME_BEFORE minute
|
62 | and now() < p.endtime + interval $TIME_AFTER minute")
|
63 | msg "potential recordings $POTENTIAL"
|
64 | # See if any of the potential upcoming recordings are real.
|
65 | # This is ugly, but only the backend knows what programs it really
|
66 | # plans to record or ignore, so we can't just check the DB. :-(
|
67 | if [ "$VERBOSE" -ge 2 ] ; then
|
68 | mysql_cmd "select p.starttime, p.endtime, p.title, subtitle
|
69 | from recordmatch as rm, program as p
|
70 | where rm.chanid = p.chanid and rm.starttime = p.starttime
|
71 | and rm.starttime < now() + interval $TIME_BEFORE minute
|
72 | and now() < p.endtime + interval $TIME_AFTER minute
|
73 | order by p.starttime, p.endtime"
|
74 | fi
|
75 | UPCOMING=$(/usr/bin/mythbackend --printsched 2>&1 |
|
76 | /bin/awk -v potential=$POTENTIAL '
|
77 | BEGIN {item=-1;real=0}
|
78 | /--- print list start ---/,/--- print list end ---/ {
|
79 | if (item>0 && item<=potential && substr($0,70,1) ~ "[0-9]") real+=1;
|
80 | item += 1;
|
81 | }
|
82 | END {print real}
|
83 | ')
|
84 | msg "planned recordings $UPCOMING"
|
85 | if [ "$VERBOSE" -ge 2 ] ; then
|
86 | /usr/bin/mythbackend --printsched 2>&1 |
|
87 | /bin/awk -v potential=$POTENTIAL '
|
88 | BEGIN {item=-1}
|
89 | /--- print list start ---/,/--- print list end ---/ {
|
90 | if (item>0 && item<=potential) print $0;
|
91 | item += 1;
|
92 | }'
|
93 | fi
|
94 | activities=$(($BUSY + $SCHEMALOCK + $JOBS + $INUSE + $UPCOMING))
|
95 | msg
|
96 | if [ "$activities" -eq 0 ] ; then
|
97 | msg "System is idle"
|
98 | exit 0
|
99 | else
|
100 | msg "System is busy"
|
101 | exit 1
|
102 | fi
|