Skip to content

Instantly share code, notes, and snippets.

@undying
Last active June 18, 2025 12:00
Show Gist options
  • Save undying/b96e814d557d7f787048abfe8566549b to your computer and use it in GitHub Desktop.
Save undying/b96e814d557d7f787048abfe8566549b to your computer and use it in GitHub Desktop.
Script to count nginx errors by their types
#!/usr/bin/awk -f
BEGIN {
ERROR_USERID_SHORT = 0
ERROR_LIMIT_REQ = 1
ERROR_RECV = 2
ERROR_UPSTREAM_CLOSED = 3
ERROR_OPEN_FILE = 4
ERROR_SSL_HANDSHAKE = 5
ERROR_FILE_NOT_FOUND = 6
ERROR_BROKEN_HEADER = 7
ERROR_USERID_INVALID = 8
ERROR_RESOLVE = 9
ERROR_DIR_INDEX = 10
ERROR_UPSTREAM_READ_TIMEOUT = 11
ERROR_UPSTREAM_TIMEOUT = 12
ERROR_UPSTREAM_INVALID_HEADER = 13
ERROR_OPEN = 14
ERROR_CONNECT = 15
ERROR_UPSTREAM_DISABLED_CONNECTING = 16
ERROR_UPSTREAM_DISABLED_READING = 17
ERROR_NO_LIVE_UPSTREAMS = 18
WARNING_REQUEST_BODY_BUFFERED = 19
ERROR_UPSTREAM_DUPLICATE_HEADER = 20
WARNING_UPSTREAM_DISABLED_SENDING = 21
ERROR_UPSTREAM_TIMEOUT_SENDING = 22
WARNING_SYSLOG_SEND_FAILED = 23
ERROR_SSL_HANDSHAKE_FAILED = 24
WARNING_UPSTREAM_DISABLED_SSL_HANDSHAKE = 25
ERROR_NETWORK_UNREACHABLE = 26
INFO_CLIENT_CANCELED_STREAM = 27
WARNING_UNINITIALIZED_VARIABLE = 28
errors_count = 29
error_names[ERROR_USERID_SHORT] = "client sent too short userid cookie"
error_names[ERROR_LIMIT_REQ] = "limiting requests"
error_names[ERROR_RECV] = "recv() failed"
error_names[ERROR_UPSTREAM_CLOSED] = "upstream prematurely closed connection"
error_names[ERROR_OPEN_FILE] = "open() file failed"
error_names[ERROR_SSL_HANDSHAKE] = "peer closed connection in SSL handshake"
error_names[ERROR_FILE_NOT_FOUND] = "file not found"
error_names[ERROR_BROKEN_HEADER] = "broken header"
error_names[ERROR_USERID_INVALID] = "client sent invalid userid cookie"
error_names[ERROR_RESOLVE] = "could not be resolved"
error_names[ERROR_DIR_INDEX] = "directory index of"
error_names[ERROR_UPSTREAM_READ_TIMEOUT] = "upstream timed out while reading"
error_names[ERROR_UPSTREAM_TIMEOUT] = "upstream timed out while connecting"
error_names[ERROR_UPSTREAM_INVALID_HEADER] = "upstream sent no valid HTTP header"
error_names[ERROR_OPEN] = "open() failed"
error_names[ERROR_CONNECT] = "connect() failed"
error_names[ERROR_UPSTREAM_DISABLED_CONNECTING] = "upstream server temporarily disabled while connecting to upstream"
error_names[ERROR_UPSTREAM_DISABLED_READING] = "upstream server temporarily disabled while reading response header from upstream"
error_names[ERROR_NO_LIVE_UPSTREAMS] = "no live upstreams while connecting to upstream"
error_names[WARNING_REQUEST_BODY_BUFFERED] = "a client request body is buffered to a temporary file"
error_names[ERROR_UPSTREAM_DUPLICATE_HEADER] = "upstream sent duplicate header line"
error_names[WARNING_UPSTREAM_DISABLED_SENDING] = "upstream server temporarily disabled while sending request to upstream"
error_names[ERROR_UPSTREAM_TIMEOUT_SENDING] = "upstream timed out while sending request to upstream"
error_names[WARNING_SYSLOG_SEND_FAILED] = "send() to syslog failed"
error_names[ERROR_SSL_HANDSHAKE_FAILED] = "SSL_do_handshake failed"
error_names[WARNING_UPSTREAM_DISABLED_SSL_HANDSHAKE] = "upstream server temporarily disabled while SSL handshaking to upstream"
error_names[ERROR_NETWORK_UNREACHABLE] = "connect() failed with Network is unreachable"
error_names[INFO_CLIENT_CANCELED_STREAM] = "client canceled stream"
error_names[WARNING_UNINITIALIZED_VARIABLE] = "using uninitialized variable"
error_patterns[ERROR_USERID_SHORT] = "client sent too short userid cookie"
error_patterns[ERROR_LIMIT_REQ] = "limiting requests"
error_patterns[ERROR_RECV] = "recv\\(\\) failed"
error_patterns[ERROR_UPSTREAM_CLOSED] = "upstream prematurely closed connection"
error_patterns[ERROR_OPEN_FILE] = "open\\(\\) \"[^\"]+\" failed \\([0-9]+: [^)]+\\)"
error_patterns[ERROR_SSL_HANDSHAKE] = "peer closed connection in SSL handshake"
error_patterns[ERROR_FILE_NOT_FOUND] = "\"[^\"]+\" is not found \\([0-9]+: [^)]+\\)"
error_patterns[ERROR_BROKEN_HEADER] = "broken header"
error_patterns[ERROR_USERID_INVALID] = "client sent invalid userid cookie"
error_patterns[ERROR_RESOLVE] = "could not be resolved"
error_patterns[ERROR_DIR_INDEX] = "directory index of"
error_patterns[ERROR_UPSTREAM_READ_TIMEOUT] = "upstream timed out \\([0-9]+: Connection timed out\\) while reading"
error_patterns[ERROR_UPSTREAM_TIMEOUT] = "upstream timed out \\([0-9]+: Connection timed out\\) while connecting to upstream"
error_patterns[ERROR_UPSTREAM_INVALID_HEADER] = "upstream sent no valid HTTP/[0-9.]+ header while reading response header from upstream"
error_patterns[ERROR_OPEN] = "open\\(\\) failed"
error_patterns[ERROR_CONNECT] = "connect\\(\\) failed"
error_patterns[ERROR_UPSTREAM_DISABLED_CONNECTING] = "upstream server temporarily disabled while connecting to upstream"
error_patterns[ERROR_UPSTREAM_DISABLED_READING] = "upstream server temporarily disabled while reading response header from upstream"
error_patterns[ERROR_NO_LIVE_UPSTREAMS] = "no live upstreams while connecting to upstream"
error_patterns[WARNING_REQUEST_BODY_BUFFERED] = "a client request body is buffered to a temporary file"
error_patterns[ERROR_UPSTREAM_DUPLICATE_HEADER] = "upstream sent duplicate header line"
error_patterns[WARNING_UPSTREAM_DISABLED_SENDING] = "upstream server temporarily disabled while sending request to upstream"
error_patterns[ERROR_UPSTREAM_TIMEOUT_SENDING] = "upstream timed out \\([0-9]+: Connection timed out\\) while sending request to upstream"
error_patterns[WARNING_SYSLOG_SEND_FAILED] = "send\\(\\) to syslog failed while logging request"
error_patterns[ERROR_SSL_HANDSHAKE_FAILED] = "SSL_do_handshake\\(\\) failed"
error_patterns[WARNING_UPSTREAM_DISABLED_SSL_HANDSHAKE] = "upstream server temporarily disabled while SSL handshaking to upstream"
error_patterns[ERROR_NETWORK_UNREACHABLE] = "connect\\(\\) .* failed \\(101: Network is unreachable\\)"
error_patterns[INFO_CLIENT_CANCELED_STREAM] = "client canceled stream"
error_patterns[WARNING_UNINITIALIZED_VARIABLE] = "using uninitialized \".+\" variable"
for (i = 0; i < errors_count; i++) {
counts[error_names[i]] = 0;
}
counts["unknown"] = 0;
}
{
matched = 0;
for (i = 0; i < errors_count; i++) {
if ($0 ~ error_patterns[i]) {
counts[error_names[i]]++;
matched = 1;
break;
}
}
if (!matched) {
counts["unknown"]++;
printf("%s\n", $0);
}
}
END {
print "=== Nginx Error Counts ===";
PROCINFO["sorted_in"] = "@val_num_desc";
for (pattern in counts) {
printf("%s: %d\n", pattern, counts[pattern]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment