-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleted_mapping_exporter.go
152 lines (136 loc) · 3.79 KB
/
deleted_mapping_exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2017 Kumina, https://kumina.nl/
// 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.
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"regexp"
"strconv"
"github.com/prometheus/client_golang/prometheus"
)
var (
deletedMappingsUpDesc = prometheus.NewDesc(
prometheus.BuildFQName("deleted_mappings_exporter", "", "up"),
"Whether scraping the metrics was successful.",
nil,
nil)
)
func searchMaps(reader io.Reader, collection *map[string]int) error {
scanner := bufio.NewScanner(reader)
re := regexp.MustCompile(" [-r][-w]xp .* (/.*) \\(deleted\\)$")
for scanner.Scan() {
fields := re.FindStringSubmatch(scanner.Text())
if fields != nil {
_, ok := (*collection)[fields[1]]
if ok {
(*collection)[fields[1]] += 1
} else {
(*collection)[fields[1]] = 1
}
}
}
return scanner.Err()
}
func CollectDeletedMappings(procPath string, ch chan<- prometheus.Metric) error {
collection := map[string]int{}
files, err := ioutil.ReadDir(procPath)
if err != nil {
panic(err)
}
var scrapeError error = nil
for _, file := range files {
_, err := strconv.Atoi(file.Name())
if err == nil {
read, err := os.Open(path.Join("/proc", file.Name(), "maps"))
if err != nil {
scrapeError = err
} else {
err = searchMaps(read, &collection)
if err != nil {
scrapeError = err
}
}
}
}
deletedMappings := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "deleted_mappings_exporter",
Name: "mapping",
Help: "Number of processes using this mapping.",
},
[]string{"mapping_name"},
)
for key, value := range collection {
fmt.Println("Key:", key, "Value:", value)
deletedMappings.WithLabelValues(key).Set(float64(value))
}
deletedMappings.Collect(ch)
return scrapeError
}
type DeletedMappingExporter struct {
procPath string
}
func NewDeletedMappingExporter(procPath string) (*DeletedMappingExporter, error) {
return &DeletedMappingExporter{
procPath: procPath,
}, nil
}
func (e *DeletedMappingExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- deletedMappingsUpDesc
}
func (e *DeletedMappingExporter) Collect(ch chan<- prometheus.Metric) {
err := CollectDeletedMappings(e.procPath, ch)
if err == nil {
ch <- prometheus.MustNewConstMetric(
deletedMappingsUpDesc,
prometheus.GaugeValue,
1.0)
} else {
ch <- prometheus.MustNewConstMetric(
deletedMappingsUpDesc,
prometheus.GaugeValue,
0.0)
}
}
func main() {
var (
listenAddress = flag.String("deletedmapping.listen-address", ":9040", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("deletedmapping.telemetry-path", "/metrics", "Path under which to expose metrics.")
procPath = flag.String("deletedmapping.proc-path", "/proc", "Path where proc is mounted.")
)
flag.Parse()
exporter, err := NewDeletedMappingExporter(*procPath)
if err != nil {
panic(err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Deleted Mappings Exporter</title></head>
<body>
<h1>Deleted Mappings Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}