Skip to content

Commit

Permalink
Merge pull request #8 from niaid/dev
Browse files Browse the repository at this point in the history
Add metric_description property, fix uptime determination
  • Loading branch information
stolarczyk committed Nov 23, 2022
2 parents bdd2f01 + bb2c09c commit ba891e8
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 28 deletions.
1 change: 1 addition & 0 deletions cloudwatcher/metric_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def __call__(self, target: str, metric_unit: str) -> None:
if self.timed_metric.label.startswith("mem") and metric_unit == "Bytes":
metric_unit = "GB"
values = [convert_mem(v, force_suffix=metric_unit)[0] for v in values]
plt.figure()
plt.plot(
self.timed_metric.timestamps,
values,
Expand Down
11 changes: 6 additions & 5 deletions cloudwatcher/metricwatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(
metric_name: str,
metric_id: str,
metric_unit: Optional[str] = None,
metric_description: Optional[str] = None,
aws_access_key_id: Optional[str] = None,
aws_secret_access_key: Optional[str] = None,
aws_session_token: Optional[str] = None,
Expand Down Expand Up @@ -162,12 +163,12 @@ def get_ec2_uptime(
"""
if not self.is_ec2_running(ec2_instance_id):
_LOGGER.info(
f"Instance '{self.dimension_value}' is not running anymore. "
f"Instance '{ec2_instance_id}' is not running anymore. "
f"Uptime will be estimated based on reported metrics in "
f"the last {days} days"
)
instances = self.ec2_resource.instances.filter(
Filters=[{"Name": "instance-id", "Values": [self.dimension_value]}]
Filters=[{"Name": "instance-id", "Values": [ec2_instance_id]}]
)
# get the latest reported metric
metrics_response = self.query_ec2_metrics(
Expand All @@ -187,14 +188,14 @@ def get_ec2_uptime(
earliest_metric_report_time - latest_metric_report_time
).total_seconds()
except IndexError:
_LOGGER.warning(f"No metric data found for EC2: {self.dimension_value}")
_LOGGER.warning(f"No metric data found for EC2: {ec2_instance_id}")
return
instances = self.ec2_resource.instances.filter(
Filters=[{"Name": "instance-id", "Values": [self.dimension_value]}]
Filters=[{"Name": "instance-id", "Values": [ec2_instance_id]}]
)
for instance in instances:
_LOGGER.info(
f"Instance '{self.dimension_value}' is still running. "
f"Instance '{ec2_instance_id}' is still running. "
f"Launch time: {instance.launch_time}"
)
return (datetime.now(pytz.utc) - instance.launch_time).total_seconds()
Expand Down
1 change: 1 addition & 0 deletions cloudwatcher/preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class MetricWatcherSetup:
aws_secret_access_key: str = None
aws_session_token: str = None
aws_region_name: str = None
metric_description: str = None

def __post_init__(self):
self.aws_access_key_id = self.aws_access_key_id or os.environ.get(
Expand Down
5 changes: 3 additions & 2 deletions cloudwatcher/presets/nephele_cpu_usage_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"dimensions_list": [
{
"Name": "InstanceId",
"Value": "i-0c4d9523c99fbc1da"
"Value": ""
},
{
"Name": "cpu",
Expand All @@ -12,5 +12,6 @@
],
"metric_name": "cpu_usage_user",
"metric_id": "nephele",
"metric_unit": "Percent"
"metric_unit": "Percent",
"metric_description": "Precentage of total CPU used over time"
}
7 changes: 4 additions & 3 deletions cloudwatcher/presets/nephele_disk_used_percent.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"dimensions_list": [
{
"Name": "InstanceId",
"Value": "i-04127265744c54ce6"
"Value": ""
},
{
"Name": "path",
Expand All @@ -19,6 +19,7 @@
}
],
"metric_name": "disk_used_percent",
"metric_id": "nephele",
"metric_unit": "Percent"
"metric_id": "root",
"metric_unit": "Percent",
"metric_description": "Percentage of disk used over time. Path: <code>/</code> (root)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"dimensions_list": [
{
"Name": "InstanceId",
"Value": "i-04127265744c54ce6"
"Value": ""
},
{
"Name": "path",
Expand All @@ -19,6 +19,7 @@
}
],
"metric_name": "disk_used_percent",
"metric_id": "nephele",
"metric_unit": "Percent"
"metric_id": "nephele_data",
"metric_unit": "Percent",
"metric_description": "Percentage of disk used over time. Path: <code>/nephele_data</code>"
}
5 changes: 3 additions & 2 deletions cloudwatcher/presets/nephele_mem.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"dimensions_list": [
{
"Name": "InstanceId",
"Value": "i-0c4d9523c99fbc1da"
"Value": ""
}
],
"metric_name": "mem_used",
"metric_id": "nephele",
"metric_unit": "Bytes"
"metric_unit": "Bytes",
"metric_description": "Memory used over time"
}
5 changes: 3 additions & 2 deletions cloudwatcher/presets/nephele_mem_cached.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"dimensions_list": [
{
"Name": "InstanceId",
"Value": "i-0c4d9523c99fbc1da"
"Value": ""
}
],
"metric_name": "mem_cached",
"metric_id": "nephele",
"metric_unit": "Bytes"
"metric_unit": "Bytes",
"metric_description": "Memory cache used over time"
}
15 changes: 5 additions & 10 deletions docs/API_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ Alternatively, you can pass the values as arguments to the `MetricWatcher` const
```python
from cloudwatcher.metricwatcher import MetricWatcher
from cloudwatcher.preset import Dimension
from cloudwatcher._version import __version__
from dotenv import load_dotenv
import os

load_dotenv()

print(f"CloudWatcher version: {__version__}")

instance_id = "i-00beb88d31184ae62"
instance_id = os.environ.get("INSTANCE_ID")
mw = MetricWatcher(
namespace="NepheleNamespaceEC2",
metric_name="mem_used",
Expand All @@ -37,9 +35,6 @@ mw = MetricWatcher(
)
```

CloudWatcher version: 0.1.0


### `MetricWatcher` presets

As you can see there are multiple arguments that can be passed to the `MetricWatcher` constructor. In order to improve the UX when using `MetricWatcher`. The `cloudwatcher` package provides a few presets that can be used to query the data reported by `CloudWatchAgent` within certain systems. Additionally, custom presets can be defined by the user and used in the same way.
Expand Down Expand Up @@ -151,12 +146,12 @@ response["ResponseMetadata"]



{'RequestId': 'd64c4f8e-8544-48ee-894e-e5acf2a66464',
{'RequestId': 'bb3880e0-17e6-45d2-a007-a7b797c6d52f',
'HTTPStatusCode': 200,
'HTTPHeaders': {'x-amzn-requestid': 'd64c4f8e-8544-48ee-894e-e5acf2a66464',
'HTTPHeaders': {'x-amzn-requestid': 'bb3880e0-17e6-45d2-a007-a7b797c6d52f',
'content-type': 'text/xml',
'content-length': '33663',
'date': 'Tue, 08 Nov 2022 16:09:50 GMT'},
'date': 'Tue, 08 Nov 2022 16:46:07 GMT'},
'RetryAttempts': 0}


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cloudwatcher"
version = "0.1.0"
version = "0.1.1"
description = "A tool for monitoring AWS CloudWatch metrics"
authors = ["Michal Stolarczyk <stolarczyk.michal93@gmail.com>"]

Expand Down

0 comments on commit ba891e8

Please sign in to comment.