# Prometheus 监控进程

#### Process-exporter

　　process-exporter可以用来检测所选进程的存活状态

**下载process-exporter**

　　下载地址：<https://github.com/ncabatoff/process-exporter/releases/tag/v0.4.0>　　

**安装部署process-exporter**

```bash
tar -zxvf process-exporter-0.4.0.linux-amd64.tar.gz -C /usr/local/process-exporter
```

　　编写配置文件

### 配置文件根据变量名匹配到配置文件

* {{.Comm}} 包含原始可执行文件的基本名称，即 /proc//stat
* {{.ExeBase}} 包含可执行文件的基本名称
* {{.ExeFull}} 包含可执行文件的标准路径
* {{.Username}} 包含有效用户的用户名

```yaml
cat process-exporter.yaml
process_names:
  
  - name: "{{.Matches}}"
    cmdline:
    - 'dbbakup'
  
  - name: "{{.Matches}}"
    cmdline:
    - 'mysql'
  
  - name: "{{.Matches}}"
    cmdline:
    - 'pushgateway'
```

注 cmdline:  所选进程的唯一标识，ps -ef 可以查询到。如果改进程不存在，则不会有该进程的数据采集到。

\
　　编写启动脚本

```bash
cat /usr/lib/systemd/system/process_exporter.service
[Unit]
Description=Prometheus exporter for processors metrics, written in Go with pluggable metric collectors.
Documentation=https://github.com/ncabatoff/process-exporter
After=network.target
  
[Service]
Type=simple
User=root
WorkingDirectory=/usr/local/process-exporter
ExecStart=/usr/local/process-exporter/process-exporter -config.path=/usr/local/process-exporter/process-exporter.yaml
Restart=on-failure
  
[Install]
WantedBy=multi-user.target
```

**启动process-exporter**

```bash
systemctl daemon-reload
systemctl start process_exporter
systemctl enable process_exporter
```

**验证**

```bash
curl localhost:9256/metrics
```

**修改Prometheus配置文件**

```bash
增加
 
 - job_name: 'process'
    static_configs:
    - targets: ['172.16.8.187:9256']
```

　　重启Prometheus

<br>

<figure><img src="/files/E4V1uIH7nLShKfgfZyDg" alt=""><figcaption></figcaption></figure>

添加告警规则

```yaml
cat process.yml 
groups:
- name: process
  rules:
  - alert: backup-mysql-Down
    expr: absent(namedprocess_namegroup_states{groupname="map[:mysql]"})
    for: 1m
    labels:
      severity: critical
    annotations:
      summary: backup slave mysql docker process Down (instance {{ $labels.instance }})
      description: "backup slave mysql process is down\n LABELS = {{ $labels }}"
 
 
  - alert: pushgateway-Down
    expr: absent(namedprocess_namegroup_states{groupname="map[:pushgateway]"})
    for: 1m
    labels:
      severity: critical
    annotations:
      summary: pushgateway process Down (instance {{ $labels.instance }})
      description: "pushgateway process is down\n LABELS = {{ $labels }}"
 
  - alert: dbback-process-Down
    expr: absent(namedprocess_namegroup_states{groupname="map[:dbbakup]"})
    for: 1m
    labels:
      severity: critical
    annotations:
      summary: dbbackup docker process Down (instance {{ $labels.instance }})
      description: "dbbackup docker process is down\n LABELS = {{ $labels }}"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/prometheus/prometheus-jian-kong-jin-cheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
