Python – convert dictionary to json

 

To convert any dictionary to json use json.dumps(YourDictionary).

Below is an example to convert a dictionary to json:

import json

def toJson():
    d = {'a' : 'Apple', 'b' : 2}
    return json.dumps(d)

print toJson()

Output:

{“a”: “Apple”, “b”: 2}

lighttpd – allow/block ip range

 

To allow some or multiple ip rages use below code:

$HTTP["remoteip"] =~ "192\.168\.1\.*|10\.2\.20\.*" {
    server.document-root = "/var/www"
}

Above code will allow only 192.18.1.* and 10.2.20.* ip rages and blocks all other ip’s.

To block some or multiple ip ragnes use below code

$HTTP["remoteip"] =~ "192\.168\.1\.*|10\.2\.20\.*" {
    url.access-deny = ( "" )
}

Above code will block 192.168.1.* and 10.2.20.* ip rages and allow all other ip’s.

awk – print last field

 

NF is built in variable in awk which stores Number of fields.

To get last field with awk use following example:

$ echo a b c | awk '{print $NF}'

Output:
c

By default awk will take any white space character(space, tab, …) to split.

To get last but one field with awk:

$ echo a b c | awk '{print $(NF-1)}'

Output:
b

To print only number of fields with awk:

$ echo a b c | awk '{print NF}'

Output:
3

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.