Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/binary"
- "math"
- "net/http"
- "context"
- "github.com/vmware/govmomi"
- "encoding/json"
- "github.com/vmware/govmomi/view"
- "github.com/vmware/govmomi/vim25/mo"
- "github.com/vmware/govmomi/vim25/soap"
- "github.com/vmware/govmomi/vim25/types"
- "log"
- "net/url"
- "time"
- "os"
- "github.com/prometheus/client_golang/prometheus"
- "github.com/prometheus/client_golang/prometheus/promhttp"
- )
- var urlFlag string = os.Getenv("VCENTER_URL")
- var user string = os.Getenv("VCENTER_USER")
- var passwd string = os.Getenv("VCENTER_PASSWD")
- type HostStatusInfo struct {
- Name string
- ConnectionState string
- InMaintenanceMode bool
- Uptime string
- }
- type Result struct {
- Node []Node
- }
- type Node struct {
- Name string `json:"name"`
- ConnectionState string `json:"ConnectionState"`
- InMaintenanceMode bool `json:"InMaintenanceMode"`
- Uptime string `json:"Uptime"`
- }
- type cmdCollector struct {
- cmdMetric *prometheus.Desc
- }
- func GetJsonWithStatusObjVcenter() string {
- ctx := context.Background()
- c, err := NewClient(ctx)
- if err != nil {
- log.Fatal(err)
- }
- defer c.Logout(ctx)
- m := view.NewManager(c.Client)
- s, _ := hostsInfo(ctx, m, c.ServiceContent.RootFolder)
- return string(s)
- }
- // NewClient creates a govmomi.Client for use in the examples
- func NewClient(ctx context.Context) (*govmomi.Client, error) {
- // Parse URL from string
- u, err := soap.ParseURL(urlFlag)
- if err != nil {
- return nil, err
- }
- u.User = url.UserPassword(user, passwd)
- // Connect and log in to ESX or vCenter
- return govmomi.NewClient(ctx, u, true)
- }
- func hostsInfo(ctx context.Context, m *view.Manager, folder types.ManagedObjectReference) ([]byte, error) {
- v, err := m.CreateContainerView(ctx, folder, []string{"HostSystem"}, true)
- if err != nil {
- log.Println(err)
- }
- defer v.Destroy(ctx)
- // Retrieve summary property for all hosts
- // Reference: http://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.HostSystem.html
- var hss []mo.HostSystem
- err = v.Retrieve(ctx, []string{"HostSystem"}, []string{"summary"}, &hss)
- if err != nil {
- log.Println(err)
- }
- var hostsInfo []HostStatusInfo
- now := time.Now()
- for _, hs := range hss {
- info := HostStatusInfo{
- Name: hs.Summary.Config.Name,
- ConnectionState: string(hs.Summary.Runtime.ConnectionState),
- InMaintenanceMode: hs.Summary.Runtime.InMaintenanceMode,
- Uptime: now.Sub(*hs.Summary.Runtime.BootTime).String(),
- }
- hostsInfo = append(hostsInfo, info)
- }
- r, err := json.Marshal(hostsInfo)
- if err != nil {
- log.Println(err)
- // return string(r), err
- }
- // fmt.Println(string(r))
- return r, err
- }
- func newCollector() *cmdCollector {
- return &cmdCollector{
- cmdMetric: prometheus.NewDesc("esx_status",
- "Show status esx's",
- []string{"hostname", "ConnectionState", "InMaintenanceMode", "Uptime"}, nil,
- ),
- }
- }
- func check(e error) {
- if e != nil {
- panic(e)
- }
- }
- func (collector *cmdCollector) Describe(ch chan<- *prometheus.Desc) {
- ch <- collector.cmdMetric
- }
- func (collector *cmdCollector) Collect(ch chan<- prometheus.Metric) {
- var metricValue float64
- var MaintenanceMode string
- metricValue = 1
- ITsrJSON := GetJsonWithStatusObjVcenter()
- j := []byte(ITsrJSON)
- var res []Node
- _ = json.Unmarshal(j, &res)
- for _, v := range res{
- if v.ConnectionState == "connected" {
- metricValue = 0
- } else {
- metricValue = 1
- }
- if v.InMaintenanceMode == true {
- MaintenanceMode = "true"
- } else {
- MaintenanceMode = "false"
- }
- ch <- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue, v.Name, v.ConnectionState, MaintenanceMode, v.Uptime)
- }
- }
- func Float64frombytes(bytes []byte) float64 {
- bits := binary.LittleEndian.Uint64(bytes)
- float := math.Float64frombits(bits)
- return float
- }
- func main() {
- //Create a new instance of the collector and
- //register it with the prometheus client.
- cmd := newCollector()
- prometheus.MustRegister(cmd)
- http.Handle("/metrics", promhttp.Handler())
- http.ListenAndServe(":2112", nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement