|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | + |
| 19 | +from gcloud import logging |
| 20 | + |
| 21 | + |
| 22 | +def list_sinks(): |
| 23 | + """Lists all sinks.""" |
| 24 | + logging_client = logging.Client() |
| 25 | + |
| 26 | + sinks = [] |
| 27 | + token = None |
| 28 | + while True: |
| 29 | + new_sinks, token = logging_client.list_sinks(page_token=token) |
| 30 | + sinks.extend(new_sinks) |
| 31 | + if token is None: |
| 32 | + break |
| 33 | + |
| 34 | + if not sinks: |
| 35 | + print('No sinks.') |
| 36 | + |
| 37 | + for sink in sinks: |
| 38 | + print('{}: {} -> {}'.format(sink.name, sink.filter_, sink.destination)) |
| 39 | + |
| 40 | + |
| 41 | +def create_sink(sink_name, destination_bucket, filter_): |
| 42 | + """Creates a sink to export logs to the given Cloud Storage bucket. |
| 43 | +
|
| 44 | + The filter determines which logs this sink matches and will be exported |
| 45 | + to the destination. For example a filter of 'severity>=INFO' will send |
| 46 | + all logs that have a severity of INFO or greater to the destination. |
| 47 | + See https://cloud.google.com/logging/docs/view/advanced_filters for more |
| 48 | + filter information. |
| 49 | + """ |
| 50 | + logging_client = logging.Client() |
| 51 | + |
| 52 | + # The destination can be a Cloud Storage bucket, a Cloud Pub/Sub topic, |
| 53 | + # or a BigQuery dataset. In this case, it is a Cloud Storage Bucket. |
| 54 | + # See https://cloud.google.com/logging/docs/api/tasks/exporting-logs for |
| 55 | + # information on the destination format. |
| 56 | + destination = 'storage.googleapis.com/{bucket}'.format( |
| 57 | + bucket=destination_bucket) |
| 58 | + |
| 59 | + sink = logging_client.sink( |
| 60 | + sink_name, |
| 61 | + filter_, |
| 62 | + destination) |
| 63 | + |
| 64 | + if sink.exists(): |
| 65 | + print('Sink {} already exists.'.format(sink.name)) |
| 66 | + return |
| 67 | + |
| 68 | + sink.create() |
| 69 | + print('Created sink {}'.format(sink.name)) |
| 70 | + |
| 71 | + |
| 72 | +def update_sink(sink_name, filter_): |
| 73 | + """Changes a sink's filter. |
| 74 | +
|
| 75 | + The filter determines which logs this sink matches and will be exported |
| 76 | + to the destination. For example a filter of 'severity>=INFO' will send |
| 77 | + all logs that have a severity of INFO or greater to the destination. |
| 78 | + See https://cloud.google.com/logging/docs/view/advanced_filters for more |
| 79 | + filter information. |
| 80 | + """ |
| 81 | + logging_client = logging.Client() |
| 82 | + sink = logging_client.sink(sink_name) |
| 83 | + |
| 84 | + sink.reload() |
| 85 | + |
| 86 | + sink.filter_ = filter_ |
| 87 | + print('Updated sink {}'.format(sink.name)) |
| 88 | + sink.update() |
| 89 | + # [END update] |
| 90 | + |
| 91 | + |
| 92 | +def delete_sink(sink_name): |
| 93 | + """Deletes a sink.""" |
| 94 | + logging_client = logging.Client() |
| 95 | + sink = logging_client.sink(sink_name) |
| 96 | + |
| 97 | + sink.delete() |
| 98 | + |
| 99 | + print('Deleted sink {}'.format(sink.name)) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + parser = argparse.ArgumentParser( |
| 104 | + description=__doc__, |
| 105 | + formatter_class=argparse.RawDescriptionHelpFormatter |
| 106 | + ) |
| 107 | + |
| 108 | + subparsers = parser.add_subparsers(dest='command') |
| 109 | + subparsers.add_parser('list', help=list_sinks.__doc__) |
| 110 | + |
| 111 | + create_parser = subparsers.add_parser('create', help=list_sinks.__doc__) |
| 112 | + create_parser.add_argument( |
| 113 | + 'sink_name', |
| 114 | + help='Name of the log export sink.') |
| 115 | + create_parser.add_argument( |
| 116 | + 'destination_bucket', |
| 117 | + help='Cloud Storage bucket where logs will be exported.') |
| 118 | + create_parser.add_argument( |
| 119 | + 'filter', |
| 120 | + help='The filter used to match logs.') |
| 121 | + |
| 122 | + update_parser = subparsers.add_parser('update', help=update_sink.__doc__) |
| 123 | + update_parser.add_argument( |
| 124 | + 'sink_name', |
| 125 | + help='Name of the log export sink.') |
| 126 | + update_parser.add_argument( |
| 127 | + 'filter', |
| 128 | + help='The filter used to match logs.') |
| 129 | + |
| 130 | + delete_parser = subparsers.add_parser('delete', help=delete_sink.__doc__) |
| 131 | + delete_parser.add_argument( |
| 132 | + 'sink_name', |
| 133 | + help='Name of the log export sink.') |
| 134 | + |
| 135 | + args = parser.parse_args() |
| 136 | + |
| 137 | + if args.command == 'list': |
| 138 | + list_sinks() |
| 139 | + elif args.command == 'create': |
| 140 | + create_sink(args.sink_name, args.destination_bucket, args.filter) |
| 141 | + elif args.command == 'update': |
| 142 | + update_sink(args.sink_name, args.filter) |
| 143 | + elif args.command == 'delete': |
| 144 | + delete_sink(args.sink_name) |
0 commit comments