|
| 1 | +#!/usr/bin/env pyhton |
| 2 | + |
1 | 3 | # Copyright 2015 Google Inc. All rights reserved. |
2 | 4 | # |
3 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
|
14 | 16 |
|
15 | 17 | """Sample command-line program for retrieving Google Cloud Monitoring API data. |
16 | 18 |
|
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: |
| 19 | +This sample is used on this page: |
35 | 20 |
|
36 | | -$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/json-key.json |
| 21 | + https://cloud.google.com/monitoring/api/authentication |
37 | 22 |
|
38 | | -[1] https://developers.google.com/identity/protocols/\ |
39 | | - application-default-credentials |
40 | | -[2] https://console.developers.google.com/project/_/apiui/credential |
41 | | -""" # NOQA |
| 23 | +For more information, see the README.md under /monitoring. |
| 24 | +""" |
42 | 25 |
|
43 | 26 | # [START all] |
| 27 | +import argparse |
44 | 28 | import json |
45 | | -import sys |
46 | 29 |
|
47 | 30 | from googleapiclient.discovery import build |
48 | | - |
49 | 31 | from oauth2client.client import GoogleCredentials |
50 | 32 |
|
51 | 33 |
|
52 | 34 | METRIC = 'compute.googleapis.com/instance/disk/read_ops_count' |
53 | 35 | YOUNGEST = '2015-01-01T00:00:00Z' |
54 | 36 |
|
55 | 37 |
|
56 | | -def ListTimeseries(project_name, service): |
| 38 | +def list_timeseries(monitoring, project_name): |
57 | 39 | """Query the Timeseries.list API method. |
58 | 40 |
|
59 | 41 | Args: |
| 42 | + monitoring: the CloudMonitoring service object. |
60 | 43 | project_name: the name of the project you'd like to monitor. |
61 | | - service: the CloudMonitoring service object. |
62 | 44 | """ |
| 45 | + timeseries = monitoring.timeseries() |
63 | 46 |
|
64 | | - timeseries = service.timeseries() |
| 47 | + response = timeseries.list( |
| 48 | + project=project_name, metric=METRIC, youngest=YOUNGEST).execute() |
65 | 49 |
|
66 | 50 | print('Timeseries.list raw response:') |
67 | | - try: |
68 | | - response = timeseries.list( |
69 | | - project=project_name, metric=METRIC, youngest=YOUNGEST).execute() |
70 | | - |
71 | | - print(json.dumps(response, |
72 | | - sort_keys=True, |
73 | | - indent=4, |
74 | | - separators=(',', ': '))) |
75 | | - except: |
76 | | - print('Error:') |
77 | | - for error in sys.exc_info(): |
78 | | - print(error) |
| 51 | + print(json.dumps(response, |
| 52 | + sort_keys=True, |
| 53 | + indent=4, |
| 54 | + separators=(',', ': '))) |
79 | 55 |
|
80 | 56 |
|
81 | 57 | def main(project_name): |
82 | | - # Create and return the CloudMonitoring service object. |
83 | | - service = build('cloudmonitoring', 'v2beta2', |
84 | | - credentials=GoogleCredentials.get_application_default()) |
| 58 | + credentials = GoogleCredentials.get_application_default() |
| 59 | + monitoring = build('cloudmonitoring', 'v2beta2', credentials=credentials) |
85 | 60 |
|
86 | | - ListTimeseries(project_name, service) |
| 61 | + list_timeseries(monitoring, project_name) |
87 | 62 |
|
88 | 63 |
|
89 | 64 | if __name__ == '__main__': |
90 | | - if len(sys.argv) != 2: |
91 | | - print("Usage: {} <project-name>".format(sys.argv[0])) |
92 | | - sys.exit(1) |
93 | | - main(sys.argv[1]) |
| 65 | + parser = argparse.ArgumentParser( |
| 66 | + description=__doc__, |
| 67 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 68 | + parser.add_argument('project_id', help='Your Google Cloud project ID.') |
| 69 | + |
| 70 | + args = parser.parse_args() |
| 71 | + |
| 72 | + main(args.project_id) |
94 | 73 | # [END all] |
0 commit comments