프로세스 모니터링을 위한 Prometheus 학습 노트: process_exporter

1. 프로세스_엑스포터 설치 및 설정

다음 링크에서 최신 버전 다운로드:

https://github.com/ncabatoff/process-exporter/releases/download/v0.8.3/process-exporter-0.8.3.linux-amd64.tar.gz
tar xf process-exporter-0.8.3.linux-amd64.tar.gz 
mv process-exporter-0.8.3.linux-amd64 /usr/local/process-exporter
cd /usr/local/process-exporter
vim config.yaml # 모든 프로세스 모니터링 설정 예시
process_names:
  - name: "{{.Matches}}" 
    cmdline:
    - 'redis-server'

  - name: "{{.Comm}}" 
    cmdline:
    - '.+'

vim /usr/lib/systemd/system/process_exporter.service 
[Unit]
Description=Go 기반 프로세스 메트릭 수집기
Documentation=https://github.com/ncabatoff/process-exporter
After=network.target

[Service]
Type=simple
WorkingDirectory=/usr/local/process-exporter/
ExecStart=/usr/local/process-exporter/process-exporter -config.path=/usr/local/process-exporter/config.yaml
Restart=on-failure

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl start process_exporter
systemctl enable process_exporter
netstat -tnlp|grep 9256

브라우저에서 http://localhost:9256/metrics 확인

2. 구성 요소 분석

2.1 YAML 구조 및 우선순위

최상위 레벨에 process_names 섹션이 존재하며, 동일한 프로세스가 여러 항목과 매칭될 경우 첫 번째 항목이 선택됩니다:

process_names:
  - matcher1  
  - matcher2
  ...
  - matcherN

2.2 템플릿 변수 시스템

각 항목은 프로세스 식별을 위한 템플릿 제공

변수명설명
{{.Comm}}/proc//stat의 두 번째 필드 추출(15자 제한)
{{.ExeBase}}실행 파일 기본 이름
{{.ExeFull}}완전 경로 포함
{{.Matches}}cmdline 정규표현식 매칭 결과
{{.PID}}프로세스 ID
{{.StartTime}}시작 시간(UTC)
{{.Cgroups}}cgroups 정보

2.3 프로세스 선택기

각 항목은 comm, exe, cmdline 중 하나 이상의 조건을 충족해야 합니다:

process_names:
  - comm:
    - bash

  - exe:
    - postgres
    - /usr/local/bin/prometheus

  - name: "{{.ExeFull}}:{{.Matches.Cfgfile}}"
    exe:
    - /usr/local/bin/process-exporter
    cmdline:
    - -config.path\s+(?P<Cfgfile>\S+)

2.4 메트릭 그룹

모니터링 대상 메트릭 목록:

  • namedprocess_namegroup_num_procs: 프로세스 수
  • namedprocess_namegroup_cpu_seconds_total: CPU 사용량
  • namedprocess_namegroup_read_bytes_total: 읽은 바이트 수
  • namedprocess_namegroup_memory_bytes: 메모리 사용량
  • namedprocess_namegroup_open_filedesc: 파일 디스크립터 수
  • namedprocess_namegroup_states: 상태별 프로세스 수

2.5 스레드 메트릭

스레드 단위 메트릭:

  • namedprocess_namegroup_thread_cpu_seconds_total
  • namedprocess_namegroup_thread_io_bytes_total
  • namedprocess_namegroup_thread_context_switches_total

3. Prometheus 설정

vim prometheus.yml 
  - job_name: 'process-monitor'
    static_configs:
    - targets: ['192.168.100.131:9256']

curl -X POST http://127.0.0.1:9090/-/reload 

4. Grafana 템플릿

https://grafana.net/dashboards/249

5. 알림 규칙

alert: 과도한 프로세스
expr: sum(namedprocess_namegroup_states) by (cluster,job,instance) > 500
for: 20s
labels:
  severity: warning
annotations:
  value: {{ $value }}개의 프로세스 생성됨(경고 기준 초과)

alert:僵尸 프로세스
expr: sum by(cluster, job, instance, groupname) (namedprocess_namegroup_states{state="Zombie"}) > 0
for: 1m
labels:
  severity: warning
annotations:
  value: {{ $value }}개의 죽은 프로세스 발생

alert: 재시작 감지
expr: ceil(time() - max by(cluster, job, instance, groupname) (namedprocess_namegroup_oldest_start_time_seconds)) < 60
for: 25s
labels:
  label: alert_once
  severity: warning
annotations:
  value: {{ $labels.groupname }} 프로세스 {{ $value }}초 전 재시작

태그: process_exporter prometheus system_monitoring metrics_collection linux_administration

5월 26일 19:56에 게시됨