Separate out memory statistics and system information into two different services

master
Harshavardhana 10 years ago
parent 8abb96c030
commit 676b9058de
  1. 11
      commands.go
  2. 26
      pkg/controller/client.go
  3. 3
      pkg/server/router.go
  4. 18
      pkg/server/rpc/sysinfo.go

@ -101,7 +101,7 @@ func runController(c *cli.Context) {
if err != nil { if err != nil {
Fatalf("Unable to determine current user. Reason: %s\n", err) Fatalf("Unable to determine current user. Reason: %s\n", err)
} }
if len(c.Args()) <= 2 || c.Args().First() == "help" { if len(c.Args()) < 2 || c.Args().First() == "help" {
cli.ShowCommandHelpAndExit(c, "controller", 1) // last argument is exit code cli.ShowCommandHelpAndExit(c, "controller", 1) // last argument is exit code
} }
switch c.Args().First() { switch c.Args().First() {
@ -117,7 +117,16 @@ func runController(c *cli.Context) {
Fatalln(err) Fatalln(err)
} }
Println(string(memstats)) Println(string(memstats))
case "sysinfo":
sysinfo, err := controller.GetSysInfo(c.Args().Tail().First())
if err != nil {
Fatalln(err)
}
Println(string(sysinfo))
case "donut": case "donut":
if len(c.Args()) <= 2 || c.Args().First() == "help" {
cli.ShowCommandHelpAndExit(c, "controller", 1) // last argument is exit code
}
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
err := controller.SetDonut(c.Args().Tail().First(), hostname, c.Args().Tail().Tail()) err := controller.SetDonut(c.Args().Tail().First(), hostname, c.Args().Tail().Tail())
if err != nil { if err != nil {

@ -47,8 +47,30 @@ func GetDisks(url string) ([]string, error) {
return reply.Disks, nil return reply.Disks, nil
} }
// GetMemStats get system info of the server at given url // GetMemStats get memory status of the server at given url
func GetMemStats(url string) ([]byte, error) { func GetMemStats(url string) ([]byte, error) {
op := RPCOps{
Method: "MemStats.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, iodine.New(err, nil)
}
resp, err := req.Do()
if err != nil {
return nil, iodine.New(err, nil)
}
defer resp.Body.Close()
var reply rpc.MemStatsReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, iodine.New(err, nil)
}
return json.MarshalIndent(reply, "", "\t")
}
// GetSysInfo get system status of the server at given url
func GetSysInfo(url string) ([]byte, error) {
op := RPCOps{ op := RPCOps{
Method: "SysInfo.Get", Method: "SysInfo.Get",
Request: rpc.Args{Request: ""}, Request: rpc.Args{Request: ""},
@ -96,4 +118,4 @@ func SetDonut(url, hostname string, disks []string) error {
return reply.Error return reply.Error
} }
// Add more functions here for many replies // Add more functions here for other RPC messages

@ -114,8 +114,9 @@ func getRPCHandler() http.Handler {
s.RegisterJSONCodec() s.RegisterJSONCodec()
s.RegisterService(new(rpc.VersionService), "Version") s.RegisterService(new(rpc.VersionService), "Version")
s.RegisterService(new(rpc.SysInfoService), "SysInfo") s.RegisterService(new(rpc.SysInfoService), "SysInfo")
s.RegisterService(new(rpc.MemStatsService), "MemStats")
s.RegisterService(new(rpc.DiskInfoService), "DiskInfo") s.RegisterService(new(rpc.DiskInfoService), "DiskInfo")
s.RegisterService(new(rpc.DonutService), "Donut") s.RegisterService(new(rpc.DonutService), "Donut")
// Add new services here // Add new RPC services here
return registerRPC(router.NewRouter(), s) return registerRPC(router.NewRouter(), s)
} }

@ -35,7 +35,14 @@ type SysInfoReply struct {
SysCPUS int `json:"sys.ncpus"` SysCPUS int `json:"sys.ncpus"`
Routines int `json:"goroutines"` Routines int `json:"goroutines"`
GOVersion string `json:"goversion"` GOVersion string `json:"goversion"`
MemStats runtime.MemStats `json:"memstats"` }
// MemStatsService -
type MemStatsService struct{}
// MemStatsReply -
type MemStatsReply struct {
runtime.MemStats `json:"memstats"`
} }
func setSysInfoReply(sis *SysInfoReply) error { func setSysInfoReply(sis *SysInfoReply) error {
@ -50,11 +57,13 @@ func setSysInfoReply(sis *SysInfoReply) error {
if err != nil { if err != nil {
return iodine.New(err, nil) return iodine.New(err, nil)
} }
return nil
}
func setMemStatsReply(sis *MemStatsReply) error {
var memStats runtime.MemStats var memStats runtime.MemStats
runtime.ReadMemStats(&memStats) runtime.ReadMemStats(&memStats)
sis.MemStats = memStats sis.MemStats = memStats
return nil return nil
} }
@ -62,3 +71,8 @@ func setSysInfoReply(sis *SysInfoReply) error {
func (s *SysInfoService) Get(r *http.Request, args *Args, reply *SysInfoReply) error { func (s *SysInfoService) Get(r *http.Request, args *Args, reply *SysInfoReply) error {
return setSysInfoReply(reply) return setSysInfoReply(reply)
} }
// Get method
func (s *MemStatsService) Get(r *http.Request, args *Args, reply *MemStatsReply) error {
return setMemStatsReply(reply)
}

Loading…
Cancel
Save