Miscellaneous updates.
[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 function rm_temp () {
26     if [[ ${TEMP} ]]; then
27         rm ${TEMP}
28         unset TEMP
29     fi
30 }
31
32 function authentify_on_dns323 () {
33     if [[ ! ${already_authentified} ]]; then
34         echo "Authentifying on ${DNS323_HOSTNAME}."
35         curl -s > /dev/null \
36             -L http://${DNS323_HOSTNAME}/goform/formLogin \
37             -d "f_LOGIN_NAME=admin&f_LOGIN_PASSWD=${DNS323_ADMIN_PASSWORD}&f_login_type=0" \
38             || (echo "Failed." >&2 && exit 1)
39         already_authentified=1
40     fi
41 }
42
43 function check_unmounted () {
44     if [[ ! ${force_operation} ]]; then
45         if [[ $(mount | grep ^//${DNS323_HOSTNAME}) ]]; then
46             echo "//${DNS323_HOSTNAME} appears to be mounted." >&2
47             exit 1
48         fi
49     fi
50 }
51
52 function show_help () {
53     cat >&2 <<EOF
54 $(basename $0) [-f|--force] [-h|--help] <status|restart|shutdown> ...
55
56   -h, --help     shows this help.
57   -f, --force    forces shutdown or restart even if it looks like the samba
58                  drive is mounted.
59   status         gets temperature and RAID info.
60   shutdown       shuts the unit down.
61   restart        restarts the unit.
62
63 EOF
64 }
65
66 ######################################################################
67
68 trap rm_temp SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM ERR
69
70 if [[ ${DNS323_HOSTNAME} ]] && \
71     [[ ${DNS323_ADMIN_PASSWORD} ]]; then
72
73     while [[ $1 ]]; do
74
75         case $1 in
76
77             ################################################################
78
79             -f|--force)
80                 force_operation=1
81                 ;;
82
83             ################################################################
84
85             -h|--help)
86                 show_help
87                 ;;
88
89             ################################################################
90
91             status)
92
93                 authentify_on_dns323
94
95                 # If you think what follows is fugly, please have a
96                 # look at the DNS323's web app HTML
97
98                 TEMP=$(mktemp /tmp/status.XXXXXX)
99
100                 curl -s \
101                     -L http://${DNS323_HOSTNAME}/goform/adv_status \
102                     -d "" > ${TEMP}
103
104                 cat ${TEMP} | \
105                     html2text | \
106                     grep "\(Sync Time Remaining\|Volume Name\|Volume Type\|Total Hard Drive Capacity\|Used Space\|Unused Space\)": | \
107                     sed -e "s/^[ \t]*//"
108
109                 grep "var temper" ${TEMP} | sed -e "s/^.*\"\([0-9]*\):\([0-9]*\)\".*$/Temperature:                \1F \/ \2C/"
110
111                 rm_temp
112
113                 ;;
114
115             ################################################################
116
117             shutdown)
118
119                 authentify_on_dns323
120
121                 check_unmounted
122
123                 curl > /dev/null \
124                     -s \
125                     -L http://${DNS323_HOSTNAME}/goform/sysShutDown \
126                     -d ""
127
128                 echo "Shutdown initiated."
129
130                 ;;
131
132             ################################################################
133
134             restart)
135
136                 authentify_on_dns323
137
138                 check_unmounted
139
140                 curl -s > /dev/null \
141                     -L http://${DNS323_HOSTNAME}/goform/System_restart \
142                     -d ""
143
144                 echo "Restart initiated."
145
146                 ;;
147
148             ################################################################
149
150             *)
151                 echo "Unknown operation $1" >&2
152                 show_help
153                 exit 1
154                 ;;
155         esac
156
157         shift
158
159     done
160
161 else
162
163     echo "Please set \$DNS323_HOSTNAME and \$DNS323_ADMIN_PASSWORD."
164
165 fi