2019-07-12 17:18:52 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2021-02-04 17:10:10 +03:00
|
|
|
"bufio"
|
2019-07-12 17:18:52 +03:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2021-02-04 17:10:10 +03:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-07-12 17:18:52 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const statFilepath = "/proc/self/stat"
|
|
|
|
|
|
|
|
// See https://github.com/prometheus/procfs/blob/a4ac0826abceb44c40fc71daed2b301db498b93e/proc_stat.go#L40 .
|
|
|
|
const userHZ = 100
|
|
|
|
|
|
|
|
// See http://man7.org/linux/man-pages/man5/proc.5.html
|
|
|
|
type procStat struct {
|
|
|
|
State byte
|
|
|
|
Ppid int
|
|
|
|
Pgrp int
|
|
|
|
Session int
|
|
|
|
TtyNr int
|
|
|
|
Tpgid int
|
|
|
|
Flags uint
|
|
|
|
Minflt uint
|
|
|
|
Cminflt uint
|
|
|
|
Majflt uint
|
|
|
|
Cmajflt uint
|
|
|
|
Utime uint
|
|
|
|
Stime uint
|
|
|
|
Cutime int
|
|
|
|
Cstime int
|
|
|
|
Priority int
|
|
|
|
Nice int
|
|
|
|
NumThreads int
|
|
|
|
ItrealValue int
|
|
|
|
Starttime uint64
|
|
|
|
Vsize uint
|
|
|
|
Rss int
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeProcessMetrics(w io.Writer) {
|
|
|
|
data, err := ioutil.ReadFile(statFilepath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: cannot open %s: %s", statFilepath, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Search for the end of command.
|
|
|
|
n := bytes.LastIndex(data, []byte(") "))
|
|
|
|
if n < 0 {
|
|
|
|
log.Printf("ERROR: cannot find command in parentheses in %q read from %s", data, statFilepath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data = data[n+2:]
|
|
|
|
|
|
|
|
var p procStat
|
|
|
|
bb := bytes.NewBuffer(data)
|
|
|
|
_, err = fmt.Fscanf(bb, "%c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
|
|
|
|
&p.State, &p.Ppid, &p.Pgrp, &p.Session, &p.TtyNr, &p.Tpgid, &p.Flags, &p.Minflt, &p.Cminflt, &p.Majflt, &p.Cmajflt,
|
|
|
|
&p.Utime, &p.Stime, &p.Cutime, &p.Cstime, &p.Priority, &p.Nice, &p.NumThreads, &p.ItrealValue, &p.Starttime, &p.Vsize, &p.Rss)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: cannot parse %q read from %s: %s", data, statFilepath, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is expensive obtaining `process_open_fds` when big number of file descriptors is opened,
|
|
|
|
// don't do it here.
|
|
|
|
|
2019-08-05 19:16:20 +03:00
|
|
|
utime := float64(p.Utime) / userHZ
|
|
|
|
stime := float64(p.Stime) / userHZ
|
|
|
|
fmt.Fprintf(w, "process_cpu_seconds_system_total %g\n", stime)
|
|
|
|
fmt.Fprintf(w, "process_cpu_seconds_total %g\n", utime+stime)
|
|
|
|
fmt.Fprintf(w, "process_cpu_seconds_user_total %g\n", utime)
|
2019-07-12 17:18:52 +03:00
|
|
|
fmt.Fprintf(w, "process_major_pagefaults_total %d\n", p.Majflt)
|
|
|
|
fmt.Fprintf(w, "process_minor_pagefaults_total %d\n", p.Minflt)
|
|
|
|
fmt.Fprintf(w, "process_num_threads %d\n", p.NumThreads)
|
|
|
|
fmt.Fprintf(w, "process_resident_memory_bytes %d\n", p.Rss*4096)
|
|
|
|
fmt.Fprintf(w, "process_start_time_seconds %d\n", startTimeSeconds)
|
|
|
|
fmt.Fprintf(w, "process_virtual_memory_bytes %d\n", p.Vsize)
|
|
|
|
}
|
|
|
|
|
|
|
|
var startTimeSeconds = time.Now().Unix()
|
2021-02-04 17:10:10 +03:00
|
|
|
|
2021-02-04 17:28:55 +03:00
|
|
|
// WriteFDMetrics writes process_max_fds and process_open_fds metrics to w.
|
|
|
|
func writeFDMetrics(w io.Writer) {
|
2021-02-04 17:10:10 +03:00
|
|
|
totalOpenFDs, err := getOpenFDsCount("/proc/self/fd")
|
|
|
|
if err != nil {
|
2021-02-04 17:28:55 +03:00
|
|
|
log.Printf("ERROR: cannot determine open file descriptors count: %s", err)
|
2021-02-04 17:10:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
maxOpenFDs, err := getMaxFilesLimit("/proc/self/limits")
|
|
|
|
if err != nil {
|
2021-02-04 17:28:55 +03:00
|
|
|
log.Printf("ERROR: cannot determine the limit on open file descritors: %s", err)
|
2021-02-04 17:10:10 +03:00
|
|
|
return
|
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
fmt.Fprintf(w, "process_max_fds %d\n", maxOpenFDs)
|
|
|
|
fmt.Fprintf(w, "process_open_fds %d\n", totalOpenFDs)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 17:28:55 +03:00
|
|
|
func getOpenFDsCount(path string) (uint64, error) {
|
2021-02-04 17:10:10 +03:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2021-02-04 17:28:55 +03:00
|
|
|
return 0, err
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
var totalOpenFDs uint64
|
|
|
|
for {
|
2021-02-04 17:28:55 +03:00
|
|
|
names, err := f.Readdirnames(512)
|
|
|
|
if err == io.EOF {
|
2021-02-04 17:10:10 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-02-04 17:28:55 +03:00
|
|
|
return 0, fmt.Errorf("unexpected error at Readdirnames: %s", err)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
|
|
|
totalOpenFDs += uint64(len(names))
|
|
|
|
}
|
|
|
|
return totalOpenFDs, nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 17:28:55 +03:00
|
|
|
func getMaxFilesLimit(path string) (uint64, error) {
|
2021-02-04 17:10:10 +03:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2021-02-04 17:28:55 +03:00
|
|
|
return 0, err
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2021-02-04 17:28:55 +03:00
|
|
|
prefix := "Max open files"
|
2021-02-04 17:10:10 +03:00
|
|
|
scan := bufio.NewScanner(f)
|
2021-02-04 17:28:55 +03:00
|
|
|
for scan.Scan() {
|
|
|
|
s := scan.Text()
|
|
|
|
if !strings.HasPrefix(s, prefix) {
|
2021-02-04 17:10:10 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
text := strings.TrimSpace(s[len(prefix):])
|
|
|
|
// Extract soft limit.
|
|
|
|
n := strings.IndexByte(text, ' ')
|
|
|
|
if n < 0 {
|
|
|
|
return 0, fmt.Errorf("cannot extract soft limit from %q", s)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
text = text[:n]
|
|
|
|
if text == "unlimited" {
|
|
|
|
return 1<<64 - 1, nil
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
limit, err := strconv.ParseUint(text, 10, 64)
|
2021-02-04 17:10:10 +03:00
|
|
|
if err != nil {
|
2021-02-04 17:28:55 +03:00
|
|
|
return 0, fmt.Errorf("cannot parse soft limit from %q: %s", s, err)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
return limit, nil
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
return 0, fmt.Errorf("cannot find max open files limit")
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|