-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-status.sh
executable file
·287 lines (223 loc) · 9.44 KB
/
update-status.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
# Author: Ashley Cawley // @ashleycawley // [email protected]
## Variables
# User Editable Variables:
REMOTESERVER="status.ashleycawley.co.uk" # The hostname of your Remote Web Server
USERNAME=pistatus # This is the username on the Remote Server
SERVERPATH="/home/$USERNAME/public_html/pies/" # This is the exact folder path to publish to on the remote server
PROGRAMPATH=/usr/local/bin # The location of where the program is installed (it will install itself there)
PROGNAME=update-status.sh # The name of the script, update as required if you rename it
RANDOMDELAY=30 # Introduces a random delay between 0 and the number specified to avoid many systems uploading to the central server at the same time
SLEEP="sleep 0"
SSHKEY=(`grep -l -r "net8xYdLIdXouObZos3Zn8V" ~/.ssh/`)
# Variables which should probably remain untouched:
TEMP=/tmp
WANIPLOOKUP="$(curl -s http://whatismyip.akamai.com/)"
MAC=$(sudo cat /sys/class/net/wlan0/address 2>/dev/null)
TEMPLATE=('echo <html>
<head>
</head>
<body>
<font face="verdana">
<h1>HOSTNAME</h1>
<p align="left">
<font size="2">
Reporting in on DATE</br></br>
ONLINEFOR</br></br>
<strong>Load:</strong> LOADAVG</br></br>
<b>Users currently logged on:</b> USERSLOGGEDON</br></br>
<b>LAN IP:</b> LOCALIP</br></br>
<b>WAN IP:</b> WANIP</br></br>
<b>MAC Address:</b> MACADDRESS</br></br>
<b>Temperatures</b></br>
<br>
CPUTEMP
<br>
GPUTEMP
</br></br>
<b>Free Disk Space:</b> FREEDISK
</br></br>
<b>System Information:</b> SYSINFO
</p>
</font>
</body>
</html>')
## Functions
# Looks up the local IP address
function LANIP {
hostname -I | awk '/192/ {print $1;}'
}
# Creates the Log Folder
function CREATELOGFOLDER {
mkdir -p $HOME/$PROJECTPATH/logs
}
# Installs the script into the crontab
function INSTALLCRON {
echo "Saving the current crontab..." && echo
crontab -l > /tmp/crons
echo "Updating file to contain new CRON task..." && echo
echo "0 * * * * bash $PROGRAMPATH/$PROGNAME" >> /tmp/crons
echo "Installing new CRON task file in to crontab" && echo
crontab /tmp/crons
echo "Removing temporary CRON file..." && echo
rm /tmp/crons
}
# Initiates SSH Key into memory
function INITIATE_SSH_KEY {
eval $(ssh-agent)
ssh-add $SSHKEY
}
############
## Script ##
############
# Performs test to see if Private Key is installed for Remote Server communication
grep -r "net8xYdLIdXouObZos3Zn8V" ~/.ssh/ &>/dev/null
if [ $? == '1' ]
then
echo
echo "### WARNING ###"
echo "Private key is not installed, this script cannot communicate with the remote server."
echo "Please install the private SSH key at $HOME/.ssh/id_rsa"
echo
exit 1
fi
# Initiates SSH Key into memory
INITIATE_SSH_KEY
###############################################################################################
# This is my Installer Sub-Script, the above performs a test to see if the program is already #
# installed if it is not then it performs the below to install it, then continues on. #
###############################################################################################
until [ -e $PROGRAMPATH/$PROGNAME ]
do
if
[ `whoami` == pi ]
then
echo && echo "----------------------------------------------" && echo
echo "It has been detected that the $PROGNAME script is not presently installed on this system."
echo "We are proceeding to install it now for future use..."
sleep 5 && echo
echo "Copying the program to $PROGRAMPATH"
sudo cp -f $0 $PROGRAMPATH/
sudo chmod +x $PROGRAMPATH/$PROGNAME
sleep 2 && echo
# CREATELOGFOLDER # This function creates the log folder, the function is declared toward the top.
echo "Installing CRON task..." && echo && sleep 2
INSTALLCRON # Uses the INSTALLCRON function which is declared toward the top of this script.
fi
done
####################
## Installed Ends ##
####################
##################################
## Normal Runtime Script Begins ##
##################################
echo "----------------------------------------------" && echo
echo "Status Update Script Starting on `date`"
# CREATELOGFOLDER # This function creates the log folder, the function is declared toward the top.
echo && $SLEEP
# This introduces a random delay in to the mix so that multiple nodes do not interfer
# with one another by trying to update the files on the central server at the same time.
echo "Introducing a random delay which could be anywhere from 1-$RANDOMDELAY seconds"
sleep $((RANDOM % $RANDOMDELAY))
echo && $SLEEP
# Tests to see if the script is already in the crontab and if not installs it in there
crontab -l | grep "$PROGNAME" 1>/dev/null
# Tests the exit status code from the command above, if it equals 1 it installs cron.
if [ $? = 1 ]
then
INSTALLCRON # Uses the INSTALLCRON function which is declared toward the top of this script.
fi
# Creates Temporary working folder
echo "Creating Temporary Working Area..."
mkdir -p /tmp/status/
echo && $SLEEP
# Calculating CPU & GPU Temperatures & storing them for later use.
echo "Calculating CPU & GPU Temperatures"
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % $cpuTemp1))
gpuTemp0=$(/opt/vc/bin/vcgencmd measure_temp)
gpuTemp0=${gpuTemp0//\'/}
gpuTemp0=${gpuTemp0//temp=/}
echo CPU: $cpuTemp1"."$cpuTempM"C" > $TEMP/CPUTEMP
echo GPU: $gpuTemp0 > $TEMP/GPUTEMP
echo && $SLEEP
# Copies template.html to index.html and then updates index.html with stats info.
echo "Performing Search & Replace to update Template and build HTML status page..."
rm -f $TEMP/index.html # Performing clean-up of files potentially left over from last run.
# Outputs the Template HTML page from the TEMPLATE varaible into a file in /tmp/index.html
$TEMPLATE > $TEMP/index.html
# Modifies the file by doing a search and replace filling in the attributes with up to date stats
sed -i s/HOSTNAME/`hostname`/g $TEMP/index.html # Sets Hostname
sed -i s/DATE/"`date +"%T %a %d %b %Y"`"/g $TEMP/index.html # Sets Date
uptime -p > $TEMP/ONLINEFOR
sed -i s/up/"Online for"/g $TEMP/ONLINEFOR # Sets Uptime / Online for x hours and seconds.
sed -i s/ONLINEFOR/"`cat $TEMP/ONLINEFOR`"/g $TEMP/index.html
rm -f $TEMP/ONLINEFOR
sed -i s/LOADAVG/`uptime | awk '/load average:/ { print $10 $11 $12 }'`/g $TEMP/index.html
sed -i s/MACADDRESS/`echo $MAC`/g $TEMP/index.html # Sets MAC Address
sed -i s/CPUTEMP/"`cat $TEMP/CPUTEMP`"/g $TEMP/index.html # Sets CPU Temp
rm -f $TEMP/CPUTEMP
sed -i s/GPUTEMP/"`cat $TEMP/GPUTEMP`"/g $TEMP/index.html # Sets GPU Temp
rm -f $TEMP/GPUTEMP
sed -i s/LOCALIP/`LANIP`/g $TEMP/index.html
sed -i "s|SYSINFO|`uname -a`|g" $TEMP/index.html
rm -f $TEMP/SYSINFO
sed -i s/USERSLOGGEDON/"`users`"/g $TEMP/index.html # Shows logged on users.
sed -i s/WANIP/`host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'`/g $TEMP/index.html
sed -i "s|FREEDISK|`df -h | grep -vE '^Filesystem|tmpfs|cdrom|mmcblk0p1' | awk '{ print $4 " " $1 }'`|g" $TEMP/index.html
echo && $SLEEP
# Syncronises copy of Remote Files to Local Temporary Working Directory at /tmp/status
echo "Syncronising Files on Remote Server to Local Temp Directory at /tmp/status/"
rsync --progress -av -e 'ssh -p 2223 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' $USERNAME@$REMOTESERVER:$SERVERPATH /tmp/status/
echo && $SLEEP
# Performing a test to see if a status update from this system already exists
grep -rl `hostname` /tmp/status/
# The test below checks to see if the exit status code from the command above is either 0 or 1
if [ $? = 0 ]
then
echo "A file on the Remote Server already contains a status update, I will overwrite that file..."
# Retrieves the Existing Filename from the Remote Server to use Later
EXISTING_FILENAME=$(grep -rl `hostname` /tmp/status/ | awk -F '/' '{ print $4 }')
echo && $SLEEP
echo "The existing filename is:"
echo "$EXISTING_FILENAME"
echo && $SLEEP
# Uploads status webpage to the Remote Server
echo "Uploading HTML page to $REMOTESERVER"
scp -P 2223 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $TEMP/index.html $USERNAME@$REMOTESERVER:/home/$USERNAME/public_html/pies/`echo $EXISTING_FILENAME` 2>/dev/null
echo && $SLEEP
# Clean up temporary working area
echo "Cleaning up temporary working area..."
rm -fr /tmp/status/
rm -f $TEMP/index.html
echo && $SLEEP
echo "Script has finished."
echo && echo "----------------------------------------------"
exit 0
else
echo "The server does not contain a previous status update from this system. Creating a new file..."
# Calculates the number of files stored on the Remote Server & Stores is in the COUNTER variable
echo "Looking at the number of status files held on the Remote Server..."
COUNTER=`ls -1 /tmp/status/ | wc -l`
echo && $SLEEP
# Increments the COUNTER variable by +1
echo "On the server there are $COUNTER files"
((COUNTER=COUNTER+1))
echo "Add 1 to that and now you have $COUNTER"
echo && $SLEEP
# Uploads status webpage to the Remote Server
echo "Uploading HTML page to $REMOTESERVER"
scp -P 2223 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $TEMP/index.html $USERNAME@$REMOTESERVER:/home/$USERNAME/public_html/pies/pi$COUNTER.html 2>/dev/null
echo && $SLEEP
# Clean up temporary working area
echo "Cleaning up temporary working area..."
rm -fr /tmp/status/
rm -f $TEMP/index.html
echo && $SLEEP
echo "Script has finished."
echo && echo "----------------------------------------------"
exit 0
fi
exit 0