-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproc-collect.py
73 lines (60 loc) · 2.17 KB
/
proc-collect.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import psutil
import time
from prometheus_client import start_http_server, Gauge
# Define Prometheus metrics for process information
PROCESS_CPU_USAGE = Gauge(
'process_cpu_usage',
'CPU usage percentage for a specific process',
['pid', 'process_name']
)
PROCESS_MEMORY_USAGE = Gauge(
'process_memory_usage_bytes',
'Memory usage in bytes for a specific process',
['pid', 'process_name']
)
PROCESS_OPEN_FILES = Gauge(
'process_open_files',
'Number of open files for a specific process',
['pid', 'process_name']
)
PROCESS_THREADS = Gauge(
'process_threads',
'Number of threads for a specific process',
['pid', 'process_name']
)
def collect_process_metrics():
"""
Collect metrics for all running processes
"""
for proc in psutil.process_iter(['pid', 'name']):
try:
# Get process details
pid = proc.info['pid']
process_name = proc.info['name']
# Collect CPU usage
cpu_usage = proc.cpu_percent(interval=0.1)
PROCESS_CPU_USAGE.labels(pid=pid, process_name=process_name).set(cpu_usage)
# Collect Memory Usage
memory_info = proc.memory_info()
PROCESS_MEMORY_USAGE.labels(pid=pid, process_name=process_name).set(memory_info.rss)
# Collect Open Files
try:
open_files_count = len(proc.open_files())
PROCESS_OPEN_FILES.labels(pid=pid, process_name=process_name).set(open_files_count)
except (psutil.AccessDenied, psutil.NoSuchProcess):
pass
# Collect Thread Count
thread_count = proc.num_threads()
PROCESS_THREADS.labels(pid=pid, process_name=process_name).set(thread_count)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
# Handle cases where process might have terminated
pass
def main():
# Start up the server to expose the metrics
start_http_server(8000)
# Collect metrics periodically
while True:
collect_process_metrics()
time.sleep(15) # Collect metrics every 15 seconds
if __name__ == '__main__':
main()