System information function library.

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#21 Post by seaside »

Sunburnt,

Here's the latest run -
# sysinfo partinfo

Part. Type Size
======================
sda1 ntfs 1 MB
sda2 ntfs 698.53 GB
sdb1 ext3 7.8 GB
sdb5 swap 3.89 GB
sdb6 ext3 221.17 GB
sdc1 ext3 3.82 GB

# sysinfo partlist
sda1
sda2
sdb1
sdb5
sdb6
sdc1
# sysinfo swapinfo
4088500 partition /dev/sdb5
# sysinfo parttype sda1
ntfs
# sysinfo parttype sdd3
### Error: No partition: sdd3
# sysinfo parttype sdb5
#
I sense there was another internal discussion :) :)

Regards,
s

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#22 Post by technosaurus »

some of these could even be aliases instead of functions
swapinfo looks to be doing this:
awk 'NR>1 {print $3 "\n" $2 "\n" $1 }' /proc/swaps
partlist:
awk '/[0-9] [hsm][dm][a-z][b0-9]/ {print $4}' /proc/partitions
EDIT ... forgot to remove logical partitions
awk '/[0-9] [hsm][dm][a-z][b0-9]/ {if ($3>1) print $4}' /proc/partitions

memtotal:
awk '/MemTotal/ {print $2 $3 }' /proc/meminfo
or in Mb
awk '/MemTotal/ {print $2/1024 "Mb" }' /proc/meminfo
memfree:
awk '/MemFree/ {print $2 $3 }' /proc/meminfo
in Mb
awk '/MemFree/ {print $2/1024 "Mb" }' /proc/meminfo

ok, now I am just showing off... memused:
awk '/MemTotal/ {kb += $2} /MemFree/ {kb -= $2} END {print kb/1024 "Mb" }' /proc/meminfo

partsize() {
[ ! "$1" ]&& exit 1 || DEV=${1##*/}
awk '/'$DEV'/ {print $3/1024 "Mb" }' /proc/partitions
}

similar for cdinfo, but that will have to wait as it is getting late
a single call to awk can often replace several commands (it is really a scripting language like sh, ash, bash... just with different inbuilt commands and it takes a while to know when to use which)
I learned a _LOT_ about awk from here:
http://www.vectorsite.net/tsawk_1.html
FWIW awk has no problem reading /proc/sys/dev/cdrom/info

Here are some unsorted notes from my todo list for bashbox (there are much more there, but I haven't separated them all out yet)

Code: Select all

bytes_rx(){
while read A || [ "${A}" ]; do
	A=`echo $A`
	case $A in
		${1:-wlan0}*)
			A=`echo "${A#* }"`
			case "${A}" in
				[0-9]*)echo "${A%% *}";;
			esac
		;;
	esac
done </proc/net/dev
}

bytes_tx(){
while read A || [ "${A}" ]; do
	A=`echo $A`
	case $A in
		${1:-wlan0}*)
			A=`echo "${A#* * * * * * * * * }"`
			case "${A}" in
				[0-9]*)echo "${A%% *}";;
			esac
		;;
	esac
done </proc/net/dev
}

#directory of the program being executed 
binpath=${0%/*}
#argpath=${1%/*}   #maybe???

#mac address
#grep ":" /proc/net/arp |awk '{print $4}'
get_mac(){
while read A || [ "${A}" ]; do
	A=`echo $A`
	A=`echo "${A#* * * }"`
	case "${A}" in
		[0-9]*)
			echo "${A%% *}"
		;;
	esac
done </proc/net/arp
}
#ip address
#grep ":" /proc/net/arp |awk '{print $1}'
get_ip(){
while read A || [ "${A}" ]; do
	case "${A}" in
		[0-9]*)
			echo "${A%% *}"
		;;
	esac
done </proc/net/arp
}

#lsmod only modules
lsmod_s(){
while read MOD; do
	echo ${MOD% *}
done </proc/modules
}

#lsmod
lsmod(){
while read MOD; do
	echo ${MOD}
done </proc/modules
}

#waitpid
waitpid(){
while ([ -d /proc/$PID ]) do
	usleep 1000
done
}

#kernel command line
read CMDLINE </proc/cmdline

mod_exists() {}
while read MOD; do
	case $MOD in
		*/${1}.ko*)return 0;;
	esac
	return 1
done </lib/modules/$KERN_VER/modules.dep
}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#23 Post by sunburnt »

Hey technosaurus; Yep, awk alone is lots faster than a string of commands.
I timed all of your code against code I have and it`s all much faster.
Your MAC and IP code was longer than just ifconfig, but it was still faster.
The IP and MAC is useful info. /arp gave a different MAC than ifconfig did.

seaside; It`s only supposed to show the swap partitions with argument -s
So something`s wrong... I`ll look closer at what technosaurus has.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#24 Post by sunburnt »

technosaurus`s partlist is the fastest I can find, of course. Average = .004 sec.

And I modded code given me by vovchik to do partinfo, and it`s .012 sec. fast.

Code: Select all

for P in /sys/block/[hs]d?/[hs]d*
do read S < $P/size ;[ $S -gt 2 ]&& T=`guess_fstype /dev/${P##*/}` && echo ${P##*/} $T $S ;done
It`s also very simple compared to my code, as is technosaurus`s code.

# I`m busy for the weekend, but I may get time to rework sysinfo, again... :roll:

Post Reply