Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
20 changes: 20 additions & 0 deletions metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,31 @@ func (rc *RttCollector) Collect(ch chan<- prometheus.Metric) {
cfg.NodeName,
cfg.NodeIP,
processInfo.ContainId,
processInfo.Comm,
)
pid2cid[pid] = processInfo.ContainId
}
proc.GlobalPidMutex.RUnlock()
}

if cfg.Metric.ScrapProcessLastSeen {
proc.GlobalPidMutex.RLock()
for pid, processInfo := range proc.GlobalNeedMonitorPid {
// TODO Check network traffic
ch <- prometheus.MustNewConstMetric(
processLastSeen, prometheus.GaugeValue,
float64(processInfo.LastSeen.Unix()),
strconv.FormatUint(uint64(pid), 10),
cfg.NodeName,
cfg.NodeIP,
processInfo.ContainId,
processInfo.Comm,
)
pid2cid[pid] = processInfo.ContainId
}
proc.GlobalPidMutex.RUnlock()
}

middleware.MiddlewareInstance.Mu.Lock()
for pid, conn := range middleware.MiddlewareInstance.Pid2Connect {
for _, info := range conn {
Expand Down
14 changes: 14 additions & 0 deletions metric/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ var processStartTime = prometheus.NewDesc(
"node_name",
"node_ip",
"container_id",
"comm",
},
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",
"comm",
},
nil,
)
Expand Down
7 changes: 7 additions & 0 deletions proc/pid.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var GlobalNeedMonitorPid = make(map[uint32]*ProcessInfo)
type ProcessInfo struct {
StartTime time.Time
ContainId string
LastSeen time.Time
Comm string
}

func GetPid() {
Expand All @@ -63,6 +65,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()
Expand Down Expand Up @@ -99,9 +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
Expand Down
11 changes: 11 additions & 0 deletions proc/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ 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 strings.TrimRight(string(comm), "\n\r ")
}

func getContainerId(pid uint32) string {
data, err := os.ReadFile(Path(pid, "cgroup"))
if err != nil {
Expand Down Expand Up @@ -54,6 +64,7 @@ func getContainerId(pid uint32) string {
}
return ""
}

func getProcessStartTime(pid uint32) (*time.Time, error) {
data, err := os.ReadFile(Path(pid, "stat"))
if err != nil {
Expand Down