This commit is contained in:
2024-06-12 13:49:42 +02:00
commit b8e15ff09c
390 changed files with 37206 additions and 0 deletions

View File

@@ -0,0 +1,129 @@
# Customizable disk block for i3blocks
Script checks the disk usage of **$BLOCK_INSTANCE** using pythons **os.statvfs** module. If the instance isn't set the home-directory of the executing user will be set as default.
So a standard configuration minding a specific partition would look as follows:
```
[disk_nas]
label=:
instance=/nas
interval=30
command=$SCRIPT_DIR/disk_usage/disk_usage
```
## Arguments
Additionally the script can take more arguments passed to it by command-line in a key=value fashion. The following is supported.
### Warning Threshold
The warning threshold in percent (used disk space).
Display warnig color when 70% of the available disk space is used:
``
warn_threshold=70
``
_Default:_ 80
### Critical Threshold
The critical threshold in percent (used disk space)
Display critical color when 80% of the available disk space is used:
``
crit_threshold=80
``
_Default:_ 90
### Warning Color
Hex-color code to use when **warn_threshold** is reached:
``
warn_color=#90ce00
``
_Default:_ #d6af4e
### Critical Color
Hex-color code to use when **crit_threshold** is reached:
``
crit_color=#ce2500
``
_Default:_ #d64e4e
### Output Format
Output format used by pythons string formatting for displayment.
To get:
```
: 119.3G used of 518.5 total beeing 23.0%
```
the following *format*-argument must be passed to the script:
```
format="{used:.1f}G used of {total:.1f} total beeing {perc_used}%"
```
Quotes are required for format.
#### Fields
* **avail:** available disk space in gigabytes
* **used:** used disk space in gigabytes
* **total:** total diskspace in gigabytes
* **perc_used:** disk usage in percent
## Example
```
[disk_root]
label=:
instance=/
interval=30
command=$SCRIPT_DIR/disk_usage/disk_usage format="{used:.1f}G used of {total:.1f} Total beeing {perc_used}%" warn_color=#90ce00
```
## Click events
Upon a click event a terminal is opened with ncdu running for the configured partition. To adapt this to your personal setup, you'll need to change the function **launch_ncdu()**:
```python
def launch_ncdu(mp):
cmd = [
'/usr/bin/sakura',
'-t',
'pop-up',
'-e',
'/usr/bin/ncdu %s' % mp,
'-x',
]
subprocess.Popen(
cmd,
stdout=open(os.devnull, 'w'),
stderr=subprocess.STDOUT
)
```
Personally, I launch sakura with *pop-up* as title, which i can then use to configure i3 to set the window into floating mode:
```
for_window [title="pop-up"] floating enable border none sticky
```
Output of the launch-command will be redirected to /dev/null
## Dependancies
The script uses only standard python modules and should therefore run on any python environment or should easily be made to run.
## Help
* Advanced String Formatting: https://www.python.org/dev/peps/pep-3101/
## Screenshot
![screenshot][screenshot]
[screenshot]: https://raw.githubusercontent.com/nevious/i3blocks_blocklets/master/disk_usage/screenshot.png

View File

@@ -0,0 +1,123 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# MIT License
# Copyright (c) 2017 Christian Schläppi
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import subprocess
def get_disk_stats(mp):
stat = os.statvfs(mp)
total = stat.f_blocks * stat.f_frsize / 1024 ** 3
avail = stat.f_bavail * stat.f_frsize / 1024 ** 3
used = total - avail
return {
'avail': avail,
'total': total,
'used': used,
'perc_used': 100 * used / total
}
def launch_ncdu(mp):
cmd = [
'/usr/bin/sakura',
'-t',
'pop-up',
'-e',
'/usr/bin/ncdu %s' % mp,
'-x',
]
subprocess.Popen(
cmd,
stdout=open(os.devnull, 'w'),
stderr=subprocess.STDOUT
)
def parse_args():
args = {
'warn_threshold': 80,
'crit_threshold': 90,
'warn_color': '#d6af4e',
'crit_color': '#d64e4e',
'format': '{used:.1f}G/{total:.1f}G ({perc_used:.1f}%) -  {avail:.1f}G'
}
try:
for arg in sys.argv[1:]:
key, value = arg.split('=')
args[key] = int(value) if value.isdigit() else value
except (KeyError, ValueError):
# ValuError in case user does something weird
pass
return args
def get_instance():
p = os.getenv('BLOCK_INSTANCE')
if p and os.path.exists(p):
return p
return os.getenv('HOME')
def main():
output_color = ''
args = parse_args()
m_point = get_instance()
stats = get_disk_stats(m_point)
# get some more info when not called by i3blocks
if not os.getenv('BLOCK_NAME'):
print('Args: %s' % args)
print('Stats: %s' % stats)
print('Mount Point: %s' % m_point)
# print stats with format if given
print(args['format'].format(**stats))
print()
# determine color
if args['crit_threshold'] > int(stats['perc_used']) >= args['warn_threshold']:
output_color = args['warn_color']
elif stats['perc_used'] >= args['crit_threshold']:
output_color = args['crit_color']
print(output_color)
# handle click-event
_button = os.getenv('BLOCK_BUTTON')
if _button and int(_button) == 1:
launch_ncdu(m_point)
if __name__ == '__main__':
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB