Skip to content

Commit

Permalink
first push
Browse files Browse the repository at this point in the history
  • Loading branch information
naorlivne committed May 16, 2018
0 parents commit 1de2acb
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[Dolphin]
Timestamp=2017,11,21,12,7,26
Version=3

[Settings]
HiddenFilesShown=true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
/idea
idea/
idea/*
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:2.7-alpine

COPY /mesos-cloudwatch-autoscale.py /mesos-cloudwatch-autoscale.py

RUN pip install requests boto3

CMD python /mesos-cloudwatch-autoscale.py
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# mesos-cloudwatch-autoscale

simple container which designed to run inside metronome (or other cron like mesos framework) which takes the current CPU\mem\HD percentage and exports it to cloudwatch every X minutes in order to allow that metrics to be used to autoscale the mesos cluster workers according to current use percentage.

required envs:
* AWS_ACCESS_KEY_ID (or IAM role)
* AWS_SECRET_ACCESS_KEY (or IAM role)
* AWS_DEFAULT_REGION (example: us-east-1)
* MESOS_URL (example: leader.mesos for use inside mesos)
* MESOS_PORT (example: 5050)
* METRICS_PREFIX (example: mesos_)
* METRICS_NAMESPCE (example: mesos/)

the script will then create 3 new cloudwatch metrics named:
* metrics_prefix_mem_percentage
* metrics_prefix_cpu_percentage
* metrics_prefix_hd_percentage

example metronome job config:
``````
{
"id": "mesos-cloudwatch-autoscale",
"run": {
"cmd": "docker pull vidazoohub/mesos-cloudwatch-autoscale:latest && docker run --rm -e METRICS_NAMESPACE=mesos/ -e AWS_ACCESS_KEY_ID=your_aws_Key -e AWS_SECRET_ACCESS_KEY=your_aws_secret -e MESOS_URL=http://leader.mesos -e AWS_DEFAULT_REGION=us-east-1 -e MESOS_PORT=5050 -e METRICS_PREFIX=vidazoo_dcos vidazoohub/mesos-cloudwatch-autoscale:latest",
"cpus": 0.1,
"mem": 256,
"disk": 100
},
"schedules": [
{
"id": "default",
"enabled": true,
"cron": "*/5 * * * *",
"timezone": "UTC",
"concurrencyPolicy": "ALLOW",
"startingDeadlineSeconds": 30
}
]
}
````````
81 changes: 81 additions & 0 deletions mesos-cloudwatch-autoscale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import os, requests, boto3, time
mesos_url = os.environ["MESOS_URL"]
mesos_port = os.environ["MESOS_PORT"]
metrics_prefix = os.environ["METRICS_PREFIX"]
metrics_namespace = os.environ["METRICS_NAMESPACE"]


def get_mesos_metrics(mesos_url, mesos_port):
headers = {
'cache-control': "no-cache"
}
response = requests.request("GET", mesos_url + ":" + mesos_port + "/metrics/snapshot", headers=headers)
mesos_info = response.json()
cpu_percent, mem_percent, hd_percent = (mesos_info["master/cpus_percent"] * 100.0), \
(mesos_info["master/mem_percent"] * 100.0), \
(mesos_info["master/disk_percent"] * 100.0)
return cpu_percent, mem_percent, hd_percent


def send_to_cloudwatch(metrics_prefix, metrics_namespace, used_cpu_percent, used_mem_percent, used_hd_percent):
client = boto3.client(
'cloudwatch'
)

client.put_metric_data(
Namespace=metrics_namespace,
MetricData=[
{
'MetricName': metrics_prefix + "_cpu_percentage",
'Dimensions': [
{
'Name': 'cpu_percentage',
'Value': 'cpu_percentage'
},
],
'Timestamp': int(time.time()),
'Value': used_cpu_percent,
'Unit': 'Percent'
},
]
)

client.put_metric_data(
Namespace=metrics_namespace,
MetricData=[
{
'MetricName': metrics_prefix + "_mem_percentage",
'Dimensions': [
{
'Name': 'mem_percentage',
'Value': 'mem_percentage'
},
],
'Timestamp': int(time.time()),
'Value': used_mem_percent,
'Unit': 'Percent'
},
]
)

client.put_metric_data(
Namespace=metrics_namespace,
MetricData=[
{
'MetricName': metrics_prefix + "_hd_percentage",
'Dimensions': [
{
'Name': 'hd_percentage',
'Value': 'hd_percentage'
},
],
'Timestamp': int(time.time()),
'Value': used_hd_percent,
'Unit': 'Percent'
},
]
)


used_cpu_percent, used_mem_percent, used_hd_percent = get_mesos_metrics(mesos_url, mesos_port)
send_to_cloudwatch(metrics_prefix, metrics_namespace, used_cpu_percent, used_mem_percent, used_hd_percent)

0 comments on commit 1de2acb

Please sign in to comment.