Skip to content

Commit c91e428

Browse files
committed
Cleanup and MAINT plugin v0.1 🔌
1 parent ee35f35 commit c91e428

File tree

10 files changed

+257
-13
lines changed

10 files changed

+257
-13
lines changed

config/.gitignore

100644100755
File mode changed.

db/.gitignore

100644100755
File mode changed.

dockerfiles/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ docker run -d --rm --network=host \
3535
| Variable | Description | Default |
3636
| :------------- |:-------------| -----:|
3737
| `PORT` |Port of the web interface | `20211` |
38-
| `LISTEN_ADDR` |Set the specific IP Address for the web interface | `0.0.0.0` |
38+
| `LISTEN_ADDR` |Set the specific IP Address for the listener address for the nginx webserver (web interface). This could be useful when using multiple subnets to hide the web interface from all untrusted networks. | `0.0.0.0` |
3939
|`TZ` |Time zone to display stats correctly. Find your time zone [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) | `Europe/Berlin` |
4040
|`HOST_USER_GID` |User ID (UID) to map the user in the container to a server user with sufficient read&write permissions on the mapped files | `1000` |
4141
|`HOST_USER_ID` |User Group ID (GID) to map the user group in the container to a server user group with sufficient read&write permissions on the mapped files | `1000` |

docs/HW_INSTALL.md

100644100755
File mode changed.

front/plugins/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
| | | DHCPSRVS | Script | ♻ other | 📚[dhcp_servers](/front/plugins/dhcp_servers/) |
2626
| | Yes | INTRNT | Script | 🔍dev scanner | 📚[internet_ip](/front/plugins/internet_ip/) |
2727
| | | INTRSPD | Script | ♻ other | 📚[internet_speedtest](/front/plugins/internet_speedtest/) |
28-
| Yes | | NEWDEV | Template | ⚙ system | 📚[newdev_template](/front/plugins/newdev_template/) |
29-
| | | PHOLUS | Script | ♻ other | 📚[pholus_scan](/front/plugins/pholus_scan/) |
28+
| | | MAINT | Script | ⚙ system | 📚[maintenance](/front/plugins/maintenance/) |
3029
| | | MQTT | Script | 💬 publisher | 📚[_publisher_mqtt](/front/plugins/_publisher_mqtt/) |
30+
| Yes | | NEWDEV | Template | ⚙ system | 📚[newdev_template](/front/plugins/newdev_template/) |
3131
| | | NMAP | Script | ♻ other | 📚[nmap_scan](/front/plugins/nmap_scan/) |
3232
| | | NTFY | Script | 💬 publisher | 📚[_publisher_ntfy](/front/plugins/_publisher_ntfy/) |
33+
| | | PHOLUS | Script | ♻ other | 📚[pholus_scan](/front/plugins/pholus_scan/) |
3334
| | Yes | PIHOLE | External SQLite DB | 🔍dev scanner | 📚[pihole_scan](/front/plugins/pihole_scan/) |
3435
| | | PUSHSAFER | Script | 💬 publisher | 📚[_publisher_pushsafer](/front/plugins/_publisher_pushsafer/) |
3536
| | | SETPWD | Script | ⚙ system | 📚[set_password](/front/plugins/set_password/) |

front/plugins/_publisher_email/email_smtp.py

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import socket
1616
import ssl
1717

18-
# Replace these paths with the actual paths to your Pi.Alert directories
1918
sys.path.extend(["/home/pi/pialert/front/plugins", "/home/pi/pialert/pialert"])
2019

2120
# PiAlert modules

front/plugins/csv_backup/config.json

+1-9
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,7 @@
2525
{
2626
"language_code": "en_us",
2727
"string": "<i class=\"fa-solid fa-save\"></i>"
28-
},
29-
{
30-
"language_code": "es_es",
31-
"string": "<i class=\"fa-solid fa-save\"></i>"
32-
},
33-
{
34-
"language_code": "de_de",
35-
"string": "<i class=\"fa-solid fa-save\"></i>"
36-
}
28+
}
3729
],
3830
"description": [
3931
{

front/plugins/maintenance/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Overview
2+
3+
A plugin responsible for general maintenance tasks. These currently include:
4+
5+
- pialert.log cleanup
6+
7+
### Usage
8+
9+
- N/A

front/plugins/maintenance/config.json

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"code_name": "maintenance",
3+
"unique_prefix": "MAINT",
4+
"plugin_type": "system",
5+
"enabled": true,
6+
"data_source": "script",
7+
"show_ui": false,
8+
"localized": ["display_name", "description", "icon"],
9+
"display_name": [
10+
{
11+
"language_code": "en_us",
12+
"string": "Maintenance"
13+
}
14+
],
15+
"icon": [
16+
{
17+
"language_code": "en_us",
18+
"string": "<i class=\"fa-solid fa-wrench\"></i>"
19+
}
20+
],
21+
"description": [
22+
{
23+
"language_code": "en_us",
24+
"string": "A plugin for maintenance tasks."
25+
}
26+
],
27+
"params" : [
28+
],
29+
"settings": [
30+
{
31+
"function": "RUN",
32+
"events": ["run"],
33+
"type": "text.select",
34+
"default_value":"schedule",
35+
"options": ["disabled", "once", "schedule", "always_after_scan", "on_new_device"],
36+
"localized": ["name", "description"],
37+
"name" :[{
38+
"language_code":"en_us",
39+
"string" : "When to run"
40+
},
41+
{
42+
"language_code":"es_es",
43+
"string" : "Cuándo ejecutar"
44+
},
45+
{
46+
"language_code":"de_de",
47+
"string" : "Wann laufen"
48+
}],
49+
"description": [{
50+
"language_code":"en_us",
51+
"string" : "When the maintenance tasks should run. A daily or weekly <code>SCHEDULE</code> is a good option."
52+
}]
53+
},
54+
{
55+
"function": "CMD",
56+
"type": "readonly",
57+
"default_value": "python3 /home/pi/pialert/front/plugins/maintenance/maintenance.py",
58+
"options": [],
59+
"localized": ["name", "description"],
60+
"name": [
61+
{
62+
"language_code": "en_us",
63+
"string": "Command"
64+
},
65+
{
66+
"language_code": "es_es",
67+
"string": "Comando"
68+
},
69+
{
70+
"language_code": "de_de",
71+
"string": "Befehl"
72+
}
73+
],
74+
"description": [
75+
{
76+
"language_code": "en_us",
77+
"string": "Command to run. This can not be changed"
78+
},
79+
{
80+
"language_code": "es_es",
81+
"string": "Comando a ejecutar. Esto no se puede cambiar"
82+
},
83+
{
84+
"language_code": "de_de",
85+
"string": "Befehl zum Ausführen. Dies kann nicht geändert werden"
86+
}
87+
]
88+
},
89+
{
90+
"function": "RUN_SCHD",
91+
"type": "text",
92+
"default_value":"0 2 * * 3",
93+
"options": [],
94+
"localized": ["name", "description"],
95+
"name" : [{
96+
"language_code":"en_us",
97+
"string" : "Schedule"
98+
},
99+
{
100+
"language_code":"es_es",
101+
"string" : "Schedule"
102+
},
103+
{
104+
"language_code":"de_de",
105+
"string" : "Schedule"
106+
}],
107+
"description": [{
108+
"language_code":"en_us",
109+
"string" : "Only enabled if you select <code>schedule</code> in the <a href=\"#MAINT_RUN\"><code>MAINT_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."
110+
},
111+
{
112+
"language_code":"es_es",
113+
"string" : "Solo está habilitado si selecciona <code>schedule</code> en la configuración <a href=\"#MAINT_RUN\"><code>MAINT_RUN</code></a>. Asegúrese de ingresar la programación en el formato similar a cron correcto (por ejemplo, valide en <a href=\"https://crontab.guru/\" target=\"_blank\">crontab.guru</a>). Por ejemplo, ingresar <code>0 4 * * *</code> ejecutará el escaneo después de las 4 a.m. en el <a onclick=\"toggleAllSettings()\" href=\"#TIMEZONE\"><code>TIMEZONE</ código> que configuró arriba</a>. Se ejecutará la PRÓXIMA vez que pase el tiempo."
114+
},
115+
{
116+
"language_code":"de_de",
117+
"string" : "Nur aktiviert, wenn Sie <code>schedule</code> in der <a href=\"#CSVBCKP_RUN\"><code>CSVBCKP_RUN</code>-Einstellung</a> auswählen. Stellen Sie sicher, dass Sie den Zeitplan im richtigen Cron-ähnlichen Format eingeben (z. B. validieren unter <a href=\"https://crontab.guru/\" target=\"_blank\">crontab.guru</a>). Wenn Sie beispielsweise <code>0 4 * * *</code> eingeben, wird der Scan nach 4 Uhr morgens in der <a onclick=\"toggleAllSettings()\" href=\"#TIMEZONE\"><code>TIMEZONE</ ausgeführt. Code> den Sie oben festgelegt haben</a>. Wird das NÄCHSTE Mal ausgeführt, wenn die Zeit vergeht."
118+
}]
119+
},
120+
{
121+
"function": "RUN_TIMEOUT",
122+
"type": "integer",
123+
"default_value": 30,
124+
"options": [],
125+
"localized": ["name", "description"],
126+
"name": [
127+
{
128+
"language_code": "en_us",
129+
"string": "Run timeout"
130+
},
131+
{
132+
"language_code": "es_es",
133+
"string": "Tiempo límite de ejecución"
134+
},
135+
{
136+
"language_code": "de_de",
137+
"string": "Zeitüberschreitung"
138+
}
139+
],
140+
"description": [
141+
{
142+
"language_code": "en_us",
143+
"string": "Maximum time in seconds to wait for the script to finish. If this time is exceeded the script is aborted."
144+
},
145+
{
146+
"language_code": "es_es",
147+
"string": "Tiempo máximo en segundos para esperar a que finalice el script. Si se supera este tiempo, el script se cancela."
148+
},
149+
{
150+
"language_code": "de_de",
151+
"string": "Maximale Zeit in Sekunden, die auf den Abschluss des Skripts gewartet werden soll. Bei Überschreitung dieser Zeit wird das Skript abgebrochen."
152+
}
153+
]
154+
},
155+
{
156+
"function": "LOG_LENGTH",
157+
"type": "integer",
158+
"default_value": 250000,
159+
"options": [],
160+
"localized": ["name", "description"],
161+
"name" : [{
162+
"language_code":"en_us",
163+
"string" : "Log length"
164+
}],
165+
"description": [{
166+
"language_code":"en_us",
167+
"string" : "How many last <code>pialert.log</code> lines to keep. If <code>LOG_LEVEL</code> is set to <code>debug</code> the app generates about 10000 lines per hour, so when debugging an issue the recommended setting should cover the bug occurence timeframe. For example for a bug with a 3 day periodical appearence the value <code>1000000</code> should be sufficient. Setting this value to <code>1000000</code> generates approximatelly a 50MB <code>pialert.log</code> file. Set to <code>0</code> disables log cleaning."
168+
}]
169+
}
170+
],
171+
172+
"database_column_definitions":
173+
[
174+
175+
]
176+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
# test script by running:
3+
# /home/pi/pialert/front/plugins/maintenance/maintenance.py
4+
5+
import os
6+
import pathlib
7+
import argparse
8+
import sys
9+
import hashlib
10+
import csv
11+
import sqlite3
12+
from io import StringIO
13+
from datetime import datetime
14+
from collections import deque
15+
16+
sys.path.extend(["/home/pi/pialert/front/plugins", "/home/pi/pialert/pialert"])
17+
18+
# pialert modules
19+
from plugin_helper import Plugin_Object, Plugin_Objects, decodeBase64
20+
from logger import mylog, append_line_to_file
21+
from helper import timeNowTZ, get_setting_value
22+
from const import logPath, pialertPath
23+
24+
25+
CUR_PATH = str(pathlib.Path(__file__).parent.resolve())
26+
LOG_FILE = os.path.join(CUR_PATH, 'script.log')
27+
RESULT_FILE = os.path.join(CUR_PATH, 'last_result.log')
28+
29+
pluginName = 'MAINT'
30+
31+
def main():
32+
33+
mylog('verbose', [f'[{pluginName}] In script'])
34+
35+
MAINT_LOG_LENGTH = int(get_setting_value('MAINT_LOG_LENGTH'))
36+
37+
# Check if set
38+
if MAINT_LOG_LENGTH != 0:
39+
40+
mylog('verbose', [f'[{pluginName}] Cleaning file'])
41+
42+
logFile = logPath + "/pialert.log"
43+
44+
# Using a deque to efficiently keep the last N lines
45+
lines_to_keep = deque(maxlen=MAINT_LOG_LENGTH)
46+
47+
with open(logFile, 'r') as file:
48+
# Read lines from the file and store the last N lines
49+
for line in file:
50+
lines_to_keep.append(line)
51+
52+
with open(logFile, 'w') as file:
53+
# Write the last N lines back to the file
54+
file.writelines(lines_to_keep)
55+
56+
mylog('verbose', [f'[{pluginName}] Cleanup finished'])
57+
58+
59+
60+
return 0
61+
62+
63+
#===============================================================================
64+
# BEGIN
65+
#===============================================================================
66+
if __name__ == '__main__':
67+
main()

0 commit comments

Comments
 (0)