Пример #1
0
    def GetBucketSize(self, bucket: str, timeframe: int = 1) -> Dict[str, int]:
        """List the size of a Google Storage Bucket in a project (default: last 1
    day).

    Note: This will list the _maximum size_
          (in bytes) the bucket had in the timeframe.

    Ref: https://cloud.google.com/monitoring/api/metrics_gcp#gcp-storage

    Args:
      bucket (str):  Name of a bucket in GCS.
      timeframe (int): Optional. The number (in days) for
          which to measure activity.
          Default: 1 day.

    Returns:
      Dict[str, int]: Dictionary mapping bucket name to its size (in bytes).
    """

        start_time = common.FormatRFC3339(datetime.datetime.utcnow() -
                                          datetime.timedelta(days=timeframe))
        end_time = common.FormatRFC3339(datetime.datetime.utcnow())
        period = timeframe * 24 * 60 * 60

        assert self.project_id  # Necessary for mypy check
        gcm = gcp_monitoring.GoogleCloudMonitoring(self.project_id)
        gcm_api = gcm.GcmApi()
        gcm_timeseries_client = gcm_api.projects().timeSeries()
        qfilter = ('metric.type="storage.googleapis.com/storage/total_bytes" '
                   'resource.type="gcs_bucket"')
        qfilter += ' resource.label.bucket_name="{0:s}"'.format(bucket)

        responses = common.ExecuteRequest(
            gcm_timeseries_client, 'list', {
                'name': 'projects/{0:s}'.format(self.project_id),
                'filter': qfilter,
                'interval_startTime': start_time,
                'interval_endTime': end_time,
                'aggregation_groupByFields': 'resource.label.bucket_name',
                'aggregation_perSeriesAligner': 'ALIGN_MAX',
                'aggregation_alignmentPeriod': '{0:d}s'.format(period),
                'aggregation_crossSeriesReducer': 'REDUCE_NONE'
            })

        ret = {}
        for response in responses:
            for ts in response.get('timeSeries', []):
                bucket = ts.get('resource', {}).get('labels',
                                                    {}).get('bucket_name', '')
                if bucket:
                    points = ts.get('points', [])
                    for point in points:
                        val = point.get('value', {}).get('doubleValue', 0)
                        if bucket not in ret:
                            ret[bucket] = val
                        elif val > ret[bucket]:
                            ret[bucket] = val
        return ret
Пример #2
0
    def monitoring(self) -> monitoring_module.GoogleCloudMonitoring:
        """Get a GoogleCloudMonitoring object for the project.

    Returns:
      GoogleCloudMonitoring: Object that represents Google Monitoring.
    """

        if self._monitoring:
            return self._monitoring
        self._monitoring = monitoring_module.GoogleCloudMonitoring(
            self.project_id)
        return self._monitoring
Пример #3
0
def ListServices(args: 'argparse.Namespace') -> None:
    """List active GCP services (APIs) for a project.

  Args:
    args (argparse.Namespace): Arguments from ArgumentParser.
  """
    apis = gcp_monitoring.GoogleCloudMonitoring(args.project)
    results = apis.ActiveServices()
    logger.info('Found {0:d} APIs:'.format(len(results)))
    sorted_apis = sorted(results.items(), key=lambda x: x[1], reverse=True)
    for apiname, usage in sorted_apis:
        logger.info('{0:s}: {1:s}'.format(apiname, usage))
Пример #4
0
]

FAKE_LOG_ENTRIES = [{
    'logName': 'test_log',
    'timestamp': '123456789',
    'textPayload': 'insert.compute.create'
}, {
    'logName': 'test_log',
    'timestamp': '123456789',
    'textPayload': 'insert.compute.create'
}]

FAKE_NEXT_PAGE_TOKEN = 'abcdefg1234567'
FAKE_GCS = gcp_storage.GoogleCloudStorage('fake-target-project')
FAKE_GCB = gcp_build.GoogleCloudBuild('fake-target-project')
FAKE_MONITORING = gcp_monitoring.GoogleCloudMonitoring('fake-target-project')

# Mock struct to mimic GCP's API responses
MOCK_INSTANCES_AGGREGATED = {
    # See https://cloud.google.com/compute/docs/reference/rest/v1/instances
    # /aggregatedList for complete structure
    'items': {
        0: {
            'instances': [{
                'name': FAKE_INSTANCE.name,
                'zone': '/' + FAKE_INSTANCE.zone
            }]
        }
    }
}