forked from cloudradar-monitoring/cagent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cagent.go
126 lines (101 loc) · 3.27 KB
/
cagent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cagent
import (
"fmt"
"net/http"
"runtime"
"sync"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/cloudradar-monitoring/selfupdate"
"github.com/cloudradar-monitoring/cagent/pkg/monitoring/fs"
"github.com/cloudradar-monitoring/cagent/pkg/monitoring/networking"
"github.com/cloudradar-monitoring/cagent/pkg/monitoring/sensors"
"github.com/cloudradar-monitoring/cagent/pkg/monitoring/updates"
"github.com/cloudradar-monitoring/cagent/pkg/monitoring/vmstat"
"github.com/cloudradar-monitoring/cagent/pkg/monitoring/vmstat/types"
"github.com/cloudradar-monitoring/cagent/pkg/smart"
)
// variables set on build. Example:
// go build -o cagent -ldflags="-X github.com/cloudradar-monitoring/cagent.Version=$(git --git-dir=src/github.com/cloudradar-monitoring/cagent/.git describe --always --long --dirty --tag)" github.com/cloudradar-monitoring/cagent/cmd/cagent
var (
Version string
LicenseInfo = "released under MIT license. https://github.com/cloudradar-monitoring/cagent/"
SelfUpdatesFeedURL = "https://repo.cloudradar.io/windows/cagent/feed/rolling"
)
type Cagent struct {
Config *Config
ConfigLocation string
selfUpdater *selfupdate.Updater
hubClient *http.Client
hubClientOnce sync.Once
cpuWatcher *CPUWatcher
cpuUtilisationAnalyser *CPUUtilisationAnalyser
fsWatcher *fs.FileSystemWatcher
netWatcher *networking.NetWatcher
vmstatLazyInit sync.Once
vmWatchers map[string]types.Provider
hwInventory sync.Once
smart *smart.SMART
}
func New(cfg *Config, cfgPath string) (*Cagent, error) {
ca := &Cagent{
Config: cfg,
ConfigLocation: cfgPath,
vmWatchers: make(map[string]types.Provider),
}
ca.configureLogger()
if ca.Config.SMARTMonitoring && ca.Config.SMARTCtl != "" {
var err error
ca.smart, err = smart.New(smart.Executable(ca.Config.SMARTCtl, false))
if err != nil {
logrus.Error(err.Error())
}
}
err := ca.configureAutomaticSelfUpdates()
if err != nil {
logrus.Error(err.Error())
return nil, err
}
return ca, nil
}
func (ca *Cagent) configureAutomaticSelfUpdates() error {
if !ca.Config.Updates.Enabled {
return nil
}
updatesConfig := selfupdate.DefaultConfig()
updatesConfig.AppName = "cagent"
updatesConfig.SigningCertificatedName = "cloudradar GmbH"
updatesConfig.CurrentVersion = Version
updatesConfig.CheckInterval = ca.Config.Updates.GetCheckInterval()
updatesConfig.UpdatesFeedURL = ca.Config.Updates.URL
logrus.Debugf("using %s as self-updates feed URL", updatesConfig.UpdatesFeedURL)
err := selfupdate.Configure(updatesConfig)
if err != nil {
return errors.Wrapf(err, "invalid configuration for self-update")
}
selfupdate.SetLogger(logrus.StandardLogger())
return nil
}
func (ca *Cagent) userAgent() string {
if Version == "" {
Version = "{undefined}"
}
return fmt.Sprintf("Cagent v%s %s %s", Version, runtime.GOOS, runtime.GOARCH)
}
func (ca *Cagent) Shutdown() {
defer sensors.Shutdown()
defer updates.Shutdown()
defer func() {
if ca.selfUpdater != nil {
ca.selfUpdater.Shutdown()
}
}()
for name, p := range ca.vmWatchers {
if err := vmstat.Release(p); err != nil {
logrus.WithFields(logrus.Fields{
"name": name,
}).WithError(err).Warnln("unable to release vm provider")
}
delete(ca.vmWatchers, name)
}
}