summaryrefslogtreecommitdiffstats
path: root/abs/core/linhes-scripts/idle.sh
blob: 7733b27baf46c3259d7332292d838b815cdc325b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash

usage () {
    echo "Usage: $0 [-h] [-s] [-t <minutes_needed>] [-l] [-w] [-d] [-v]"
    echo
    echo "-h - Print this help/usage message and quit"
    echo "-s - Run silently (default is verbose)"
    echo "-t - Minutes of idle time needed (default is 20)"
    echo "-l - Check for user logins (default: false - do not check)"
    echo "-w - Check for open windows (default: false - do not check)"
    echo "-d - Include mythshutdown daily wake in system busy (default: daily wake is system idle)"
    echo "-v - Be more verbose for debugging"
    echo
    echo "Silent mode is recommended for use in cron jobs or scripts."
    exit $1
}

msg () {  # A status reporting function
    [ "$VERBOSE" -ne 0 ] && echo "$*"
}

mysql_cmd () {
    /usr/bin/mysql -u root mythconverg -sBe "$*"
}

# Command line argument handling
VERBOSE=1
LOGINS=0
WINDOWS=0
DAILY=0
TIME_BEFORE=20
TIME_AFTER=5    # Only adjustable by editing here

while getopts "hslwdt:v" OPT ; do
    case $OPT in
    h) usage 0 ;;
    s) VERBOSE=0 ;;
    t) TIME_BEFORE=$OPTARG ;;
    l) LOGINS=1 ;;
    w) WINDOWS=1 ;;
    d) DAILY=1 ;;
    v) VERBOSE=2 ;;
    *) usage 1 ;;
    esac
done
# Check for extra cruft on the command line...
shift $(($OPTIND - 1))
[ -n "$*" ] && usage 1

msg "Checking what MythTV is doing now or plans to within $TIME_BEFORE minutes..."
msg

/usr/bin/mythshutdown -s 1
BUSY="$?"
msg "mythshutdown returned $BUSY"
if [ "$DAILY" -eq 0 ] ; then
    msg "  including daily wake (64) as system idle"
    # Ignore certain non-zero flag values
    BUSY=$(($BUSY & 0x2F))
fi

SCHEMALOCK=$(mysql_cmd "select count(*) from schemalock")
msg "schemalock $SCHEMALOCK"

JOBS=$(mysql_cmd "select count(*) from jobqueue where status = 4")
msg "running jobs $JOBS"

INUSE=$(mysql_cmd "select count(*) from inuseprograms")
msg "inuse programs $INUSE"

POTENTIAL=$(mysql_cmd "select count(*) from recordmatch as rm, program as p
  where rm.chanid = p.chanid and rm.starttime = p.starttime
  and rm.starttime < now() + interval $TIME_BEFORE minute
  and now() < p.endtime + interval $TIME_AFTER minute")
msg "potential recordings $POTENTIAL"
# See if any of the potential upcoming recordings are real.
# This is ugly, but only the backend knows what programs it really
# plans to record or ignore, so we can't just check the DB. :-(
if [ "$VERBOSE" -ge 2 ] ; then
    mysql_cmd "select p.starttime, p.endtime, p.title, subtitle
      from recordmatch as rm, program as p
      where rm.chanid = p.chanid and rm.starttime = p.starttime
      and rm.starttime < now() + interval $TIME_BEFORE minute
      and now() < p.endtime + interval $TIME_AFTER minute
      order by p.starttime, p.endtime"
fi
UPCOMING=$(/usr/bin/mythbackend --printsched 2>&1 |
  /bin/awk -v potential=$POTENTIAL '
    BEGIN {item=-1;real=0}
    /--- print list start ---/,/---  print list end  ---/ {
        if (item>0 && item<=potential && substr($0,70,1) ~ "[0-9]") real+=1;
        item += 1;
    }
    END {print real}
')
msg "planned recordings $UPCOMING"
if [ "$VERBOSE" -ge 2 ] ; then
    /usr/bin/mythbackend --printsched 2>&1 |
      /bin/awk -v potential=$POTENTIAL '
        BEGIN {item=-1}
        /--- print list start ---/,/---  print list end  ---/ {
            if (item>0 && item<=potential) print $0;
            item += 1;
        }'
fi

# Check all frontends if they are playing
HOSTNAMES=$(mysql_cmd "select hostname from settings where value = 'FrontendIdleTimeout'")
PLAYING=0
for HOST in $HOSTNAMES
do
    if [ "$VERBOSE" -ge 2 ]; then
        msg "Checking if $HOST is playing recordings or videos..."
    fi
ncOUTPUT=$(nc $HOST 6546  << EOF
query location
quit
EOF
)
    if [[ "$ncOUTPUT" == *"# Playback "* ]]; then
        PLAYING=$(( $PLAYING + 1 ))
        msg "$HOST is playing a recording or video"
    else
        msg "$HOST is NOT playing a recording or video"
    fi
done

# Check for users logged in
if [ "$LOGINS" -ge 1 ] ; then
  USERS=`/usr/bin/last | /bin/grep "still logged in"  | awk '{ print $1 }'`
  if [ -n "$USERS" ] ; then
    LOGINS=1
    msg "The following user(s) are still logged in:"
    msg "${USERS}"
  else
    LOGINS=0
    msg "No users are logged in"
  fi
fi

# Check for open windows
FOUNDWINDOWS=0
if [ "$WINDOWS" -ge 1 ] ; then
    /usr/LH/bin/xwin_find.sh -q '.*(mythtv@|xterm|Firefox|Namoroka|Opera|Chromium).*'
    FOUNDWINDOWS="$?"
    FOUNDWINDOWS=$(($FOUNDWINDOWS == 0))
    if [ "$FOUNDWINDOWS" -eq 0 ] ; then
        msg "no application windows open"
    else
        msg "one or more application windows open"
    fi
fi

activities=$(($BUSY + $SCHEMALOCK + $JOBS + $INUSE + $UPCOMING + $PLAYING + $LOGINS + $FOUNDWINDOWS))
msg
if [ "$activities" -eq 0 ] ; then
    msg "System is idle"
    exit 0
else
    msg "System is busy"
    exit 1
fi