source: server/bin/ogagentqueue.cron @ 56a0aff

918-git-images-111dID-1020_logrotateBugID-1037_Mostrar.TipoDisco.WebID-1038_Muestra.el.numero.de.ordenadoresID-1039_Asignar_Disco.Particion.Imagen.AccesoRemotoID-824_Iniciar.Sistema.Restauradodevel-ogadmserver-for-masterdisk-imagemainmaster-1037opengnsys-1.1.1dpreqndtest
Last change on this file since 56a0aff was 703551b, checked in by Ramón M. Gómez <ramongomez@…>, 5 years ago

#877: Copy hosts file on OGAgent activation, and some code cleanup.

  • Property mode set to 100755
File size: 4.1 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
17LOGFILE=$OPENGNSYS/log/remotepc.log
18MYCNF=/tmp/.my.cnf.$$
19
20# Basic error control
21if [ ! -r "$SERVERCONF" ]; then
22    echo "$PROG: Cannot access to configuration file." >&2
23    exit 2
24fi
25if ! touch "$LOGFILE"; then
26    echo "$PROG: Cannot write to log file." >&2
27    exit 2
28fi
29
30# Fetching database access data.
31source "$SERVERCONF"
32# Composing connection credentils file.
33touch $MYCNF 
34chmod 600 $MYCNF 
35cat << EOT > $MYCNF
36[client]
37user=$USUARIO
38password=$PASSWORD
39EOT
40# Trap to delete temporal file if process ends.
41trap "rm -f $MYCNF" 0 1 2 3 6 9 15 
42# Reading pending operations.
43mysql --defaults-extra-file="$MYCNF" -D "$CATALOG" -Nse "
44SELECT ogagent_queue.id, ogagent_queue.exectime, ogagent_queue.operation,
45       ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.language
46  FROM ogagent_queue
47  JOIN ordenadores ON ogagent_queue.clientid=ordenadores.idordenador
48  JOIN remotepc ON ogagent_queue.clientid=remotepc.id
49 WHERE exectime < NOW()
50 ORDER BY exectime;" | \
51    while read -r OPERID DATE TIME OPER CLNTID AGNTIP AGNTKEY LANGUAGE; do
52        # Preparing operation data.
53        case "$OPER" in
54            popup-10)   # Message: 10 min. before power off.
55                AGNTURL=https://$AGNTIP:8000/opengnsys/popup
56                case "$LANGUAGE" in
57                    es)     DATA='{"title":"Apagado en 10 min.","message":"Fin del tiempo de acceso remoto.\nEl ordenador se apagará automáticamente dentro de 10 minutos."}' ;;
58                    *)      DATA='{"title":"Shutdown after 10 min.","message":"Remote access time is ended.\nComputer will be powered off automaticly after 10 minutes."}' ;;
59                esac
60                ;;
61            popup-5)    # Message: 5 min. before power off.
62                AGNTURL=https://$AGNTIP:8000/opengnsys/popup
63                case "$LANGUAGE" in
64                    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."}' ;;
65                    *)      DATA='{"title":"Shutdown after 5 min.","message":"The computer will be powered off automaticly after 5 minutes.\nATTENTION: This is the last warning."}'
66                esac
67                ;;
68            poweroff)   # Power off client.
69                AGNTURL=https://$AGNTIP:8000/opengnsys/poweroff
70                DATA=
71                ;;
72            *)          # Unknown operation.
73                AGNTURL=
74                ;;
75        esac
76        # Sending operation to OGAgent.
77        if [ -n "$AGNTURL" ]; then
78            CODE=$(curl -ksm 1 -w "%{http_code}" -o /dev/null -H "Authorization: $AGNTKEY" ${DATA:+"-d $DATA"} "$AGNTURL")
79            case "$CODE" in
80                000)    # Client does not respond (may be halted).
81                    ;;
82                200)    # Operation sent.
83                    echo "$(date +"%FT%T%z"): $PROG: Operation sent to OGAgent: client=$AGNTIP, oper=$OPER, exectime=\"$DATE $TIME\"" >> $LOGFILE ;;
84                *)      # Operation error.
85                    echo "$(date +"%FT%T%z"): $PROG: Operation error: client=$AGNTIP, oper=$OPER, code=$CODE" >> $LOGFILE ;;
86            esac
87        else    # Unknown operation.
88            echo "$(date +"%FT%T%z"): $PROG: Unknown operation: client=$AGNTIP, oper=$OPER" >> $LOGFILE
89        fi
90        # Deleting operation from database.
91        SQL="DELETE FROM ogagent_queue WHERE id='$OPERID';"
92        [ "$OPER" == "poweroff" ] && SQL="$SQL
93UPDATE remotepc
94   SET reserved = NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL, language=NULL
95 WHERE id = '$CNLTID';
96DELETE FROM acciones
97 WHERE idordenador = '$CLNTID'
98   AND descriaccion = 'RemotePC Session';"
99        mysql --defaults-extra-file="$MYCNF" -D "$CATALOG" -Nse "$SQL"
100    done
Note: See TracBrowser for help on using the repository browser.