source: server/bin/ogagentqueue.cron

lgromero-new-oglive
Last change on this file was c28eefa, checked in by Natalia Serrano <natalia.serrano@…>, 19 months ago

Log to syslog in a number of shell scripts

  • Property mode set to 100755
File size: 3.5 KB
Line 
1#!/bin/bash
2
3#/**
4#@file    ogagentqueue.cron
5#@brief   Cronfile to send pending operations to OGAgent.
6#warning  This file must be executed under system Cron every minute.
7#@version 1.1.0 - Initial version.
8#@date    2017-10-26
9#@author  Ramón M. Gómez - Univ. Sevilla
10#*/ ##
11
12
13# Variables.
14PROG=$(basename "$0")
15OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"}
16SERVERCONF=$OPENGNSYS/etc/ogAdmServer.cfg
17
18source $OPENGNSYS/lib/ogfunctions.sh || exit 1
19
20# Basic error control
21source $SERVERCONF 2> /dev/null || raiseError access "Server configuration file"
22
23# Reading pending operations.
24dbexec "
25SELECT ogagent_queue.id, ogagent_queue.exectime, ogagent_queue.operation,
26       ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.language
27  FROM ogagent_queue
28  JOIN ordenadores ON ogagent_queue.clientid=ordenadores.idordenador
29  JOIN remotepc ON ogagent_queue.clientid=remotepc.id
30 WHERE exectime < NOW()
31 ORDER BY exectime;" | \
32    while read -r OPERID DATE TIME OPER CLNTID AGNTIP AGNTKEY LANGUAGE; do
33        # Preparing operation data.
34        case "$OPER" in
35            popup-10)   # Message: 10 min. before power off.
36                AGNTURL="https://$AGNTIP:8000/opengnsys/popup"
37                case "$LANGUAGE" in
38                    es)     DATA='{"title":"Apagado en 10 min.","message":"Fin del tiempo de acceso remoto.\nEl ordenador se apagará automáticamente dentro de 10 minutos."}' ;;
39                    *)      DATA='{"title":"Shutdown after 10 min.","message":"Remote access time is ended.\nComputer will be powered off automaticly after 10 minutes."}' ;;
40                esac
41                ;;
42            popup-5)    # Message: 5 min. before power off.
43                AGNTURL="https://$AGNTIP:8000/opengnsys/popup"
44                case "$LANGUAGE" in
45                    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."}' ;;
46                    *)      DATA='{"title":"Shutdown after 5 min.","message":"The computer will be powered off automaticly after 5 minutes.\nATTENTION: This is the last warning."}'
47                esac
48                ;;
49            poweroff)   # Power off client.
50                AGNTURL="https://$AGNTIP:8000/opengnsys/poweroff"
51                DATA=
52                ;;
53            *)          # Unknown operation.
54                AGNTURL=
55                ;;
56        esac
57        # Sending operation to OGAgent.
58        if [ -n "$AGNTURL" ]; then
59            CODE=$(curl -ksm 1 -w "%{http_code}" -o /dev/null -H "Authorization: $AGNTKEY" ${DATA:+"-d $DATA"} "$AGNTURL")
60            case "$CODE" in
61                000)    # Client does not respond (may be halted).
62                    ;;
63                200)    # Operation sent.
64                    echolog "Operation sent to OGAgent: client=$AGNTIP, oper=$OPER, exectime=\"$DATE $TIME\"" ;;
65                *)      # Operation error.
66                    echolog "Operation error: client=$AGNTIP, oper=$OPER, code=$CODE" ;;
67            esac
68        else    # Unknown operation.
69            echolog "Unknown operation: client=$AGNTIP, oper=$OPER"
70        fi
71        # Deleting operation from the database.
72        SQL="DELETE FROM ogagent_queue WHERE id='$OPERID';"
73        [ "$OPER" == "poweroff" ] && SQL+="
74UPDATE remotepc
75   SET reserved = NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL, language=NULL
76 WHERE id = '$CNLTID';
77DELETE FROM acciones
78 WHERE idordenador = '$CLNTID'
79   AND descriaccion = 'RemotePC Session';"
80        dbexec "$SQL"
81    done
Note: See TracBrowser for help on using the repository browser.