expvarmon/self.go

49 lines
1.0 KiB
Go
Raw Normal View History

2015-05-04 00:46:26 +08:00
package main
import (
"expvar"
"fmt"
"net"
"net/http"
2015-07-11 00:29:22 +08:00
"net/url"
2015-05-04 00:46:26 +08:00
"runtime"
"time"
)
var startTime = time.Now().UTC()
func goroutines() interface{} {
return runtime.NumGoroutine()
}
// uptime is an expvar.Func compliant wrapper for uptime info.
func uptime() interface{} {
uptime := time.Since(startTime)
return int64(uptime)
}
// startPort defines lower port for bind
const startPort = 32768
// StartSelfMonitor starts http server on random port and exports expvars.
//
// It tries 1024 ports, starting from startPort and registers some expvars if ok.
2015-07-11 00:29:22 +08:00
func StartSelfMonitor() (url.URL, error) {
2015-05-04 00:46:26 +08:00
for port := startPort; port < startPort+1024; port++ {
bind := fmt.Sprintf("localhost:%d", port)
l, err := net.Listen("tcp", bind)
if err != nil {
continue
}
l.Close()
expvar.Publish("Goroutines", expvar.Func(goroutines))
expvar.Publish("Uptime", expvar.Func(uptime))
go http.ListenAndServe(bind, nil)
2015-07-11 00:29:22 +08:00
return NewURL(fmt.Sprintf("%d", port)), nil
2015-05-04 00:46:26 +08:00
}
2015-07-11 00:29:22 +08:00
return url.URL{}, fmt.Errorf("no free ports found")
2015-05-04 00:46:26 +08:00
}