bettergoapi

package module
v0.0.0-...-bb934de Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2025 License: MIT Imports: 17 Imported by: 0

README

bettergoapi Uptime API Clients

This is a go library for setting up and pausing and unpausing bettergoapiUptime Monitors

This can be used to temporarily pause monitoring during patching

It uses the Uptime API see Getting started with Uptime API

Example programs are included to set up from a CSV file and to pause and unpause monitors

Programs using the library

path
scripts/alarmtoggle/main.go alarmtoggle
scripts/fromCSV/main.go fromCSV

How to build alarm toggle for Linux

go mod tidy
env GOOS=linux GOARCH=amd64 go build -ldflags "-w -s" -o alarmtoggle scripts/alarmtoggle/main.go

This makes a single file static binary that can be copied directly to Intel / AMD hosts and used Does not bundle the TEAM_TOKEN (see below). This must be in the environment where alarmtoggle is used

Running alarm toggle

To test script is working
./alarmtoggle 

Shows verbose dump of json of monitors

To store all alarm states locally
./alarmtoggle  -store=true

alarm states are stored in /tmp/alarms.json

To turn off monitors (pause monitors)

./alarmtoggle -set=off -pattern=testrpt1

output is lines in this format

2023/04/14 11:53:25 OK paused=true https://romeo.linuxufo.com/state.txt alarm id 1148817

pattern match is on the URL only. It is a substring match. alarmtoggle -set= only works if -store=true was previously run. It uses /tmp/alarms.json as a list of alarms

To turn on monitors (unpause monitors)

./alarmtoggle -set=on -pattern=testrpt1

output is lines in this format

2023/04/14 11:57:05 OK paused=false https://romeo.linuxufo.com/state.txt alarm id 1148817

pattern match is on the URL only. It is a substring match. alarmtoggle -set= only works if -store=true was previously run. It uses /tmp/alarms.json as a list of alarms

To run without building

To run a local copy without building

go run scripts/alarmtoggle/main.go  -set=on  -pattern=testrpt1
Problem "patch failed 401 Unauthorized"

If this happens

2023/04/14 11:59:39 env var not set and failed to get from ssm
2023/04/14 11:59:39 patch failed 401 Unauthorized
2023/04/14 11:59:39 response was {"errors":"Invalid Team API token. How to find your Team API token: https://bettergoapistack.com/docs/uptime/api/getting-started-with-bettergoapi-uptime-api#obtaining-a-bettergoapi-uptime-api-token"}
2023/04/14 11:59:39 patch failed paused=false https://romeo.linuxufo.com/state.txt alarm id 1148817
exit status 1

The TEAM_TOKEN is not set, see below

alarmtoggle all options
option parameter default use or result
-show true/false false when true verbose show as json all monitors. Do not do anything else
-store true/false false write json of monitors to /tmp/alarms.json
-set on on monitors force on
-set off off monitors force off
-set last use previously stored values for pause state of monitor
-set reverse use opposite of stored values for pause state of monitor
-pattern string only set alarms that substring match this url pattern

Dependencies

Files

file
client.go golang http client
client_test.go
get_monitor_test.go
go.mod go module config file
go.sum go module config file
loadcompare.go golang compare monitors (not currently used)
loadcompare_test.go
monitor.go golang definition of bettergoapi uptime monitor data structure
save.go save monitors as json to file or s3
save_test.go

Platforms

Tested on Ubuntu 22.04 and MacOS 14.6 go version go 1.23.8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EzDiff

func EzDiff(m1 Monitor, m2 Monitor) string

func GetAWSConfig

func GetAWSConfig(ctx context.Context) aws.Config

func GetRemote

func GetRemote(path string, cli HClient) ([]byte, error)

func PatchRemote

func PatchRemote(cli HClient, path string, obj []byte) ([]byte, error)

func PostRemote

func PostRemote(cli HClient, path string, obj []byte) ([]byte, error)

func SaveToFile

func SaveToFile(monitor []Monitor, path string) error

func SaveToS3

func SaveToS3(ctx context.Context, monitor []Monitor, path string, uploader MyUploader) error

call as SaveToS3(ctx, monitor, "monitors.json", manager.NewUploader(client))

func SelectiveCompare

func SelectiveCompare(m1 Monitor, m2 Monitor) bool

Types

type HClient

type HClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type Monitor

type Monitor struct {
	ID                  string     `json:"id,omitempty"`
	Type                string     `json:"type,omitempty"`
	URL                 string     `json:"url,omitempty"`
	PronounceableName   string     `json:"pronounceable_name,omitempty"`
	AuthUsername        string     `json:"auth_username,omitempty"`
	AuthPassword        string     `json:"auth_password,omitempty"`
	MonitorType         string     `json:"monitor_type,omitempty"`
	MonitorGroupID      any        `json:"monitor_group_id,omitempty"`
	LastCheckedAt       *time.Time `json:"last_checked_at,omitempty"`
	Status              string     `json:"status,omitempty"`
	PolicyID            any        `json:"policy_id,omitempty"`
	RequiredKeyword     string     `json:"required_keyword,omitempty"`
	VerifySsl           bool       `json:"verify_ssl,omitempty"`
	CheckFrequency      int        `json:"check_frequency,omitempty"`
	Call                bool       `json:"call,omitempty"`
	Sms                 bool       `json:"sms,omitempty"`
	Email               bool       `json:"email,omitempty"`
	Push                bool       `json:"push,omitempty"`
	TeamWait            any        `json:"team_wait,omitempty"`
	HTTPMethod          string     `json:"http_method,omitempty"`
	RequestTimeout      int        `json:"request_timeout,omitempty"`
	RecoveryPeriod      int        `json:"recovery_period,omitempty"`
	RequestHeaders      []any      `json:"request_headers,omitempty"`
	RequestBody         string     `json:"request_body,omitempty"`
	FollowRedirects     bool       `json:"follow_redirects,omitempty"`
	RememberCookies     bool       `json:"remember_cookies,omitempty"`
	CreatedAt           *time.Time `json:"created_at,omitempty"`
	UpdatedAt           *time.Time `json:"updated_at,omitempty"`
	SslExpiration       any        `json:"ssl_expiration,omitempty"`
	DomainExpiration    any        `json:"domain_expiration,omitempty"`
	Regions             []string   `json:"regions,omitempty"`
	ExpectedStatusCodes []int      `json:"expected_status_codes,omitempty"`
	Port                any        `json:"port,omitempty"`
	ConfirmationPeriod  int        `json:"confirmation_period,omitempty"`
	PausedAt            any        `json:"paused_at,omitempty"`
	Paused              *bool      `json:"paused,omitempty"`
	MaintenanceFrom     any        `json:"maintenance_from,omitempty"`
	MaintenanceTo       any        `json:"maintenance_to,omitempty"`
	MaintenanceTimezone string     `json:"maintenance_timezone,omitempty"`
}

func LoadFromFile

func LoadFromFile(path string) ([]Monitor, error)

func LoadFromS3

func LoadFromS3(ctx context.Context, path string, downloader MyDownloader) ([]Monitor, error)

call as LoadFromS3(ctx, "monitors.json", manager.NewDownloader(client))

func PatchMonitor

func PatchMonitor(cli HClient, updateMonitor Monitor) (Monitor, error)

func PutMonitor

func PutMonitor(cli HClient, newMonitor Monitor) (Monitor, error)

func (Monitor) GetData

func (b Monitor) GetData() interface{}

func (Monitor) GetID

func (b Monitor) GetID() string

func (Monitor) GetType

func (b Monitor) GetType() string

func (*Monitor) SetData

func (b *Monitor) SetData(to func(target interface{}) error) error

func (*Monitor) SetID

func (b *Monitor) SetID(id string) error

func (*Monitor) SetType

func (b *Monitor) SetType(t string) error

type Monitors

type Monitors []Monitor

func GetMonitors

func GetMonitors(cli HClient) (Monitors, error)

&http.Client{}

func (*Monitors) SetData

func (b *Monitors) SetData(to func(target interface{}) error) error

type MyDownloader

type MyDownloader interface {
	Download(ctx context.Context, w io.WriterAt, input *s3.GetObjectInput, opts ...func(*manager.Downloader)) (n int64, err error)
}

type MyUploader

type MyUploader interface {
	Upload(ctx context.Context, input *s3.PutObjectInput, opts ...func(*manager.Uploader)) (*manager.UploadOutput, error)
}

Directories

Path Synopsis
scripts
alarmtoggle command
fromCSV command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL