Last active
June 13, 2020 10:14
-
-
Save doorbash/2a257031edcf6c704af7f2620c71c574 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 ( | |
"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