github.com/vmware/govmomi@v0.37.2/examples/alarms/main.go (about) 1 /* 2 Copyright (c) 2014-2022 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 24 "github.com/dougm/pretty" 25 26 "github.com/vmware/govmomi/examples" 27 "github.com/vmware/govmomi/property" 28 "github.com/vmware/govmomi/vim25" 29 "github.com/vmware/govmomi/vim25/methods" 30 "github.com/vmware/govmomi/vim25/mo" 31 "github.com/vmware/govmomi/vim25/types" 32 ) 33 34 func main() { 35 var limit int 36 flag.IntVar(&limit, "limit", 10, "maximum number of alarms to retrieve") 37 38 examples.Run(func(ctx context.Context, c *vim25.Client) error { 39 pc := property.DefaultCollector(c) 40 am := c.ServiceContent.AlarmManager 41 42 fmt.Println("retrieving all alarms") 43 alarms, err := methods.GetAlarm(ctx, c, &types.GetAlarm{ 44 This: *am, 45 Entity: nil, // if not set, alarms are returned for all visible entities 46 }) 47 if err != nil { 48 return fmt.Errorf("could not get alarms: %w", err) 49 } 50 51 counter := 0 52 for _, a := range alarms.Returnval { 53 counter++ 54 fmt.Printf("retrieving details for alarm %q\n", a.String()) 55 56 var info mo.Alarm 57 if err = pc.RetrieveOne(ctx, a, nil, &info); err != nil { 58 return fmt.Errorf("retrieve alarm info: %w", err) 59 } 60 61 _, err = pretty.Println(info) 62 if err != nil { 63 return fmt.Errorf("print alarm: %w", err) 64 } 65 66 if counter == limit { 67 fmt.Printf("reached maximum number of alarms to read (limit=%d)\n", limit) 68 break 69 } 70 } 71 72 return nil 73 }) 74 }