Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
onlymellb
committed
Nov 13, 2015
1 parent
be56dc9
commit eefdb0d
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
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