Skip to content

Commit

Permalink
让agent支持url探测功能
Browse files Browse the repository at this point in the history
  • Loading branch information
onlymellb committed Nov 13, 2015
1 parent be56dc9 commit eefdb0d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cron/builtin.go
Expand Up @@ -28,6 +28,7 @@ func syncBuiltinMetrics() {
var ports = []int64{}
var paths = []string{}
var procs = make(map[string]map[int]string)
var urls = make(map[string]string)

hostname, err := g.Hostname()
if err != nil {
Expand Down Expand Up @@ -58,6 +59,27 @@ func syncBuiltinMetrics() {
checksum = resp.Checksum

for _, metric := range resp.Metrics {

if metric.Metric == "url.check.health" {
arr := strings.Split(metric.Tags, ",")
if len(arr) != 2 {
continue
}
url := strings.Split(arr[0], "=")
if len(url) != 2 {
continue
}
stime := strings.Split(arr[1], "=")
if len(stime) != 2 {
continue
}
if _, err := strconv.ParseInt(stime[1], 10, 64); err == nil {
urls[url[1]] = stime[1]
} else {
log.Println("metric ParseInt timeout failed:", err)
}
}

if metric.Metric == "net.port.listen" {
arr := strings.Split(metric.Tags, "=")
if len(arr) != 2 {
Expand Down Expand Up @@ -100,6 +122,7 @@ func syncBuiltinMetrics() {
}
}

g.SetReportUrls(urls)
g.SetReportPorts(ports)
g.SetReportProcs(procs)
g.SetDuPaths(paths)
Expand Down
6 changes: 6 additions & 0 deletions funcs/funcs.go
Expand Up @@ -50,5 +50,11 @@ func BuildMappers() {
},
Interval: interval,
},
FuncsAndInterval{
Fs: []func() []*model.MetricValue{
UrlMetrics,
},
Interval: interval,
},
}
}
51 changes: 51 additions & 0 deletions funcs/urlstat.go
@@ -0,0 +1,51 @@
package funcs

import (
"bufio"
"bytes"
"fmt"
"log"
"strings"

"github.com/open-falcon/agent/g"
"github.com/open-falcon/common/model"
"github.com/toolkits/file"
"github.com/toolkits/sys"
)

func UrlMetrics() (L []*model.MetricValue) {
reportUrls := g.ReportUrls()
sz := len(reportUrls)
if sz == 0 {
return
}

for furl, timeout := range reportUrls {
tags := fmt.Sprintf("url=%v,timeout=%v", furl, timeout)
if ok, _ := probeUrl(furl, timeout); !ok {
L = append(L, GaugeValue("url.check.health", 0, tags))
continue
}
L = append(L, GaugeValue("url.check.health", 1, tags))
}
return
}

func probeUrl(furl string, timeout string) (bool, error) {
bs, err := sys.CmdOutBytes("curl", "-I", "-m", timeout, "-o", "/dev/null", "-s", "-w", "%{http_code}", furl)
if err != nil {
log.Printf("probe url [%v] failed.the err is: [%v]\n", furl, err)
return false, err
}
reader := bufio.NewReader(bytes.NewBuffer(bs))
retcode, err := file.ReadLine(reader)
if err != nil {
log.Println("read retcode failed.err is:", err)
return false, err
}
if strings.TrimSpace(string(retcode)) != "200" {
log.Printf("return code [%v] is not 200.query url is [%v]", string(retcode), furl)
return false, err
}
return true, err
}
17 changes: 17 additions & 0 deletions g/var.go
Expand Up @@ -74,6 +74,23 @@ func SendToTransfer(metrics []*model.MetricValue) {
}
}

var (
reportUrls map[string]string
reportUrlsLock = new(sync.RWMutex)
)

func ReportUrls() map[string]string {
reportUrlsLock.RLock()
defer reportUrlsLock.RUnlock()
return reportUrls
}

func SetReportUrls(urls map[string]string) {
reportUrlsLock.RLock()
defer reportUrlsLock.RUnlock()
reportUrls = urls
}

var (
reportPorts []int64
reportPortsLock = new(sync.RWMutex)
Expand Down

0 comments on commit eefdb0d

Please sign in to comment.