From 6e3eff089d765192054713d9653d2ab5f5bd7f46 Mon Sep 17 00:00:00 2001 From: maride Date: Tue, 12 Feb 2019 15:41:27 +0100 Subject: Add simple metrics --- main.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index b2b2ae4..bf1a021 100644 --- a/main.go +++ b/main.go @@ -3,19 +3,34 @@ package main import ( "fmt" "github.com/gliderlabs/ssh" + "net/http" "log" "strings" ) +var( + metrics_num_passwords int +) + func main() { - ssh.Handle(handleConnection) - listenErr := ssh.ListenAndServe(":2222", nil) + log.Println("Starting SSH listener") + go func() { + ssh.Handle(handleConnection) + listenErr := ssh.ListenAndServe(":2222", nil) + if listenErr != nil { + log.Fatalln(listenErr.Error()) + } + }() + log.Println("Starting HTTP metrics listener") + http.HandleFunc("/metrics", metricsHandler) + listenErr := http.ListenAndServe(":8080", nil) if listenErr != nil { log.Fatalln(listenErr.Error()) } } +// Handling incoming SSH connections func handleConnection(s ssh.Session) { // Set up buffer buf := make([]byte, 1) @@ -39,6 +54,7 @@ func handleConnection(s ssh.Session) { if readErr == nil { // Print out pass + metrics_num_passwords++ log.Printf("%s@%s: '%s'", s.User(), s.RemoteAddr().String(), strBuf) } else { // Read error - just log that. @@ -48,3 +64,8 @@ func handleConnection(s ssh.Session) { // And close it. s.Exit(1) } + +// Handle HTTP /metrics requests +func metricsHandler(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords) +} -- cgit 1.4.1