Zabbix实战-简易教程--AWS/RDS/ElastiCache监控

作者:@skyflask
转载本文请注明出处:https://www.cnblogs.com/skyflask/p/12917816.html


目录

一、需求
二、解决方法
三、操作
四、参考文献和文件

一、需求

同事说最近接手的业务大部分都是AWS的,希望能够监控aws的RDS和ElastiCache。我二话不说,看了一下AWS的api,说,没问题,提单吧。

 

二、解决方法

难点:RDS和ElastiCache都是没有IP的,所以只能通过API来取值。

zabbix解决方式:采用zabbix 的External checks方式。

注意:
此方式原理为,zabbix server上有External脚本,它会根据Agent上配置的hostname去进行数据采集

此方式的优点:对于无Agent而只有api的场景非常适应;缺点是:会增加server或proxy的压力。

 

三、操作

1、server或proxy编写脚本

需要安装boto3,aws的sdk。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
import datetime
import sys
from optparse import OptionParser
import boto3
 
### Arguments
parser = OptionParser()
parser.add_option("-i", "--instance-id", dest="instance_id",
                help="DBInstanceIdentifier")
parser.add_option("-a", "--access-key", dest="access_key", default="",
                help="AWS Access Key")
parser.add_option("-k", "--secret-key", dest="secret_key", default="",
                help="AWS Secret Access Key")
parser.add_option("-m", "--metric", dest="metric",
                help="RDS cloudwatch metric")
parser.add_option("-r", "--region", dest="region", default="us-east-1",
                help="RDS region")
 
(options, args) = parser.parse_args()
 
if (options.instance_id == None):
    parser.error("-i DBInstanceIdentifier is required")
if (options.metric == None):
    parser.error("-m RDS cloudwatch metric is required")
###
 
if not options.access_key or not options.secret_key:
    use_roles = True
else:
    use_roles = False
 
### Real code
metrics = {"CPUUtilization":{"type":"float", "value":None},
    "ReadLatency":{"type":"float", "value":None},
    "DatabaseConnections":{"type":"int", "value":None},
    "FreeableMemory":{"type":"float", "value":None},
    "ReadIOPS":{"type":"int", "value":None},
    "WriteLatency":{"type":"float", "value":None},
    "WriteThroughput":{"type":"float", "value":None},
    "WriteIOPS":{"type":"int", "value":None},
    "SwapUsage":{"type":"float", "value":None},
    "ReadThroughput":{"type":"float", "value":None},
    "DiskQueueDepth":{"type":"float", "value":None},
    "ReplicaLag":{"type":"int", "value":None},
    "DiskQueueDepth":{"type":"float", "value":None},
    "ReplicaLag":{"type":"int", "value":None},
    "NetworkReceiveThroughput":{"type":"float", "value":None},
    "NetworkTransmitThroughput":{"type":"float", "value":None},
    "FreeStorageSpace":{"type":"float", "value":None}}
end = datetime.datetime.utcnow()
start = end - datetime.timedelta(minutes=5)
 
### Zabbix hack for supporting FQDN addresses
### This is useful if you have instances with the same nam but in diffrent AWS locations (i.e. db1 in eu-central-1 and db1 in us-east-1)
if "." in options.instance_id:
    options.instance_id = options.instance_id.split(".")[0]
 
if use_roles:
    conn = boto3.client('cloudwatch', region_name=options.region)
else:
    conn = boto3.client('cloudwatch', aws_access_key_id=options.access_key, aws_secret_access_key=options.secret_key, region_name=options.region)
 
if options.metric in metrics.keys():
  k = options.metric
  vh = metrics[options.metric]
   
  try:
          res = conn.get_metric_statistics(Namespace="AWS/RDS", MetricName=k, Dimensions=[{'Name': "DBInstanceIdentifier", 'Value': options.instance_id}], StartTime=start, EndTime=end, Period=60, Statistics=["Average"])
  except Exception as e:
          print("status err Error running rds_stats: %s" % e)
          sys.exit(1)
  datapoints = res.get('Datapoints')
  if len(datapoints) == 0:
      print("Could not find datapoints for specified instance. Please review if provided instance (%s) and region (%s) are correct" % (options.instance_id, options.region)) # probably instance-id is wonrg
       
  average = datapoints[-1].get('Average') # last item in result set
  if (k == "FreeStorageSpace" or k == "FreeableMemory"):
          average = average / 1024.0**3.0
  if vh["type"] == "float":
          metrics[k]["value"] = "%.4f" % average
  if vh["type"] == "int":
          metrics[k]["value"] = "%i" % average
 
  #print "metric %s %s %s" % (k, vh["type"], vh["value"])
  print("%s" % (vh["value"]))

  注意:脚本存放位置:默认为/usr/share/zabbix/externalscripts

 

2、web端配置

a、主机设置

 

 

 

 

 

b、macro配置

 

 

c、模板导入

 

 

 

d、效果图

 

 

四、参考文献和文件

文件地址:

https://github.com/loveqx/zabbix-doc/tree/master/zabbix-scripts/zabbix-template-aws-RDS-ElastiCache

posted @   skyflask  阅读(1834)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2018-05-19 JavaScript快速入门-ECMAScript本地对象(RexExp)
2018-05-19 JavaScript快速入门-ECMAScript本地对象(Date)
2018-05-19 JavaScript快速入门-ECMAScript本地对象(Number)
点击右上角即可分享
微信分享提示