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
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

System information function library.

#1 Post by sunburnt »

I wrote this script function library to simplify getting system information.
It`s a bare set of functions for partitions, optical drives, memory, and swap.
Some functions echo user readable text, and some echo raw data ( no parsing ).
I included the most useful info. ( I think...), more are needed. ### Suggestions?
All output is in KB, except the partinfo function is in Bytes to Yattabytes.
It can probably be better ( more simply ) written, always room for improvement.

Code: Select all

It can be sourced as a function library.	". sysinfo"
Run by it`s file name with the functions as an argument.	"sysinfo (function)"
Or it has a command to make links to run the functions directly. "sysinfo mklinks"
### Final rewrite, ver-3
Most of the functions are rewritten, most are faster.
The *info functions are faster, and have option [-s] to show swap partitions.

# There`s also a very nice converter "Bytes to KB, MB, GB, TB, PB, EB, ZB, YB".
# Improved. Converts from any size upward. Current size must be given.
Current given size options are: -b, -k, -m, -g, -t, -p, -e, -z.
It`s not listed in the help, partinfo uses it. # It`s kinda like an Easter Egg.
I`ll move it to another library when I write others for different purposes.

For help type: sysinfo ,or sysinfo [?, -h, --help]
Gunzip it in: /root/my-applications/bin
Attachments
sysinfo_ver-3.zip
(2.06 KiB) Downloaded 235 times
Last edited by sunburnt on Mon 14 Nov 2011, 03:34, edited 3 times in total.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#2 Post by seaside »

sunburnt,

Thanks for this. It seems very handy for other programs to use to get sys info.

One item - there was no device name for the two NTFS partitions on my system.
./sysinfo partlist
NTFS
NTFS
sdb1

sdb6
sdc1
Regards,
s

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

#3 Post by sunburnt »

I was going to say above that I use to have 4 or 5 PCs to test stuff on,
but now I only have my Puppy PC, so there`s bound to be failures.

seaside; Thanks, my PC has no NTFS partitions, nor will it ever probably.

Did "partinfo" work properly? It uses disktype and "partlist" uses fdisk.
I`m surprised my code it didn`t work for fdisk`s output, maybe different formatting.

If you could post for me the output from the command: fdisk -l

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#4 Post by seaside »

sunburnt,
# ./sysinfo partinfo
sda1 ntfs 100 MB
sda2 ntfs 715302 MB
sdb1 ext3 7993 MB
sdb5 ext3 3992 MB
sdb6 ext3 226486 MB
sdc1 3912 MB Did not report ext 3 (boot dev)
# fdisk -l

Disk /dev/sda: 750.1 GB, 750156374016 bytes
255 heads, 63 sectors/track, 91201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 7 HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 91202 732469248 7 HPFS/NTFS

Disk /dev/sdb: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 1019 8185086 83 Linux
/dev/sdb2 1020 30401 236010915 f W95 Ext'd (LBA)
/dev/sdb5 1020 1528 4088511 82 Linux swap / Solaris
/dev/sdb6 1529 30401 231922341 83 Linux

Disk /dev/sdc: 4105 MB, 4105175040 bytes
255 heads, 32 sectors/track, 982 cylinders
Units = cylinders of 8160 * 512 = 4177920 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 * 1 982 4006544 83 Linux
There we go.... partlist doesn't and partinfo does produce a device name.

Regards,
s
(This is my only ntfs machine :) )

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

#5 Post by sunburnt »

Thanks seaside; I forgot the $ at the end of the sed command, maybe that`s it.

Code: Select all

partlist() {
	 fdisk -l |grep '^/dev' |grep -v Ext.d |sed 's/^.*\///;s/ .*$//'
}
Try adding the $ near the end here -------------------------^
Let me know...

I`d gladly use fdisk for partinfo too, it`s faster, but it doesn`t tell if ext2-3-4.

### But it looks like partinfo is failing also... Correct?
Is it a USB flash drive? ( It shouldn`t matter...)

Could you please post the output from disktype?

Code: Select all

disktype /dev/sdc

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#6 Post by seaside »

sunburnt,

Perhaps more directly for partlist-
# grep '^ .*[^k][0-9]$' /proc/partitions | tr -s ' ' | cut -f5 -d ' ' | grep -v loop
sda1
sda2
sdb1
sdb2
sdb5
sdb6
sdc1
And for partsizes-
# grep '^ .*[^k][0-9]$' /proc/partitions | tr -s ' ' | cut -f4 -d ' ' | grep -v loop
Also my sdc is a boot usb thumb drive.
# disktype /dev/sdc

--- /dev/sdc
Block device, size 3.823 GiB (4105175040 bytes)
DOS/MBR partition map
Partition 1: 3.821 GiB (4102701056 bytes, 8013088 sectors from 32, bootable)
Type 0x83 (Linux)
GRUB boot loader, compat version 3.2, boot drive 0xff
Ext3 file system
UUID B28AF40F-3652-4C51-AF68-0DF8F533F328 (DCE, v4)
Last mounted at "/mnt-system"
Volume size 3.821 GiB (4102701056 bytes, 1001636 blocks of 4 KiB)
Regards,
s

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

#7 Post by sunburnt »

seaside; In timing your " |tr -s ' ' |cut -f4 -d ' ' " compared to " |awk '{print $3}' ":

Code: Select all

# time grep $Dev /proc/partitions |tr -s ' ' |cut -f4 -d ' ' |grep -v loop
58613121
real	0m0.016s
user	0m0.000s
sys	0m0.000s

# time grep $Dev /proc/partitions |awk '{print $3}' |grep -v loop
58613121
real	0m0.066s
user	0m0.004s
sys	0m0.012s
Much faster it seems, I`ll be using it from now on... Thanks!

### Q: What does the [^k] do? ... I see no lines in /proc/partitions with a "k".

# The function is: partsize, it returns the size of a given partition.
partinfo and opticalinfo functions are for human eyes, the rest are raw data.

"partitions" is simplier and faster than fdisk and much better than disktype:

Code: Select all

partsize() {
	[ ! "$1" ]&& exit 1 ; Dev=`echo $1 |sed 's#/dev/##'`
	grep $Dev /proc/partitions |tr -s ' ' |cut -f4 -d ' '
}
# In "partlist" your code output the extended partition.
# I rewrote it to solve that:

Code: Select all

 grep '^ .*[0-9]$' /proc/partitions |tr -s ' ' |cut -f4,5 -d ' ' |egrep -v '(^1 |loop)' |sed 's/^.* //'
# This assumes that all extended partitions have a size of 1 KB...

### I`m rewriting partinfo.
### Code should get your USB drive type, it uses "grep file" to get it.
# I ran your posted output code through partinfo`s code and it worked...

### NOTE: I just noticed partinfo said sdb5 is ext3, but fdisk says it`s a swap.
I have no swap partition, just a swap file...
### Please post "disktype /dev/sdb5"...
.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#8 Post by seaside »

sunburnt wrote:
### Q: What does the [^k] do? ... I see no lines in /proc/partitions with a "k".

### NOTE: I just noticed partinfo said sdb5 is ext3, but fdisk says it`s a swap.
I have no swap partition, just a swap file...
### Please post "disktype /dev/sdb5"...
.
sunburnt,

The exclude "k" was already in this snippet which I got from somewhere and my guess is it was to exclude the first line that has "block" in it. It doesn't seem necessary, since the match is only based on a number at the end.

sdb5 is indeed a "swap" partition.....
# disktype /dev/sdb5
--- /dev/sdb5
Block device, size 3.899 GiB (4186635264 bytes)
Linux swap, version 2, subversion 1, 4 KiB pages, little-endian
Swap size 3.899 GiB (4186624000 bytes, 1022125 pages of 4 KiB)
Regards,
s
(I'm not even sure why this swap partition is even there, since this machine has plenty of ram :) )

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

#9 Post by sunburnt »

Hey seaside; I was just looking at your post and the swap error hit me.
I add a line to continue loop if disktype reported a swap.

I`ll be posting a new one, if you could test it. ... Tx for the help... 8)
Should be no swap? ... Is the USB still not showing it`s part. type?

# Yes... I`ve posted many times about how a swap just slows the PC down.
1 GB of ram and you`re good, no swap`s needed, 2 GB and you`re great.

Lobster and some others have been talking about a dev. PC bd. with a 1/4 GB.
My PC has a 1/4 GB, it use to do okay, but now Firefox is so bloated it`s a pig.
It of course needs a swap badly, but it`s sooo damn slow it drives me nuts.
And Firefox seems to be up to it`s old tricks of hogging ram... Kill it and restart.
I think it keeps saving everything to a web cache that keeps the HD running.
If only I could control it, say none or a small cache, then it`d be much better.
In my opinion a 1/4 GB of ram is silly, 1 GB is the minimum for a desktop PC.

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#10 Post by vovchik »

Dear sunburnt,

This might be off topic, but I just did the following a few days ago to reduce Firefox's memory footprint:

1 .Go into about:config

2. Search for: browser.cache.memory.enable

3. Click on it to set to "false"

This means Firefox will no longer keep even closed pages in the memory cache. There is no adverse effect, as I can see, just better memory utilization. Now, when you close tabs, the memory hogged by Firefox will return to the system.

With kind regards,
vovchik

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

#11 Post by technosaurus »

for x in /sys/block/sd?/sd* ;do read SIZE < $x/size;echo ${x##*/}:$SIZE;done

i don't have any hd* devices but I assume that would work too with

for x in /sys/block/[hs]d?/[hs]d* ;do read SIZE < $x/size;echo ${x##*/}:$SIZE;done
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.

#12 Post by sunburnt »

Hey technosaurus; I`ve looked at /sys before and found it to be a maze.
But your code scrap is really fast compared to disktype, of course.
/proc/partitions has same info and speed... I need partition type also.
I looked and found a file "type", but it has "0" in it, the drive type probably.
I`d like to be rid of disktype, but it gives an unmounted partition`s format.

Howdy vovchik; You da man! ( You`re the man! ) ... I`ll try that immediatly!
And yet another important piece of info. for my doc. file library.

seaside; I have ver-2 done, but I`ll delay to see what technosaurus has.

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

#13 Post by technosaurus »

there isn't a fast way to get the type except on brand new pups that have busybox with device type enabled for blkid

I do know that many distros would be happy with a small change in the kernel source code that would put the device type in each partitions uevent. I think I tracked down the code for the rest of the variables to:

fs/partitions/check.c: for the uevent
block/genhd.c: disk->driverfs_dev->driver

really those 2 share a lot of code and with the right reorganization it could be smaller and a lot more useful

here is a function to complex math
calc() {
awk "BEGIN{print $@}"
}

MODULES="";while read MODULE || [ "$MODULE" ]; do MODULES="$MODULES ${MODULE%% *}"; done </proc/modules

while I am on modules here is the module remover that I wrote to speed up subsequent boots (similar to my prior zdrv cutter, but I rewrote it for removing unneeded stuff from modules.order and flattened out the directory structure - all modules go in /lib/modules/$KERNELVERSION/ for smaller/faster modules.* files)

Code: Select all

#!/bin/ash
while read MODULE || [ "$MODULE" ]; do MODULES="${MODULE%% *}*|$MODULES"; done </proc/modules

rm_mods(){
DIR=${1:-/lib/modules/*.*/}
cd $DIR
#echo $DIR $MODULES
for FILE in * ; do
case $FILE in
	*.ko*)MODNAME=${FILE%%.*}
	CHECK="case $MODNAME in $MODULES'')mv $FILE /lib/modules/*.*/;;*)rm $FILE;;esac"
	eval $CHECK;;
	modules.order)printf "">/tmp/$FILE		
		while read LINE || [ "$LINE" ];do
			CHECK="case \"${LINE##*/}\" in $MODULES'')echo ${LINE##*/} >>/tmp/modules.order;;esac"
			eval $CHECK
		done <$FILE && mv -f /tmp/$FILE $FILE & ;;
	"*")continue;;
	*)	if [ ! -d $DIR/$FILE ];then
			rm $FILE &
		else
			rm_mods $DIR/$FILE &
		fi
esac
done
wait
rmdir $DIR
}

rm_mods
depmod
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.

#14 Post by sunburnt »

technosaurus; Looks interesting and may produce resuilts.

The modules are the second longest part of Puppy`s booting.
If all the modules were compiled into the kernel it`d speed it up.
This is how Linus Torvalds envisioned Linux to be. But reality is...
If compiled for one type of PCs hardware this would be very good.

Same thought I had to reduce the number of folders ( and files...).
Linux has legacy directory trees and newer patches added to them. Clean up!
Gobos Linux and TinyCore Linux both have fewer folders and files than Puppy.

In my TinyMod version of TinyCore I used links to merge /lib and /etc folders.
I`ve noticed a few apps. that expect libs. in different folders, and so errors.
And links to the other /lib folder in Puppy to cover for this problem.
Just link /usr/lib and /usr/local/lib to /lib and there`s no problem...
Last edited by sunburnt on Sun 13 Nov 2011, 06:10, edited 4 times in total.

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

#15 Post by sunburnt »

.
### New version, ver-2. See first post for changes and download.

seaside; There should be no swap partition showing
And is the USB still not showing it`s partition type? ( can`t see why it failed...)
All of mine work properly, but that`s no indicator...

vovchik; I forgot to say, I mentioned the Firefox problem, so it`s not off subject.
Besides, I`m the king of off subject, cross posting, thread stepping on, etc.

# Also if you want to send the code files you mentioned I`ll gladly look at them.
.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#16 Post by seaside »

sunburnt wrote:.
### New version, ver-2. See first post for changes and download.

seaside; There should be no swap partition showing
And is the USB still not showing it`s partition type? ( can`t see why it failed...)
All of mine work properly, but that`s no indicator...

.
sunburnt,

++++++++ Nice work. See below-
# ./sysinfo partlist
sda1
sda2
sdb1
sdb5 this is swap
sdb6
sdc1 boot usb thumb drive

# ./sysinfo partinfo swap sdb5 not shown

Part. Type Size
======================
sda1 ntfs 100. MB
sda2 ntfs 698. GB
sdb1 ext3 7.80 GB
sdb6 ext3 221. GB
sdc1 ext3 3.82 GB

# ./sysinfo partsize sdc1
4102701056
#
# ./sysinfo swapinfo
4088500 partition /dev/sdb5
All nice, accurate and snappy; and with "partinfo" only a slight lag (to be expected).

Another arrow in the puppy toolbox quiver. :)

Regards,
s

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

#17 Post by sunburnt »

Thanks seaside; My thought is that the swap should not show in partinfo and partlist.
# What do you think?

### Update: I just found this at Ubuntu: # /proc/swaps shows the device.
Pull up a Terminal again and run cat /proc/swaps and hopefully you see the path to your swap partition listed there. If not chances are something went wrong in the steps above. Here's my output:

Filename Type Size Used Priority
/dev/sda2 partition 2676732 73380 -1
I`ll be adding new code to sysinfo to stop partlist from showing the swap.
# Unless you think otherwise.
My thought is showing "accessible" partitions.
But showing the swap is showing "complete" partition data...
But it doesn`t show swap files, so it`s incomplete in reqard to swaps.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#18 Post by seaside »

sunburnt wrote: I`ll be adding new code to sysinfo to stop partlist from showing the swap.
# Unless you think otherwise.
My thought is showing "accessible" partitions.
But showing the swap is showing "complete" partition data...
But it doesn`t show swap files, so it`s incomplete in reqard to swaps.
sunburnt,

This is beginning to sound like the conversations that sometimes go on inside my head on the pro's and con's of doing something one way or the other.... :)

I lean toward excluding it in "partlist" because it's use would be primarily to look for partitions to DO something to or with .... and what exactly would one be doing with a swap partition except perhaps enabling or disabling.

In any case the "swapinfo" parameter provides the information, if needed.

Regards,
s
(Internal debate is best because it's hard to offend yourself, and furthermore you'll never be wrong either :) )

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

#19 Post by sunburnt »

My thought pattern exactly. Tx...

( If sysinfo were only for my use, dialog would have stayed internal...)

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

#20 Post by sunburnt »

.
Version-3 posted...

# partinfo and partlist have "show swap partitions" option.

# The size conversion now will convert upward to any size.

### If anyone thinks it should have a certain function, speak now!


seaside; If you`ll run it through it`s paces please. ( if tired, just say so...) :roll:
Pay close attention to the swap reporting, and try the new swap option (-s).
Last edited by sunburnt on Wed 16 Nov 2011, 04:15, edited 1 time in total.

Post Reply