Self-challenge to report disks like Windows

Reported need:


Challenge to self:

See what I can do, so …

The following is a snapshot of what I was able to come up with:

As can be seen from the end of my script, the intent was to use YAD’s HTML capability to present that as a pop-up, but, my version on UbuntuMATE 22.04 LTS doesn’t seem to work. I do plan to test it on my UbuntuMATE 26.04 test install soon.

So … maybe someone can turn this into an actual Panel App, if the HTML can be easily wrapped into some other tool generator. :slight_smile:


I offer below the SHELL script used to create that HTML page. Admittedly, it is a hack, but I think it is decent enough!

  • I used the supplied image to create my own transparent icon for the “basic” drive.

  • I used the SVG icon artwork for the Ubuntu MATE roundel, so that “conditions” used to validate the OS type, it goes thru a case statement to choose which icon to overlay to represent that OS.

  • I did not incorporate logic for network drives because I don’t have any, but I looked at the tools to do so, and if someone would share a report for some commands to have the output that would be parsed, I could work out that additional logic to handle the case for network-mounted drives as well.


Artwork Ref:


Script:   Devices__ReportDiskStatus.sh

#!/bin/bash

getUsage()
{
	# Get infor for individual disk
	lsblk --output-all --pairs --paths ${drive} | sed 's+["][ ]+"\n+g' >${devDat}

	# Get partition label & path
	#dPath=$(echo "${dDat}" | awk '{ print $1 }' )
	dPath=$(grep '^PATH' ${devDat} | cut -f2 -d\" )

	#dName=$(echo "${dDat}" | awk '{ print $NF }' )
	dName=$(grep '^LABEL' ${devDat} | cut -f2 -d\" )
	if [ "$(df -h ${dPath} | grep '^/dev/sd' | awk '{ print $6 }' )" = "/" ]
	then
		test ${dbg} -eq 1 && echo "root" >&2
		dName="/"
	fi

	# Get disk capacity in GB
	#vals=$(echo "${dDat}" | awk '{ cnt=length($2) ; printf("%s %s\n", substr( $2, 1, cnt-1), substr( $2, cnt) ) ; }' )
	vals=$(grep '^FSSIZE' ${devDat} | cut -f2 -d\" | awk '{ cnt=length($1) ; printf("%s %s\n", substr( $1, 1, cnt-1), substr( $1, cnt) ) ; }' )
	dSiz=$(echo ${vals} | awk '{ print $1 }' )
	dUniS=$(echo ${vals} | awk '{ print $2 }' )

	# Get disk usage as percentage
	#dUse=$(echo "${dDat}" | awk '{print $5}' | tr -d '%')
	dUse=$(grep '^FSUSE%' ${devDat} | cut -f2 -d\" | tr -d '%' )

	# Get free space
	#vals=$(echo "${dDat}" | awk '{ cnt=length($4) ; printf("%s %s\n", substr( $4, 1, cnt-1), substr( $4, cnt) ) }' )
	vals=$(grep '^FSAVAIL' ${devDat} | cut -f2 -d\" | awk '{ cnt=length($1) ; printf("%s %s\n", substr( $1, 1, cnt-1), substr( $1, cnt) ) ; }' )
	dFree=$(echo ${vals} | awk '{ print $1 }' )
	dUniF=$(echo ${vals} | awk '{ print $2 }' )
	#dFree=$(echo "${dDat}" | awk '{print $4}')
}

colourUsage()
{
	# Define color based on usage percentage
	if [ "${dUse}" -lt 50 ]; then
		statColour="green"
	elif [ "${dUse}" -lt 85 ]; then
		statColour="orange"
	else
		statColour="red"
	fi
}


getOpSysType()
{
	test ${dbg} -eq 1 && test -n "${testor}" && { echo ${testor} >&2 ; }
	#/etc/X11/default-display-manager
	#	/usr/sbin/arctica-greeter
	#	/usr/sbin/lightdm
	#	/usr/sbin/lightdm-gtk-greeter
	#	/usr/sbin/slick-greeter
	#
	#	# sudo:x:27:ericthered
	#	# primaryUserID=$( grep '^sudo:' /etc/group | cut -f4 -d":" | cut -f1 -d"," )
	#
	#	# cat ericthered/.dmrc
	#	[Desktop]
	#	Session=mate
	#
	#	# primaryUserSession=$( eval grep '^Session=' ~${primaryUserID}/.dmrc | cut -f2 -d"=" )
	#	mate

	case "${testor}" in
		"mate" )
			#Ubuntu MATE
			flavIcon="file:///home/ericthered/Downloads/Ubuntu__MATE_Roundel.svg"
			#/usr/share/xsessions/mate.desktop
			;;
		"ubuntu" | "gnome" )
			#Ubuntu Desktop
			flavIcon="file:///home/ericthered/Downloads/Ubuntu__Standard_Roundel.svg"
			#/usr/share/wayland-sessions/gnome.desktop
			#/usr/share/xsessions/gnome.desktop
			;;
		"kde-plasma" | "kubuntu" )
			#Kubuntu
			flavIcon=""
			#/usr/share/wayland-sessions/plasma.desktop
			#/usr/share/xsessions/plasma.desktop
			;;
		"Lubuntu*" | "LXDE*" )
			#Lubuntu
			#/usr/share/xsessions/lubuntu.desktop
			#/usr/share/xsessions/lxqt.desktop
			flavIcon=""
			;;
		"xubuntu" )
			#Xubuntu
			flavIcon=""
			#/usr/share/xsessions/xubuntu.desktop
			;;
		"budgie-desktop" )
			#Ubuntu Budgie
			flavIcon=""
			;;
		"cinnamon" )
			#Ubuntu Cinnamon
			flavIcon=""
			;;
		"xfce" )
			#Ubuntu Studio
			flavIcon=""
			;;
		"ukui" )
			#Ubuntu Kylin
			flavIcon=""
			;;
		"unity" )
			#Ubuntu Unity
			flavIcon=""
			;;
		* )
			flavIcon=""
			;;
	esac
	test ${dbg} -eq 1 && test -n "${flavIcon}" && { echo ${flavIcon} >&2 ; }
}


getDriveType()
{
	# Logic to be finalized for optional addition
	# of GUI hint of network-based drive

	#findmnt -l -t nfs,nfs4,cifs,smb3,sshfs
	#TARGET                SOURCE                              FSTYPE OPTIONS
	#/mnt/nas              //192.168.1.50/share                cifs   rw,relatime,vers=3.0,iocharset=utf8
	#/home/user/network    192.168.1.10:/volume1/homes         nfs4   rw,relatime,vers=4.1,rsize=131072
	#/mnt/remote           user@host:/home/user                fuse.s rw,nosuid,nodev,relatime,user_id=1000


	#findmnt -D -t nfs,nfs4,cifs,smb3
	#SOURCE                      FSTYPE   SIZE  USED AVAIL USE% TARGET
	#//192.168.1.50/share        cifs     1.8T  1.2T  600G  67% /mnt/nas
	#192.168.1.10:/volume1/homes nfs4     5.4T  2.0T  3.4T  37% /home/user/network

	# Need equivalent lsblk report for an instance of each type of remote mounted filesystem
	#lsblk --output-all --pairs --paths ${drive} | sed 's+["][ ]+"\n+g' >${devDat}

	echo "" >>/dev/null
}


diskHtml()
{
	cat <<-EnDoFfIlE
<div style='width: 280px ; align-items: right ; margin: 0,0,0,0' >

<table style='width: 270px ;' >
        <!-- Container -->
<tr>
<td style='width: 54px ; height: 54px' >

<span style='position: absolute ;
	' >
EnDoFfIlE

	if [ \( "${dName}" = "/" \) -a \( -n "${flavIcon}" \) ]
	then
		cat <<-EnDoFfIlE
	<img style='z-index: 99 ;
		position: relative ;
		top: 0%; left: 0% ;
		transform: translate(50%, -150%) ;
		width: 25px ;
		height: 25px ; ' src="${flavIcon}" >

	<img style='z-index: 1 ;
		position: relative ;
		top: 0%; left: 0% ;
		transform: translate(-63%, -50%) ;
		width: 48px ;
		height: 37px ; ' src="file:///home/ericthered/Downloads/DRIVE__Local__DiskUsage__Windows.png" >
</span>
EnDoFfIlE
	else
		cat <<-EnDoFfIlE
	<img style='z-index: 1 ;
		position: relative ;
		top: 0%; left: 0% ;
		transform: translate(0%, -50%) ;
		width: 48px ;
		height: 37px ; ' src="file:///home/ericthered/Downloads/DRIVE__Local__DiskUsage__Windows.png" >
</span>
EnDoFfIlE
	fi
	
	cat <<-EnDoFfIlE
</td>

<td>
	<table>
    	<tr>
	<td>
        	<p class="textUpper" > <span class="bigger" >${dName}</span> &nbsp; [${dPath}] &nbsp; ${dUse}%</p>
	</td>
    	</tr>
    	<tr>
	<td>
            	<!-- Bar Bounds -->
        	<div style='width: 200px ;
			background-color: #ddd ;
			margin: 0 ;
			'>
            		<!-- Bar Measure -->
            		<div style='width: ${dUse}% ;
	    			background-color: ${statColour} ;
				height: 15px ;
				text-align: left ;
				line-height: 15px ;
				color: white ;
				font-weight: bold ;
				' >
            		</div>
		</div>
	</td>
    	</tr>
    	<tr>
	<td>
       		<p class="textLower" >${dFree} ${dUniF}B &nbsp; free of &nbsp; ${dSiz} ${dUniS}B</p>
	</td>
    	</tr>
	</table>
</td>
</tr>
</table>
</div>
EnDoFfIlE

}

#####################################################################################
#####################################################################################


dbg=0
dbg=2

displayHtml="/home/ericthered/Downloads/$(basename "${0}" ".sh" )_report.html"
devDat="/home/ericthered/Downloads/$(basename "${0}" ".sh" )_driveRaw.txt"

# HTML and CSS for the progress bar
rm -f ${displayHtml}

	#margin:		0, 0, 1px, 0 ;
echo "
<html>
<head>

<style type="text/css" >

div {
	margin: 0px ;
	padding: 0px ;
	border-spacing: 0px ;
	background-color: #000 ;
	color:		#eee ;
}

table,tr,td {
	margin: 0px ;
	padding: 1px ;
	border-spacing: 0px ;
	background-color: #000 ;
	color:		#eee ;
}

.textUpper {
	margin:		0 ;
	margin-bottom:	0px ;
	font-family:	Ubuntu ;
	font-size:	12px ;
	font-weight:	normal
	/* padding:	10px ; */
}

.textLower {
	margin:		0 ;
	margin-top:	0px ;
	font-family:	Ubuntu ;
	font-size:	12px ;
	font-weight:	normal
	/* padding:	10px ; */
}

.bigger {
	font-size:	15px ;
	font-weight:	900 
}

.overlay-img {
}

</style>

</head>
<body>
" > ${displayHtml}


first=1

for drive in $(df -h | grep '^/dev/sd' | sort -k1,1 | sort -k6,6 | awk '{ print $1 }' )
do
	test ${first} -eq 0 && { echo "<div style='height: 10px ; width: 280px'></div>" >> ${displayHtml} ; } 

	getUsage

	colourUsage

	flavIcon=""
	if [ "${dName}" = "/" ]
	then
		testor=${GDMSESSION}
		if [ -z "${testor}" ]
		then
			if [ -n "$( ps -e | grep 'mate-session' )" ]
			then
				testor="mate"
			else
				testor="ubuntu"
			fi
		fi

		getOpSysType
	fi


	if [ "${dName}" != "/" ]
	then
		getDriveType
	fi


	diskHtml >> ${displayHtml}

	first=0
done


echo "
</body>
</html>
" >> ${displayHtml}

# Display in YAD
#yad --html --title="Disk Usage" --width=400 --height=150 --center --no-buttons < ${displayHtml}

#sudo -i -u ericthered firefox --new-tab ${displayHtml}
#su - ericthered -c "firefox --new-tab ${displayHtml}"
ls -l ${displayHtml}
6 Likes

For on-demand operation, you can create custom application launcher for a wrapper script. The wrapper script would invoke your html generator and pass html file to a simple html viewer.

Some ideas regarding html viewer can be found here:

P.S. Could not resist to mention

1 Like

Tried to test device.sh but it looks for /home/ericthered/ stuff

user@cw:~$ . device.sh 
bash: /home/ericthered/Downloads/bash_report.html: No such file or directory
bash: /home/ericthered/Downloads/bash_driveRaw.txt: No such file or directory
grep: /home/ericthered/Downloads/bash_driveRaw.txt: No such file or directory
grep: /home/ericthered/Downloads/bash_driveRaw.txt: No such file or directory
grep: /home/ericthered/Downloads/bash_driveRaw.txt: No such file or directory
grep: /home/ericthered/Downloads/bash_driveRaw.txt: No such file or directory
grep: /home/ericthered/Downloads/bash_driveRaw.txt: No such file or directory