/
exporter.go
216 lines (197 loc) · 5.53 KB
/
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace = "MONITORING"
subsystem = "FINOPS"
)
var (
isDebug bool
apiURL string
queueName string
port int
endpoint string
registry *prometheus.Registry
s3LastModifiedObjectDate = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "last_modified_object_date",
Help: "The last modified date of the object that was modified most recently",
},
[]string{
"bucket",
},
)
s3LastModifiedObjectSize = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "last_modified_object_size_bytes",
Help: "The size of the object that was modified most recently",
},
[]string{
"bucket",
},
)
s3ObjectTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "objects_total",
Help: "The total number of objects for the bucket/prefix combination",
},
[]string{
"bucket",
},
)
s3SumSize = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "objects_size_sum_bytes",
Help: "The total size of all objects summed",
},
[]string{
"bucket",
},
)
s3BiggestSize = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "biggest_object_size_bytes",
Help: "The size of the biggest object",
},
[]string{
"bucket",
},
)
)
type processingTypes []string
func createS3Client() (*s3.S3, error) {
endpoint := os.Getenv("S3_ENDPOINT")
if endpoint == "" {
return nil, fmt.Errorf("S3_ENDPOINT is not set")
}
region := os.Getenv("S3_REGION")
if region == "" {
return nil, fmt.Errorf("S3_REGION is not set")
}
accessKey := os.Getenv("S3_ACCESS_KEY")
if accessKey == "" {
return nil, fmt.Errorf("S3_ACCESS_KEY is not set")
}
secretKey := os.Getenv("S3_SECRET_KEY")
if secretKey == "" {
return nil, fmt.Errorf("S3_SECRET_KEY is not set")
}
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
Endpoint: aws.String(endpoint),
Region: aws.String(region),
}
s3Session, err := session.NewSession(s3Config)
if err != nil {
return nil, fmt.Errorf("Failed to create a new session - %s", err.Error())
}
return s3.New(s3Session), nil
}
func watchObjectStorage() {
s3Client, err := createS3Client()
if err != nil {
DisplayLog("FATAL", fmt.Sprintf("Failed to create s3Client - %s", err.Error()))
os.Exit(1)
}
DisplayLog("INFO", fmt.Sprintf("object-storage-exporter connected to S3 endpoint %s", endpoint))
for {
buckets, err := s3Client.ListBuckets(nil)
if err != nil {
DisplayLog("FATAL", fmt.Sprintf("Failed to list buckets - %s", err.Error()))
os.Exit(2)
}
for _, bucket := range buckets.Buckets {
var lastModified time.Time
var numberOfObjects float64
var totalSize int64
var biggestObjectSize int64
var lastObjectSize int64
s3Client.ListObjectsPages(&s3.ListObjectsInput{
Bucket: bucket.Name,
}, func(p *s3.ListObjectsOutput, last bool) (shouldContinue bool) {
for _, object := range p.Contents {
numberOfObjects++
totalSize = totalSize + *object.Size
if object.LastModified.After(lastModified) {
lastModified = *object.LastModified
lastObjectSize = *object.Size
}
if *object.Size > biggestObjectSize {
biggestObjectSize = *object.Size
}
}
return true
})
s3LastModifiedObjectDate.WithLabelValues(
*bucket.Name,
).Set(float64(lastModified.UnixNano() / 1e9))
s3LastModifiedObjectSize.WithLabelValues(
*bucket.Name,
).Set(float64(lastObjectSize))
s3ObjectTotal.WithLabelValues(
*bucket.Name,
).Set(numberOfObjects)
s3BiggestSize.WithLabelValues(
*bucket.Name,
).Set(float64(biggestObjectSize))
s3SumSize.WithLabelValues(
*bucket.Name,
).Set(float64(totalSize))
}
time.Sleep(5 * time.Second)
}
}
func init() {
const (
isDebugDefault = false
isDebugUsage = "Run in debug mode"
portDefault = 2112
portUsage = "Port to export on"
endpointDefault = "/metrics"
endpointUsage = "Endpoint to export on"
)
flag.BoolVar(&isDebug, "debug", isDebugDefault, isDebugUsage)
flag.BoolVar(&isDebug, "d", isDebugDefault, isDebugUsage+" (shorthand)")
flag.IntVar(&port, "port", portDefault, portUsage)
flag.IntVar(&port, "p", portDefault, portUsage+" (shorthand)")
flag.StringVar(&endpoint, "endpoint", endpointDefault, endpointUsage)
flag.StringVar(&endpoint, "e", endpointDefault, endpointUsage+" (shorthand)")
if boolValue, err := strconv.ParseBool(os.Getenv("API_EXPORTER_DEBUG")); err == nil {
isDebug = boolValue
}
prometheus.MustRegister(s3BiggestSize)
prometheus.MustRegister(s3LastModifiedObjectDate)
prometheus.MustRegister(s3LastModifiedObjectSize)
prometheus.MustRegister(s3ObjectTotal)
prometheus.MustRegister(s3SumSize)
}
func main() {
flag.Parse()
go watchObjectStorage()
DisplayLog("INFO", fmt.Sprintf("object-storage-exporter started to listen on http://x.x.x.x:%d%s", port, endpoint))
http.Handle(endpoint, promhttp.Handler())
http.ListenAndServe(":"+strconv.Itoa(port), nil)
DisplayLog("INFO", "Object-storage-exporter stopped")
}