{"id":1948,"date":"2020-05-21T14:39:06","date_gmt":"2020-05-21T11:39:06","guid":{"rendered":"https:\/\/artem.services\/?p=1944"},"modified":"2020-05-21T14:46:09","modified_gmt":"2020-05-21T11:46:09","slug":"1948","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1948&lang=en","title":{"rendered":"AWS &#8212; Script to get metrics from CloudWatch"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-214\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo.png\" alt=\"\" width=\"975\" height=\"450\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo.png 975w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-300x138.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-768x354.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-954x440.png 954w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><\/p>\n<p><strong>Python3<\/strong> sample script to get metrics from <strong>AWS CloudWatch<\/strong>. In the example, we get the maximum value in the last minute and display only the value, this is necessary if you want to collect metrics for example in <strong>Zabbix<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<h3>Script:<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python3\r\n\r\nimport boto3\r\nimport datetime\r\n\r\nawsRegion = &quot;eu-west-1&quot; \r\nnamespace = &quot;AWS\/ElastiCache&quot;\r\nmetric = &quot;CurrConnections&quot;\r\nstatistics = &quot;Maximum&quot;\r\nperiod = 60 # Seconds\r\ntimeRange = 1 # Minutes \r\n\r\nclient = boto3.client('cloudwatch', region_name=awsRegion)\r\n\r\nstartTime = (datetime.datetime.utcnow() - datetime.timedelta(minutes=timeRange))\r\nstartTime = startTime.strftime(&quot;%Y-%m-%dT%H:%M:%S&quot;)\r\nendTime = datetime.datetime.utcnow()\r\nendTime = endTime.strftime(&quot;%Y-%m-%dT%H:%M:%S&quot;)\r\n\r\nresponse = client.get_metric_statistics(\r\n    Namespace=namespace,\r\n    MetricName=metric,\r\n    StartTime=startTime,\r\n    EndTime=endTime,\r\n    Period=period,\r\n    Statistics=[\r\n        statistics,\r\n    ]\r\n)\r\n\r\nfor cw_metric in response['Datapoints']:\r\n    print(cw_metric['Maximum'])\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python3 sample script to get metrics from AWS CloudWatch. In the example, we get the maximum value in the last minute and display only the value, this is necessary if you want to collect metrics for example in Zabbix. &nbsp; Script:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[599,405],"tags":[543,1439,887,1527,1529],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1948"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1948"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1948\/revisions"}],"predecessor-version":[{"id":1950,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1948\/revisions\/1950"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}