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/ogserver.json |
---|
17 | LOGFILE=$OPENGNSYS/log/remotepc.log |
---|
18 | |
---|
19 | source $OPENGNSYS/lib/ogfunctions.sh || exit 1 |
---|
20 | |
---|
21 | # Basic error control |
---|
22 | source_json_config $SERVERCONF 2> /dev/null || raiseError access "Server configuration file" |
---|
23 | touch "$LOGFILE" 2> /dev/null || raiseError access "Cannot write in the log file" |
---|
24 | |
---|
25 | # Reading pending operations. |
---|
26 | dbexec " |
---|
27 | SELECT ogagent_queue.id, ogagent_queue.exectime, ogagent_queue.operation, |
---|
28 | ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.language |
---|
29 | FROM ogagent_queue |
---|
30 | JOIN ordenadores ON ogagent_queue.clientid=ordenadores.idordenador |
---|
31 | JOIN remotepc ON ogagent_queue.clientid=remotepc.id |
---|
32 | WHERE exectime < NOW() |
---|
33 | ORDER BY exectime;" | \ |
---|
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. |
---|
38 | AGNTURL="https://$AGNTIP:8000/opengnsys/popup" |
---|
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. |
---|
45 | AGNTURL="https://$AGNTIP:8000/opengnsys/popup" |
---|
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. |
---|
52 | AGNTURL="https://$AGNTIP:8000/opengnsys/poweroff" |
---|
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 |
---|
63 | 000) # Client does not respond (may be halted). |
---|
64 | ;; |
---|
65 | 200) # Operation sent. |
---|
66 | echo "$(date +"%FT%T%z"): $PROG: Operation sent to OGAgent: client=$AGNTIP, oper=$OPER, exectime=\"$DATE $TIME\"" >> $LOGFILE ;; |
---|
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 |
---|
73 | # Deleting operation from the database. |
---|
74 | SQL="DELETE FROM ogagent_queue WHERE id='$OPERID';" |
---|
75 | [ "$OPER" == "poweroff" ] && SQL+=" |
---|
76 | UPDATE remotepc |
---|
77 | SET reserved = NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL, language=NULL |
---|
78 | WHERE id = '$CNLTID'; |
---|
79 | DELETE FROM acciones |
---|
80 | WHERE idordenador = '$CLNTID' |
---|
81 | AND descriaccion = 'RemotePC Session';" |
---|
82 | dbexec "$SQL" |
---|
83 | done |
---|