|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 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 | +"""Sample command-line program for retrieving Google Cloud Monitoring API data. |
| 16 | +
|
| 17 | +Simple command-line program to demonstrate connecting to the Google Cloud |
| 18 | +Monitoring API to retrieve API data, using application default credentials to |
| 19 | +authenticate. |
| 20 | +
|
| 21 | +This sample obtains authentication information from its environment via |
| 22 | +application default credentials [1]. |
| 23 | +
|
| 24 | +If you're not running the sample on Google App Engine or Compute Engine (where |
| 25 | +the environment comes pre-authenticated as a service account), you'll have to |
| 26 | +initialize your environment with credentials the sample can use. |
| 27 | +
|
| 28 | +One way to do this is through the cloud console's credentials page [2]. Create |
| 29 | +a new client ID of type 'Service account', and download its JSON key. This will |
| 30 | +be the account the sample authenticates as. |
| 31 | +
|
| 32 | +Once you've downloaded the service account's JSON key, you provide it to the |
| 33 | +sample by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to |
| 34 | +point to the key file: |
| 35 | +
|
| 36 | +$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/json-key.json |
| 37 | +
|
| 38 | +[1] https://developers.google.com/identity/protocols/application-default-credentials |
| 39 | +[2] https://console.developers.google.com/project/_/apiui/credential |
| 40 | +""" # NOQA |
| 41 | + |
| 42 | +# [START all] |
| 43 | +import json |
| 44 | +import sys |
| 45 | + |
| 46 | +from googleapiclient.discovery import build |
| 47 | + |
| 48 | +from oauth2client.client import GoogleCredentials |
| 49 | + |
| 50 | + |
| 51 | +METRIC = 'compute.googleapis.com/instance/disk/read_ops_count' |
| 52 | +YOUNGEST = '2015-01-01T00:00:00Z' |
| 53 | + |
| 54 | + |
| 55 | +def ListTimeseries(project_name, service): |
| 56 | + """Query the Timeseries.list API method. |
| 57 | +
|
| 58 | + Args: |
| 59 | + project_name: the name of the project you'd like to monitor. |
| 60 | + service: the CloudMonitoring service object. |
| 61 | + """ |
| 62 | + |
| 63 | + timeseries = service.timeseries() |
| 64 | + |
| 65 | + print 'Timeseries.list raw response:' |
| 66 | + try: |
| 67 | + response = timeseries.list( |
| 68 | + project=project_name, metric=METRIC, youngest=YOUNGEST).execute() |
| 69 | + |
| 70 | + print json.dumps(response, |
| 71 | + sort_keys=True, |
| 72 | + indent=4, |
| 73 | + separators=(',', ': ')) |
| 74 | + except: |
| 75 | + print 'Error:' |
| 76 | + for error in sys.exc_info(): |
| 77 | + print error |
| 78 | + |
| 79 | + |
| 80 | +def main(project_name): |
| 81 | + # Create and return the CloudMonitoring service object. |
| 82 | + service = build('cloudmonitoring', 'v2beta2', |
| 83 | + credentials=GoogleCredentials.get_application_default()) |
| 84 | + |
| 85 | + ListTimeseries(project_name, service) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + if len(sys.argv) != 2: |
| 90 | + print "Usage: %s <project-name>" % sys.argv[0] |
| 91 | + sys.exit(1) |
| 92 | + main(sys.argv[1]) |
| 93 | +# [END all] |
0 commit comments