info.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package client
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/engine"
  8. flag "github.com/docker/docker/pkg/mflag"
  9. "github.com/docker/docker/pkg/units"
  10. )
  11. // CmdInfo displays system-wide information.
  12. //
  13. // Usage: docker info
  14. func (cli *DockerCli) CmdInfo(args ...string) error {
  15. cmd := cli.Subcmd("info", "", "Display system-wide information", true)
  16. cmd.Require(flag.Exact, 0)
  17. cmd.ParseFlags(args, false)
  18. body, _, err := readBody(cli.call("GET", "/info", nil, nil))
  19. if err != nil {
  20. return err
  21. }
  22. out := engine.NewOutput()
  23. remoteInfo, err := out.AddEnv()
  24. if err != nil {
  25. return err
  26. }
  27. if _, err := out.Write(body); err != nil {
  28. logrus.Errorf("Error reading remote info: %s", err)
  29. return err
  30. }
  31. out.Close()
  32. if remoteInfo.Exists("Containers") {
  33. fmt.Fprintf(cli.out, "Containers: %d\n", remoteInfo.GetInt("Containers"))
  34. }
  35. if remoteInfo.Exists("Images") {
  36. fmt.Fprintf(cli.out, "Images: %d\n", remoteInfo.GetInt("Images"))
  37. }
  38. if remoteInfo.Exists("Driver") {
  39. fmt.Fprintf(cli.out, "Storage Driver: %s\n", remoteInfo.Get("Driver"))
  40. }
  41. if remoteInfo.Exists("DriverStatus") {
  42. var driverStatus [][2]string
  43. if err := remoteInfo.GetJson("DriverStatus", &driverStatus); err != nil {
  44. return err
  45. }
  46. for _, pair := range driverStatus {
  47. fmt.Fprintf(cli.out, " %s: %s\n", pair[0], pair[1])
  48. }
  49. }
  50. if remoteInfo.Exists("ExecutionDriver") {
  51. fmt.Fprintf(cli.out, "Execution Driver: %s\n", remoteInfo.Get("ExecutionDriver"))
  52. }
  53. if remoteInfo.Exists("KernelVersion") {
  54. fmt.Fprintf(cli.out, "Kernel Version: %s\n", remoteInfo.Get("KernelVersion"))
  55. }
  56. if remoteInfo.Exists("OperatingSystem") {
  57. fmt.Fprintf(cli.out, "Operating System: %s\n", remoteInfo.Get("OperatingSystem"))
  58. }
  59. if remoteInfo.Exists("NCPU") {
  60. fmt.Fprintf(cli.out, "CPUs: %d\n", remoteInfo.GetInt("NCPU"))
  61. }
  62. if remoteInfo.Exists("MemTotal") {
  63. fmt.Fprintf(cli.out, "Total Memory: %s\n", units.BytesSize(float64(remoteInfo.GetInt64("MemTotal"))))
  64. }
  65. if remoteInfo.Exists("Name") {
  66. fmt.Fprintf(cli.out, "Name: %s\n", remoteInfo.Get("Name"))
  67. }
  68. if remoteInfo.Exists("ID") {
  69. fmt.Fprintf(cli.out, "ID: %s\n", remoteInfo.Get("ID"))
  70. }
  71. if remoteInfo.GetBool("Debug") || os.Getenv("DEBUG") != "" {
  72. if remoteInfo.Exists("Debug") {
  73. fmt.Fprintf(cli.out, "Debug mode (server): %v\n", remoteInfo.GetBool("Debug"))
  74. }
  75. fmt.Fprintf(cli.out, "Debug mode (client): %v\n", os.Getenv("DEBUG") != "")
  76. if remoteInfo.Exists("NFd") {
  77. fmt.Fprintf(cli.out, "File Descriptors: %d\n", remoteInfo.GetInt("NFd"))
  78. }
  79. if remoteInfo.Exists("NGoroutines") {
  80. fmt.Fprintf(cli.out, "Goroutines: %d\n", remoteInfo.GetInt("NGoroutines"))
  81. }
  82. if remoteInfo.Exists("SystemTime") {
  83. t, err := remoteInfo.GetTime("SystemTime")
  84. if err != nil {
  85. logrus.Errorf("Error reading system time: %v", err)
  86. } else {
  87. fmt.Fprintf(cli.out, "System Time: %s\n", t.Format(time.UnixDate))
  88. }
  89. }
  90. if remoteInfo.Exists("NEventsListener") {
  91. fmt.Fprintf(cli.out, "EventsListeners: %d\n", remoteInfo.GetInt("NEventsListener"))
  92. }
  93. if initSha1 := remoteInfo.Get("InitSha1"); initSha1 != "" {
  94. fmt.Fprintf(cli.out, "Init SHA1: %s\n", initSha1)
  95. }
  96. if initPath := remoteInfo.Get("InitPath"); initPath != "" {
  97. fmt.Fprintf(cli.out, "Init Path: %s\n", initPath)
  98. }
  99. if root := remoteInfo.Get("DockerRootDir"); root != "" {
  100. fmt.Fprintf(cli.out, "Docker Root Dir: %s\n", root)
  101. }
  102. }
  103. if remoteInfo.Exists("HttpProxy") {
  104. fmt.Fprintf(cli.out, "Http Proxy: %s\n", remoteInfo.Get("HttpProxy"))
  105. }
  106. if remoteInfo.Exists("HttpsProxy") {
  107. fmt.Fprintf(cli.out, "Https Proxy: %s\n", remoteInfo.Get("HttpsProxy"))
  108. }
  109. if remoteInfo.Exists("NoProxy") {
  110. fmt.Fprintf(cli.out, "No Proxy: %s\n", remoteInfo.Get("NoProxy"))
  111. }
  112. if len(remoteInfo.GetList("IndexServerAddress")) != 0 {
  113. cli.LoadConfigFile()
  114. u := cli.configFile.Configs[remoteInfo.Get("IndexServerAddress")].Username
  115. if len(u) > 0 {
  116. fmt.Fprintf(cli.out, "Username: %v\n", u)
  117. fmt.Fprintf(cli.out, "Registry: %v\n", remoteInfo.GetList("IndexServerAddress"))
  118. }
  119. }
  120. if remoteInfo.Exists("MemoryLimit") && !remoteInfo.GetBool("MemoryLimit") {
  121. fmt.Fprintf(cli.err, "WARNING: No memory limit support\n")
  122. }
  123. if remoteInfo.Exists("SwapLimit") && !remoteInfo.GetBool("SwapLimit") {
  124. fmt.Fprintf(cli.err, "WARNING: No swap limit support\n")
  125. }
  126. if remoteInfo.Exists("IPv4Forwarding") && !remoteInfo.GetBool("IPv4Forwarding") {
  127. fmt.Fprintf(cli.err, "WARNING: IPv4 forwarding is disabled.\n")
  128. }
  129. if remoteInfo.Exists("Labels") {
  130. fmt.Fprintln(cli.out, "Labels:")
  131. for _, attribute := range remoteInfo.GetList("Labels") {
  132. fmt.Fprintf(cli.out, " %s\n", attribute)
  133. }
  134. }
  135. return nil
  136. }