Added a printing of what is done.
[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 -s > /dev/null \
62             -L http://${DNS323_HOSTNAME}/goform/formLogin \
63             -d "f_LOGIN_NAME=admin&f_LOGIN_PASSWD=${DNS323_ADMIN_PASSWORD}&f_login_type=0" \
64             || (echo "Failed." >&2 && exit 1)
65         already_authentified=1
66     fi
67 }
68
69 function check_unmounted () {
70     if [[ ! ${force_operation} ]]; then
71         if [[ $(mount | grep ^//${DNS323_HOSTNAME}) ]]; then
72             echo "//${DNS323_HOSTNAME} appears to be mounted." >&2
73             exit 1
74         fi
75     fi
76 }
77
78 function show_help () {
79     cat >&2 <<EOF
80 $(basename $0) [-f|--force] [-h|--help] <status|restart|shutdown> ...
81
82   -h, --help     shows this help.
83   -f, --force    forces shutdown or restart even if it looks like the samba
84                  drive is mounted.
85   status         gets temperature and RAID info.
86   shutdown       shuts the unit down.
87   restart        restarts the unit.
88
89 EOF
90 }
91
92 ######################################################################
93
94 trap rm_temp SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM ERR
95
96 if [[ ${DNS323_HOSTNAME} ]] && \
97     [[ ${DNS323_ADMIN_PASSWORD} ]]; then
98
99     while [[ $1 ]]; do
100
101         case $1 in
102
103             ################################################################
104
105             -f|--force)
106                 force_operation=1
107                 ;;
108
109             ################################################################
110
111             -h|--help)
112                 show_help
113                 ;;
114
115             ################################################################
116
117             status)
118
119                 authentify_on_dns323
120
121                 # If you think what follows is fugly, please have a
122                 # look at the DNS323's web app HTML
123
124                 TMP=$(mktemp /tmp/status.XXXXXX)
125
126                 curl -s \
127                     -L http://${DNS323_HOSTNAME}/goform/adv_status \
128                     -d "" > ${TMP}
129
130                 cat ${TMP} | \
131                     html2text | \
132                     grep "\(Sync Time Remaining\|Volume Name\|Volume Type\|Total Hard Drive Capacity\|Used Space\|Unused Space\)": | \
133                     sed -e "s/^[ \t]*//" | while read line; do
134                     label=$(echo ${line} | sed -e "s/:.*$//")
135                     value=$(echo ${line} | sed -e "s/^[^0-9]*\([0-9]*\).*$/\1/")
136                     case ${label} in
137                         "Total Hard Drive Capacity")
138                             total=${value}
139                             ;;
140                         "Used Space")
141                             line="${line} ($(((value*100)/total))%)"
142                             ;;
143                         "Unused Space")
144                             line="${line} ($(((value*100)/total))%)"
145                             ;;
146                         "Sync Time Remaining")
147                             state=$(echo ${line} | sed -e "s/^[^:]*:[^A-Za-z]*//")
148                             if [[ ! "${state}" == "Completed" ]]; then
149                                 line="${VT_RED_FG}${VT_BOLD}${line}     !!! THIS IS NOT NORMAL !!!${VT_RESET}"
150                             fi
151                             ;;
152                         *)
153                             ;;
154                     esac
155                     echo "${line}"
156                 done
157
158                 grep "var temper" ${TMP} | sed -e "s/^.*\"\([0-9]*\):\([0-9]*\)\".*$/Temperature:                \1F \/ \2C/"
159
160                 rm_temp
161
162                 ;;
163
164             ################################################################
165
166             shutdown)
167
168                 authentify_on_dns323
169
170                 check_unmounted
171
172                 curl > /dev/null \
173                     -s \
174                     -L http://${DNS323_HOSTNAME}/goform/sysShutDown \
175                     -d ""
176
177                 echo "Shutdown initiated."
178
179                 ;;
180
181             ################################################################
182
183             restart)
184
185                 authentify_on_dns323
186
187                 check_unmounted
188
189                 curl -s > /dev/null \
190                     -L http://${DNS323_HOSTNAME}/goform/System_restart \
191                     -d ""
192
193                 echo "Restart initiated."
194
195                 ;;
196
197             ################################################################
198
199             *)
200                 echo "Unknown operation $1" >&2
201                 show_help
202                 exit 1
203                 ;;
204         esac
205
206         shift
207
208     done
209
210 else
211
212     echo "Please set \$DNS323_HOSTNAME and \$DNS323_ADMIN_PASSWORD."
213
214 fi