|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | + |
| 17 | +import argparse |
| 18 | +import os |
| 19 | +import pprint |
| 20 | + |
| 21 | +from google.cloud import monitoring_v3 |
| 22 | +import tabulate |
| 23 | + |
| 24 | + |
| 25 | +# [START monitoring_uptime_check_create] |
| 26 | +def create_uptime_check_config(project_name, host_name=None, |
| 27 | + display_name=None): |
| 28 | + config = monitoring_v3.types.uptime_pb2.UptimeCheckConfig() |
| 29 | + config.display_name = display_name or 'New uptime check' |
| 30 | + config.monitored_resource.type = 'uptime_url' |
| 31 | + config.monitored_resource.labels.update( |
| 32 | + {'host': host_name or 'example.com'}) |
| 33 | + config.http_check.path = '/' |
| 34 | + config.http_check.port = 80 |
| 35 | + config.timeout.seconds = 10 |
| 36 | + config.period.seconds = 300 |
| 37 | + |
| 38 | + client = monitoring_v3.UptimeCheckServiceClient() |
| 39 | + new_config = client.create_uptime_check_config(project_name, config) |
| 40 | + pprint.pprint(new_config) |
| 41 | + return new_config |
| 42 | +# [END monitoring_uptime_check_create] |
| 43 | + |
| 44 | + |
| 45 | +# [START monitoring_uptime_check_list_configs] |
| 46 | +def list_uptime_check_configs(project_name): |
| 47 | + client = monitoring_v3.UptimeCheckServiceClient() |
| 48 | + configs = client.list_uptime_check_configs(project_name) |
| 49 | + |
| 50 | + for config in configs: |
| 51 | + pprint.pprint(config) |
| 52 | +# [END monitoring_uptime_check_list_configs] |
| 53 | + |
| 54 | + |
| 55 | +# [START monitoring_uptime_check_list_ips] |
| 56 | +def list_uptime_check_ips(): |
| 57 | + client = monitoring_v3.UptimeCheckServiceClient() |
| 58 | + ips = client.list_uptime_check_ips() |
| 59 | + print(tabulate.tabulate( |
| 60 | + [(ip.region, ip.location, ip.ip_address) for ip in ips], |
| 61 | + ('region', 'location', 'ip_address') |
| 62 | + )) |
| 63 | +# [END monitoring_uptime_check_list_ips] |
| 64 | + |
| 65 | + |
| 66 | +# [START monitoring_uptime_check_get] |
| 67 | +def get_uptime_check_config(config_name): |
| 68 | + client = monitoring_v3.UptimeCheckServiceClient() |
| 69 | + config = client.get_uptime_check_config(config_name) |
| 70 | + pprint.pprint(config) |
| 71 | +# [END monitoring_uptime_check_get] |
| 72 | + |
| 73 | + |
| 74 | +# [START monitoring_uptime_check_delete] |
| 75 | +def delete_uptime_check_config(config_name): |
| 76 | + client = monitoring_v3.UptimeCheckServiceClient() |
| 77 | + client.delete_uptime_check_config(config_name) |
| 78 | + print('Deleted ', config_name) |
| 79 | +# [END monitoring_uptime_check_delete] |
| 80 | + |
| 81 | + |
| 82 | +class MissingProjectIdError(Exception): |
| 83 | + pass |
| 84 | + |
| 85 | + |
| 86 | +def project_id(): |
| 87 | + """Retreieves the project id from the environment variable. |
| 88 | +
|
| 89 | + Raises: |
| 90 | + MissingProjectIdError -- When not set. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + str -- the project name |
| 94 | + """ |
| 95 | + project_id = os.environ['GCLOUD_PROJECT'] |
| 96 | + |
| 97 | + if not project_id: |
| 98 | + raise MissingProjectIdError( |
| 99 | + 'Set the environment variable ' + |
| 100 | + 'GCLOUD_PROJECT to your Google Cloud Project Id.') |
| 101 | + return project_id |
| 102 | + |
| 103 | + |
| 104 | +def project_name(): |
| 105 | + return 'projects/' + project_id() |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + |
| 110 | + parser = argparse.ArgumentParser( |
| 111 | + description='Demonstrates Uptime Check API operations.') |
| 112 | + |
| 113 | + subparsers = parser.add_subparsers(dest='command') |
| 114 | + |
| 115 | + list_uptime_check_configs_parser = subparsers.add_parser( |
| 116 | + 'list-uptime-check-configs', |
| 117 | + help=list_uptime_check_configs.__doc__ |
| 118 | + ) |
| 119 | + |
| 120 | + list_uptime_check_ips_parser = subparsers.add_parser( |
| 121 | + 'list-uptime-check-ips', |
| 122 | + help=list_uptime_check_ips.__doc__ |
| 123 | + ) |
| 124 | + |
| 125 | + create_uptime_check_config_parser = subparsers.add_parser( |
| 126 | + 'create-uptime-check', |
| 127 | + help=create_uptime_check_config.__doc__ |
| 128 | + ) |
| 129 | + create_uptime_check_config_parser.add_argument( |
| 130 | + '-d', '--display_name', |
| 131 | + required=False, |
| 132 | + ) |
| 133 | + create_uptime_check_config_parser.add_argument( |
| 134 | + '-o', '--host_name', |
| 135 | + required=False, |
| 136 | + ) |
| 137 | + |
| 138 | + get_uptime_check_config_parser = subparsers.add_parser( |
| 139 | + 'get-uptime-check-config', |
| 140 | + help=get_uptime_check_config.__doc__ |
| 141 | + ) |
| 142 | + get_uptime_check_config_parser.add_argument( |
| 143 | + '-m', '--name', |
| 144 | + required=True, |
| 145 | + ) |
| 146 | + |
| 147 | + delete_uptime_check_config_parser = subparsers.add_parser( |
| 148 | + 'delete-uptime-check-config', |
| 149 | + help=delete_uptime_check_config.__doc__ |
| 150 | + ) |
| 151 | + delete_uptime_check_config_parser.add_argument( |
| 152 | + '-m', '--name', |
| 153 | + required=True, |
| 154 | + ) |
| 155 | + |
| 156 | + args = parser.parse_args() |
| 157 | + |
| 158 | + if args.command == 'list-uptime-check-configs': |
| 159 | + list_uptime_check_configs(project_name()) |
| 160 | + |
| 161 | + elif args.command == 'list-uptime-check-ips': |
| 162 | + list_uptime_check_ips() |
| 163 | + |
| 164 | + elif args.command == 'create-uptime-check': |
| 165 | + create_uptime_check_config(project_name(), args.host_name, |
| 166 | + args.display_name) |
| 167 | + |
| 168 | + elif args.command == 'get-uptime-check-config': |
| 169 | + get_uptime_check_config(args.name) |
| 170 | + |
| 171 | + elif args.command == 'delete-uptime-check-config': |
| 172 | + delete_uptime_check_config(args.name) |
0 commit comments