forked from torgrimt/zabbix-ntfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbx_ntfy.yaml
201 lines (184 loc) · 7.92 KB
/
zbx_ntfy.yaml
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
zabbix_export:
version: '7.0'
media_types:
- name: ntfy.sh
type: WEBHOOK
parameters:
- name: Message
value: '{ALERT.MESSAGE}'
- name: Nseverity
value: '{EVENT.NSEVERITY}'
- name: Password
value: '{$NTFY.PASS}'
- name: Priority_average
value: min
- name: Priority_disaster
value: default
- name: Priority_high
value: default
- name: Priority_information
value: min
- name: Priority_not_classified
value: min
- name: Priority_resolved
value: min
- name: Priority_warning
value: min
- name: Subject
value: '{ALERT.SUBJECT}'
- name: Token
value: '{$NTFY.TOKEN}'
- name: Topic
value: '{$ALERT.SENDTO}'
- name: URL
value: '{$NTFY.URL}'
- name: Username
value: '{$NTFY.USER}'
- name: ZabbixURL
value: '{$ZABBIX.URL}'
script: |
try {
Zabbix.log(4, '[ ntfy Webhook ] Executed with params: ' + value);
var params = JSON.parse(value);
severities = [
{name: 'not_classified'},
{name: 'information'},
{name: 'warning'},
{name: 'average'},
{name: 'high'},
{name: 'disaster'}
];
// Check if a value is an int for input validation
function isInt(value) {
return !isNaN(value) &&
parseInt(Number(value)) == value &&
!isNaN(parseInt(value, 10));
}
// Input validation
if (!params.URL) {
throw 'Cannot get ntfy url!';
}
if (!isInt(params.Nseverity) || params.Nseverity < 0 || params.Nseverity > 5){
throw 'Nseverity value must be passed as an int from 0-5';
}
var response, request = new HttpRequest();
// Check for proxy
if (typeof params.HTTPProxy === 'string' && params.HTTPProxy.trim() !== '') {
request.setProxy(params.HTTPProxy);
}
if (params.Subject && params.Subject !== '') {
request.addHeader('Title: ' + params.Subject);
} else {
request.addHeader('Title: Zabbix');
}
// Determine Priority, or just use 'default' if not chosen by user
// Priority defined by: https://docs.ntfy.sh/publish/#message-priority
priority = params['Priority_' + severities[params.Nseverity].name] || 'default';
if (priority !== 'min' && priority !== 'low' && priority !== 'default' && priority !== 'high' && priority !== 'max' && priority !== 'urgent'){
throw '"priority" should be min, low, default, high, max, or urgent';
}
request.addHeader('X-Priority: ' + priority);
if (params.Token && params.Token !== '') {
request.addHeader('Authorization: Bearer ' + params.Token);
} else if (params.Username && params.Password && params.Username !== '' && params.Password !== '') {
request.setHttpAuth(HTTPAUTH_BASIC, params.Username, params.Password);
}
if (params.ZabbixURL) {
request.addHeader('Actions: view, Open Zabbix, ' + params.ZabbixURL);
}
// Icon path may change, but I'm not sure of a better location
request.addHeader('Icon: https://git.zabbix.com/projects/ZBX/repos/zabbix/raw/ui/assets/img/touch-icon-192x192.png?at=refs%2Fheads%2Fmaster')
//if a topic is provided, include this in the URL, we assume that the Macro would just be the server, not including a topic
if (params.Topic && params.Topic !== '') {
params.URL = params.URL + '/' + params.Topic;
}
response = request.post(params.URL, params.Message);
return true;
}
catch (error) {
Zabbix.log(3, '[ ntfy webhook ] notification failed: ' + error);
throw 'Sending failed: ' + error;
}
message_templates:
- event_source: TRIGGERS
operation_mode: PROBLEM
subject: 'Problem: {EVENT.NAME}'
message: |
Problem started at {EVENT.TIME} on {EVENT.DATE}
Problem name: {EVENT.NAME}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Operational data: {EVENT.OPDATA}
Original problem ID: {EVENT.ID}
{TRIGGER.URL}
- event_source: TRIGGERS
operation_mode: RECOVERY
subject: 'Resolved in {EVENT.DURATION}: {EVENT.NAME}'
message: |
Problem has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}
Problem name: {EVENT.NAME}
Problem duration: {EVENT.DURATION}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Original problem ID: {EVENT.ID}
{TRIGGER.URL}
- event_source: TRIGGERS
operation_mode: UPDATE
subject: 'Updated problem in {EVENT.AGE}: {EVENT.NAME}'
message: |
{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.
{EVENT.UPDATE.MESSAGE}
Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}.
- event_source: DISCOVERY
operation_mode: PROBLEM
subject: 'Discovery: {DISCOVERY.DEVICE.STATUS} {DISCOVERY.DEVICE.IPADDRESS}'
message: |
Discovery rule: {DISCOVERY.RULE.NAME}
Device IP: {DISCOVERY.DEVICE.IPADDRESS}
Device DNS: {DISCOVERY.DEVICE.DNS}
Device status: {DISCOVERY.DEVICE.STATUS}
Device uptime: {DISCOVERY.DEVICE.UPTIME}
Device service name: {DISCOVERY.SERVICE.NAME}
Device service port: {DISCOVERY.SERVICE.PORT}
Device service status: {DISCOVERY.SERVICE.STATUS}
Device service uptime: {DISCOVERY.SERVICE.UPTIME}
- event_source: AUTOREGISTRATION
operation_mode: PROBLEM
subject: 'Autoregistration: {HOST.HOST}'
message: |
Host name: {HOST.HOST}
Host IP: {HOST.IP}
Agent port: {HOST.PORT}
- event_source: INTERNAL
operation_mode: PROBLEM
- event_source: INTERNAL
operation_mode: RECOVERY
- event_source: SERVICE
operation_mode: PROBLEM
subject: 'Service "{SERVICE.NAME}" problem: {EVENT.NAME}'
message: |
Service problem started at {EVENT.TIME} on {EVENT.DATE}
Service problem name: {EVENT.NAME}
Service: {SERVICE.NAME}
Severity: {EVENT.SEVERITY}
Original problem ID: {EVENT.ID}
Service description: {SERVICE.DESCRIPTION}
{SERVICE.ROOTCAUSE}
- event_source: SERVICE
operation_mode: RECOVERY
subject: 'Service "{SERVICE.NAME}" resolved in {EVENT.DURATION}: {EVENT.NAME}'
message: |
Service "{SERVICE.NAME}" has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}
Problem name: {EVENT.NAME}
Problem duration: {EVENT.DURATION}
Severity: {EVENT.SEVERITY}
Original problem ID: {EVENT.ID}
Service description: {SERVICE.DESCRIPTION}
- event_source: SERVICE
operation_mode: UPDATE
subject: 'Changed "{SERVICE.NAME}" service status to {EVENT.UPDATE.SEVERITY} in {EVENT.AGE}'
message: |
Changed "{SERVICE.NAME}" service status to {EVENT.UPDATE.SEVERITY} at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.
Current problem age is {EVENT.AGE}.
Service description: {SERVICE.DESCRIPTION}
{SERVICE.ROOTCAUSE}