Skip to content

Commit 1fed6cb

Browse files
author
Caglar Anar
committed
Process metrics are added to report total CPU and Memory usage of a specific process.a
1 parent c1bba2e commit 1fed6cb

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

cloudwatchmon/cli/put_instance_stats.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import re
3030
import sys
3131
import time
32+
import subprocess
3233

3334
CLIENT_NAME = 'CloudWatch-PutInstanceData'
3435
FileCache.CLIENT_NAME = CLIENT_NAME
@@ -302,11 +303,19 @@ def config_parser():
302303
type=to_lower,
303304
choices=size_units,
304305
help='Specifies units for disk space metrics.')
305-
disk_group.add_argument('--disk-inode-util',
306+
disk_group.add_argument('--disk-inode-util',
306307
action='store_true',
307308
help='Reports disk inode utilization in percentages.')
308309

309310

311+
process_group = parser.add_argument_group('process metrics')
312+
process_group.add_argument('--process-name',
313+
metavar='PROCNAME',
314+
default='init',
315+
type=to_lower,
316+
help='Report CPU and Memory metrics of comma-sperated processes.')
317+
318+
310319
exclusive_group = parser.add_mutually_exclusive_group()
311320
exclusive_group.add_argument('--from-cron',
312321
action='store_true',
@@ -422,6 +431,20 @@ def add_disk_metrics(args, metrics):
422431
disk.inode_util, disk.mount, disk.file_system)
423432

424433

434+
def add_process_metrics(args, metrics):
435+
process_names = args.process_name.split(',')
436+
for process_name in process_names:
437+
processes = subprocess.Popen(["ps", "axco", "command,pcpu,pmem"], stdout=subprocess.PIPE)
438+
total_cpu = 0.0
439+
total_mem = 0.0
440+
for line in processes.stdout:
441+
if re.search(process_name, line):
442+
out = line.split()
443+
total_cpu += float(out[1])
444+
total_mem += float(out[2])
445+
metrics.add_metric(process_name+'-CpuUtilization', 'Percent', total_cpu)
446+
metrics.add_metric(process_name+'-MemoryUtilization', 'Percent', total_mem)
447+
425448

426449
def add_static_file_metrics(args, metrics):
427450
with open(args.from_file[0]) as f:
@@ -457,6 +480,7 @@ def validate_args(args):
457480
args.swap_util or args.swap_used
458481
report_disk_data = args.disk_path is not None
459482
report_loadavg_data = args.loadavg or args.loadavg_percpu
483+
report_process_data = args.process_name is not None
460484

461485
if report_disk_data:
462486
if not args.disk_space_util and not args.disk_space_used and \
@@ -478,7 +502,7 @@ def validate_args(args):
478502
raise ValueError('No metrics specified for collection and '
479503
'submission to CloudWatch.')
480504

481-
return report_disk_data, report_mem_data, report_loadavg_data
505+
return report_disk_data, report_mem_data, report_loadavg_data, report_process_data
482506

483507

484508
def main():
@@ -496,7 +520,7 @@ def main():
496520
return 0
497521

498522
try:
499-
report_disk_data, report_mem_data, report_loadavg_data = validate_args(args)
523+
report_disk_data, report_mem_data, report_loadavg_data, report_process_data = validate_args(args)
500524

501525
# avoid a storm of calls at the beginning of a minute
502526
if args.from_cron:
@@ -541,6 +565,9 @@ def main():
541565
if report_disk_data:
542566
add_disk_metrics(args, metrics)
543567

568+
if report_process_data:
569+
add_process_metrics(args, metrics)
570+
544571
if args.verbose:
545572
print 'Request:\n' + str(metrics)
546573

0 commit comments

Comments
 (0)