Last active
August 29, 2015 14:18
-
-
Save sgmac/71d10f0d1c83fcb93901 to your computer and use it in GitHub Desktop.
memcache stats
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 ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net" | |
"regexp" | |
"strconv" | |
) | |
func main() { | |
var cacheRatio int | |
fmt.Println("Getting Memcache stats....") | |
conn, err := net.Dial("tcp", "localhost:11211") | |
if err != nil { | |
log.Fatal("error net Dial") | |
} | |
fmt.Fprintf(conn, "stats\r\nquit\r\n") | |
data, err := ioutil.ReadAll(conn) | |
reghits, err := regexp.Compile(`cas_hits ([0-9]+)`) | |
regmisses, err := regexp.Compile(`cas_misses ([0-9]+)`) | |
hitsData := reghits.Find(data) | |
missesData := regmisses.Find(data) | |
if hitsData != nil && missesData != nil { | |
// cas_hits value | |
casHits, _ := strconv.Atoi(string(reghits.FindSubmatch(hitsData)[1])) | |
// cas_misses value | |
casMisses, _ := strconv.Atoi(string(regmisses.FindSubmatch(missesData)[1])) | |
totalHits := (casHits + casMisses) | |
if totalHits != 0 { | |
cacheRatio = casHits / totalHits | |
} | |
fmt.Printf("cache hits: %d\n", casHits) | |
fmt.Printf("cache misses: %d\n", casMisses) | |
fmt.Printf("cache ratio: %d\n", cacheRatio) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment