Update.
[scripts.git] / dns323-op.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 # A command-line tool to restart, shutdown or get the status of a
21 # D-Link DNS323.
22
23 set -e
24
25 export VT_RESET=$'\e[0m'
26 export VT_BOLD=$'\e[1m'
27 export VT_UNDERLINE=$'\e[4m'
28 export VT_BLINK=$'\e[5m'
29
30 export VT_SET_TITLE=$'\e]0;'
31 export VT_END_TITLE=$'\007'
32
33 export VT_BLACK_FG=$'\e[30m'
34 export VT_RED_FG=$'\e[31m'
35 export VT_GREEN_FG=$'\e[32m'
36 export VT_YELLOW_FG=$'\e[33m'
37 export VT_BLUE_FG=$'\e[34m'
38 export VT_MAGENTA_FG=$'\e[35m'
39 export VT_CYAN_FG=$'\e[36m'
40 export VT_WHITE_FG=$'\e[37m'
41
42 export VT_BLACK_BG=$'\e[40m'
43 export VT_RED_BG=$'\e[41m'
44 export VT_GREEN_BG=$'\e[42m'
45 export VT_YELLOW_BG=$'\e[43m'
46 export VT_BLUE_BG=$'\e[44m'
47 export VT_MAGENTA_BG=$'\e[45m'
48 export VT_CYAN_BG=$'\e[46m'
49 export VT_WHITE_BG=$'\e[47m'
50
51 function rm_temp () {
52     if [[ ${TMP} ]]; then
53         rm ${TMP}
54         unset TMP
55     fi
56 }
57
58 function authentify_on_dns323 () {
59     if [[ ! ${already_authentified} ]]; then
60         echo "Authentifying on ${DNS323_HOSTNAME}."
61         curl > /dev/null \
62             -s \
63             -L http://${DNS323_HOSTNAME}/goform/formLogin \
64             -d "f_LOGIN_NAME=admin&f_LOGIN_PASSWD=${DNS323_ADMIN_PASSWORD}&f_login_type=0" \
65             || (echo "Failed." >&2 && exit 1)
66         already_authentified=1
67     fi
68 }
69
70 function check_unmounted () {
71     if [[ ! ${force_operation} ]]; then
72         if [[ $(mount | grep ^//${DNS323_HOSTNAME}) ]]; then
73             echo "//${DNS323_HOSTNAME} appears to be mounted." >&2
74             exit 1
75         fi
76     fi
77 }
78
79 function show_help () {
80     cat >&2 <<EOF
81 $(basename $0) [-f|--force] [-h|--help] <status|restart|shutdown> ...
82
83   -h, --help     shows this help.
84   -f, --force    forces shutdown or restart even if it looks like the samba
85                  drive is mounted.
86   status         gets temperature and RAID info.
87   shutdown       shuts the unit down.
88   restart        restarts the unit.
89
90 EOF
91 }
92
93 ######################################################################
94
95 trap rm_temp SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM ERR
96
97 if [[ ${DNS323_HOSTNAME} ]] && \
98    [[ ${DNS323_ADMIN_PASSWORD} ]]; then
99
100     while [[ $1 ]]; do
101
102         case $1 in
103
104             ################################################################
105
106             -f|--force)
107                 force_operation=1
108                 ;;
109
110             ################################################################
111
112             -h|--help)
113                 show_help
114                 ;;
115
116             ################################################################
117
118             status)
119
120                 authentify_on_dns323
121
122                 # If you think what follows is fugly, please have a
123                 # look at the DNS323's web app HTML
124
125                 TMP=$(mktemp /tmp/status.XXXXXX)
126
127                 curl -s \
128                     -L http://${DNS323_HOSTNAME}/goform/adv_status \
129                     -d "" > ${TMP}
130
131                 cat ${TMP} | \
132                     html2text | \
133                     grep "\(Sync Time Remaining\|Volume Name\|Volume Type\|Total Hard Drive Capacity\|Used Space\|Unused Space\)": | \
134                     sed -e "s/^[ \t]*//" | while read line; do
135                     label=$(echo ${line} | sed -e "s/:.*$//")
136                     value=$(echo ${line} | sed -e "s/^[^0-9]*\([0-9]*\).*$/\1/")
137                     case ${label} in
138                         "Total Hard Drive Capacity")
139                             total=${value}
140                             ;;
141                         "Used Space")
142                             line="${line} ($(((value*100)/total))%)"
143                             ;;
144                         "Unused Space")
145                             line="${line} ($(((value*100)/total))%)"
146                             ;;
147                         "Sync Time Remaining")
148                             state=$(echo ${line} | sed -e "s/^[^:]*:[^A-Za-z]*//")
149                             if [[ ! "${state}" == "Completed" ]]; then
150                                 line="${VT_RED_FG}${VT_BOLD}${line}     !!! THIS IS NOT NORMAL !!!${VT_RESET}"
151                             fi
152                             ;;
153                         *)
154                             ;;
155                     esac
156                     echo "${line}"
157                 done
158
159                 grep "var temper" ${TMP} | sed -e "s/^.*\"\([0-9]*\):\([0-9]*\)\".*$/Temperature:                \1F \/ \2C/"
160
161                 rm_temp
162
163                 ;;
164
165             ################################################################
166
167             shutdown)
168
169                 authentify_on_dns323
170
171                 check_unmounted
172
173                 curl > /dev/null \
174                     -s \
175                     -L http://${DNS323_HOSTNAME}/goform/sysShutDown \
176                     -d ""
177
178                 echo "Shutdown initiated."
179
180                 ;;
181
182             ################################################################
183
184             restart)
185
186                 authentify_on_dns323
187
188                 check_unmounted
189
190                 curl > /dev/null \
191                     -s \
192                     -L http://${DNS323_HOSTNAME}/goform/System_restart \
193                     -d ""
194
195                 echo "Restart initiated."
196
197                 ;;
198
199             ################################################################
200
201             *)
202                 echo "Unknown operation $1" >&2
203                 show_help
204                 exit 1
205                 ;;
206         esac
207
208         shift
209
210     done
211
212 else
213
214     echo "Please set \$DNS323_HOSTNAME and \$DNS323_ADMIN_PASSWORD."
215
216 fi