forked from cameri/iot_push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·226 lines (185 loc) · 7.16 KB
/
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
# This file is part of rpi2pachube (formerly rpi2cosm).
# Copyright (c) 2012, Ricardo Cabral <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
. utils/utils.sh
cat <<EOF
==================================================================
= rpi2pachube configuration utility =
==================================================================
EOF
which realpath &> /dev/null
if [ $? -eq 0 ]; then
realpath=$(realpath $0 2>/dev/null)
else
echo "Command 'realpath' is missing." 1>&2
echo "Install 'realpath' before running this utility." 1>&2
exit 1
fi
if [[ -f "$HOME/.rpi2pachube.conf" ]]; then
. $HOME/.rpi2pachube.conf
cat <<EOF
Current configuration:
-API Key: $api_key
-Feed: $feed
-Monitor load avg: $(bool2str "$monitor_load_avg")
-Monitor free memory: $(bool2str "$monitor_mem_free")
-Monitor used memory: $(bool2str "$monitor_mem_used")
-Monitor cached memory: $(bool2str "$monitor_mem_cached")
-Monitor temperature: $(bool2str "$monitor_temp")
-Monitor indoor temperature: $(bool2str "$monitor_temp_in")
-Monitor outdoor temperature: $(bool2str "$monitor_temp_out")
-Monitor temp. in Fahrenheit: $(bool2str "$monitor_temp_f")
-Monitor number of processes: $(bool2str "$monitor_pid_count")
-Monitor number of connections: $(bool2str "$monitor_connections")
-Monitor no. of users logged in: $(bool2str "$monitor_users")
-Monitor uptime: $(bool2str "$monitor_uptime")
-Monitor network interfaces: $(bool2str "$monitor_network_interfaces")
-Network interfaces: $network_interfaces
EOF
read_yn "Would you like to keep your current configuration? (y/n)"
if [ $? -eq 1 ]; then
echo "Nothing to do."
exit 0
fi
fi
read_s "Enter your API Key:" result "$api_key"
api_key=$result
read_s "Enter the Feed ID for this device:" result "$feed"
feed=$result
read_yn "Would you like to monitor the load average over 5 minutes? (y/n)" "$monitor_load_avg"
monitor_load_avg=$?
read_yn "Would you like to monitor free RAM memory? (y/n)" "$monitor_mem_free"
monitor_mem_free=$?
read_yn "Would you like to monitor used RAM memory? (y/n)" "$monitor_mem_used"
monitor_mem_used=$?
read_yn "Would you like to monitor cached RAM memory? (y/n)" "$monitor_mem_cached"
monitor_mem_cached=$?
read_yn "Would you like to monitor the temperature? (y/n)" "$monitor_temp"
monitor_temp=$?
read_yn "Would you like the temperature to be converted to Fahrenheit? (y/n)" "$monitor_temp_f"
monitor_temp_f=$?
read_yn "Would you like to monitor the number of processes? (y/n)" "$monitor_pid_count"
monitor_pid_count=$?
read_yn "Would you like to monitor the number of active TCP/UDP connections? (y/n)" "$monitor_connections"
monitor_connections=$?
read_yn "Would you like to monitor the number of users logged in? (y/n)" "$monitor_users"
monitor_users=$?
read_yn "Would you like to monitor the uptime? (y/n)" "$monitor_uptime"
monitor_uptime=$?
read_yn "Would you like to monitor indoor temperature with a 1-wire sensor? (y/n)" "$monitor_temp_in"
monitor_temp_in=$?
if [ "${monitor_temp_in:-0}" -eq 1 ]; then
echo "Please select the 1-wire device:"
monitor_temp_in_dev=`read_w1_dev`
fi
read_yn "Would you like to monitor the outdoor temperature with a 1-wire sensor? (y/n)" "$monitor_temp_out"
monitor_temp_out=$?
if [ "${monitor_temp_out:-0}" -eq 1 ]; then
echo "Please select the 1-wire device:"
monitor_temp_out_dev=`read_w1_dev`
fi
# Check that ifstat command is installed
which ifstat &>/dev/null
if [ $? -eq 0 ]; then
read_yn "Would you like to monitor any network interfaces? (y/n)" "$monitor_network_interfaces"
monitor_network_interfaces=$?
if [ $monitor_network_interfaces -eq 1 ]; then
avail_ifaces=$(get_interfaces);
read_s "Enter a comma-separated list of network interfaces ($avail_ifaces):" result "$network_interfaces"
network_interfaces=$result
fi
else
monitor_network_interfaces=0
echo "WARNING: ifstat command not found. Unable to monitor network
interfaces. Press Enter to continue."
read
fi
# Prompt user to back up existing configuration, if any
if [[ -f "$HOME/.rpi2pachube.conf" ]]; then
read_yn "Configuration file already exists. Would you like to make a back up first? (y/n)"
if [ $? -eq 1 ]; then
echo "Moved old configuration to $HOME/.rpi2pachube.conf.backup"
cp $HOME/.rpi2pachube.conf $HOME/.rpi2pachube.conf.backup
fi
fi
cat <<EOF > /tmp/rpi2pachube.conf
# Generated by rpi2pachube configuration utility
# from $realpath
# on `date` by `id -un`
# Feel free to modify this file or use the configuration utility.
# API Key
api_key=$api_key
# Feed ID
feed=$feed
# Monitor load average
monitor_load_avg=$monitor_load_avg
# Monitor free RAM memory
monitor_mem_free=$monitor_mem_free
# Monitor used RAM memory
monitor_mem_used=$monitor_mem_used
# Monitor cached RAM memory
monitor_mem_cached=$monitor_mem_cached
# Monitor the temperature
monitor_temp=$monitor_temp
monitor_temp_f=$monitor_temp_f
# Monitor the 1-wire sensors
monitor_temp_in=$monitor_temp_in
monitor_temp_in_dev=$monitor_temp_in_dev
monitor_temp_out=$monitor_temp_out
monitor_temp_out_dev=$monitor_temp_out_dev
# Monitor the number of processes
monitor_pid_count=$monitor_pid_count
# Monitor the number of active TCP/UDP connections
monitor_connections=$monitor_connections
# Monitor the number of users logged in
monitor_users=$monitor_users
# Monitor the uptime
monitor_uptime=$monitor_uptime
# Monitor monitor_network_interfaces
monitor_network_interfaces=$monitor_network_interfaces
# Network Interfaces
network_interfaces=$network_interfaces
EOF
# Back up old configuration
if [ -f "$HOME/.rpi2pachube.conf" ]; then
mv "$HOME/.rpi2pachube.conf" "$HOME/.rpi2pachube.conf.backup"
fi
# Write new configuration
echo "Writing new configuration to $HOME/.rpi2pachube.conf..."
mv /tmp/rpi2pachube.conf $HOME/.rpi2pachube.conf
if [[ $? -eq 0 ]]; then
echo "Configuration file saved to $HOME/.rpi2pachube.conf"
else
echo "Unable to save configuration file to $HOME/.rpi2pachube.conf" 1>&2
exit 1
fi
echo "Updating crontab..."
# Get crontab without any entries to rpi2pachube and save to temporary file
crontab -l 2>/dev/null | grep -v rpi2pachube > /tmp/crontab
# Get the directory where setup utility is; assuming rpi2pachube.sh is there too
dirname=`dirname $realpath`
# Add a new entry to our crontab
echo "*/5 * * * * ${dirname}/rpi2pachube.sh" >> /tmp/crontab
# Replace current crontab with our version
crontab /tmp/crontab
# Show it to the user
#env EDITOR=nano crontab -e
echo "Displaying `id -un`'s crontab:"
crontab -l
echo "Run 'crontab -e' if you see any problem with your crontab."
echo "Done."
# Exit
exit 0