[0645906] | 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. |
---|
| 14 | PROG=$(basename "$0") |
---|
| 15 | OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} |
---|
| 16 | SERVERCONF=$OPENGNSYS/etc/ogAdmServer.cfg |
---|
| 17 | LOGFILE=$OPENGNSYS/log/remotepc.log |
---|
[4377223] | 18 | |
---|
| 19 | source $OPENGNSYS/lib/ogfunctions.sh || exit 1 |
---|
[0645906] | 20 | |
---|
| 21 | # Basic error control |
---|
[4377223] | 22 | source $SERVERCONF 2> /dev/null || raiseError access "Server configuration file" |
---|
| 23 | touch "$LOGFILE" 2> /dev/null || raiseError access "Cannot write in the log file" |
---|
[0645906] | 24 | |
---|
| 25 | # Reading pending operations. |
---|
[4377223] | 26 | dbexec " |
---|
[0645906] | 27 | SELECT ogagent_queue.id, ogagent_queue.exectime, ogagent_queue.operation, |
---|
[b46042c] | 28 | ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.language |
---|
[0645906] | 29 | FROM ogagent_queue |
---|
| 30 | JOIN ordenadores ON ogagent_queue.clientid=ordenadores.idordenador |
---|
[b46042c] | 31 | JOIN remotepc ON ogagent_queue.clientid=remotepc.id |
---|
[0645906] | 32 | WHERE exectime < NOW() |
---|
| 33 | ORDER BY exectime;" | \ |
---|
[b46042c] | 34 | while read -r OPERID DATE TIME OPER CLNTID AGNTIP AGNTKEY LANGUAGE; do |
---|
| 35 | # Preparing operation data. |
---|
| 36 | case "$OPER" in |
---|
| 37 | popup-10) # Message: 10 min. before power off. |
---|
[4377223] | 38 | AGNTURL="https://$AGNTIP:8000/opengnsys/popup" |
---|
[b46042c] | 39 | case "$LANGUAGE" in |
---|
| 40 | es) DATA='{"title":"Apagado en 10 min.","message":"Fin del tiempo de acceso remoto.\nEl ordenador se apagará automáticamente dentro de 10 minutos."}' ;; |
---|
| 41 | *) DATA='{"title":"Shutdown after 10 min.","message":"Remote access time is ended.\nComputer will be powered off automaticly after 10 minutes."}' ;; |
---|
| 42 | esac |
---|
| 43 | ;; |
---|
| 44 | popup-5) # Message: 5 min. before power off. |
---|
[4377223] | 45 | AGNTURL="https://$AGNTIP:8000/opengnsys/popup" |
---|
[b46042c] | 46 | case "$LANGUAGE" in |
---|
| 47 | 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."}' ;; |
---|
| 48 | *) DATA='{"title":"Shutdown after 5 min.","message":"The computer will be powered off automaticly after 5 minutes.\nATTENTION: This is the last warning."}' |
---|
| 49 | esac |
---|
| 50 | ;; |
---|
| 51 | poweroff) # Power off client. |
---|
[4377223] | 52 | AGNTURL="https://$AGNTIP:8000/opengnsys/poweroff" |
---|
[b46042c] | 53 | DATA= |
---|
| 54 | ;; |
---|
| 55 | *) # Unknown operation. |
---|
| 56 | AGNTURL= |
---|
| 57 | ;; |
---|
| 58 | esac |
---|
| 59 | # Sending operation to OGAgent. |
---|
| 60 | if [ -n "$AGNTURL" ]; then |
---|
| 61 | CODE=$(curl -ksm 1 -w "%{http_code}" -o /dev/null -H "Authorization: $AGNTKEY" ${DATA:+"-d $DATA"} "$AGNTURL") |
---|
| 62 | case "$CODE" in |
---|
[703551b3] | 63 | 000) # Client does not respond (may be halted). |
---|
[b46042c] | 64 | ;; |
---|
[703551b3] | 65 | 200) # Operation sent. |
---|
| 66 | echo "$(date +"%FT%T%z"): $PROG: Operation sent to OGAgent: client=$AGNTIP, oper=$OPER, exectime=\"$DATE $TIME\"" >> $LOGFILE ;; |
---|
[b46042c] | 67 | *) # Operation error. |
---|
| 68 | echo "$(date +"%FT%T%z"): $PROG: Operation error: client=$AGNTIP, oper=$OPER, code=$CODE" >> $LOGFILE ;; |
---|
| 69 | esac |
---|
| 70 | else # Unknown operation. |
---|
| 71 | echo "$(date +"%FT%T%z"): $PROG: Unknown operation: client=$AGNTIP, oper=$OPER" >> $LOGFILE |
---|
| 72 | fi |
---|
[4377223] | 73 | # Deleting operation from the database. |
---|
[b46042c] | 74 | SQL="DELETE FROM ogagent_queue WHERE id='$OPERID';" |
---|
[4377223] | 75 | [ "$OPER" == "poweroff" ] && SQL+=" |
---|
[0645906] | 76 | UPDATE remotepc |
---|
[b46042c] | 77 | SET reserved = NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL, language=NULL |
---|
[0645906] | 78 | WHERE id = '$CNLTID'; |
---|
| 79 | DELETE FROM acciones |
---|
| 80 | WHERE idordenador = '$CLNTID' |
---|
| 81 | AND descriaccion = 'RemotePC Session';" |
---|
[4377223] | 82 | dbexec "$SQL" |
---|
[b46042c] | 83 | done |
---|