|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | """
|
3 | 3 | * *******************************************************
|
4 |
| -* Copyright (c) VMware, Inc. 2017. All Rights Reserved. |
| 4 | +* Copyright (c) VMware, Inc. 2019. All Rights Reserved. |
5 | 5 | * SPDX-License-Identifier: MIT
|
6 | 6 | * *******************************************************
|
7 | 7 | *
|
|
13 | 13 | """
|
14 | 14 |
|
15 | 15 | __author__ = 'VMware, Inc.'
|
16 |
| -__copyright__ = 'Copyright 2017 VMware, Inc. All rights reserved.' |
17 | 16 | __vcenter_version__ = '6.7+'
|
18 | 17 |
|
19 |
| -from samples.vsphere.common import sample_cli |
20 |
| -from samples.vsphere.common import sample_util |
21 |
| -from samples.vsphere.common import vapiconnect |
22 |
| -from com.vmware.appliance_client import Health |
| 18 | +from samples.vsphere.common import (sample_cli, sample_util) |
| 19 | +from com.vmware.appliance_client import Health |
23 | 20 | from com.vmware.vapi.std_client import LocalizableMessage
|
| 21 | +from com.vmware.appliance_client import Notification |
| 22 | +from vmware.vapi.vsphere.client import create_vsphere_client |
| 23 | +from samples.vsphere.common.ssl_helper import get_unverified_session |
24 | 24 |
|
25 |
| -class HealthMessages(object) : |
| 25 | + |
| 26 | +class HealthMessages(object): |
26 | 27 | """
|
27 |
| - Demonstrates getting Health messages for various health items |
| 28 | + Demonstrates getting Health messages for memory, storage and cpu |
28 | 29 |
|
29 | 30 | Retrieves Health messages details from vCenter and prints the data
|
30 | 31 |
|
31 | 32 | """
|
32 | 33 |
|
33 |
| - |
34 | 34 | def __init__(self):
|
35 | 35 | self.item = None
|
36 |
| - self.stub_config = None |
37 |
| - |
38 |
| - def setup(self): |
| 36 | + self.client = None |
39 | 37 | parser = sample_cli.build_arg_parser()
|
| 38 | + |
40 | 39 | parser.add_argument(
|
41 | 40 | '--item',
|
42 | 41 | required=True,
|
43 | 42 | action='store',
|
44 |
| - choices=['memory', 'cpu' , 'storage'], |
| 43 | + choices=['memory', 'cpu', 'storage'], |
45 | 44 | help='Specify the name of health item to view the messages')
|
| 45 | + |
46 | 46 | args = sample_util.process_cli_args(parser.parse_args())
|
47 | 47 | self.item = args.item
|
48 |
| - |
49 |
| - # Connect to vAPI services |
50 |
| - self.stub_config = vapiconnect.connect( |
51 |
| - host=args.server, |
52 |
| - user=args.username, |
53 |
| - pwd=args.password, |
54 |
| - skip_verification=args.skipverification) |
55 |
| - |
56 |
| - self.health_client = Health(self.stub_config) |
| 48 | + # Connect to vAPI services |
| 49 | + session = get_unverified_session() if args.skipverification else None |
| 50 | + self.client = create_vsphere_client(server=args.server, |
| 51 | + username=args.username, |
| 52 | + password=args.password, |
| 53 | + session=session) |
| 54 | + self.health_client = Health(self.client._stub_config) |
57 | 55 |
|
58 | 56 | def run(self):
|
59 | 57 | message_list = self.health_client.messages(self.item)
|
60 |
| - print(" Health Alarams") |
| 58 | + print("Health Alarams") |
61 | 59 | print("-------------------\n")
|
62 | 60 | if not message_list:
|
63 | 61 | print("No health alarms for : " + self.item)
|
64 | 62 | else:
|
65 |
| - for message in message_list : |
66 |
| - print("Alert time : " + message.getTime()) |
67 |
| - print("Alert message Id: " + message.getId()) |
68 |
| - msg = message.getMessage() |
69 |
| - def_message = LocalizableMessage(msg.default_message) |
70 |
| - print("Alert message : "+ def_message) |
| 63 | + for message in message_list: |
| 64 | + print("Alert time : {}".format(message.time)) |
| 65 | + print("Alert message Id: " + message.id) |
| 66 | + local_message = message.message |
| 67 | + default_msg = local_message.default_message |
| 68 | + print("Alert message : " + default_msg) |
71 | 69 |
|
72 | 70 |
|
73 | 71 | def main():
|
74 | 72 | health_sample = HealthMessages()
|
75 |
| - health_sample.setup() |
76 | 73 | health_sample.run()
|
77 | 74 |
|
| 75 | + |
78 | 76 | if __name__ == '__main__':
|
79 | 77 | main()
|
0 commit comments