forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes: vmware#2783 Signed-off-by: Michael Gasch <mgasch@vmware.com>
- Loading branch information
Michael Gasch
committed
Mar 22, 2022
1 parent
34f9952
commit 631b6a3
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/dougm/pretty" | ||
|
||
"github.com/vmware/govmomi/examples" | ||
"github.com/vmware/govmomi/property" | ||
"github.com/vmware/govmomi/vim25" | ||
"github.com/vmware/govmomi/vim25/methods" | ||
"github.com/vmware/govmomi/vim25/mo" | ||
"github.com/vmware/govmomi/vim25/types" | ||
) | ||
|
||
func main() { | ||
var limit int | ||
flag.IntVar(&limit, "limit", 10, "maximum number of alarms to retrieve") | ||
|
||
examples.Run(func(ctx context.Context, c *vim25.Client) error { | ||
pc := property.DefaultCollector(c) | ||
am := c.ServiceContent.AlarmManager | ||
|
||
fmt.Println("retrieving all alarms") | ||
alarms, err := methods.GetAlarm(ctx, c, &types.GetAlarm{ | ||
This: *am, | ||
Entity: nil, // if not set, alarms are returned for all visible entities | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("could not get alarms: %w", err) | ||
} | ||
|
||
counter := 0 | ||
for _, a := range alarms.Returnval { | ||
counter++ | ||
fmt.Printf("retrieving details for alarm %q\n", a.String()) | ||
|
||
var info mo.Alarm | ||
if err = pc.RetrieveOne(ctx, a, nil, &info); err != nil { | ||
return fmt.Errorf("retrieve alarm info: %w", err) | ||
} | ||
|
||
_, err = pretty.Println(info) | ||
if err != nil { | ||
return fmt.Errorf("print alarm: %w", err) | ||
} | ||
|
||
if counter == limit { | ||
fmt.Printf("reached maximum number of alarms to read (limit=%d)\n", limit) | ||
break | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
} |