Skip to content

Instantly share code, notes, and snippets.

@doorbash
Last active June 13, 2020 10:14
Show Gist options
  • Save doorbash/2a257031edcf6c704af7f2620c71c574 to your computer and use it in GitHub Desktop.
Save doorbash/2a257031edcf6c704af7f2620c71c574 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net/http"
"os/exec"
)
func metrics(w http.ResponseWriter, req *http.Request) {
cmd := exec.Command("bash", "-c", "netstat -an | grep ESTABLISHED | grep :11000 | wc -l")
data, err := cmd.Output()
if err != nil {
log.Println(err)
fmt.Println(w, "error")
return
}
cmd = exec.Command("bash", "-c", "netstat -tn 2>/dev/null | grep :11000 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head | wc -l")
data1, err1 := cmd.Output()
if err1 != nil {
log.Println(err1)
fmt.Println(w, "error")
return
}
fmt.Fprintf(w, "num_connections{} %s\nnum_ips{} %s", data, data1)
}
func main() {
http.HandleFunc("/metrics", metrics)
http.ListenAndServe(":9898", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment