2018-12-04 05:57:11 +00:00
|
|
|
// Statping
|
2018-08-16 06:22:20 +00:00
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
2018-12-04 04:17:29 +00:00
|
|
|
// https://github.com/hunterlong/statping
|
2018-08-16 06:22:20 +00:00
|
|
|
//
|
|
|
|
// The licenses for most software and other practical works are designed
|
|
|
|
// to take away your freedom to share and change the works. By contrast,
|
|
|
|
// the GNU General Public License is intended to guarantee your freedom to
|
|
|
|
// share and change all versions of a program--to make sure it remains free
|
|
|
|
// software for all its users.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-06-30 00:57:05 +00:00
|
|
|
package core
|
2018-06-10 01:31:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/core/notifier"
|
2020-02-22 23:52:05 +00:00
|
|
|
"github.com/hunterlong/statping/database"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/types"
|
2018-09-10 22:16:23 +00:00
|
|
|
"sort"
|
2018-06-10 01:31:13 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
2020-02-26 05:38:03 +00:00
|
|
|
*database.ServiceObj
|
2018-07-14 02:37:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:38:03 +00:00
|
|
|
func Services() []*Service {
|
2020-02-25 07:41:28 +00:00
|
|
|
return CoreApp.services
|
2018-10-11 16:53:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// SelectService returns a *core.Service from in memory
|
2020-02-26 05:38:03 +00:00
|
|
|
func SelectService(id int64) *Service {
|
2018-11-09 05:29:01 +00:00
|
|
|
for _, s := range Services() {
|
2020-02-26 05:38:03 +00:00
|
|
|
if s.Id == id {
|
|
|
|
s.UpdateStats()
|
|
|
|
fmt.Println("service: ", s.Name, s.Stats)
|
2020-02-25 13:18:29 +00:00
|
|
|
return s
|
2019-02-06 18:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-07 22:38:56 +00:00
|
|
|
// CheckinProcess runs the checkin routine for each checkin attached to service
|
2020-02-25 07:41:28 +00:00
|
|
|
func CheckinProcess(s database.Servicer) {
|
2020-02-26 05:38:03 +00:00
|
|
|
for _, c := range s.Checkins() {
|
2018-10-07 22:38:56 +00:00
|
|
|
c.Start()
|
2020-02-25 07:41:28 +00:00
|
|
|
go CheckinRoutine(c)
|
2018-10-07 22:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 05:53:15 +00:00
|
|
|
// SelectAllServices returns a slice of *core.Service to be store on []*core.Services
|
|
|
|
// should only be called once on startup.
|
2020-02-26 05:38:03 +00:00
|
|
|
func SelectAllServices(start bool) ([]*Service, error) {
|
2020-02-25 07:41:28 +00:00
|
|
|
srvs := database.Services()
|
|
|
|
for _, s := range srvs {
|
2018-10-11 16:53:13 +00:00
|
|
|
if start {
|
2020-02-26 05:38:03 +00:00
|
|
|
s.Start()
|
2020-02-25 07:41:28 +00:00
|
|
|
CheckinProcess(s)
|
2018-11-08 10:50:06 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
|
|
|
|
fails := s.Failures().Last(limitedFailures)
|
|
|
|
s.Service.Failures = fails
|
|
|
|
|
|
|
|
for _, c := range s.Checkins() {
|
|
|
|
s.Service.Checkins = append(s.Service.Checkins, c.Checkin)
|
2018-12-06 19:03:55 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
|
2020-02-04 03:49:17 +00:00
|
|
|
// collect initial service stats
|
2020-02-26 05:38:03 +00:00
|
|
|
s.UpdateStats()
|
|
|
|
CoreApp.services = append(CoreApp.services, &Service{s})
|
2018-06-22 06:56:44 +00:00
|
|
|
}
|
2018-12-06 19:03:55 +00:00
|
|
|
reorderServices()
|
2020-02-26 05:38:03 +00:00
|
|
|
return CoreApp.services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapFailures(f []*types.Failure) []*Failure {
|
|
|
|
var fails []*Failure
|
|
|
|
for _, v := range f {
|
|
|
|
fails = append(fails, &Failure{v})
|
|
|
|
}
|
|
|
|
return fails
|
2018-08-20 07:20:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// reorderServices will sort the services based on 'order_id'
|
|
|
|
func reorderServices() {
|
2020-02-25 07:41:28 +00:00
|
|
|
sort.Sort(ServiceOrder(CoreApp.services))
|
2018-09-16 10:29:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// index returns a services index int for updating the []*core.Services slice
|
2020-02-26 05:38:03 +00:00
|
|
|
func index(s int64) int {
|
2020-02-25 07:41:28 +00:00
|
|
|
for k, service := range CoreApp.services {
|
2020-02-26 05:38:03 +00:00
|
|
|
if s == service.Id {
|
2018-08-21 03:11:40 +00:00
|
|
|
return k
|
2018-08-20 07:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-21 03:11:40 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// updateService will update a service in the []*core.Services slice
|
2020-02-26 05:38:03 +00:00
|
|
|
func updateService(s *Service) {
|
|
|
|
CoreApp.services[index(s.Id)] = s
|
2018-08-20 07:20:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// Delete will remove a service from the database, it will also end the service checking go routine
|
2020-02-26 05:38:03 +00:00
|
|
|
func (s *Service) Delete() error {
|
|
|
|
i := index(s.Id)
|
2020-02-25 07:41:28 +00:00
|
|
|
err := database.Delete(s)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(fmt.Sprintf("Failed to delete service %v. %v", s.Name, err))
|
|
|
|
return err
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
2018-10-07 05:04:06 +00:00
|
|
|
s.Close()
|
2020-02-25 07:41:28 +00:00
|
|
|
slice := CoreApp.services
|
|
|
|
CoreApp.services = append(slice[:i], slice[i+1:]...)
|
2018-09-10 22:16:23 +00:00
|
|
|
reorderServices()
|
2020-02-26 05:38:03 +00:00
|
|
|
notifier.OnDeletedService(s.Service)
|
2020-02-25 07:41:28 +00:00
|
|
|
return err
|
2018-09-05 10:54:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// Update will update a service in the database, the service's checking routine can be restarted by passing true
|
2020-02-26 05:38:03 +00:00
|
|
|
func Update(s *Service, restart bool) error {
|
2020-02-25 07:41:28 +00:00
|
|
|
err := database.Update(s)
|
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(fmt.Sprintf("Failed to update service %v. %v", s.Name, err))
|
2020-02-25 07:41:28 +00:00
|
|
|
return err
|
2018-07-01 03:54:28 +00:00
|
|
|
}
|
2018-11-29 09:10:33 +00:00
|
|
|
// clear the notification queue for a service
|
|
|
|
if !s.AllowNotifications.Bool {
|
|
|
|
for _, n := range CoreApp.Notifications {
|
2018-11-29 16:26:42 +00:00
|
|
|
notif := n.(notifier.Notifier).Select()
|
2019-03-04 17:18:50 +00:00
|
|
|
notif.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
2018-11-29 09:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-22 05:41:15 +00:00
|
|
|
if restart {
|
2018-10-07 05:04:06 +00:00
|
|
|
s.Close()
|
|
|
|
s.Start()
|
2020-02-26 05:38:03 +00:00
|
|
|
s.SleepDuration = s.Duration()
|
|
|
|
go ServiceCheckQueue(s, true)
|
2018-08-22 05:41:15 +00:00
|
|
|
}
|
2018-09-10 22:16:23 +00:00
|
|
|
reorderServices()
|
2020-02-26 05:38:03 +00:00
|
|
|
updateService(s)
|
|
|
|
notifier.OnUpdatedService(s.Service)
|
2020-02-25 07:41:28 +00:00
|
|
|
return err
|
2018-07-27 02:10:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// Create will create a service and insert it into the database
|
2020-02-25 07:41:28 +00:00
|
|
|
func Create(srv database.Servicer, check bool) (int64, error) {
|
|
|
|
s := srv.Model()
|
2020-01-04 02:03:59 +00:00
|
|
|
s.CreatedAt = time.Now().UTC()
|
2020-02-25 07:41:28 +00:00
|
|
|
_, err := database.Create(s)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(fmt.Sprintf("Failed to create service %v #%v: %v", s.Name, s.Id, err))
|
|
|
|
return 0, err
|
2018-06-10 03:44:47 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
service := &Service{s}
|
2018-10-07 05:04:06 +00:00
|
|
|
s.Start()
|
2020-02-26 05:38:03 +00:00
|
|
|
CoreApp.services = append(CoreApp.services, service)
|
|
|
|
go ServiceCheckQueue(service, check)
|
2018-09-10 22:16:23 +00:00
|
|
|
reorderServices()
|
2020-02-26 05:38:03 +00:00
|
|
|
notifier.OnNewService(s.Service)
|
2018-10-07 05:04:06 +00:00
|
|
|
return s.Id, nil
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|