Bash script to monitor disk space and email alerts if space is low, great to use with crontab

Wrote this for FreeBSD, should run on Linux with minor modifications.
(fix the paths, and replace jot with seq)

#!/bin/bash
#
# Bash script to monitor disk space
# It will email $ADMIN if disk space goes below
#
# Pétur Ingi Egilsson # 3 March 2011
ADMIN="petur@petur.eu"
SUBJECT="warning"
#Device to monitor
Disk[1]="/dev/mirror/gm0s1a"
#Min space in KiB
MinDisk[1]=500000 # 500 MiB
Disk[2]="/dev/mirror/gm0s1e"
MinDisk[2]=500000 # 500 MiB
Disk[3]="/dev/mirror/gm0s1d"
MinDisk[3]=500000 # 500 MiB
for i in `/usr/bin/jot - 1 ${#Disk[@]}`;
do
space=`/bin/df -k ${Disk[$i]} | /usr/bin/awk '{print $4}' | tail -n 1`
if [ $space -le ${MinDisk[$i]} ];
then /bin/echo "Warning: ${Disk[$i]} has $space KiB left" | /usr/bin/mail -s $SUBJECT $ADMIN;
fi
done

Using the /proc filesystem

[ad#Google Adsense-1]
The proc filesystem is a special filesystem found on most UNIX-based systems.
It holds a great deal of information, in ASCII format, most of which is not very friendly to the average user.
It is important that you keep in mind that the files under /proc are not kept on a physical storage, meaning they are subject to change after reboot. Also, they should not really be called files as they are pseudo-files, as they exist only in memory.
I break that rule on regular basis and intend to do that also in this article.
I’ve made a list of some of the files i find to be of most use.
/proc/[pid]/
/proc contains a directory named after the PID (process identification number) of each excising process on the system.
Lets take a look at some of the files found there.

/proc/[pid]/cmdline

Contains the command line used to launch the process.

/proc/[pid]/cwd

This is a symbolic link to the current working directory of the process.
If you have a process with the PID 1234, then you can find out it’s current working directory by using the command “cd /proc/1234/cwd; /bin/pwd”

/proc/[pid]/status

This file contains information about the processes status, such as it’s name, state, pid, parent pid, owner.

/proc/cmdline
Contains all the arguments passed to the kernel at boot time.
/proc/cpuinfo
Perhaps the most known one, it contains processor related information, such as the architecture, frequency and amount of cache found on the cpu.
/proc/filesystems
A list of all the file systems supported by the current kernel.
Lines beginning with ‘nodev’ indicate non-physical filesystems such as network filesystems and proc.
/proc/loadavg
Holds information regarding the load average of the system.
The first three fields are the same ones you get from ‘uptime’.
The fourth field consists of two numbers seperated by a slash, the first one represents the number of currently executing processes/threads. This number will not exceed the number of processors cores the system has.
The second number (the one after the slash) represents the number of processes/threads currently existing on the system.
The fifth field is the PID of the process most recently created.Now, this is where you need to be careful.If you execute ‘cat /proc/loadav’, then this number will represent the PID of the cat command you just executed!
/proc/free
Contains statistics about memory usage.
The command ‘free’ makes use of this file to build its output.
/proc/net/
This directory holds alot of files rated to the networking layer.
All the files are ASCII structured and can be read.

/proc/net/arp

Holds the arp table

/proc/net/dev

Information such as the total number of received and transmitted packets and bytes by each network interface.

/proc/net/route

Holds the routing table, in hexademical format.

/proc/net/wireless

Holds information related to the current wireless connection, such as thequality and number of discarded packets.

/proc/swaps
Shows the amount of swap in use and the priority of the defined swap partitions.
/proc/sys/kernel/hostname
Contains the current hostname of the system.
You can change this by executing “echo ‘newHostname’ > /proc/sys/kernel/hostname”
/proc/sys/kernel/threads-max
Specifies the maximum number of processess/threads that can excist at any given time on the system.
Compare this to the current number of processes/threads from the fourth field in /proc/loadavg
/proc/sys/vm/swappiness
The value in this file controls how willing the kernel will be to swap memory.
If you raise this number, the kernel will want to swap more often, while lowering it will decrease his tendency to swap.
The default value is 60.
/proc/uptime
Contains two numbers, the first one tells you how long the system has been up (in seconds), while the second one tells you for how long it has been idle.
You can use something like:echo `cut -d’ ‘ -f2 /proc/uptime` / `cut -d’ ‘ -f1 /proc/uptime` | bc -l to get the percentage of idle time on your computer.
/proc/vmstat
Contains virtual memory statistics
/proc/sys/net/ipv4/conf/default/forwarding
Controls whether the kernel will allow tcp forwarding.The default value is 0 which means forwarding is OFF.You can set this to 1 if you with to enable it…
Think: Internet connection sharing without password protection.
Please share your thoughts in the comment section below!
 
[ad#Google Adsense-1]

How to detect mouse movement on linux



The following script will display the current date and time, every second the mouse is in motion
It must be run as root…

#!/bin/bash
while :
do
cat /dev/input/mice | read -n 1
date
sleep 1
done

9 OS X Command Line Utilities you probably didn’t know about

OS X is ment to be a clean and a simple operating system,
yet under the hood it’s very powerful.

Here I shall go over 9, rather random, commands.
You can learn more about them by typing “man command-name” in the console.
Example, “man shutdown”.

Here we go!

1. shutdown
The command shutdown will shut down the system.

It must be run as root, by using the sudo prefix (which makes the command run as root)
Examples:
Reboot the computer
sudo shutdown -r now
Halt the computer (turn if off)
sudo shutdown -h now
Put the computer to sleep
sudo shutdown -s now
2. say
This tool uses the Speech Synthesis manager to convert input text to audible speech.

3. screencapture
The screencapture command can be used to capture a screenshot of the computer.
Example:
box:- petur$ screencapture screen.png
“Why on earth would you want to do that using the command line?”, you might ask. Because you can capture screenshots of remote computers, proven that you have root access to it via ssh.
It could also be useful in some situations to make a screenshot of a particular computer every x-minutes by combining ‘screencapture’ with the root crontab. Unfortunately crontab is, as I would say, an advanced topic and it will not be covered in this post.
4. arch
The arch command with no arguments, displays the machine’s architecture type.

The results can be read as follow:
i386 : 32-bit intel
x86_64 : 64-bit intel
ppc : 32-bit powerpc
ppc64 : 64-bit powerpc
5. top
The top CLI (Command Line Interface) equvalient of the Activity Monitor.


6. file
The file command is used to determine a file type.

Usage: file filename

7. iostat
iostat reports I/O statistics.
In other words, the load on your hard disk and the cpu.

Those numbers can be a little difficult to understand at first,
tps stands for Transfers per Second
KB/t stands for kilobytes per Transfer
MB/s stands for megabytes per second.

8. df
df stands for “display free”, as in display free disk space.
It’s one of those utilities you can’t be without!
-l filters out all non-local filesystems
-h makes the output more human friendly (Gi and Mi instead of plain Bytes)


9. vm_stat
Virtual memory Statistics
the hit rate is perhaps the most important information,
as a high rate indicates that you should add more RAM (memory) to your computer.


 
Please share your thoughts with me,
if you have any questions don’t hesitate to ask!

Skype on a 64bit Fedora 13 – The solution to : Failed to execute child process “skype” (No such file or directory)

When trying to install Skype on Fedora 13 (64bit) i recived the following error:
“Failed to execute child process “skype” (No such file or directory)”
The solution is to install the required 32bit libs.
Do the following in order to get Skype to work on a Fedora 13 x86_64 machines
1. Download the i686 rpm from http://www.skype.com/go/getskype-linux-beta-fc10
2. Install the required 32bit libs as root:

yum -y install pulseaudio-libs.i686 pulseaudio-libs-devel.i686 alsa-plugins-pulseaudio.i686 \
libv4l.i686 libXv.i686 libXv-devel.i686 libXScrnSaver.i686 libXScrnSaver-devel.i686 \
dbus-qt.i686 dbus-qt-devel.i686 qt.i686 qt-devel.i686

3. configure the dynamic linker by executing ‘ldconfig‘ as root.

Update: Change to your Favorite directories on the bash shell by scripting ‘cd’

I’m sick and tired of having to remember and ‘cd’ long paths.
I decided to create my own console based Favorites-script which makes use of a definition file which is easy to change.
This post updates http://www.petur.eu/blog/?p=175
“cd /home/petur/Documents/school/2010/fall/chemistry” becomes “cdf chemistry”
and “cd /var/log” becomes “cdf log”
Demonstration video:

The setup process is pretty straight forward, here we go (single user setup):
Append the following to:
~/.bashrc file (single user setup)
or
/etc/bash.bashrc (for all users)

function cdf() {
#
# Pétur Ingi Egilsson ( petur <@> petur.eu )
# http://www.petur.eu/blog/?p=175
#
# Stuart Rackham ( srackham <@> gmail.com )
#
# cdf (Change to favorites)
local aliasfile fullpath
aliasfile=~/.cdfrc
if [ $# -eq 0 ]
then
echo Usage: cdf FAVORITE
return 1
fi
fullpath=$(grep $1, $aliasfile|cut -d, -f2)
fullpath=$(eval "echo -n $fullpath")
if [ ${#fullpath} -ne 0 ]
then
cd $fullpath
else
echo "Error: '$1' has not been defined in $aliasfile"
echo -n "Do you want to edit the file? (y/n): "
read editFile
case $editFile in
[yY])
if [ ! -n "$EDITOR" ]
then
# Use the nano editor because
# the EDITOR env has not been set.
nano $aliasfile
else
$EDITOR $aliasfile
fi
;;
[nN])
;;
*)
echo "Please use y,Y,n or N."
exit 1
esac
fi
}

Save the following file as ~/.cdfrc

# Definition file for cdf
#
# format: name,/path/to/directoryetc,/etc
logs,/var/logs
mnt,/mnt
root,/root
var,/var

Do you have any comments?

[ad#Banner]

Change to your Favorite directories on the bash shell by scripting ‘cd’.

[ad#Banner]

I’m sick and tired of having to remember and ‘cd’ long paths.
I decided to create my own console based Favorites-script which makes use of a definition file which is easy to change.
A newer version has been released, please visit:
http://www.petur.eu/blog/?p=190.
“cd /home/petur/Documents/school/2010/fall/chemistry” becomes “cdf chemistry”
and “cd /var/log” becomes “cdf log”

The setup process is pretty straight forward, here we go (single user setup):
Save the following script as ~/bin/cdf

#!/bin/bash
#
# Pétur Ingi Egilsson ( petur@petur.eu )
#
# cdf (Change to favorites)
# Usage: cdf favoriteALIASES=~/bin/cdf.conf
fullpath=$(grep $1, $ALIASES|cut -d, -f2)
if [ ${#fullpath} -ne 0 ]
then
cd $fullpath
else
echo "Error: '$1' has not been defined in $ALIASES"
echo -n "Do you want to edit the file? (y/n): "
read editFile
case $editFile in
[yY])
if [ ! -n "$EDITOR" ]
then
# Use the nano editor because
# the EDITOR env has not been set.
nano $ALIASES
else
$EDITOR $ALIASES
fi
;;
[nN])
;;
*)
echo "Please use y,Y,n or N."
exit 1
esac
fi

Make it executable

petur@klettur:~$ chmod +x ~/bin/cdf

Save the following file as ~/bin/cdf.conf

# Definition file for cdf
#
# ATTENTION: Do not use the ~ (tilda).
# WRONG: documents,~/Documents
# RIGHT: documents,/home/petur/Documents
#
# format: name,/path/to/directoryetc,/etc
logs,/var/logs
mnt,/mnt
root,/root
var,/var

Bash scripts are executed in a subshell as child processes.
This behavior is undesirable as the bash child-process cannot tell the parent to change to the new directory.
In order to work around this you’ll need to execute the script by placing a dot-space(. ) in front of it.
If you are like me you would like to avoid having to do that so create an alias like this in .bashrc :

petur@klettur:~$ echo alias cdf=\". ~/bin/cdf\" | tee -a ~/.bashrc

Do you have any comments?

Linux: Howto download ISO files directly to a CD/DVD/BLU-RAY

It is possible to download .iso files directly onto a cd\dvd\blu-ray disk.
/dev/shm is a directory found on Debian & RedHat based distributions. (Please let me know if you find it on dists as well)
/dev/shm is a dynamic RAM disk, meaning it’s size will grow and shrink with the size of the files placed there.
The following process is especially ideal for users running Linux from RAM or small USB sticks.
No drive space will be used during the process & only a very small amount of RAM is used.
First create a FIFO (first in first out) on /dev/shm
Next we command the burner to write any data written to the FIFO onto the preferred medium (dvd, cd, blu-ray, etc.)
Then we download the file ‘directly’ onto the preferred medium through the FIFO.
And finally we remove the FIFO.
I’ll demonstrate this by downloading the “Fedora Core 13 LIVECD” ISO to a CD.

petur@laptop:~$ mkfifo /dev/shm/fedora-fifo
petur@laptop:~$ cdrecord /dev/shm/fedora-fifo &
petur@laptop:~$ wget http://ftp.crc.dk/fedora/linux/releases/13/Live/i686/Fedora-13-i686-Live.iso -O /dev/shm/fedora-fifo
petur@laptop:~$ rm /dev/shm/fedora-fifo

Attention! The -O parameter used with wget is case sensitive!