Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/binary"
  5. "math"
  6. "net/http"
  7. "context"
  8. "github.com/vmware/govmomi"
  9. "encoding/json"
  10. "github.com/vmware/govmomi/view"
  11. "github.com/vmware/govmomi/vim25/mo"
  12. "github.com/vmware/govmomi/vim25/soap"
  13. "github.com/vmware/govmomi/vim25/types"
  14. "log"
  15. "net/url"
  16. "time"
  17. "os"
  18. "github.com/prometheus/client_golang/prometheus"
  19. "github.com/prometheus/client_golang/prometheus/promhttp"
  20. )
  21.  
  22. var urlFlag string = os.Getenv("VCENTER_URL")
  23. var user string = os.Getenv("VCENTER_USER")
  24. var passwd string = os.Getenv("VCENTER_PASSWD")
  25.  
  26.  
  27.  
  28. type HostStatusInfo struct {
  29. Name string
  30. ConnectionState string
  31. InMaintenanceMode bool
  32. Uptime string
  33. }
  34.  
  35. type Result struct {
  36. Node []Node
  37. }
  38.  
  39. type Node struct {
  40. Name string `json:"name"`
  41. ConnectionState string `json:"ConnectionState"`
  42. InMaintenanceMode bool `json:"InMaintenanceMode"`
  43. Uptime string `json:"Uptime"`
  44. }
  45.  
  46. type cmdCollector struct {
  47. cmdMetric *prometheus.Desc
  48. }
  49.  
  50. func GetJsonWithStatusObjVcenter() string {
  51.  
  52. ctx := context.Background()
  53. c, err := NewClient(ctx)
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57.  
  58. defer c.Logout(ctx)
  59.  
  60. m := view.NewManager(c.Client)
  61.  
  62. s, _ := hostsInfo(ctx, m, c.ServiceContent.RootFolder)
  63. return string(s)
  64.  
  65. }
  66.  
  67. // NewClient creates a govmomi.Client for use in the examples
  68. func NewClient(ctx context.Context) (*govmomi.Client, error) {
  69. // Parse URL from string
  70. u, err := soap.ParseURL(urlFlag)
  71. if err != nil {
  72. return nil, err
  73. }
  74.  
  75. u.User = url.UserPassword(user, passwd)
  76.  
  77. // Connect and log in to ESX or vCenter
  78. return govmomi.NewClient(ctx, u, true)
  79. }
  80.  
  81. func hostsInfo(ctx context.Context, m *view.Manager, folder types.ManagedObjectReference) ([]byte, error) {
  82.  
  83. v, err := m.CreateContainerView(ctx, folder, []string{"HostSystem"}, true)
  84. if err != nil {
  85. log.Println(err)
  86. }
  87.  
  88. defer v.Destroy(ctx)
  89.  
  90. // Retrieve summary property for all hosts
  91. // Reference: http://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.HostSystem.html
  92. var hss []mo.HostSystem
  93. err = v.Retrieve(ctx, []string{"HostSystem"}, []string{"summary"}, &hss)
  94. if err != nil {
  95. log.Println(err)
  96. }
  97.  
  98. var hostsInfo []HostStatusInfo
  99. now := time.Now()
  100. for _, hs := range hss {
  101. info := HostStatusInfo{
  102. Name: hs.Summary.Config.Name,
  103. ConnectionState: string(hs.Summary.Runtime.ConnectionState),
  104. InMaintenanceMode: hs.Summary.Runtime.InMaintenanceMode,
  105. Uptime: now.Sub(*hs.Summary.Runtime.BootTime).String(),
  106. }
  107. hostsInfo = append(hostsInfo, info)
  108. }
  109. r, err := json.Marshal(hostsInfo)
  110. if err != nil {
  111. log.Println(err)
  112. // return string(r), err
  113. }
  114. // fmt.Println(string(r))
  115. return r, err
  116. }
  117.  
  118.  
  119. func newCollector() *cmdCollector {
  120. return &cmdCollector{
  121. cmdMetric: prometheus.NewDesc("esx_status",
  122. "Show status esx's",
  123. []string{"hostname", "ConnectionState", "InMaintenanceMode", "Uptime"}, nil,
  124. ),
  125. }
  126. }
  127.  
  128. func check(e error) {
  129. if e != nil {
  130. panic(e)
  131. }
  132. }
  133.  
  134. func (collector *cmdCollector) Describe(ch chan<- *prometheus.Desc) {
  135. ch <- collector.cmdMetric
  136. }
  137.  
  138. func (collector *cmdCollector) Collect(ch chan<- prometheus.Metric) {
  139.  
  140. var metricValue float64
  141. var MaintenanceMode string
  142.  
  143. metricValue = 1
  144. ITsrJSON := GetJsonWithStatusObjVcenter()
  145.  
  146. j := []byte(ITsrJSON)
  147.  
  148.  
  149. var res []Node
  150. _ = json.Unmarshal(j, &res)
  151.  
  152. for _, v := range res{
  153. if v.ConnectionState == "connected" {
  154. metricValue = 0
  155. } else {
  156. metricValue = 1
  157. }
  158. if v.InMaintenanceMode == true {
  159. MaintenanceMode = "true"
  160. } else {
  161. MaintenanceMode = "false"
  162. }
  163.  
  164.  
  165. ch <- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue, v.Name, v.ConnectionState, MaintenanceMode, v.Uptime)
  166. }
  167.  
  168. }
  169.  
  170. func Float64frombytes(bytes []byte) float64 {
  171. bits := binary.LittleEndian.Uint64(bytes)
  172. float := math.Float64frombits(bits)
  173. return float
  174. }
  175.  
  176. func main() {
  177. //Create a new instance of the collector and
  178. //register it with the prometheus client.
  179. cmd := newCollector()
  180. prometheus.MustRegister(cmd)
  181.  
  182. http.Handle("/metrics", promhttp.Handler())
  183. http.ListenAndServe(":2112", nil)
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement