mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
Add expvar JSON @ /debug/vars
Counters include: - Uptime - SMTP connections - SMTP connections (current) - SMTP messages delivered
This commit is contained in:
10
inbucket.go
10
inbucket.go
@@ -4,6 +4,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"expvar"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jhillyerd/inbucket/config"
|
"github.com/jhillyerd/inbucket/config"
|
||||||
@@ -11,10 +12,13 @@ import (
|
|||||||
"github.com/jhillyerd/inbucket/smtpd"
|
"github.com/jhillyerd/inbucket/smtpd"
|
||||||
"github.com/jhillyerd/inbucket/web"
|
"github.com/jhillyerd/inbucket/web"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var help = flag.Bool("help", false, "Displays this help")
|
var help = flag.Bool("help", false, "Displays this help")
|
||||||
|
|
||||||
|
var startTime = time.Now()
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *help {
|
if *help {
|
||||||
@@ -49,4 +53,10 @@ func init() {
|
|||||||
fmt.Fprintln(os.Stderr, "Usage of inbucket [options] <conf file>:")
|
fmt.Fprintln(os.Stderr, "Usage of inbucket [options] <conf file>:")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expvar.Publish("uptime", expvar.Func(uptime))
|
||||||
|
}
|
||||||
|
|
||||||
|
func uptime() interface{} {
|
||||||
|
return time.Since(startTime) / time.Second
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,7 +88,9 @@ func (ss *Session) String() string {
|
|||||||
*/
|
*/
|
||||||
func (s *Server) startSession(id int, conn net.Conn) {
|
func (s *Server) startSession(id int, conn net.Conn) {
|
||||||
log.Info("Connection from %v, starting session <%v>", conn.RemoteAddr(), id)
|
log.Info("Connection from %v, starting session <%v>", conn.RemoteAddr(), id)
|
||||||
|
expConnectsCurrent.Add(1)
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
defer expConnectsCurrent.Add(-1)
|
||||||
|
|
||||||
ss := NewSession(s, id, conn)
|
ss := NewSession(s, id, conn)
|
||||||
ss.greet()
|
ss.greet()
|
||||||
@@ -316,6 +318,7 @@ func (ss *Session) dataHandler() {
|
|||||||
// Mail data complete
|
// Mail data complete
|
||||||
for _, m := range messages {
|
for _, m := range messages {
|
||||||
m.Close()
|
m.Close()
|
||||||
|
expDeliveredTotal.Add(1)
|
||||||
}
|
}
|
||||||
ss.send("250 Mail accepted for delivery")
|
ss.send("250 Mail accepted for delivery")
|
||||||
ss.info("Message size %v bytes", msgSize)
|
ss.info("Message size %v bytes", msgSize)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package smtpd
|
package smtpd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"expvar"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jhillyerd/inbucket/config"
|
"github.com/jhillyerd/inbucket/config"
|
||||||
"github.com/jhillyerd/inbucket/log"
|
"github.com/jhillyerd/inbucket/log"
|
||||||
@@ -16,6 +17,10 @@ type Server struct {
|
|||||||
dataStore *DataStore
|
dataStore *DataStore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var expConnectsTotal = new(expvar.Int)
|
||||||
|
var expConnectsCurrent = new(expvar.Int)
|
||||||
|
var expDeliveredTotal = new(expvar.Int)
|
||||||
|
|
||||||
// Init a new Server object
|
// Init a new Server object
|
||||||
func New() *Server {
|
func New() *Server {
|
||||||
ds := NewDataStore()
|
ds := NewDataStore()
|
||||||
@@ -49,7 +54,15 @@ func (s *Server) Start() {
|
|||||||
// or maybe attempt to restart smtpd
|
// or maybe attempt to restart smtpd
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
|
expConnectsTotal.Add(1)
|
||||||
go s.startSession(sid, conn)
|
go s.startSession(sid, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
m := expvar.NewMap("smtp")
|
||||||
|
m.Set("connectsTotal", expConnectsTotal)
|
||||||
|
m.Set("connectsCurrent", expConnectsCurrent)
|
||||||
|
m.Set("deliveredTotal", expDeliveredTotal)
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,11 +21,10 @@ var Router *mux.Router
|
|||||||
var sessionStore sessions.Store
|
var sessionStore sessions.Store
|
||||||
|
|
||||||
func setupRoutes(cfg config.WebConfig) {
|
func setupRoutes(cfg config.WebConfig) {
|
||||||
Router = mux.NewRouter()
|
|
||||||
log.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
|
log.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
|
||||||
log.Info("Theme static content mapped to '%v'", cfg.PublicDir)
|
log.Info("Theme static content mapped to '%v'", cfg.PublicDir)
|
||||||
|
|
||||||
r := Router
|
r := mux.NewRouter()
|
||||||
// Static content
|
// Static content
|
||||||
r.PathPrefix("/public/").Handler(http.StripPrefix("/public/",
|
r.PathPrefix("/public/").Handler(http.StripPrefix("/public/",
|
||||||
http.FileServer(http.Dir(cfg.PublicDir))))
|
http.FileServer(http.Dir(cfg.PublicDir))))
|
||||||
@@ -38,6 +37,10 @@ func setupRoutes(cfg config.WebConfig) {
|
|||||||
r.Path("/mailbox/html/{name}/{id}").Handler(handler(MailboxHtml)).Name("MailboxHtml").Methods("GET")
|
r.Path("/mailbox/html/{name}/{id}").Handler(handler(MailboxHtml)).Name("MailboxHtml").Methods("GET")
|
||||||
r.Path("/mailbox/source/{name}/{id}").Handler(handler(MailboxSource)).Name("MailboxSource").Methods("GET")
|
r.Path("/mailbox/source/{name}/{id}").Handler(handler(MailboxSource)).Name("MailboxSource").Methods("GET")
|
||||||
r.Path("/mailbox/delete/{name}/{id}").Handler(handler(MailboxDelete)).Name("MailboxDelete").Methods("POST")
|
r.Path("/mailbox/delete/{name}/{id}").Handler(handler(MailboxDelete)).Name("MailboxDelete").Methods("POST")
|
||||||
|
|
||||||
|
// Register w/ HTTP
|
||||||
|
Router = r
|
||||||
|
http.Handle("/", Router)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start() the web server
|
// Start() the web server
|
||||||
@@ -51,7 +54,7 @@ func Start() {
|
|||||||
log.Info("HTTP listening on TCP4 %v", addr)
|
log.Info("HTTP listening on TCP4 %v", addr)
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
Handler: Router,
|
Handler: nil,
|
||||||
ReadTimeout: 60 * time.Second,
|
ReadTimeout: 60 * time.Second,
|
||||||
WriteTimeout: 60 * time.Second,
|
WriteTimeout: 60 * time.Second,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user