Archive for the ‘Linux’ Category

If you use Postgrey (and you should do it) on your mail servers and you want to have some statistics on the amount of greylisted messages and other information, you should find useful the following script.

The scripts assume that you have installed Postgrey (http://postgrey.schweikert.ch/) on RHEL (http://www.redhat.com) or CentOS (http://www.centos.org).
If you are using other distros you may fix some path problems.

Create a file called “greylisting_statistics.sh” in your “/usr/local/bin” folder with the following content :

#!/bin/sh

LOGFILE=/tmp/greylist-statistics
YOURMAIL=you@yourdomain.com

echo -n "Total amount of GreyListed messages" > $LOGFILE
cat /var/log/maillog | /usr/sbin/postgreyreport --delay=300 >> $LOGFILE
echo -ne  "\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -n "Get only the top 20 sources getting greylisted out" >> $LOGFILE
cat /var/log/maillog | postgreyreport | awk '{print $1}' | sort | uniq -c | sort -nr | head -n20 >> $LOGFILE
echo -ne  "\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -n "Get a list of the top 20 email address that the greylisted sources are sending email to"  >> $LOGFILE
cat /var/log/maillog | postgreyreport | awk '{print $4}'  | sort  | uniq -c | sort -nr | head -n20
echo -ne  "\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE

cat $LOGFILE | mail -s "Greylisting Statistics of `hostname` for `date +%Y-%m-%d`" $YOURMAIL
#--- End of file ---

Assign to it the correct ownership and permission by running :

chmod 700 /usr/local/bin/greylisting_statistics.sh
chown root:root /usr/local/bin/greylisting_statistics.sh

Schedule the execution using “crontab -e” and adding the following line to have a daily based statistics :

59 23 * * * /usr/local/bin/greylisting_statistics.sh

Hope this help

Bye
Riccardo

Print This Post Print This Post

This simple How-To will explain a raw and dirt method to daemonize to http manager for ARECA Raid Controller.

If you have this controller on your system you should find in the bundled CD-ROM the following folder :

root
PACKAGES -> Linux -> CLI -> version -> i386 -> cli32

PACKAGES -> Linux -> CLI -> version -> x86-64 -> cli32

PACKAGES -> Linux -> HTTP -> version.x.y.x -> i386 -> archttp32

PACKAGES -> Linux -> HTTP -> version.x.y.x -> x86-64 -> archttp32

Create a folder in your /usr/local folder called “areca” e and create into it two folders called “http” and “cli”
Copy the “cli32″ and “archttp32″ executable file that match your architecture (i386 or x86-64) in the specified folder, so you should have something similar :

[root@relay ~]# ls -lahR /usr/local/areca/
/usr/local/areca/:
total 20K
drwxr-xr-x  4 root root 4.0K Feb 12 10:28 .
drwxr-xr-x 13 root root 4.0K Feb 12 10:26 ..
dr-xr-xr-x  2 root root 4.0K Feb 12 10:27 cli
dr-xr-xr-x  2 root root 4.0K Feb 12 10:42 http

/usr/local/areca/cli:
total 1.6M
dr-xr-xr-x 2 root root 4.0K Feb 12 10:27 .
drwxr-xr-x 4 root root 4.0K Feb 12 10:28 ..
-r-xr-xr-x 1 root root 1.6M Dec 26  2008 cli32

/usr/local/areca/http:
total 1.6M
dr-xr-xr-x 2 root root 4.0K Feb 12 10:42 .
drwxr-xr-x 4 root root 4.0K Feb 12 10:28 ..
-r-xr-xr-x 1 root root 1.6M Dec 26  2008 archttp32
-rw-r--r-- 1 root root   91 Feb 12 10:42 archttpsrv.conf

Create a new file called “areca” in your /etc/init.d folder with the following content :

#!/bin/sh
#
# Startup script for the ARECA RAID CONTROLLER HTTP Monitor
#
# chkconfig: 2345 62 38
# description: HTTP Tools to monitor and manage ARECA RAID Controller
#
# processname: archttp32
# config: none
# lockfile: /var/lock/subsys/archttp32
#
# Author: Riccardo Riva
# WebSite: http://www.riccardoriva.com
#
# This script is realeased under the terms of the GPL.
#====================================================================

# Source function library
. /etc/init.d/functions

RETVAL=0

start() {
echo -n $"Starting ARECA HTTP Manager: "
daemon /usr/local/areca/http/archttp32 2>&1>/dev/null &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/archttp32
return $RETVAL
}

stop() {
echo -n $"Stopping ARECA HTTP Manager: "
killproc archttp32
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/archttp32
return $RETVAL
}

restart() {
stop
start
}

reload() {
echo -n $"Reloading ARECA HTTP Manager: "
killproc archttp32 -ALRM
RETVAL=$?
echo
return $RETVAL
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status archttp32
;;
restart)
restart
;;
condrestart)
[ -f /var/lock/subsys/archttp32 ] && restart || :
;;
reload)
reload
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
exit 1
esac

exit $?
#---End of file---

Assing to it the right permission and ownership

chmod 755 /etc/init.d/areca
chown root:root /etc/init.d/areca

You should also place in the executable file folder the configuration file, called “archttp32srv.conf” with the following content :

[GENERAL]
BindingIp=0.0.0.0
HTTPPort=81
SMTPPort=25
ScanPci=YES
ScanRs232=NO
ScanInband=NO
ConnInfo=NO

[MAIL]
Server=MAIL.SERVER.IP.ADDRESS # Please Change ME
Sender=SENDERNAME # Please Change ME
SenderMail=SENDERMAIL # Please Change ME
Account=
Password=
MailToName1=RECEIVERNAME # Please Change ME
MailToName2=
MailToName3=
MailToName4=
MailAddr1=RECEIVERMAIL # Please Change ME
MailAddr2=
MailAddr3=
MailAddr4=
EvtLevel=3
NotifyForNoEvent=NO

[SNMP]
TrapIp1=SNMP.SERVER.IP.ADDRESS # Please Change ME
TrapIp2=0.0.0.0
TrapIp3=0.0.0.0
TrapPort1=162
TrapPort2=162
TrapPort3=162
Community=public
EvtLevel=3

And configure it for startup at boot time.

chkconfig areca on

I know it very dirt, but I had no time left to search a better way, maybe in the future.

Hope this help

Bye
Riccardo

Print This Post Print This Post

In this tutorial I will describe how setup a complete mail server for relaying mail to another server, for example an IMAP server or an Exchange, tipically this machine will be placed in a DMZ network and will be reachable from internet.
I will explain how to install and configure :

- Postgrey : to do GreyListing on incoming mails and avoid SPAM.
- Postfix : to receive mails for local or remote mailboxes and do some checks for trash mail using RBL and internal options.
- Amavisd-New : to scan incoming mails form Viruses and SPAM.
- Clamd : to avoid Amavisd-new virus scan.
- Spamassassin : to avoid amavisd-new SPAM scan with Razor, Pyzor and DCC.
- Altermime : to add a disclaimer (both text and html) for the outgoing mail.
- Cyrus-Sasl : to enable sasl authentication for road warriors.
- Fetchmail : to eventually fetch the mail from another mail server.
- Apache : to create a reverse proxy to reach a webmail service on the internal web server.

I’ll describe all steps needed to install it on RHEL or CentOS because the steps are identical for both distributions and because are my favourites ones.
I’m assuming you’re going to install a 64bit operating system, but if you choose to install the 32bit version pay only attention for some packages to fit your architecture.

First of all you have to install you operating systems. I’ll not explain this process because it’s very simple, but I’ll suggest you to NOT customize the system during the installation procedure, it will be done later.

At the first reboot, disable both Selinux and Firewall, then reboot again and login to your system as user root.

Read the rest of this entry »

Virtualization Technology (VT) is a set of enhancements to CPU that improve performance for running a virtual machine by offloading some of the work to the new cpu extensions. Both AMD and Intel have CPU that support this technology.

In some cases it will be very usefule to know if your system can handle it.

It will be very simple from a Linux system because you should only take a look to /proc/cpuino looking for one of the following value :

  • vmx – (intel)
  • svm – (amd)

You can use grep to quickly find if either value exists in the file by running the following command:

egrep ‘(vmx|svm)’ /proc/cpuinfo

If your system supports VT, then you’ll see vmx or svm in the list of flags.

My system has four quad core AMD processors, so I’ll find the following repeated from 16 times :

My system has two processors, so there are two separate sections:

flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt

VT technology can still be disabled in your computer’s BIOS, however, so you’ll want to check there to make sure that it hasn’t been disabled. The flags in cpuinfo simply mean that your processor supports it.

VT is required to run 64-bit guests using Hypervisor like VMware Server or others similar products.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This post will explain how to create users and mailboxes for Zarafa.

I’ve tested it with CentOS 5.4 X86_64 and Zarafa 6.30.9 but it will fit with nearly all installation.

First of all you have to create a test file with all the users you want to create, providing the following information :

  • username
  • password
  • email address
  • name
  • surname

For example :

johndoe   password    johndoe@example.com    John    Doe
janedoe    password   janedoe@example.com     Jane     Doe

Save the text file whatever you want.

The following is the shell script that read your text file and create all Zarafa users.

If you use the script, all the users will not be administrator, so you have to create manually and administrator.


#!/bin/bash
# Written by Matteo Predieri - m.predieri_AT_damsistemi_DOT_it
# Written by Riccardo Riva - r.riva_AT_damsistemi_DOT_it
#
# Simple and raw script to create zarafa users and mailboxes from a test file
#
# The full fill-in of the text file is mandatory
# So fill in both "Name" and "Surname" with the following syntax
#
# username    password    email    name    surname
#

USERS_LIST=/tmp/userlists.txt
ZARAFA_CMD=/usr/bin/zarafa-admin
LOGFILE=/tmp/zara_user_creation.log

echo "Zarafa User Creation Log" > $LOGFILE
what="user";

for item in $( cat $USERS_LIST ); do

 if [ $what = "user" ]; then
 user=$item;
 what="passwd";
 else
 if [ $what = "passwd" ]; then
 passwd=$item;
 what="email";
 else
 if [ $what = "email" ]; then
 email=$item;
 what="name";
 else
 if [ $what = "name" ]; then
 name=$item;
 what="surname";
 else
 surname=$item;

 echo "Result in creating User: $user, with password: $passwd, with email address: $email with Full Name: $name $surname :" >> $LOGFILE
 FULLNAME="'$name $surname'"
 $ZARAFA_CMD -c $user -p $passwd -e $email -f "$FULLNAME" -a0 >> $LOGFILE
 echo -ne  "-------------------------------\n" >> $LOGFILE
 what="user";
 fi

 fi
 fi

 fi
done

# --- End of file ---

You’ve done.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This is a simple shell script that count all files in a local directory and all subdir. It doesn’t care what extention the file has, it will just recursively go through each directory and count any files within it, plus recurse into any more directories it finds. Give it as a parameter the directory you want to count.


#!/bin/sh
NUMBER=0
count ()
{
for temporary in $1/* ; do
if [ -d "$temporary" ] ; then
count "$temporary"
elif [ -f "$temporary" ] ; then
NUMBER=$(($NUMBER+1))
fi
done
}
count $1
echo "I found $NUMBER files, in the specified folder and all subfolders"

I know you should also did it with

ls -R | wc -l

but it was a shell exercise.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This wuold be a simple reminder for the most common tips for VI or VIM.
It would not be a deep dive on it, simply a memo for the most common feature used by me.

Search and replace a string in a document :
:.,$s/search_string/replacement_string/g

Disabling highlighted patterns :
Type “:nohl” to disable highlighting

Comment out several lines at once (I like that one) :
1. Ctrl + V (visual block)
2. Select the lines you want to be commented out
3. Shift + I (insert mode)
4. Add the “#” character on the first line
5. Press escape

Copy and paste several lines :
1. Shift + V (visual line)
2. Select the lines
3. Press “yyN” (N lines yanked), for example to copy 3 lines use “yy3″
4. Go where you want the block to be pasted and press “p”

Cut and paste several lines :
1. Let’s say you want to cut 3 lines, put your cursor on the first of the 3 lines
2. Type : 3dd
3. Locate your cursor where you want the block to be pasted
4. Type “p”

Some key combinations :
dd : Delete current line
D : Delete from cursor to end of line
d$ : Delete from cursor to end of line
d0 : Delete from cursor to beginning of line
dw : Delete from cursor to end of current word
db : Delete from cursor to beginning of current word
yy : Copy current line (use “p” to paste)

Here’s a great cheatsheet from http://www.worldtimzone.com/res/vi.html

Cursor movement

* h – move left
* j – move down
* k – move up
* l – move right
* w – jump by start of words (punctuation considered words)
* W – jump by words (spaces separate words)
* e – jump to end of words (punctuation considered words)
* E – jump to end of words (no punctuation)
* b – jump backward by words (punctuation considered words)
* B – jump backward by words (no punctuation)
* 0 – (zero) start of line
* ^ – first non-blank character of line
* $ – end of line
* G – Go To command (prefix with number – 5G goes to line 5)

Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
Insert Mode – Inserting/Appending text

* i – start insert mode at cursor
* I – insert at the beginning of the line
* a – append after the cursor
* A – append at the end of the line
* o – open (append) blank line below current line (no need to press return)
* O – open blank line above current line
* ea – append at end of word
* Esc – exit insert mode

Editing

* r – replace a single character (does not use insert mode)
* J – join line below to the current one
* cc – change (replace) an entire line
* cw – change (replace) to the end of word
* c$ – change (replace) to the end of line
* s – delete character at cursor and subsitute text
* S – delete line at cursor and substitute text (same as cc)
* xp – transpose two letters (delete and paste, technically)
* u – undo
* . – repeat last command

Marking text (visual mode)

* v – start visual mode, mark lines, then do command (such as y-yank)
* V – start Linewise visual mode
* o – move to other end of marked area
* Ctrl+v – start visual block mode
* O – move to Other corner of block
* aw – mark a word
* ab – a () block (with braces)
* aB – a {} block (with brackets)
* ib – inner () block
* iB – inner {} block
* Esc – exit visual mode

Visual commands

* > – shift right
* < – shift left
* y – yank (copy) marked text
* d – delete marked text
* ~ – switch case

Cut and Paste

* yy – yank (copy) a line
* 2yy – yank 2 lines
* yw – yank word
* y$ – yank to end of line
* p – put (paste) the clipboard after cursor
* P – put (paste) before cursor
* dd – delete (cut) a line
* dw – delete (cut) the current word
* x – delete (cut) current character

Exiting

* :w – write (save) the file, but don’t exit
* :wq – write (save) and quit
* :q – quit (fails if anything has changed)
* :q! – quit and throw away changes

Search/Replace

* /pattern – search for pattern
* ?pattern – search backward for pattern
* n – repeat search in same direction
* N – repeat search in opposite direction
* :%s/old/new/g – replace all old with new throughout file
* :%s/old/new/gc – replace all old with new throughout file with confirmations

Working with multiple files

* :e filename – Edit a file in a new buffer
* :bnext (or :bn) – go to next buffer
* :bprev (of :bp) – go to previous buffer
* :bd – delete a buffer (close a file)
* :sp filename – Open a file in a new buffer and split window
* ctrl+ws – Split windows
* ctrl+ww – switch between windows
* ctrl+wq – Quit a window
* ctrl+wv – Split windows vertically

Hope this help

Bye
Riccardo

Print This Post Print This Post

The following is a great memo for a lot of bash shortcuts.

Original link : HowToGeek

CTRL Key Bound

Ctrl + a – Jump to the start of the line
Ctrl + b – Move back a char
Ctrl + c – Terminate the command
Ctrl + d – Exit the current shell
Ctrl + e – Jump to the end of the line
Ctrl + f – Move forward a char
Ctrl + h – Same as backspace
Ctrl + k – Delete to EOL
Ctrl + l – Clear the screen
Ctrl + r – Search the history backwards
Ctrl + R – Search the history backwards with multi occurrence
Ctrl + t : Swap the last two characters before the cursor
Ctrl + u – Delete backward from cursor
Ctrl + w : Delete the word before the cursor
Ctrl + xx – Move between EOL and current cursor position
Ctrl + x @ – Show possible hostname completions
Ctrl + z – Suspend/ Stop the command. “fg” restores the suspended command.

ALT Key Bound

Alt + < – Move to the first line in the history
Alt + > – Move to the last line in the history
Alt + ? – Show current completion list
Alt + * – Insert all possible completions
Alt + / – Attempt to complete filename
Alt + . – Yank last argument to previous command
Alt + b – Move backward
Alt + c – Capitalize the word
Alt + d – Delete word
Alt + f – Move forward
Alt + l – Make word lowercase
Alt + n – Search the history forwards non-incremental
Alt + p – Search the history backwards non-incremental
Alt + r – Recall command
Alt + t – Move words around
Alt + u – Make word uppercase
Alt + back-space – Delete backward from cursor

More Special Keybindings

$ 2T – All available commands(common) (2T means press the TAB twice)
$ (string)2T – All available commands starting with (string)
$ /2T – Entire directory structure including Hidden one
$ 2T – Only Sub Dirs inside including Hidden one
$ *2T – Only Sub Dirs inside without Hidden one
$ ~2T – All Present Users on system from “/etc/passwd”
$ $2T – All Sys variables
$ @2T – Entries from “/etc/hosts”
$ =2T – Output like ls or dir

Hope this help

Bye
Riccardo

Print This Post Print This Post

The following is a simple memo on how to use the three powerful tools above.
It will not be a complete guide, and you should find only a small part of the possible use, but this would be a review only for the most common use (for me) and would not go a deep dive.

Grep

grep : print lines matching a pattern (equals “grep -G” which is the default)
egrep : equals grep -E (interpret extended regexp)

grep -n : line numbered
grep -i : ignore case
grep -c : count matches
grep -v : print non-matching lines
grep -r : recursivity, read all files under each directory

grep pattern file.txt

Will display the lines containing the pattern

grep -c pattern file.txt

Will display how many lines contain the pattern

grep -i pattern file.txt

Will display the lines containing the pattern regardless of the case

grep -A 1 -B 1 pattern output.txt

Will print the one line before (-B) and one line after (-A) the matching pattern

Awk

awk '{ print $0 }' file

Output the content of the file

awk '{ print $2 }' file

Output the second field of data of the file, space is the default separator

awk -F ':' '{ print $2 }' file

Same but separator is “:”

Sed

cat file | sed -e 's/old_pattern/new_pattern/g'

sed would replace old_pattern by new_pattern in the output

cat file | sed -e '4,10s/old_pattern/new_pattern/g'

sed would replace old_pattern by new_pattern in the output between line 4 and 10

cat file | sed '/pattern/d'

Delete a pattern

cat file | sed '/pattern/!d'

Delete everything but the pattern (this equals grep “string_to_remove”)

Hope this help

Bye
Riccardo

Print This Post Print This Post

This would be another little memo of all the most used (by me) Input/output redirections and pipes.
As other times this would not be a complete guide, and would not go into the deep.

The base :

Standard input (stdin) : 0
Standard output (stdout) : 1
Error output (stderr) : 2

> : output redirection
< : input redirection
>> : append output
| : pass the output to the next utility
|| : execute the next command if the previous failed
&& : execute the next command only if the previous has succeded

Examples :

cat file.txt > /tmp/file.txt

If file.txt exists, the output will be written in /tmp/file.txt
If file.txt doesn’t exist, the error message will be output to the screen while nothing will be written to /tmp/file.txt (but the file will be created if not existing)

cat file.txt 2> /tmp/error_file.txt > /tmp/file.txt

In this case the error message would be written to /tmp/error_file.txt if the file doesn’t exist

cat file.txt 2> /tmp/error_file.txt >> /tmp/file.txt

The error output would still go in error_file.txt while the standard output would be APPENDED to file.txt

cat file1 > file2

Will copy the content of file1 to file2

 cat &gt; file3

Hello world

Will write “Hello world” to file3

[/bash]wc < file.txt > count_file.txt[/bash]

This would count the number of lines, words and bytes in file.txt (”wc < file.txt”) and output the result to count_file.txt

Several ways of counting lines etc. :
$ wc file.txt
7 2 17 file.txt
$ wc < file.txt
7 2 17
$ cat file.txt | wc
7 2 17

ls -l test10 > file.txt 2>&1

This would output anything under file.txt

<code>ls -l test10 2>&1 | mail -s “output in a mail” address@domain.com
Any output in a mail

Hope this help

Bye
Riccardo

Print This Post Print This Post

Which Linux distribution is more suitable for you regarding your experience and preferences ?

find it at :

http://www.zegeniestudios.net/ldc/index.php?lang=en

Mine was 100% Debian / 100% Ubuntu

No surprises on that !

Bye
Riccardo

This post will explain how to disable IPv6 in a system running Debian GNU/Linux or Ubuntu Linux.

You should want to disable IPv6 for compatibility reason or if you not plan to use it for speed up your system and/or to avoid loading of unuseful modules on system start up.

For disable the protocol you have to edit

/etc/modprobe.d/aliases

file and change two lines as follow :

#alias net-pf-10 ipv6
net-pf-10 off

You should also tell to your kernel to not load IPv6 module by blacklisting it at the boot, for doing so edit

/etc/modprobe.d/blacklist 

file adding the following line :

blacklist ipv6

Reboot your system and check with the following command if the module ipv6 is not present :

lsmod |grep ipv6

You’ve done

Hope this help

Bye
Riccardo

Print This Post Print This Post

This pot will show some useful example on how to use the “grep” command to find an occurency in a Linux system.
Grep searches the input file (or files) for lines containing a match to a given pattern. Whe an occurency math it copies to standard input the line with the occurrency or you should rediret the output whatever you want.

You should simpy invoke grep with the following :

grep 'STRING' filename

The above is a very simple use f grep, infact it check only in a single file searching all the occurency for ‘STRING’.
You should also use it to find ‘STRING1 STRING2′ in all files in your current location, running ::

grep 'STRING1 STRING2' *

or if you want to make the same research on a given path (i.e. all files in /etc) you should use :

grep 'STRING1 STRING2' /etc/*

Notice the use of single quotes; This are not essential but in this example it was required since the name contains a space. Double quotes could also have been used in this example.

In case of too much occurency you should redirect the output o a file to make more comfortable the research, for example using :

grep 'STRING 1 STRING2" /etc/* > /tmp/grepresults.txt

The following is a simple list for Grep Regular Expression

grep can search for complicated pattern to find what you need using some special characters used to create a regular expression:

`.’ The period `.’ matches any single character.

`?’ The preceding item is optional and will be matched at most once.

`*’ The preceding item will be matched zero or more times.

`+’ The preceding item will be matched one or more times.

for example, a regular expression search would be :

grep "\<[A-Za-z].*" file

The search above will search for any word which begins with a letter upper or lower case.

For more details check :

man grep

Hope this help

Bye
Riccardo

Print This Post Print This Post

Very often it happens that I must give support to a colleague or a customer on a Linux machine.

It’s very difficult to spell all bash command I will use to check which could be the problem, especially by phone.

When I discover “screen” it was a revelation.

With screen http://www.gnu.org/software/screen/screen.html you should share a linux session with other people.

You should use screen by simply ask to the user you want to assist to type on the console the following command :

screen

So if you can connect to the machine (even with ssh) you should run the following command :

screen -x

to share the same bash session.

Hope this help

Bye

Riccardo

Print This Post Print This Post

The following is a simple script to monitor a Software Raid configuration on a Linux System.
It execute a /proc/mdstat check to search a ‘blocks_’ occurency which indicates problems on the Raid system and in case of match it notify the system administrator with a mail message.

It should be very useful for non-presidiated system, even if I always prefer Hardware Raid.


#!/bin/bash
#
# Script created by Riccardo Riva
# http://www.riccardoriva.com
#
# It check a Software Raid subsystem and notify by mail any occurency problem

# Define variable
LOG_FILE=/tmp/raid-check.log
SYSTEM=`uname --nodename`
MAILTO='systemadmin@mail.exp'

# Checking /proc/mdstat
cat /proc/mdstat | grep 'blocks.*_' > $LOG_FILE

# Define function in case of problems detected
if [ $? -eq 0 ]
then
echo "The $SYSTEM system has RAID failures on it." >> $LOG_FILE
echo "Below is the output from /proc/mdstat" >> $LOG_FILE
echo "===========================================" >> $LOG_FILE
cat /proc/mdstat >> $LOG_FILE
echo "===========================================" >> $LOG_FILE
cat $LOG_FILE | mail -s 'URGENT: RAID disk failure detected' $MAILTO
fi

# Deleting log file
rm -f >> $LOG_FILE

# Exit
exit 0

Save the file above as /usr/local/bin/raidcheck.sh  and assign to it correct permission and ownership with the folowing :

chmod 700 /usr/local/bin/raidcheck.sh
chown root:nobody /usr/local/bin/raidcheck.sh

Change the email address variable with a real email address who will receive the notification.

You should now schedule the script execution at every time interval you want (i.e. twice a day, or hourly if you are paranoic).

Remember that is better to be paranoic that have a degraded system with unrecoverable data.

Hope this help

Bye
Riccardo

Print This Post Print This Post

Hi all, the following will be a quick and dirt list of commands for use “dd” in a linux system for various task.

First of all you should create an Hard Disk Image, for example if you want to create an image of your /dev/sda device, you should have another disk (with a writable partition) (i.e. /dev/sdb) with a directory (i.e. /home mounted on it) and simply type :

dd if=/dev/sda of=/home/sda.bin

Or even a partition backup using the same disk device for source and destination like the following (if for example you have /home mounted on /dev/sda2) :

dd if=/dev/sda1 of=/home/sda1.bin

You should even create a compress image of the same disk above, using GZIP, simply type the following :

dd if=/dev/sda1 | gzip > /home/sda1.bin.gz

One of the most useful use of dd (it’s saved my life a lot of time) will be :

dd if=/dev/sda of=sda.boot.mbr bs=512 count=1

With the above command, you have backuped up your MBR (Master Boot Record) of your /dev/sda device and in case of disaster you should restore it, for example booting with a live CD using the following :

dd if=sda.boot.mbr of=/dev/sda bs=512 count=1

You should also create a manual RAID1 between two disk by executing the followinf script with a cron job :


#!/bin/sh
#

LOG="/var/log/mirror.log"
ADMIN="yourmail@yourprovider.ext"
ERROR=0

echo `/bin/date` >$LOG 2>&1
/bin/dd if=/dev/sda of=/dev/sdb bs=1M >>$LOG 2>&1
ERROR=$?
echo `/bin/date` >>$LOG 2>&1

if [ $ERROR -ne 0 ]; then
cat $LOG | /bin/mail -s "Report mirror `uname -n`" $ADMIN
fi

The above sript will copy the entire /dev/sda to /dev/sdb logging the process and send an email to your email address at the end for debug and monitoring purpose.
If the first hard disk (/dev/sda) fail, you should phisicaly umount it, and subsitute it with /dev/sdb then reboot the system.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This simple post will show how to configure Ethernet Bonding on two (or more) network interfaces on RHEL 5 or CentOS 5.
I’ve tested this configuration on a CentOS 5.2 with kernel 2.6.18-92.1.22.el5 as you could see below :

uname -a

Linux serverlab.riccardoriva.local 2.6.18-92.1.22.el5 #1 SMP Tue Dec 16 11:57:43 EST 2008 x86_64 x86_64 x86_64 GNU/Linux

cat /etc/redhat-release
CentOS release 5.2 (Final)

If you want to create a bonding on two interface (i.e. eth0 and eth1) you should do the following :

Edit /etc/sysconfig/network-scripts/ifcfg-eth0

# Bonding eth0 to bond0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=NO

Edit /etc/sysconfig/network-scripts/ifcfg-eth1

# Bonding eth1 to bond0
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=NO

Copy /etc/sysconfig/network-scripts/ifcfg-eth1 to /etc/sysconfig/network-scripts/ifcfg-bond0 to keep the same file permission by executing the following commands :

cd /etc/sysconfig/network-scripts
copy ifcfg-eth1 ifcfg-bond0

Edit /etc/sysconfig/network-scripts/ifcfg-bond0

ifcfg-bond0
DEVICE=bond0
BOOTPROTO=none
ONBOOT=yes
NETWORK=10.100.100.0
NETMASK=255.255.255.0
IPADDR=10.100.100.1
USERCTL=NO

Edit /etc/modprobe.conf adding the following line :

alias bond0 bonding

Reboot your system to let modules be loaded or load it manually with the following command :

insmod bond0 bonding

If you haven’t rebooted your system, restart your network with the following command :

/etc/init.d/network restart

You should check if bonding is working you should look at /proc/net/bonding/bond0 with the following command :

cat /proc/net/bonding/bond0

and you should see something similar to the following :

Ethernet Channel Bonding Driver: v3.2.4 (January 28, 2008)

Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 0
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:15:17:88:5a:3c

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:15:17:88:5a:3d

You’ve done

Hope this help

Bye
Riccardo

Print This Post Print This Post

I’m using this script in a lot of Linux Server, especially some file server with a little ERP software written in COBOL with all files in /home/erp.
I’m saving all /home and /etc to have a full backup for disaster recovery purpose.

This scrit will do the following :

- Define variables
- Define DATE
- Create a compress archive of /home and /etc
- Rewind the TAPE
- Write all the data on TAPE
- Verify the TAPE
- Eject the TAPE

Obviously all the output of the above operations will be redirected to a log file fr diagnostic purpose.

- Mail all logged operations to the Server admin

The only prerequisites for let all works is “mail” packages installed on your system and a local mail server configured for routing mail to your “Server Admin” email address, I’m using Postfix, but you coul’d use whatever you want (eg. QMail or EXIM).
Read the rest of this entry »

In some situation you may want to avoid loading a Linux driver module automatically . For example:

- In some cases buggy driver causes kernel BUG or system fault on load so you just want to avoid the problem.

- If your system connected without a diskette / floppy drive; kernel will try to load floppy driver – disable floppy driver or module.

The Linux kernel get module information on boot from /etc/modprobe.conf file and /etc/modprobe.d/* file(s).

If you are using RHEL or CentOS do the following :

open your /etc/modprobe.conf file and turn of auto loading using following syntax:

alias driver-name off

If you are using Debian or Ubuntu do the following :

open /etc/modprobe.d/blacklist file and add driver name using following syntax:

blacklist driver-name

Reboot your system and use lsmod command to show the status of modules in the Linux Kernel.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This simple init script is for daemonize freshclam (Clamd Antivirus Updater) on RHEL or CentOS.

Create a brand new file called freshclam in /etc/init.d/ with the following content :


#!/bin/sh
#
# Startup script for the Clam AntiVirus Update Tool
#
# chkconfig: 2345 62 38
# description: freshclam is an update daemon for Clam AV database.
#
# processname: freshclam
# config: /etc/freshclam.conf
# pidfile: /var/run/clamav/freshclam.pid
#
# Author: Riccardo Riva
# WebSite: http://www.riccardoriva.com
#
# This script is realeased under the terms of the GPL.
#====================================================================

# Source function library
. /etc/init.d/functions

# Get network config
. /etc/sysconfig/network

test -f /etc/freshclam.conf || exit 0

RETVAL=0

start() {
echo -n $"Starting freshclam: "
daemon /usr/bin/freshclam -d -p /var/run/clamav/freshclam.pid
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/freshclam
return $RETVAL
}

stop() {
echo -n $"Stopping freshclam: "
killproc freshclam
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/run/clamav/freshclam.pid /var/lock/subsys/freshclam
return $RETVAL
}

restart() {
stop
start
}

reload() {
echo -n $"Reloading DB: "
killproc freshclam -ALRM
RETVAL=$?
echo
return $RETVAL
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status freshclam
;;
restart)
restart
;;
condrestart)
[ -f /var/lock/subsys/freshclam ] && restart || :
;;
reload)
reload
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
exit 1
esac

exit $?

Assign to it corect ownership and permission :

chown root:root /etc/init.d/freshclam
chmod 755 /etc/init.d/freshclam

Test if the scripts works correctly on your system with the following commands :

service freshclam start
service freshclam stop
service freshclam restart
service freshclam reload
service freshclam condstart

If all works fine you should add freshclam to the automatic startup services with the following command :

chkconfig freshclam on

Hope this help
Bye
Riccardo

Print This Post Print This Post
Contacts
Look at me at Linkedin Follow me on Twitter
My Flickr Albums My Facebook profile My YouTube Videos
SkypeMe My Linux Counter GMail me
Search
Google Search
Categories
Tag Cloud 3D
FeedBurner RSS

Visitors
Locations of visitors to this page
VMware related Blogs
The following are Blog sites with feeds I personally follow. When I'll have some spare time I will complete all Feed and Twitter links.