Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 27 May 2018, 07:23
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
System information function library.
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [24 Posts]   Goto page: 1, 2 Next
Author Message
sunburnt


Joined: 08 Jun 2005
Posts: 5087
Location: Arizona, U.S.A.

PostPosted: Mon 07 Nov 2011, 04:10    Post subject:  System information function library.  

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:
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
sysinfo_ver-3.zip
Description 
zip

 Download 
Filename  sysinfo_ver-3.zip 
Filesize  2.06 KB 
Downloaded  209 Time(s) 

Last edited by sunburnt on Sun 13 Nov 2011, 23:34; edited 3 times in total
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 917

PostPosted: Mon 07 Nov 2011, 18:06    Post subject:  

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.

Quote:
./sysinfo partlist
NTFS
NTFS
sdb1

sdb6
sdc1


Regards,
s
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5087
Location: Arizona, U.S.A.

PostPosted: Tue 08 Nov 2011, 14:45    Post subject:  

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
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 917

PostPosted: Tue 08 Nov 2011, 18:13    Post subject:  

sunburnt,

Quote:

# ./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 Smile )
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5087
Location: Arizona, U.S.A.

PostPosted: Wed 09 Nov 2011, 02:34    Post subject:  

Thanks seaside; I forgot the $ at the end of the sed command, maybe that`s it.
Code:
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:
disktype /dev/sdc
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 917

PostPosted: Wed 09 Nov 2011, 19:41    Post subject:  

sunburnt,

Perhaps more directly for partlist-
Quote:
# grep '^ .*[^k][0-9]$' /proc/partitions | tr -s ' ' | cut -f5 -d ' ' | grep -v loop
sda1
sda2
sdb1
sdb2
sdb5
sdb6
sdc1


And for partsizes-

Quote:
# grep '^ .*[^k][0-9]$' /proc/partitions | tr -s ' ' | cut -f4 -d ' ' | grep -v loop


Also my sdc is a boot usb thumb drive.

Quote:
# 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
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5087
Location: Arizona, U.S.A.

PostPosted: Fri 11 Nov 2011, 00:18    Post subject:  

seaside; In timing your " |tr -s ' ' |cut -f4 -d ' ' " compared to " |awk '{print $3}' ":
Code:
# 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:
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:
 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"...
.
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 917

PostPosted: Fri 11 Nov 2011, 18:59    Post subject:  

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.....
Quote:
# 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 Smile )
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5087
Location: Arizona, U.S.A.

PostPosted: Sat 12 Nov 2011, 01:16    Post subject:  

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... Cool
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.
Back to top
View user's profile Send private message 
vovchik


Joined: 23 Oct 2006
Posts: 1448
Location: Ukraine

PostPosted: Sat 12 Nov 2011, 03:11    Post subject:  

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
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4811
Location: Kingwood, TX

PostPosted: Sat 12 Nov 2011, 04:03    Post subject:  

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 github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
sunburnt


Joined: 08 Jun 2005
Posts: 5087
Location: Arizona, U.S.A.

PostPosted: Sat 12 Nov 2011, 05:00    Post subject:  

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.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4811
Location: Kingwood, TX

PostPosted: Sat 12 Nov 2011, 21:53    Post subject:  

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:
#!/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 github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
sunburnt


Joined: 08 Jun 2005
Posts: 5087
Location: Arizona, U.S.A.

PostPosted: Sun 13 Nov 2011, 01:36    Post subject:  

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, 02:10; edited 4 times in total
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5087
Location: Arizona, U.S.A.

PostPosted: Sun 13 Nov 2011, 01:42    Post subject:  

.
### 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.
.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [24 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0724s ][ Queries: 12 (0.0113s) ][ GZIP on ]