def get_metric_data(self, monitor_graph: MonitorGraph, resource: Resource, token: str = None): params = dict(MetricDataQueries=[ dict(Id=monitor_graph.metric_name.lower(), MetricStat=dict(Metric=dict( Namespace=resource.get_namespace(), MetricName=monitor_graph.metric_name, Dimensions=[ dict(Name=resource.get_id_name(), Value=resource.resource_id) ]), Period=monitor_graph.period, Stat=monitor_graph.stat)) ], StartTime=monitor_graph.start_time, EndTime=monitor_graph.end_time, ScanBy="TimestampAscending", MaxDatapoints=500) if token: params["NextToken"] = token response = self.client.get_metric_data(**params) return response
def put_metric_alarms(self, resource: Resource, topic_arn: str): """ アラームを設定する 一つのメトリクスに対しNARUKOで取り扱う監視レベルの数だけアラームを設定する 最も低いレベルのアラームには復旧時のトリガーを設定する :param resource: :param topic_arn: :return: """ monitor = resource.monitors[0] for monitor_value in monitor.monitor_values: params = dict( AlarmName=CloudWatch.NARUKO_ALARM_NAME.format( resource.get_service_name(), resource.resource_id, monitor.metric.name, monitor_value.level.name), ActionsEnabled=monitor.enabled, AlarmActions=[topic_arn], MetricName=monitor.metric.name, Namespace=resource.get_namespace(), Statistic=monitor.statistic, Dimensions=[ dict(Name=resource.get_id_name(), Value=resource.resource_id) ], Period=monitor.period, EvaluationPeriods=1, Threshold=monitor_value.value, ComparisonOperator=monitor.metric.comparison_operator) # 最も安全なレベルのアラームには復旧時の通知を設定する if monitor_value.level.is_lowest_level(): params.update(dict(OKActions=[topic_arn])) self.client.put_metric_alarm(**params)
def graph(self, request_user: UserModel, resource: Resource, aws: AwsEnvironmentModel, monitor_graph: MonitorGraph): self.logger.info("START: graph") # 使用できるAWSアカウントか if not request_user.has_aws_env(aws): raise PermissionDenied( "request user can't use aws account. user_id: {}, aws_id: {}". format(request_user.id, aws.id)) if monitor_graph.metric_name not in resource.get_metrics(): raise ObjectDoesNotExist( "service doesn't have metric service_type: {} metric: {}". format(resource.get_service_name(), monitor_graph.metric_name)) # API引数をresourceから充足 monitor_graph.service_name = resource.get_namespace() monitor_graph.dimensions.append( dict(Name=resource.get_id_name(), Value=resource.resource_id)) monitor_graph = CloudWatch(aws, resource.region).get_chart(monitor_graph) self.logger.info("END: graph") return monitor_graph