[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 |
---|
[4377223] | 17 | |
---|
| 18 | source $OPENGNSYS/lib/ogfunctions.sh || exit 1 |
---|
[0645906] | 19 | |
---|
| 20 | # Basic error control |
---|
[4377223] | 21 | source $SERVERCONF 2> /dev/null || raiseError access "Server configuration file" |
---|
[0645906] | 22 | |
---|
| 23 | # Reading pending operations. |
---|
[4377223] | 24 | dbexec " |
---|
[0645906] | 25 | SELECT ogagent_queue.id, ogagent_queue.exectime, ogagent_queue.operation, |
---|
[b46042c] | 26 | ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.language |
---|
[0645906] | 27 | FROM ogagent_queue |
---|
| 28 | JOIN ordenadores ON ogagent_queue.clientid=ordenadores.idordenador |
---|
[b46042c] | 29 | JOIN remotepc ON ogagent_queue.clientid=remotepc.id |
---|
[0645906] | 30 | WHERE exectime < NOW() |
---|
| 31 | ORDER BY exectime;" | \ |
---|
[b46042c] | 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. |
---|
[4377223] | 36 | AGNTURL="https://$AGNTIP:8000/opengnsys/popup" |
---|
[b46042c] | 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. |
---|
[4377223] | 43 | AGNTURL="https://$AGNTIP:8000/opengnsys/popup" |
---|
[b46042c] | 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. |
---|
[4377223] | 50 | AGNTURL="https://$AGNTIP:8000/opengnsys/poweroff" |
---|
[b46042c] | 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 |
---|
[703551b3] | 61 | 000) # Client does not respond (may be halted). |
---|
[b46042c] | 62 | ;; |
---|
[703551b3] | 63 | 200) # Operation sent. |
---|
[c28eefa] | 64 | echolog "Operation sent to OGAgent: client=$AGNTIP, oper=$OPER, exectime=\"$DATE $TIME\"" ;; |
---|
[b46042c] | 65 | *) # Operation error. |
---|
[c28eefa] | 66 | echolog "Operation error: client=$AGNTIP, oper=$OPER, code=$CODE" ;; |
---|
[b46042c] | 67 | esac |
---|
| 68 | else # Unknown operation. |
---|
[c28eefa] | 69 | echolog "Unknown operation: client=$AGNTIP, oper=$OPER" |
---|
[b46042c] | 70 | fi |
---|
[4377223] | 71 | # Deleting operation from the database. |
---|
[b46042c] | 72 | SQL="DELETE FROM ogagent_queue WHERE id='$OPERID';" |
---|
[4377223] | 73 | [ "$OPER" == "poweroff" ] && SQL+=" |
---|
[0645906] | 74 | UPDATE remotepc |
---|
[b46042c] | 75 | SET reserved = NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL, language=NULL |
---|
[0645906] | 76 | WHERE id = '$CNLTID'; |
---|
| 77 | DELETE FROM acciones |
---|
| 78 | WHERE idordenador = '$CLNTID' |
---|
| 79 | AND descriaccion = 'RemotePC Session';" |
---|
[4377223] | 80 | dbexec "$SQL" |
---|
[b46042c] | 81 | done |
---|