health.go 864 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package service
  2. import (
  3. "github.com/IceWhaleTech/CasaOS-Common/utils/port"
  4. "github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
  5. )
  6. type HealthService interface {
  7. Services() (map[bool]*[]string, error)
  8. Ports() ([]int, []int, error)
  9. }
  10. type service struct{}
  11. func (s *service) Services() (map[bool]*[]string, error) {
  12. services, err := systemctl.ListServices("casaos*")
  13. if err != nil {
  14. return nil, err
  15. }
  16. var running, notRunning []string
  17. for _, service := range services {
  18. if service.Running {
  19. running = append(running, service.Name)
  20. } else {
  21. notRunning = append(notRunning, service.Name)
  22. }
  23. }
  24. result := map[bool]*[]string{
  25. true: &running,
  26. false: &notRunning,
  27. }
  28. return result, nil
  29. }
  30. func (s *service) Ports() ([]int, []int, error) {
  31. return port.ListPortsInUse()
  32. }
  33. func NewHealthService() HealthService {
  34. return &service{}
  35. }