Created
August 26, 2012 23:53
-
-
Save blakesmith/3484463 to your computer and use it in GitHub Desktop.
Simple Grok test. Compile with: gcc grok_test.c -Wall -lgrok -o grok_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USERNAME [a-zA-Z0-9_-]+ | |
USER %{USERNAME} | |
INT (?:[+-]?(?:[0-9]+)) | |
BASE10NUM (?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+))) | |
NUMBER (?:%{BASE10NUM}) | |
BASE16NUM (?<![0-9A-Fa-f])(?:[+-]?(?:0x)?(?:[0-9A-Fa-f]+)) | |
BASE16FLOAT \b(?<![0-9A-Fa-f.])(?:[+-]?(?:0x)?(?:(?:[0-9A-Fa-f]+(?:\.[0-9A-Fa-f]*)?)|(?:\.[0-9A-Fa-f]+)))\b | |
POSINT \b(?:[0-9]+)\b | |
WORD \b\w+\b | |
NOTSPACE \S+ | |
DATA .*? | |
GREEDYDATA .* | |
#QUOTEDSTRING (?:(?<!\\)(?:"(?:\\.|[^\\"])*"|(?:'(?:\\.|[^\\'])*')|(?:`(?:\\.|[^\\`])*`))) | |
QUOTEDSTRING (?:(?<!\\)(?:"(?>[^\\"]+|\\.)*")|(?:'(?>[^\\']+|\\.)*')|(?:`(?>[^\\`]+|\\.)*`)) | |
# Networking | |
MAC (?:%{CISCOMAC}|%{WINDOWSMAC}|%{COMMONMAC}) | |
CISCOMAC (?:(?:[A-Fa-f0-9]{4}\.){2}[A-Fa-f0-9]{4}) | |
WINDOWSMAC (?:(?:[A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2}) | |
COMMONMAC (?:(?:[A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}) | |
IP (?<![0-9])(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))(?![0-9]) | |
HOSTNAME \b(?:[0-9A-Za-z][0-9A-Za-z-]{0,62})(?:\.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(\.?|\b) | |
HOST %{HOSTNAME} | |
IPORHOST (?:%{HOSTNAME}|%{IP}) | |
HOSTPORT (?:%{IPORHOST=~/\./}:%{POSINT}) | |
# paths | |
PATH (?:%{UNIXPATH}|%{WINPATH}) | |
UNIXPATH (?:/(?:[\w_%!$@:.,-]+|\\.)*)+ | |
#UNIXPATH (?<![\w\/])(?:/[^\/\s?*]*)+ | |
LINUXTTY (?:/dev/pts/%{POSINT}) | |
BSDTTY (?:/dev/tty[pq][a-z0-9]) | |
TTY (?:%{BSDTTY}|%{LINUXTTY}) | |
WINPATH (?:[A-Za-z]+:|\\)(?:\\[^\\?*]*)+ | |
URIPROTO [A-Za-z]+(\+[A-Za-z+]+)? | |
URIHOST %{IPORHOST}(?::%{POSINT:port})? | |
# uripath comes loosely from RFC1738, but mostly from what Firefox | |
# doesn't turn into %XX | |
URIPATH (?:/[A-Za-z0-9$.+!*'(),~:#%_-]*)+ | |
#URIPARAM \?(?:[A-Za-z0-9]+(?:=(?:[^&]*))?(?:&(?:[A-Za-z0-9]+(?:=(?:[^&]*))?)?)*)? | |
URIPARAM \?[A-Za-z0-9$.+!*'(),~#%&/=:;_-]* | |
URIPATHPARAM %{URIPATH}(?:%{URIPARAM})? | |
URI %{URIPROTO}://(?:%{USER}(?::[^@]*)?@)?(?:%{URIHOST})?(?:%{URIPATHPARAM})? | |
# Months: January, Feb, 3, 03, 12, December | |
MONTH \b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\b | |
MONTHNUM (?:0?[1-9]|1[0-2]) | |
MONTHDAY (?:3[01]|[1-2]?[0-9]|0?[1-9]) | |
# Days: Monday, Tue, Thu, etc... | |
DAY (?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?) | |
# Years? | |
YEAR [0-9]+ | |
# Time: HH:MM:SS | |
#TIME \d{2}:\d{2}(?::\d{2}(?:\.\d+)?)? | |
# I'm still on the fence about using grok to perform the time match, | |
# since it's probably slower. | |
# TIME %{POSINT<24}:%{POSINT<60}(?::%{POSINT<60}(?:\.%{POSINT})?)? | |
HOUR (?:2[0123]|[01][0-9]) | |
MINUTE (?:[0-5][0-9]) | |
# '60' is a leap second in most time standards and thus is valid. | |
SECOND (?:(?:[0-5][0-9]|60)(?:[.,][0-9]+)?) | |
TIME (?!<[0-9])%{HOUR}:%{MINUTE}(?::%{SECOND})(?![0-9]) | |
# datestamp is YYYY/MM/DD-HH:MM:SS.UUUU (or something like it) | |
DATE_US %{MONTHNUM}[/-]%{MONTHDAY}[/-]%{YEAR} | |
DATE_EU %{YEAR}[/-]%{MONTHNUM}[/-]%{MONTHDAY} | |
ISO8601_TIMEZONE (?:Z|[+-]%{HOUR}(?::?%{MINUTE})) | |
ISO8601_SECOND (?:%{SECOND}|60) | |
TIMESTAMP_ISO8601 %{YEAR}-%{MONTHNUM}-%{MONTHDAY}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}? | |
DATE %{DATE_US}|%{DATE_EU} | |
DATESTAMP %{DATE}[- ]%{TIME} | |
TZ (?:[PMCE][SD]T) | |
DATESTAMP_RFC822 %{DAY} %{MONTH} %{MONTHDAY} %{YEAR} %{TIME} %{TZ} | |
DATESTAMP_OTHER %{DAY} %{MONTH} %{MONTHDAY} %{TIME} %{TZ} %{YEAR} | |
# Syslog Dates: Month Day HH:MM:SS | |
SYSLOGTIMESTAMP %{MONTH} +%{MONTHDAY} %{TIME} | |
PROG (?:[\w._/-]+) | |
SYSLOGPROG %{PROG:program}(?:\[%{POSINT:pid}\])? | |
SYSLOGHOST %{IPORHOST} | |
SYSLOGFACILITY <%{POSINT:facility}.%{POSINT:priority}> | |
HTTPDATE %{MONTHDAY}/%{MONTH}/%{YEAR}:%{TIME} %{INT:ZONE} | |
# Shortcuts | |
QS %{QUOTEDSTRING} | |
# Log formats | |
SYSLOGBASE %{SYSLOGTIMESTAMP:timestamp} (?:%{SYSLOGFACILITY} )?%{SYSLOGHOST:logsource} %{SYSLOGPROG}: | |
COMBINEDAPACHELOG %{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response} (?:%{NUMBER:bytes}|-) "(?:%{URI:referrer}|-)" %{QS:agent} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <grok.h> | |
#include <grok_match.h> | |
#include <assert.h> | |
int main() { | |
const char *patterns_file = "/Users/blake/projects/go-grok/patterns/base"; | |
int ret; | |
grok_match_t match; | |
grok_t *grok = grok_new(); | |
if (grok == NULL) { | |
puts("Failed to init grok!"); | |
exit(-1); | |
} | |
ret = grok_patterns_import_from_file(grok, patterns_file); | |
if (ret != GROK_OK) { | |
puts("Failed to import files!"); | |
exit(-1); | |
} | |
ret = grok_compile(grok, "%{DAY}"); | |
if (ret != GROK_OK) { | |
puts("Failed to compile!"); | |
exit(-1); | |
} | |
const char *text = "Tue May 15 11:21:42 [conn1047685] moveChunk deleted: 7157"; | |
ret = grok_exec(grok, text, &match); | |
if (ret != GROK_OK) { | |
puts("Failed to match!"); | |
printf("%d\n", ret); | |
exit(-1); | |
} | |
char *name = NULL; | |
const char *substring = NULL; | |
int namelen; | |
int sublen; | |
grok_match_walk_init(&match); | |
while (grok_match_walk_next(&match, &name, &namelen, &substring, &sublen) == GROK_OK) { | |
char n[namelen]; | |
char s[sublen]; | |
strncpy(n, name, namelen); | |
strncpy(s, substring, sublen); | |
printf("%d: %d\n", namelen, sublen); | |
printf("%s: %s\n", n, s); | |
} | |
grok_match_walk_end(&match); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment