forked from netkiller/shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpd.sh
More file actions
61 lines (52 loc) · 1.98 KB
/
httpd.sh
File metadata and controls
61 lines (52 loc) · 1.98 KB
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
#!/bin/bash
############################
logfile=/www/logs/httpd.log
APACHE_HOME=/usr/local/httpd
MIN_MEMORY_LIMIT=256
httpd_script_restart="$APACHE_HOME/bin/apachectl restart"
############################
function logs(){
local logfile="$logfile.$(date -d "today" +"%Y-%m-%d")"
local timepoint=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")
local status=$1
local message=$2
echo "[$timepoint] [${status}] ${message}" >> $logfile
}
#logs 'OK' 'This is test msg!!!'
function httpd_monitor_mem(){
#memory_size=$(free -m | head -n 2 | tail -n 1 | awk '{print $(4)}')
#memory_status=$(free -m | head -n 2 | tail -n 1 | awk '{print "Mem total(M): "$2", used: "$3", free: "$4}')
memory_size=$(free -m | head -n 3 | tail -n 1 | awk '{print $(4)}')
memory_status=$(free -m | head -n 2 | tail -n 1 | awk '{print "Mem total(M): "$2", used: "$3", free: "$4}')
memory_status="${memory_status} $(free -m | head -n 3 | tail -n 1 | awk '{print "buffer: "$3" cache: "$4}')"
memory_status="${memory_status} $(free -m | head -n 4 | tail -n 1 | sed 's/\s./ /g')"
if [ ${memory_size} -lt $MIN_MEMORY_LIMIT ]; then
$httpd_script_restart
logs 'RESTART' "${memory_status}"
else
logs 'OK' "${memory_status}"
fi
}
function httpd_monitor_port(){
#curl --silent --max-time 10 --output /dev/null --write-out %{http_code} http://server:port/filename
#http_status=$(curl -o /dev/null -s -w %{http_code} -x 127.0.0.1:80 www.example.com/index.php)
http_status=$(curl -o /dev/null -s -w %{http_code} localhost/index.html)
if [ ${http_status} == 200 ]; then
logs 'OK' "${http_status}"
else
$httpd_script_restart >/dev/null 2>&1
logs 'RESTART' "${http_status}"
fi
}
##################################
while true
do
httpd_monitor_mem
sleep 5
done
#while true
#do
# httpd_monitor_port
# sleep 5
#done
##################################