Skip to content

Instantly share code, notes, and snippets.

@m0n5t3r
Last active August 29, 2015 14:09
Show Gist options
  • Save m0n5t3r/2ca132fba9e624044495 to your computer and use it in GitHub Desktop.
Save m0n5t3r/2ca132fba9e624044495 to your computer and use it in GitHub Desktop.
Filter varnishlog output and only display those requests that match a regexp pattern; match is against the whole text, not on indivicual fields; requires gawk (Debian derivatives seem to hate gawk, it's not installed by default)
#!/bin/bash
# Copyright: 2014 Sabin Iacob
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
PATTERN="$1"
[ -z "$PATTERN" ] && echo "Usage: $0 pattern_to_match" && exit 1
AWK_PROG='
{
reqs[$1] = reqs[$1]"\n"$0;
}
$2 == "ReqEnd" {
if(reqs[$1] ~ pattern) {
print reqs[$1];
}
delete reqs[$1];
}
'
varnishlog | gawk --assign pattern="$PATTERN" "$AWK_PROG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment