Last active
August 1, 2016 17:27
-
-
Save jacoelho/6c7fd75fe97627e8318ef70140703212 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "bufio" | |
| "flag" | |
| "fmt" | |
| "log" | |
| "net" | |
| "os" | |
| "strings" | |
| "time" | |
| ) | |
| func check_redis(hostname, port string) map[string]string { | |
| result := make(map[string]string) | |
| conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", hostname, port)) | |
| if err != nil { | |
| log.Println("connection error") | |
| return result | |
| } | |
| defer conn.Close() | |
| fmt.Fprintf(conn, "info\r\nquit\r\n") | |
| scanner := bufio.NewScanner(conn) | |
| for scanner.Scan() { | |
| line := scanner.Text() | |
| if strings.ContainsRune(line, ':') { | |
| tmp := strings.Split(line, ":") | |
| result[tmp[0]] = tmp[1] | |
| } | |
| } | |
| return result | |
| } | |
| func loop(interval int, work func()) { | |
| tick := time.NewTicker(time.Duration(interval) * time.Second) | |
| for { | |
| select { | |
| case <-tick.C: | |
| go work() | |
| } | |
| } | |
| } | |
| func collectd(hostname, port string, interval int) func() { | |
| return func() { | |
| keys := []string{ | |
| "instantaneous_ops_per_sec", | |
| "connected_clients", | |
| "used_memory", | |
| "instantaneous_input_kbps", | |
| "instantaneous_output_kbps", | |
| } | |
| output := check_redis(hostname, port) | |
| hostname, _ := os.Hostname() | |
| for _, k := range keys { | |
| if value, ok := output[k]; ok { | |
| fmt.Printf("PUTVAL %s/redis/gauge-%s interval=%d N:%s\n", hostname, k, interval, value) | |
| } | |
| } | |
| } | |
| } | |
| func main() { | |
| var ( | |
| hostname = flag.String("hostname", "127.0.0.1", "hostname to connect") | |
| port = flag.String("port", "6379", "redis port") | |
| interval = flag.Int("interval", 5, "time in seconds between checks") | |
| ) | |
| flag.Parse() | |
| loop(*interval, collectd(*hostname, *port, *interval)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment