forked from delanohelio/autoscaling-rancher-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grafana.js
182 lines (153 loc) · 5.61 KB
/
grafana.js
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
const defaults = require('superagent-defaults');
var dashboard_json = require('./ScaleMonitoring');
const panel_template = require("./PanelTemplate")
var URL_API_GRAFANA = "http://192.168.99.100:3000/api/";
var TOKEN = "eyJrIjoiNk9DbEQ0YXNHbWRwQ29wTHNuRjU1NG84NjdySldHaW4iLCJuIjoic2NhbGUtc2VydmljZSIsImlkIjoxfQ=="
var WEBHOOK_SERVICE
var NOTIFICATION_CHANNEL_ID
const NOTIFICATION_NAME = "scale-service-notification"
const DASHBOARD_UID = "scaledashboard"
const request = defaults();
request.set("Authorization", "Bearer " + TOKEN);
module.exports = {
setWebhookURL: function (url) {
WEBHOOK_SERVICE = url
},
setGrafanaAPI: function (url) {
URL_API_GRAFANA= url
},
setGrafanaTOKEN: function (t) {
TOKEN= t
},
createAlert: function (service, metric, threshould, callback, error) {
checkNotificationChannel((r => {
checkDashboard((r2 => {
var new_dashboard = JSON.parse(JSON.stringify(r2))
var panels = new_dashboard["panels"];
if(panels == undefined) {
new_dashboard["panels"] = [];
panels = new_dashboard["panels"];
}
var new_panel = createPanelAndAlert(service, metric, threshould)
new_panel.id = panels.length + 1
panels.push(new_panel);
createOrUpdateDashboard(new_dashboard, callback, error)
}))
}));
},
createAlerts: function (alerts, callback, error) {
checkNotificationChannel((r => {
checkDashboard((r2 => {
var new_dashboard = JSON.parse(JSON.stringify(r2))
var panels = new_dashboard["panels"];
if(panels == undefined) {
new_dashboard["panels"] = [];
panels = new_dashboard["panels"];
}
for(var i in alerts){
var alert = alerts[i]
var new_panel = createPanelAndAlert(alert.service, alert.metric, alert.threshould)
new_panel.id = panels.length + 1
panels.push(new_panel);
}
createOrUpdateDashboard(new_dashboard, callback, error)
}))
}));
},
getAlerts: function (callback) {
checkDashboard((result) => {
alerts = []
for (var i in result.panels){
var panel = result.panels[i]
alerts.push({"service": panel.alert.name, "metric": panel.targets[0].measurement, "threshould": panel.thresholds[0].value})
}
if(callback){callback(alerts)}
})
}
};
var getDashboard = function (callback, error) {
request.get(URL_API_GRAFANA + "dashboards/uid/" + DASHBOARD_UID).then((result) => {
dashboard_json = result.body.dashboard
if(callback){callback(dashboard_json)}
}).catch(err => {
if(error){error(err)}
});
};
var createOrUpdateDashboard = function (dashboard,callback, error) {
request.post(URL_API_GRAFANA + "dashboards/db").send({"dashboard": dashboard}).then((result) => {
dashboard_json = result.body
if(callback){callback(dashboard_json)}
}).catch(err => {
if(error) {error(err)}
});
};
var checkDashboard = function (callback) {
if(dashboard_json.id == undefined) {
getDashboard(callback, (error) => {
dashboard_json["uid"] = DASHBOARD_UID
createOrUpdateDashboard(dashboard_json, callback)
});
}else{
callback(dashboard_json)
}
};
var getNotificationChannel = function (callback) {
request.get(URL_API_GRAFANA + "alert-notifications").then((result) => {
for (i in result.body){
var notification = result.body[i]
if(notification.name == NOTIFICATION_NAME){
NOTIFICATION_CHANNEL_ID = notification.id;
break
}
}
if(callback) {
callback(NOTIFICATION_CHANNEL_ID)
}
});
};
var createNotificationChannel = function (callback) {
var notification = {
"name": NOTIFICATION_NAME, //Required
"type": "webhook", //Required
"disableResolveMessage": true,
"settings": {
"autoResolve": true,
"httpMethod": 'POST',
"uploadImage": false,
"url": WEBHOOK_SERVICE }
};
request.post(URL_API_GRAFANA + "alert-notifications").send(notification).then((result) => {
NOTIFICATION_CHANNEL_ID = result.body.id;
if(callback) {
callback(NOTIFICATION_CHANNEL_ID)
}
});
};
var checkNotificationChannel = function (callback) {
if(NOTIFICATION_CHANNEL_ID == undefined) {
getNotificationChannel((result) => {
if (result == undefined) {
createNotificationChannel(callback)
} else {
if (callback) {
callback(result)
}
}
});
}else{
callback(NOTIFICATION_CHANNEL_ID)
}
};
var createPanelAndAlert = function (service_name, metric,threshould, ) {
var new_panel = JSON.parse(JSON.stringify(panel_template))
var title = metric + "(" + service_name + ")"
new_panel.alert.conditions[0].evaluator.params = [threshould]
new_panel.alert.name = service_name
new_panel.alert.notifications[0]["id"] = NOTIFICATION_CHANNEL_ID
new_panel.targets[0].alias = title
new_panel.targets[0].measurement = metric
new_panel.targets[0].tags[0].value = service_name
new_panel.thresholds[0].value = threshould
new_panel.title = title
return new_panel;
};