diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8121576..37e8466 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,11 +3,11 @@ repos: hooks: - id: check-hooks-apply - id: check-useless-excludes - - repo: git://github.com/pre-commit/pre-commit-hooks + - repo: git@github.com:pre-commit/pre-commit-hooks rev: v2.2.3 hooks: - id: check-merge-conflict - - repo: git://github.com/Lucas-C/pre-commit-hooks.git + - repo: git@github.com:Lucas-C/pre-commit-hooks rev: v1.1.7 hooks: - id: insert-license diff --git a/aws_exporter/aws/ses.py b/aws_exporter/aws/ses.py new file mode 100644 index 0000000..0bf746a --- /dev/null +++ b/aws_exporter/aws/ses.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +# ISC License +# +# Copyright 2019 FL Fintech E GmbH +# +# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import boto3 +import logging + +from prometheus_client.core import GaugeMetricFamily + +from aws_exporter.aws import COMMON_LABELS +from aws_exporter.aws.sts import get_account_id +from aws_exporter.util import strget + + +SES = boto3.client("ses") +LOGGER = logging.getLogger(__name__) +SES_LABELS = COMMON_LABELS + + +class AWSSESMetricsCollector: + def _metrics_containers(self): + return { + 'aws_ses_send_quota_max': GaugeMetricFamily( + 'aws_ses_send_quota_max', + 'The maximum number of emails the user is allowed to send in a 24-hour interval.', + labels=SES_LABELS + ), + 'aws_ses_send_quota_rate_max': GaugeMetricFamily( + 'aws_ses_send_quota_rate_max', + 'The maximum number of emails that Amazon SES can accept from the user\'s account per second.', + labels=SES_LABELS + ), + 'aws_ses_send_quota_used': GaugeMetricFamily( + 'aws_ses_send_quota_used', + 'The number of emails sent during the previous 24 hours.', + labels=SES_LABELS + ), + } + + def describe(self): + return self._metrics_containers().values() + + def collect(self): + metrics = self._metrics_containers() + labels = [get_account_id()] + mapping = { + 'aws_ses_send_quota_max': 'Max24HourSend', + 'aws_ses_send_quota_rate_max': 'MaxSendRate', + 'aws_ses_send_quota_used': 'SentLast24Hours', + } + + LOGGER.debug('querying sending quotas') + + response = SES.get_send_quota() + + for metric_key, response_key in mapping.items(): + metrics[metric_key].add_metric(labels, strget(response, response_key)) + yield metrics[metric_key] + + LOGGER.debug('finished querying sending quotas') + +# -*- coding: utf-8 -*- diff --git a/aws_exporter/cli.py b/aws_exporter/cli.py index 692d585..b7268ae 100644 --- a/aws_exporter/cli.py +++ b/aws_exporter/cli.py @@ -12,7 +12,7 @@ import logging from prometheus_client import start_http_server - +from aws_exporter.util import strtobool from aws_exporter.metrics import MetricsCollector LOGGER = logging.getLogger(__name__) @@ -22,25 +22,32 @@ def main(): delay = int(os.environ.get('AWS_EXPORTER_POLL_DELAY', 30)) port = int(os.environ.get('AWS_EXPORTER_PORT', 8000)) log_level = getattr(logging, os.environ.get('AWS_EXPORTER_LOG_LEVEL', 'info').upper()) + metrics_config = { + 'enable_ec2': strtobool(os.environ.get('ENABLE_EC2_METRICS', 'True')), + 'enable_sns': strtobool(os.environ.get('ENABLE_SNS_METRICS', 'True')), + 'enable_ses': strtobool(os.environ.get('ENABLE_SES_METRICS', 'True')), + 'enable_backup': strtobool(os.environ.get('ENABLE_BACKUP_METRICS','True')), + 'ec2': dict(ami_owners=['self']), + } + logging.basicConfig(level=log_level) logging.getLogger('botocore').setLevel(logging.WARN) logging.getLogger('urllib3').setLevel(logging.WARN) - ami_owners = ['self'] additional_ami_owners = os.environ.get('AWS_EXPORTER_EC2_AMI_OWNERS') if additional_ami_owners is not None: - ami_owners.extend(additional_ami_owners.split(',')) + metrics_config['ec2']['ami_owners'].extend(additional_ami_owners.split(',')) - LOGGER.debug('getting amis from owners: %s', ami_owners) + LOGGER.debug('getting amis from owners: %s', metrics_config['ec2']['ami_owners']) try: LOGGER.info('listening on port %d', port) start_http_server(port) - metrics = MetricsCollector(ec2_config=dict(ami_owners=ami_owners)) + metrics = MetricsCollector(metrics_config=metrics_config) metrics.run_loop(delay) except KeyboardInterrupt: exit(137) diff --git a/aws_exporter/metrics.py b/aws_exporter/metrics.py index 6c36a7a..dae12c5 100644 --- a/aws_exporter/metrics.py +++ b/aws_exporter/metrics.py @@ -16,15 +16,24 @@ from aws_exporter.aws.ec2 import AWSEC2MetricsCollector from aws_exporter.aws.backup import AWSBackupMetricsCollector from aws_exporter.aws.sns import AWSSNSMetricsCollector +from aws_exporter.aws.ses import AWSSESMetricsCollector LOGGER = logging.getLogger(__name__) class MetricsCollector: - def __init__(self, ec2_config = None): - REGISTRY.register(AWSEC2MetricsCollector(config = ec2_config)) - REGISTRY.register(AWSSNSMetricsCollector()) - REGISTRY.register(AWSBackupMetricsCollector()) + def __init__(self, metrics_config): + if metrics_config['enable_ec2']: + REGISTRY.register(AWSEC2MetricsCollector(config = metrics_config['ec2'])) + + if metrics_config['enable_sns']: + REGISTRY.register(AWSSNSMetricsCollector()) + + if metrics_config['enable_backup']: + REGISTRY.register(AWSBackupMetricsCollector()) + + if metrics_config['enable_ses']: + REGISTRY.register(AWSSESMetricsCollector()) def run_loop(self, polling_interval_seconds = 30): while True: diff --git a/aws_exporter/util.py b/aws_exporter/util.py index 5efa275..44483b4 100644 --- a/aws_exporter/util.py +++ b/aws_exporter/util.py @@ -53,3 +53,6 @@ def function_wrapper(*args, **kwargs): def strget(dict_obj, value, default=None): return str(dict_obj.get(value, default)) + +def strtobool(string): + return string.lower() in ('true', '1', 't')