diff --git a/front/pluginsCore.php b/front/pluginsCore.php index 03557f6d..7062f396 100755 --- a/front/pluginsCore.php +++ b/front/pluginsCore.php @@ -446,22 +446,21 @@ function initTabs() } // -------------------------------------------------------- -// Data cleanup/purge functionality +// Filter method taht determines if an entry should be shown function shouldBeShown(entry, pluginObj) { if (pluginObj.hasOwnProperty('data_filters')) { + let dataFilters = pluginObj.data_filters; + // Loop through 'data_filters' array for (let i = 0; i < dataFilters.length; i++) { - - // console.log(dataFilters[i]) + compare_field_id = dataFilters[i].compare_field_id; compare_column = dataFilters[i].compare_column; compare_operator = dataFilters[i].compare_operator; compare_js_wrapper = dataFilters[i].compare_js_wrapper; - compare_field_id_value = $(`#${compare_field_id}`).val(); - - + compare_field_id_value = $(`#${compare_field_id}`).val(); if(compare_field_id_value != undefined && compare_field_id_value != '--') { @@ -470,26 +469,18 @@ function shouldBeShown(entry, pluginObj) console.log(compare_column) console.log(compare_operator) console.log(entry[compare_column]) - - // console.log(entry[compare_column]) - // console.log(`"${compare_field_id_value}" ${compare_operator} "${entry[compare_column]}"`) - // console.log(`${compare_js_wrapper.replace('{value}', `"${compare_field_id_value}"`)} ${compare_operator} ${compare_js_wrapper.replace('{value}', `"${entry[compare_column]}"`)}`) - // console.log(eval(`${compare_js_wrapper.replace('{value}', `"${compare_field_id_value}"`)} ${compare_operator} ${compare_js_wrapper.replace('{value}', `"${entry[compare_column]}"`)}`)) + + // resolve the left and right part of the comparison let left = compare_js_wrapper.replace('{value}', `"${compare_field_id_value}"`) let right = compare_js_wrapper.replace('{value}', `"${entry[compare_column]}"`) - console.log(`${eval(left)}` + - ` ${compare_operator} ` + - `${eval(right)}`) - result = eval( `${eval(left)}` + ` ${compare_operator} ` + `${eval(right)}` ); - return result; - + return result; } } } diff --git a/pialert/plugins/README.md b/pialert/plugins/README.md index 94a72ee2..1b87af41 100755 --- a/pialert/plugins/README.md +++ b/pialert/plugins/README.md @@ -193,6 +193,29 @@ Required `CMD` setting example with above query (you can set `"type": "label"` i Used to initialize internal settings. Check the `newdev_template` plugin for details. +## Filters + +Plugin entries can be filtered based on values entered into filter fields. The `txtMacFilter` textbox/field contains the Mac address of the currently viewed device or simply a mac address that's available in the `mac` query string. + + | Property | Required | Description | + |----------------------|----------------------|----------------------| + | `compare_column` | yes | Plugin column name that's value is used for comparison (**Left** side of the equation) | + | `compare_operator` | yes | JavaScript comparison operator | + | `compare_field_id` | yes | The `id` of a input text field containing a value is used for comparison (**Right** side of the equation)| + | `compare_js_wrapper` | yes | JavaScript code used to convert left and right side of the equation. `{value}` is replaced with input values. | + + +```json + "data_filters": [ + { + "compare_column" : "Object_PrimaryID", + "compare_operator" : "==", + "compare_field_id": "txtMacFilter", + "compare_js_wrapper": "'{value}.toString()'" + } + ], +``` + ### Mapping the plugin results into a database table PiAlert will take the results of the plugin execution and insert these results into a database table, if a plugin contains the property `"mapped_to_table"` in the `config.json` root. The mapping of the columns is defined in the `database_column_definitions` array. diff --git a/pialert/plugins/dhcp_leases/config.json b/pialert/plugins/dhcp_leases/config.json index a57b483b..2f8466df 100755 --- a/pialert/plugins/dhcp_leases/config.json +++ b/pialert/plugins/dhcp_leases/config.json @@ -3,6 +3,14 @@ "unique_prefix": "DHCPLSS", "enabled": true, "data_source": "python-script", + "data_filters": [ + { + "compare_column" : "Object_PrimaryID", + "compare_operator" : "==", + "compare_field_id": "txtMacFilter", + "compare_js_wrapper": "'{value}.toString()'" + } + ], "localized": ["display_name", "description", "icon"], "mapped_to_table": "DHCP_Leases", "display_name" : [{ diff --git a/pialert/plugins/snmp_discovery/config.json b/pialert/plugins/snmp_discovery/config.json index 61854aaf..534bb07d 100755 --- a/pialert/plugins/snmp_discovery/config.json +++ b/pialert/plugins/snmp_discovery/config.json @@ -2,7 +2,15 @@ "code_name": "snmp_discovery", "unique_prefix": "SNMPDSC", "enabled": true, - "data_source": "python-script", + "data_source": "pyton-script", + "data_filters": [ + { + "compare_column" : "Object_PrimaryID", + "compare_operator" : "==", + "compare_field_id": "txtMacFilter", + "compare_js_wrapper": "'{value}.toString()'" + } + ], "localized": ["display_name", "description", "icon"], "mapped_to_table": "DHCP_Leases", "display_name" : [{ diff --git a/pialert/plugins/unifi_import/config.json b/pialert/plugins/unifi_import/config.json index 0bbe4cc5..96beba17 100755 --- a/pialert/plugins/unifi_import/config.json +++ b/pialert/plugins/unifi_import/config.json @@ -3,6 +3,14 @@ "unique_prefix": "UNFIMP", "enabled": true, "data_source": "python-script", + "data_filters": [ + { + "compare_column" : "Object_PrimaryID", + "compare_operator" : "==", + "compare_field_id": "txtMacFilter", + "compare_js_wrapper": "'{value}.toString()'" + } + ], "localized": ["display_name", "description", "icon"], "mapped_to_table": "DHCP_Leases", "display_name" : [{