Added the percentage of disk usage.
[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 [[ ${TMP} ]]; then
27         rm ${TMP}
28         unset TMP
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                 TMP=$(mktemp /tmp/status.XXXXXX)
99
100                 curl -s \
101                     -L http://${DNS323_HOSTNAME}/goform/adv_status \
102                     -d "" > ${TMP}
103
104                 cat ${TMP} | \
105                     html2text | \
106                     grep "\(Sync Time Remaining\|Volume Name\|Volume Type\|Total Hard Drive Capacity\|Used Space\|Unused Space\)": | \
107                     sed -e "s/^[ \t]*//" | while read line; do
108                     label=$(echo ${line} | sed -e "s/:.*$//")
109                     value=$(echo ${line} | sed -e "s/^[^0-9]*\([0-9]*\).*$/\1/")
110                     comment=""
111                     case ${label} in
112                         "Total Hard Drive Capacity")
113                             total=${value}
114                             ;;
115                         "Used Space")
116                             comment="($(((value*100)/total))%)"
117                             ;;
118                         "Unused Space")
119                             comment="($(((value*100)/total))%)"
120                             ;;
121                         *)
122                             ;;
123                     esac
124                     echo "${line} ${comment}"
125                 done
126
127                 grep "var temper" ${TMP} | sed -e "s/^.*\"\([0-9]*\):\([0-9]*\)\".*$/Temperature:                \1F \/ \2C/"
128
129                 rm_temp
130
131                 ;;
132
133             ################################################################
134
135             shutdown)
136
137                 authentify_on_dns323
138
139                 check_unmounted
140
141                 curl > /dev/null \
142                     -s \
143                     -L http://${DNS323_HOSTNAME}/goform/sysShutDown \
144                     -d ""
145
146                 echo "Shutdown initiated."
147
148                 ;;
149
150             ################################################################
151
152             restart)
153
154                 authentify_on_dns323
155
156                 check_unmounted
157
158                 curl -s > /dev/null \
159                     -L http://${DNS323_HOSTNAME}/goform/System_restart \
160                     -d ""
161
162                 echo "Restart initiated."
163
164                 ;;
165
166             ################################################################
167
168             *)
169                 echo "Unknown operation $1" >&2
170                 show_help
171                 exit 1
172                 ;;
173         esac
174
175         shift
176
177     done
178
179 else
180
181     echo "Please set \$DNS323_HOSTNAME and \$DNS323_ADMIN_PASSWORD."
182
183 fi