undiscoverables initial version

This commit is contained in:
Data-Monkey
2023-05-17 12:55:34 +10:00
parent c54156ca1e
commit 95a7dcc7fc
3 changed files with 277 additions and 0 deletions

View File

@@ -0,0 +1,158 @@
{
"code_name": "undiscoverables",
"unique_prefix": "UNDIS",
"enabled": true,
"data_source": "python-script",
"localized": ["display_name", "description", "icon"],
"display_name": [
{
"language_code": "en_us",
"string": "Un-Discoverable Devices"
}
],
"icon": [
{
"language_code": "en_us",
"string": "<i class=\"fa-solid fa-binoculars\"></i>"
}
],
"description": [
{
"language_code": "en_us",
"string": "This plugin is to import undiscoverable devices from a file."
}
],
"settings": [
{
"function": "RUN",
"type": "selecttext",
"default_value": "once",
"options": [
"disabled",
"once",
"schedule",
"always_after_scan",
"on_new_device"
],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "When to run"
}
],
"description": [
{
"language_code": "en_us",
"string": "Enable import of devices from a file. If you select <code>schedule</code> the scheduling settings from below are applied. If you select <code>once</code> the scan is run only once on start of the application (container) or after you update your settings."
}
]
},
{
"function": "CMD",
"type": "text",
"default_value": "python3 /home/pi/pialert/front/plugins/undiscoverables/script.py",
"options": [],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "Command"
}
],
"description": [
{
"language_code": "en_us",
"string": "Command to run"
}
]
},
{
"function": "RUN_SCHD",
"type": "text",
"default_value": "0 2 * * *",
"options": [],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "Schedule"
}
],
"description": [
{
"language_code": "en_us",
"string": "Only enabled if you select <code>schedule</code> in the <a href=\"#DHCPLSS_RUN\"><code>DHCPLSS_RUN</code> setting</a>. Make sure you enter the schedule in the correct cron-like format (e.g. validate at <a href=\"https://crontab.guru/\" target=\"_blank\">crontab.guru</a>). For example entering <code>0 4 * * *</code> will run the scan after 4 am in the <a onclick=\"toggleAllSettings()\" href=\"#TIMEZONE\"><code>TIMEZONE</code> you set above</a>. Will be run NEXT time the time passes."
}
]
},
{
"function": "RUN_TIMEOUT",
"type": "integer",
"default_value": 5,
"options": [],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "Run timeout"
},
{
"language_code": "de_de",
"string": "Wartezeit"
}
],
"description": [
{
"language_code": "en_us",
"string": "Maximum time in seconds to wait for the script to finish. If this time is exceeded the script is aborted."
}
]
},
{
"function": "WATCH",
"type": "multiselect",
"default_value": ["Watched_Value1", "Watched_Value4"],
"options": [
"Watched_Value1",
"Watched_Value2",
"Watched_Value3",
"Watched_Value4"
],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "Watched"
}
],
"description": [
{
"language_code": "en_us",
"string": "Send a notification if selected values change. Use <code>CTRL + Click</code> to select/deselect. <ul> <li><code>Watched_Value1</code> is Active </li><li><code>Watched_Value2</code> is Hostname </li><li><code>Watched_Value3</code> is hardware </li><li><code>Watched_Value4</code> is State </li></ul>"
}
]
},
{
"function": "REPORT_ON",
"type": "multiselect",
"default_value": ["new", "watched-changed"],
"options": ["new", "watched-changed", "watched-not-changed"],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "Report on"
}
],
"description": [
{
"language_code": "en_us",
"string": "Send a notification only on these statuses. <code>new</code> means a new unique (unique combination of PrimaryId and SecondaryId) object was discovered. <code>watched-changed</code> means that selected <code>Watched_ValueN</code> columns changed."
}
]
}
]
}

View File

@@ -0,0 +1,81 @@
from time import sleep, time, strftime
import sys
import pathlib
# -------------------------------------------------------------------
class Plugin_Object:
def __init__(
self,
primaryId="",
secondaryId="",
watched1="",
watched2="",
watched3="",
watched4="",
extra="",
foreignKey=""
):
self.pluginPref = ""
self.primaryId = primaryId
self.secondaryId = secondaryId
self.created = strftime("%Y-%m-%d %H:%M:%S")
self.changed = ""
self.watched1 = watched1
self.watched2 = watched2
self.watched3 = watched3
self.watched4 = watched4
self.status = ""
self.extra = extra
self.userData = ""
self.foreignKey = foreignKey
def write(self):
line = ("{}|{}|{}|{}|{}|{}|{}|{}|{}\n".format(
self.primaryId,
self.secondaryId,
self.created,
self.watched1,
self.watched2,
self.watched3,
self.watched4,
self.extra,
self.foreignKey
)
)
return line
class Plugin_Objects:
def __init__(self, result_file):
self.result_file = result_file
self.objects = []
def add_object ( self, primaryId="",
secondaryId="",
watched1="",
watched2="",
watched3="",
watched4="",
extra="",
foreignKey="" ):
self.objects.append(Plugin_Object(primaryId,
secondaryId,
watched1,
watched2,
watched3,
watched4,
extra,
foreignKey)
)
def write_result_file(self):
with open(self.result_file, mode='w') as fp:
for obj in self.objects:
fp.write ( obj.write() )
fp.close()

View File

@@ -0,0 +1,38 @@
#!/usr/bin/env python
# Based on the work of https://github.com/leiweibau/Pi.Alert
# python3 /home/pi/pialert/front/plugins/website_monitor/script.py urls=http://google.com,http://bing.com
import sys
import pathlib
from plugin_helper import Plugin_Objects, Plugin_Object
sys.dont_write_bytecode = True
curPath = str(pathlib.Path(__file__).parent.resolve())
log_file = curPath + '/script.log'
result_file = curPath + '\last_result.log'
FAKE_DEVICES = ["routerXX","hubZZ"]
def main():
print("Hello")
devices = Plugin_Objects( result_file )
for fake_dev in FAKE_DEVICES:
devices.add_object(fake_dev, fake_dev, fake_dev, fake_dev, "", "", "", "")
devices.write_result_file()
return devices
#===============================================================================
# BEGIN
#===============================================================================
if __name__ == '__main__':
d = main()