-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.sh
43 lines (29 loc) · 965 Bytes
/
server.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
#!/bin/bash
PORT=8080
### Create the response FIFO
rm -f response
mkfifo response
function handle_request() {
request=""
while read line; do
request="$line"
break
done
method=$(echo "$request" | awk '{print $1}')
path=$(echo "$request" | awk '{print $2}')
echo "Received $method request for $path"
if [ "$method" = "GET" ] && [[ $path =~ ^/process/[^/]+$ ]]; then
process_name=$(echo "$path" | awk -F '/' '{print $3}')
memory=$(ps -C "$process_name" -o rss=)
cpu=$(ps -C "$process_name" -o %cpu=)
response="{\"memory\": \"$memory\", \"cpu\": \"$cpu\"}"
http_response="HTTP/1.1 200 OK\r\nContent-Length: ${#response}\r\n\r\n$response"
else
http_response="HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"
fi
echo -e "$http_response" > response
}
echo "Server running on port $PORT"
while true; do
cat response | nc -lN $PORT | handle_request
done