2018-06-10 01:31:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-06 22:18:06 +00:00
|
|
|
"fmt"
|
2020-02-29 23:36:31 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-04 00:28:09 +00:00
|
|
|
"github.com/statping/statping/database"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/handlers"
|
2020-04-04 00:28:09 +00:00
|
|
|
"github.com/statping/statping/notifiers"
|
2020-04-02 04:44:41 +00:00
|
|
|
"github.com/statping/statping/source"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/configs"
|
2020-04-04 00:28:09 +00:00
|
|
|
"github.com/statping/statping/types/core"
|
2020-06-16 09:42:21 +00:00
|
|
|
"github.com/statping/statping/types/metrics"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/services"
|
|
|
|
"github.com/statping/statping/utils"
|
2020-04-04 00:28:09 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2018-06-10 01:31:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-04 04:17:29 +00:00
|
|
|
// VERSION stores the current version of Statping
|
2018-10-07 04:48:33 +00:00
|
|
|
VERSION string
|
2018-12-04 04:17:29 +00:00
|
|
|
// COMMIT stores the git commit hash for this version of Statping
|
2020-06-05 03:47:35 +00:00
|
|
|
COMMIT string
|
|
|
|
log = utils.Log.WithField("type", "cmd")
|
|
|
|
confgs *configs.DbConfig
|
2020-06-11 09:14:46 +00:00
|
|
|
stopped chan bool
|
2018-06-10 01:31:13 +00:00
|
|
|
)
|
|
|
|
|
2020-04-04 00:28:09 +00:00
|
|
|
func init() {
|
2020-06-11 09:14:46 +00:00
|
|
|
stopped = make(chan bool, 1)
|
2020-08-18 19:39:55 +00:00
|
|
|
core.New(VERSION, COMMIT)
|
2020-06-10 19:41:43 +00:00
|
|
|
utils.InitEnvs()
|
2020-08-18 19:39:55 +00:00
|
|
|
utils.Params.Set("VERSION", VERSION)
|
|
|
|
utils.Params.Set("COMMIT", COMMIT)
|
2020-06-10 19:41:43 +00:00
|
|
|
|
2020-04-16 09:57:00 +00:00
|
|
|
rootCmd.AddCommand(versionCmd)
|
2020-07-14 20:32:31 +00:00
|
|
|
rootCmd.AddCommand(updateCmd)
|
2020-04-16 09:57:00 +00:00
|
|
|
rootCmd.AddCommand(assetsCmd)
|
|
|
|
rootCmd.AddCommand(exportCmd)
|
|
|
|
rootCmd.AddCommand(importCmd)
|
|
|
|
rootCmd.AddCommand(sassCmd)
|
|
|
|
rootCmd.AddCommand(onceCmd)
|
2020-04-17 03:21:17 +00:00
|
|
|
rootCmd.AddCommand(envCmd)
|
2020-07-24 01:36:30 +00:00
|
|
|
rootCmd.AddCommand(systemctlCmd)
|
2020-04-17 03:21:17 +00:00
|
|
|
rootCmd.AddCommand(resetCmd)
|
2020-06-10 19:41:43 +00:00
|
|
|
|
2020-04-17 03:21:17 +00:00
|
|
|
parseFlags(rootCmd)
|
2020-04-04 00:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// exit will return an error and return an exit code 1 due to this error
|
2020-03-03 06:42:37 +00:00
|
|
|
func exit(err error) {
|
2020-04-04 00:28:09 +00:00
|
|
|
utils.SentryErr(err)
|
|
|
|
log.Fatalln(err)
|
2020-06-11 09:14:46 +00:00
|
|
|
os.Exit(1)
|
2020-03-03 06:42:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 00:28:09 +00:00
|
|
|
// Close will gracefully stop the database connection, and log file
|
|
|
|
func Close() {
|
|
|
|
utils.CloseLogs()
|
|
|
|
confgs.Close()
|
|
|
|
fmt.Println("Shutting down Statping")
|
2020-03-22 07:09:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 04:17:29 +00:00
|
|
|
// main will run the Statping application
|
2018-06-10 01:31:13 +00:00
|
|
|
func main() {
|
2020-06-05 03:47:35 +00:00
|
|
|
go Execute()
|
2020-06-11 09:14:46 +00:00
|
|
|
<-stopped
|
2020-06-05 03:47:35 +00:00
|
|
|
Close()
|
2020-04-16 09:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// main will run the Statping application
|
|
|
|
func start() {
|
2019-12-19 16:48:59 +00:00
|
|
|
go sigterm()
|
2020-06-05 03:47:35 +00:00
|
|
|
var err error
|
2020-03-04 10:29:00 +00:00
|
|
|
if err := source.Assets(); err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.VerboseMode = verboseMode
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2019-04-29 18:11:44 +00:00
|
|
|
if err := utils.InitLogs(); err != nil {
|
2019-12-30 11:12:14 +00:00
|
|
|
log.Errorf("Statping Log Error: %v\n", err)
|
2019-04-29 18:11:44 +00:00
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2020-04-04 00:28:09 +00:00
|
|
|
log.Info(fmt.Sprintf("Starting Statping v%s", VERSION))
|
2018-11-21 08:45:55 +00:00
|
|
|
|
2020-06-14 02:46:31 +00:00
|
|
|
utils.Params.Set("SERVER_IP", ipAddress)
|
|
|
|
utils.Params.Set("SERVER_PORT", port)
|
|
|
|
|
2020-05-10 21:40:45 +00:00
|
|
|
confgs, err = configs.LoadConfigs(configFile)
|
2020-03-04 10:29:00 +00:00
|
|
|
if err != nil {
|
2020-04-19 12:53:23 +00:00
|
|
|
log.Infoln("Starting in Setup Mode")
|
2020-06-05 03:47:35 +00:00
|
|
|
if err = handlers.RunHTTPServer(); err != nil {
|
2020-03-03 06:42:37 +00:00
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 08:03:50 +00:00
|
|
|
if err = configs.ConnectConfigs(confgs, true); err != nil {
|
2020-03-04 10:29:00 +00:00
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:22:59 +00:00
|
|
|
if err = confgs.ResetCore(); err != nil {
|
|
|
|
exit(err)
|
2020-03-09 15:15:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 04:37:52 +00:00
|
|
|
if err = confgs.DatabaseChanges(); err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := confgs.MigrateDatabase(); err != nil {
|
2020-03-04 10:29:00 +00:00
|
|
|
exit(err)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2020-02-26 06:39:54 +00:00
|
|
|
|
2019-12-30 11:12:14 +00:00
|
|
|
if err := mainProcess(); err != nil {
|
2020-03-03 06:42:37 +00:00
|
|
|
exit(err)
|
2019-12-30 11:12:14 +00:00
|
|
|
}
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:48:59 +00:00
|
|
|
// sigterm will attempt to close the database connections gracefully
|
|
|
|
func sigterm() {
|
|
|
|
sigs := make(chan os.Signal, 1)
|
2020-03-06 22:18:06 +00:00
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2019-12-19 16:48:59 +00:00
|
|
|
<-sigs
|
2020-06-11 09:14:46 +00:00
|
|
|
stopped <- true
|
2019-12-19 16:48:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 04:17:29 +00:00
|
|
|
// mainProcess will initialize the Statping application and run the HTTP server
|
2019-12-30 11:12:14 +00:00
|
|
|
func mainProcess() error {
|
2020-03-14 03:13:20 +00:00
|
|
|
if err := InitApp(); err != nil {
|
2020-02-26 05:38:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2020-04-30 08:35:33 +00:00
|
|
|
services.LoadServicesYaml()
|
|
|
|
|
2020-06-05 03:47:35 +00:00
|
|
|
if err := handlers.RunHTTPServer(); err != nil {
|
2020-03-04 10:29:00 +00:00
|
|
|
log.Fatalln(err)
|
|
|
|
return errors.Wrap(err, "http server")
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2020-02-29 23:36:31 +00:00
|
|
|
return nil
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2020-04-04 00:28:09 +00:00
|
|
|
// InitApp will start the Statping instance with a valid database connection
|
|
|
|
// This function will gather all services in database, add/init Notifiers,
|
|
|
|
// and start the database cleanup routine
|
|
|
|
func InitApp() error {
|
2020-06-11 09:14:46 +00:00
|
|
|
// fetch Core row information about this instance.
|
2020-04-04 00:28:09 +00:00
|
|
|
if _, err := core.Select(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-26 10:48:05 +00:00
|
|
|
// init Sentry error monitoring (its useful)
|
|
|
|
utils.SentryInit(core.App.AllowReports.Bool)
|
2020-06-16 09:42:21 +00:00
|
|
|
// init prometheus metrics
|
|
|
|
metrics.InitMetrics()
|
2020-08-26 10:48:05 +00:00
|
|
|
// connect each notifier, added them into database if needed
|
|
|
|
notifiers.InitNotifiers()
|
2020-06-11 09:14:46 +00:00
|
|
|
// select all services in database and store services in a mapping of Service pointers
|
2020-04-04 00:28:09 +00:00
|
|
|
if _, err := services.SelectAllServices(true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-11 09:14:46 +00:00
|
|
|
// start routines for each service checking process
|
2020-06-11 06:21:53 +00:00
|
|
|
services.CheckServices()
|
2020-06-11 09:14:46 +00:00
|
|
|
// start routine to delete old records (failures, hits)
|
2020-04-11 22:18:43 +00:00
|
|
|
go database.Maintenance()
|
2020-04-04 00:28:09 +00:00
|
|
|
core.App.Setup = true
|
|
|
|
core.App.Started = utils.Now()
|
|
|
|
return nil
|
|
|
|
}
|