Skip to content

Commit

Permalink
Merge pull request #85 from sooraj-sky/main
Browse files Browse the repository at this point in the history
v0.1.6
  • Loading branch information
sooraj-sky authored Feb 26, 2023
2 parents 650f1fb + 3ee09ab commit a8533fb
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 230 deletions.
61 changes: 34 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@ See Docker Hub Image
https://hub.docker.com/r/soorajsky/sky-meter

## Environment variables
Currenly we have two environment variables.
1. sentry_dsn
2. PORT
| Variable | Type | Example |
|----------------|---------|-----------------|
| DnsServer | string | 8.8.8.8 |
| Port | string | 8000 |
| EmailPass | string | youremailpass |
| EmailFrom | string | [email protected] |
| EmailPort | string | 583 |
| EmailServer | string | smtp.gmail.com |
| OpsgenieSecret | string | examplesecret |
| SentryDsn | string | exapledsnvalue |
| Mode | string | prod |
| DbUrl | string | host=localhost user=postgres password=postgres dbname=postgres port=5433 sslmode=disable |



- You can create sentry project and imput the **sentry_dsn** as env variable.
- You can export the **PORT** variable to set the http port of the server

## Add URLs to check
To add a URL to minitoring is pertty simple. Create **settings.yml** to add your endpoints to monitor. See an example of **settings.yml** below
```sh
opegenie:
- enabled: false
enabled: false
email:
- enabled: true
server: smtp.gmail.com
port: 587
sender: [email protected]
enabled: true
groups:
- name: prod
emails:
Expand Down Expand Up @@ -89,28 +95,29 @@ Clone the code
$ git clone https://github.com/sooraj-sky/sky-meter.git
$ cd sky-meter
```
Run the postgres docker container
Run the postgres docker container (skip this step if you already have a database)
```sh
$ docker-compose up -d
```
Added Env Option: You can enable sentry by adding
Export ENV variables (Sentry will work only in dev mode)
If Email is disbled on **settings.yml** the following variables are not needed.
1. EmailPass
2. EmailFrom
3. EmailPort
4. EmailServer

**SentryDsn** is only needed when **Mode=dev**

Export sentry dsn
```sh
$ export mode="dev"
$ export sentry_dsn="<yourDsnHere>"
```
Export the port
```sh
$ export PORT=8080
```
Export opsgenieSecret
```sh
$ export opsgeniesecret="your-opsgenie-api-keyhere"
```
Export email passsword
```sh
export emailpass="your-email-pass-here"
$ export DnsServer="8.8.8.8" #requied
$ export DbUrl="host=localhost user=postgres password=postgres dbname=postgres port=5433 sslmode=disable" #requied
$ export EmailPass="your-pass-here" #requied when Email is Enabled
$ export EmailFrom="[email protected]" #requied when Email is Enabled
$ export EmailPort="587" #requied when Email is Enabled
$ export EmailServer="smtp.server-address-here.com" #requied when Email is Enabled
$ export OpsgenieSecret="your-opsgenie-key-here" #requied when Opsgenie is Enabled on settings.yml
$ export Mode="dev"
$ export SentryDsn="your-DSn-key-here" #requied when Mode="dev"
```
Run the project
```sh
Expand Down
36 changes: 23 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,50 @@ package main

import (
"github.com/jasonlvhit/gocron"
dbops "github.com/sooraj-sky/sky-meter/packages/dbops"
skymeter "github.com/sooraj-sky/sky-meter/packages/httpserver"
sentry "github.com/sooraj-sky/sky-meter/packages/logger"
yamlops "github.com/sooraj-sky/sky-meter/packages/yamlops"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"os"
dbops "sky-meter/packages/dbops"
skyenv "sky-meter/packages/env"
sentry "sky-meter/packages/logger"
yamlops "sky-meter/packages/yamlops"
)

func init() {

// Initialize the environment variables
skyenv.InitEnv()
}

func main() {
log.Println("Launching sky-meter")

// Initialize the Sentry logger
sentry.SentryInit()
dbconnect := os.Getenv("dbconnect")
if opsgenieSecret == "" {
log.Fatal("Please specify the opsgeniesecret as environment variable, e.g. sooraj@sky:~/go/src/sky-meter$ export dbconnect=host=localhost user=postgres password=postgres dbname=postgres port=5433 sslmode=disable")
}

// Get all the environment variables
allEnv := skyenv.GetEnv()
dbconnect := allEnv.DbUrl
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: dbconnect,
PreferSimpleProtocol: true, // disables implicit prepared statement usage

}), &gorm.Config{})

if err != nil {
log.Println(err)

// Exit the program if there is an error connecting to the database
log.Fatal(err)
}

// Read the YAML file to get the list of endpoints to monitor
endpoints := yamlops.InputYml()
dbops.InitialMigration(db)
dbops.InsertUrlsToDb(db, endpoints)
dbops.RemoveOldEntry(db, endpoints)
log.Println("Updated sky-meter targets")
log.Println("Staring sky-meter Health Check")
gocron.Every(1).Second().Do(dbops.GetUrlFrequency, db)
<-gocron.Start()
skymeter.InitServer()
gocron.Every(1).Second().Do(dbops.GetUrlFrequency, db, endpoints)
<-gocron.Start() // Start the scheduler and block the main thread until it exits

}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/sooraj-sky/sky-meter
module sky-meter

go 1.18

Expand Down Expand Up @@ -33,7 +33,7 @@ require (
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.8 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gorm.io/driver/mysql v1.3.2 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
Expand Down
46 changes: 0 additions & 46 deletions input.json

This file was deleted.

20 changes: 15 additions & 5 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,13 @@ type SmtpErr struct {
}

type UserInput struct {
Opegenie []struct {
Opegenie struct {
Enabled bool
}
Email []struct {
Email struct {
Enabled bool
Server string
Port int
Sender string
}

Groups []struct {
Name string
Emails []string
Expand All @@ -126,3 +124,15 @@ type AlertGroups struct {
Name string
Email string
}

type AllEnvs struct {
DnsServer string
EmailPass string
EmailFrom string
EmailPort string
EmailServer string
OpsgenieSecret string
SentryDsn string
Mode string
DbUrl string
}
Loading

0 comments on commit a8533fb

Please sign in to comment.