Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Islamgaliev committed Oct 5, 2017
2 parents 0a4abfd + 55346ba commit 8cbd306
Show file tree
Hide file tree
Showing 413 changed files with 184,508 additions and 47 deletions.
13 changes: 13 additions & 0 deletions .promu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
repository:
path: github.com/Lusitaniae/apache_exporter
build:
flags: -a -tags netgo
ldflags: |
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Version={{.Version}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Revision={{.Revision}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Branch={{.Branch}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.BuildUser={{user}}@{{host}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}}
tarball:
files:
- LICENSE
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM golang:alpine
FROM quay.io/prometheus/busybox:latest

RUN apk add --no-cache ca-certificates git
RUN go get github.com/neezgee/apache_exporter
COPY apache_exporter /bin/apache_exporter

EXPOSE 9117
ENTRYPOINT ["/go/bin/apache_exporter"]
ENTRYPOINT ["/bin/apache_exporter"]
EXPOSE 9117
58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2015 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 := GO15VENDOREXPERIMENT=1 go
PROMU := $(GOPATH)/bin/promu
pkgs = $(shell $(GO) list ./... | grep -v /vendor/)

PREFIX ?= $(shell pwd)
BIN_DIR ?= $(shell pwd)
DOCKER_IMAGE_NAME ?= apache-exporter
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))

all: format build test

test:
@echo ">> running tests"
@$(GO) test -short $(pkgs)

style:
@echo ">> checking code style"
@! gofmt -d $(shell find . -path ./vendor -prune -o -name '*.go' -print) | grep '^'

format:
@echo ">> formatting code"
@$(GO) fmt $(pkgs)

vet:
@echo ">> vetting code"
@$(GO) vet $(pkgs)

build: promu
@echo ">> building binaries"
@$(PROMU) build --prefix $(PREFIX)

tarball: promu
@echo ">> building release tarball"
@$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)

docker:
@echo ">> building docker image"
@docker build -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .

promu:
@GOOS=$(shell uname -s | tr A-Z a-z) \
GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) \
$(GO) get -u github.com/prometheus/promu

.PHONY: all style format build test vet tarball docker promu
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ Help on flags:

```
-insecure
Ignore server certificate if using https (default false)
Ignore server certificate if using https. (default false)
-log.level value
Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal, panic]. (default info)
-scrape_uri string
URI to apache stub status page (default "http://localhost/server-status/?auto")
URI to apache stub status page. (default "http://localhost/server-status/?auto")
-telemetry.address string
Address on which to expose metrics. (default ":9117")
-telemetry.endpoint string
Path under which to expose metrics. (default "/metrics")
-version
Version of the Apache exporter.
```

Tested on Apache 2.2 and Apache 2.4.
Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.4.0
83 changes: 47 additions & 36 deletions apache_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)

const (
Expand All @@ -23,6 +25,7 @@ var (
metricsEndpoint = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics.")
scrapeURI = flag.String("scrape_uri", "http://localhost/server-status/?auto", "URI to apache stub status page.")
insecure = flag.Bool("insecure", false, "Ignore server certificate if using https.")
showVersion = flag.Bool("version", false, "Print version information.")
)

type Exporter struct {
Expand All @@ -44,30 +47,30 @@ func NewExporter(uri string) *Exporter {
return &Exporter{
URI: uri,
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Could the apache server be reached",
nil,
prometheus.BuildFQName(namespace, "", "up"),
"Could the apache server be reached",
nil,
nil),
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrape_failures_total",
Help: "Number of errors while scraping apache.",
}),
accessesTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "accesses_total"),
"Current total apache accesses",
nil,
nil),
kBytesTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "sent_kilobytes_total"),
"Current total kbytes sent",
nil,
nil),
uptime: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "uptime_seconds_total"),
"Current uptime in seconds",
nil,
nil),
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrape_failures_total",
Help: "Number of errors while scraping apache.",
}),
accessesTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "accesses_total"),
"Current total apache accesses",
nil,
nil),
kBytesTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "sent_kilobytes_total"),
"Current total kbytes sent",
nil,
nil),
uptime: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "uptime_seconds_total"),
"Current uptime in seconds",
nil,
nil),
workers: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "workers",
Expand Down Expand Up @@ -98,14 +101,14 @@ func NewExporter(uri string) *Exporter {
}

func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.up
ch <- e.accessesTotal
ch <- e.kBytesTotal
ch <- e.uptime
e.scrapeFailures.Describe(ch)
e.workers.Describe(ch)
e.scoreboard.Describe(ch)
e.connections.Describe(ch)
ch <- e.up
ch <- e.accessesTotal
ch <- e.kBytesTotal
ch <- e.uptime
e.scrapeFailures.Describe(ch)
e.workers.Describe(ch)
e.scoreboard.Describe(ch)
e.connections.Describe(ch)
}

// Split colon separated string into two fields
Expand Down Expand Up @@ -158,10 +161,10 @@ func (e *Exporter) updateScoreboard(scoreboard string) {
func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
resp, err := e.client.Get(e.URI)
if err != nil {
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 0)
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 0)
return fmt.Errorf("Error scraping apache: %v", err)
}
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 1)
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 1)

data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
Expand Down Expand Up @@ -189,21 +192,21 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
return err
}

ch <- prometheus.MustNewConstMetric(e.accessesTotal, prometheus.CounterValue, val)
ch <- prometheus.MustNewConstMetric(e.accessesTotal, prometheus.CounterValue, val)
case key == "Total kBytes":
val, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}

ch <- prometheus.MustNewConstMetric(e.kBytesTotal, prometheus.CounterValue, val)
ch <- prometheus.MustNewConstMetric(e.kBytesTotal, prometheus.CounterValue, val)
case key == "Uptime":
val, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}

ch <- prometheus.MustNewConstMetric(e.uptime, prometheus.CounterValue, val)
ch <- prometheus.MustNewConstMetric(e.uptime, prometheus.CounterValue, val)
case key == "BusyWorkers":
val, err := strconv.ParseFloat(v, 64)
if err != nil {
Expand Down Expand Up @@ -277,8 +280,16 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
func main() {
flag.Parse()

if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("apache_exporter"))
os.Exit(0)
}
exporter := NewExporter(*scrapeURI)
prometheus.MustRegister(exporter)
prometheus.MustRegister(version.NewCollector("apache_exporter"))

log.Infoln("Starting apache_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())

log.Infof("Starting Server: %s", *listeningAddress)
http.Handle(*metricsEndpoint, prometheus.Handler())
Expand Down
7 changes: 3 additions & 4 deletions apache_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,9 @@ IdleWorkers: 8
Scoreboard: _W_______K......................................................................................................................................................................................................................................................
`

metricCountApache22 = 10
metricCountApache24 = 12
metricCountApache24Worker = 10

metricCountApache22 = 10
metricCountApache24 = 12
metricCountApache24Worker = 10
)

func checkApacheStatus(t *testing.T, status string, metricCount int) {
Expand Down
62 changes: 62 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
machine:
environment:
DOCKER_IMAGE_NAME: lusotycoon/apache-exporter
QUAY_IMAGE_NAME: quay.io/lusitaniae/apache-exporter
DOCKER_TEST_IMAGE_NAME: quay.io/prometheus/golang-builder:1.8-base
REPO_PATH: github.com/Lusitaniae/apache_exporter
pre:
- sudo curl -L -o /usr/bin/docker 'https://s3-external-1.amazonaws.com/circle-downloads/docker-1.9.1-circleci'
- sudo chmod 0755 /usr/bin/docker
- sudo curl -L 'https://github.com/aktau/github-release/releases/download/v0.6.2/linux-amd64-github-release.tar.bz2' | tar xvjf - --strip-components 3 -C $HOME/bin
services:
- docker

dependencies:
pre:
- make promu
- docker info
override:
- promu crossbuild
- ln -s .build/linux-amd64/apache_exporter apache_exporter
- |
if [ -n "$CIRCLE_TAG" ]; then
make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME DOCKER_IMAGE_TAG=$CIRCLE_TAG
make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME DOCKER_IMAGE_TAG=$CIRCLE_TAG
else
make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME
make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME
fi
post:
- mkdir $CIRCLE_ARTIFACTS/binaries/ && cp -a .build/* $CIRCLE_ARTIFACTS/binaries/
- docker images

test:
override:
- docker run --rm -t -v "$(pwd):/app" "${DOCKER_TEST_IMAGE_NAME}" -i "${REPO_PATH}" -T

deployment:
hub_branch:
branch: master
owner: Lusitaniae
commands:
- docker login -e $DOCKER_EMAIL -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
- docker login -e $QUAY_EMAIL -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io
- docker push $DOCKER_IMAGE_NAME
- docker push $QUAY_IMAGE_NAME
hub_tag:
tag: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
owner: Lusitaniae
commands:
- promu crossbuild tarballs
- promu checksum .tarballs
- promu release .tarballs
- mkdir $CIRCLE_ARTIFACTS/releases/ && cp -a .tarballs/* $CIRCLE_ARTIFACTS/releases/
- docker login -e $DOCKER_EMAIL -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
- docker login -e $QUAY_EMAIL -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io
- |
if [[ "$CIRCLE_TAG" =~ ^v[0-9]+(\.[0-9]+){2}$ ]]; then
docker tag "$DOCKER_IMAGE_NAME:$CIRCLE_TAG" "$DOCKER_IMAGE_NAME:latest"
docker tag "$QUAY_IMAGE_NAME:$CIRCLE_TAG" "$QUAY_IMAGE_NAME:latest"
fi
- docker push $DOCKER_IMAGE_NAME
- docker push $QUAY_IMAGE_NAME
27 changes: 27 additions & 0 deletions vendor/github.com/alecthomas/template/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/alecthomas/template/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8cbd306

Please sign in to comment.