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 |
---|
18 | MYCNF=/tmp/.my.cnf.$$ |
---|
19 | |
---|
20 | # Basic error control |
---|
21 | if [ ! -r "$SERVERCONF" ]; then |
---|
22 | echo "$PROG: Cannot access to configuration file." >&2 |
---|
23 | exit 2 |
---|
24 | fi |
---|
25 | if ! touch "$LOGFILE"; then |
---|
26 | echo "$PROG: Cannot write to log file." >&2 |
---|
27 | exit 2 |
---|
28 | fi |
---|
29 | |
---|
30 | # Fetching database access data. |
---|
31 | source "$SERVERCONF" |
---|
32 | # Composing connection credentils file. |
---|
33 | touch $MYCNF |
---|
34 | chmod 600 $MYCNF |
---|
35 | cat << EOT > $MYCNF |
---|
36 | [client] |
---|
37 | user=$USUARIO |
---|
38 | password=$PASSWORD |
---|
39 | EOT |
---|
40 | # Trap to delete temporal file if process ends. |
---|
41 | trap "rm -f $MYCNF" 0 1 2 3 6 9 15 |
---|
42 | # Reading pending operations. |
---|
43 | mysql --defaults-extra-file="$MYCNF" -D "$CATALOG" -Nse " |
---|
44 | SELECT 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 sended. |
---|
83 | echo "$(date +"%FT%T%z"): $PROG: Operation sended 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 |
---|
93 | UPDATE remotepc |
---|
94 | SET reserved = NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL, language=NULL |
---|
95 | WHERE id = '$CNLTID'; |
---|
96 | DELETE FROM acciones |
---|
97 | WHERE idordenador = '$CLNTID' |
---|
98 | AND descriaccion = 'RemotePC Session';" |
---|
99 | mysql --defaults-extra-file="$MYCNF" -D "$CATALOG" -Nse "$SQL" |
---|
100 | done |
---|