Cloudwatch 客户端安装

采用AWS的amazon linux 2 标准AMI或基于这个AMI做定制的镜像安装的EC2实例默认已经集成了Cloudwatch客户端。
手工安装客户端可以参考官方文档:
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/download-cloudwatch-agent-commandline.html

Cloudwatch配置样例

可以通过如下语句以向导方式生成一个配置模版:

  1. /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

可以在这个模版基础上做修改,下面是一个定制后的配置样例:

  1. {
  2. "agent": {
  3. "metrics_collection_interval": 10,
  4. "run_as_user": "root"
  5. },
  6. "metrics": {
  7. "append_dimensions": {
  8. "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
  9. "ImageId": "${aws:ImageId}",
  10. "InstanceId": "${aws:InstanceId}",
  11. "InstanceType": "${aws:InstanceType}"
  12. },
  13. "metrics_collected": {
  14. "cpu": {
  15. "resources": [
  16. "*"
  17. ],
  18. "measurement": [
  19. {
  20. "name": "cpu_usage_idle",
  21. "unit": "Percent"
  22. },
  23. {
  24. "name": "cpu_usage_iowait",
  25. "unit": "Percent"
  26. },
  27. {
  28. "name": "cpu_usage_system",
  29. "unit": "Percent"
  30. },
  31. {
  32. "name": "cpu_usage_user",
  33. "unit": "Percent"
  34. },
  35. {
  36. "name": "cpu_usage_nice",
  37. "unit": "Percent"
  38. },
  39. "cpu_usage_guest"
  40. ],
  41. "totalcpu": false,
  42. "metrics_collection_interval": 10
  43. },
  44. "netstat": {
  45. "measurement": [
  46. "tcp_established",
  47. "tcp_syn_sent",
  48. "tcp_close"
  49. ],
  50. "metrics_collection_interval": 60
  51. },
  52. "disk": {
  53. "measurement": [
  54. "used_percent",
  55. "disk_inodes_free"
  56. ],
  57. "resources": [
  58. "/",
  59. "/home/t4",
  60. "/emr",
  61. "/mnt",
  62. "/mnt1",
  63. "/var/log"
  64. ],
  65. "metrics_collection_interval": 60,
  66. "drop_device": true
  67. },
  68. "processes": {
  69. "measurement": [
  70. "running",
  71. "sleeping",
  72. "dead"
  73. ]
  74. },
  75. "mem": {
  76. "measurement": [
  77. "mem_used_percent",
  78. "mem_used",
  79. "mem_cached",
  80. "mem_total"
  81. ],
  82. "metrics_collection_interval": 10
  83. }
  84. }
  85. }
  86. }

配置推送到 ssm的parameter store

可以将上述配置保存成文件,然后推送到ssm的parameter store,方便各个机器共享配置文件,实现统一配置管理。

  1. aws ssm put-parameter --name "parameter name" --type "String" --value file://configuration_file_pathname

配置生效

在EC2上执行如下语句将parameter store上的配置文件下载到本地,并重启服务生效:

  1. /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c ssm:AmazonCloudWatch-linux