Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
uses: goreleaser/goreleaser-action@f06c13b6b1a9625abc9e6e439d9c05a8f2190e94 # v7.2.3
with:
version: latest
args: build --snapshot
args: build --snapshot
4 changes: 1 addition & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ builds:
goarch:
- amd64
- arm64
ldflags:
- -w -s -X main.version={{.Version}} -X github.com/kazeburo/mackerel-plugin-maxcpu/maxcpu.version={{.Version}}
archives:
- format: zip
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
release:
github:
owner: kazeburo
owner: monitoring-forge
name: mackerel-plugin-maxcpu
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
VERSION=0.0.15
LDFLAGS=-ldflags "-w -s -X main.version=${VERSION} -X github.com/kazeburo/mackerel-plugin-maxcpu/maxcpu.version=${VERSION}"
GITCOMMIT?=$(shell git describe --dirty --always)
LDFLAGS=-ldflags "-w -s -X main.version=${VERSION} -X main.commit=${GITCOMMIT}"

all: mackerel-plugin-maxcpu

Expand Down Expand Up @@ -29,6 +30,7 @@ linux-check: mackerel-plugin-maxcpu
pkill -f $$tmpfile'

check:
go test ./...
go test -v ./...
go test -race ./...
go vet ./...

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ At the first time of execution, mackerel-plugin-maxcpu spawns the calculating da
Sample

```
$ ./mackerel-plugin-maxcpu --socket /tmp/maxcpu.sock
2020/10/30 10:40:54 rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial unix /tmp/maxcpu.sock: connect: no such file or directory"
$ sudo ./mackerel-plugin-maxcpu --socket /var/run/maxcpu.sock
2020/10/30 10:40:54 check daemon alive failed: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial unix /var/run/maxcpu.sock: connect: no such file or directory"
2020/10/30 10:40:54 start background process

$ ./mackerel-plugin-maxcpu --socket /tmp/maxcpu.sock
$ sudo ./mackerel-plugin-maxcpu --socket /var/run/maxcpu.sock
maxcpu.us_sy_wa_si_st_usage.max 0.251256 1604022058
maxcpu.us_sy_wa_si_st_usage.min 0.250627 1604022058
maxcpu.us_sy_wa_si_st_usage.avg 0.250941 1604022058
Expand All @@ -40,5 +40,5 @@ maxcpu.us_sy_wa_si_st_usage.75pt 0.251256 1604022058

## Install

Please download release page or `mkr plugin install kazeburo/mackerel-plugin-maxcpu`.
Please download release page or `mkr plugin install monitoring-forge/mackerel-plugin-maxcpu`.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/kazeburo/mackerel-plugin-maxcpu
module github.com/monitoring-forge/mackerel-plugin-maxcpu

go 1.25.0

Expand Down
2 changes: 1 addition & 1 deletion internal/statworker/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/bufbuild/connect-go"
"github.com/kazeburo/mackerel-plugin-maxcpu/maxcpu"
"github.com/monitoring-forge/mackerel-plugin-maxcpu/maxcpu"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand Down
45 changes: 35 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"time"

connect "github.com/bufbuild/connect-go"
"github.com/jessevdk/go-flags"
"github.com/kazeburo/mackerel-plugin-maxcpu/internal/statworker"
maxcpuconnect "github.com/kazeburo/mackerel-plugin-maxcpu/maxcpu/maxcpuconnect"
"github.com/monitoring-forge/mackerel-plugin-maxcpu/internal/statworker"
maxcpuconnect "github.com/monitoring-forge/mackerel-plugin-maxcpu/maxcpu/maxcpuconnect"
"google.golang.org/protobuf/types/known/emptypb"
)

// version by Makefile
var version string
var commit string

type Opt struct {
Socket string `short:"s" long:"socket" required:"true" description:"Socket file used calcurating daemon" `
Expand Down Expand Up @@ -122,6 +123,10 @@ func runBackground(opt *Opt) int {
log.Printf("%v", err)
return 1
}
if err := os.Chmod(opt.Socket, 0600); err != nil {
log.Printf("%v", err)
return 1
}
srv := &http.Server{Handler: mux}
go func() {
if err := srv.Serve(unixListener); err != nil && err != http.ErrServerClosed {
Expand Down Expand Up @@ -167,10 +172,26 @@ func getStats(opt *Opt) int {
}

func makeClient(socket string) (maxcpuconnect.MaxCPUClient, error) {
uid := os.Geteuid()
httpClient := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.DialTimeout("unix", socket, 1*time.Second)
// check owner of socket file
fi, err := os.Stat(socket)
if err != nil {
return nil, err
}
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return nil, fmt.Errorf("failed to get socket file stat")
}
if int(stat.Uid) != uid {
return nil, fmt.Errorf("socket file owner is not current user")
}
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
var d net.Dialer
return d.DialContext(ctx, "unix", socket)
},
},
}
Expand All @@ -188,13 +209,17 @@ func _main() int {
psr := flags.NewParser(opt, flags.HelpFlag|flags.PassDoubleDash)
_, err := psr.Parse()
if opt.Version {
fmt.Printf(`%s %s
Compiler: %s %s
`,
os.Args[0],
if commit == "" {
commit = "dev"
}
Comment thread
kazeburo marked this conversation as resolved.
fmt.Printf(
"%s-%s\n%s/%s, %s, %s\n",
filepath.Base(os.Args[0]),
version,
runtime.Compiler,
runtime.Version())
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
commit)
return 0
}
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"

connect "github.com/bufbuild/connect-go"
"github.com/kazeburo/mackerel-plugin-maxcpu/internal/statworker"
maxcpuconnect "github.com/kazeburo/mackerel-plugin-maxcpu/maxcpu/maxcpuconnect"
"github.com/monitoring-forge/mackerel-plugin-maxcpu/internal/statworker"
maxcpuconnect "github.com/monitoring-forge/mackerel-plugin-maxcpu/maxcpu/maxcpuconnect"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand Down
4 changes: 2 additions & 2 deletions maxcpu.proto
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
syntax = "proto3";
// go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
// go install google.golang.org/grpc/protoc-gen-go-grpc@lastest
// go install google.golang.org/grpc/protoc-gen-go-grpc@latest
// go install github.com/bufbuild/connect-go/cmd/protoc-gen-connect-go@latest
// protoc --go_out=maxcpu --go_opt=paths=source_relative maxcpu.proto
// protoc --connect-go_out=maxcpu --connect-go_opt=paths=source_relative maxcpu.proto

import "google/protobuf/empty.proto";

package maxcpu;
option go_package = "github.com/kazeburo/mackerel-plugin-maxcpu/maxcpu";
option go_package = "github.com/monitoring-forge/mackerel-plugin-maxcpu/maxcpu";

service MaxCPU {
rpc GetStats(google.protobuf.Empty) returns (StatsResponse) {}
Expand Down
4 changes: 2 additions & 2 deletions maxcpu/maxcpu.pb.go

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

7 changes: 3 additions & 4 deletions maxcpu/maxcpuconnect/maxcpu.connect.go

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

Loading