11"""
2- This plugin allows users to query metrics from the monitoring service for various services .
2+ This plugin provides access to the Linode Monitor API .
33
4+ Commands:
5+ get-metrics: Query metrics from the monitoring service for various services.
6+
7+ Usage:
8+ linode-cli monitor-api get-metrics <service> [options]
49"""
510
611import json
1621from linodecli .help_formatter import SortingHelpFormatter
1722from linodecli .helpers import register_debug_arg
1823
19- PLUGIN_BASE = "linode-cli get_metrics "
24+ PLUGIN_BASE = "linode-cli monitor-api "
2025
2126# API Configuration
22- API_BASE_URL = "https://monitor-api.linode.com/v2/monitor/services"
27+ API_BASE_URL = "https://monitor-api.linode.com"
28+ API_VERSION = "v2"
2329
2430
2531def get_auth_token ():
@@ -80,7 +86,7 @@ def make_api_request(
8086 Returns:
8187 Tuple of (status_code, response_data)
8288 """
83- url = f"{ API_BASE_URL } /{ service_name } /{ endpoint } "
89+ url = f"{ API_BASE_URL } /{ API_VERSION } /monitor/services/ { service_name } /{ endpoint } "
8490
8591 headers = {
8692 "Authorization" : f"Bearer { token or get_auth_token ()} " ,
@@ -263,7 +269,7 @@ def print_help(parser: ArgumentParser):
263269 print ("\n Examples:" )
264270 print (" # Get metrics with relative time duration" )
265271 print (
266- " linode-cli get_metrics dbaas --entity-ids 123 --duration 15 "
272+ " linode-cli monitor-api get-metrics dbaas --entity-ids 123 --duration 15 "
267273 "--duration-unit min --metrics cpu_usage:avg"
268274 )
269275
@@ -272,42 +278,42 @@ def print_help(parser: ArgumentParser):
272278 "(only allowed for objectstorage service)"
273279 )
274280 print (
275- " linode-cli get_metrics objectstorage --duration 15 "
281+ " linode-cli monitor-api get-metrics objectstorage --duration 15 "
276282 "--duration-unit min --metrics obj_requests_num:avg "
277283 "--entity-region us-east-1"
278284 )
279285
280286 print ("\n # Get metrics with absolute time duration" )
281287 print (
282- " linode-cli get_metrics dbaas --entity-ids 123 "
288+ " linode-cli monitor-api get-metrics dbaas --entity-ids 123 "
283289 "--start-time 2024-10-10T00:00:01Z --end-time 2024-10-10T23:59:59Z "
284290 "--metrics cpu_usage:avg,memory_usage:sum"
285291 )
286292
287293 print ("\n # Get metrics with filters" )
288294 print (
289- " linode-cli get_metrics dbaas --entity-ids 123 --duration 15 "
295+ " linode-cli monitor-api get-metrics dbaas --entity-ids 123 --duration 15 "
290296 "--duration-unit min --metrics cpu_usage:avg "
291297 "--filters 'node_type:in:primary,secondary'"
292298 )
293299
294300 print ("\n # Get metrics with multiple filters" )
295301 print (
296- " linode-cli get_metrics dbaas --entity-ids 123 --duration 15 "
302+ " linode-cli monitor-api get-metrics dbaas --entity-ids 123 --duration 15 "
297303 "--duration-unit min --metrics cpu_usage:avg "
298304 "--filters 'node_type:in:primary,secondary;status:eq:active'"
299305 )
300306
301307 print ("\n # Get metrics with granularity" )
302308 print (
303- " linode-cli get_metrics netloadbalancer --entity-ids 123 "
309+ " linode-cli monitor-api get-metrics netloadbalancer --entity-ids 123 "
304310 "--duration 1 --duration-unit hour --metrics nlb_ingress_traffic:sum "
305311 "--granularity 10 --granularity-unit min"
306312 )
307313
308314 print ("\n # Get metrics with entity region (required ObjectStorage)" )
309315 print (
310- " linode-cli get_metrics objectstorage --entity-region us-east-1 "
316+ " linode-cli monitor-api get-metrics objectstorage --entity-region us-east-1 "
311317 "--duration 15 --duration-unit min --metrics obj_requests_num:sum"
312318 )
313319
@@ -316,7 +322,7 @@ def print_help(parser: ArgumentParser):
316322 "(mandatory for cloud firewall service)"
317323 )
318324 print (
319- " linode-cli get_metrics firewall --entity-region us-east-1 "
325+ " linode-cli monitor-api get-metrics firewall --entity-region us-east-1 "
320326 "--associated-entity-region us-west-1 --duration 15 "
321327 "--duration-unit min --metrics fw_active_connections:sum"
322328 )
@@ -332,12 +338,19 @@ def get_metrics_parser():
332338
333339 register_debug_arg (parser )
334340
335- # Service name as positional argument
341+ # Command as first positional argument
342+ parser .add_argument (
343+ "command" ,
344+ nargs = "?" ,
345+ help = "Command to execute (get-metrics)" ,
346+ )
347+
348+ # Service name as second positional argument
336349 parser .add_argument (
337350 "service" ,
338351 nargs = "?" ,
339352 help = "Service name (Dbaas, Nodebalancer, NetLoadBalancer, Linode, "
340- "Firewall, ObjectStorage, Blockstorage,LKE)" ,
353+ "Firewall, ObjectStorage, Blockstorage, LKE)" ,
341354 )
342355
343356 # Optional arguments for get-metrics functionality
@@ -457,10 +470,23 @@ def call(args, context=None): # pylint: disable=unused-argument
457470 parsed , remaining_args = parser .parse_known_args (args )
458471
459472 # Handle help cases
460- if not parsed .service or parsed .service == "help" or "--help" in args :
473+ if not parsed .command or parsed .command == "help" or "--help" in args :
461474 print_help (parser )
462475 sys .exit (ExitCodes .SUCCESS )
463476
477+ # Validate command
478+ if parsed .command != "get-metrics" :
479+ print (f"Unknown command: { parsed .command } " , file = sys .stderr )
480+ print ("Available commands: get-metrics" , file = sys .stderr )
481+ print_help (parser )
482+ sys .exit (ExitCodes .REQUEST_FAILED )
483+
484+ # Validate service is provided
485+ if not parsed .service :
486+ print ("Service name is required" , file = sys .stderr )
487+ print_help (parser )
488+ sys .exit (ExitCodes .REQUEST_FAILED )
489+
464490 if remaining_args :
465491 print (f"Unknown arguments: { ' ' .join (remaining_args )} " , file = sys .stderr )
466492 print_help (parser )
0 commit comments