-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOneAgent.go
112 lines (100 loc) · 2.95 KB
/
OneAgent.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os/exec"
"strings"
"com.chinatelecom.oneops.exporter/OneAgent/exporter"
node_exporter_main "github.com/chaolihf/node_exporter"
//包引用是包含模块名/路径名/包名
collector "com.chinatelecom.oneops.exporter/OneAgent/pkg"
"github.com/chaolihf/udpgo/lang"
"github.com/containerd/cgroups/v3/cgroup1"
//"github.com/elastic/beats/v7/filebeat/cmd"
//inputs "github.com/elastic/beats/v7/filebeat/input/default-inputs"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
type oneAgentCollector struct {
}
var logger *zap.Logger
var loggerDatas *exporter.LoggerData
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":19172", "The address to listen on for HTTP requests.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
)
func init() {
//初始化插件
logger = lang.InitProductLogger("logs/agent.log", 100, 5, 10)
}
/*
use cgroup to limit process resource,such as cpu,memory
*/
func limitResource() {
shares := uint64(20)
period := uint64(1000000)
quota := int64(200000)
memory := int64(10000000)
swap := int64(1000000000)
control, err := cgroup1.New(cgroup1.StaticPath("/test"), &specs.LinuxResources{
CPU: &specs.LinuxCPU{
Shares: &shares,
Quota: "a,
Period: &period,
},
Memory: &specs.LinuxMemory{
Limit: &memory,
Swap: &swap,
},
})
if err != nil {
logger.Error(err.Error())
} else {
defer control.Delete()
cmd := exec.Command("./mock/mock")
if err := cmd.Start(); err != nil {
fmt.Println("Error:", err)
return
}
if err := control.Add(cgroup1.Process{Pid: cmd.Process.Pid}); err != nil {
logger.Error(err.Error())
}
logger.Info("add")
}
}
func main() {
logger.Info("host collector\n")
node_exporter_main.Main()
flag.Parse()
if !strings.Contains(*listenAddress, ":9172") {
node_exporter_main.Main()
}
if *showVersion {
fmt.Println("version V1.6.1")
return
}
http.HandleFunc(*metricsPath, func(w http.ResponseWriter, r *http.Request) {
registry := prometheus.NewRegistry()
registry.MustRegister(&oneAgentCollector{})
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
})
// prometheus.MustRegister(&hadoopCollector{})
logger.Info("start to listen on " + *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Fatalf("Error starting HTTP server: %s", err)
}
//limitResource()
}
func (oneCollect *oneAgentCollector) Describe(ch chan<- *prometheus.Desc) {
}
func (oneCollector *oneAgentCollector) Collect(ch chan<- prometheus.Metric) {
for _, item := range collector.GetAllCollector() {
item.Update(ch)
}
}