-
Notifications
You must be signed in to change notification settings - Fork 6
/
process_monitor.py
executable file
·53 lines (40 loc) · 1.59 KB
/
process_monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
#!/usr/bin/env python
# Copyright (c) 2015-2016 University Corporation for Atmospheric Research/Unidata
# Distributed under the terms of the MIT License.
# SPDX-License-Identifier: MIT
from datetime import datetime
import boto3
import psutil
def find_proc(proc_name):
for proc in psutil.process_iter():
try:
cmd = proc.cmdline()
if len(cmd) >= 2 and cmd[0] == 'python' and proc_name in cmd[1]:
return proc
except psutil.AccessDenied:
pass
def make_data(name, value):
return {'MetricName': name, 'Timestamp': datetime.utcnow(), 'Value': value,
'Unit': 'Percent'}
def dump_stats(client, proc):
client.put_metric_data(Namespace='ProcessMetrics',
MetricData=[make_data('CPUUsage', proc.cpu_percent(1.0)),
make_data('MemoryUsage', proc.memory_percent())])
if __name__ == '__main__':
import argparse
import sys
from urllib.request import urlopen
# Set up argument parsing
parser = argparse.ArgumentParser(description='Monitor Python script stats.')
parser.add_argument('name', help='Name of script to monitor', type=str)
args = parser.parse_args()
proc_name = sys.argv[1]
endpoint = 'http://169.254.169.254/latest/meta-data/placement/availability-zone'
region_name = urlopen(endpoint).read().decode('utf-8')[:-1]
client = boto3.client('cloudwatch', region_name=region_name)
try:
proc = find_proc(args.name)
if proc:
dump_stats(client, proc)
except psutil.NoSuchProcess:
pass