|
| 1 | +// Copyright 2023 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 | +//go:build linux && !noselinux |
| 15 | +// +build linux,!noselinux |
| 16 | + |
| 17 | +package selinuxfs |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | +) |
| 26 | + |
| 27 | +// SELinux access vector cache statistics. |
| 28 | +type AVCStat struct { |
| 29 | + // Number of total lookups |
| 30 | + Lookups uint64 |
| 31 | + // Number of total hits |
| 32 | + Hits uint64 |
| 33 | + // Number of total misses |
| 34 | + Misses uint64 |
| 35 | + // Number of total allocations |
| 36 | + Allocations uint64 |
| 37 | + // Number of total reclaims |
| 38 | + Reclaims uint64 |
| 39 | + // Number of total frees |
| 40 | + Frees uint64 |
| 41 | +} |
| 42 | + |
| 43 | +// ParseAVCStats returns the total SELinux access vector cache statistics, |
| 44 | +// or error on failure. |
| 45 | +func (fs FS) ParseAVCStats() (AVCStat, error) { |
| 46 | + avcStat := AVCStat{} |
| 47 | + |
| 48 | + file, err := os.Open(fs.selinux.Path("avc/cache_stats")) |
| 49 | + if err != nil { |
| 50 | + return avcStat, err |
| 51 | + } |
| 52 | + defer file.Close() |
| 53 | + |
| 54 | + scanner := bufio.NewScanner(file) |
| 55 | + scanner.Scan() // Skip header |
| 56 | + |
| 57 | + for scanner.Scan() { |
| 58 | + avcValues := strings.Fields(scanner.Text()) |
| 59 | + |
| 60 | + if len(avcValues) != 6 { |
| 61 | + return avcStat, fmt.Errorf("invalid AVC stat line: %s", |
| 62 | + scanner.Text()) |
| 63 | + } |
| 64 | + |
| 65 | + lookups, err := strconv.ParseUint(avcValues[0], 0, 64) |
| 66 | + if err != nil { |
| 67 | + return avcStat, fmt.Errorf("could not parse expected integer value for lookups") |
| 68 | + } |
| 69 | + |
| 70 | + hits, err := strconv.ParseUint(avcValues[1], 0, 64) |
| 71 | + if err != nil { |
| 72 | + return avcStat, fmt.Errorf("could not parse expected integer value for hits") |
| 73 | + } |
| 74 | + |
| 75 | + misses, err := strconv.ParseUint(avcValues[2], 0, 64) |
| 76 | + if err != nil { |
| 77 | + return avcStat, fmt.Errorf("could not parse expected integer value for misses") |
| 78 | + } |
| 79 | + |
| 80 | + allocations, err := strconv.ParseUint(avcValues[3], 0, 64) |
| 81 | + if err != nil { |
| 82 | + return avcStat, fmt.Errorf("could not parse expected integer value for allocations") |
| 83 | + } |
| 84 | + |
| 85 | + reclaims, err := strconv.ParseUint(avcValues[4], 0, 64) |
| 86 | + if err != nil { |
| 87 | + return avcStat, fmt.Errorf("could not parse expected integer value for reclaims") |
| 88 | + } |
| 89 | + |
| 90 | + frees, err := strconv.ParseUint(avcValues[5], 0, 64) |
| 91 | + if err != nil { |
| 92 | + return avcStat, fmt.Errorf("could not parse expected integer value for frees") |
| 93 | + } |
| 94 | + |
| 95 | + avcStat.Lookups += lookups |
| 96 | + avcStat.Hits += hits |
| 97 | + avcStat.Misses += misses |
| 98 | + avcStat.Allocations += allocations |
| 99 | + avcStat.Reclaims += reclaims |
| 100 | + avcStat.Frees += frees |
| 101 | + } |
| 102 | + |
| 103 | + return avcStat, scanner.Err() |
| 104 | +} |
0 commit comments