Welcome
Welcome to refracta

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

[Solved, sort of..] function check_space

Refracta Development, Scripts, etc.

Re: function check_space - Please test

Postby thwak » Mon Oct 14, 2013 9:38 pm

fsmithred wrote:output so that the columns line up better

short anwser, found at stackexchange:
df -Ph | column -t

longer propellerhead awk-til-ya-drop approach described here:
http://www.unix.com/aix/19979-df-output ... igned.html

Aside from all else, alignment depends whether a monospaced font is displayed when the dialog is rendered?
thwak
 
Posts: 165
Joined: Tue Nov 20, 2012 3:58 am

Re: function check_space - Please test

Postby fsmithred » Tue Oct 15, 2013 2:38 pm

Well, no matter what I do to make it look right in a terminal, zenity and yad screw up the formatting. I don't know how to change the fonts in either of those, so I'll probably go with the extra tabs and spaces that make it look pretty close.

For the extra space at the bottom of the zenity window that goes off-screen, adding "--no-wrap" works to make the window shorter and wider, and then breaking the long first line of text into two shorter lines decreases the width. I think it'll work ok like this. Height and width don't need to be specified.

This one uses zenity:
Code: Select all
#!/usr/bin/env bash

# if yad is installed, use in preference
#if [[ -f /usr/bin/yad ]]; then

   DIALOG="yad"
   INFO="image=gtk-dialog-info"
   QUESTION="image=gtk-dialog-question"
   WARNING="image=gtk-dialog-warning"
   ERROR="image=gtk-dialog-error"
   
   #buttons
   BUTTON0="button"
   BUTTON1="button"
   BUTTON0NUM=":0"
   BUTTON1NUM=":1"

#cancel button always returns 1 as $?
#ok button always returns 0 as $?
#ok is default (highlighted)
#buttons in yad dialog window may show reversed from zenity window, e.g.
#yad: ok -- cancel (0 -- 1)
#zenity: cancel -- ok (1 -- 0)

#el
if [[ -f /usr/bin/zenity ]]; then

   # use zenity
   
   DIALOG="zenity"
   INFO="info"
   QUESTION="question"
   WARNING="warning"
   ERROR="error"
   
   #buttons
   BUTTON0="ok-label"
   BUTTON1="cancel-label"
   BUTTON0NUM=""
   BUTTON1NUM=""

else

   xterm -fa monaco -fs 12 -hold -e echo "
  Neither Yad nor Zenity is installed. You can't run the GUI version of
  Refracta Installer without one of those. Instead, you can run
  'refractainstaller' from a terminal or console for the CLI version.
  "
fi


# Check disk space on mounted filesystems.
check_space () {
   
disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 | awk '{ print "  " $2 "\t" $3 "\t" $4 "\t" $5 "  \t" $6 "\t\t\t" $1 }' | tee >($DIALOG --title="Checking disk space..." --progress --pulsate --auto-close --width 300) ;)
}

# Put information in a zenity or yad window to show current settings and disk space
report_space () {
$DIALOG --$QUESTION --title="Disk Space and Settings Report" --${BUTTON0}="Create Snapshot"${BUTTON0NUM} \
     --${BUTTON1}="Exit"${BUTTON1NUM} --no-wrap \
     --text "Please CLOSE any running applications NOW.

You will need plenty of free space. It is recommended that free space (Avail) in the partition that
holds the work directory (probably \"/\") should be two times the total installed system size (Used).
(Note: You can deduct the space taken up by previous snapshots and any saved copies of the system from the Used amount.)

* You have $snapshot_count snapshots taking up $snapshot_size of disk space.
$saved_copy
$save_message
* The snapshot directory is currently set to $snapshot_dir
$tmp_warning

You can change these and other settings by editing
$configfile.


Current disk usage:
(For complete listing, exit and run 'df -h')

$disk_space

Here's an extra line of text.
"


if [ $? -ne 0 ]; then
    exit 0
fi
}


check_space
report_space

echo "Done! "


More: I did find a way to change the fonts in the windows by tweaking ~/.gtkrc-2.0, but I'm not going to mess with that. Found it here -
http://unix.stackexchange.com/questions ... -workaroun

Thanks to everyone for ideas and testing.
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Re: [Solved, sort of..] function check_space - Please test

Postby thwak » Wed Oct 16, 2013 9:44 pm

https://groups.google.com/forum/#!topic ... nuLybExCVo
recent yad versions support specifying a fontname for use within a dialog window

Not bulletproof b/c the script doesn't firmly depend on yad (accommodates fallback to zenity)
and presumes the user's system will correctly substitute another _monospaced_ font if the specified
e.g. "Droid Sans mono" monospaced font is not installed
thwak
 
Posts: 165
Joined: Tue Nov 20, 2012 3:58 am

Re: [Solved, sort of..] function check_space - Please test

Postby dzz » Mon Oct 21, 2013 11:25 am

Is it that important to change the column order? Simplifying things a little, I found that this displays perfectly:

Code: Select all
disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660|sed 's/Mounted on/Mountpoint/'|column -t )


Also where the script calls $disk_space as text, change to:
Code: Select all
\n$disk_space


Only tested so far using zenity on my Squeeze laptop, will try later on another system
dzz
 
Posts: 629
Joined: Wed Apr 27, 2011 11:53 am
Location: Devon, England

Re: [Solved, sort of..] function check_space - Please test

Postby fsmithred » Mon Oct 21, 2013 1:07 pm

dzz wrote:Is it that important to change the column order? Simplifying things a little, I found that this displays perfectly:

Code: Select all
disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660|sed 's/Mounted on/Mountpoint/'|column -t )


Also where the script calls $disk_space as text, change to:
Code: Select all
\n$disk_space


Only tested so far using zenity on my Squeeze laptop, will try later on another system


The reason I changed the order of the columns is because I have some long paths, like /dev/mapper/sdb3_crypt, and that pushes the rest of the line over to the right, so the columns don't line up right. If the long entry is at the end of the line, then only the last column is crooked.

Your fix doesn't work for me. Columns are all uneven in both yad and zenity.
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Re: [Solved, sort of..] function check_space - Please test

Postby fsmithred » Sat Oct 26, 2013 9:08 pm

Couple more changes coming...

Here it is in 9.0.9-5, but the progress bar doesn't work.
Code: Select all
# Check disk space on mounted /, /home, /media, /mnt, /tmp
check_space () {

disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 | awk '{ print "  " $2 "\t" $3 "\t" $4 "\t" $5 "  \t" $6 "\t\t\t" $1 }' | tee >($DIALOG --title="Checking disk space..." --progress --pulsate --auto-close --width 300) ;)
}


Get rid of everything after the pipe (except the closing parenthesis.)
Move the progress bar down to where check_space is called, around line 367, remove '--auto-close' and kill it before the report window comes up:
Code: Select all
# These functions create the information window

$DIALOG --title="Checking disk space..." --progress --pulsate --width 300
check_copies
check_directories
check_space
kill $(pgrep $DIALOG)
report_space


Also fixed $snapshot_count. This is part of the check_copies function, around line 170. Changed this, which works in terminal but not in a script
Code: Select all
if [[ -d $snapshot_dir ]]; then
   if [[ -e "$snapshot_dir"/*.iso ]] ; then
      snapshot_count=$(ls "$snapshot_dir"/*.iso | wc -l)

To this, which works in the script
Code: Select all
# Check how many snapshots already exist and their total size
if [[ -d $snapshot_dir ]]; then
   if ls "$snapshot_dir"/*.iso > /dev/null ; then
      snapshot_count=$(ls "$snapshot_dir"/*.iso | wc -l)
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Re: [Solved, sort of..] function check_space - Please test

Postby dzz » Sun Oct 27, 2013 2:41 pm

Your fix doesn't work for me. Columns are all uneven in both yad and zenity

It did on my squeeze box (all I had available at the time) but doesn't in wheezy or sid so it's useless..

There is another to display neat columns (df, blkid ..) using gxmessage (a lightweight gtk dialog in official repos, Installed-Size: 208)

Code: Select all
df -h -x tmpfs -x devtmpfs -x iso9660|awk '{print $2 " " $3 " " $4 " " $5 " " $6 " " $1}'|column -t|gxmessage -geometry 480x320 -title "Current Disk Usage" -fn Monospace -file -


It can be used (for example) like this, displaying the actual df output in a separate window:

Code: Select all
#!/usr/bin/env bash

# if yad is installed, use in preference
if [[ -f /usr/bin/yad ]]; then

   DIALOG="yad"
   INFO="image=gtk-dialog-info"
   QUESTION="image=gtk-dialog-question"
   WARNING="image=gtk-dialog-warning"
   ERROR="image=gtk-dialog-error"
   
   #buttons
   BUTTON0="button"
   BUTTON1="button"
   BUTTON0NUM=":0"
   BUTTON1NUM=":1"

#cancel button always returns 1 as $?
#ok button always returns 0 as $?
#ok is default (highlighted)
#buttons in yad dialog window may show reversed from zenity window, e.g.
#yad: ok -- cancel (0 -- 1)
#zenity: cancel -- ok (1 -- 0)

elif [[ -f /usr/bin/zenity ]]; then

   # use zenity
   
   DIALOG="zenity"
   INFO="info"
   QUESTION="question"
   WARNING="warning"
   ERROR="error"
   
   #buttons
   BUTTON0="ok-label"
   BUTTON1="cancel-label"
   BUTTON0NUM=""
   BUTTON1NUM=""

else

   xterm -fa monaco -fs 12 -hold -e echo "
  Neither Yad nor Zenity is installed. You can't run the GUI version of
  Refracta Installer without one of those. Instead, you can run
  'refractainstaller' from a terminal or console for the CLI version.
  "
fi


# Check disk space on mounted filesystems.
check_space () {
   
disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660| sed 's/Mounted on/Mountpoint/' | sed 's/ /\t/'| awk '{ print $2 " " $3 " " $4 " " $5 " " $6 " " $1 }'|column -t)

}

# Put information in a zenity or yad window to show current settings and disk space
report_space () {

# use gxmessage to display disk space with neatly lined columns in a separate window
# gxmessage window will exit after main dialog is answered

df -h -x tmpfs -x devtmpfs -x iso9660|awk '{print $2 " " $3 " " $4 " " $5 " " $6 " " $1}'|column -t|gxmessage -geometry 480x320 -title "Current Disk Usage" -fn Monospace -file -  &

$DIALOG --question --width 480 --height 320 --title="Disk Space and Settings Report" --${BUTTON0}="Create Snapshot"${BUTTON0NUM} \
     --${BUTTON1}="Exit"${BUTTON1NUM} \
     --text "Please CLOSE any running applications NOW and check the \"Current Disk Usage\" window.

You will need plenty of free space. It is recommended that free space (Avail) in the partition that holds the work directory (probably \"/\") should be two times the total installed system size (Used).

You can deduct the space taken up by previous snapshots and any saved copies of the system from the Used amount.

* You have $snapshot_count snapshots taking up $snapshot_size of disk space.
$saved_copy
$save_message

* The snapshot directory is currently set to $snapshot_dir
$tmp_warning

You can change these and other settings by editing
$configfile.

"

if  [ "$?" = "0" ]; then
echo "Creating snapshot.. "
killall gxmessage

else

echo "Script will exit"
killall gxmessage
    exit 0
fi

}

#check_space
report_space

echo "Done! "

dzz
 
Posts: 629
Joined: Wed Apr 27, 2011 11:53 am
Location: Devon, England

Re: [Solved, sort of..] function check_space - Please test

Postby fsmithred » Sun Oct 27, 2013 8:25 pm

Looks nice and neat, but it comes up behind the yad window.
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Re: [Solved, sort of..] function check_space - Please test

Postby fsmithred » Fri Nov 01, 2013 1:43 pm

I uploaded new debs for snapshot. Wanted to get the snapshot_count fix out there. For free space, using the line below for now - it's not too bad. Two spaces before the tabs in awk works well for all but the last column. I'm a little irritated that I can't get printf to do what I want. Also excluded a couple more filesystem types, in case someone is making a snapshot from a running live system. Also considered excluding rootfs, but I didn't.
Code: Select all
disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 -x udf -x aufs -x squashfs | awk '{ print $2 "  \t" $3 "  \t" $4 "  \t" $5 "  \t" $6 "  \t\t\t" $1 }')


Note: for $snapshot_count, the old test that used to work no longer does. Test -e does not like globbing.
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Re: [Solved, sort of..] function check_space - Please test

Postby fsmithred » Thu Nov 07, 2013 2:41 pm

The progress bar for "Checking disk space" does not go away on its own, and you have to OK it to get to the next step, which is the report window. To fix it, add "&" to the end of line 369, like this:
Code: Select all
$DIALOG --title="Checking disk space..." --progress --pulsate --width 300 &
check_copies
check_directories
check_space
kill $(pgrep $DIALOG)
report_space
Oh yeah, the "--auto-close" can be removed from that line, too.
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Previous

Return to Discuss

Who is online

Users browsing this forum: No registered users and 0 guests

suspicion-preferred