Skip to content

Commit ec366a0

Browse files
committed
Add alarm rule profile monitoring
1 parent 97fc5dc commit ec366a0

4 files changed

Lines changed: 70 additions & 13 deletions

File tree

cmd/prometheus/main.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ func main() {
434434
conntrack.DialWithTracing(),
435435
)
436436

437+
cMonitoring := config.NewCMonitoring()
438+
437439
reloaders := []func(cfg *config.Config) error{
438440
remoteStorage.ApplyConfig,
439441
webHandler.ApplyConfig,
@@ -479,6 +481,7 @@ func main() {
479481
}
480482
files = append(files, fs...)
481483
}
484+
go cMonitoring.JudgeChange(files)
482485
return ruleManager.Update(
483486
time.Duration(cfg.GlobalConfig.EvaluationInterval),
484487
files,
@@ -762,8 +765,9 @@ func main() {
762765
)
763766
}
764767

765-
// 添加配置文件监听器
766-
if cfg.monitorConfig {
768+
{
769+
// 添加配置文件监听器
770+
// if cfg.monitorConfig {
767771
cancel := make(chan struct{})
768772
g.Add(
769773
func() error {
@@ -773,10 +777,6 @@ func main() {
773777
return errors.New("Profile listener creation failed: " + err.Error())
774778
}
775779
defer watch.Close()
776-
//添加要监控的对象,文件或文件夹
777-
if err = watch.Add(cfg.configFile); err != nil {
778-
return errors.New("Failed to listen to profile err: " + err.Error())
779-
}
780780

781781
level.Info(logger).Log("msg", "Profile listening on...")
782782
for {
@@ -797,15 +797,30 @@ func main() {
797797
level.Error(logger).Log("Error reloading config err: " + err.Error())
798798
}
799799

800-
if err = watch.Add(cfg.configFile); err != nil {
801-
return errors.New("Failed to listen to profile err: " + err.Error())
802-
}
803-
804800
}
805801
case err := <-watch.Errors:
806802
{
807803
return errors.New("Listener exits abnormally err: " + err.Error())
808804
}
805+
case <-cMonitoring.Channel:
806+
{
807+
for _, file := range cMonitoring.Removefiles {
808+
if err := watch.Remove(file); err != nil {
809+
return errors.New("File listening removal failed: " + err.Error())
810+
}
811+
}
812+
813+
//添加要监控的对象,文件或文件夹
814+
if err = watch.Add(cfg.configFile); err != nil {
815+
return errors.New("Failed to listen to profile err: " + err.Error())
816+
}
817+
818+
for _, file := range cMonitoring.Rules {
819+
if err := watch.Add(file); err != nil {
820+
return errors.New("Failed to listen to profile err: " + err.Error())
821+
}
822+
}
823+
}
809824
case <-cancel:
810825
{
811826
return errors.New("Listener receives interrupt processing signal")
@@ -817,6 +832,7 @@ func main() {
817832
close(cancel)
818833
},
819834
)
835+
// }
820836
}
821837

822838
if err := g.Run(); err != nil {

cmd/prometheus/prometheus.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ alerting:
99
alertmanagers:
1010
- static_configs:
1111
- targets:
12-
# - alertmanager:9093
12+
# - http://127.0.0.1:9093
1313

1414
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
1515
rule_files:
16-
# - "first_rules.yml"
17-
# - "second_rules.yml"
16+
- "rules/first_rules.yml"
1817

1918
# A scrape configuration containing exactly one endpoint to scrape:
2019
# Here it's Prometheus itself.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
groups:
2+
- name: InstanceException
3+
rules:
4+
- alert: 心跳失败
5+
expr: up == 0
6+
for: 1m
7+
labels:
8+
severity: critical
9+
annotations:
10+
summary: "连通性检测失败,请排查处理"

config/monitoring.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package config
2+
3+
type CMonitoring struct {
4+
Rules []string
5+
Removefiles []string
6+
7+
Channel chan struct{}
8+
}
9+
10+
func NewCMonitoring() *CMonitoring {
11+
return &CMonitoring{Channel: make(chan struct{})}
12+
}
13+
14+
func (c *CMonitoring) JudgeChange(files []string) {
15+
c.Removefiles = []string{}
16+
17+
for _, cfile := range c.Rules {
18+
in := false
19+
for _, file := range files {
20+
if file == cfile {
21+
in = true
22+
break
23+
}
24+
}
25+
if in == false {
26+
c.Removefiles = append(c.Removefiles, cfile)
27+
}
28+
}
29+
c.Rules = files
30+
c.Channel <- struct{}{}
31+
return
32+
}

0 commit comments

Comments
 (0)