HTTP Diagnosis
Goal
You read an HTTP response along four axes, status code, headers, timing, and body, and tie it together into a health check script.
Why it matters
What is needed when you receive a report that "responses are slow" is not more dashboards but splitting the time of a single request by segment. The timing variables of curl -w tell you name resolution / TCP connection / TLS / server first byte / body transfer separately. If everything is normal up to time_connect and only time_starttransfer is large, the network is innocent and the application is the culprit - the debate ends right there. And a health check must always have a maximum wait time. Without it, the script stops forever when the other side does not respond, and if cron launches it every 5 minutes, processes pile up.
Steps
Use /root/www as the document root and /root/http as the working directory.
- Create
/root/www/index.htmlcontaininglabhub-http-ok, startpython3 -m http.server 8000 --bind 127.0.0.1 --directory /root/wwwin the background, and write its PID to/root/http/server_pid.txt. - Write only the number of the status code for a request to a nonexistent path to
/root/http/status404.txt. - Receive only the response headers of
/index.htmland save them to/root/http/headers.txt. The body must not be mixed in. - Save the three values
time_namelookup,time_connect, andtime_totalof the request on one line separated by spaces to/root/http/timing.txt. - Create
/root/www/api.json.namemust belabhub, andtagsmust be an array with two or more values. Then fetch that file from the server and write the second value oftagsto/root/http/tag.txt. - Create the directory
/root/www/suband put one file in it. Write the status codes of requestinghttp://127.0.0.1:8000/subwithout following redirects and with following them to/root/http/redirect.txtas the following two lines.no_follow=<코드>(the code)follow=<코드>(the code)
- Write the status code for sending a POST request to
/root/http/method.txtin the formpost=<코드>(the code goes where the Korean word is). - Create
/root/http/healthcheck.sh <URL>.- If 2xx, print
ok code=<코드> time=<초>(the code and the seconds) and exit with code0 - If a response came but it is not 2xx, print
down code=<코드>(the code) and exit with code1 - If the connection itself fails, print a message starting with
downand exit with code1. You must decide within 10 seconds.
- If 2xx, print
Notes
- Extract only the status code with
curl -s -o /dev/null -w '%{http_code}' URL. On connection failure,000comes out. curl -sI URLreceives only the headers with a HEAD request.- Set the maximum wait time with
curl --max-time 5. - Common mistake 1: if you get 200 even without
-Lin step 6, you requested it with a trailing slash. You must request it without the slash to get a 301. - Common mistake 2: in step 8, if you compare
000only as a number, you cannot tell a connection failure from normal.
Start a local HTTP server
Create /root/www/index.html containing labhub-http-ok, start python3 -m http.server 8000 --bind 127.0.0.1 --directory /root/www in the background, and write its PID to /root/http/server_pid.txt.
python3 -m http.server has an option to specify the document root. Bind it only to 127.0.0.1.
Extract only the status code
Write only the number of the status code for a request to a nonexistent path to /root/http/status404.txt.
There is a -w variable that discards the body and prints only the status code.
Receive only headers
Receive only the response headers of /index.html and save them to /root/http/headers.txt. The body must not be mixed in.
It is the option that sends a HEAD request. The body must not be mixed into the file.
Measure the time per segment
Save the three values time_namelookup, time_connect, and time_total of the request on one line separated by spaces to /root/http/timing.txt.
Print the three of -w's time_namelookup, time_connect, and time_total on one line separated by spaces.
Extract a value from a JSON response
Create /root/www/api.json. name must be labhub, and tags must be an array with two or more values. Then fetch that file from the server and write the second value of tags to /root/http/tag.txt.
Pass curl's output to jq. Array indexes start at 0.
Follow a redirect
Create the directory /root/www/sub and put one file in it. Write the status codes of requesting http://127.0.0.1:8000/sub without following redirects and with following them to /root/http/redirect.txt as the following two lines.
no_follow=<코드>(the code)follow=<코드>(the code)
By default curl does not follow redirects. If a directory path has no trailing slash, the server returns a 301.
Response for an unimplemented method
Write the status code for sending a POST request to /root/http/method.txt in the form post=<코드> (the code goes where the Korean word is).
Specify the method with -X. This server implements only GET and HEAD.
Health check script
Create /root/http/healthcheck.sh <URL>.
- If 2xx, print
ok code=<코드> time=<초>(the code and the seconds) and exit with code0 - If a response came but it is not 2xx, print
down code=<코드>(the code) and exit with code1 - If the connection itself fails, print a message starting with
downand exit with code1. You must decide within 10 seconds.
You must distinguish three cases: healthy, non-2xx, and unreachable. Always set a maximum wait time.