diff options
-rw-r--r-- | collector/fixtures/e2e-64k-page-output.txt | 16 | ||||
-rw-r--r-- | collector/fixtures/e2e-output.txt | 16 | ||||
-rw-r--r-- | collector/fixtures/proc/10/stat | 1 | ||||
-rw-r--r-- | collector/fixtures/proc/sys/kernel/pid_max | 1 | ||||
-rw-r--r-- | collector/fixtures/proc/sys/kernel/threads-max | 1 | ||||
-rw-r--r-- | collector/fixtures/proc/sys/pid_max | 1 | ||||
-rw-r--r-- | collector/fixtures/proc/sys/threads-max | 1 | ||||
-rw-r--r-- | collector/processes_linux.go | 109 | ||||
-rw-r--r-- | collector/processes_linux_test.go | 47 | ||||
-rwxr-xr-x | end-to-end-test.sh | 1 |
10 files changed, 194 insertions, 0 deletions
diff --git a/collector/fixtures/e2e-64k-page-output.txt b/collector/fixtures/e2e-64k-page-output.txt index 3015262..f2f660f 100644 --- a/collector/fixtures/e2e-64k-page-output.txt +++ b/collector/fixtures/e2e-64k-page-output.txt | |||
@@ -2094,6 +2094,21 @@ node_nfsd_server_rpcs_total 18628 | |||
2094 | # HELP node_nfsd_server_threads Total number of NFSd kernel threads that are running. | 2094 | # HELP node_nfsd_server_threads Total number of NFSd kernel threads that are running. |
2095 | # TYPE node_nfsd_server_threads gauge | 2095 | # TYPE node_nfsd_server_threads gauge |
2096 | node_nfsd_server_threads 8 | 2096 | node_nfsd_server_threads 8 |
2097 | # HELP node_processes_max_processes Number of max PIDs limit | ||
2098 | # TYPE node_processes_max_processes gauge | ||
2099 | node_processes_max_processes 123 | ||
2100 | # HELP node_processes_max_threads Limit of threads in the system | ||
2101 | # TYPE node_processes_max_threads gauge | ||
2102 | node_processes_max_threads 7801 | ||
2103 | # HELP node_processes_pids Number of PIDs | ||
2104 | # TYPE node_processes_pids gauge | ||
2105 | node_processes_pids 1 | ||
2106 | # HELP node_processes_state Number of processes in each state. | ||
2107 | # TYPE node_processes_state gauge | ||
2108 | node_processes_state{state="S"} 1 | ||
2109 | # HELP node_processes_threads Allocated threads in system | ||
2110 | # TYPE node_processes_threads gauge | ||
2111 | node_processes_threads 1 | ||
2097 | # HELP node_procs_blocked Number of processes blocked waiting for I/O to complete. | 2112 | # HELP node_procs_blocked Number of processes blocked waiting for I/O to complete. |
2098 | # TYPE node_procs_blocked gauge | 2113 | # TYPE node_procs_blocked gauge |
2099 | node_procs_blocked 0 | 2114 | node_procs_blocked 0 |
@@ -2149,6 +2164,7 @@ node_scrape_collector_success{collector="netdev"} 1 | |||
2149 | node_scrape_collector_success{collector="netstat"} 1 | 2164 | node_scrape_collector_success{collector="netstat"} 1 |
2150 | node_scrape_collector_success{collector="nfs"} 1 | 2165 | node_scrape_collector_success{collector="nfs"} 1 |
2151 | node_scrape_collector_success{collector="nfsd"} 1 | 2166 | node_scrape_collector_success{collector="nfsd"} 1 |
2167 | node_scrape_collector_success{collector="processes"} 1 | ||
2152 | node_scrape_collector_success{collector="qdisc"} 1 | 2168 | node_scrape_collector_success{collector="qdisc"} 1 |
2153 | node_scrape_collector_success{collector="sockstat"} 1 | 2169 | node_scrape_collector_success{collector="sockstat"} 1 |
2154 | node_scrape_collector_success{collector="stat"} 1 | 2170 | node_scrape_collector_success{collector="stat"} 1 |
diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 4eb1dd0..218aaf5 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt | |||
@@ -2094,6 +2094,21 @@ node_nfsd_server_rpcs_total 18628 | |||
2094 | # HELP node_nfsd_server_threads Total number of NFSd kernel threads that are running. | 2094 | # HELP node_nfsd_server_threads Total number of NFSd kernel threads that are running. |
2095 | # TYPE node_nfsd_server_threads gauge | 2095 | # TYPE node_nfsd_server_threads gauge |
2096 | node_nfsd_server_threads 8 | 2096 | node_nfsd_server_threads 8 |
2097 | # HELP node_processes_max_processes Number of max PIDs limit | ||
2098 | # TYPE node_processes_max_processes gauge | ||
2099 | node_processes_max_processes 123 | ||
2100 | # HELP node_processes_max_threads Limit of threads in the system | ||
2101 | # TYPE node_processes_max_threads gauge | ||
2102 | node_processes_max_threads 7801 | ||
2103 | # HELP node_processes_pids Number of PIDs | ||
2104 | # TYPE node_processes_pids gauge | ||
2105 | node_processes_pids 1 | ||
2106 | # HELP node_processes_state Number of processes in each state. | ||
2107 | # TYPE node_processes_state gauge | ||
2108 | node_processes_state{state="S"} 1 | ||
2109 | # HELP node_processes_threads Allocated threads in system | ||
2110 | # TYPE node_processes_threads gauge | ||
2111 | node_processes_threads 1 | ||
2097 | # HELP node_procs_blocked Number of processes blocked waiting for I/O to complete. | 2112 | # HELP node_procs_blocked Number of processes blocked waiting for I/O to complete. |
2098 | # TYPE node_procs_blocked gauge | 2113 | # TYPE node_procs_blocked gauge |
2099 | node_procs_blocked 0 | 2114 | node_procs_blocked 0 |
@@ -2149,6 +2164,7 @@ node_scrape_collector_success{collector="netdev"} 1 | |||
2149 | node_scrape_collector_success{collector="netstat"} 1 | 2164 | node_scrape_collector_success{collector="netstat"} 1 |
2150 | node_scrape_collector_success{collector="nfs"} 1 | 2165 | node_scrape_collector_success{collector="nfs"} 1 |
2151 | node_scrape_collector_success{collector="nfsd"} 1 | 2166 | node_scrape_collector_success{collector="nfsd"} 1 |
2167 | node_scrape_collector_success{collector="processes"} 1 | ||
2152 | node_scrape_collector_success{collector="qdisc"} 1 | 2168 | node_scrape_collector_success{collector="qdisc"} 1 |
2153 | node_scrape_collector_success{collector="sockstat"} 1 | 2169 | node_scrape_collector_success{collector="sockstat"} 1 |
2154 | node_scrape_collector_success{collector="stat"} 1 | 2170 | node_scrape_collector_success{collector="stat"} 1 |
diff --git a/collector/fixtures/proc/10/stat b/collector/fixtures/proc/10/stat new file mode 100644 index 0000000..1451c8a --- /dev/null +++ b/collector/fixtures/proc/10/stat | |||
@@ -0,0 +1 @@ | |||
17 (khungtaskd) S 2 0 0 0 -1 2129984 0 0 0 0 14 0 0 0 20 0 1 0 24 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file | |||
diff --git a/collector/fixtures/proc/sys/kernel/pid_max b/collector/fixtures/proc/sys/kernel/pid_max new file mode 100644 index 0000000..190a180 --- /dev/null +++ b/collector/fixtures/proc/sys/kernel/pid_max | |||
@@ -0,0 +1 @@ | |||
123 | |||
diff --git a/collector/fixtures/proc/sys/kernel/threads-max b/collector/fixtures/proc/sys/kernel/threads-max new file mode 100644 index 0000000..0ccbf45 --- /dev/null +++ b/collector/fixtures/proc/sys/kernel/threads-max | |||
@@ -0,0 +1 @@ | |||
7801 \ No newline at end of file | |||
diff --git a/collector/fixtures/proc/sys/pid_max b/collector/fixtures/proc/sys/pid_max new file mode 100644 index 0000000..190a180 --- /dev/null +++ b/collector/fixtures/proc/sys/pid_max | |||
@@ -0,0 +1 @@ | |||
123 | |||
diff --git a/collector/fixtures/proc/sys/threads-max b/collector/fixtures/proc/sys/threads-max new file mode 100644 index 0000000..0ccbf45 --- /dev/null +++ b/collector/fixtures/proc/sys/threads-max | |||
@@ -0,0 +1 @@ | |||
7801 \ No newline at end of file | |||
diff --git a/collector/processes_linux.go b/collector/processes_linux.go new file mode 100644 index 0000000..fd6ddb1 --- /dev/null +++ b/collector/processes_linux.go | |||
@@ -0,0 +1,109 @@ | |||
1 | // Copyright 2018 The Prometheus Authors | ||
2 | // Licensed under the Apache License, Version 2.0 (the "License"); | ||
3 | // you may not use this file except in compliance with the License. | ||
4 | // You may obtain a copy of the License at | ||
5 | // | ||
6 | // http://www.apache.org/licenses/LICENSE-2.0 | ||
7 | // | ||
8 | // Unless required by applicable law or agreed to in writing, software | ||
9 | // distributed under the License is distributed on an "AS IS" BASIS, | ||
10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
11 | // See the License for the specific language governing permissions and | ||
12 | // limitations under the License. | ||
13 | |||
14 | // +build !noprocesses | ||
15 | |||
16 | package collector | ||
17 | |||
18 | import ( | ||
19 | "fmt" | ||
20 | "github.com/prometheus/client_golang/prometheus" | ||
21 | "github.com/prometheus/procfs" | ||
22 | ) | ||
23 | |||
24 | type processCollector struct { | ||
25 | threadAlloc *prometheus.Desc | ||
26 | threadLimit *prometheus.Desc | ||
27 | procsState *prometheus.Desc | ||
28 | pidUsed *prometheus.Desc | ||
29 | pidMax *prometheus.Desc | ||
30 | } | ||
31 | |||
32 | func init() { | ||
33 | registerCollector("processes", defaultDisabled, NewProcessStatCollector) | ||
34 | } | ||
35 | |||
36 | func NewProcessStatCollector() (Collector, error) { | ||
37 | subsystem := "processes" | ||
38 | return &processCollector{ | ||
39 | threadAlloc: prometheus.NewDesc( | ||
40 | prometheus.BuildFQName(namespace, subsystem, "threads"), | ||
41 | "Allocated threads in system", | ||
42 | nil, nil, | ||
43 | ), | ||
44 | threadLimit: prometheus.NewDesc( | ||
45 | prometheus.BuildFQName(namespace, subsystem, "max_threads"), | ||
46 | "Limit of threads in the system", | ||
47 | nil, nil, | ||
48 | ), | ||
49 | procsState: prometheus.NewDesc( | ||
50 | prometheus.BuildFQName(namespace, subsystem, "state"), | ||
51 | "Number of processes in each state.", | ||
52 | []string{"state"}, nil, | ||
53 | ), | ||
54 | pidUsed: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "pids"), | ||
55 | "Number of PIDs", nil, nil, | ||
56 | ), | ||
57 | pidMax: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "max_processes"), | ||
58 | "Number of max PIDs limit", nil, nil, | ||
59 | ), | ||
60 | }, nil | ||
61 | } | ||
62 | func (t *processCollector) Update(ch chan<- prometheus.Metric) error { | ||
63 | pids, states, threads, err := getAllocatedThreads() | ||
64 | if err != nil { | ||
65 | return fmt.Errorf("Unable to retrieve number of allocated threads %v\n", err) | ||
66 | } | ||
67 | |||
68 | ch <- prometheus.MustNewConstMetric(t.threadAlloc, prometheus.GaugeValue, float64(threads)) | ||
69 | maxThreads, err := readUintFromFile(procFilePath("sys/kernel/threads-max")) | ||
70 | if err != nil { | ||
71 | return fmt.Errorf("Unable to retrieve limit number of threads %v\n", err) | ||
72 | } | ||
73 | ch <- prometheus.MustNewConstMetric(t.threadLimit, prometheus.GaugeValue, float64(maxThreads)) | ||
74 | |||
75 | for state := range states { | ||
76 | ch <- prometheus.MustNewConstMetric(t.procsState, prometheus.GaugeValue, float64(states[state]), state) | ||
77 | } | ||
78 | |||
79 | pidM, err := readUintFromFile(procFilePath("sys/kernel/pid_max")) | ||
80 | if err != nil { | ||
81 | return fmt.Errorf("Unable to retrieve limit number of maximum pids alloved %v\n", err) | ||
82 | } | ||
83 | ch <- prometheus.MustNewConstMetric(t.pidUsed, prometheus.GaugeValue, float64(pids)) | ||
84 | ch <- prometheus.MustNewConstMetric(t.pidMax, prometheus.GaugeValue, float64(pidM)) | ||
85 | |||
86 | return nil | ||
87 | } | ||
88 | |||
89 | func getAllocatedThreads() (int, map[string]int32, int, error) { | ||
90 | fs, err := procfs.NewFS(*procPath) | ||
91 | if err != nil { | ||
92 | return 0, nil, 0, err | ||
93 | } | ||
94 | p, err := fs.AllProcs() | ||
95 | if err != nil { | ||
96 | return 0, nil, 0, err | ||
97 | } | ||
98 | thread := 0 | ||
99 | procStates := make(map[string]int32) | ||
100 | for _, pid := range p { | ||
101 | stat, err := pid.NewStat() | ||
102 | if err != nil { | ||
103 | return 0, nil, 0, err | ||
104 | } | ||
105 | procStates[stat.State] += 1 | ||
106 | thread += stat.NumThreads | ||
107 | } | ||
108 | return len(p), procStates, thread, nil | ||
109 | } | ||
diff --git a/collector/processes_linux_test.go b/collector/processes_linux_test.go new file mode 100644 index 0000000..1cf9e4f --- /dev/null +++ b/collector/processes_linux_test.go | |||
@@ -0,0 +1,47 @@ | |||
1 | // Copyright 2018 The Prometheus Authors | ||
2 | // Licensed under the Apache License, Version 2.0 (the "License"); | ||
3 | // you may not use this file except in compliance with the License. | ||
4 | // You may obtain a copy of the License at | ||
5 | // | ||
6 | // http://www.apache.org/licenses/LICENSE-2.0 | ||
7 | // | ||
8 | // Unless required by applicable law or agreed to in writing, software | ||
9 | // distributed under the License is distributed on an "AS IS" BASIS, | ||
10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
11 | // See the License for the specific language governing permissions and | ||
12 | // limitations under the License. | ||
13 | |||
14 | // +build !noprocesses | ||
15 | |||
16 | package collector | ||
17 | |||
18 | import ( | ||
19 | "testing" | ||
20 | |||
21 | "gopkg.in/alecthomas/kingpin.v2" | ||
22 | ) | ||
23 | |||
24 | func TestReadProcessStatus(t *testing.T) { | ||
25 | if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "fixtures/proc"}); err != nil { | ||
26 | t.Fatal(err) | ||
27 | } | ||
28 | want := 1 | ||
29 | pids, states, threads, err := getAllocatedThreads() | ||
30 | if err != nil { | ||
31 | t.Fatalf("Cannot retrieve data from procfs getAllocatedThreads function: %v ", err) | ||
32 | } | ||
33 | if threads < want { | ||
34 | t.Fatalf("Current threads: %d Shouldn't be less than wanted %d", threads, want) | ||
35 | } | ||
36 | if states == nil { | ||
37 | |||
38 | t.Fatalf("Process states cannot be nil %v:", states) | ||
39 | } | ||
40 | maxPid, err := readUintFromFile(procFilePath("sys/kernel/pid_max")) | ||
41 | if err != nil { | ||
42 | t.Fatalf("Unable to retrieve limit number of maximum pids alloved %v\n", err) | ||
43 | } | ||
44 | if uint64(pids) > maxPid || pids == 0 { | ||
45 | t.Fatalf("Total running pids cannot be greater than %d or equals to 0", maxPid) | ||
46 | } | ||
47 | } | ||
diff --git a/end-to-end-test.sh b/end-to-end-test.sh index ec99cae..469d244 100755 --- a/end-to-end-test.sh +++ b/end-to-end-test.sh | |||
@@ -36,6 +36,7 @@ enabled_collectors=$(cat << COLLECTORS | |||
36 | wifi | 36 | wifi |
37 | xfs | 37 | xfs |
38 | zfs | 38 | zfs |
39 | processes | ||
39 | COLLECTORS | 40 | COLLECTORS |
40 | ) | 41 | ) |
41 | disabled_collectors=$(cat << COLLECTORS | 42 | disabled_collectors=$(cat << COLLECTORS |