Wednesday, February 17, 2010

How to get the percentage of Command executed?

Get percentage of Command executed

Do you want to know which command on you Linux box has executed the most and also would you like to get the Percentage. This is very Simple and can be achieved useing awk, grep. It can be acheived in many other possible ways too.


root# history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10

OutPut:

1 133 13.3% ls
2 127 12.7% cd
3 78 7.8% vi
4 35 3.5% netstat
5 32 3.2% postfix
6 31 3.1% tail
7 30 3% mv
8 29 2.9% telnet
9 29 2.9% ll
10 23 2.3% postqueue

In the above example ls was the most executed command with share of 13.3% following with cd with 12.7 and then rest.

Understanding and Reading Mail Logs, Postfix Logs, Sendmail Logs, Qmail Logs

How do I read Mail Log?

Logs play a crucial role in identifying the issue may it be Basic Problem or a Big Problem heap, logs generated can make a System Admins life quite easy if you know what exactly they say. Understanding Mail Logs, Postfix Logs, Sendmail Logs or any other MTA are all the same. All the Mail Server Programs are Logging aware.

Lets get to work and understand the Logs generated by RSyslog Daemon. I am Using Fedora 11 with Sendmail 8.14 installed on it. Sendmail Listens on Two ports
25 : MTA (Mail Transfer Agent)
587 : MSP (*Mail Submission Program/Port)

Example Log:

Feb 4 06:10:09 techy sendmail[5392]: o140e90B005392: from=, size=2434, class=0, nrcpts=1, msgid=<201002040040.o140e9Mi005380@techy.bounceme.net>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1]
Feb 4 06:10:09 techy sendmail[5380]: o140e9Mi005380: to=root, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=32168, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (o140e90B005392 Message accepted for delivery)



Feb 4 06:10:09 = Date of Mail received to the Mail Transfer Agent
techy = Hostname
sendmail[5392] = MTA or Application Name with PID for the Mail
o140e90B005392 = Queue ID
from= = Sender Address
to= Reciepient Address
Size=2434 = Size in Bytes
nrcpts=1 = Number of Recepients
msgid=<201002040040.o140e9Mi005380@www.linuxmaza.com> = Message ID (Unique of every Mail)
proto=ESMTP = Protocol Used
daemon=MTA = Application Handler In Sendmail it could be MSP or MTA
relay=localhost [127.0.0.1] = Mail relayed to the next Destination Server / Recipient Server
"delays=a/b/c/d"
where a=time before queue manager, including message transmission;
b=time in queue manager; c=connection setup time including DNS,
HELO and TLS; d=message transmission time. Numbers smaller than 0.01 seconds
are rounded to 0, to reduce the noise level in the logfile.

orig_to = This is omitted when the address did not change.
conn_use = This is omitted when a connection is used once.
ctladdr=root = Controlling user
dsn=2.0.0 = Delivery Status Notification, It can be 2.x.x, 4.x.x, 5.x.x Where 2.x.x is Successfully Sent, 4.x.x Is Mail Temporarily Deferred, 5.x.x stands for Permanent Failure
stat=Sent = Status of the Message. Values Sent,Deferred,Bounced

All the above parameters are almost same generated in Postfix Logs, Sendmail Logs, Qmail Logs also there could be some extra information about the mail added into the logs.

Solved: PHP Fatal error: Call to undefined function json_encode(), How to Install JSON in PHP

PHP 5.2 has included inbuilt json_encode() and json_decode() functions by default. Using any version prior to PHP 5.2 doesn't have native JSON support. You need to compile and install json.

JSON stands for "JavaScript Object Notation" is a lightweight data-interchange format. For More Information in JSON Visit JSON Website

Error : "PHP Fatal error: Call to undefined function json_encode()"

How to install JSON in 2 easy steps.
1. Use PECL JSON Package. Log in as root and run the command
root# pecl install json
This will compile the JSON package.
2. Create a file named json.in in /etc/php.d or Check the Module Location in php.ini. Add extension=json.so to the file, and you are done
root#touch /etc/php.d/json.ini
root# echo "extension=json.so" > /etc/php.d/json.in

Restart Apache and run you application.
root# service httpd restart

You have successfully installed JSON.

PHP Fatal error: Call to undefined function json_encode, How to install JSON in PHP

PHP 5.2 has included inbuilt json_encode() and json_decode() functions by default. Using any version prior to PHP 5.2 doesn't have native JSON support. You need to compile and install json.

JSON stands for "JavaScript Object Notation" is a lightweight data-interchange format. For More Information in JSON Visit JSON Website

Error : "PHP Fatal error: Call to undefined function json_encode()"

How to install JSON in 2 easy steps.
1. Use PECL JSON Package. Log in as root and run the command
root# pecl install json
This will compile the JSON package.
2. Create a file named json.in in /etc/php.d or Check the Module Location in php.ini. Add extension=json.so to the file, and you are done
root#touch /etc/php.d/json.ini
root# echo "extension=json.so" > /etc/php.d/json.in

Restart Apache and run you application.
root# service httpd restart

You have successfully installed JSON.

Friday, February 5, 2010

How to mount RAMFS, TMPFS in Centos, RHEL, Fedora, Ubuntu, Linux

How to Use and mount RAMFS / TMPFS in Centos, RHEL, Fedora, Ubuntu?
Definition :
RAMFS Random Access Memory Filing System
TMPFS Temporary File System / Temporary File Storage

RAMFS / TMPFS is used to assign or allocate a part of physical memory to be used as a individual partition which can be used for storing data (Reading, Writing) like all other Disk partition on a system. As the data is stored in RAM (Random Access Memory) it is much faster than that of writing or reading from a System Physical disk Partition.
Performance can be measured when writing or Reading a large file from this partition (Small File size wont give you the performance). Mounting RAMFS and TMPFS is same on almost all linux distribution RHEL, Centos, Ubuntu, Fedora.

1. How to mount Tmpfs

# mkdir -p /mnt/tmp
# mount -t tmpfs -o size=200m tmpfs /tmp/ashfs

The last line in the following df -h shows the above mounted /tmp/tmpfs tmpfs file system.

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/hda2 24G 11G 12G 47% /
/dev/hda1 99M 12M 83M 12% /boot
tmpfs 248M 0 248M 0% /dev/shm
tmpfs 200M 0 200M 0% /tmp/ashfs

2. How do I mount Ramfs on the System?

# mkdir -p /tmp/ashfs

# mount -t ramfs -o size=200m ramfs /tmp/ashfs

You can change the size of the partition on the Fly.

# mount -o remount,size=2G /tmp/ashfs
It grows dynamically
You can verify the mount using

# mount
/dev/hda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
tmpfs on /tmp/ashfs type tmpfs (rw,size=200m)


By adding the partition in /etc/fstab they RAMFS/TMPFS can be mounted on boot time.







RAMFS
Uses RAM of a System
It grows dynamically
Allows to write data above the partition created
After Full Usage system may hang or stop working.
Data would be deleted once system is rebooted or incase of crashing.
You have to make a process to transfer data from RAMFS to Physical disk periodically to loose data
Suitable to have less data and read intensive
TMPFS
Uses Swap of a System.
It doesn't grow dynamically.
Doesn't allow to write data above the partition created.
System would give an error display “No space left on device”.
Data would be deleted once system is rebooted or incase of crashing.
You have to make a process to transfer data from RAMFS to Physical disk periodically to loose data.
Suitable if you want to write very fast Write intensive.


Example : You have a system with 2GB RAM and 2 GB Swap Space
RAMFS : When a partition of 1 GB is created and mounted users can even write more than 1GB till the total RAM size is exhausted as the parition is made on RAM.

TMPFS : When a partition of 1 Gb is created and mounted users wont be able to use more than 1GB space as designated on the partition. If required the space can be extended on a fly. Useses dont need to worry to control the process that writes on tmpfs partition

Thursday, January 21, 2010

How to Enable htaccess in Apache?

What is htaccess?
Htaccess files are files containing single or several configuration apache directives, which should be placed inside the web directory where changes have to made and all its sub directories.

The easient way to enable .htaccess file in apache is tp add the following in the Apache / HTTPD conf, Uncomment the below line and restart apache. You need to enable rewrite module

#LoadModule rewrite_module modules/mod_rewrite.so to

LoadModule rewrite_module modules/mod_rewrite.so

Add this to the Apache Main Configuration.
AccessFileName .htaccess
.htaccess file can have different name just specify the name following
AccessFileName your_htaccess_file_name

Pros
Useful when Administrators, Developers, requires to make configuration changes on per directory basis
Useful for ISP and Webhosting providers to permit multiple users running multiple sites on the same server to change thier own htaccess file.

Cons
Never use .htaccess files if you dont have the access to the Server Configuration File.
Uses Need to have root or Super User access to edit the main configuration file in apache.
Never Use user authetication in htaccess file instead do it in the Apache Main Configuration.
AllowOverride when set to allow apache will search for .htaccess file in every directory which would lead in poor performance of the webserver.

Directives in a .htaccess file

SSI (Server Side includes .htaccess file) Samples

Options +Includes
AddType text/html shtml
AddHandler server-parsed shtml


CGI Samples

Options +ExecCGI
AddHandler cgi-script cgi pl

Live Examples :


Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all

Tuesday, January 12, 2010

Simple Steps to Send Mail Using Sendmail in Linux, Fedora, Centos, Ubuntu

Have you got your Linux distribution like Fedora, Centos, Redhat, Gentoo up and running?

Have you found issues sending mail from linux or send mail from command line?

Having Problem in Manageing Linux Sendmail Mail Server to send your First Mail?

Your Answer to all your Questions ends Here..

Follow the below step by step guide how to send mails from the default installation of Sendmail. No Sendmail Configuration required to be changed.

Sendmail is generally used as a Mail server for relaying all your mails for your Local Area Network and also used as Direct delivery. Sendmail is very powerful and can be build as a standalone Centralized High Avaliability Mail Server for a organization. It has the capability to stop SPAM Stop Virus mail with 1000's of Trick which can be best suited for your needs. As Sendmail is Open Source Free to Use, Develop and Distribute its the best choice for todays Linux System Administrator. The Sendmail Configuration is as easy as adding or removing a line from a word file "here You must have a bit of knowledge what is to be deleted or added", Today it comes default installed with majority of Linux distribution avaliable worldwide. And if you dont find it installed just google it THE BIG G "Referring Google" will give you precisely the exact rpm (linux executable program) for your distribution of linux.

Its popularty increased with the Integration of Sendmail Milters (Mail Filter which are Highly Customizab;e) which have really changed the Fucntionality of Mail Server Today

Step 1.
Check if Sendmail is Running for which execute the following command :
ps ax | grep sendmail
If you get the output something like this
1846 ? Ss 0:13 sendmail: accepting connections
1850 ? Ss 0:00 sendmail: Queue runner@00:15:00 for /var/spool/clientmqueue
1855 ? Ss 0:00 sendmail: Queue runner@00:15:00 for /var/spool/mqueue

Great Your Copy of Sendmail is Running
OR Type the command in the terminal
/etc/init.d/sendmail start
You can see Sendmail Starting
Starting sendmail: [ OK ]


Step 2.
Next Step is to check Sendmail Listening IP:port. Listening IP:Port is nothing but the IP address and port number on which sendmail has started and will accept mails other than which connection would be refused. To Check enter the following:
[root@techy ~]# netstat -tnlp | grep sendmail
If You Get the resultant as this
tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN 1846/sendmail: acce
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 1846/sendmail: acce


Hurray !!! Your sendmail is ready to accept connection on all the interfaces of your machine. Here “0.0.0.0:587” is Sendmail Mail Submission Port and “0.0.0.0:25” is used to Send mail to External SMTP Source. IF You Need to make sendmail listen on specific port you need to change the sendmail.mc and Build a new Sendmail.cf which we would cover in the Sendmail Configuration Part.

Sendmail.mc : It is the macrotized Version for sendmail configuration
Sendmail.cf : IT is read by Sendmail Deamon to perform task.

BY Default Sendmail Does not allow mails to be relayed to external sources. We need to make sendmail configuration to perform such tasks. In this example we’ll send mail to local user which is “root”

Step 3.
Now the Final Steps is to check if Mail are sent properly
We’ll use Telnet if you are not familiar with telnet don’t worry just copy the code and it will do the magic for you.
[root@techy ~]# telnet 127.0.0.1 25
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 techy.bounceme.net ESMTP Sendmail 8.14.3/8.14.3; Tue, 12 Jan 2010 23:29:14 +0530
mail from:support@linuxmaza.com
250 2.1.0 support@linuxmaza.com... Sender ok
rcpt to:root@localhost
250 2.1.5 root@localhost... Recipient ok
data
354 Enter mail, end with "." on a line by itself
subject: My First Test Mail Using Sendmail.

This is First Paragraph.
.
250 2.0.0 o0CHxEYV022243 Message accepted for delivery
quit
221 2.0.0 techy.bounceme.net closing connection


Once You receive the final Message accepted for delivery. Great Just remember the first part of the line “o0CHxEYV022243” which would be helpful during crisis or troubleshooting sendmail. Now it’s the time to check the mail We’ll use mutt to check the mail.
[root@techy ~]# mutt
And check the mail. IF you Did not receive the mail You need to troubleshoot the issue checking the logs. The default location to check the logs for sendmail is /var/log/maillog or /var/log/mail.
Command to check: Do you remember the “o0CHxEYV022243” which we asked you earlier just parse the logs using the same you would get the reason for non-delivery or success messages.

[root@techy ~]# cat /var/log/maillog | grep "o0CHxEYV022243"
Jan 12 23:29:29 techy sendmail[22243]: o0CHxEYV022243: Authentication-Warning: techy.bounceme.net: localhost [127.0.0.1] didn't use HELO protocol
Jan 12 23:30:22 techy sendmail[22243]: o0CHxEYV022243: from=support@linuxmaza.com, size=70, class=0, nrcpts=1, msgid=<201001121759.o0CHxEYV022243@techy.bounceme.net>, proto=SMTP, daemon=MTA, relay=localhost [127.0.0.1]




We would be adding some Sendmail Videos. Please let us Know how did you like the information provided above and any suggestions. Drop a Mail to admin@linuxmaza.com