forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
90 lines (85 loc) · 2.27 KB
/
echo.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
// Copyright 2015 Eleme Inc. All rights reserved.
// Example alerter command to echo message to console.
//
// An alerter command is some command or script to be called by alerter on
// anomalies found.
//
// Command Line Call
//
// This command will be called by alerter with a JSON-formatted argument:
//
// $ ./echo <JSON-string>
//
// JSON Argument
//
// JSON argument example
// {
// "project": {"name": "foo"},
// "user": {
// "name": "jack",
// "email": "jack@gmail.com",
// "enableEmail": true,
// "enablePhone": true,
// "phone": "18735121212"
// },
// "metric": {
// "name": "timer.count_ps.api",
// "score": 1.02,
// "stamp": 1452494901,
// "value": 139.1
// },
// "rule": {
// "pattern": "timer.mean_90.note.*",
// "comment": "service note get api"
// }
// }
//
// Alerter Command
//
// An alerter command/script should be like this (pseudo code):
//
// // Parse command line arguments#1 json string
// s = argv[1]
// data = loadJSON(s)
//
// if data['user']['enableEmail'] then
// // Send email.
// sendEmail(data['user']['email'], data['metric'])
// endif
// if data['user']['enablePhone'] then
// // Send sms.
// sendPhone(data['user']['phone'], data['metric'])
// endif
//
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/eleme/banshee/models"
)
type msg struct {
Project *models.Project `json:"project"`
Metric *models.Metric `json:"metric"`
User *models.User `json:"user"`
}
func main() {
// Parse json from command line argv#1
str := os.Args[1]
res := msg{}
json.Unmarshal([]byte(str), &res)
// Email
if res.User.EnableEmail {
fmt.Printf("Alert metric - project:%s metric:%s value:%.3f average:%.3f score:%.3f timestamp:%d\n",
res.Project.Name, res.Metric.Name, res.Metric.Value, res.Metric.Average, res.Metric.Score,
res.Metric.Stamp)
fmt.Printf("User info - name:%s email:%s\n", res.User.Name, res.User.Email)
}
// Phone
if res.User.EnablePhone {
fmt.Printf("Alert metric - project:%s metric:%s value:%.3f average:%.3f score:%.3f timestamp:%d\n",
res.Project.Name, res.Metric.Name, res.Metric.Value, res.Metric.Average, res.Metric.Score,
res.Metric.Stamp)
fmt.Printf("User info - name:%s phone:%s\n", res.User.Name, res.User.Phone)
}
}