Skip to content

Commit 3ead7e8

Browse files
author
Sergei Semenchuk
authored
parse /proc/sys/kernel/random (prometheus#305)
Signed-off-by: binjip978 <[email protected]>
1 parent 7c97c6f commit 3ead7e8

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

fixtures.ttar

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,32 @@ Mode: 644
23462346
Directory: fixtures/proc/sys
23472347
Mode: 775
23482348
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2349+
Directory: fixtures/proc/sys/kernel
2350+
Mode: 775
2351+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2352+
Directory: fixtures/proc/sys/kernel/random
2353+
Mode: 755
2354+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2355+
Path: fixtures/proc/sys/kernel/random/entropy_avail
2356+
Lines: 1
2357+
3943
2358+
Mode: 644
2359+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2360+
Path: fixtures/proc/sys/kernel/random/poolsize
2361+
Lines: 1
2362+
4096
2363+
Mode: 644
2364+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2365+
Path: fixtures/proc/sys/kernel/random/urandom_min_reseed_secs
2366+
Lines: 1
2367+
60
2368+
Mode: 644
2369+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2370+
Path: fixtures/proc/sys/kernel/random/write_wakeup_threshold
2371+
Lines: 1
2372+
3072
2373+
Mode: 644
2374+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
23492375
Directory: fixtures/proc/sys/vm
23502376
Mode: 775
23512377
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

kernel_random.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2020 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 !windows
15+
16+
package procfs
17+
18+
import (
19+
"os"
20+
21+
"github.com/prometheus/procfs/internal/util"
22+
)
23+
24+
// KernelRandom contains information about to the kernel's random number generator.
25+
type KernelRandom struct {
26+
// EntropyAvaliable gives the available entropy, in bits.
27+
EntropyAvaliable *uint64
28+
// PoolSize gives the size of the entropy pool, in bytes.
29+
PoolSize *uint64
30+
// URandomMinReseedSeconds is the number of seconds after which the DRNG will be reseeded.
31+
URandomMinReseedSeconds *uint64
32+
// WriteWakeupThreshold the number of bits of entropy below which we wake up processes
33+
// that do a select(2) or poll(2) for write access to /dev/random.
34+
WriteWakeupThreshold *uint64
35+
// ReadWakeupThreshold is the number of bits of entropy required for waking up processes that sleep
36+
// waiting for entropy from /dev/random.
37+
ReadWakeupThreshold *uint64
38+
}
39+
40+
// KernelRandom returns values from /proc/sys/kernel/random.
41+
func (fs FS) KernelRandom() (KernelRandom, error) {
42+
random := KernelRandom{}
43+
44+
for file, p := range map[string]**uint64{
45+
"entropy_avail": &random.EntropyAvaliable,
46+
"poolsize": &random.PoolSize,
47+
"urandom_min_reseed_secs": &random.URandomMinReseedSeconds,
48+
"write_wakeup_threshold": &random.WriteWakeupThreshold,
49+
"read_wakeup_threshold": &random.ReadWakeupThreshold,
50+
} {
51+
val, err := util.ReadUintFromFile(fs.proc.Path("sys", "kernel", "random", file))
52+
if os.IsNotExist(err) {
53+
continue
54+
}
55+
if err != nil {
56+
return random, err
57+
}
58+
*p = &val
59+
}
60+
61+
return random, nil
62+
}

kernel_random_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 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 !windows
15+
16+
package procfs
17+
18+
import (
19+
"testing"
20+
)
21+
22+
const procfsFixtures = "fixtures/proc"
23+
24+
func TestKernelRandom(t *testing.T) {
25+
fs, err := NewFS(procfsFixtures)
26+
if err != nil {
27+
t.Fatalf("failed to access %s: %v", procfsFixtures, err)
28+
}
29+
30+
random, err := fs.KernelRandom()
31+
if err != nil {
32+
t.Fatalf("failed to collect %s/sys/kernel/random: %v", procfsFixtures, err)
33+
}
34+
35+
if *random.EntropyAvaliable != 3943 {
36+
t.Errorf("entropy_avail, want %d got %d", 3943, *random.EntropyAvaliable)
37+
}
38+
if *random.PoolSize != 4096 {
39+
t.Errorf("poolsize, want %d got %d", 4096, *random.PoolSize)
40+
}
41+
if *random.URandomMinReseedSeconds != 60 {
42+
t.Errorf("urandom_min_reseed_secs, want %d got %d", 60, *random.URandomMinReseedSeconds)
43+
}
44+
if *random.WriteWakeupThreshold != 3072 {
45+
t.Errorf("write_wakeup_threshold, want %d got %d", 3072, *random.WriteWakeupThreshold)
46+
}
47+
if random.ReadWakeupThreshold != nil {
48+
t.Errorf("read_wakeup_threshold, want %v got %d", nil, *random.ReadWakeupThreshold)
49+
}
50+
}

0 commit comments

Comments
 (0)