forked from rbicelli/pfsense-zabbix-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfsense_zbx.php
539 lines (436 loc) · 16 KB
/
pfsense_zbx.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
/***
pfsense_zbx.php - pfSense Zabbix Interface
Version 0.9.3 - 2020-04-26
Written by Riccardo Bicelli <[email protected]>
This program is licensed under Apache 2.0 License
*/
require_once('globals.inc');
require_once('functions.inc');
require_once('config.inc');
require_once('util.inc');
//For Interfaces Discovery
require_once('interfaces.inc');
//For OpenVPN Discovery
require_once('openvpn.inc');
//For Service Discovery
require_once("service-utils.inc");
//For System
require_once('pkg-utils.inc');
//Testing function, for template creating purpose
function pfz_test(){
$line = "-------------------\n";
$ovpn_servers = pfz_openvpn_get_all_servers();
echo "OPENVPN Servers:\n";
print_r($ovpn_servers);
echo $line;
$ovpn_clients = openvpn_get_active_clients();
echo "OPENVPN Clients:\n";
print_r($ovpn_clients);
echo $line;
$ifdescrs = get_configured_interface_with_descr(true);
$ifaces=array();
foreach ($ifdescrs as $ifdescr => $ifname){
$ifinfo = get_interface_info($ifdescr);
$ifaces[$ifname] = $ifinfo;
}
echo "Network Interfaces:\n";
print_r($ifaces);
print_r(get_interface_arr());
print_r(get_configured_interface_list());
echo $line;
$services = get_services();
echo "Services: \n";
print_r($services);
echo $line;
}
// Interface Discovery
// Improved performance
function pfz_interface_discovery() {
$ifdescrs = get_configured_interface_with_descr(true);
$ifaces = get_interface_arr();
$ifcs=array();
$json_string = '{"data":[';
foreach ($ifdescrs as $ifname => $ifdescr){
$ifinfo = get_interface_info($ifname);
$ifinfo["description"] = $ifdescr;
$ifcs[$ifname] = $ifinfo;
}
foreach ($ifaces as $hwif) {
$json_string .= '{"{#IFNAME}":"' . $hwif . '"';
$ifdescr = $hwif;
foreach($ifcs as $ifc=>$ifinfo){
if ($ifinfo["hwif"] == $hwif){
$ifdescr = $ifinfo["description"];
break;
}
}
$json_string .= ',"{#IFDESCR}":"' . $ifdescr . '"';
$json_string .= '},';
}
$json_string = rtrim($json_string,",");
$json_string .= "]}";
echo $json_string;
}
// OpenVPN Server Discovery
function pfz_openvpn_get_all_servers(){
$servers = openvpn_get_active_servers();
$sk_servers = openvpn_get_active_servers("p2p");
$servers = array_merge($servers,$sk_servers);
return ($servers);
}
function pfz_openvpn_serverdiscovery() {
$servers = pfz_openvpn_get_all_servers();
$json_string = '{"data":[';
foreach ($servers as $server){
$name = trim(preg_replace('/\w{3}(\d)?\:\d{4,5}/i', '', $server['name']));
$json_string .= '{"{#SERVER}":"' . $server['vpnid'] . '"';
$json_string .= ',"{#NAME}":"' . $name . '"';
$json_string .= '},';
}
$json_string = rtrim($json_string,",");
$json_string .= "]}";
echo $json_string;
}
// Get OpenVPN Server Value
function pfz_openvpn_servervalue($server_id,$valuekey){
$servers = pfz_openvpn_get_all_servers();
foreach($servers as $server) {
if($server['vpnid']==$server_id){
$value = $server[$valuekey];
if ($valuekey=="status") {
if ( ($server['mode']=="server_user") || ($server['mode']=="server_tls_user") || ($server['mode']=="server_tls") ){
if ($value=="") $value="server_user_listening";
}
}
}
}
switch ($valuekey){
case "conns":
//Client Connections: is an array so it is sufficient to count elements
if (is_array($value))
$value = count($value);
else
$value = "0";
break;
case "status":
$value = pfz_valuemap("openvpn.server.status", $value);
break;
case "mode":
$value = pfz_valuemap("openvpn.server.mode", $value);
break;
}
//if ($value=="") $value="none";
echo $value;
}
//OpenVPN Server/User-Auth Discovery
function pfz_openvpn_server_userdiscovery(){
$servers = pfz_openvpn_get_all_servers();
$json_string = '{"data":[';
foreach ($servers as $server){
if ( ($server['mode']=='server_user') || ($server['mode']=='server_tls_user') ) {
if (is_array($server['conns'])) {
$name = trim(preg_replace('/\w{3}(\d)?\:\d{4,5}/i', '', $server['name']));
foreach($server['conns'] as $conn) {
$json_string .= '{"{#SERVERID}":"' . $server['vpnid'] . '"';
$json_string .= ',"{#SERVERNAME}":"' . $name . '"';
$json_string .= ',"{#UNIQUEID}":"' . $server['vpnid'] . '+' . $conn['common_name'] . '"';
$json_string .= ',"{#USERID}":"' . $conn['common_name'] . '"';
$json_string .= '},';
}
}
}
}
$json_string = rtrim($json_string,",");
$json_string .= "]}";
echo $json_string;
}
// Get OpenVPN User Connected Value
function pfz_openvpn_server_uservalue($unique_id, $valuekey){
$atpos=strpos($unique_id,'+');
$server_id = substr($unique_id,0,$atpos);
$user_id = substr($unique_id,$atpos+1);
$servers = pfz_openvpn_get_all_servers();
foreach($servers as $server) {
if($server['vpnid']==$server_id) {
foreach($server['conns'] as $conn) {
if ($conn['common_name']==$user_id){
$value = $conn[$valuekey];
}
}
}
}
echo $value;
}
// OpenVPN Client Discovery
function pfz_openvpn_clientdiscovery() {
$clients = openvpn_get_active_clients();
$json_string = '{"data":[';
foreach ($clients as $client){
$name = trim(preg_replace('/\w{3}(\d)?\:\d{4,5}/i', '', $client['name']));
$json_string .= '{"{#CLIENT}":"' . $client['vpnid'] . '"';
$json_string .= ',"{#NAME}":"' . $name . '"';
$json_string .= '},';
}
$json_string = rtrim($json_string,",");
$json_string .= "]}";
echo $json_string;
}
function pfz_openvpn_clientvalue($client_id, $valuekey){
$clients = openvpn_get_active_clients();
foreach($clients as $client) {
if($client['vpnid']==$client_id)
$value = $client[$valuekey];
}
switch ($valuekey){
case "status":
$value = pfz_valuemap("openvpn.client.status", $value);
break;
}
if ($value=="") $value="none";
echo $value;
}
// Services Discovery
// 2020-03-27: Added space replace with __ for issue #12
function pfz_services_discovery(){
$services = get_services();
$json_string = '{"data":[';
foreach ($services as $service){
if (!empty($service['name'])) {
$status = get_service_status($service);
if ($status="") $status = 0;
$id="";
//id for OpenVPN
if (!empty($service['id'])) $id = "." . $service["id"];
//zone for Captive Portal
if (!empty($service['zone'])) $id = "." . $service["zone"];
$json_string .= '{"{#SERVICE}":"' . str_replace(" ", "__", $service['name']) . $id . '"';
$json_string .= ',"{#DESCRIPTION}":"' . $service['description'] . '"';
$json_string .= '},';
}
}
$json_string = rtrim($json_string,",");
$json_string .= "]}";
echo $json_string;
}
// Get service value
// 2020-03-27: Added space replace in service name for issue #12
function pfz_service_value($name,$value){
$services = get_services();
//List of service which are stopped on CARP Slave.
//For now this is the best way i found for filtering out the triggers
//Waiting for a way in Zabbix to use Global Regexp in triggers with items discovery
$stopped_on_carp_slave = array("haproxy","openvpn.","openvpn");
foreach ($services as $service){
$namecfr=str_replace("__"," ",$service["name"]);
$carpcfr=str_replace("__"," ",$service["name"]);
//OpenVPN
if (!empty($service['id'])) {
$namecfr = $service['name'] . "." . $service["id"];
$carpcfr = $service['name'] . ".";
}
//Captive Portal
if (!empty($service['zone'])) {
$namecfr = $service['name'] . "." . $service["zone"];
$carpcfr = $service['name'] . ".";
}
if ($namecfr == $name){
switch ($value) {
case "status":
$status = get_service_status($service);
if ($status=="") $status = 0;
echo $status;
break;
case "name":
echo $namecfr;
break;
case "enabled":
if (is_service_enabled($service['name']))
echo 1;
else
echo 0;
break;
case "run_on_carp_slave":
if (in_array($carpcfr,$stopped_on_carp_slave))
echo 0;
else
echo 1;
default:
echo $service[$value];
break;
}
}
}
}
//Gateway Discovery
function pfz_gw_rawstatus() {
// Return a Raw Gateway Status, useful for action Scripts (e.g. Update Cloudflare DNS config)
$gws = return_gateways_status(true);
$gw_string="";
foreach ($gws as $gw){
$gw_string .= ($gw['name'] . '.' . $gw['status'] .",");
}
echo rtrim($gw_string,",");
}
function pfz_gw_discovery() {
$gws = return_gateways_status(true);
$json_string = '{"data":[';
foreach ($gws as $gw){
$json_string .= '{"{#GATEWAY}":"' . $gw['name'] . '"';
$json_string .= '},';
}
$json_string = rtrim($json_string,",");
$json_string .= "]}";
echo $json_string;
}
function pfz_gw_value($gw, $valuekey) {
$gws = return_gateways_status(true);
if(array_key_exists($gw,$gws))
echo $gws[$gw][$valuekey];
}
function pfz_carp_status(){
//Detect CARP Status
global $config;
$status_return = 0;
$status = get_carp_status();
$carp_detected_problems = get_single_sysctl("net.inet.carp.demotion");
if ($status != 0) { //CARP is enabled
if ($carp_detected_problems != 0) {
echo 4; //There's some Major Problems with CARP
return true;
}
$status_changed = false;
$prev_status = "";
foreach ($config['virtualip']['vip'] as $carp) {
if ($carp['mode'] != "carp") {
continue;
}
$if_status = get_carp_interface_status("_vip{$carp['uniqid']}");
if ( ($prev_status != $if_status) && (empty($if_status)==false) ) { //Some glitches with GUI
if ($prev_status!="") $status_changed = true;
$prev_status = $if_status;
}
}
if ($status_changed) {
//CARP Status is inconsistent across interfaces
echo 3;
} else {
if ($prev_status=="MASTER")
echo 1;
else
echo 2;
}
} else {
//CARP is Disabled
echo 0;
}
}
//System Information
function pfz_get_system_value($section){
switch ($section){
case "version":
echo( get_system_pkg_version()['version']);
break;
case "installed_version":
echo( get_system_pkg_version()['installed_version']);
break;
case "new_version_available":
$pkgver = get_system_pkg_version();
if ($pkgver['version']==$pkgver['installed_version'])
echo "0";
else
echo "1";
break;
}
}
// Value mappings
// Each value map is represented by an associative array
function pfz_valuemap($valuename, $value){
switch ($valuename){
case "openvpn.server.status":
$valuemap = array(
"down" => "0",
"up" => "1",
"none" => "2",
"reconnecting; ping-restart" => "3",
"waiting" => "4",
"server_user_listening" => "5");
break;
case "openvpn.client.status":
$valuemap = array(
"up" => "1",
"down" => "0",
"none" => "0",
"reconnecting; ping-restart" => "2");
break;
case "openvpn.server.mode":
$valuemap = array(
"p2p_tls" => "1",
"p2p_shared_key" => "2",
"server_tls" => "3",
"server_user" => "4",
"server_tls_user" => "5");
break;
}
if (array_key_exists($value, $valuemap))
return $valuemap[$value];
return "0";
}
//Argument parsers for Discovery
function pfz_discovery($section){
switch (strtolower($section)){
case "gw":
pfz_gw_discovery();
break;
case "openvpn_server":
pfz_openvpn_serverdiscovery();
break;
case "openvpn_server_user":
pfz_openvpn_server_userdiscovery();
break;
case "openvpn_client":
pfz_openvpn_clientdiscovery();
break;
case "services":
pfz_services_discovery();
break;
case "interfaces":
pfz_interface_discovery();
break;
}
}
//Main Code
switch (strtolower($argv[1])){
case "discovery":
pfz_discovery($argv[2]);
break;
case "gw_value":
pfz_gw_value($argv[2],$argv[3]);
break;
case "gw_status":
pfz_gw_rawstatus();
break;
case "openvpn_servervalue":
pfz_openvpn_servervalue($argv[2],$argv[3]);
break;
case "openvpn_server_uservalue":
pfz_openvpn_server_uservalue($argv[2],$argv[3]);
break;
case "openvpn_clientvalue":
pfz_openvpn_clientvalue($argv[2],$argv[3]);
break;
case "service_value":
pfz_service_value($argv[2],$argv[3]);
break;
case "carp_status":
pfz_carp_status();
break;
case "if_name":
pfz_get_if_name($argv[2]);
break;
case "system":
pfz_get_system_value($argv[2]);
break;
default:
pfz_test();
}