This collects usage information (per-user) on the Linux kernel keyring See: https://man7.org/linux/man-pages/man7/keyrings.7.html It is disabled by default because cardinality is one-per user. Signed-off-by: Robert Zimmerman <rzimmerman@cloudflare.com>pull/2786/head
parent
381f32b1c5
commit
4b0986fcee
@ -0,0 +1,5 @@
|
||||
0: 51 50/50 44/1000000 883/25000000
|
||||
101: 1 1/1 1/200 9/20000
|
||||
102: 1 1/1 1/200 9/20000
|
||||
104: 1 1/1 1/200 9/20000
|
||||
1000: 11 11/12 13/200 250/20000
|
@ -0,0 +1,111 @@
|
||||
// Copyright 2023 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build linux && !nokey_users
|
||||
// +build linux,!nokey_users
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
const (
|
||||
keyUsersSubsystem = "keys"
|
||||
)
|
||||
|
||||
var (
|
||||
// See https://man7.org/linux/man-pages/man7/keyrings.7.html
|
||||
keyUsersLineRegex = regexp.MustCompile(`^\s*(?P<uid>\d+):\s+(?P<usage>\d+)\s+(?P<nkeys>\d+)/(?P<nikeys>\d+)\s+(?P<qnkeys>\d+)/(?P<maxkeys>\d+)\s+(?P<qnbytes>\d+)/(?P<maxbytes>\d+)$`)
|
||||
)
|
||||
|
||||
type keyUsersCollector struct {
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
type keyUsersEntry struct {
|
||||
uid int64
|
||||
name string
|
||||
}
|
||||
|
||||
func init() {
|
||||
registerCollector("key_users", defaultDisabled, NewKeyUsersCollector)
|
||||
}
|
||||
|
||||
func NewKeyUsersCollector(logger log.Logger) (Collector, error) {
|
||||
return &keyUsersCollector{logger}, nil
|
||||
}
|
||||
|
||||
func (c *keyUsersCollector) Update(ch chan<- prometheus.Metric) error {
|
||||
keyUsers, err := c.getKeyUsers()
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't get key-users: %w", err)
|
||||
}
|
||||
level.Debug(c.logger).Log("msg", "Set node_keys", "keyUsers", keyUsers)
|
||||
for k, v := range keyUsers {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, keyUsersSubsystem, k.name),
|
||||
fmt.Sprintf("Key utilization field %s.", k.name),
|
||||
[]string{"uid"}, nil,
|
||||
),
|
||||
prometheus.GaugeValue, v, strconv.FormatInt(int64(k.uid), 10),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *keyUsersCollector) getKeyUsers() (map[keyUsersEntry]float64, error) {
|
||||
file, err := os.Open(procFilePath("key-users"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return parseKeyUsers(file)
|
||||
}
|
||||
|
||||
func parseKeyUsers(r io.Reader) (map[keyUsersEntry]float64, error) {
|
||||
var scanner = bufio.NewScanner(r)
|
||||
var keyUsersInfo = map[keyUsersEntry]float64{}
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
match := keyUsersLineRegex.FindStringSubmatch(line)
|
||||
names := keyUsersLineRegex.SubexpNames()
|
||||
if len(match) != len(names) {
|
||||
return nil, fmt.Errorf("invalid line in key-users: %s", line)
|
||||
}
|
||||
uid, err := strconv.ParseInt(match[1], 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid UID in key-users: %s", match[0])
|
||||
}
|
||||
for i := 2; i < len(names); i++ {
|
||||
v, err := strconv.ParseInt(match[i], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid metric '%s' in key-users: %s", names[i], match[i])
|
||||
}
|
||||
keyUsersInfo[keyUsersEntry{uid, names[i]}] = float64(v)
|
||||
}
|
||||
}
|
||||
return keyUsersInfo, scanner.Err()
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
// Copyright 2023 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build linux && !nokey_users
|
||||
// +build linux,!nokey_users
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestKeyUsers(t *testing.T) {
|
||||
file, err := os.Open("fixtures/proc/key-users")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
keyUsers, err := parseKeyUsers(file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if want, got := 51.0, keyUsers[keyUsersEntry{0, "usage"}]; want != got {
|
||||
t.Errorf("want uid 0 usage %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 50.0, keyUsers[keyUsersEntry{0, "nkeys"}]; want != got {
|
||||
t.Errorf("want uid 0 nkeys %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 50.0, keyUsers[keyUsersEntry{0, "nikeys"}]; want != got {
|
||||
t.Errorf("want uid 0 nikeys %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 44.0, keyUsers[keyUsersEntry{0, "qnkeys"}]; want != got {
|
||||
t.Errorf("want uid 0 qnkeys %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 1000000.0, keyUsers[keyUsersEntry{0, "maxkeys"}]; want != got {
|
||||
t.Errorf("want uid 0 maxkeys %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 883.0, keyUsers[keyUsersEntry{0, "qnbytes"}]; want != got {
|
||||
t.Errorf("want uid 0 qnbytes %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 25000000.0, keyUsers[keyUsersEntry{0, "maxbytes"}]; want != got {
|
||||
t.Errorf("want uid 0 maxbytes %f, got %f", want, got)
|
||||
}
|
||||
|
||||
if want, got := 11.0, keyUsers[keyUsersEntry{1000, "usage"}]; want != got {
|
||||
t.Errorf("want uid 0 usage %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 11.0, keyUsers[keyUsersEntry{1000, "nkeys"}]; want != got {
|
||||
t.Errorf("want uid 0 nkeys %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 12.0, keyUsers[keyUsersEntry{1000, "nikeys"}]; want != got {
|
||||
t.Errorf("want uid 0 nikeys %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 13.0, keyUsers[keyUsersEntry{1000, "qnkeys"}]; want != got {
|
||||
t.Errorf("want uid 0 qnkeys %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 200.0, keyUsers[keyUsersEntry{1000, "maxkeys"}]; want != got {
|
||||
t.Errorf("want uid 0 maxkeys %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 250.0, keyUsers[keyUsersEntry{1000, "qnbytes"}]; want != got {
|
||||
t.Errorf("want uid 0 qnbytes %f, got %f", want, got)
|
||||
}
|
||||
if want, got := 20000.0, keyUsers[keyUsersEntry{1000, "maxbytes"}]; want != got {
|
||||
t.Errorf("want uid 0 maxbytes %f, got %f", want, got)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue