Skip to content

Commit 8654991

Browse files
committed
replace python psutil to gopsutil, so do not to install a package on every machine
1 parent 93010ea commit 8654991

File tree

9 files changed

+340
-17
lines changed

9 files changed

+340
-17
lines changed

cmd/sentry_agent/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/sentrycloud/sentry/pkg/agent/httpcollector"
66
"github.com/sentrycloud/sentry/pkg/agent/reporter"
77
"github.com/sentrycloud/sentry/pkg/agent/script"
8+
"github.com/sentrycloud/sentry/pkg/agent/system"
89
"github.com/sentrycloud/sentry/pkg/cmdflags"
910
"github.com/sentrycloud/sentry/pkg/newlog"
1011
"github.com/sentrycloud/sentry/pkg/profile"
@@ -34,6 +35,8 @@ func main() {
3435
script.StartScriptScheduler(s.ScriptPath, s.ScriptType, agentReporter)
3536
}
3637

38+
go system.CollectSystemMetric(agentReporter)
39+
3740
newlog.Info("start complete in %d ms", time.Now().UnixMilli()-startTime)
3841
profile.StartProfileInBlockMode(agentConfig.ProfilePort)
3942
}

configs/create_tables.sql

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ INSERT INTO `alarm_rule` (`name`, `type`, `query_range`, `contacts`, `level`, `m
114114
'{"metric":"sentry_sys_mem_usage", "tags":{"ip":"127.0.0.1"}, "aggregation":"max", "down_sample":10, "compare_type":0, "compare_days_ago":0, "compare_seconds":60}',
115115
'{"less_than": -8.0, "greater_than": 8.0, "error_count": 1}');
116116

117+
/* create dashboard for sentry_server monitor */
117118
INSERT INTO `dashboard` (`name`, `creator`, `app_name`, `chart_layout`)
118119
VALUES ('sentry server monitor', 'eric', 'sentry_server', '[{"w":6,"h":4,"x":0,"y":0,"i":"1","moved":false,"static":true},{"w":6,"h":4,"x":6,"y":0,"i":"2","moved":false,"static":true},{"w":4,"h":4,"x":0,"y":4,"i":"3","moved":false,"static":true},{"w":4,"h":4,"x":4,"y":4,"i":"4","moved":false,"static":true},{"w":4,"h":4,"x":8,"y":4,"i":"5","moved":false,"static":true},{"w":4,"h":4,"x":0,"y":8,"i":"6","moved":false,"static":true},{"w":4,"h":4,"x":4,"y":8,"i":"7","moved":false,"static":true},{"w":4,"h":4,"x":8,"y":8,"i":"8","moved":false,"static":true}]');
119120

@@ -165,3 +166,79 @@ INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
165166

166167
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
167168
VALUES (8, 'chan size', 'sentry_server_chan_size', '{"chan":"*"}', 0);
169+
170+
/* create dashboard for machine monitor (cpu, memory, disk, network ...) */
171+
INSERT INTO `dashboard` (`name`, `creator`, `app_name`, `chart_layout`)
172+
VALUES ('machine monitor', 'eric', 'sentry_agent', '[{"w":4,"h":4,"x":0,"y":0,"i":"9","moved":false,"static":true},{"w":4,"h":4,"x":4,"y":0,"i":"10","moved":false,"static":true},{"w":4,"h":4,"x":8,"y":0,"i":"11","moved":false,"static":true},{"w":4,"h":4,"x":0,"y":4,"i":"12","moved":false,"static":true},{"w":4,"h":4,"x":4,"y":4,"i":"13","moved":false,"static":true},{"w":4,"h":4,"x":8,"y":4,"i":"14","moved":false,"static":true},{"w":4,"h":4,"x":0,"y":8,"i":"15","moved":false,"static":true},{"w":4,"h":4,"x":4,"y":8,"i":"16","moved":false,"static":true},{"w":4,"h":4,"x":8,"y":8,"i":"17","moved":false,"static":true},{"w":6,"h":4,"x":0,"y":12,"i":"18","moved":false,"static":true},{"w":6,"h":4,"x":6,"y":12,"i":"19","moved":false,"static":true}]');
173+
174+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
175+
VALUES (2, 'cpu usage', 'line', 'max', '10s', 10);
176+
177+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
178+
VALUES (9, 'cpu', 'sentry_sys_cpu_usage', '{}', 0);
179+
180+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
181+
VALUES (2, 'load average', 'line', 'max', '10s', 10);
182+
183+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
184+
VALUES (10, 'load', 'sentry_sys_load_average', '{}', 0);
185+
186+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
187+
VALUES (2, 'memory usage', 'line', 'max', '10s', 10);
188+
189+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
190+
VALUES (11, 'memory', 'sentry_sys_mem_usage', '{}', 0);
191+
192+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
193+
VALUES (2, 'disk usage', 'line', 'max', '10s', 10);
194+
195+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
196+
VALUES (12, 'disk usage', 'sentry_sys_disk_usage', '{"device":"*"}', 0);
197+
198+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
199+
VALUES (2, 'disk io wait', 'line', 'max', '10s', 10);
200+
201+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
202+
VALUES (13, 'io wait', 'sentry_sys_io_wait', '{}', 0);
203+
204+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
205+
VALUES (2, 'disk io util', 'line', 'max', '10s', 10);
206+
207+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
208+
VALUES (14, 'io util', 'sentry_sys_io_util', '{}', 0);
209+
210+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
211+
VALUES (2, 'disk io stats', 'line', 'max', '10s', 10);
212+
213+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
214+
VALUES (15, 'read bytes/s', 'sentry_sys_io_read', '{}', 0);
215+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
216+
VALUES (15, 'write bytes/s', 'sentry_sys_io_write', '{}', 0);
217+
218+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
219+
VALUES (2, 'net stats in bytes', 'line', 'max', '10s', 10);
220+
221+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
222+
VALUES (16, 'bytes sent/s', 'sentry_sys_net_bytes_sent', '{}', 0);
223+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
224+
VALUES (16, 'bytes recv/s', 'sentry_sys_net_bytes_recv', '{}', 0);
225+
226+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
227+
VALUES (2, 'net stats in packets', 'line', 'max', '10s', 10);
228+
229+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
230+
VALUES (17, 'packets sent/s', 'sentry_sys_net_packets_sent', '{}', 0);
231+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
232+
VALUES (17, 'packets recv/s', 'sentry_sys_net_packets_recv', '{}', 0);
233+
234+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
235+
VALUES (2, 'tcp status', 'line', 'max', '10s', 10);
236+
237+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
238+
VALUES (18, 'status', 'sentry_sys_tcp_status', '{"status":"*"}', 0);
239+
240+
INSERT INTO `chart` (`dashboard_id`, `name`, `type`, `aggregation`, `down_sample`, `topn_limit`)
241+
VALUES (2, 'process number', 'line', 'max', '10s', 10);
242+
243+
INSERT INTO `line` (`chart_id`, `name`, `metric`, `tags`, `offset`)
244+
VALUES (19, 'number', 'sentry_sys_process_number', '{}', 0);

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,27 @@ require (
77
github.com/buger/jsonparser v1.1.1
88
github.com/json-iterator/go v1.1.12
99
github.com/sentrycloud/sentry-sdk-go v1.1.0
10+
github.com/shirou/gopsutil/v3 v3.23.12
1011
github.com/taosdata/driver-go/v3 v3.5.0
1112
gopkg.in/natefinch/lumberjack.v2 v2.2.1
1213
gorm.io/driver/mysql v1.5.2
1314
gorm.io/gorm v1.25.5
1415
)
1516

1617
require (
18+
github.com/go-ole/go-ole v1.2.6 // indirect
1719
github.com/go-sql-driver/mysql v1.7.0 // indirect
1820
github.com/google/uuid v1.3.0 // indirect
1921
github.com/gorilla/websocket v1.5.0 // indirect
2022
github.com/jinzhu/inflection v1.0.0 // indirect
2123
github.com/jinzhu/now v1.1.5 // indirect
24+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
2225
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
2326
github.com/modern-go/reflect2 v1.0.2 // indirect
27+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
28+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
29+
github.com/tklauser/go-sysconf v0.3.12 // indirect
30+
github.com/tklauser/numcpus v0.6.1 // indirect
31+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
32+
golang.org/x/sys v0.15.0 // indirect
2433
)

go.sum

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx2
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
9+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
810
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
911
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
12+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
13+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
14+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1015
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
1116
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
1217
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -18,25 +23,53 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
1823
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
1924
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
2025
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
26+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
27+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
2128
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
2229
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
2330
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
2431
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
2532
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2633
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
35+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
2736
github.com/sentrycloud/sentry-sdk-go v1.1.0 h1:3tC4u8GhBArdrGWbTHyGNwXRaabv/HY1EKfAYOG9Vwo=
2837
github.com/sentrycloud/sentry-sdk-go v1.1.0/go.mod h1:p+CRnOwPlVae2zkCTK7LGeifBo67zm4VsQmcJoPSvvM=
38+
github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4=
39+
github.com/shirou/gopsutil/v3 v3.23.12/go.mod h1:1FrWgea594Jp7qmjHUUPlJDTPgcsb9mGnXDxavtikzM=
40+
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
41+
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
42+
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
2943
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
44+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
45+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
3046
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3147
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
3248
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
49+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
50+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
51+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
3352
github.com/taosdata/driver-go/v3 v3.5.0 h1:30crN+E+ACURmq28kn3Y8B3jfL5knaC1fc1rLvgyXqs=
3453
github.com/taosdata/driver-go/v3 v3.5.0/go.mod h1:H2vo/At+rOPY1aMzUV9P49SVX7NlXb3LAbKw+MCLrmU=
54+
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
55+
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
56+
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
57+
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
58+
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
59+
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
60+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
61+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
62+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
64+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
65+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
66+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3567
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3668
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
3769
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
3870
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
3971
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
72+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4073
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
4174
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
4275
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=

pkg/agent/reporter/reporter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ func (r *Reporter) processMetrics(metrics []protocol.MetricValue) {
8383
continue
8484
}
8585

86+
if metric.Tags == nil {
87+
metric.Tags = make(map[string]string)
88+
}
89+
8690
_, exist := metric.Tags["sentryIP"]
8791
if !exist {
8892
metric.Tags["sentryIP"] = r.localIP // if tags not contain sentryIP, agent add local ip to tags

0 commit comments

Comments
 (0)