-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
252 lines (219 loc) · 8.3 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Example of a daemon with echo service
package main
import (
"context"
"flag"
"io"
"log"
"os"
"runtime"
"github.com/Releem/daemon"
"github.com/Releem/mysqlconfigurer/config"
"github.com/Releem/mysqlconfigurer/metrics"
"github.com/Releem/mysqlconfigurer/models"
r "github.com/Releem/mysqlconfigurer/repeater"
"github.com/Releem/mysqlconfigurer/utils"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/aws/aws-sdk-go/aws/awserr"
_ "github.com/go-sql-driver/mysql"
logging "github.com/google/logger"
)
const (
// name of the service
serviceName = "releem-agent"
serviceDescription = "Releem Agent"
)
var logger logging.Logger
var SetConfigRun, GetConfigRun *bool
var ConfigFile, AgentEvent, AgentTask *string
// Service has embedded daemon
type Service struct {
daemon.Daemon
}
type Programm struct{}
func (programm *Programm) Stop() {
// Stop should not block. Return with a few seconds.
}
func (programm *Programm) Start() {
// Start should not block. Do the actual work async.
go programm.Run()
}
func (programm *Programm) Run() {
var TypeConfiguration string
var gatherers, gatherers_configuration, gatherers_query_optimization []models.MetricsGatherer
var Mode models.ModeType
if *SetConfigRun {
TypeConfiguration = "set"
} else if *GetConfigRun {
TypeConfiguration = "get"
} else {
TypeConfiguration = "default"
}
// Do something, call your goroutines, etc
logger.Info("Starting releem-agent of version is ", config.ReleemAgentVersion)
configuration, err := config.LoadConfig(*ConfigFile, logger)
if err != nil {
logger.Error("Config load failed", err)
return
}
defer utils.HandlePanic(configuration, logger)
if configuration.Debug {
logger.SetLevel(10)
} else {
logger.SetLevel(1)
}
if len(*AgentEvent) > 0 {
Mode.Name = "Event"
Mode.Type = *AgentEvent
} else if len(*AgentTask) > 0 {
Mode.Name = "TaskSet"
Mode.Type = *AgentTask
} else {
Mode.Name = "Configurations"
Mode.Type = TypeConfiguration
}
// if Mode.Name != "Event" {
// Select how we collect instance metrics depending on InstanceType
switch configuration.InstanceType {
case "aws/rds":
logger.Info("InstanceType is aws/rds")
logger.Info("Loading AWS configuration")
awscfg, err := awsconfig.LoadDefaultConfig(context.TODO(), awsconfig.WithRegion(configuration.AwsRegion))
if err != nil {
logger.Error("Load AWS configuration FAILED", err)
return
} else {
logger.Info("AWS configuration loaded SUCCESS")
}
cwlogsclient := cloudwatchlogs.NewFromConfig(awscfg)
// cwclient := cloudwatch.NewFromConfig(awscfg)
rdsclient := rds.NewFromConfig(awscfg)
// ec2client := ec2.NewFromConfig(awscfg)
// Prepare request to RDS
input := &rds.DescribeDBInstancesInput{
DBInstanceIdentifier: &configuration.AwsRDSDB,
}
// Request to RDS
result, err := rdsclient.DescribeDBInstances(context.TODO(), input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
logger.Error(aerr.Error())
return
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
logger.Error(err.Error())
return
}
}
logger.Info("RDS.DescribeDBInstances SUCCESS")
// Request detailed instance info
if result != nil && len(result.DBInstances) == 1 {
// gatherers = append(gatherers, models.NewAWSRDSMetricsGatherer(logger, cwclient, configuration))
// gatherers = append(gatherers, models.NewAWSRDSInstanceGatherer(logger, rdsclient, ec2client, configuration))
configuration.Hostname = configuration.AwsRDSDB
configuration.MysqlHost = *result.DBInstances[0].Endpoint.Address
gatherers = append(gatherers, metrics.NewAWSRDSEnhancedMetricsGatherer(logger, result.DBInstances[0], cwlogsclient, configuration))
} else if result != nil && len(result.DBInstances) > 1 {
logger.Infof("RDS.DescribeDBInstances: Database has %d instances. Clusters are not supported", len(result.DBInstances))
return
} else {
logger.Info("RDS.DescribeDBInstances: No instances")
return
}
default:
logger.Info("InstanceType is Local")
gatherers = append(gatherers, metrics.NewOSMetricsGatherer(logger, configuration))
}
models.DB = utils.ConnectionDatabase(configuration, logger, "mysql")
defer models.DB.Close()
//Init repeaters
// repeaters := make(map[string]models.MetricsRepeater)
// repeaters["Metrics"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, models.Mode{Name: "Metrics", Type: ""}))
// repeaters["Configurations"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, Mode))
// repeaters["Event"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, Mode))
// repeaters["TaskGet"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, models.Mode{Name: "TaskGet", Type: ""}))
// repeaters["TaskStatus"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, models.Mode{Name: "TaskStatus", Type: ""}))
// repeaters["TaskSet"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, Mode))
// repeaters["GetConfigurationJson"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, models.Mode{Name: "Configurations", Type: "get-json"}))
// repeaters["QueryOptimization"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, models.Mode{Name: "Metrics", Type: "QuerysOptimization"}))
// repeaters["QueriesOptimization"] = models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, models.Mode{Name: "TaskSet", Type: "queries_optimization"}))
//var repeaters models.MetricsRepeater
repeaters := models.MetricsRepeater(r.NewReleemConfigurationsRepeater(configuration, logger))
//Init gatherers
gatherers = append(gatherers,
metrics.NewDbConfGatherer(logger, configuration),
metrics.NewDbInfoGatherer(logger, configuration),
metrics.NewDbMetricsBaseGatherer(logger, configuration),
metrics.NewAgentMetricsGatherer(logger, configuration))
gatherers_configuration = append(gatherers_configuration, metrics.NewDbMetricsGatherer(logger, configuration))
gatherers_query_optimization = append(gatherers_query_optimization, metrics.NewDbCollectQueriesOptimization(logger, configuration))
metrics.RunWorker(gatherers, gatherers_configuration, gatherers_query_optimization, repeaters, logger, configuration, Mode)
}
// Manage by daemon commands or run the daemon
func (service *Service) Manage(command []string) (string, error) {
usage := "Usage: myservice install | remove | start | stop | status"
// if received any kind of command, do it
if len(command) >= 1 {
switch command[0] {
case "install":
return service.Install()
case "remove":
return service.Remove()
case "start":
return service.Start()
case "stop":
return service.Stop()
case "status":
return service.Status()
default:
return usage, nil
}
}
return service.Run(&Programm{})
// never happen, but need to complete code
}
func defaultConfigPath() string {
switch runtime.GOOS {
case "windows":
return "C:\\ProgramData\\ReleemAgent\\releem.conf"
default: // для Linux и других UNIX-подобных систем
return "/opt/releem/releem.conf"
}
}
func defaultDependencies() []string {
switch runtime.GOOS {
case "windows":
return []string{}
default: // для Linux и других UNIX-подобных систем
return []string{"network.target"}
}
}
func main() {
logger = *logging.Init("releem-agent", true, false, io.Discard)
defer logger.Close()
logging.SetFlags(log.LstdFlags | log.Lshortfile)
defaultPath := defaultConfigPath()
SetConfigRun = flag.Bool("f", false, "Releem agent generate config")
GetConfigRun = flag.Bool("c", false, "Releem agent get config")
ConfigFile = flag.String("config", defaultPath, "Releem agent config")
AgentEvent = flag.String("event", "", "Releem agent type event")
AgentTask = flag.String("task", "", "Releem agent task name")
flag.Parse()
command := flag.Args()
dependencies := defaultDependencies()
srv, err := daemon.New(serviceName, serviceDescription, daemon.SystemDaemon, dependencies...)
if err != nil {
logger.Error("Error: ", err)
os.Exit(1)
}
service := &Service{srv}
status, err := service.Manage(command)
if err != nil {
logger.Info(status, "\nError: ", err)
os.Exit(1)
}
logger.Info(status)
}