summaryrefslogtreecommitdiffstats
path: root/server/bin/ogagentqueue.cron
blob: 16f7c67e2db872f34a8c7a8f8e1f3a072cb99324 (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
#!/bin/bash

#/**
#@file	  ogagentqueue.cron
#@brief   Cronfile to send pending operations to OGAgent.
#warning  This file must be executed under system Cron every minute.
#@version 1.1.0 - Initial version.
#@date    2017-10-26
#@author  Ramón M. Gómez - Univ. Sevilla
#*/ ##


# Variables.
PROG=$(basename "$0")
OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"}
SERVERCONF=$OPENGNSYS/etc/ogserver.cfg
LOGFILE=$OPENGNSYS/log/remotepc.log

source $OPENGNSYS/lib/ogfunctions.sh || exit 1

# Basic error control
source $SERVERCONF 2> /dev/null || raiseError access "Server configuration file"
touch "$LOGFILE" 2> /dev/null || raiseError access "Cannot write in the log file" 

# Reading pending operations.
dbexec "
SELECT ogagent_queue.id, ogagent_queue.exectime, ogagent_queue.operation,
       ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.language
  FROM ogagent_queue
  JOIN ordenadores ON ogagent_queue.clientid=ordenadores.idordenador
  JOIN remotepc ON ogagent_queue.clientid=remotepc.id
 WHERE exectime < NOW()
 ORDER BY exectime;" | \
    while read -r OPERID DATE TIME OPER CLNTID AGNTIP AGNTKEY LANGUAGE; do
        # Preparing operation data.
        case "$OPER" in
            popup-10)   # Message: 10 min. before power off.
                AGNTURL="https://$AGNTIP:8000/opengnsys/popup"
                case "$LANGUAGE" in
                    es)     DATA='{"title":"Apagado en 10 min.","message":"Fin del tiempo de acceso remoto.\nEl ordenador se apagará automáticamente dentro de 10 minutos."}' ;;
                    *)      DATA='{"title":"Shutdown after 10 min.","message":"Remote access time is ended.\nComputer will be powered off automaticly after 10 minutes."}' ;;
                esac
                ;;
            popup-5)    # Message: 5 min. before power off.
                AGNTURL="https://$AGNTIP:8000/opengnsys/popup"
                case "$LANGUAGE" in
                    es)     DATA='{"title":"Apagado en 5 min.","message":"El ordenador se apagará automáticamente dentro de 5 minutos.\nATENCIÓN: Este es el último aviso."}' ;;
                    *)      DATA='{"title":"Shutdown after 5 min.","message":"The computer will be powered off automaticly after 5 minutes.\nATTENTION: This is the last warning."}'
                esac
                ;;
            poweroff)   # Power off client.
                AGNTURL="https://$AGNTIP:8000/opengnsys/poweroff"
                DATA=
                ;;
            *)          # Unknown operation.
                AGNTURL=
                ;;
        esac
        # Sending operation to OGAgent.
        if [ -n "$AGNTURL" ]; then
            CODE=$(curl -ksm 1 -w "%{http_code}" -o /dev/null -H "Authorization: $AGNTKEY" ${DATA:+"-d $DATA"} "$AGNTURL")
            case "$CODE" in
                000)    # Client does not respond (may be halted).
                    ;;
                200)    # Operation sent.
                    echo "$(date +"%FT%T%z"): $PROG: Operation sent to OGAgent: client=$AGNTIP, oper=$OPER, exectime=\"$DATE $TIME\"" >> $LOGFILE ;;
                *)      # Operation error.
                    echo "$(date +"%FT%T%z"): $PROG: Operation error: client=$AGNTIP, oper=$OPER, code=$CODE" >> $LOGFILE ;;
            esac
        else	# Unknown operation.
            echo "$(date +"%FT%T%z"): $PROG: Unknown operation: client=$AGNTIP, oper=$OPER" >> $LOGFILE
        fi
        # Deleting operation from the database.
        SQL="DELETE FROM ogagent_queue WHERE id='$OPERID';"
        [ "$OPER" == "poweroff" ] && SQL+="
UPDATE remotepc
   SET reserved = NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL, language=NULL
 WHERE id = '$CNLTID';
DELETE FROM acciones
 WHERE idordenador = '$CLNTID'
   AND descriaccion = 'RemotePC Session';"
        dbexec "$SQL"
    done