-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from danielringch/release/1.1.0
Release/1.1.0
- Loading branch information
Showing
19 changed files
with
509 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM python:3.11-slim | ||
FROM python:3.9 | ||
|
||
WORKDIR / | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: "my_diskfree" #unique name | ||
check_interval: "0 0 * * *" #cron schedule expression | ||
alert_mute_interval: 24 #hours | ||
drives: | ||
- "/mnt/foo": #optional | ||
minimum_space: "10G" #bytes or percent | ||
delete: "/mnt/foo/*.txt" #optional | ||
- "/mnt/bar": #optional | ||
minimum_space: "5%" #bytes or percent | ||
delete: "/mnt/bar/*.zip" #optional |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# The Xiamon diskfree plugin | ||
|
||
This plugin checks the free space at configured locations and can also delete files based on a pattern if necessary. | ||
This plugin is mainly intented to be used with expendable data which can be removed if more space is needed. E.g. the free space of a drive used by Sia or Storj may be used by Chia plot files. | ||
|
||
## **Configuration template** | ||
|
||
The basic configuration information can be found [here](../config_basics.md). | ||
|
||
```yaml | ||
name: "my_diskfree" #unique name | ||
check_interval: "0 0 * * *" #cron schedule expression | ||
alert_mute_interval: 24 #hours | ||
drives: | ||
- "/mnt/foo": #optional | ||
minimum_space: "10G" #bytes or percent | ||
delete: "/mnt/foo/*.txt" #optional | ||
- "/mnt/bar": #optional | ||
minimum_space: "5%" #bytes or percent | ||
delete: "/mnt/bar/*.zip" #optional | ||
``` | ||
## **Setup** | ||
The locations to check are added as list item to the key **drives**. The path is used as key. | ||
Per location, the **minimum_space** can be configured as either a relative (suffix: %) or an absolute (optional suffix: k, M, G, T, P) value. | ||
If the key **delete** is given, files matching the pattern are deleted until the minimum free space is reached again. | ||
## **Check** | ||
The plugin checks the free space of all configured locations. | ||
If a file gets deleted, a message is sent to the **info** channel. | ||
If the free space of a location falls below its minimum value, an **alert** is sent. | ||
The [execution interval](../config_basics.md) is set by the key **check_interval**. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .diskfree import Diskfree | ||
from .filleddisk import FilledDisk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
from ...core import Plugin, Config | ||
from .filleddisk import FilledDisk | ||
|
||
class Diskfree(Plugin): | ||
|
||
def __init__(self, config, scheduler, outputs): | ||
config_data = Config(config) | ||
name = config_data.get('diskfree', 'name') | ||
super(Diskfree, self).__init__(name, outputs) | ||
self.print(f'Plugin diskfree; name: {name}') | ||
|
||
alert_mute_intervall = config_data.get(24, 'alert_mute_interval') | ||
self.__drives = {} | ||
|
||
for drive_block in config_data.data['drives']: | ||
for path, drive_config in drive_block.items(): | ||
self.__drives[path] = FilledDisk(self, path, drive_config, alert_mute_intervall) | ||
|
||
scheduler.add_job(f'{name}-check', self.check, config_data.get('0 * * * *', 'check_interval')) | ||
|
||
async def check(self): | ||
for drive in self.__drives.values(): | ||
drive.check() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os, shutil, glob | ||
from ...core.conversions import Conversions | ||
from ...core.alert import Alert | ||
|
||
class FilledDisk: | ||
|
||
def __init__(self, plugin, path, config, mute_interval): | ||
|
||
self.__plugin = plugin | ||
self.__path = path | ||
self.__full_alert = Alert(plugin, mute_interval) | ||
raw_minimum_space = config['minimum_space'] | ||
if raw_minimum_space.endswith('%'): | ||
abs_space, _, _ = shutil.disk_usage(self.__path) | ||
self.__min_space = abs_space * float(raw_minimum_space[:-1]) / 100.0 | ||
else: | ||
if raw_minimum_space[-1].isdigit(): | ||
unit = "" | ||
bytes = float(raw_minimum_space) | ||
else: | ||
unit = raw_minimum_space[-1] | ||
bytes = float(raw_minimum_space[:-1]) | ||
self.__min_space = Conversions.to_byte(bytes, unit) | ||
self.__delete = config.setdefault('delete', None) | ||
|
||
def check(self): | ||
while True: | ||
_, _, free_space = shutil.disk_usage(self.__path) | ||
|
||
if free_space >= self.__min_space: | ||
self.__plugin.msg.debug('Free space at {0}: {1[0]:.2f} {1[1]}'.format(self.__path, Conversions.byte_to_auto(free_space))) | ||
self.__full_alert.reset(f'Free space at {self.__path} meets its minimum value again.') | ||
break; | ||
|
||
normalized_free_space = Conversions.byte_to_auto(free_space) | ||
normalized_min_space = Conversions.bit_to_auto(self.__min_space) | ||
low_space_message = 'Low free space at {0}: {1[0]:.2f} {1[1]} required, {2[0]:.2f} {2[1]} available.'.format( | ||
self.__path, | ||
normalized_min_space, | ||
normalized_free_space | ||
) | ||
self.__plugin.msg.debug(low_space_message) | ||
|
||
if not self.__try_delete(): | ||
self.__full_alert.send(low_space_message) | ||
break | ||
|
||
def __try_delete(self): | ||
if self.__delete is None: | ||
return False | ||
candidates = glob.glob(self.__delete) | ||
if len(candidates) == 0: | ||
return False | ||
file_to_delete = candidates[0] | ||
try: | ||
os.remove(file_to_delete) | ||
delete_message = f'Deleted file {file_to_delete}.' | ||
self.__plugin.msg.debug(delete_message) | ||
self.__plugin.msg.info(delete_message) | ||
return True | ||
except: | ||
self.__plugin.msg.error(f'Failed to delete file {file_to_delete}') | ||
return False | ||
|
Oops, something went wrong.