Configuring mod_dumpio with apache2 in Ubuntu

 mod_dumpio allows for the logging of all input received by Apache and/or all output sent by Apache to be logged (dumped) to the error.log file.

The data logging is done right after SSL decoding (for input) and right before SSL encoding (for output). As can be expected, this can produce extreme volumes of data, and should only be used when debugging problems.

To configure mod_dumpio you need to enable dump_io.

$ sudo a2enmod dump_io

Next add below mentioned 3 lines in /etc/apache2/apache2.conf

#To capture http request
DumpIOInput On

#To capture http response
DumpIOOutput On 

#To capture everything
DumpIOLogLevel debug

Now change LogLevel in your /etc/apache2/sites-enabled/000-default or /etc/apache2/sites-available/default file to debug.

LogLevel debug

After doing all above changes restart apache2.

$ service apache2 restart

To view logs you need to check /var/log/apache2/error.log

$ tail -f /var/log/apache2/error.log

To test make a dummy request as shown below and at the same time view error.log file where you can lot of lines related to this request.

$ curl http://127.0.0.1/ -d group=user -d sort=name

Since dump_io will create log of disk io, enabling it in production servers is not a good idea. It will slow down your production server.

How to clear swap space – Ubuntu/Linux

 

To clear swap space in Ubuntu/Linux we need to use swapoff command as shown below:

$ swapoff -a

Above command will turn of swap completely.

Before running swapoff command you need to have enough free RAM available, since swapoff command will move all swap data to RAM.

If you wont have enough free RAM this command may make your system unstable.

Again to turn on swap you need to use swapon command ash shown below:

$ swapon -a

You need to have root/sudo permissions to run above commands.

Number of open files or file descriptors associated with a process

Since in *nix every thing is a file, When you try to find open files or file descriptor associated with a process you will see Actual file log, Unix Sockets, Library files, Network Sockets,  /dev files and others.

lsof is a command to list all open files.

To list all open files or file descriptors by a process:

$ lsof -p PID

or

$ lsof -a -p PID

or

$ ls /proc/PID/fd

where PID is a process ID of any process that is running.

To count all open files by a process:

$ lsof -p PID | wc -l

or

$ ls /proc/PID/fd | wc -l

To list all open files:

$ lsof 

To count all open files:

$ lsof | wc -l

Python – remove newline from readlines

 

readlines() is a function in python used to read text file and convert it into list where each line in at file is a element in list with new line(\n) at the end of each element.

Example:

>>> filelist = open("file.txt").readlines()
['a\n', 'b\n', 'c\n', 'd\n']

If you want to remove new line(\n) at the end of all elements in list use read() and splitlines() functions as shown in below example.

read().splitlines() will do same funcationality as readlines() & only difference is read().splitlines() won’t have have new line(‘\n’) at the end of all elements in list.

Example:

>>>open("file.txt").read().splitlines()
['a', 'b', 'c', 'd']

Python – check if string starts with number

With the help of isdigit() function we can check if a string start with number or not.

Lets check with following examples:

>>> input = '1viewsby'
>>> input[0].isdigit()
True

Above example is returned true since input string is started with with number.

>>> input = 'viewsby'
>>> input[0].isdigit()
False

Since input is is not started with number above example returned false.

iostat – installation and usage

 

The iostat command is used for monitoring system input/output device loading by observing the time the devices are active in relation to their average transfer rates.

The iostat command generates reports that can be used to change system configuration to better balance the input/output load between physicaldisks.

How to install iostat:

iostat is available in sysstat module, so to install iostat we need to install sysstat module.

Use following command to sysstat:

$ apt-get install sysstat

Examples:

To print all disks IO stat:

$ iostat

To print a specific disk IO stat:

$ iostat /dev/sdb

To contiuesly print a specific disk IO stats every 10 seconds:

$ iostat -d 10 /dev/sdb

To display device utilization and extended statistics in Kilobytes:

$ iostat -dkx

Ubuntu install gnome classic from command line

Recent versions of Ubuntu is coming with Unity desktop which are bit heavy.

We can also install gnome classic from command line.

To install gnome classic or gnome session fallback use following command:

$ sudo apt-get install  gnome-session-fallback

After installing just logout from this session and relogin.

Before logging in click the icon(Ubuntu Icon) in the top right of the Login box and select Gnome classic and login to you account.

This will take to gnome classic desktop.

Lighttpd – block user agent

 

In lighttpd we can block or deny access to a specific user agent.

Syntax to block user agent:

$HTTP["useragent"] =~ "User agent name" {url.access-deny = ( "" )}

where “User agent name” is any user agent.

Example:

$HTTP["useragent"] =~ "Xenu Link Sleuth" {url.access-deny = ( "" )}

Above example will block all requests with user agent name “Xenu Link Sleuth”.

Nmap – Scan for open ports

 

nmap is a network exploration tool and security/port scanner.

We can list all open ports using nmap for a given IP/Host.

Use following command to list all open ports:

$ sudo nmap -v -sS 192.168.1.1

Use following command to scan specific ports:

$ nmap -sS -O -p80,8080 192.168.1.1

To can specific range of ports(scan from port 80 to 100):

$ sudo nmap -sS -O -p80-100 192.168.1.1

Ping all IP’s in a subnet

 

We can use nmap command to ping all IP’s in subnet.

If you don’t have nmap installed in your machine install nmap with following command:

$ apt-get install nmap

Use following command to ping all IP’s in a subnet:

$ nmap -n -sP 192.168.2.0/26

Output:

Starting Nmap 5.21 ( http://nmap.org ) at 2014-06-30 17:04 PDT
Nmap scan report for 192.168.2.1
Host is up (0.00073s latency).
Nmap scan report for 192.168.2.2
Host is up (0.0011s latency).
Nmap scan report for 192.168.2.4
Host is up (0.0010s latency).
Nmap done: 64 IP addresses (3 hosts up) scanned in 0.62 seconds

In above output we can see all reachable IP’s.