API documentation
CloudWatcher
A base class for CloudWatch managers
Source code in cloudwatcher/cloudwatcher.py
__init__(service_name, aws_region_name=None, aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None)
Initialize CloudWatcher
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service_name |
str
|
The name of the service to use |
required |
aws_region_name |
Optional[str]
|
The AWS region name. |
None
|
aws_access_key_id |
Optional[str]
|
The AWS access key ID. |
None
|
aws_secret_access_key |
Optional[str]
|
The AWS secret access key. |
None
|
aws_session_token |
Optional[str]
|
The AWS session token. |
None
|
Source code in cloudwatcher/cloudwatcher.py
LogWatcher
Bases: CloudWatcher
A class for AWS CloudWatch log events retrieval and parsing
Source code in cloudwatcher/logwatcher.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
|
__init__(log_group_name, log_stream_name, start_token=None, aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None, aws_region_name=None)
Initialize LogWatcher
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_group_name |
str
|
The name of the log group |
required |
log_stream_name |
str
|
The name of the log stream |
required |
start_token |
Optional[str]
|
The token to use for the next query |
None
|
aws_access_key_id |
Optional[str]
|
The AWS access key ID |
None
|
aws_secret_access_key |
Optional[str]
|
The AWS secret access key |
None
|
aws_session_token |
Optional[str]
|
The AWS session token |
None
|
aws_region_name |
Optional[str]
|
The AWS region name |
None
|
Source code in cloudwatcher/logwatcher.py
__repr__()
Return a string representation of the object
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The string representation of the object |
check_log_exists()
Check if the log stream exists
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the log stream exists, False otherwise |
Source code in cloudwatcher/logwatcher.py
return_formatted_logs(events_limit=1000, max_retry_attempts=5)
A generator that yields formatted log events
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events_limit |
int
|
The number of events to retrieve per iteration. |
1000
|
max_retry_attempts |
int
|
The number of retry attempts. |
5
|
Returns:
Type | Description |
---|---|
Tuple[str, Optional[str]]
|
Tuple[str, str]: The list of formatted log events and the next token |
Source code in cloudwatcher/logwatcher.py
save_log_file(file_path)
Save the log file to the specified path
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
The path to save the log file to. |
required |
Source code in cloudwatcher/logwatcher.py
stream_cloudwatch_logs(events_limit=1000, max_retry_attempts=5)
A generator that retrieves desired number of log events per iteration
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events_limit |
int
|
The number of events to retrieve per iteration. |
1000
|
max_retry_attempts |
int
|
The number of retry attempts. |
5
|
Returns:
Type | Description |
---|---|
Generator[LogEventsList, None, None]
|
List[Event]: The list of log events |
Source code in cloudwatcher/logwatcher.py
stream_formatted_logs(events_limit=1000, max_retry_attempts=5, sep='<br>')
A generator that yields formatted log events
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events_limit |
int
|
The number of events to retrieve per iteration. |
1000
|
max_retry_attempts |
int
|
The number of retry attempts. |
5
|
sep |
str
|
The separator to use between log events. |
'<br>'
|
Returns:
Type | Description |
---|---|
Generator[Tuple[str, Optional[str]], None, None]
|
Tuple[List[str], str]: The list of formatted log events and the next token |
Source code in cloudwatcher/logwatcher.py
MetricWatcher
Bases: CloudWatcher
A class for AWS CloudWatch metric retrieval and parsing
Source code in cloudwatcher/metricwatcher.py
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
__init__(namespace, dimensions_list, metric_name, metric_id, metric_unit=None, metric_description=None, aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None, aws_region_name=None)
Initialize MetricWatcher
Parameters:
Name | Type | Description | Default |
---|---|---|---|
namespace |
str
|
the namespace of the metric |
required |
dimensions_list |
List[Dimension]
|
the dimensions of the metric |
required |
metric_name |
str
|
the name of the metric |
required |
metric_id |
str
|
the ID of the metric |
required |
metric_unit |
Optional[str]
|
the unit of the metric |
None
|
aws_access_key_id |
Optional[str]
|
the AWS access key ID |
None
|
aws_secret_access_key |
Optional[str]
|
the AWS secret access key |
None
|
aws_session_token |
Optional[str]
|
the AWS session token |
None
|
aws_region_name |
Optional[str]
|
the AWS region name |
None
|
Source code in cloudwatcher/metricwatcher.py
get_ec2_uptime(ec2_instance_id, days, hours, minutes, period=60)
Get the runtime of an EC2 instance
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ec2_instance_id |
str
|
the ID of the EC2 instance |
required |
days |
int
|
how many days to subtract from the current date to determine the metric collection start time |
required |
hours |
int
|
how many hours to subtract from the current time to determine the metric collection start time |
required |
minutes |
int
|
how many minutes to subtract from the current time to determine the metric collection start time |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
Optional[float]
|
the runtime of the EC2 instance in minutes |
Source code in cloudwatcher/metricwatcher.py
is_ec2_running(ec2_instance_id)
Check if EC2 instance is running
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ec2_instance_id |
str
|
the ID of the EC2 instance |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if EC2 instance is running, False otherwise. |
Source code in cloudwatcher/metricwatcher.py
log_metric(response=None)
Query and log the metric data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Optional[Dict]
|
the response from the query |
None
|
Source code in cloudwatcher/metricwatcher.py
log_metric_summary(response=None)
Query and summarize the metric data to a JSON file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Optional[Dict]
|
the response from the query |
None
|
Source code in cloudwatcher/metricwatcher.py
log_response(response=None)
Query and log the response
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Optional[Dict]
|
the response from the query |
None
|
Source code in cloudwatcher/metricwatcher.py
query_ec2_metrics(days, hours, minutes, stat, period)
Query EC2 metrics
Parameters:
Name | Type | Description | Default |
---|---|---|---|
days |
int
|
how many days to subtract from the current date to determine the metric collection start time |
required |
hours |
int
|
how many hours to subtract from the current time to determine the metric collection start time |
required |
minutes |
int
|
how many minutes to subtract from the current time to determine the metric collection start time |
required |
stat |
str
|
the statistic to query |
required |
period |
int
|
the period of the metric |
required |
Returns:
Name | Type | Description |
---|---|---|
Dict |
Optional[Dict]
|
the response from the query, check the structure of the |
Optional[Dict]
|
response here # noqa: E501 |
Source code in cloudwatcher/metricwatcher.py
save_metric_csv(file_path, response=None, query_kwargs=None)
Query and save the metric data to a CSV file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
the file path to save the metric data to |
required |
response |
Optional[Dict]
|
the response from the query |
None
|
query_kwargs |
Optional[str]
|
the query preset to use for the query |
None
|
Source code in cloudwatcher/metricwatcher.py
save_metric_json(file_path, response=None, query_kwargs=None)
Query and save the metric data to a JSON file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
the file path to save the metric data to |
required |
response |
Optional[Dict]
|
the response from the query |
None
|
query_kwargs |
Optional[str]
|
the query preset to use for the query |
None
|
Source code in cloudwatcher/metricwatcher.py
save_metric_plot(file_path, response=None, query_kwargs=None)
Query and plot the metric data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
the file path to save the metric data to |
required |
response |
Optional[Dict]
|
the response from the query |
None
|
query_kwargs |
Optional[str]
|
the query preset to use for the query |
None
|
Source code in cloudwatcher/metricwatcher.py
save_response_json(file_path, response=None, query_kwargs=None)
Query and save the response data to a JSON file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
the file path to save the response data to |
required |
response |
Optional[Dict]
|
the response from the query |
None
|
query_kwargs |
Optional[str]
|
the query preset to use for the query |
None
|
Source code in cloudwatcher/metricwatcher.py
timed_metric_factory(response)
staticmethod
Create a collection of TimedMetrics from the CloudWatch client response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
dict
|
the response from the query |
required |
Returns:
Type | Description |
---|---|
List[TimedMetric]
|
List[TimedMetric]: a collection of TimedMetrics |
Source code in cloudwatcher/metricwatcher.py
MetricWatcherSetup
A class for the setup of the MetricWatcher
Source code in cloudwatcher/preset.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
from_dict(data)
classmethod
Create a MetricWatcherSetup object from a dictionary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
The dictionary to use |
required |
from_json(file_path)
classmethod
Create a MetricWatcherSetup object from a JSON file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
The path to the JSON file |
required |
Source code in cloudwatcher/preset.py
to_dict()
Convert the MetricWatcherSetup object to a dictionary
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The dictionary representation of the object |
upsert_dimensions(dimensions_specs=None)
Upsert the dimensions list with the dimensions specified in the environment
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dimensions_specs |
List[str]
|
A list of strings. Format: "Name:Value" |
None
|
Source code in cloudwatcher/preset.py
PresetFilesInventory
Source code in cloudwatcher/preset.py
presets: Dict[str, Path]
property
Get the available presets
Returns:
Type | Description |
---|---|
Dict[str, Path]
|
Dict[str, Path]: The available presets |
presets_dir: Path
property
Get the presets directory
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The presets directory |
presets_list: List[str]
property
Get the list of available presets
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: The list of available presets |
presets_table: Table
property
Get a rich table with the available presets
Returns:
Name | Type | Description |
---|---|---|
Table |
Table
|
The rich table |
__init__(presets_dir=None)
Initialize the preset inventory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
presets_dir |
Path
|
The path to the presets directory |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the presets directory does not exist |
Source code in cloudwatcher/preset.py
get_preset_path(preset_name)
Get the preset file content
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preset_name |
str
|
The name of the preset |
required |
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
the path to the preset file |
Source code in cloudwatcher/preset.py
Metric handlers
ResponseHandler
Abstract class to establish the interface for a response handling
Source code in cloudwatcher/metric_handlers.py
__init__(response)
Initialize the handler
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
dict
|
The response from the AWS API |
required |
ResponseLogger
Bases: ResponseHandler
Log the response to the console
Source code in cloudwatcher/metric_handlers.py
ResponseSaver
Bases: ResponseHandler
Save the response to a file
Source code in cloudwatcher/metric_handlers.py
__call__(target)
Save the response to a file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
str
|
The target file to save the response to |
required |
Source code in cloudwatcher/metric_handlers.py
TimedMetric
dataclass
Timed metric object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamps |
List[datetime]
|
The timestamps of the metric |
required |
values |
List[float]
|
The values of the metric |
required |
label |
str
|
The label of the metric |
required |
Source code in cloudwatcher/metric_handlers.py
TimedMetricCsvSaver
Bases: TimedMetricHandler
Source code in cloudwatcher/metric_handlers.py
__call__(target)
Write the object to a csv file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
str
|
The target file to save the object to |
required |
Source code in cloudwatcher/metric_handlers.py
TimedMetricHandler
Class to establish the interface for a timed metric handling
Source code in cloudwatcher/metric_handlers.py
__init__(timed_metric)
Initialize the handler
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timed_metric |
TimedMetric
|
The timed metric to use |
required |
TimedMetricJsonSaver
Bases: TimedMetricHandler
Source code in cloudwatcher/metric_handlers.py
__call__(target)
Write the object to a json file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
str
|
The target file to save the object to |
required |
Source code in cloudwatcher/metric_handlers.py
TimedMetricLogger
Bases: TimedMetricHandler
Source code in cloudwatcher/metric_handlers.py
__call__(target)
Log the timed metric as a table
Source code in cloudwatcher/metric_handlers.py
mem_to_str(size, precision=3)
staticmethod
Convert bytes to human readable string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
The size in bytes |
required |
precision |
int
|
The precision to use, number of decimal places |
3
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The human readable string |
Source code in cloudwatcher/metric_handlers.py
TimedMetricPlotter
Bases: TimedMetricHandler
Source code in cloudwatcher/metric_handlers.py
__call__(target, metric_unit)
Plot the timed metric
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
str
|
The target file to save the plot to |
required |
metric_unit |
str
|
The unit of the metric |
required |
Source code in cloudwatcher/metric_handlers.py
TimedMetricSummarizer
Bases: TimedMetricHandler
Source code in cloudwatcher/metric_handlers.py
__call__(target, metric_unit, summarizer)
Summarize the metric
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
str
|
The target file to save the summary to |
required |
metric_unit |
str
|
The unit of the metric |
required |
summarizer |
Tuple[str, callable]
|
The summarizer to use and the function to use |
required |
Source code in cloudwatcher/metric_handlers.py
convert_mem(value, force_suffix=None)
Convert memory in bytes to the highest possible, or desired memory unit
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
int
|
The memory in bytes |
required |
force_suffix |
str
|
The desired memory unit |
None
|
Returns:
Type | Description |
---|---|
Tuple[float, str]
|
Tuple[float, str]: The memory in the desired unit and the unit |
Source code in cloudwatcher/metric_handlers.py
LogEvent
Bases: BaseModel
A class for AWS CloudWatch log events
Attributes:
Name | Type | Description |
---|---|---|
message |
str
|
The log message |
timestamp |
datetime
|
The log timestamp |
Source code in cloudwatcher/logwatcher.py
__bool__()
Return True if the message is not empty
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the message is not empty |
format_message(regex=None, fmt_str_log=None, fmt_str_datetime=None)
Format the message by removing the embedded timestamp and adding a UTC timestamp
Parameters:
Name | Type | Description | Default |
---|---|---|---|
regex |
str
|
regex to match the timestamp in the message |
None
|
fmt_str_log |
str
|
format string for the log message |
None
|
fmt_str_datetime |
str
|
format string for the datetime |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
LogEvent
|
formatted message |
Source code in cloudwatcher/logwatcher.py
from_response(response)
classmethod
Create a LogEvent object from a response
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Dict[str, Any]
|
The response from AWS |
required |
Returns:
Name | Type | Description |
---|---|---|
LogEvent |
LogEvent
|
The LogEvent object |
Source code in cloudwatcher/logwatcher.py
LogEventsList
Bases: BaseModel
A class for AWS CloudWatch log events list
Attributes:
Name | Type | Description |
---|---|---|
events |
List[LogEvent]
|
The list of log events |
next_forward_token |
Optional[str]
|
The next forward token |
next_backward_token |
Optional[str]
|
The next backward token |
Source code in cloudwatcher/logwatcher.py
__bool__()
Return True if the events list is not empty
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the events list is not empty |
format_messages(regex=None, fmt_str_datetime=None, fmt_str_log=None)
Format the messages by removing the embedded timestamp and adding a UTC timestamp
Parameters:
Name | Type | Description | Default |
---|---|---|---|
regex |
str
|
regex to match the timestamp in the message |
None
|
fmt_str_log |
str
|
format string for the log message |
None
|
fmt_str_datetime |
str
|
format string for the datetime |
None
|
Returns:
Name | Type | Description |
---|---|---|
LogEventsList |
LogEventsList
|
The LogEventsList object, with formatted messages |
Source code in cloudwatcher/logwatcher.py
from_response(response)
classmethod
Create a LogEventsList object from a response
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Dict[str, Any]
|
The response from AWS |
required |
Returns:
Name | Type | Description |
---|---|---|
LogEventsList |
LogEventsList
|
The LogEventsList object |