Skip to content

Commit 2fb24c2

Browse files
author
Beau
committed
initial commit
1 parent fa3d124 commit 2fb24c2

14 files changed

+1687
-0
lines changed

check_apache_stats.sh

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#!/bin/bash
2+
3+
##
4+
## Author: Beau Bilyeu ([email protected])
5+
## Usage: ./check_apache_status.sh [host=127.0.0.1] [port=80] [uri=/server-status?auto]
6+
##
7+
## (none) = 127.0.0.1:80/server-status?auto
8+
## host = $host:80/server-status?auto
9+
## port = $host:$port/server-status?auto
10+
## uri = $host:$port/$uri
11+
##
12+
## Noteable Reference: https://httpd.apache.org/docs/2.4/mod/mod_status.html
13+
##
14+
15+
helpArgs="/(-h|-\?|--help|--\?)/i"
16+
trueArgs="/(t|1|True)/i"
17+
falseArgs="/(f|0|False)/i"
18+
portStatus="open"
19+
20+
## process arguments, or default values
21+
host=""
22+
if [[ "$1" ]]; then host="$1"; else host="127.0.0.1"; fi
23+
port=""
24+
if [[ "$2" ]]; then port="$2"; else port="80"; fi
25+
uri=""
26+
if [[ "$3" ]]; then uri="$3"; else uri="/server-status?auto"; fi
27+
28+
#echo "DEBUG: host [$host], port [$port], uri [$uri], 1 [$1], 2 [$2], 3 [$3]"
29+
30+
## print help string and exit
31+
printHelp()
32+
{
33+
echo -e "Usage: $0 [host=127.0.0.1] [port=80] [uri=/server-status?auto]\n\t(no parameters) = 127.0.0.1:80/server-status?auto\n\thost = \$host:80/server-status?auto\n\tport = \$host:\$port/server-status?auto\n\turi = \$host:\$port/\$uri\n"
34+
}
35+
36+
## ensure we have a valid hostname/IP and port
37+
if [[ $1 =~ $helpArgs ]]; then
38+
printHelp
39+
exit 0
40+
elif [ -z "$host" ] || [ "$host" == "" ]; then
41+
echo -e "UNKNOWN: Blank hostname or IP address passed.\n"
42+
printHelp
43+
exit 3
44+
elif [ -z "$port" ] || [ "$port" == "" ]; then
45+
echo -e "UNKNOWN: Blank port passed. \n"
46+
printHelp
47+
exit 3
48+
fi
49+
50+
## check port
51+
{ exec 3<>"/dev/tcp/$host/$port"; } > /dev/null 2>&1 || portStatus="closed"
52+
53+
if [ "$portStatus" == "open" ]; then
54+
## check http status code
55+
retCode=$(curl -s -o /dev/null -w "%{http_code}" "http://$host:$port/$uri")
56+
if [ "$retCode" == "200" ]; then
57+
stats=$(curl -s "http://$host:$port/$uri")
58+
IFS=$'\n'; read -r -d '' -a row <<< "$stats"
59+
60+
# 1. Parse key:value pairs
61+
# 2. Parse "Scoreboard: ..." string, counting symbols (. _ S R W K D C L G I) and total length
62+
63+
for r in "${row[@]}"; do
64+
IFS=' ' read -r -a field <<< "$r"
65+
66+
if [[ $r =~ 'Total Accesses' ]]; then
67+
value="${field[2]// }"
68+
perfData="${perfData} totalAccesses=$value;"
69+
elif [[ $r =~ 'Total kBytes' ]]; then
70+
value="${field[2]// }"
71+
perfData="${perfData} totalKBs=${value}KB;"
72+
elif [[ $r =~ 'CPULoad' ]]; then
73+
value="${field[1]// }"
74+
value=$(bc <<< " scale=2; $value * 100.0 " 2>/dev/null)
75+
value=$(printf %0.2f "$value")
76+
perfData="${perfData} cpuLoad=$value%;"
77+
elif [[ $r =~ 'Uptime' ]]; then
78+
value="${field[1]// }"
79+
perfData="${perfData} uptime=${value}s;"
80+
elif [[ $r =~ 'ReqPerSec' ]]; then
81+
value="${field[1]// }"
82+
value=$(printf %0.2f "$value")
83+
perfData="${perfData} requestsPerSec=$value;"
84+
elif [[ $r =~ 'BytesPerSec' ]]; then
85+
value="${field[1]// }"
86+
value=$(printf %0.2f "$value")
87+
perfData="${perfData} bytesPerSec=${value}B;"
88+
elif [[ $r =~ 'BytesPerReq' ]]; then
89+
value="${field[1]// }"
90+
value=$(printf %0.2f "$value")
91+
perfData="${perfData} bytesPerRequest=${value}B;"
92+
elif [[ $r =~ 'BusyWorkers' ]]; then
93+
value="${field[1]// }"
94+
perfData="${perfData} busyWorkers=$value;"
95+
elif [[ $r =~ 'IdleWorkers' ]]; then
96+
value="${field[1]// }"
97+
perfData="${perfData} idleWorkers=$value;"
98+
elif [[ ${field[0]} =~ Scoreboard ]]; then
99+
sb="${field[1]// }"
100+
101+
workersOpen=$(tr -dc '.' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
102+
if [ -z "$workersOpen" ] || [ "$workersOpen" == "" ]; then workersOpen=0; fi
103+
perfData="${perfData} workersOpen=$workersOpen;"
104+
105+
workersWaiting=$(tr -dc '_' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
106+
if [ -z "$workersWaiting" ] || [ "$workersWaiting" == "" ]; then workersWaiting=0; fi
107+
perfData="${perfData} workersWaiting=$workersWaiting;"
108+
109+
workersStarting=$(tr -dc 'S' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
110+
if [ -z "$workersStarting" ] || [ "$workersStarting" == "" ]; then workersStarting=0; fi
111+
perfData="${perfData} workersStarting=$workersStarting;"
112+
113+
workersReading=$(tr -dc 'R' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
114+
if [ -z "$workersReading" ] || [ "$workersReading" == "" ]; then workersReading=0; fi
115+
perfData="${perfData} workersReading=$workersReading;"
116+
117+
workersSending=$(tr -dc 'W' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
118+
if [ -z "$workersSending" ] || [ "$workersSending" == "" ]; then workersSending=0; fi
119+
perfData="${perfData} workersSending=$workersSending;"
120+
121+
workersKeepalive=$(tr -dc 'K' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
122+
if [ -z "$workersKeepalive" ] || [ "$workersKeepalive" == "" ]; then workersKeepalive=0; fi
123+
perfData="${perfData} workersKeepalive=$workersKeepalive;"
124+
125+
workersDNSlookup=$(tr -dc 'D' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
126+
if [ -z "$workersDNSlookup" ] || [ "$workersDNSlookup" == "" ]; then workersDNSlookup=0; fi
127+
perfData="${perfData} workersDNSlookup=$workersDNSlookup;"
128+
129+
workersLosing=$(tr -dc 'C' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
130+
if [ -z "$workersLosing" ] || [ "$workersLosing" == "" ]; then workersLosing=0; fi
131+
perfData="${perfData} workersLosing=$workersLosing;"
132+
133+
workersLogging=$(tr -dc 'L' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
134+
if [ -z "$workersLogging" ] || [ "$workersLogging" == "" ]; then workersLogging=0; fi
135+
perfData="${perfData} workersLogging=$workersLogging;"
136+
137+
workersFinishing=$(tr -dc 'G' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
138+
if [ -z "$workersFinishing" ] || [ "$workersFinishing" == "" ]; then workersFinishing=0; fi
139+
perfData="${perfData} workersFinishing=$workersFinishing;"
140+
141+
workersIdleCleanup=$(tr -dc 'I' <<<"$sb" | awk '{ print length; }' 2>/dev/null);
142+
if [ -z "$workersIdleCleanup" ] || [ "$workersIdleCleanup" == "" ]; then workersIdleCleanup=0; fi
143+
perfData="${perfData} workersIdleCleanup=$workersIdleCleanup;"
144+
145+
perfData="${perfData} workersTotal=${#sb};"
146+
fi
147+
done
148+
149+
echo "OK: apache status|$perfData"
150+
exit 0
151+
else
152+
echo "WARNING: http://$host:$port/$uri returned a status code '$retCode' instead of 200."
153+
exit 1
154+
fi
155+
else
156+
echo "CRITICAL: http://$host:$port is not responding."
157+
exit 2
158+
fi
159+
160+
echo "UNKNOWN: Could not reach http://$host:$port"
161+
exit 3

check_fds_by_procname.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
##
4+
## Author: Beau Bilyeu ([email protected])
5+
## Usage: ./check_fds_by_procname.sh <username> <process_name>]
6+
##
7+
## Attempts to find process IDs by string, then issues cat against
8+
## /proc/$PID/fd to sum up total FDs being used.
9+
10+
pOwner=""
11+
pName=""
12+
count=0
13+
14+
if [ "$#" != "2" ]; then
15+
echo "Usage: $0 <username> <process_name>"
16+
exit 0
17+
elif [ "$#" == "2" ]; then
18+
pOwner="$1" ## read in username
19+
pName="$2" ## read in proc name
20+
fi
21+
22+
## get PID(s)
23+
PID=$(pgrep -U "$pOwner" -u "$pOwner" -f "$pName")
24+
if [ -z "$PID" ] || [ "$PID" == "0" ]; then
25+
echo "UNKNOWN: Could not locate pids for '$pName' by user '$pOwner'."
26+
exit 3
27+
fi
28+
29+
## loop through pid(s) and get values
30+
for i in $PID; do
31+
count=$((count + $(ls -lah "/proc/$i/fd" | wc -l))); ## append to count variable
32+
done
33+
34+
## output
35+
echo "OK: User $pOwner has $count $pName fds|${pName}_fds=$count"

0 commit comments

Comments
 (0)