80-char length where possible

interval renamed to parse_interval
added more comments about regex
This commit is contained in:
Alexander Oleinik 2015-09-10 20:29:42 +00:00
parent 956cb528c2
commit b5c95cd931
1 changed files with 16 additions and 9 deletions

View File

@ -18,7 +18,7 @@ type Vhost struct {
Host string Host string
AccessLog string AccessLog string
RegexParsestring string RegexParsestring string
Interval string ParseInterval string
} }
type Webservercodes struct { type Webservercodes struct {
@ -30,21 +30,27 @@ type HttpStats struct {
} }
var sampleConfig = ` var sampleConfig = `
# List of virtualhosts for http codes collecting (each section for one virtualhost, none for disable collecting codes) # List of virtualhosts for http codes collecting
# (each section for one virtualhost, none for disable collecting codes)
[[webservercodes.vhosts]] [[webservercodes.vhosts]]
# 'host' field should match hostname in appropriate status url # 'host' field should match hostname in appropriate status url
host = "defaulthost" host = "defaulthost"
# telegraf user must have read permissions to this log file (shell command 'sudo adduser telegraf adm' for apache on Ubuntu) # Telegraf user must have read permissions to this log file
# (shell command 'sudo adduser telegraf adm' for apache on Ubuntu)
access_log = "/var/log/apache2/access.log" access_log = "/var/log/apache2/access.log"
# Regular expression for fetching codes from log file strings. You can adjust regex for your custom log format # Regular expression for fetching codes from log file strings.
# example for apache "common" and "combined" log formats, nginx default log format ("combined"): # You can adjust this pattern for your custom log format
# Example for apache "common" and "combined" log formats, nginx default log format ("combined"):
# '\[(?P<time>[^\]]+)\] ".*?" (?P<code>\d{3})' # '\[(?P<time>[^\]]+)\] ".*?" (?P<code>\d{3})'
# This pattern matches for strings like (example):
# 127.0.0.1 - - [30/Aug/2015:05:59:36 +0000] "GET / HTTP/1.1" 404 379 "-" "-"
regex_parsestring = '\[(?P<time>[^\]]+)\] ".*?" (?P<code>\d{3})' regex_parsestring = '\[(?P<time>[^\]]+)\] ".*?" (?P<code>\d{3})'
# plugin will count http codes in this interval. Interval must be in time.Duration format (see https://golang.org/pkg/time/#ParseDuration) # Plugin will parse log for http codes from 'now' till 'now - parse_interval' moments.
interval = "10s" # parse_interval must be in time.Duration format (see https://golang.org/pkg/time/#ParseDuration)
parse_interval = "10s"
` `
func (n *Webservercodes) SampleConfig() string { func (n *Webservercodes) SampleConfig() string {
@ -64,7 +70,7 @@ func (n *Webservercodes) Gather(acc plugins.Accumulator) error {
remainingItems := len(n.Vhosts) remainingItems := len(n.Vhosts)
for _, vhost := range n.Vhosts { for _, vhost := range n.Vhosts {
if duration, err := time.ParseDuration(vhost.Interval); err == nil { if duration, err := time.ParseDuration(vhost.ParseInterval); err == nil {
wg.Add(1) wg.Add(1)
go func(host string, logfile string, regex string, duration time.Duration, successChan chan bool, errChan chan error) { go func(host string, logfile string, regex string, duration time.Duration, successChan chan bool, errChan chan error) {
defer wg.Done() defer wg.Done()
@ -118,7 +124,8 @@ func (n Webservercodes) ParseRegex(regex string, f io.ReadSeeker) (*regexp.Regex
reader := reverse.NewScanner(f) reader := reverse.NewScanner(f)
if (reader.Scan()) { if (reader.Scan()) {
// we will check regexp validity by scan the last log line and parse it, assuming that other lines will match as well // we will check regexp validity by scan the last log line
// and parse it, assuming that other lines will match as well
parsedLine := rx.FindStringSubmatch(reader.Text()) parsedLine := rx.FindStringSubmatch(reader.Text())
if len(parsedLine) >= 3 { if len(parsedLine) >= 3 {