Skip to content

Commit

Permalink
Separate out memory statistics and system information into two differ…
Browse files Browse the repository at this point in the history
…ent services
  • Loading branch information
harshavardhana committed Jul 7, 2015
1 parent 8abb96c commit 676b905
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
11 changes: 10 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func runController(c *cli.Context) {
if err != nil {
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
}
switch c.Args().First() {
Expand All @@ -117,7 +117,16 @@ func runController(c *cli.Context) {
Fatalln(err)
}
Println(string(memstats))
case "sysinfo":
sysinfo, err := controller.GetSysInfo(c.Args().Tail().First())
if err != nil {
Fatalln(err)
}
Println(string(sysinfo))
case "donut":
if len(c.Args()) <= 2 || c.Args().First() == "help" {
cli.ShowCommandHelpAndExit(c, "controller", 1) // last argument is exit code
}
hostname, _ := os.Hostname()
err := controller.SetDonut(c.Args().Tail().First(), hostname, c.Args().Tail().Tail())
if err != nil {
Expand Down
26 changes: 24 additions & 2 deletions pkg/controller/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,30 @@ func GetDisks(url string) ([]string, error) {
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) {
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{
Method: "SysInfo.Get",
Request: rpc.Args{Request: ""},
Expand Down Expand Up @@ -96,4 +118,4 @@ func SetDonut(url, hostname string, disks []string) error {
return reply.Error
}

// Add more functions here for many replies
// Add more functions here for other RPC messages
3 changes: 2 additions & 1 deletion pkg/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ func getRPCHandler() http.Handler {
s.RegisterJSONCodec()
s.RegisterService(new(rpc.VersionService), "Version")
s.RegisterService(new(rpc.SysInfoService), "SysInfo")
s.RegisterService(new(rpc.MemStatsService), "MemStats")
s.RegisterService(new(rpc.DiskInfoService), "DiskInfo")
s.RegisterService(new(rpc.DonutService), "Donut")
// Add new services here
// Add new RPC services here
return registerRPC(router.NewRouter(), s)
}
30 changes: 22 additions & 8 deletions pkg/server/rpc/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ type SysInfoService struct{}

// SysInfoReply -
type SysInfoReply struct {
Hostname string `json:"hostname"`
SysARCH string `json:"sys.arch"`
SysOS string `json:"sys.os"`
SysCPUS int `json:"sys.ncpus"`
Routines int `json:"goroutines"`
GOVersion string `json:"goversion"`
MemStats runtime.MemStats `json:"memstats"`
Hostname string `json:"hostname"`
SysARCH string `json:"sys.arch"`
SysOS string `json:"sys.os"`
SysCPUS int `json:"sys.ncpus"`
Routines int `json:"goroutines"`
GOVersion string `json:"goversion"`
}

// MemStatsService -
type MemStatsService struct{}

// MemStatsReply -
type MemStatsReply struct {
runtime.MemStats `json:"memstats"`
}

func setSysInfoReply(sis *SysInfoReply) error {
Expand All @@ -50,15 +57,22 @@ func setSysInfoReply(sis *SysInfoReply) error {
if err != nil {
return iodine.New(err, nil)
}
return nil
}

func setMemStatsReply(sis *MemStatsReply) error {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
sis.MemStats = memStats

return nil
}

// Get method
func (s *SysInfoService) Get(r *http.Request, args *Args, reply *SysInfoReply) error {
return setSysInfoReply(reply)
}

// Get method
func (s *MemStatsService) Get(r *http.Request, args *Args, reply *MemStatsReply) error {
return setMemStatsReply(reply)
}

0 comments on commit 676b905

Please sign in to comment.