From 208021c2bb959072751744f4a44d2b293eaaebe6 Mon Sep 17 00:00:00 2001 From: misluyu Date: Wed, 23 Jul 2025 11:22:03 +0800 Subject: [PATCH 1/6] feat(metric): Add 'originx_process_last_seen' metric. --- config/config.go | 9 +++++---- metric/metric.go | 18 ++++++++++++++++++ metric/process.go | 12 ++++++++++++ proc/pid.go | 2 ++ proc/proc.go | 2 ++ 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 388e183..0196cc9 100644 --- a/config/config.go +++ b/config/config.go @@ -24,10 +24,11 @@ type Config struct { } type MetricConfig struct { - PingSpan int `yaml:"ping_span"` - PidSpan int `yaml:"pid_span"` - LRUCacheSize int `yaml:"lru_cache_size"` - ProcessTime bool `yaml:"process_time"` + PingSpan int `yaml:"ping_span"` + PidSpan int `yaml:"pid_span"` + LRUCacheSize int `yaml:"lru_cache_size"` + ProcessTime bool `yaml:"process_time"` + ScrapProcessLastSeen bool `yaml:"scrap_process_last_seen"` } func (m *MetricConfig) setDefault() { diff --git a/metric/metric.go b/metric/metric.go index 40c1d95..dd23990 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -59,6 +59,24 @@ func (rc *RttCollector) Collect(ch chan<- prometheus.Metric) { } proc.GlobalPidMutex.RUnlock() } + + if cfg.Metric.ScrapProcessLastSeen { + proc.GlobalPidMutex.RLock() + for pid, processInfo := range proc.GlobalNeedMonitorPid { + // TODO Check network traffic + ch <- prometheus.MustNewConstMetric( + processStartTime, prometheus.GaugeValue, + float64(processInfo.LastSeen.Unix()), + strconv.FormatUint(uint64(pid), 10), + cfg.NodeName, + cfg.NodeIP, + processInfo.ContainId, + ) + pid2cid[pid] = processInfo.ContainId + } + proc.GlobalPidMutex.RUnlock() + } + middleware.MiddlewareInstance.Mu.Lock() for pid, conn := range middleware.MiddlewareInstance.Pid2Connect { for _, info := range conn { diff --git a/metric/process.go b/metric/process.go index d20fab1..0ad1fcf 100644 --- a/metric/process.go +++ b/metric/process.go @@ -14,6 +14,18 @@ var processStartTime = prometheus.NewDesc( nil, ) +var processLastSeen = prometheus.NewDesc( + "originx_process_last_seen", + "Unix timestamp when the process was last detected.", + []string{ + "pid", + "node_name", + "node_ip", + "container_id", + }, + nil, +) + var middlewareConnect = prometheus.NewDesc( "apo_network_middleware_connect", "Middleware Connect", diff --git a/proc/pid.go b/proc/pid.go index 6435725..89b6c2c 100644 --- a/proc/pid.go +++ b/proc/pid.go @@ -37,6 +37,7 @@ var GlobalNeedMonitorPid = make(map[uint32]*ProcessInfo) type ProcessInfo struct { StartTime time.Time ContainId string + LastSeen time.Time } func GetPid() { @@ -102,6 +103,7 @@ func listPids() map[uint32]*ProcessInfo { pids[intpid] = &ProcessInfo{ StartTime: *startTime, ContainId: cid, + LastSeen: time.Now(), } } return pids diff --git a/proc/proc.go b/proc/proc.go index 086eeb7..8627714 100644 --- a/proc/proc.go +++ b/proc/proc.go @@ -25,6 +25,7 @@ func getCommand(pid uint32) string { } return strings.Replace(string(cmdline), "\x00", " ", -1) } + func getContainerId(pid uint32) string { data, err := os.ReadFile(Path(pid, "cgroup")) if err != nil { @@ -54,6 +55,7 @@ func getContainerId(pid uint32) string { } return "" } + func getProcessStartTime(pid uint32) (*time.Time, error) { data, err := os.ReadFile(Path(pid, "stat")) if err != nil { From f212871ed9056982b2dcdb22d619728a63200760 Mon Sep 17 00:00:00 2001 From: misluyu Date: Wed, 23 Jul 2025 11:43:15 +0800 Subject: [PATCH 2/6] fix(metric): typo --- metric/metric.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metric/metric.go b/metric/metric.go index dd23990..ed817c7 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -65,7 +65,7 @@ func (rc *RttCollector) Collect(ch chan<- prometheus.Metric) { for pid, processInfo := range proc.GlobalNeedMonitorPid { // TODO Check network traffic ch <- prometheus.MustNewConstMetric( - processStartTime, prometheus.GaugeValue, + processLastSeen, prometheus.GaugeValue, float64(processInfo.LastSeen.Unix()), strconv.FormatUint(uint64(pid), 10), cfg.NodeName, From a40508d2dc23017d5e47af2fca822078e34d847b Mon Sep 17 00:00:00 2001 From: misluyu Date: Wed, 23 Jul 2025 14:03:46 +0800 Subject: [PATCH 3/6] fix: Last seen not update. --- proc/pid.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/proc/pid.go b/proc/pid.go index 89b6c2c..a41c863 100644 --- a/proc/pid.go +++ b/proc/pid.go @@ -64,6 +64,8 @@ func UpdatePid() { for pid := range newSet { if _, ok := GlobalNeedMonitorPid[pid]; !ok { GlobalNeedMonitorPid[pid] = pids[pid] + } else { + GlobalNeedMonitorPid[pid].LastSeen = pids[pid].LastSeen } } GlobalPidMutex.Unlock() From b7e6eeb53487499c5552f4bae55b10f51119c538 Mon Sep 17 00:00:00 2001 From: misluyu Date: Thu, 24 Jul 2025 11:58:35 +0800 Subject: [PATCH 4/6] feat: Add 'comm' lable. --- metric/metric.go | 1 + metric/process.go | 1 + proc/pid.go | 3 +++ proc/proc.go | 9 +++++++++ 4 files changed, 14 insertions(+) diff --git a/metric/metric.go b/metric/metric.go index ed817c7..8649305 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -71,6 +71,7 @@ func (rc *RttCollector) Collect(ch chan<- prometheus.Metric) { cfg.NodeName, cfg.NodeIP, processInfo.ContainId, + processInfo.Comm, ) pid2cid[pid] = processInfo.ContainId } diff --git a/metric/process.go b/metric/process.go index 0ad1fcf..de91822 100644 --- a/metric/process.go +++ b/metric/process.go @@ -22,6 +22,7 @@ var processLastSeen = prometheus.NewDesc( "node_name", "node_ip", "container_id", + "comm", }, nil, ) diff --git a/proc/pid.go b/proc/pid.go index a41c863..f217491 100644 --- a/proc/pid.go +++ b/proc/pid.go @@ -38,6 +38,7 @@ type ProcessInfo struct { StartTime time.Time ContainId string LastSeen time.Time + Comm string } func GetPid() { @@ -102,10 +103,12 @@ func listPids() map[uint32]*ProcessInfo { if err != nil { continue } + comm := getComm(uint32(pid)) pids[intpid] = &ProcessInfo{ StartTime: *startTime, ContainId: cid, LastSeen: time.Now(), + Comm: comm, } } return pids diff --git a/proc/proc.go b/proc/proc.go index 8627714..5e13954 100644 --- a/proc/proc.go +++ b/proc/proc.go @@ -26,6 +26,15 @@ func getCommand(pid uint32) string { return strings.Replace(string(cmdline), "\x00", " ", -1) } +func getComm(pid uint32) string { + comm, err := os.ReadFile(Path(pid, "comm")) + if err != nil { + return "" + } + + return string(comm) +} + func getContainerId(pid uint32) string { data, err := os.ReadFile(Path(pid, "cgroup")) if err != nil { From b2ac639e40208ac16de2925d4025983a015da89b Mon Sep 17 00:00:00 2001 From: misluyu Date: Thu, 24 Jul 2025 14:17:41 +0800 Subject: [PATCH 5/6] fix: Trim comm right whitespace. --- proc/proc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proc/proc.go b/proc/proc.go index 5e13954..0a5c9b5 100644 --- a/proc/proc.go +++ b/proc/proc.go @@ -32,7 +32,7 @@ func getComm(pid uint32) string { return "" } - return string(comm) + return strings.TrimRight(string(comm), "\n\r ") } func getContainerId(pid uint32) string { From 9aa1699d04ffab6e4adaf2087b055a10b8a2772b Mon Sep 17 00:00:00 2001 From: misluyu Date: Fri, 25 Jul 2025 10:29:28 +0800 Subject: [PATCH 6/6] feat: Add 'comm' label to originx_process_start_time. --- metric/metric.go | 1 + metric/process.go | 1 + 2 files changed, 2 insertions(+) diff --git a/metric/metric.go b/metric/metric.go index 8649305..8437d87 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -54,6 +54,7 @@ func (rc *RttCollector) Collect(ch chan<- prometheus.Metric) { cfg.NodeName, cfg.NodeIP, processInfo.ContainId, + processInfo.Comm, ) pid2cid[pid] = processInfo.ContainId } diff --git a/metric/process.go b/metric/process.go index de91822..14dca92 100644 --- a/metric/process.go +++ b/metric/process.go @@ -10,6 +10,7 @@ var processStartTime = prometheus.NewDesc( "node_name", "node_ip", "container_id", + "comm", }, nil, )