-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e3e8752
Showing
12 changed files
with
777 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
name: release | ||
concurrency: | ||
group: release | ||
cancel-in-progress: true | ||
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
jobs: | ||
build_and_release: | ||
name: build_and_release | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: clone | ||
uses: actions/checkout@main | ||
- name: Setup Go | ||
uses: actions/setup-go@main | ||
with: | ||
go-version: 1.22 | ||
- name: install depends | ||
run: | | ||
go mod download | ||
mkdir artifacts | ||
- name: build | ||
run: | | ||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o artifacts/custom-exporter | ||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o artifacts/custom-exporter-arm64 | ||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="-s -w" -o artifacts/custom-exporter-arm7 | ||
- name: upload artifacts to cache | ||
id: cache-artifacts | ||
uses: actions/cache@main | ||
with: | ||
path: ./artifacts | ||
key: artifacts | ||
|
||
container_image: | ||
name: container_image | ||
runs-on: ubuntu-22.04 | ||
outputs: | ||
image: ${{ steps.set-image-name.outputs.image }} | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@main | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v1 | ||
- name: image name | ||
id: set-image-name | ||
run: | | ||
name="$(echo ${{ github.repository }} | tr 'A-Z' 'a-z')" | ||
echo "image=ghcr.io/$name:${{ github.ref_name }}" >> $GITHUB_OUTPUT | ||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.repository_owner }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Build and push Docker image | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: . | ||
push: true | ||
platforms: linux/amd64,linux/arm64,linux/arm/v7 | ||
tags: ${{ steps.set-image-name.outputs.image }} | ||
|
||
release: | ||
name: release | ||
runs-on: ubuntu-22.04 | ||
needs: [build_and_release, container_image] | ||
steps: | ||
- name: download cache-artifacts | ||
uses: actions/cache/restore@main | ||
with: | ||
path: ./artifacts | ||
key: artifacts | ||
|
||
- name: ${{ github.ref_name }} | ||
uses: softprops/action-gh-release@v2 | ||
with: | ||
body: | | ||
container image: | ||
``` | ||
${{ needs.container_image.outputs.image }} | ||
``` | ||
files: | | ||
./artifacts/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
FROM --platform=$BUILDPLATFORM golang:1.22 AS build | ||
|
||
WORKDIR /app | ||
|
||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
|
||
COPY ./main.go ./ | ||
COPY ./pkg ./pkg | ||
|
||
ARG TARGETPLATFORM | ||
RUN echo "Building BUILDPLATFORM: '${BUILDPLATFORM}', TARGETPLATFORM: '$TARGETPLATFORM'"; \ | ||
export GOOS=$(echo $TARGETPLATFORM | cut -d / -f1); \ | ||
export GOARCH=$(echo $TARGETPLATFORM | cut -d / -f2); \ | ||
if [ "$GOARCH" = "arm" ]; then \ | ||
export GOARCH="arm"; \ | ||
export GOARM=$(echo $TARGETPLATFORM | cut -d / -f3 | sed 's/v//g'); \ | ||
fi; \ | ||
export CGO_ENABLED=0; \ | ||
echo "Building for $GOOS/$GOARCH/$GOARM"; \ | ||
go build -ldflags="-s -w" -o ./custom-exporter . | ||
|
||
|
||
FROM scratch | ||
COPY --from=build /app/custom-exporter /custom-exporter | ||
ENTRYPOINT ["/custom-exporter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# custom-exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module github.com/orangeAppsRu/custom-exporter | ||
|
||
go 1.22 | ||
|
||
require ( | ||
github.com/go-yaml/yaml v2.1.0+incompatible | ||
github.com/prometheus/client_golang v1.19.1 | ||
github.com/prometheus/procfs v0.12.0 | ||
) | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/kr/text v0.1.0 // indirect | ||
github.com/prometheus/client_model v0.5.0 // indirect | ||
github.com/prometheus/common v0.48.0 // indirect | ||
golang.org/x/sys v0.17.0 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= | ||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= | ||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= | ||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= | ||
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= | ||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= | ||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= | ||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE= | ||
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho= | ||
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= | ||
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= | ||
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= | ||
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= | ||
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= | ||
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= | ||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= | ||
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= | ||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= | ||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= | ||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= | ||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"time" | ||
"net/http" | ||
// "encoding/json" | ||
|
||
"github.com/orangeAppsRu/custom-exporter/pkg/filehash" | ||
"github.com/orangeAppsRu/custom-exporter/pkg/config" | ||
"github.com/orangeAppsRu/custom-exporter/pkg/metrics" | ||
"github.com/orangeAppsRu/custom-exporter/pkg/network" | ||
"github.com/orangeAppsRu/custom-exporter/pkg/proc" | ||
|
||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
|
||
) | ||
|
||
func main() { | ||
configFilePath := flag.String("config", "", "path to config file") | ||
flag.Parse() | ||
|
||
if *configFilePath == "" { | ||
*configFilePath = os.Getenv("CONFIG") | ||
} | ||
|
||
if *configFilePath == "" { | ||
fmt.Println("Config file path is not provided. Use --config flag or set CONFIG environment variable.") | ||
return | ||
} | ||
|
||
cfg, err := config.ReadConfig(*configFilePath) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
|
||
host := os.Getenv("HOST") | ||
if host == "" { | ||
host = "127.0.0.1" | ||
} | ||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8200" | ||
} | ||
listenAddr := fmt.Sprintf("%s:%s", host, port) | ||
|
||
metrics.RegistrMetrics() | ||
|
||
if cfg.FileHashCollector.Enabled { | ||
go func() { | ||
for { | ||
|
||
filesWithHash := []filehash.FileHash{} | ||
for _, filePath := range cfg.FileHashCollector.Files { | ||
if _, err := os.Stat(filePath); err == nil { | ||
number, err := filehash.Calculate(filePath) | ||
if err != nil { | ||
fmt.Printf("Error calculating hash for %s: %v\n", filePath, err) | ||
continue | ||
} | ||
|
||
filesWithHash = append(filesWithHash, filehash.FileHash{ | ||
File: filePath, | ||
Hash: number, | ||
}) | ||
} | ||
} | ||
metrics.UpdateFileHashMetrics(filesWithHash) | ||
time.Sleep(60 * time.Second) | ||
} | ||
}() | ||
} | ||
|
||
if cfg.PortCollector.Enabled { | ||
go func() { | ||
for { | ||
rTargets := network.CheckTargets(cfg.PortCollector.Targets) | ||
metrics.UpdateNetworkTargetsMetrics(rTargets) | ||
time.Sleep(60 * time.Second) | ||
} | ||
}() | ||
} | ||
|
||
if cfg.ProcessCollector.Enabled { | ||
go func() { | ||
for { | ||
if np, err := proc.CountProcesses(); err != nil { | ||
fmt.Printf("Error counting processes: %v\n", err) | ||
} else { | ||
metrics.UpdateProcessCountMetrics("all", np) | ||
} | ||
|
||
if npt, err := proc.CountProcessTypes(); err != nil { | ||
fmt.Printf("Error counting processes: %v\n", err) | ||
} else { | ||
for typeProcess, count := range npt { | ||
metrics.UpdateProcessCountMetrics(typeProcess, count) | ||
} | ||
} | ||
|
||
if processResources, err := proc.AggregateCPUTimeAndMemoryUsageByRegex(cfg.ProcessCollector.Processes); err != nil { | ||
fmt.Printf("Error aggregating process resources: %v\n", err) | ||
} else { | ||
for process, usage := range processResources { | ||
metrics.UpdateProcessCPUTimeMetrics(process, usage.CPUTime) | ||
} | ||
} | ||
|
||
if processRunningStatus, err := proc.FindProcessesByRegex(cfg.ProcessCollector.Processes); err != nil { | ||
fmt.Printf("Error finding processes: %v\n", err) | ||
} else { | ||
for process, count := range processRunningStatus { | ||
metrics.UpdateProcessRunningStatusMetrics(process, count) | ||
} | ||
} | ||
|
||
time.Sleep(60 * time.Second) | ||
} | ||
}() | ||
} | ||
|
||
|
||
http.Handle("/metrics", promhttp.Handler()) | ||
http.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
fmt.Println("Prometheus metrics server started at", listenAddr) | ||
if err := http.ListenAndServe(listenAddr, nil); err != nil { | ||
fmt.Printf("Error starting server: %s\n", err) | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/go-yaml/yaml" | ||
"github.com/orangeAppsRu/custom-exporter/pkg/network" | ||
"github.com/orangeAppsRu/custom-exporter/pkg/proc" | ||
) | ||
|
||
type Config struct { | ||
FileHashCollector struct { | ||
Enabled bool `yaml:"enabled"` | ||
Files []string `yaml:"files"` | ||
} `yaml:"fileHashCollector"` | ||
|
||
PortCollector struct { | ||
Enabled bool `yaml:"enabled"` | ||
Targets []network.Target `yaml:"targets"` | ||
} `yaml:"portCollector"` | ||
|
||
ProcessCollector struct { | ||
Enabled bool `yaml:"enabled"` | ||
Processes []proc.ProcessFilter `yaml:"processes"` | ||
} `yaml:"processCollector"` | ||
} | ||
|
||
|
||
func ReadConfig(filePath string) (Config, error) { | ||
configFile, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return Config{}, fmt.Errorf("error reading config file: %v", err) | ||
} | ||
|
||
var config Config | ||
if err := yaml.Unmarshal(configFile, &config); err != nil { | ||
return Config{}, fmt.Errorf("error parsing config file: %v", err) | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package filehash | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
type FileHash struct { | ||
File string | ||
Hash float64 | ||
} | ||
|
||
func Calculate(filePath string) (float64, error) { | ||
fileContent, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return 0, fmt.Errorf("error reading file: %v", err) | ||
} | ||
|
||
hash := sha256.Sum256(fileContent) | ||
|
||
hashString := hex.EncodeToString(hash[:]) | ||
|
||
lastTenChars := hashString[len(hashString)-10:] | ||
|
||
number, err := strconv.ParseUint(lastTenChars, 16, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("error converting to number: %v", err) | ||
} | ||
|
||
return float64(number), nil | ||
} |
Oops, something went wrong.