trying to resolve process name
build / build (push) Successful in 1m14s

This commit is contained in:
tchivert committed 2026-04-28 18:53:35 +02:00
1 parent dccf2aadf9
commit aeab295af1
3 files changed
+92 -11

No files matched your search

+25
View File
@@ -70,6 +70,8 @@ processes:
- Can be specified by name only: `- nginx`
- Can be specified by name and PID: `- name: haproxy
pid: 516525`
- Can resolve the full process name from cmdline: `- name: php-fpm
resolveName: true` (useful for distinguishing pools, e.g. `php-fpm: pool www` vs `php-fpm: pool staging`)
- `interval`: Metrics collection interval (optional, e.g., `5s`, `1m`, `30s`). If not specified, defaults to 10 seconds. Set to `0s` for on-demand collection only.
- Comments can be added using `#` prefix
- YAML supports multi-line arrays for better readability
@@ -119,6 +121,29 @@ This configuration will:
- Monitor the specific nginx process with PID 12345
- Monitor all postgres processes by name
### Example with Process Name Resolution
When monitoring processes that have multiple instances with different configurations
(such as php-fpm pools), you can enable `resolveName: true` to use the full command
line as the process label instead of just the base executable name.
```yaml
processes:
- name: php-fpm
resolveName: true
```
This will produce labels like:
- `process="php-fpm: pool www", pid="1234"`
- `process="php-fpm: pool staging", pid="1235"`
Instead of:
- `process="php-fpm", pid="1234"`
- `process="php-fpm", pid="1235"`
This is especially useful when process instances are short-lived and you need
to identify which pool they belong to from the metrics.
### Example with Mixed Configuration
```yaml
+7 -1
View File
@@ -7,4 +7,10 @@ processes:
- haproxy
- postgres
# - mysql
# - redis
# - redis
# Example with process name resolution (uncomment to use):
# processes:
# - name: php-fpm
# resolveName: true
# - name: nginx
+60 -10
View File
@@ -10,6 +10,7 @@ import (
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
@@ -22,8 +23,9 @@ import (
)
type ProcessConfig struct {
Name string `yaml:"name"`
Pid int `yaml:"pid"`
Name string `yaml:"name"`
Pid int `yaml:"pid"`
ResolveName bool `yaml:"resolveName"`
}
type Config struct {
@@ -180,7 +182,7 @@ func (e *Exporter) collectAndCacheMetrics() {
foundCounts := make(map[string]int)
for _, proc := range procs {
name, err := proc.Name()
baseName, err := proc.Name()
if err != nil {
continue
}
@@ -188,11 +190,25 @@ func (e *Exporter) collectAndCacheMetrics() {
// Check if this process matches any of our configured processes
for _, procConfig := range e.config.Processes {
// Match by name
if procConfig.Name != "" && procConfig.Name == name {
if procConfig.Name != "" && procConfig.Name == baseName {
foundCounts[procConfig.Name]++
e.collectProcessMetrics(proc, procConfig.Name)
// Use resolved name if enabled
labelName := procConfig.Name
if procConfig.ResolveName {
resolved := resolveProcessName(proc)
if resolved != "" {
labelName = resolved
}
}
e.collectProcessMetrics(proc, labelName)
if e.verbose {
log.Printf("Collected metrics for %s (PID: %d)", procConfig.Name, proc.Pid)
if procConfig.ResolveName {
log.Printf("Collected metrics for %s (PID: %d, resolved: %s)", procConfig.Name, proc.Pid, labelName)
} else {
log.Printf("Collected metrics for %s (PID: %d)", procConfig.Name, proc.Pid)
}
}
continue
}
@@ -200,10 +216,11 @@ func (e *Exporter) collectAndCacheMetrics() {
// Match by PID
if procConfig.Pid != 0 && int(proc.Pid) == procConfig.Pid {
// For PID-based monitoring, use the process name as the label
foundCounts[fmt.Sprintf("%s:%d", name, procConfig.Pid)]++
e.collectProcessMetrics(proc, fmt.Sprintf("%s:%d", name, procConfig.Pid))
labelName := fmt.Sprintf("%s:%d", baseName, procConfig.Pid)
foundCounts[labelName]++
e.collectProcessMetrics(proc, labelName)
if e.verbose {
log.Printf("Collected metrics for %s (PID: %d)", name, proc.Pid)
log.Printf("Collected metrics for %s (PID: %d)", baseName, proc.Pid)
}
}
}
@@ -228,6 +245,36 @@ func (e *Exporter) collectAndCacheMetrics() {
}
}
// resolveProcessName returns the full process name from the command line.
// On Linux, php-fpm workers show as "php-fpm" via proc.Name(), but their
// cmdline contains pool info like "php-fpm: pool www". This function
// reads /proc/PID/cmdline to get the full command line.
func resolveProcessName(proc *process.Process) string {
cmdline, err := proc.Cmdline()
if err != nil {
return ""
}
if cmdline == "" {
return ""
}
// Split cmdline by null bytes (Linux convention) and rejoin with spaces
parts := strings.Split(cmdline, "\x00")
if len(parts) > 1 {
// Join non-empty parts back with spaces
var args []string
for _, p := range parts {
if p != "" {
args = append(args, p)
}
}
return strings.Join(args, " ")
}
// If no null-separated parts, return the raw cmdline
return cmdline
}
func (pm *ProcessMetrics) resetAll() {
pm.cpuUsage.Reset()
pm.memoryUsage.Reset()
@@ -359,7 +406,7 @@ func readConfig(filename string, verbose bool) (Config, error) {
Pid: 0,
})
case map[interface{}]interface{}:
// New format: struct with name and/or pid
// New format: struct with name, pid, and/or resolveName
procConfig := ProcessConfig{}
if name, ok := p["name"].(string); ok {
procConfig.Name = name
@@ -369,6 +416,9 @@ func readConfig(filename string, verbose bool) (Config, error) {
} else if pid, ok := p["pid"].(float64); ok {
procConfig.Pid = int(pid)
}
if resolveName, ok := p["resolveName"].(bool); ok {
procConfig.ResolveName = resolveName
}
config.Processes = append(config.Processes, procConfig)
default:
return config, fmt.Errorf("invalid process configuration format: %v", proc)