Skip to content

Commit

Permalink
Calculate process start time and uptime from file stat data of /proc/…
Browse files Browse the repository at this point in the history
…<pid> directory
  • Loading branch information
d10v committed Jun 22, 2015
1 parent 3e9002e commit 9fe0b0d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/scollector/collectors/processes_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func WatchProcesses() {
func linuxProcMonitor(w *WatchedProc, md *opentsdb.MultiDataPoint) error {
var err error
for pid, id := range w.Processes {
file_status, e := os.Stat("/proc/" + pid)
if e != nil {
w.Remove(pid)
continue
}
stats_file, e := ioutil.ReadFile("/proc/" + pid + "/stat")
if e != nil {
w.Remove(pid)
Expand Down Expand Up @@ -91,6 +96,7 @@ func linuxProcMonitor(w *WatchedProc, md *opentsdb.MultiDataPoint) error {
}
}
}
start_ts := file_status.ModTime().Unix()
Add(md, "linux.proc.cpu", stats[13], opentsdb.TagSet{"type": "user"}.Merge(tags), metadata.Counter, metadata.Pct, descLinuxProcCPUUser)
Add(md, "linux.proc.cpu", stats[14], opentsdb.TagSet{"type": "system"}.Merge(tags), metadata.Counter, metadata.Pct, descLinuxProcCPUSystem)
Add(md, "linux.proc.mem.fault", stats[9], opentsdb.TagSet{"type": "minflt"}.Merge(tags), metadata.Counter, metadata.Fault, descLinuxProcMemFaultMin)
Expand All @@ -104,6 +110,8 @@ func linuxProcMonitor(w *WatchedProc, md *opentsdb.MultiDataPoint) error {
Add(md, "linux.proc.io_bytes", io[4], opentsdb.TagSet{"type": "read"}.Merge(tags), metadata.Counter, metadata.Bytes, descLinuxProcIoBytesRead)
Add(md, "linux.proc.io_bytes", io[5], opentsdb.TagSet{"type": "write"}.Merge(tags), metadata.Counter, metadata.Bytes, descLinuxProcIoBytesWrite)
Add(md, "linux.proc.num_fds", len(fds), tags, metadata.Gauge, metadata.Files, descLinuxProcFd)
Add(md, "linux.proc.start_time", start_ts, tags, metadata.Gauge, metadata.Timestamp, descLinuxProcStartTS)
Add(md, "linux.proc.uptime", now()-start_ts, tags, metadata.Gauge, metadata.Second, descLinuxProcUptime)
}
return err
}
Expand All @@ -124,6 +132,8 @@ const (
descLinuxProcFd = "The number of open file descriptors."
descLinuxSoftFileLimit = "The soft limit on the number of open file descriptors."
descLinuxHardFileLimit = "The hard limit on the number of open file descriptors."
descLinuxProcUptime = "The length of time, in seconds, since the process was started."
descLinuxProcStartTS = "The timestamp of process start."
)

func getLinuxProccesses() ([]*Process, error) {
Expand Down

0 comments on commit 9fe0b0d

Please sign in to comment.