Skip to content

Commit e17e94b

Browse files
authored
Merge pull request #1 from emnavarro02/fix/script
Fix item script
2 parents 9048a74 + 5a081b6 commit e17e94b

File tree

3 files changed

+142
-51
lines changed

3 files changed

+142
-51
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 1.0.0
2+
3+
* Initial Release
4+
5+
BACKWARDS INCOMPATIBILITIES / NOTES:
6+
* Zabbix version 6.2.6
7+
* N2WS CPM Backup version 4.1.1
8+
9+
## 0.0.1
10+
11+
* Non-released version

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ There are no discovery rules in this template.
3232
|---------------------------|---------------------------------------------------|--------|-------------------------|
3333
| Number of errors in CPM | Retrieve the number of error alerts in CPM API | Script | cpm.errors |
3434
| Number of warnings in CPM | Retrieve the number of warining alerts in CPM API | Script | cpm.warning |
35+
| Number of informations in CPM | Retrieve the number of information notifications in CPM API | Script | cpm.info |
3536

3637
### Triggers
3738
There are no triggers in this template.
@@ -40,3 +41,4 @@ There are no triggers in this template.
4041
Please report any issues with the template at https://github.com/emnavarro02/zabbix-script-n2ws-monitoring/issues.
4142

4243
You can also provide feedback, discuss the template or ask for help with it at ZABBIX forums.
44+

template_cpm backup_alerts.yaml

Lines changed: 129 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
zabbix_export:
2-
version: "6.2"
3-
date: "2023-02-02T10:51:24Z"
2+
version: '6.2'
3+
date: '2023-02-24T12:26:54Z'
44
template_groups:
55
- uuid: a571c0d144b14fd4a87a9d9b2aa9fcd6
66
name: Templates/Applications
77
templates:
88
- uuid: 98a83193b39340adaf9c5b8f5de945bc
9-
template: "CPM Backup"
10-
name: "CPM Backup"
9+
template: 'T_App_CPM Backup'
10+
name: 'T_App_CPM Backup'
1111
description: |
1212
This template will add items to monitor the CPM backup tool via its API.
13-
13+
14+
Monitoring has been tested on N2WS CPM Bacup 4.1.1a
15+
1416
MACROS:
1517
* This template adds 2 macros to the hosts: {$API_KEY} and {$CPM_URL}}
1618
!Make sure you update the Macros' values in the associated hosts!
17-
19+
1820
REQUIREMENTS
1921
* Zabbix server must have network connectivity to the CPM backup server
2022
* Zabbix server must have HTTP/S access to the CPM backup server
2123
groups:
2224
- name: Templates/Applications
2325
items:
2426
- uuid: d551af8f9cbf4a37a8e1a5c6a00db24e
25-
name: "Number of errors in CPM"
27+
name: 'Number of errors in CPM'
2628
type: SCRIPT
2729
key: cpm.errors
2830
delay: 1h
29-
trends: "0"
31+
trends: '0'
3032
value_type: TEXT
3133
params: |
3234
try {
@@ -35,65 +37,139 @@ zabbix_export:
3537
api_key = params.api_key;
3638
cpm_url = params.endpoint;
3739
var type = params.count_type;
38-
40+
3941
// Set the data as the api_key
4042
var data = 'api_key=' + api_key;
4143
var alerts = 0;
42-
44+
4345
// Create a new HttpRequest
4446
req = new HttpRequest();
45-
47+
4648
// Add the header
4749
req.addHeader('Accept: application/json; version=1.0');
48-
50+
4951
// Request the access token
5052
auth_response = req.post(cpm_url + '/api/token/obtain/api_key/', data);
51-
53+
5254
// Parses the response to get the access token
5355
json_obj = JSON.parse(auth_response);
5456
access_token = json_obj['access'];
55-
57+
5658
// Add the access token to the headers
5759
req.addHeader('Authorization: Bearer ' + access_token);
58-
60+
5961
// Retrieve the alerts from CPM Backup api
6062
response = req.get(cpm_url + '/api/alerts/');
61-
63+
//return response.length;
64+
6265
// Parse response as JSON and retrieve the list of alerts
6366
alerts_list = JSON.parse(response);
64-
67+
6568
// Check if the list contains any alerts
6669
if (alerts_list.length > 0) {
6770
// If the list contains alerts, iterate through the list
6871
for (i = 0; i < alerts_list.length; i++) {
6972
// Check if the alert contains "error" or "warning"
70-
if (alerts_list[i].toLowerCase().includes(type.toLowerCase())) {
71-
// Adds 1 to the alerts counter
72-
alerts++;
73+
if (alerts_list[i].severity.toLowerCase().includes(type.toLowerCase())) {
74+
// Adds 1 to the alerts counter
75+
alerts++;
7376
}
74-
}
77+
}
7578
}
7679
} catch (error) {
77-
Zabbix.log(4, 'Failed accessing CPM Backup API: ' + error);
80+
Zabbix.log(1, 'Failed accessing CPM Backup API: ' + error);
7881
alerts = -1;
82+
// return error;
7983
}
80-
84+
8185
// Return number of alerts
8286
return alerts;
8387
timeout: 5s
8488
parameters:
8589
- name: api_key
86-
value: "{$API_KEY}"
90+
value: '{$API_KEY}'
8791
- name: endpoint
88-
value: "{$CPM_URL}"
92+
value: '{$CPM_URL}'
8993
- name: count_type
90-
value: error
94+
value: E
95+
- uuid: 0191ed4b836041a383aa87fe84fcbe16
96+
name: 'Number of information in CPM'
97+
type: SCRIPT
98+
key: cpm.info
99+
delay: 1h
100+
trends: '0'
101+
value_type: CHAR
102+
params: |
103+
try {
104+
// Retrieve parameters defined in the Zabbix item
105+
var params = JSON.parse(value);
106+
api_key = params.api_key;
107+
cpm_url = params.endpoint;
108+
var type = params.count_type;
109+
110+
// Set the data as the api_key
111+
var data = 'api_key=' + api_key;
112+
var alerts = 0;
113+
114+
// Create a new HttpRequest
115+
req = new HttpRequest();
116+
117+
// Add the header
118+
req.addHeader('Accept: application/json; version=1.0');
119+
120+
// Request the access token
121+
auth_response = req.post(cpm_url + '/api/token/obtain/api_key/', data);
122+
123+
// Parses the response to get the access token
124+
json_obj = JSON.parse(auth_response);
125+
access_token = json_obj['access'];
126+
127+
// Add the access token to the headers
128+
req.addHeader('Authorization: Bearer ' + access_token);
129+
130+
// Retrieve the alerts from CPM Backup API
131+
response = req.get(cpm_url + '/api/alerts/');
132+
//return response.length;
133+
134+
// Parse response as JSON and retrieve the list of alerts
135+
alerts_list = JSON.parse(response);
136+
137+
// Check if the list contains any alerts
138+
if (alerts_list.length > 0) {
139+
// If the list contains alerts iterate through the list
140+
for (i = 0; i < alerts_list.length; i++) {
141+
// Check if the alert contains an "error" or "warning"
142+
if (alerts_list[i].severity.toLowerCase().includes(type.toLowerCase())) {
143+
// Adds 1 to the alerts counter
144+
alerts++;
145+
}
146+
}
147+
}
148+
} catch (error) {
149+
Zabbix.log(1, 'Failed accessing CPM Backup API: ' + error);
150+
alerts = -1;
151+
// return error;
152+
}
153+
154+
// Return the number of alerts
155+
return alerts;
156+
description: |
157+
Obtain the number of "Information" notifications from CPM Backup API.
158+
159+
Make sure you set the {$API_KEY} and {$CPM_URL} macros accordingly.
160+
parameters:
161+
- name: api_key
162+
value: '{$API_KEY}'
163+
- name: count_type
164+
value: I
165+
- name: endpoint
166+
value: '{$CPM_URL}'
91167
- uuid: 460d3a3e19fc4390a479f68c7c537d68
92-
name: "Number of warnings in CPM"
168+
name: 'Number of warnings in CPM'
93169
type: SCRIPT
94170
key: cpm.warning
95171
delay: 1h
96-
trends: "0"
172+
trends: '0'
97173
value_type: TEXT
98174
params: |
99175
try {
@@ -102,62 +178,64 @@ zabbix_export:
102178
api_key = params.api_key;
103179
cpm_url = params.endpoint;
104180
var type = params.count_type;
105-
181+
106182
// Set the data as the api_key
107183
var data = 'api_key=' + api_key;
108184
var alerts = 0;
109-
185+
110186
// Create a new HttpRequest
111187
req = new HttpRequest();
112-
188+
113189
// Add the header
114190
req.addHeader('Accept: application/json; version=1.0');
115-
191+
116192
// Request the access token
117193
auth_response = req.post(cpm_url + '/api/token/obtain/api_key/', data);
118-
194+
119195
// Parses the response to get the access token
120196
json_obj = JSON.parse(auth_response);
121197
access_token = json_obj['access'];
122-
198+
123199
// Add the access token to the headers
124200
req.addHeader('Authorization: Bearer ' + access_token);
125-
201+
126202
// Retrieve the alerts from CPM Backup api
127203
response = req.get(cpm_url + '/api/alerts/');
128-
204+
//return response.length;
205+
129206
// Parse response as JSON and retrieve the list of alerts
130207
alerts_list = JSON.parse(response);
131-
208+
132209
// Check if the list contains any alerts
133210
if (alerts_list.length > 0) {
134-
// If the list contains alerts, iterates through the list
211+
// If the list contains alerts, iterate through the list
135212
for (i = 0; i < alerts_list.length; i++) {
136213
// Check if the alert contains "error" or "warning"
137-
if (alerts_list[i].toLowerCase().includes(type.toLowerCase())) {
138-
// Adds 1 to the alerts counter
139-
alerts++;
214+
if (alerts_list[i].severity.toLowerCase().includes(type.toLowerCase())) {
215+
// Adds 1 to the alerts counter
216+
alerts++;
140217
}
141-
}
218+
}
142219
}
143220
} catch (error) {
144-
Zabbix.log(4, 'Failed accessing CPM Backup API: ' + error);
221+
Zabbix.log(1, 'Failed accessing CPM Backup API: ' + error);
145222
alerts = -1;
223+
// return error;
146224
}
147-
225+
148226
// Return number of alerts
149227
return alerts;
150228
timeout: 5s
151229
parameters:
152230
- name: api_key
153-
value: "{$API_KEY}"
231+
value: '{$API_KEY}'
154232
- name: count_type
155-
value: warning
233+
value: W
156234
- name: endpoint
157-
value: "{$CPM_URL}"
235+
value: '{$CPM_URL}'
158236
macros:
159-
- macro: "{$API_KEY}"
237+
- macro: '{$API_KEY}'
160238
type: SECRET_TEXT
161-
description: "The CPM Backup API key. Refer to CPM documentation on how to obtain the API key,"
162-
- macro: "{$CPM_URL}"
163-
description: "The CPM Backup endpoint URL (e.g., https://cpmbackup.mydomain.com)"
239+
description: 'The CPM Backup API key. Refer to CPM documentation on how to obtain the API key,'
240+
- macro: '{$CPM_URL}'
241+
description: 'The CPM Backup endpoint URL (e.g., https://backup.mydomain.com)'

0 commit comments

Comments
 (0)