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