2020-02-12 16:07:45 +00:00
// Copyright 2020 Trey Dockendorf
// 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 (
"fmt"
"net/http"
"os"
"strings"
2023-05-06 12:35:46 +00:00
"github.com/alecthomas/kingpin/v2"
2020-02-12 16:07:45 +00:00
"github.com/containerd/cgroups"
2022-03-08 16:56:57 +00:00
"github.com/go-kit/log"
"github.com/go-kit/log/level"
2020-02-12 16:07:45 +00:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
2020-10-02 19:21:18 +00:00
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
2020-02-12 16:07:45 +00:00
"github.com/prometheus/common/version"
2024-01-24 17:54:50 +00:00
"github.com/treydock/cgroup_exporter/collector"
2020-02-12 16:07:45 +00:00
)
var (
2020-02-12 17:58:18 +00:00
configPaths = kingpin . Flag ( "config.paths" , "Comma separated list of cgroup paths to check, eg /user.slice,/system.slice,/slurm" ) . Required ( ) . String ( )
2020-02-13 16:59:25 +00:00
listenAddress = kingpin . Flag ( "web.listen-address" , "Address to listen on for web interface and telemetry." ) . Default ( ":9306" ) . String ( )
2020-02-12 16:07:45 +00:00
disableExporterMetrics = kingpin . Flag ( "web.disable-exporter-metrics" , "Exclude metrics about the exporter (promhttp_*, process_*, go_*)" ) . Default ( "false" ) . Bool ( )
)
2020-10-02 19:21:18 +00:00
func metricsHandler ( logger log . Logger ) http . HandlerFunc {
2020-02-12 16:07:45 +00:00
return func ( w http . ResponseWriter , r * http . Request ) {
registry := prometheus . NewRegistry ( )
paths := strings . Split ( * configPaths , "," )
2024-01-24 17:54:50 +00:00
var cgroupV2 bool
if cgroups . Mode ( ) == cgroups . Unified {
cgroupV2 = true
}
cgroupCollector := collector . NewCgroupCollector ( cgroupV2 , paths , logger )
registry . MustRegister ( cgroupCollector )
registry . MustRegister ( version . NewCollector ( fmt . Sprintf ( "%s_exporter" , collector . Namespace ) ) )
2020-02-12 16:07:45 +00:00
gatherers := prometheus . Gatherers { registry }
if ! * disableExporterMetrics {
gatherers = append ( gatherers , prometheus . DefaultGatherer )
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp . HandlerFor ( gatherers , promhttp . HandlerOpts { } )
h . ServeHTTP ( w , r )
}
}
func main ( ) {
metricsEndpoint := "/metrics"
2020-10-02 19:21:18 +00:00
promlogConfig := & promlog . Config { }
flag . AddFlags ( kingpin . CommandLine , promlogConfig )
2020-02-12 16:07:45 +00:00
kingpin . Version ( version . Print ( "cgroup_exporter" ) )
kingpin . HelpFlag . Short ( 'h' )
kingpin . Parse ( )
2020-10-02 19:21:18 +00:00
logger := promlog . New ( promlogConfig )
level . Info ( logger ) . Log ( "msg" , "Starting cgroup_exporter" , "version" , version . Info ( ) )
level . Info ( logger ) . Log ( "msg" , "Build context" , "build_context" , version . BuildContext ( ) )
level . Info ( logger ) . Log ( "msg" , "Starting Server" , "address" , * listenAddress )
2020-02-12 16:07:45 +00:00
http . HandleFunc ( "/" , func ( w http . ResponseWriter , r * http . Request ) {
//nolint:errcheck
w . Write ( [ ] byte ( ` < html >
< head > < title > cgroup Exporter < / title > < / head >
< body >
< h1 > cgroup Exporter < / h1 >
< p > < a href = ' ` + metricsEndpoint + ` ' > Metrics < / a > < / p >
< / body >
< / html > ` ) )
} )
2020-10-02 19:21:18 +00:00
http . Handle ( metricsEndpoint , metricsHandler ( logger ) )
err := http . ListenAndServe ( * listenAddress , nil )
if err != nil {
level . Error ( logger ) . Log ( "err" , err )
os . Exit ( 1 )
}
2020-02-12 16:07:45 +00:00
}