简体   繁体   English

Amazon EC2 Cloudwatch python插件Boto不返回指标

[英]Amazon EC2 Cloudwatch python plugin boto not returning metrics

I'm using the boto plugin for python to query Amazon's CloudWatch EC2. 我正在使用boto python插件来查询Amazon的CloudWatch EC2。 I'm trying to get metrics for individual hosts. 我正在尝试获取各个主机的指标。 I first start by building a list of host and metrics that I want to query and I make the query. 首先,我首先构建要查询的主机和指标的列表,然后进行查询。 I am following the documentation percisely and I'm not sure why with ElastiCache I'm not getting any data returned, but I have another script against ELB and it returns queried data just fine. 我正在精确地遵循文档,并且不确定使用ElastiCache为何没有返回任何数据,但是我有另一个针对ELB的脚本,它可以返回查询的数据。

There is no data returned for the clusters that I am querying. 我正在查询的集群没有返回任何数据。 If I just return a list of all metrics being recorded for every host I have verified there is data for each cluster. 如果我只返回已验证的每台主机记录的所有指标的列表,则每个群集都有数据。 Thus, I have narrowed it down to a problem with my query or with the module. 因此,我将其范围缩小到查询或模块的问题。

The returned stats are completely blank for every cluster node and no error is given. 对于每个群集节点,返回的统计信息完全空白,并且未给出错误。

[] GetMisses ctlive-master [] GetMisses ctlive-master

[] CmdGet ctlive-master [] CmdGet ctlive-master

[] CPUUtilization ctlive-master [] CPUUtilization ctlive-master

[] CmdSet ctlive-master [] CmdSet ctlive-master

import boto.ec2.cloudwatch
import datetime
import os
import subprocess

end = datetime.datetime.utcnow()
start = end - datetime.timedelta(minutes=5)
period = 300

conn = boto.ec2.cloudwatch.connect_to_region("us-east-1",)

metrics = conn.list_metrics()

metric_list = [ 'GetMisses',
                'CmdGet',
                'CPUUtilization',
                'CmdSet']

instance_list = {}

for metric in metrics:
    try: 
        instance_list[metric.dimensions['DBInstanceIdentifier'][0]] = {'Name': metric.dimensions['DBInstanceIdentifier'][0]}
    except:
        continue

for instance in instance_list:
    for metric in metric_list:
        instance = str(instance)
        metric_return = conn.get_metric_statistics(period, start,end,metric,'AWS/ElastiCache','Sum',dimensions={'CacheClusterId':instance})
        print metric_return, metric, instance 

Amazon SDK Link http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/elasticache-metricscollected.html Amazon SDK链接http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/elasticache-metricscollected.html

boto SDK boto SDK

http://boto.readthedocs.org/en/latest/cloudwatch_tut.html http://boto.readthedocs.org/en/latest/cloudwatch_tut.html

This is the answer to the question. 这就是问题的答案。 When you are trying to query ElastiCache you need to include the CacheNodeId as well. 当您尝试查询ElastiCache时,还需要包括CacheNodeId。

{'CacheClusterId':cluster_name,'CacheNodeId':cluster_id}) { 'CacheClusterId':群集名, 'CacheNodeId':CLUSTER_ID})

def get_metric(cluster_name, cluster_id, metric, warning, critical):
    end = datetime.datetime.utcnow()
    start = end - datetime.timedelta(minutes=5)
    period = 300

    conn = boto.ec2.cloudwatch.connect_to_region("us-east-1")    

    if 'CPU' in metric:
        CPU = conn.get_metric_statistics(period, start, end, 'CPUUtilization','AWS/ElastiCache', 'Average', dimensions={'CacheClusterId':cluster_name,'CacheNodeId':cluster_id})

        if CPU[0]['Average'] < warning:
            print ('CPU Utilization for host %s is within limits|cpu_usage=%s' % (cluster_name, CPU[0]['Average']))
            sys.exit(0)
        if CPU[0]['Average'] >= warning and CPU[0]['Average'] < critical:
            print ('CPU Utilization for host %s is within the warning range|cpu_usage=%s' % (cluster_name, CPU[0]['Average']))
            sys.exit(1)
        if CPU[0]['Average'] >= critical:
            print ('CPU Utilization for host %s is within the critical range|cpu_usage=%s' % (cluster_name, CPU[0]['Average']))
            sys.exit(2)

    elif 'Misses' in metric:
        GetMisses = conn.get_metric_statistics(period, start, end, 'GetMisses','AWS/ElastiCache', 'Sum', dimensions={'CacheClusterId':cluster_name,'CacheNodeId':cluster_id})
        if GetMisses[0]['Sum'] < warning:
            print ('GetMisses for host %s is within limits|command_misses=%s' % (cluster_name, GetMisses[0]['Sum']))
            sys.exit(0)
        if GetMisses[0]['Sum'] >= warning and GetMisses[0]['Sum'] < critical:
            print ('GetMisses for host %s is within the warning range|command_misses=%s' % (cluster_name, GetMisses[0]['Sum']))
            sys.exit(1)
        if GetMisses[0]['Sum'] >= critical:
            print ('GetMisses for host %s is within the critical range|command_misses=%s' % (cluster_name, GetMisses[0]['Sum']))
            sys.exit(2)

    elif 'Set' in metric:
        CmdSet = conn.get_metric_statistics(period, start, end, 'CmdSet','AWS/ElastiCache', 'Sum', dimensions={'CacheClusterId':cluster_name,'CacheNodeId':cluster_id})
        if CmdSet[0]['Sum'] < warning:
            print ('CmdSet for host %s is within limits|command_set=%s;' % (cluster_name, CmdSet[0]['Sum']))
            sys.exit(0)
        if CmdSet[0]['Sum'] >= warning and CmdSet[0]['Sum'] < critical:
            print ('CmdSet for host %s is within the warning range|command_set=%s;' % (cluster_name, CmdSet[0]['Sum']))
            sys.exit(1)
        if CmdSet[0]['Sum'] >= critical:
            print ('CmdSet for host %s is within the critical range|command_set=%s;' % (cluster_name, CmdSet[0]['Sum']))
            sys.exit(2)

    elif 'Get' in metric:
        CmdGet = conn.get_metric_statistics(period, start, end, 'CmdGet','AWS/ElastiCache', 'Sum', dimensions={'CacheClusterId':cluster_name,'CacheNodeId':cluster_id})
        if CmdGet[0]['Sum'] < warning:
            print ('CmdGet for host %s is within limits|command_get=%s;' % (cluster_name, CmdGet[0]['Sum']))
            sys.exit(0)
        if CmdGet[0]['Sum'] >= warning and CmdGet[0]['Sum'] < critical:
            print ('CmdGet for host %s is within the warning range|command_get=%s;' % (cluster_name, CmdGet[0]['Sum']))
            sys.exit(1)
        if CmdGet[0]['Sum'] >= critical:
            print ('CmdGet for host %s is within the critical range|command_get=%s;' % (cluster_name, CmdGet[0]['Sum']))
            sys.exit(2)

def main():

    parser=argparse.ArgumentParser(description="Amazon ElastiCache Metrics")
    parser.add_argument('-W',action="store",dest="warning",type=float,required=True)
    parser.add_argument('-C',action="store",dest="critical",type=float,required=True)
    parser.add_argument('-M',action="store",dest="metric",type=str,required=True)
    parser.add_argument('-H',action="store",dest="host",type=str,required=True)
    parser.add_argument('-I',action="store",dest="host_id",type=str,required=True)
    args=parser.parse_args()
    get_metric(args.host, args.host_id, args.metric, args.warning, args.critical)
    #CPU, Misses, Set,Get

if __name__ == "__main__":
  main()                            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM