简体   繁体   English

如何通过memory监控EC2实例?

[英]How to monitor EC2 instances by memory?

Using Cloudwatch you can monitor your EC2 instances by several criteria, such as.network usage, CPU usage, and so on…使用 Cloudwatch,您可以通过多种标准监控您的 EC2 实例,例如网络使用情况、CPU 使用情况等……

Unfortunately, there is no metric for memory consumption.不幸的是,没有 memory 消费的指标。 First of all, just out of curiosity, I would like to know, why?首先,出于好奇,我想知道,为什么? Can anybody explain why it is possible to, eg, monitor CPU usage, but not memory usage?谁能解释为什么可以监控 CPU 使用率,但不能监控 memory 的使用率? At least to me, that's not obvious.至少对我来说,这并不明显。

And then, my actual question: Okay, given that Cloudwatch doesn't allow monitoring the EC2 instances' memory usage - what is the alternative?然后,我的实际问题是:好的,鉴于 Cloudwatch 不允许监控 EC2 实例的 memory 使用情况——有什么替代方案? How should I setup an alarm if, eg > 80% of the memory of an instance is being used?如果实例的 memory 的 80% 以上被使用,我应该如何设置警报?

Memory and Disk specific statistics require AWS to monitor at the OS level rather than the host level, so that is why they leave it out by default. 内存和磁盘特定统计信息要求AWS在操作系统级别而不是主机级别进行监视,因此这是默认情况下将其保留的原因。 It will probably be added at some point but since it has been on the wish list for about 7 years, we can assume it's a very low priority item. 它可能会在某个时候添加,但由于它已经在愿望清单上大约7年,我们可以假设它是一个非常低优先级的项目。

The recommended way to monitor memory usage is to create a custom Cloudwatch metric by using your own monitoring scripts on your instances. 监视内存使用情况的推荐方法是在实例上使用自己的监视脚本创建自定义Cloudwatch指标。 AWS have published documentation on how to achieve this on Linux instances with a set of (unsupported) scripts. AWS已发布有关如何使用一组(不支持的)脚本在Linux实例上实现此目的的文档

Once your instances are publishing the custom metrics, you will be able to attach alarms to them in CloudWatch. 在您的实例发布自定义指标后,您将能够在CloudWatch中将警报附加到它们。

Well, now the new CloudWatch agent can collect metrics like memory and disk usage, see the docs. 那么,现在新的CloudWatch代理可以收集内存和磁盘使用等指标,请参阅文档。

With this you can monitor this kind of metrics, but you will need to install and configure the agent in the instances. 通过此,您可以监视此类指标,但您需要在实例中安装配置代理。

Well by default the AWS Ec2 does not provide memory metrics.默认情况下,AWS Ec2 不提供 memory 指标。 So we have to install the cloud watch agent separately and then get the metrics所以我们要单独安装cloud watch agent然后获取metrics

Steps:脚步:

  1. Download the cloud watch agent from s3从 s3 下载云监视代理

    sudo wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm须藤 wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm

  2. Install the cloud watch agent安装云监视代理

    sudo rpm -U./amazon-cloudwatch-agent.rpm sudo rpm -U./amazon-cloudwatch-agent.rpm

  3. We need to write a config file in which we will specify the metrics we need to put.我们需要编写一个配置文件,在其中指定我们需要放置的指标。

    sudo vi /opt/aws/amazon-cloudwatch-agent/bin/config.json sudo vi /opt/aws/amazon-cloudwatch-agent/bin/config.json

you need to insert this inside the config file您需要将其插入到配置文件中

{
    "metrics": {
        "metrics_collected": {
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 30
            }
        }
    }
}
  1. Then after specifying the metric then we just need to start the cloud watch agent然后在指定指标之后我们只需要启动云监视代理

    sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c 文件:/opt/aws/amazon-cloudwatch-agent/bin/config.json -秒

  2. You can go and test out if the metric is coming or not in the cloud watch page您可以拨打 go 并在云观察页面中测试指标是否出现

Check:查看:

Cloudwatch console --> All metrics --> under custom namespace --> CW Agent --> host --> ip of the instance server Cloudwatch console --> All metrics --> custom namespace下 --> CW Agent --> host --> 实例服务器的ip

Now you can view the memory usage and monitor the uage现在可以查看memory的使用情况,监控使用情况

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

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